干货!将ChatGPT对接到你自己的程序~

552 阅读1分钟

image.png

前言

ChatGPT的火热不用我来介绍大家也知道,功能十分强大,这样一款强大的Ai,如果能够对接到我们自己的产品当中,一定能吸引不少人的眼球,同时也能增强我们的产品体验~,那么我们应该如何将ChatGPT对接到我们的程序当中呢?

官方Api(需要绑定国外信用卡付费)

OpenAi官方GPT3.5模型Api文档

  1. 注册OpenAi账号(需国外手机号接码验证)
  2. 创建自己的API Key
  3. 官网提供了封装的库,可以快速使用:

Python安装库进行使用:

 pip install openai
import openai
openai.api_key = "你的APIKEY"

completion = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "user", "content": "Hello!"}
  ]
)

print(completion.choices[0].message)

Nodejs安装库进行使用:

npm install openai
const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
  apiKey: "你的APIKEY",
});
const openai = new OpenAIApi(configuration);

const completion = await openai.createChatCompletion({
  model: "gpt-3.5-turbo",
  messages: [{role: "user", content: "Hello world"}],
});
console.log(completion.data.choices[0].message);

国内第三方Api(更便宜,支付宝微信付款)

小M智能对话助手:https://ai.mxcks.com 这是一家国内镜像的ChatGPT,并且将ChatGPT GPT3.5的接口使用开放API的形式公开,实测使用价格比官方API便宜很多

Nodejs使用

import axios from "axios";

const options = {
  method: 'POST',
  url: 'https://api.ai.mxcks.com/api/userApis/sendMessage',
  params: {token: '你的用户密钥'},
  headers: {'content-type': 'application/json'},
  data: {message: '你好,你叫什么名字?', systemMessage: '你是智能助手张三'}
};

axios.request(options).then(function (response) {
  console.log(response.data);
}).catch(function (error) {
  console.error(error);
});

Python使用

import requests

url = "https://api.ai.mxcks.com/api/userApis/sendMessage"

querystring = {"token":"你的用户密钥"}

payload = {
    "message": "你好,你叫什么名字?",
    "systemMessage": "你是智能助手张三"
}
headers = {"content-type": "application/json"}

response = requests.request("POST", url, json=payload, headers=headers, params=querystring)

print(response.text)

部分接口文档:

image.png

image.png