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、根据出错的引用文件,找到

修改如下:

再次执行:

相关推荐
雪的季节6 分钟前
OPENCV学习(补充1)
人工智能·opencv·学习
9i编程11 分钟前
四大准则也还是不靠谱啊:定好了铁律,AI 照样偷懒给你看
人工智能·openai·ai编程
狂僧12 分钟前
从 Markdown、Wiki 到 AI 知识库:本体与知识图谱到底处在哪一层?
人工智能
lf132102715 分钟前
用 JSON Schema 管装修节点记录:从照片台账到可校验工程数据
网络·数据库·人工智能·经验分享·物联网·json·智能家居
watersink19 分钟前
机器学习XGBoost
人工智能·机器学习
新知图书42 分钟前
7.2 上下文记忆的优化与重整合(智能体工程)
人工智能·agent·ai agent·智能体
watersink1 小时前
机器学习极大似然估计与EM算法
人工智能·算法·机器学习
叠层归一研究院1 小时前
如何用程序搭建一个 AGI 种子系统(一):从向量种子到无限生长引擎
人工智能·python·算法·机器学习·agi
名不经传的养虾人1 小时前
从0到1:企业级AI项目迭代日记 Vol.87|记忆链路切换了,系统接管有了质量门
大数据·人工智能·ai编程·企业ai·多agent协作
GlobalInfo1 小时前
2026年显微外科手术机器人系统市场报告:市场规模、产业研究、十五五规划与发展趋势预测
大数据·人工智能·机器人