手把手做一个公众号GPT智能客服(六)GPT 调用

简介: 手把手做一个公众号GPT智能客服(六)GPT 调用

第六课:ChatGPT 调用

  1. 电脑具备魔法
  2. 获取

https://platform.openai.com/overview

2. 查看余额 

一般情况下注册的新用户会赠送一定美金的使用量,现在大多数是5美金,chatgpt 根据模型不同,按照使用的token数进行收费

获取可用的模型列表

1. 接口
const axios = require("axios");
const ApiKey = "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";
async function getModelList() {
  const url = "https://api.openai.com/v1/models";
  const headers = {
    "Content-Type": "application/json",
    Authorization: `Bearer ${ApiKey}`,
  };
  const res = await axios.get(url, {headers});
  console.log(res.data)
}
getModelList();
2. 模块
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
  apiKey: ApiKey,
});
const openai = new OpenAIApi(configuration);
async function getModalList() {
  const res = await openai.listModels();
  console.log(res.data)
}
getModalList();

获取token使用量

function formatDate() {
  const today = new Date()
  const year = today.getFullYear()
  const month = today.getMonth() + 1
  const lastDay = new Date(year, month, 0)
  const formattedFirstDay = `${year}-${month.toString().padStart(2, '0')}-01`
  const formattedLastDay = `${year}-${month.toString().padStart(2, '0')}-${lastDay.getDate().toString().padStart(2, '0')}`
  return [formattedFirstDay, formattedLastDay]
}
async function  getUsage() {
  const [startDate, endDate] = formatDate()
  const url = `https://api.openai.com/v1/dashboard/billing/usage?start_date=${startDate}&end_date=${endDate}`
    const headers = {
    "Content-Type": "application/json",
    Authorization: `Bearer ${ApiKey}`,
  };
  const res = await axios.get(url, {headers})
  console.log(res.data)
}
getUsage();

补全对话

1. 接口
async function completions() {
  const url = "https://api.openai.com/v1/completions"
  const headers = {
    "Content-Type": "application/json",
    Authorization: `Bearer ${ApiKey}`,
  };
  const data = {
    model: "text-davinci-003",
    prompt: "说一下chatgpt",
    max_tokens: 1000,
    temperature: 0,
    // stream: true
  };
  const res = await axios.post(url,data,{ headers })
  console.log(res.data)
}
completions()
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
  apiKey: ApiKey,
});
const openai = new OpenAIApi(configuration);
async function completions() {
  const response = await openai.createCompletion({
    model: "text-davinci-003",
    prompt: "你是谁?",
    max_tokens: 20,
    temperature: 0,
  });
  console.log(response.data)
}
completions()

对话

1.接口

async function chat() {
  const url = "https://api.openai.com/v1/chat/completions";
  const headers = {
    'Content-Type': 'application/json',
    Authorization: `Bearer ${ApiKey}`
  }
  const body  = {
    model: "gpt-3.5-turbo",
    messages: [
      { role: "system", content: '你是一个英语老师'},
      { role: "user", content: "把我下面的内容翻译为英文" },
      { role: "user", content: "你好" },
      { role: "user", content: "今天的雨下的可真大" },
      { role: "user", content: "晚饭吃什么呢?" },
    ],
    temperature: 0,
    stream: false
  }
  const res = await axios.post(url, body, { headers })
  // console.log(res.data)
  console.log(res.data.choices)
}
chat()

2.模块

const str =
  "你是一名英语老师,批改英语作业,告诉作者哪里不好,哪里可以改进,所有的回复都使用中文";
