前期准备
1、chatGPT api官方文档:
-
https://platform.openai.com/docs/api-reference/
-
https://platform.openai.com/docs/introduction/key-concepts
2、需要获取chatGPT API密钥:申请地址:https://platform.openai.com/account/api-keys
-
chatGPT模型介绍:https://platform.openai.com/docs/models
了解chatGPT的模型有哪些,有助于选择更适合自己方向的模型进行调用。可考虑查看
- chatGPT模型原理:https://zhuanlan.zhihu.com/p/589621442
-
chatGPT 应用例子:https://platform.openai.com/examples
Python 脚本测试
# -*- coding: utf-8 -*-
"""
Created on Fri May 19 11:49:02 2023
@author: Administrator
# 注意:运行时需要翻墙,不然是不能进行的。
"""
import os
import openai
# =============================================================================
# # 设置 API Key,申请地址:https://platform.openai.com/account/api-keys
# =============================================================================
openai.api_key = "自己的chatGPTAPI密钥"
openai.organization = "org-TuohiZkkexN6XwotaKgCWjZd"
# =============================================================================
# # 获取模型列表
# =============================================================================
openai.Model.list()
# 检索模型信息
openai.Model.retrieve("text-davinci-003")
# 信息处理
# chatGPT对内容的处理主要包括三种:Text completion、Code completion和Image generation,分别针对文本、代码和图片
# =============================================================================
# # Completion
# =============================================================================
# 早期版本的核心功能,但升后后chat接口比这个更强,此接口不支持gpt-3.5和gpt-4模型,所以推荐使用后面的chat接口代替。
response = openai.Completion.create(
model="text-davinci-003", #必填,指定功能模型
prompt="Say: hello.", #提示语
temperature=0, #默认值1,采样精度,取值范围为0~2,较高的值(如0.8)将使输出更加随机,而较低的值(例如0.2)将使其更加相关
max_tokens=100, #返回内容的最大长度
top_p=1, #默认值1,采样精度,取值范围为0~2,较高的值(如0.8)将使输出更加随机,而较低的值(例如0.2)将使其更加相关
frequency_penalty=0.0, #-2.0到2.0之间的数字,正值会根据标记在文本中的现有频率对新标记进行惩罚,降低模型重复相同行的可能性
presence_penalty=0.0, #-2.0到2.0之间的数字,正值会根据标记在文本中的现有频率对新标记进行惩罚,降低模型重复相同行的可能性
stop=["\n"],
n = 1, # 默认值1,采样精度,取值范围为0~2,较高的值(如0.8)将使输出更加随机,而较低的值(例如0.2)将使其更加相关
stream = False, #默认值false,是否流式输出内容
echo = False #默认值false,是否回显prompt内容
)
# response = openai.Completion.create(
# model="text-davinci-003",
# prompt="你是谁?",
# temperature=0,
# max_tokens=100,
# top_p=1,
# frequency_penalty=0.0,
# presence_penalty=0.0,
# stop=["\n"]
# )
# =============================================================================
# # chat
# =============================================================================
# 消息的角色只能为system、user和assistant,而且消息你可以只用user角色发,这样效果就和以前的一样。
# 而使用system角色可以指定chatGPT的功能定位,
# 在assistant中传chatGPT回答的消息就实现了上下文关联,需要注意的是不要超了token限制。
# 链接:https://www.zhihu.com/question/575983484/answer/2895862383
a = openai.ChatCompletion.create(
model="gpt-3.5-turbo", #使用的模型ID
messages=[
# {"role": "system", "content": "You are a helpful assistant."},
# {"role": "user", "content": "Who won the world series in 2020?"},
# {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"} #描述对话的消息列表
]
)
a['choices'][0]['message']['content']