Call chatgpt's api, three things you must know

Uncle Tooth Tutorial is easy to understand

The code to call the api

let url = "https://api.openai.com/v1/completions";
let answer = await axios // use axios to send post request
  .post(url, data, { headers: headers })
  .then((res) => {
    return res.data.choices[0].text.trim();
  })
  .catch((err) => {
    console.log(err.response.data);
    return "error";
  });

It's very simple, it's a post request

Instructions for calling api

character limit

Question+answer<4000, the unit is token; one English character counts as 1 token, and one Chinese character counts as 2 tokens;

That is to say, the total number of words in questions and answers should not exceed 2,000 Chinese characters and 4,000 English words;

token price

Generally, the model everyone uses is Davinci, $0.02 per token

The token price of the image model

Token price of other products

what is token

You just think that a token is an English word.

One Chinese character counts as 2 tokens

How to calculate the price for a question

The translation is:

The tokens spent for a question = question tokens + answer tokens

Crack the character limit

The question should not exceed 1000 characters, and the excess characters will be discarded; leave 1000 characters for the answer, ;

According to your actual situation, decide to discard those words, the ratio of the number of words in questions and answers,

For example, if you want an answer of 2000 words,

Then ask a question of 100 words, AI can answer up to 1900, which is almost 2000 words

if (data.prompt.length > 1000) {
  data.prompt = data.prompt.slice(-1000);
}

Is there any context

For example, you asked AI two questions, and then you asked AI: What was my last question?

AI Answer: XXXX,

If he answers correctly, it means that there is context, and if he answers wrong, it means that there is no context;

create context

When post submits data, submit all previous conversations, so that there is a context;

For example, you save the previous conversations in an array, and when submitting data, use the elements in the array

\n\n

Connected, AI will recognize the content of the conversation you uploaded, and there will be context

getPreviousConversationContent(data) {
  var len = data.length;
  let arr = [];
  for (var i = 0; i < len; i++) {
    let item = data[i];
    arr.push(item.content);
  }
  return arr.join("\n\n");
}

usage time

The difference between China and the United States is 12 to 13 hours. There are many Chinese and many Americans. If there are too many people, the server will be stuck.

Therefore, when we use it at night, the server is prone to report errors, and it is less likely to report errors during the day;

title

In addition to server errors, another common error is the title , which is really big;

This can only be changed;

If you have money, subscribe to the original version; $20 a month;

environment

Device: Mi 11pro
Android version: 12
Autojs version: 9.3.11

celebrity quotes

Ideas are the most important, other Baidu, bing, stackoverflow, github, Android documents, autojs documents, and finally ask in the group --- Uncle Tooth Tutorial

statement

Part of the content comes from the Internet. This tutorial is only for learning, and other purposes are prohibited

Guess you like

Origin blog.csdn.net/snailuncle2/article/details/128962603