赞
踩
一、Python环境部署
Python官网:Welcome to Python.org
Python文档下载地址:Our Documentation | Python.org
二、Thonny的安装
安装包地址:Thonny, Python IDE for beginners
三、某PT的Key申请
网页在线体验地址:上谷歌搜索即可
注:得先注册海外的谷歌账号,这些万能的淘宝都能买得到。并且 VPN要开美国服。其他国家的没试,可以尝试下。
获取API的Key方法:
打开它的官网
依次进入API-》 EXAMPLES
选择Q&A
接着打开Open in Playground
右上角就可以看到调用 的API源码
可切换不同的开发语言。
接着点右上方的Personal可以选择View API Keys,查看Key
记得要在创建的时候就要将Key保存下来,否则后期是无法查看Key的。
四、完整的某pt调用例子Python版
4.1问答:
- import openai,os
- start_sequence = "\nA:"
- restart_sequence = "Q: "
- # Replace `<your_api_key>` with your actual OpenAI API key
- openai.api_key = "你的key"
- prompt = " "
- while len(prompt)!=0:
- # Ask a question
- prompt = input(restart_sequence)
- #prompt = "tell me in Chinese:" + input("\n请输入要翻译的内容:")
-
- # Get my answer
- response = openai.Completion.create(
- engine="text-davinci-003",
- prompt=prompt,
- temperature=1,
- max_tokens=2000,
- frequency_penalty=0,
- presence_penalty=0
- )
-
- # Print my answer
- print(start_sequence,response["choices"][0]["text"].strip())
4.2翻译:
- import openai,os
- # Replace `<your_api_key>` 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 `<your_api_key>` 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例子。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。