chatGPT 最近很火,有意做个套壳小程序玩玩跟跟风
第三方依赖 async-http-client
<dependency>
<groupId>org.asynchttpclient</groupId>
<artifactId>async-http-client</artifactId>
<version>2.12.3</version>
</dependency>
上代码
import io.netty.handler.codec.http.HttpHeaders;
import lombok.extern.slf4j.Slf4j;
import org.asynchttpclient.HttpResponseBodyPart;
import org.asynchttpclient.HttpResponseStatus;
import org.asynchttpclient.handler.StreamedAsyncHandler;
import org.reactivestreams.Publisher;
@Slf4j
public class ChatGptStreamedHandler implements StreamedAsyncHandler {
@Override
public State onStream(Publisher publisher) {
publisher.subscribe(new ChatGptSubscriber());
log.info("》》》》》 onStream");
return State.CONTINUE;
}
@Override
public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
log.info("》》》》》onStatusReceived");
return responseStatus.getStatusCode() == 200 ? State.CONTINUE : State.ABORT;
}
@Override
public State onHeadersReceived(HttpHeaders headers) throws Exception {
log.info("》》》》》 头信息处理");
return State.CONTINUE;
}
@Override
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
log.info("》》》》》 onBodyPartReceived");
return State.CONTINUE;
}
@Override
public void onThrowable(Throwable t) {
log.error("发生异常", t);
}
@Override
public Object onCompleted() throws Exception {
log.info("》》》》》 完成");
return State.ABORT;
}
}
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
@Slf4j
public class ChatGptSubscriber implements Subscriber {
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(Object o) {
log.info(">>>>> onNext {}", JSON.toJSONString(o));
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
}
}
public static void main(String[] args) throws Exception {
String apiUrl = "http://xxx.xxx.xxx/v1/chat/completions";
String apiKey = "sk-xxxxxxxxxxxxxxxxxxxxxx";
List<JSONObject> msgData = new ArrayList<>();
msgData.add(
new JSONObject()
.fluentPut("role", "user")
.fluentPut("content", "夸奖我一下")
);
JSONObject req = new JSONObject()
.fluentPut("max_tokens", 512)
.fluentPut("model", "gpt-3.5-turbo")
.fluentPut("temperature", 0.6)
.fluentPut("stream", Boolean.TRUE)
.fluentPut("messages", msgData);
log.info("》》 {}", JSON.toJSONString(req));
AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient();
asyncHttpClient.preparePost(apiUrl)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer " + apiKey)
.addHeader("Accept", "text/event-stream")
.setBody(JSON.toJSONString(req))
.execute(new ChatGptStreamedHandler())
.get();
asyncHttpClient.close();
}
}
补充一下nginx配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name api.openai.com;
charset utf-8;
location / {
proxy_pass https://api.openai.com/;
proxy_set_header Host api.openai.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
proxy_ssl_session_reuse off;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 9898;
server_name proxy;
charset utf-8;
location / {
resolver 8.8.8.8;
proxy_pass $scheme://$host$request_uri;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}