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

闪电发卡3年前ChatGPT5643

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


相关文章

如何选择适合你的GPT 4.0或4.5版本:购买指南

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

ChatGPT Plus 4.0跨境购买指南:不同地区价格与支付方式对比

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

怎样使用ChatGPT实现财务自由?国内使用ChatGPT的方法教程

相信很多人最近刷到利用ChatGPT赚钱的视频,那么到底是真的还是假的呢?我大致给大家分了两大种类型,请大家看下我分析的是否正确。ChatGPTChatGPT是一种基于自然语言处理技术的人工智能模型,...

GPT4.0独享账号vs共享账号:性能差异及使用体验详解

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

GPT o1 Pro共享账号性价比高吗?购买前你该了解的事实

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

OpenAI O3模型使用指南:企业级AI助手如何提升工作效率

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