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

修改如下:

再次执行:

相关推荐
鸿蒙布道师1 小时前
百度Create大会深度解读:AI Agent与多模态模型如何重塑未来?
人工智能·深度学习·神经网络·机器学习·百度·自然语言处理·dubbo
睿途低空新程1 小时前
面向城市治理的AI集群空域融合模型
人工智能·经验分享·其他·无人机
LaughingZhu1 小时前
PH热榜 | 2025-04-26
前端·数据库·人工智能·mysql·开源
海盗儿2 小时前
吴恩达深度学习作业之风格转移Neural Style Transfer (pytorch)
人工智能·计算机视觉
kyle~2 小时前
深度学习---Pytorch概览
人工智能·pytorch·python·深度学习
说私域2 小时前
开源AI智能名片链动2+1模式S2B2C商城小程序源码赋能下的社交电商创业者技能跃迁与价值重构
人工智能·小程序·重构·开源·零售
一点.点3 小时前
自动驾驶(ADAS)领域常用数据集介绍
人工智能·深度学习·机器学习·自动驾驶
智驱力人工智能3 小时前
夏季道路安全的AI革命:节省人力、提升效率
人工智能·安全·边缘计算·视觉算法·视觉分析·智能巡航·人工智能云计算
晓数5 小时前
“平价”微智码初尝试
人工智能·jetbrains
新加坡内哥谈技术5 小时前
MCP:人工智能时代的HTTP?探索AI通信新标准
人工智能·自然语言处理·chatgpt