openAIchatGPTapi调用方法
1.申请key
可以在openai官网申请
2.安装openai APK # 我是使用的python pip install openai
3.调用api import openai openai.api_key = "sk-xxx" # 分三种角色system、assistant、user # system的作用是设定助手的行为,可以填入chatgpt-prompts # user是最终的用户输入 # assistant是当chatGPT忘记了上下文,就可以用来存储先前的响应,给chatGPT提供所需的行为实例 response=openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role":"user","content":"怎么申请visa信用卡"}]) print(response["choices"][0]["message"]["content"]) #响应的格式 { "id": "chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve", "object": "chat.completion", "created": 1677649420, "model": "gpt-3.5-turbo", "usage": {"prompt_tokens": 56, "completion_tokens": 31, "total_tokens": 87}, "choices": [ { "message": { "role": "assistant", "content": "The 2020 World Series was played in Arlington, Texas at the Globe Life Field, which was the new home stadium for the Texas Rangers."}, "finish_reason": "stop", "index": 0 } ] }
4.api调用计费情况
刚开始会有免费试用阶段到6月1号
申请成为付费用户,需要添加信用卡
差不多7.5万字2美元,价格还可以