Update chatgpt.py

This commit is contained in:
luchun 2023-11-16 22:50:03 +08:00 committed by GitHub
parent 1b615aedd2
commit 2ff9625d3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -170,10 +170,7 @@ def chatgpt_generate_stream(
from openai import OpenAI from openai import OpenAI
client = OpenAI(**openai_params) client = OpenAI(**openai_params)
print("openai_params", openai_params)
print("payloads", payloads)
res = client.chat.completions.create(messages=history, **payloads) res = client.chat.completions.create(messages=history, **payloads)
print(res)
text = "" text = ""
for r in res: for r in res:
if r.choices[0].delta.content is not None: if r.choices[0].delta.content is not None:
@ -209,19 +206,23 @@ async def async_chatgpt_generate_stream(
client = AsyncAzureOpenAI( client = AsyncAzureOpenAI(
api_key=openai_params["api_key"], api_key=openai_params["api_key"],
end_point=openai_params["base_url"],
api_version=api_version, api_version=api_version,
azure_endpoint=os.getenv( azure_endpoint=openai_params[
"AZURE_OPENAI_ENDPOINT" "base_url"
), # Your Azure OpenAI resource's endpoint value. ], # Your Azure OpenAI resource's endpoint value.
) )
else: else:
from openai import AsyncOpenAI from openai import AsyncOpenAI
client = AsyncOpenAI(**openai_params) client = AsyncOpenAI(**openai_params)
res = await client.chat.completions.create(
messages=history, **payloads res = await client.chat.completions.create(messages=history, **payloads)
).model_dump() text = ""
for r in res:
if r.choices[0].delta.content is not None:
content = r.choices[0].delta.content
text += content
yield text
else: else:
import openai import openai
@ -229,9 +230,9 @@ async def async_chatgpt_generate_stream(
res = await openai.ChatCompletion.acreate(messages=history, **payloads) res = await openai.ChatCompletion.acreate(messages=history, **payloads)
text = "" text = ""
async for r in res: async for r in res:
if r["choices"][0]["delta"].get("content") is not None: if r["choices"][0]["delta"].get("content") is not None:
content = r["choices"][0]["delta"]["content"] content = r["choices"][0]["delta"]["content"]
text += content text += content
yield text yield text