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

闪电发卡3年前ChatGPT5754

新版: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)


相关文章

OpenAI O1模型详解:比GPT-4o更强大的AI能做什么?实用案例分析

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

全面了解ChatGPT Plus的订阅和购买方式

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

ChatGPT 3.5 API Key密钥免费获取方法攻略,如何用更低的价格购买到GPT3.5和4.0API Key

首先,我们得到一个不幸的消息,自2024年4月以来,ChatGPT普通账号不再赠送5美元余额了,这就意味着,我们无法低成本甚至免费地获取到APIKey余额了。当然,如果你的ChatGPT账号还有API...

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

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

购买ChatGPT Plus会员:全面提升你的AI体验

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

如何用ChatGPT生成原创内容:AI书籍编写全攻略

大家好!今天我们来聊聊一个非常有趣的话题:如何用ChatGPT生成原创内容,特别是AI书籍的编写全攻略。随着人工智能技术的不断发展,越来越多的人开始关注如何利用AI工具来提高工作效率和创造力。那么,如...