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

修改如下:

再次执行:

相关推荐
蓝冰露25 分钟前
把chatgpt聊天记录保存下来
chrome·ai·chatgpt·大模型·markdown·chrome devtools
sali-tec5 小时前
C# 基于halcon的视觉工作流-章66 四目匹配
开发语言·人工智能·数码相机·算法·计算机视觉·c#
这张生成的图像能检测吗5 小时前
(论文速读)ParaDiffusion:基于信息扩散模型的段落到图像生成
人工智能·机器学习·计算机视觉·文生图·图像生成·视觉语言模型
新程记5 小时前
2025年,上海CAIE认证报考指南:把握AI机遇的实用起点
人工智能·百度
unicrom_深圳市由你创科技6 小时前
汽修AI智能体V1.0——从模型微调到应用部署
人工智能
路边草随风6 小时前
milvus向量数据库使用尝试
人工智能·python·milvus
irizhao6 小时前
基于深度学习的智能停车场系统设计与实现
人工智能·深度学习
九河云7 小时前
华为云 ECS 弹性伸缩技术:应对业务峰值的算力动态调度策略
大数据·服务器·人工智能·物联网·华为云
IT空门:门主8 小时前
Spring AI的教程,持续更新......
java·人工智能·spring·spring ai
美狐美颜SDK开放平台8 小时前
美颜sdk是什么?如何将美颜SDK接入安卓/iOS直播平台?
人工智能·美颜sdk·直播美颜sdk·美颜api·美狐美颜sdk