第六课:ChatGPT 调用
- 电脑具备魔法
- 获取
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()
2. 模块
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()