当前位置:   article > 正文

OpenAI API调用实践

openai api

目录

一、环境准备

二、获取OpenAI的API keys

1.登录OpenAI账号进入生成API Keys页面

2.添加支付方式并和持卡信息对应

3.ChatGPT根据不同模型可Token数获取产生价格资费标准

4.完成付费

三、调用OpenAI API示例

1.OpenAI官方参考接入文档

2.通过Java测试例子调用OpenAI API


一、环境准备

序号账号类型用户名密码备注
1谷歌*********个人谷歌账号,无请申请注册
2OpenAI *********可通过谷歌账号申请
3VPN*********科学上网
4信用卡(美国)*********可通过第三方软件申请虚拟信用卡

ChatGPT国内无法直接访问,需借助科学上网,本地提供的方法只供参考并不保证安全和持续有效,请谨慎参考!

二、获取OpenAI的API keys

1.登录OpenAI账号进入生成API Keys页面

https://platform.openai.com/api-keys

2.添加支付方式并和持卡信息对应

https://platform.openai.com/account/billing/payment-methods

3.ChatGPT根据不同模型可Token数获取产生价格资费标准

https://openai.com/pricing

4.完成付费

https://platform.openai.com/account/billing/overview

三、调用OpenAI API示例

1.OpenAI官方参考接入文档

https://platform.openai.com/docs/libraries/community-libraries

2.通过Java测试例子调用OpenAI API

  1. import java.io.*;
  2. import java.net.HttpURLConnection;
  3. import java.net.InetSocketAddress;
  4. import java.net.Proxy;
  5. import java.net.URL;
  6. public class ChatGPTAPIExample {
  7. public static String chatGPT(String prompt) {
  8. String url = "https://api.openai.com/v1/chat/completions";
  9. String apiKey = "sk-***********************************";
  10. String model = "gpt-3.5-turbo";
  11. try {
  12. URL obj = new URL(url);
  13. // 结合VPN设置代理IP和Port
  14. Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1", Integer.valueOf(7890)));
  15. HttpURLConnection connection = (HttpURLConnection) obj.openConnection(proxy);
  16. connection.setRequestMethod("POST");
  17. connection.setRequestProperty("Authorization", "Bearer " + apiKey);
  18. connection.setRequestProperty("Content-Type", "application/json");
  19. // body
  20. String body = "{\"model\": \"" + model + "\", \"messages\": [{\"role\": \"user\", \"content\": \"" + prompt + "\"}]}";
  21. connection.setDoOutput(true);
  22. OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
  23. writer.write(body);
  24. writer.flush();
  25. writer.close();
  26. // 生成回复内容
  27. BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  28. String line;
  29. StringBuffer response = new StringBuffer();
  30. while ((line = br.readLine()) != null) {
  31. response.append(line);
  32. }
  33. br.close();
  34. return extractMessageFromJSONResponse(response.toString());
  35. } catch (IOException e) {
  36. throw new RuntimeException(e);
  37. }
  38. }
  39. public static String extractMessageFromJSONResponse(String response) {
  40. int start = response.indexOf("content")+ 11;
  41. int end = response.indexOf("\"", start);
  42. return response.substring(start, end);
  43. }
  44. public static void main(String[] args) {
  45. System.out.println(chatGPT("Java有哪些特性请用20以内字数描述"));
  46. }
  47. }

调用OpenAI API时候请设置VPN代理的IP和端口

语言后续推荐使用Python调用。或者https://github.com/TheoKanning/openai-java/tree/main

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

闽ICP备14008679号