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

闪电发卡3年前ChatGPT5663

新版: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 Pro共享账号vs官方订阅:哪种方式更划算?成本效益全面分析

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

ChatGPT Plus和Pro年费会员值不值?使用体验与投资回报分析

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

ChatGPT Pro 4.5 官方会员账号常见问题解答:解决你的所有疑虑

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

ChatGPT技术原理:让AI对话更自然

大家好,我是你们的老朋友,今天我们来聊聊一个非常有趣的话题——ChatGPT技术原理。身处信息时代,我们越来越离不开人工智能,而ChatGPT作为其中一颗闪耀的明珠,大大提升了我们与AI互动的体验。那...

对比ChatGPT 3.5和ChatGPT 4.0,差距真的挺大的!!!

对比ChatGPT 3.5和ChatGPT 4.0,差距真的挺大的!!!

话不多说,直接上图:下面是GPT4的回答,我感觉完全可以通过图灵测试,openAI不这么做可能是不想引起大家的恐慌提问:讲一讲关公战秦琼的故事,为什么关公最后去西天取经了,然后完了又跟朱元璋打天下?C...

ChatGPT性能测试感受分享,以及ChatGPT的使用场景分析

ChatGPT性能测试感受分享,以及ChatGPT的使用场景分析

体验了一下最近火出圈的 ChatGPT,聊聊使用感受。没用之前,看网上直接吹爆,什么“可以自己写代码、debug”,“制定了毁灭人类的计划”。于是赶紧去试了下。结果预期太高,反倒有些失望。别说毁灭人类...