Python 调用ChatGPT API 接口介绍

ChatGPT 介绍

  • platform.openai.com/examples
  • platform.openai.com/docs/api-re…

ChatGPT能够完成chat,生成图片,辨认关键,改错等等功用,本文简单介绍怎么运用python调用ChatGPT API 接口。

Python 调用ChatGPT API 接口介绍

1. 生成API Key

从openai官网网址:platform.openai.com/account/api…,生成咱们的API key

Python 调用ChatGPT API 接口介绍

取得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

运行作用:

Python 调用ChatGPT API 接口介绍