一、Python环境部署
参考Python 环境搭建 | 菜鸟教程
Python官网:Welcome to Python.org
Python文档下载地址:Our Documentation | Python.org
二、Thonny的安装
安装包地址:Thonny, Python IDE for beginners
三、ChatGPT的Key申请
网页在线体验地址:https://chat.openai.com/chat
注:得先注册海外的谷歌账号,这些万能的淘宝都能买得到。并且 VPN要开美国服。其他国家的没试,可以尝试下。
获取API的Key方法:
打开Opanai官网OpenAI
依次进入API-》 EXAMPLES
选择Q&A
接着打开Open in Playground
右上角就可以看到调用 的API源码
可切换不同的开发语言。
接着点右上方的Personal可以选择View API Keys,查看Key
记得要在创建的时候就要将Key保存下来,否则后期是无法查看Key的。
四、完整的ChatGpt调用例子Python版
参考:如何注册体验ChatGPT,用于Ai回答、翻译和写作 - 知乎
4.1问答:
import openai,os
start_sequence = "\nA:"
restart_sequence = "Q: "
# Replace `` with your actual OpenAI API key
openai.api_key = "你的key"
prompt = " "
while len(prompt)!=0:# Ask a questionprompt = input(restart_sequence)#prompt = "tell me in Chinese:" + input("\n请输入要翻译的内容:")# Get my answerresponse = openai.Completion.create(engine="text-davinci-003",prompt=prompt,temperature=1,max_tokens=2000,frequency_penalty=0,presence_penalty=0)# Print my answerprint(start_sequence,response["choices"][0]["text"].strip())
4.2翻译:
import openai,os
# Replace `` with your actual OpenAI API key
openai.api_key = ""
# Ask a question
#prompt = "\nQ::" + sys.argv[1]
prompt = "translate this into 1.English,2.Japanese,3.German:" + input("请输入要翻译的内容:")# Get my answer
response = openai.Completion.create(engine="text-davinci-003",prompt=prompt,temperature=1,max_tokens=200,frequency_penalty=0,presence_penalty=0)# Print my answer 英语、日语、德语
print(response["choices"][0]["text"])
4.3纠错:
import os
import openai
# Replace `` with your actual OpenAI API key
openai.api_key = ""
prompt = " "
while len(prompt)!=0:response = openai.Completion.create(model="text-davinci-003",prompt= "Correct this to standard English:"+input(""),temperature=0,max_tokens=100,top_p=1,frequency_penalty=0,presence_penalty=0)print(response["choices"][0]["text"].strip())
五、后面再出份JS版本的调用API例子。
提供API调用的Key
https://download.csdn.net/download/Highning0007/87431577