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

修改如下:

再次执行:

相关推荐
Wanderer X7 分钟前
【ML】rope
人工智能
nVisual9 分钟前
nVisual:AI驱动的智能排障,平均响应时间缩短40%
android·人工智能·缓存
白日做梦Q10 分钟前
PyTorch从零手写CNN实现NEU钢材表面缺陷检测(完整实战·单篇完结)
人工智能·pytorch·cnn
星马梦缘15 分钟前
机器学习与模式识别 第十一章 优化于梯度下降 考点压缩
人工智能·机器学习·梯度下降·hessian·条件数
想会飞的蒲公英17 分钟前
TF-IDF + 随机森林中文文本分类全链路实战:从训练脚本到 Flask API + Streamlit 前端
人工智能·pytorch·python·随机森林·分类·flask·tf-idf
AI服务老曹23 分钟前
摄像机安装AI视频分析参数配置说明:POC现场交付全流程指南
人工智能·音视频
FriendshipT26 分钟前
Ultralytics:解读C3模块
人工智能·pytorch·python·深度学习·目标检测
极光代码工作室27 分钟前
基于深度学习的异常行为检测系统
人工智能·python·深度学习·神经网络·机器学习
阿拉斯攀登30 分钟前
RAG 性能优化:缓存、批量与并发
人工智能·缓存·性能优化·embedding·agent·loop·rag
次旅行的库31 分钟前
解决ValueError: too many values to unpack (expected 2)
linux·人工智能·深度学习·机器学习·推荐算法