Python调用ChatGPT API接口的用法详解
作者:陶老师运维笔记
ChatGPT可以实现chat,生成图片,识别关键,改错等等功能,本文简单的给大家介绍一下如何使用python调用ChatGPT API接口,感兴趣的小伙伴可以参考一下
Python 调用ChatGPT API 接口介绍
ChatGPT 介绍
- https://platform.openai.com/examples
- https://platform.openai.com/docs/api-reference
ChatGPT可以实现chat,生成图片,识别关键,改错等等功能,本文简单介绍如何使用python调用ChatGPT API 接口。
1. 生成API Key
从openai官网网址:https://platform.openai.com/account/api-keys,生成我们的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效果的示例
https://github.com/openai/openai-quickstart-python
下载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
运行效果:
到此这篇关于Python调用ChatGPT API接口的用法详解的文章就介绍到这了,更多相关Python调用ChatGPT API接口内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!