chatgpt 接口使用(一)

使用api实现功能

参考链接:https://platform.openai.com/examples

安装库:

pip3 install openai

例如:

python 复制代码
import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {
      "role": "system",
      "content": "You will be provided with unstructured data, and your task is to parse it into CSV format."
    },
    {
      "role": "user",
      "content": "There are many fruits that were found on the recently discovered planet Goocrux. There are neoskizzles that grow there, which are purple and taste like candy. There are also loheckles, which are a grayish blue fruit and are very tart, a little bit like a lemon. Pounits are a bright green color and are more savory than sweet. There are also plenty of loopnovas which are a neon pink flavor and taste like cotton candy. Finally, there are fruits called glowls, which have a very sour and bitter taste which is acidic and caustic, and a pale orange tinge to them."
    }
  ],
  temperature=0,
  max_tokens=256
)

print(response)

输出:

json 复制代码
{
  "id": "chatcmpl-7hwS9AKlAdarpkhXq3vHJAqnAETvz",
  "object": "chat.completion",
  "created": 1690707153,
  "model": "gpt-3.5-turbo-0613",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Fruit,Color,Taste\nneoskizzles,purple,candy\nloheckles,grayish blue,tart\npounits,bright green,savory\nloopnovas,neon pink,cotton candy\nglowls,pale orange,sour and bitter"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 161,
    "completion_tokens": 58,
    "total_tokens": 219
  }
}

利用私有数据

参考:

https://github.com/langchain-ai/langchain

https://github.com/techleadhd/chatgpt-retrieval

pip3 install langchain openai chromadb tiktoken unstructured

在chatgpt-retrieval测试:

python 复制代码
python3 chatgpt.py "what is my dog's name"

出现错误:

bash 复制代码
Chroma requires sqlite3 >= 3.35.0

解决方法:

1、安装

bash 复制代码
pip3 install pysqlite3-binary

2、根据出错的引用文件,找到

修改如下:

再次执行:

相关推荐
缘华工业智维2 小时前
工业设备预测性维护:能源成本降低的“隐藏钥匙”?
大数据·网络·人工智能
DooTask官方号3 小时前
跨语言协作新范式:阿里云Qwen-MT与DooTask的翻译技术突破
人工智能·ai·项目管理·机器翻译·dootask
凯禾瑞华养老实训室5 小时前
聚焦生活照护能力培育:老年生活照护实训室建设清单的模块设计与资源整合
大数据·人工智能·科技·ar·vr·智慧养老·智慧健康养老服务与管理
倔强青铜三5 小时前
苦练Python第64天:从零掌握多线程,threading模块全面指南
人工智能·python·面试
格林威5 小时前
偏振相机是否属于不同光谱相机的范围内
图像处理·人工智能·数码相机·计算机视觉·视觉检测·工业相机
A-大程序员5 小时前
【pytorch】合并与分割
人工智能·pytorch·深度学习
AI新兵6 小时前
AI大事记12:Transformer 架构——重塑 NLP 的革命性技术(上)
人工智能·自然语言处理·transformer
Dongsheng_20196 小时前
【汽车篇】AI深度学习在汽车零部件外观检测——刹车片中的应用
人工智能·汽车
LONGZETECH6 小时前
【龙泽科技】汽车转向悬架与制动安全系统技术1+X仿真教学软件(1.2.3 -初级)
人工智能·科技·汽车·汽车仿真教学软件·汽车教学软件
JAVA学习通7 小时前
PostgreSQL 的 hstore、arrays 数据类型
人工智能·自然语言处理