forked from Anning01/ComicTweets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatgpt.py
More file actions
44 lines (32 loc) · 914 Bytes
/
chatgpt.py
File metadata and controls
44 lines (32 loc) · 914 Bytes
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
40
41
42
43
44
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# @author:anning
# @email:anningforchina@gmail.com
# @time:2024/05/01 23:40
# @file:chatgpt.py
from openai import OpenAI
from load_config import get_yaml_config
config = get_yaml_config()
ForwardKey = config["chatgpt"]["ForwardKey"]
base_url = config["chatgpt"]["url"]
model = config["chatgpt"]["model"]
class Main:
client = OpenAI(
api_key=ForwardKey,
base_url=base_url,
)
def chat(self, query, history):
history += [{
"role": "user",
"content": query
}]
completion = self.client.chat.completions.create(
model=model,
messages=history,
)
result = completion.choices[0].message.content
history += [{
"role": "assistant",
"content": result
}]
return history, result, completion.usage.total_tokens