Skip to content

Commit

Permalink
- 1.9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteMagic2014 committed Dec 6, 2023
1 parent 9a49c62 commit ffdf3bc
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ new CreateChatCompletionRequest()

## Version

### 1.9.1

- Update: RequestUtil now includes a new method called streamRequestV2, which is an extended version of streamRequest
and provides enhanced support for the function mode.

### 1.9.0

- Update: update models in GptModel.class
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<url>https://github.com/WhiteMagic2014/gpt-magic.git</url>
<groupId>io.github.whitemagic2014</groupId>
<artifactId>gpt-magic</artifactId>
<version>1.9.0</version>
<version>1.9.1</version>

<developers>
<developer>
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/com/github/WhiteMagic2014/util/RequestUtil.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package com.github.WhiteMagic2014.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.WhiteMagic2014.gptApi.Chat.CreateChatCompletionRequest;
import com.github.WhiteMagic2014.gptApi.Chat.pojo.ChatMessage;

import java.io.ByteArrayOutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -38,4 +45,65 @@ public static String streamRequest(CreateChatCompletionRequest request) {
}


/**
* Get result content with stream model (support function)
*
* @param request
* @return
*/
public static ChatMessage streamRequestV2(CreateChatCompletionRequest request) {
JSONObject result = new JSONObject();
result.put("role", "assistant");
Map<String, String> paramsTemp = new HashMap<>();
StringBuilder sb = new StringBuilder();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
request.stream(true).outputStream(baos).send();
byte[] data = baos.toByteArray();
if (data.length > 0) {
String tmpStr = new String(data);
baos.reset();
String str = "[" + tmpStr.replace("data: [DONE]", "").replace("data:", ",") + "]";
JSONArray jsonArray = JSON.parseArray(str);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject choice = jsonArray.getJSONObject(i).getJSONArray("choices").getJSONObject(0);
if (choice.getString("finish_reason") != null) {
break;
}
JSONObject delta = choice.getJSONObject("delta");
if (delta.containsKey("tool_calls")) {
JSONObject callItem = delta.getJSONArray("tool_calls").getJSONObject(0);
if (callItem.containsKey("id")) {
result.put("callId", callItem.getString("id"));
}
if (callItem.containsKey("type")) {
result.put("type", callItem.getString("type"));
}
if (callItem.containsKey("function")) {
JSONObject func = callItem.getJSONObject("function");
if (func.containsKey("name")) {
paramsTemp.put("funcName", func.getString("name"));
}
sb.append(func.getString("arguments"));
}
} else {
result.put("type", "chat");
sb.append(delta.getString("content"));
}
}
}
if ("chat".equals(result.get("type"))) {
result.put("content", sb.toString());
} else if ("function".equals(result.get("type"))) {
JSONObject func = new JSONObject();
func.put("name", paramsTemp.get("funcName"));
func.put("arguments", sb.toString());
JSONObject call = new JSONObject();
call.put("type", "function");
call.put("id", paramsTemp.get("callId"));
call.put("function", func);
result.put("tool_calls", Collections.singletonList(call));
}
return JSON.parseObject(result.toJSONString(), ChatMessage.class);
}

}

0 comments on commit ffdf3bc

Please sign in to comment.