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

update OpenAI version to 1.2.3 #592

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion services/openai_api_lm/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ sentry-sdk[flask]==0.14.1
healthcheck==1.3.3
jinja2<=3.0.3
Werkzeug<=2.0.3
openai==0.27.6
openai==1.2.3
27 changes: 11 additions & 16 deletions services/openai_api_lm/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import logging
import os
import time

import openai
import sentry_sdk

from openai import OpenAI
from common.prompts import META_GOALS_PROMPT
from common.universal_templates import GENERATIVE_ROBOT_TEMPLATE
from flask import Flask, request, jsonify
Expand Down Expand Up @@ -38,8 +38,7 @@ def generate_responses(context, openai_api_key, openai_org, prompt, generation_p
outputs = []

assert openai_api_key, logger.error("Error: OpenAI API key is not specified in env")
openai.api_key = openai_api_key
openai.organization = openai_org if openai_org else None
client = OpenAI(api_key=openai_api_key, organization=openai_org if openai_org else None)

if PRETRAINED_MODEL_NAME_OR_PATH in CHAT_COMPLETION_MODELS:
logger.info("Use special chat completion endpoint")
Expand All @@ -55,7 +54,7 @@ def generate_responses(context, openai_api_key, openai_org, prompt, generation_p
for uttr_id, uttr in enumerate(context)
]
logger.info(f"context inside generate_responses seen as: {messages}")
response = openai.ChatCompletion.create(
response = client.chat.completions.create(
model=PRETRAINED_MODEL_NAME_OR_PATH, messages=messages, **generation_params
)
else:
Expand All @@ -69,19 +68,15 @@ def generate_responses(context, openai_api_key, openai_org, prompt, generation_p
else:
dialog_context += "\n".join(context) + f"\n{NAMING[0]}:"
logger.info(f"context inside generate_responses seen as: {dialog_context}")
response = openai.Completion.create(
model=PRETRAINED_MODEL_NAME_OR_PATH,
prompt=dialog_context,
**generation_params,
response = client.completions.create(
model=PRETRAINED_MODEL_NAME_OR_PATH, prompt=dialog_context, **generation_params
)

if isinstance(response, dict) and "choices" in response:
outputs = [
resp["message"]["content"].strip() if "message" in resp else resp.get("text", "").strip()
for resp in response["choices"]
]
elif isinstance(response, str):
outputs = [response.strip()]
response = response.model_dump()
outputs = [
resp["message"]["content"].strip() if "message" in resp else resp.get("text", "").strip()
for resp in response["choices"]
]

if PRETRAINED_MODEL_NAME_OR_PATH not in CHAT_COMPLETION_MODELS:
# post-processing of the responses by all models except of ChatGPT
Expand Down
Loading