赞
踩
目录
3.ChatGPT根据不同模型可Token数获取产生价格资费标准
序号 | 账号类型 | 用户名 | 密码 | 备注 |
---|---|---|---|---|
1 | 谷歌 | *** | ****** | 个人谷歌账号,无请申请注册 |
2 | OpenAI | *** | ****** | 可通过谷歌账号申请 |
3 | VPN | *** | ****** | 科学上网 |
4 | 信用卡(美国) | *** | ****** | 可通过第三方软件申请虚拟信用卡 |
ChatGPT国内无法直接访问,需借助科学上网,本地提供的方法只供参考并不保证安全和持续有效,请谨慎参考! |
https://platform.openai.com/api-keys
https://platform.openai.com/account/billing/payment-methods
https://platform.openai.com/account/billing/overview
https://platform.openai.com/docs/libraries/community-libraries
- import java.io.*;
- import java.net.HttpURLConnection;
- import java.net.InetSocketAddress;
- import java.net.Proxy;
- import java.net.URL;
-
- public class ChatGPTAPIExample {
-
- public static String chatGPT(String prompt) {
- String url = "https://api.openai.com/v1/chat/completions";
- String apiKey = "sk-***********************************";
- String model = "gpt-3.5-turbo";
- try {
- URL obj = new URL(url);
- // 结合VPN设置代理IP和Port
- Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1", Integer.valueOf(7890)));
- HttpURLConnection connection = (HttpURLConnection) obj.openConnection(proxy);
- connection.setRequestMethod("POST");
- connection.setRequestProperty("Authorization", "Bearer " + apiKey);
- connection.setRequestProperty("Content-Type", "application/json");
- // body
- String body = "{\"model\": \"" + model + "\", \"messages\": [{\"role\": \"user\", \"content\": \"" + prompt + "\"}]}";
- connection.setDoOutput(true);
- OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
- writer.write(body);
- writer.flush();
- writer.close();
- // 生成回复内容
- BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- String line;
- StringBuffer response = new StringBuffer();
- while ((line = br.readLine()) != null) {
- response.append(line);
- }
- br.close();
- return extractMessageFromJSONResponse(response.toString());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- public static String extractMessageFromJSONResponse(String response) {
- int start = response.indexOf("content")+ 11;
- int end = response.indexOf("\"", start);
- return response.substring(start, end);
- }
-
- public static void main(String[] args) {
- System.out.println(chatGPT("Java有哪些特性请用20以内字数描述"));
- }
- }
调用OpenAI API时候请设置VPN代理的IP和端口
语言后续推荐使用Python调用。或者https://github.com/TheoKanning/openai-java/tree/main
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。