Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Done some Refactoring, like extract method, extract class, decomposing conditional etc. #489

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions api/src/main/java/com/theokanning/openai/utils/TikTokensUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,30 +173,42 @@ public static int tokens(String modelName, List<ChatMessage> messages) {
Encoding encoding = getEncoding(modelName);
int tokensPerMessage = 0;
int tokensPerName = 0;
//3.5统一处理

// Constants for token counts per message and name
final int TOKENS_PER_MESSAGE_GPT_3_5_TURBO = 4;
final int TOKENS_PER_MESSAGE_GPT_4 = 3;
final int TOKENS_PER_NAME = 1;

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.


// Determine token counts based on model
if (modelName.equals("gpt-3.5-turbo-0301") || modelName.equals("gpt-3.5-turbo")) {
tokensPerMessage = 4;
tokensPerMessage = TOKENS_PER_MESSAGE_GPT_3_5_TURBO;
tokensPerName = -1;
}
//4.0统一处理
if (modelName.equals("gpt-4") || modelName.equals("gpt-4-0314")) {
tokensPerMessage = 3;
tokensPerName = 1;
tokensPerMessage = TOKENS_PER_MESSAGE_GPT_4;
tokensPerName = TOKENS_PER_NAME;
}
int sum = 0;

int totalTokens = 0; // Variable to hold total tokens

for (ChatMessage msg : messages) {
sum += tokensPerMessage;
sum += tokens(encoding, msg.getContent());
sum += tokens(encoding, msg.getRole());
sum += tokens(encoding, msg.getName());
int messageTokens = tokens(encoding, msg.getContent()) +
tokens(encoding, msg.getRole()) +
tokens(encoding, msg.getName());

if (isNotBlank(msg.getName())) {
sum += tokensPerName;
messageTokens += tokensPerName;
}

totalTokens += tokensPerMessage + messageTokens;
}
sum += 3;
return sum;

totalTokens += 3; // Additional tokens for processing

return totalTokens;
}


/**
* Reverse the string text through the model name and the encoded array.
*
Expand Down
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ allprojects {
}
}
}



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
*
Expand Down
6 changes: 1 addition & 5 deletions example/src/main/java/example/OpenAiApiExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void main(String... args) {

System.out.println("\nCreating Image...");
CreateImageRequest request = CreateImageRequest.builder()
.prompt("A cow breakdancing with a turtle")
.prompt("A+ in coding assignment")
.build();

System.out.println("\nImage is located at:");
Expand All @@ -48,10 +48,6 @@ public static void main(String... args) {
.logitBias(new HashMap<>())
.build();

service.streamChatCompletion(chatCompletionRequest)
.doOnError(Throwable::printStackTrace)
.blockingForEach(System.out::println);

service.shutdownExecutor();
}
}
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() {
}

Expand All @@ -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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Returning collections such as Map - from functions, should preserve their immutability.
Thing about collections.unmodifiableMap
https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableMap-java.util.Map-

}

@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);
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public FunctionExecutor(List<ChatFunction> functions, ObjectMapper objectMapper)

public Optional<ChatMessage> executeAndConvertToMessageSafely(ChatFunctionCall call) {
try {
return Optional.ofNullable(executeAndConvertToMessage(call));
return Optional.ofNullable(MessageConverter.executeAndConvertToMessage(this, call));
} catch (Exception ignored) {
return Optional.empty();
}
}

public ChatMessage executeAndConvertToMessageHandlingExceptions(ChatFunctionCall call) {
try {
return executeAndConvertToMessage(call);
return MessageConverter.executeAndConvertToMessage(this, call);
} catch (Exception exception) {
exception.printStackTrace();
return convertExceptionToMessage(exception);
Expand All @@ -48,34 +48,6 @@ public ChatMessage convertExceptionToMessage(Exception exception) {
return new ChatMessage(ChatMessageRole.FUNCTION.value(), "{\"error\": \"" + error + "\"}", "error");
}

public ChatMessage executeAndConvertToMessage(ChatFunctionCall call) {
return new ChatMessage(ChatMessageRole.FUNCTION.value(), executeAndConvertToJson(call).toPrettyString(), call.getName());
}

public JsonNode executeAndConvertToJson(ChatFunctionCall call) {
try {
Object execution = execute(call);
if (execution instanceof TextNode) {
JsonNode objectNode = MAPPER.readTree(((TextNode) execution).asText());
if (objectNode.isMissingNode())
return (JsonNode) execution;
return objectNode;
}
if (execution instanceof ObjectNode) {
return (JsonNode) execution;
}
if (execution instanceof String) {
JsonNode objectNode = MAPPER.readTree((String) execution);
if (objectNode.isMissingNode())
throw new RuntimeException("Parsing exception");
return objectNode;
}
return MAPPER.readValue(MAPPER.writeValueAsString(execution), JsonNode.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@SuppressWarnings("unchecked")
public <T> T execute(ChatFunctionCall call) {
ChatFunction function = FUNCTIONS.get(call.getName());
Expand All @@ -102,4 +74,34 @@ public void setObjectMapper(ObjectMapper objectMapper) {
this.MAPPER = objectMapper;
}

// Inner class to handle message conversion
private static class MessageConverter {
public static ChatMessage executeAndConvertToMessage(FunctionExecutor executor, ChatFunctionCall call) {
return new ChatMessage(ChatMessageRole.FUNCTION.value(), executeAndConvertToJson(executor, call).toPrettyString(), call.getName());
}

public static JsonNode executeAndConvertToJson(FunctionExecutor executor, ChatFunctionCall call) {
try {
Object execution = executor.execute(call);
if (execution instanceof TextNode) {
JsonNode objectNode = executor.MAPPER.readTree(((TextNode) execution).asText());
if (objectNode.isMissingNode())
return (JsonNode) execution;
return objectNode;
}
if (execution instanceof ObjectNode) {
return (JsonNode) execution;
}
if (execution instanceof String) {
JsonNode objectNode = executor.MAPPER.readTree((String) execution);
if (objectNode.isMissingNode())
throw new RuntimeException("Parsing exception");
return objectNode;
}
return executor.MAPPER.readValue(executor.MAPPER.writeValueAsString(execution), JsonNode.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Choose a reason for hiding this comment

The 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);
}
}
}
Loading