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

闪电发卡2年前ChatGPT4873

新版: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,谷歌正式推出Bard!结果...

反击ChatGPT,谷歌正式推出Bard!结果...

1、谷歌发大招这段时间,一直在看大神相互打架。OpenAI 宣布发布 GPT-4,百度发布了文心一言,微软深夜放了一个炸弹发布 Copilot。紧接着到昨天晚上,谷歌终于发大招发布了 Bar...

ChatGPT 与 GPT:它们有何不同?

尽管术语 ChatGPT 和 GPT 都用于谈论生成式预训练 Transformer,但仍需要考虑显着的技术差异ChatGPT和GPT都是OpenAI推出的自然语言处理工具,但它们在技术能力和定价上有...

如何购买ChatGPT Plus 4.0会员账号:国内用户的完整指南

闪电发卡ChatGPT产品推荐: ChatGPT独享账号 ChatGPT Plus 4.0独享共享账号购买代充 ChatGPT APIKey 3.5和4.0购买充值(直连+...

ChatGPT Pro 4.5账号购买与共享的利与弊,哪种方式更适合你?

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

为什么要选择ChatGPT Pro?从深度研究揭示答案

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

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。