AI
[ChatGPT] 기본 대화를 java Streming으로 구연해보자.
태주니아빠
2025. 2. 13. 14:17
반응형
ChatGPT의 /openai/v1/chat/completions/streaming을 자바, 자바스크립트, jQuery로 구현해봤습니다.
참고하세요~
1. javascript
const eventSource = new EventSource("/openai/v1/chat/completions/streaming?msg=" + $("textarea[name=iptChat]").val());
eventSource.onmessage = event => {
var chunkJson = JSON.parse(event.data);
$(`#${convId}`).append(chunkJson.choices[0].delta.content);
$(".chat-area").scrollTop($(".chat-area")[0].scrollHeight);
};
eventSource.onerror = error => {
eventSource.close();
};
2. java
/**
* 대화생성
* @param params
* @return
* @throws Exception
*/
@RequestMapping(value = {"/v1/chat/completions/streaming"})
public SseEmitter createChatCompletion(@RequestParam Map params, HttpServletRequest request) throws Exception {
SseEmitter emitter = new SseEmitter();
String model = "gpt-4o";
String project = "PROJECT-ID";
String apiKey = "API-KEY";
String url = "https://api.openai.com/v1/chat/completions";
// HTTP 클라이언트 생성
HttpURLConnection httpConn = (HttpURLConnection) new URL(url).openConnection();
// HTTP GET 요청
httpConn.setRequestMethod("GET");
httpConn.setConnectTimeout(3000); // 연결 타임아웃
httpConn.setReadTimeout(60000); // 읽기 타임아웃
httpConn.setDoOutput(true);
// HEADER 셋팅
httpConn.setRequestProperty("Content-Type", "application/json");
httpConn.setRequestProperty("Authorization", apiKey);
// BODY 셋팅
JSONObject jo = new JSONObject();
jo.put("model", "gpt-4o");
jo.put("store", true);
jo.put("stream", true);
JSONArray ja = new JSONArray();
JSONObject msg = new JSONObject();
msg.put("role", "developer");
msg.put("content", "반말로 무시하는 말투로 도와주는 선생님역할.");
ja.add(msg);
msg = new JSONObject();
msg.put("role", "user");
msg.put("content", params.get("msg"));
ja.add(msg);
jo.put("messages", ja);
log.debug("### INFO jo.toJSONString() : {}", jo.toJSONString());
// 요청
OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
writer.write(jo.toJSONString());
writer.flush();
writer.close();
httpConn.getOutputStream().close();
// 응답 코드 확인
int responseCode = httpConn.getResponseCode();
System.out.println("HTTP Response Code: " + responseCode);
InputStream responseStream = httpConn.getResponseCode() == 200 ? httpConn.getInputStream() : httpConn.getErrorStream();
try {
// 새로운 Thread로 실행
new Thread(() -> {
BufferedReader reader = new BufferedReader(new InputStreamReader(responseStream));
try {
String inputLine;
while ((inputLine = reader.readLine()) != null) {
if(inputLine.indexOf("{") <= -1 && inputLine.lastIndexOf("}") <= -1 ) {
continue;
}
inputLine = inputLine.substring(inputLine.indexOf("{"), inputLine.lastIndexOf("}")+1);
emitter.send(inputLine);
}
} catch (Exception e) {
e.printStackTrace();
emitter.completeWithError(e);
} finally {
emitter.complete();
if(reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(httpConn != null) {
httpConn.disconnect();
}
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
return emitter;
}
3. 테스트
4.끝
반응형