-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpt.py
More file actions
39 lines (32 loc) · 1.45 KB
/
Copy pathgpt.py
File metadata and controls
39 lines (32 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from openai import OpenAI
import httpx as httpx
class ChatGptService:
client: OpenAI = None
message_list: list = None
def __init__(self, token):
token = "sk-proj-" + token[:3:-1] if token.startswith('gpt:') else token
self.client = OpenAI(
http_client=httpx.Client(proxies="http://18.199.183.77:49232"),
api_key=token)
self.message_list = []
async def send_message_list(self) -> str:
completion = self.client.chat.completions.create(
model="gpt-3.5-turbo", # gpt-4o, gpt-4-turbo, gpt-3.5-turbo, GPT-4o mini
messages=self.message_list,
max_tokens=3000,
temperature=0.9
)
message = completion.choices[0].message
self.message_list.append(message)
return message.content
def set_prompt(self, prompt_text: str) -> None:
self.message_list.clear()
self.message_list.append({"role": "system", "content": prompt_text})
async def add_message(self, message_text: str) -> str:
self.message_list.append({"role": "user", "content": message_text})
return await self.send_message_list()
async def send_question(self, prompt_text: str, message_text: str) -> str:
self.message_list.clear()
self.message_list.append({"role": "system", "content": prompt_text})
self.message_list.append({"role": "user", "content": message_text})
return await self.send_message_list()