chatgpt api使用途径 ChatGPT的API调用方式总结

AI资讯1年前 (2023)发布 fengdao
38 0

的 API调用方式有多种,有基于SDK和HTTP的调用方式,也有流式和非流式的调用方式,接下来将分别举例说明。

本文示例的普通模型是text–003模型聊天模型是gpt-3.5-turbo,可以在官网查看更多模型介绍:

示例中的参数是是设置回答的随机性,取值0,1,值越大每次回答内容越随机。

基于SDK

基于SDK的方式调用,需要设置环境变量,或者在代码中设置. = .

普通模型-API调用

chatgpt api使用途径_途径软件_途径信息是什么意思

model = "text-davinci-003"
def openai_sdk_http_api(content):
    response = openai.Completion.create(
        model=model,
        prompt=content,
        temperature=0.8,
    )
    answer = response.choices[0].text.strip()
    return answer

复制

普通模型-API流式调用

model = "text-davinci-003"
def openai_sdk_stream_http_api(prompt):
    response = openai.Completion.create(
        model=model,
        prompt=prompt,
        stream=True
    )
    for message in response:
        print(message.choices[0].text.strip(), end='', flush=True)

复制

途径软件_途径信息是什么意思_chatgpt api使用途径

聊天模型-API调用

model = "gpt-3.5-turbo"
def openai_sdk_chat_http_api(message):
    response = openai.ChatCompletion.create(
        model=model,
        messages=[{"role": "user", "content": message}],
        temperature=0.8,
        top_p=1,
        presence_penalty=1,
    )
    answer = response.choices[0].message.content.strip()
    return answer

复制

聊天模型-API流式调用

model = "gpt-3.5-turbo"
def openai_sdk_stream_chat_http_api(content):
    response = openai.ChatCompletion.create(
        model=model,
        messages=[{"role": "user", "content": content}],
        temperature=0.8,
        stream=True
    )
    i = 0
    for chunk in response:
        if i > 0 and chunk.choices[0].delta['content']:
            print(chunk.choices[0].delta.content, end='', flush=True)
        i += 1
    return

复制

© 版权声明

相关文章

暂无评论

暂无评论...