lemon-little / Dashscope_to_openai

将阿里巴巴开源模型的Dashscope api 转换成Openai格式
6 stars 0 forks source link

这个好像不是最新的openai调用方式,运行会报错 #1

Open luodeb opened 6 months ago

luodeb commented 6 months ago
import openai

# optional; defaults to `os.environ['OPENAI_API_KEY']`
openai.api_key = '...'

# all client options can be configured just like the `OpenAI` instantiation counterpart
openai.base_url = "https://..."
openai.default_headers = {"x-foo": "true"}

completion = openai.chat.completions.create(
    model="gpt-4",
    messages=[
        {
            "role": "user",
            "content": "How do I output all files in a directory using Python?",
        },
    ],
)
print(completion.choices[0].message.content)

最新的调用方式OpenAI

lemon-little commented 6 months ago

是的,我的openai==0.28.0

lemon-little commented 6 months ago
import openai

# optional; defaults to `os.environ['OPENAI_API_KEY']`
openai.api_key = '...'

# all client options can be configured just like the `OpenAI` instantiation counterpart
openai.base_url = "https://..."
openai.default_headers = {"x-foo": "true"}

completion = openai.chat.completions.create(
    model="gpt-4",
    messages=[
        {
            "role": "user",
            "content": "How do I output all files in a directory using Python?",
        },
    ],
)
print(completion.choices[0].message.content)

最新的调用方式OpenAI

  1. 目前,我在windows电脑上运行非流式输出也会报错,但在mac上运行非流式就不会报错,具体原因是我在windows 上create_chat_complete中调用dashscope.Generation.call函数设置result_format="message"后函数返回格式为非message,这个原因目前我也不太清楚。
  2. 当openai==0.28.0时,流式输出在windows和mac上依旧能够运行
  3. 使用这个代码需要更改dashscope_to_openai.py内的api key,其次,在测试api的脚本中,需要设置openai.api_base=="http://localhost:8000/v1" openai.api_key="none"
  4. 如果需要对最新版本的openai支持,建议重写脚本,可参考github上claude_to_chatgpt(最好写完借我也用用2333)
  5. 如果想要更改代码,可查看create_chat_complete函数和predict函数,这是非流式输出和流式输出的主要代码