Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate the gradio server to openai v1 #3032

Merged
merged 3 commits into from
Feb 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
migrate api_provider
  • Loading branch information
merrymercy committed Feb 10, 2024
commit b3ed05baa4282f1e75b8f631c2b5850f5eafb065
42 changes: 16 additions & 26 deletions fastchat/serve/api_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,18 @@ def openai_api_stream_iter(
):
import openai

is_azure = False
api_key = api_key or os.environ["OPENAI_API_KEY"]

if "azure" in model_name:
is_azure = True
openai.api_type = "azure"
openai.api_version = "2023-07-01-preview"
client = openai.AzureOpenAI(
api_version="2023-07-01-preview",
azure_endpoint=api_base or "https://api.openai.com/v1",
api_key=api_key)
else:
openai.api_type = "open_ai"
openai.api_version = None
client = openai.OpenAI(
base_url=api_base or "https://api.openai.com/v1",
api_key=api_key)

openai.api_base = api_base or "https://api.openai.com/v1"
openai.api_key = api_key or os.environ["OPENAI_API_KEY"]
if model_name == "gpt-4-turbo":
model_name = "gpt-4-1106-preview"

Expand All @@ -123,26 +124,15 @@ def openai_api_stream_iter(
}
logger.info(f"==== request ====\n{gen_params}")

if is_azure:
res = openai.ChatCompletion.create(
engine=model_name,
messages=messages,
temperature=temperature,
max_tokens=max_new_tokens,
stream=True,
)
else:
res = openai.ChatCompletion.create(
model=model_name,
messages=messages,
temperature=temperature,
max_tokens=max_new_tokens,
stream=True,
)
res = client.chat.completions.create(model=model_name,
messages=messages,
temperature=temperature,
max_tokens=max_new_tokens,
stream=True)
text = ""
for chunk in res:
if len(chunk["choices"]) > 0:
text += chunk["choices"][0]["delta"].get("content", "")
if len(chunk.choices) > 0:
text += chunk.choices[0].delta.content or ""
data = {
"text": text,
"error_code": 0,
Expand Down
Loading