openai_api笔记
相关资料
doc官方文档
大多数的内容都可以从官方文档的搜索当中找到
https://platform.openai.com/docs/overview
api-reference链接地址:
api调用的问题,以及一些旧版的api的内容,可以在api 应用程序接口 中找到
https://platform.openai.com/docs/api-reference/introduction
api reference
经典的版本使用错误
错误原因:openai.ChatCompletion.create(proj:MAD)
import openai
try:
if self.model_name in support_models:
response = openai.ChatCompletion.create(
model=self.model_name,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
api_key=api_key,
)
gen = response['choices'][0]['message']['content']
return gen
You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.
You can run openai migrate
to automatically upgrade your codebase to use the 1.0.0 interface.
Alternatively, you can pin your installation to the old version, e.g. pip install openai==0.28
A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742
最新的调用代码应该参考官网为https://platform.openai.com/docs/api-reference/streaming [proj:LVSR]
from openai import OpenAI
client = OpenAI(api_key=args.api_key_token)
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Say this is a test"}],
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")