[feature] add gpt-4o, fix openai API version compatibility

Signed-off-by: ltn18 <nguyentamhoanglam@gmail.com>
This commit is contained in:
ltn18 2024-10-09 23:19:30 -04:00
parent ab400345c5
commit b441123518

View File

@ -30,10 +30,10 @@ def chatgpt(messages, model="gpt-4", temperature=0.7, max_tokens=1000, n=1, stop
cnt = min(n, 20) cnt = min(n, 20)
n -= cnt n -= cnt
res = completions_with_backoff(model=model, messages=messages, temperature=temperature, max_tokens=max_tokens, n=cnt, stop=stop) res = completions_with_backoff(model=model, messages=messages, temperature=temperature, max_tokens=max_tokens, n=cnt, stop=stop)
outputs.extend([choice["message"]["content"] for choice in res["choices"]]) outputs.extend([choice.message.content for choice in res.choices])
# log completion tokens # log completion tokens
completion_tokens += res["usage"]["completion_tokens"] completion_tokens += res.usage.completion_tokens
prompt_tokens += res["usage"]["prompt_tokens"] prompt_tokens += res.usage.prompt_tokens
return outputs return outputs
def gpt_usage(backend="gpt-4"): def gpt_usage(backend="gpt-4"):
@ -42,4 +42,6 @@ def gpt_usage(backend="gpt-4"):
cost = completion_tokens / 1000 * 0.06 + prompt_tokens / 1000 * 0.03 cost = completion_tokens / 1000 * 0.06 + prompt_tokens / 1000 * 0.03
elif backend == "gpt-3.5-turbo": elif backend == "gpt-3.5-turbo":
cost = completion_tokens / 1000 * 0.002 + prompt_tokens / 1000 * 0.0015 cost = completion_tokens / 1000 * 0.002 + prompt_tokens / 1000 * 0.0015
elif backend == "gpt-4o":
cost = completion_tokens / 1000 * 0.00250 + prompt_tokens / 1000 * 0.01
return {"completion_tokens": completion_tokens, "prompt_tokens": prompt_tokens, "cost": cost} return {"completion_tokens": completion_tokens, "prompt_tokens": prompt_tokens, "cost": cost}