如何在Java项目里整合ChatGPT?

如题,如何在Java项目中调用ChatGPT的接口,我看了官方文档看不懂。


回答:

请把这整篇文章过一遍:
[https://juejin.cn/post/7208907027841171512]
你使用的应该是openAI提供的API-key,并且使用软件对某一个代理IP和端口进行了监听,参照这里面的第2.3.6点:

@Slf4j

@Component

public class OpenAiApi {

@Value("${open.ai.url}")

private String url;

@Value("${open.ai.token}")

private String token;

private static final MultiThreadedHttpConnectionManagerCONNECTION_MANAGER=new MultiThreadedHttpConnectionManager();

static {

// 默认单个host最大链接数

CONNECTION_MANAGER.getParams().setDefaultMaxConnectionsPerHost(

Integer.valueOf(20));

// 最大总连接数,默认20

CONNECTION_MANAGER.getParams()

.setMaxTotalConnections(20);

// 连接超时时间

CONNECTION_MANAGER.getParams()

.setConnectionTimeout(60000);

// 读取超时时间

CONNECTION_MANAGER.getParams().setSoTimeout(60000);

}

public ExecuteRet get(Stringpath, Map<String, String> headers) {

GetMethod method = new GetMethod(url +path);

if (headers== null) {

headers = new HashMap<>();

}

headers.put("Authorization", "Bearer " + token);

for (Map.Entry<String, String> h : headers.entrySet()) {

method.setRequestHeader(h.getKey(), h.getValue());

}

return execute(method);

}

public ExecuteRet post(Stringpath, Stringjson, Map<String, String> headers) {

try {

PostMethod method = new PostMethod(url +path);

//log.info("POST Url is {} ", url + path);

// 输出传入参数

log.info(String.format("POST JSON HttpMethod's Params = %s",json));

StringRequestEntity entity = new StringRequestEntity(json, "application/json", "UTF-8");

method.setRequestEntity(entity);

if (headers== null) {

headers = new HashMap<>();

}

headers.put("Authorization", "Bearer " + token);

for (Map.Entry<String, String> h : headers.entrySet()) {

method.setRequestHeader(h.getKey(), h.getValue());

}

return execute(method);

} catch (UnsupportedEncodingExceptionex) {

log.error(ex.getMessage(),ex);

}

return new ExecuteRet(false, "", null, -1);

}

public ExecuteRet execute(HttpMethodmethod) {

HttpClient client = new HttpClient(CONNECTION_MANAGER);

// 创建代理对象

HttpHost proxy = new HttpHost("你的代理主机ip", 你的代理主机端口);

client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

int statusCode = -1;

String respStr = null;

boolean isSuccess = false;

try {

client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF8");

statusCode = client.executeMethod(method);

method.getRequestHeaders();

// log.info("执行结果statusCode = " + statusCode);

InputStreamReader inputStreamReader = new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8");

BufferedReader reader = new BufferedReader(inputStreamReader);

StringBuilder stringBuffer = new StringBuilder(100);

String str;

while ((str = reader.readLine()) != null) {

log.debug("逐行读取String = " + str);

stringBuffer.append(str.trim());

}

respStr = stringBuffer.toString();

if (respStr != null) {

log.info(String.format("执行结果String = %s, Length = %d", respStr, respStr.length()));

}

inputStreamReader.close();

reader.close();

// 返回200,接口调用成功

isSuccess = (statusCode == HttpStatus.SC_OK);

} catch (IOExceptionex) {

} finally {

method.releaseConnection();

}

return new ExecuteRet(isSuccess, respStr,method, statusCode);

}

}

核心的设置代理的代码:

HttpHost proxy = new HttpHost("你的代理主机ip", 你的代理主机端口);

client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

如果你不是采用HttpClient的方法,其他的发起请求的客户端也是相似的,只要设置响应的代理主机就可以了,其他代码不需要变动


回答:

github有直接写好的,拿下来做个参考可以。 比如chatgptjava 。

以上是 如何在Java项目里整合ChatGPT? 的全部内容, 来源链接: utcz.com/p/945127.html

回到顶部