前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >手把手做一个公众号GPT智能客服(六)GPT 调用

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

作者头像
Maynor
发布2023-10-10 08:17:34
5060
发布2023-10-10 08:17:34
举报
文章被收录于专栏:最新最全的大数据技术体系

第六课:ChatGPT 调用

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

https://platform.openai.com/overview

image-20230420170043938
image-20230420170043938
image-20230420170225019
image-20230420170225019
代码语言:javascript
复制
2. 查看余额 

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

image-20230420170330874
image-20230420170330874
image-20230420170817226
image-20230420170817226

获取可用的模型列表

代码语言:javascript
复制
1. 接口
代码语言:javascript
复制
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();
代码语言:javascript
复制
2. 模块
代码语言:javascript
复制
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使用量

代码语言:javascript
复制
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();

补全对话

代码语言:javascript
复制
1. 接口
代码语言:javascript
复制
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()
代码语言:javascript
复制
2. 模块
代码语言:javascript
复制
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.接口

代码语言:javascript
复制
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.模块

代码语言:javascript
复制
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()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-10-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 获取可用的模型列表
  • 获取token使用量
  • 补全对话
  • 对话
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档