Python 调用ChatGPT API 接口介绍
ChatGPT 介绍
- platform.openai.com/examples
- platform.openai.com/docs/api-re…
ChatGPT可以实现chat,生成图片,识别关键,改错等等功能,本文简单介绍如何使用python调用ChatGPT API 接口。
1. 生成API Key
从openai官网网址:platform.openai.com/account/api…,生成我们的API key:
获得key后我们就可以调用API接口了。
2. 安装openai
使用pip安装openai库,命令: pip install openai。
安装openai库: pip install openai
3. python代码调用API
3.1 主要步骤
# 调用openai api的步骤 # 1. 安装openai库 pip install openai # 2. 设置openai的api_key # 3. 调用openai的api # 4. 参考文档 # https://platform.openai.com/docs/api-reference/completions/create # https://platform.openai.com/docs/api-reference/authentication # https://platform.openai.com/docs/api-reference/completions/create # https://platform.openai.com/docs/libraries/community-libraries
3.2 代码示例
API可以实现chat,生成图片,识别关键,改错等等功能。 下面是主要示例代码。 注意:openai.api_key = "sk-xxxFQ" #要更换成自已的API KEY
#!/usr/local/bin/python3.8 # -*- coding: utf8 -*- # 调用openai api的步骤 # 1. 安装openai库 pip install openai # 2. 设置openai的api_key # 3. 调用openai的api # 4. 参考文档 # https://platform.openai.com/docs/api-reference/completions/create # https://platform.openai.com/docs/api-reference/authentication # https://platform.openai.com/docs/api-reference/completions/create # https://platform.openai.com/docs/libraries/community-libraries import os import openai import json # 1. 准备好请求的url #openai.organization = "YOUR_ORG_ID" # #openai.api_key = os.getenv("OPENAI_API_KEY") openai.api_key = "sk-xxxFQ" #要更换成自已的API KEY # 查看可以使用的模型列表 def get_model_list(): models= openai.Model.list() print(models) # 生成文本示例 def generate_text(prompt): completions = openai.Completion.create( engine="text-davinci-002", prompt=prompt, max_tokens=1024, n=1, stop=None, temperature=0.5, ) message = completions.choices[0].text return message.strip() # 调用openai 画图示例 def generate_image(prompt): response = openai.Image.create( prompt = prompt, n=1, size="512x512" ) image_url = response["data"
][0]["url"] return image_url # 调用openai 问答示例 def chat(prompt): response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content":prompt} ] ) answer = response.choices[0].message.content return answer # 调用openai 改正错词输出正确句子 def correct(): prompt="改正错词输出正确句子:\n\n我在京东电商平台买了苹果耳几和华为体脂称" #建议prompt: 改正错词输出正确句子:\n\n input_sentence response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content":prompt} ] ) answer = response.choices[0].message.content return answer # 调用openai 识别关键词 def keyword(): prompt="对下面内容识别2个关键词,每个词字数不超过3个字:\n\n齐选汽车挂件车内挂饰车载后视镜吊坠高档实心黄铜玉石出入平安保男女 红流苏-玉髓平安扣" #建议prompt: 对下面内容识别n个关键词,每个词字数不超过m个字:\n\n input data response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content":prompt} ] ) answer = response.choices[0].message.content return answer # 抽取文本向量 (Embedding) def embedding(): content = "苹果手机" response = openai.Embedding.create( model="text-embedding-ada-002", input=content ) answer = response.data[0].embedding return answer def api_test(): # 测试chat # prompt = "人口最多的国家?" # response = chat(prompt) # print(response) # # 测试generate_text # prompt = "Hello, how are you today?" # response = generate_text(prompt) # print(response) # 测试generate_image #prompt = "a delicious dessert" #response = generate_image(prompt) #print(response) # 测试correct # response = correct() # print(response) #输出结果: 我在京东电商平台买了苹果耳机和华为体脂秤。 # 测试keyword #response = keyword() #print(response) #输出结果: 挂件、平安扣 # 测试embedding result = embedding() print(len(result)) print(result) if __name__ == "__main__": api_test()
4. flask实现chat效果的示例
github.com/openai/open…
下载git代码:
git clone https://github.com/openai/openai-quickstart-python.git cd openai-quickstart-python cp .env.example .env python -m venv venv . venv/bin/activate pip install -r requirements.txt flask run
运行效果:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo6@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。版权声明:如无特殊标注,文章均为本站原创,转载时请以链接形式注明文章出处。
评论