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

修改如下:

再次执行:

相关推荐
m4Rk_2 小时前
【论文阅读】Agent 记忆机制(83):Inside Out——用可演化 PersonaTree 构建 Agent 的核心长期记忆
论文阅读·人工智能·学习·开源·github
运行时异常2 小时前
【WMS 仓储系统集成 AI Agent 实战】第 8 讲(终篇):生产部署与并发安全——Semaphore 放进 Flux.defer 的坑,压测抓了一晚上
人工智能·安全
杨杨杨大侠2 小时前
Jev、Kev、Laya:决策模型怎么选,什么时候需要微调?
人工智能·python·agent
代码方舟2 小时前
零信任架构实战:基于天远人企关联构建自动化供应链金融网关
运维·人工智能·架构·自动化
虹科网络安全3 小时前
“顶会”看安全(十八):超越越狱:揭示由能力边界模糊引发的 LLM 应用安全风险
人工智能·安全
Maiko Star3 小时前
* LangChain 提示词模板详解:ChatPromptTemplate 的使用与高级特性
java·人工智能·langchain
鬓戈3 小时前
Rust 语言与 AI 应用生态调研及学习路径
人工智能·学习·rust
天远API3 小时前
零信任架构实战:基于天远人企关联构建自动化图谱网关
网络·人工智能·架构·自动化
爱吃提升3 小时前
文生视频模型发展趋势(2026)
人工智能·音视频
AIGS0013 小时前
什么是本体语义平台?和知识图谱、传统数据中台的区别在哪
人工智能·知识图谱·数据中台·ai问数·本体语义平台·智能数据中台