Python使用OpenAI官方库调用ChatGPT转发API调用示例(新版+旧版)

闪电发卡2年前ChatGPT5532

新版:OpenAI库1.0.0以上

调用参数解释:https://www.chatgptzh.com/post/12.html


from openai import OpenAI

client = OpenAI(
    # 输入转发API Key,注意转发地址需要加/v1
    api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxx",
    base_url="https://api.wumingai.com/v1"   
)

completion = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "你是谁!"}
    ],
    stream=True  # 是否开启流式输出
)

# 非流式输出获取结果
# print(completion.choices[0].message)
# 流式输出获取结果
for chunk in completion:
    print(chunk.choices[0].delta)


旧版:


import openai

# 输入转发API Key,注意转发地址需要加/v1
openai.api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
openai.api_base = "https://api.wumingai/v1"


response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",  
    messages="hello",
    max_tokens=2000,
    n=1,
    stop=None,
    temperature=0.5,
)

response_content = response.choices[0].message.content
print(response_content)


相关文章

ChatGPT API技术教程OpenAI APIKey在线对接-Chat Completion对象

闪电发卡ChatGPT产品推荐:ChatGPT独享账号:https://www.chatgptzh.com/post/86.htmlChatGPT Plus独享共享账号购买代充:https://www...

什么是ChatGPT?ChatGPT有什么应用场景?ChatGPT的优点和限制

 一、什么是ChatGPTChatGPT是一种基于GPT模型的聊天机器人框架,能够通过学习和模仿人类对话的方式进行自然语言处理和生成,从而实现高质量的聊天交互体验。它采用了开源的transf...

ChatGPT Pro共享账号购买安全吗?我们为你解答

闪电发卡ChatGPT和Claude产品推荐: ChatGPT Pro共享账号 ChatGPT Pro独享账号 ChatGPT独享账号 Claude Pr...

怎样使用Sora?Sora产品介绍和使用教程攻略

怎样使用Sora?Sora产品介绍和使用教程攻略

Sora 是什么? 2024年2月16日,OpenAI 在其官网上面正式宣布推出文本生成视频的大模型 Sora: openai.com/sora (PS:目前 openai 官方还未开放 so...

OpenAI停止不支持国家API调用对国内从业者的影响

闪电发卡ChatGPT产品推荐:ChatGPT独享账号:https://www.chatgptzh.com/post/86.htmlChatGPT Plus独享共享账号购买代充:https://www...

最新ChatGPT国内镜像中文版下载与安装步骤

闪电发卡ChatGPT产品推荐:ChatGPT独享账号:https://www.chatgptzh.com/post/86.htmlChatGPT Plus独享共享账号购买代充:https://www...