diff --git a/fastchat/llm_judge/common.py b/fastchat/llm_judge/common.py index ad1180034..064056a5c 100644 --- a/fastchat/llm_judge/common.py +++ b/fastchat/llm_judge/common.py @@ -11,7 +11,8 @@ import time from typing import Optional -import openai +from openai import OpenAI, OpenAIError + import anthropic from fastchat.model.model_adapter import get_conversation_template @@ -398,20 +399,21 @@ def play_a_match_pair(match: MatchPair, output_file: str): def chat_compeletion_openai(model, conv, temperature, max_tokens): + client = OpenAI() output = API_ERROR_OUTPUT for _ in range(API_MAX_RETRY): try: messages = conv.to_openai_api_messages() - response = openai.ChatCompletion.create( + response = client.chat.completions.create( model=model, messages=messages, n=1, temperature=temperature, - max_tokens=max_tokens, + max_tokens=max_tokens ) - output = response["choices"][0]["message"]["content"] + output = response.choices[0].message.content break - except openai.error.OpenAIError as e: + except OpenAIError as e: print(type(e), e) time.sleep(API_RETRY_SLEEP) diff --git a/fastchat/llm_judge/gen_api_answer.py b/fastchat/llm_judge/gen_api_answer.py index 151acd2d4..6382527b3 100644 --- a/fastchat/llm_judge/gen_api_answer.py +++ b/fastchat/llm_judge/gen_api_answer.py @@ -114,7 +114,7 @@ def get_answer( args = parser.parse_args() if args.openai_api_base is not None: - openai.api_base = args.openai_api_base + raise ValueError("The 'openai.api_base' option is not available in openai>=1.0, pass it when you instantiate the client, e.g. 'OpenAI(base_url=args.openai_api_base)") question_file = f"data/{args.bench_name}/question.jsonl" questions = load_questions(question_file, args.question_begin, args.question_end)