mirror of
https://github.com/Kakune55/PyGetGPT.git
synced 2025-05-06 18:29:24 +08:00
28 lines
1.0 KiB
Python
28 lines
1.0 KiB
Python
import openai , config
|
|
|
|
openai.api_key = config.readConf()["gpt4.0turbo"]["Authorization"]
|
|
openai.base_url = config.readConf()["gpt4.0turbo"]["url"]
|
|
|
|
def service(prompt,history = ""):
|
|
if history == "":
|
|
response = openai.chat.completions.create(
|
|
model="gpt-4-1106-preview",
|
|
messages=[
|
|
{"role": "user", "content": prompt},
|
|
]
|
|
)
|
|
else:
|
|
response = openai.chat.completions.create(
|
|
model="gpt-4-1106-preview",
|
|
messages=[
|
|
{"role": "user", "content": history[1]["user"]},
|
|
{"role": "assistant", "content": history[1]["bot"]},
|
|
{"role": "user", "content": history[0]["user"]},
|
|
{"role": "assistant", "content": history[0]["bot"]},
|
|
{"role": "user", "content": prompt},
|
|
]
|
|
)
|
|
if response.choices[0].finish_reason == "stop":
|
|
return 200, response.choices[0].message.content, int(response.usage.total_tokens*45) #45倍tokens消耗
|
|
else:
|
|
return 50 , "API Error!", 0 |