This repository was archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Done some Refactoring, like extract method, extract class, decomposing conditional etc. #489
Open
BuzzLightyear2002
wants to merge
1
commit into
TheoKanning:main
Choose a base branch
from
BuzzLightyear2002:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,6 @@ allprojects { | |
} | ||
} | ||
} | ||
|
||
|
||
|
1 change: 1 addition & 0 deletions
1
client/src/main/java/com/theokanning/openai/AuthenticationInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package com.theokanning.openai; | ||
|
||
|
||
/** | ||
* OkHttp Interceptor that adds an authorization token header | ||
* | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,32 @@ | ||
package com.theokanning.openai.service; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonParseException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.JsonNodeType; | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
|
||
import com.fasterxml.jackson.databind.node.TextNode; | ||
import com.fasterxml.jackson.databind.*; | ||
|
||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ChatFunctionCallArgumentsSerializerAndDeserializer { | ||
|
||
private final static ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
public class ChatFunctionCallArgumentsSerializerAndDeserializer { | ||
private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
private ChatFunctionCallArgumentsSerializerAndDeserializer() { | ||
} | ||
|
||
public static class Serializer extends JsonSerializer<JsonNode> { | ||
|
||
private Serializer() { | ||
} | ||
|
||
|
@@ -32,33 +40,40 @@ public void serialize(JsonNode value, JsonGenerator gen, SerializerProvider seri | |
} | ||
} | ||
|
||
public abstract static class JsonNodeHandler { | ||
public abstract JsonNode handle(JsonParser p, DeserializationContext ctxt) throws IOException; | ||
} | ||
|
||
public static class MissingNodeHandler extends JsonNodeHandler { | ||
@Override | ||
public JsonNode handle(JsonParser p, DeserializationContext ctxt) { | ||
return JsonNodeFactory.instance.missingNode(); | ||
} | ||
} | ||
|
||
public static class DefaultNodeHandler extends JsonNodeHandler { | ||
@Override | ||
public JsonNode handle(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
return MAPPER.readTree(p); | ||
} | ||
} | ||
|
||
public static class Deserializer extends JsonDeserializer<JsonNode> { | ||
private static final Map<JsonToken, JsonNodeHandler> HANDLERS = initializeHandlers(); | ||
|
||
private Deserializer() { | ||
private static Map<JsonToken, JsonNodeHandler> initializeHandlers() { | ||
Map<JsonToken, JsonNodeHandler> handlers = new HashMap<>(); | ||
handlers.put(JsonToken.VALUE_NULL, new MissingNodeHandler()); | ||
// Add more handlers for different token types if needed | ||
return handlers; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Returning collections such as Map - from functions, should preserve their immutability. |
||
} | ||
|
||
@Override | ||
public JsonNode deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
String json = p.getValueAsString(); | ||
if (json == null || p.currentToken() == JsonToken.VALUE_NULL) { | ||
return null; | ||
} | ||
|
||
try { | ||
JsonNode node = null; | ||
try { | ||
node = MAPPER.readTree(json); | ||
} catch (JsonParseException ignored) { | ||
} | ||
if (node == null || node.getNodeType() == JsonNodeType.MISSING) { | ||
node = MAPPER.readTree(p); | ||
} | ||
return node; | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
return null; | ||
} | ||
JsonToken currentToken = p.getCurrentToken(); | ||
JsonNodeHandler handler = HANDLERS.getOrDefault(currentToken, new DefaultNodeHandler()); | ||
return handler.handle(p, ctxt); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,61 +40,79 @@ public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) | |
|
||
try { | ||
if (!response.isSuccessful()) { | ||
HttpException e = new HttpException(response); | ||
ResponseBody errorBody = response.errorBody(); | ||
|
||
if (errorBody == null) { | ||
throw e; | ||
} else { | ||
OpenAiError error = mapper.readValue( | ||
errorBody.string(), | ||
OpenAiError.class | ||
); | ||
throw new OpenAiHttpException(error, e, e.code()); | ||
} | ||
handleUnsuccessfulResponse(response); | ||
return; | ||
} | ||
|
||
InputStream in = response.body().byteStream(); | ||
reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); | ||
String line; | ||
SSE sse = null; | ||
parseSSE(reader); | ||
|
||
emitter.onComplete(); | ||
|
||
} catch (Throwable t) { | ||
onFailure(call, t); | ||
} finally { | ||
if (reader != null) { | ||
try { | ||
reader.close(); | ||
} catch (IOException e) { | ||
// do nothing | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void handleUnsuccessfulResponse(Response<ResponseBody> response) throws IOException { | ||
HttpException e = new HttpException(response); | ||
ResponseBody errorBody = response.errorBody(); | ||
|
||
if (errorBody == null) { | ||
throw e; | ||
} else { | ||
OpenAiError error = mapper.readValue( | ||
errorBody.string(), | ||
OpenAiError.class | ||
); | ||
throw new OpenAiHttpException(error, e, e.code()); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: It can be optimized try {
ResponseBody errorBody = response.errorBody();
if (errorBody != null) {
OpenAiError error = mapper.readValue(errorBody.string(), OpenAiError.class);
throw new OpenAiHttpException(error, new HttpException(response), response.code());
}
} catch (IOException ex) {
throw new HttpException(response);
} |
||
|
||
private void parseSSE(BufferedReader reader) throws IOException { | ||
String line; | ||
SSE sse = null; | ||
|
||
try { | ||
while (!emitter.isCancelled() && (line = reader.readLine()) != null) { | ||
if (line.startsWith("data:")) { | ||
String data = line.substring(5).trim(); | ||
sse = new SSE(data); | ||
} else if (line.equals("") && sse != null) { | ||
if (sse.isDone()) { | ||
if (emitDone) { | ||
emitter.onNext(sse); | ||
} | ||
break; | ||
} | ||
|
||
emitter.onNext(sse); | ||
handleSSELine(sse); | ||
sse = null; | ||
} else { | ||
throw new SSEFormatException("Invalid sse format! " + line); | ||
} | ||
} | ||
} catch (SSEFormatException e) { | ||
throw new IOException("Error parsing SSE", e); | ||
} | ||
} | ||
|
||
emitter.onComplete(); | ||
|
||
} catch (Throwable t) { | ||
onFailure(call, t); | ||
} finally { | ||
if (reader != null) { | ||
try { | ||
reader.close(); | ||
} catch (IOException e) { | ||
// do nothing | ||
} | ||
private void handleSSELine(SSE sse) { | ||
if (sse.isDone()) { | ||
if (emitDone) { | ||
emitter.onNext(sse); | ||
} | ||
return; | ||
} | ||
|
||
emitter.onNext(sse); | ||
} | ||
|
||
@Override | ||
public void onFailure(Call<ResponseBody> call, Throwable t) { | ||
emitter.onError(t); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Final members for utils class, should be static.
Reason: It provides better performance, values are inlined at compile time instead of a runtime value lookup.