当前位置:   article > 正文

Java 調用ChatGPT API實例(text-davinci-003)_调用text-davinci-003计费方式

调用text-davinci-003计费方式

1、獲取調用ChatGPT的key

登錄官網https://platform.openai.com/account/api-keysAPI生成一個key(請求token)

2、官方API請求示例查看

請求:

  1. curl https://api.openai.com/v1/completions \
  2. -H "Content-Type: application/json" \
  3. -H "Authorization: Bearer YOUR_API_KEY" \
  4. -d '{"model": "text-davinci-003", "prompt": "Say this is a test", "temperature": 0, "max_tokens": 7}'

響應:

  1. {
  2. "id": "cmpl-GERzeJQ4lvqPk8SkZu4XMIuR",
  3. "object": "text_completion",
  4. "created": 1586839808,
  5. "model": "text-davinci:003",
  6. "choices": [
  7. {
  8. "text": "\n\nThis is indeed a test",
  9. "index": 0,
  10. "logprobs": null,
  11. "finish_reason": "length"
  12. }
  13. ],
  14. "usage": {
  15. "prompt_tokens": 5,        // 提問使用token數
  16. "completion_tokens": 7,    // 回答使用token數
  17. "total_tokens": 12         // 總計token數
  18. }
  19. }

3、Java調用

  1. public static void main(String[] args) throws JsonProcessingException {
  2. ObjectMapper om=new ObjectMapper();
  3. List<String> str=new ArrayList<String>();
  4. str.add(" Human:");
  5. str.add(" AI:");
  6. SendChatGPTVO s=new SendChatGPTVO("text-davinci-003",
  7.         "使用python写一个识别猫猫的程序的步骤",
  8.         2048,0,1,0f,0.6f,str);
  9. RestTemplate restTemplate=new RestTemplate();
  10. HttpHeaders xheaders = new HttpHeaders();
  11. xheaders.setBearerAuth("sk-**********************");
  12. xheaders.setContentType(MediaType.APPLICATION_JSON);
  13. String params = om.writeValueAsString(s);
  14. HttpEntity<String> requestEntity = new HttpEntity<String>(params,xheaders);
  15. JSONObject result = restTemplate.postForObject("https://api.openai.com/v1/completions", requestEntity, JSONObject.class);
  16. System.out.println("result:"+result);
  17. }

4、參數說明  

GPT3模型接口,模型名称为“text-davinci-003”,调用费用为0.02美元/1000tokens,折合下来差不多0.1元400~500字。这个字数包括问题和返回结果字数。

GPT3模型调用方式如下,输入主要有7个参数:

model:模型名称,text-davinci-003

prompt:问题或待补全内容,例如“how are you”。

temperature:控制结果随机性,0.0表示结果固定,随机性大可以设置为0.9。

max_tokens:最大返回字数(包括问题和答案),通常汉字占两个token。假设设置成100,如果prompt问题中有40个汉字,那么返回结果中最多包括10个汉字。

top_p:设置为1即可。

frequency_penalty:设置为0即可。

presence_penalty:设置为0即可。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/348731
推荐阅读
相关标签
  

闽ICP备14008679号