const home = `My Dream
Everyone has a dream, and so do I. My dream is to become an accomplished writer and share my ideas with the world.
Since childhood, I have been fascinated by books and the power of words. I spent countless hours reading novels, poems, and essays from various writers, and I was amazed at how they could use language to express their emotions and convey their messages. As I grew older, I started writing stories and essays myself, and I discovered that writing was not only a way to express myself but also a way to connect with others.
Now, as I pursue my dream of becoming a writer, I am faced with challenges and obstacles. Writing is a demanding craft that requires discipline, patience, and creativity. Some days I struggle to find the right words or the right ideas, and other days I face rejection and criticism. But every setback is a lesson, and every challenge is an opportunity to grow.
To achieve my dream, I know I must work hard and never give up. I read extensively and write every day, honing my skills and exploring new styles and genres. I also seek feedback from other writers and readers, learning from their perspectives and insights. And most importantly, I stay true to myself and my vision, always striving to write with authenticity and integrity.
In conclusion, my dream of becoming a writer may seem daunting, but I believe that anything is possible if I work hard and stay committed. With perseverance and passion, I hope to one day create works that inspire, entertain, and enlighten others.`;
const { Configuration, OpenAIApi } = require("openai")
const configuration = new Configuration({
  apiKey: ApiKey,
})
const openai = new OpenAIApi(configuration)
async function chat () {
  const completion = await openai.createChatCompletion({
    model: "gpt-3.5-turbo",
    messages: [
      { role: "system", content: str },
      { role: "user", content: "你是谁!" },
      { role: "user", content: "请对我接下来输入的内容进行批改" },
      { role: "user", content: home },
    ],
  })
  console.log(completion.data.choices[0].message)
}
chat()


目录
相关文章
|
2月前
|
存储 人工智能 自然语言处理
轻松改造公众号:10分钟实现智能客服自动化!
在阿里云平台上,仅需10分钟即可将微信公众号(订阅号)升级为AI智能客服,提供7x24小时客户支持,显著提升用户体验。方案包括四步:创建大模型问答应用、搭建微信公众号连接流、引入AI智能客服以及增加私有知识库,确保客服能精准回答复杂咨询,助力业务竞争力提升。整个过程简单快捷,在免费试用额度内费用为零。
73 7
轻松改造公众号:10分钟实现智能客服自动化!
|
2月前
|
人工智能 自然语言处理 数据管理
Step By Step 体验10 分钟在公众号和企微中构建自己的AI客服
为提升用户体验与竞争力,企业纷纷构建AI助手实现7x24小时客户服务。在阿里云平台上,仅需十分钟即可完成AI助手的搭建并发布至微信公众号或企业微信。流程包括创建大模型应用、引入AI助手至微信平台、导入私有知识以增强功能,以及将助手集成至企业微信中。此方案操作简便,文档详尽,可快速打造专属AI助手。但现有方案在错误提示、知识库构建指导及部署流程简化方面仍有待改进。
|
5月前
|
小程序 PHP 数据安全/隐私保护
ThinkPHP内核在线客服系统源码多商户版 对接适用场景(PC+WAP+公众号)
大部分站长都了解美洽系统,就跟这种类似的,可以实现一行代码接入客服,非常舒服,支持无限客服,无限坐席! 私有化源码部署,数据可控,稳定可靠。可自定义版权、logo。支持网页、微信公众号、小程序、App等任何程序对接。 客服数量不限,每个客服又独立管理后台和账户密码。每个账户管理可以添加N个客服并且可以分组。 双向微信模板消息通知。支持商品推送,对客服评价。支持客户分组。 支持设置问候语,进入对话自动发送消息。可只能分配客服和转接
38 2
|
5月前
|
人工智能 机器人 API
Dify 构建一个基于 GPT 的 AI 客服系统
Dify 构建一个基于 GPT 的 AI 客服系统
720 0
|
6月前
|
自然语言处理 安全 机器人
使用RAG-GPT和Ollama搭建智能客服
使用RAG-GPT和Ollama搭建智能客服
274 0
|
6月前
|
自然语言处理 机器人 API
手把手做一个公众号GPT智能客服(七)GPT 接入微信机器人
手把手做一个公众号GPT智能客服(七)GPT 接入微信机器人
187 1
|
6月前
|
自然语言处理 数据可视化 NoSQL
手把手做一个公众号GPT智能客服(五)免费云数据库
手把手做一个公众号GPT智能客服(五)免费云数据库
92 0
|
4月前
|
存储 SQL 数据库
Python 金融编程第二版(GPT 重译)(四)(4)
Python 金融编程第二版(GPT 重译)(四)
45 3
|
4月前
|
存储 NoSQL 索引
Python 金融编程第二版(GPT 重译)(一)(4)
Python 金融编程第二版(GPT 重译)(一)
54 2
|
4月前
|
存储 机器学习/深度学习 关系型数据库
Python 金融编程第二版(GPT 重译)(四)(5)
Python 金融编程第二版(GPT 重译)(四)
30 2