Skip to content
This repository was archived by the owner on Feb 11, 2025. It is now read-only.

Commit a82341c

Browse files
committed
Move gson out of the model
1 parent 8edae00 commit a82341c

File tree

7 files changed

+53
-45
lines changed

7 files changed

+53
-45
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package dev.ai4j.openai4j;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.reflect.TypeToken;
5+
import dev.ai4j.openai4j.chat.FunctionCall;
6+
import java.lang.reflect.Type;
7+
import java.util.Map;
8+
9+
public class FunctionCallUtil {
10+
public static final Gson GSON = new Gson();
11+
public static final Type MAP_TYPE = new TypeToken<Map<String, Object>>() {
12+
}.getType();
13+
14+
public static <T> T argument(FunctionCall functionCall, String name) {
15+
Map<String, Object> arguments = argumentsAsMap(functionCall.arguments()); // TODO cache
16+
return (T) arguments.get(name);
17+
}
18+
19+
public static Map<String, Object> argumentsAsMap(String arguments) {
20+
return GSON.fromJson(arguments, MAP_TYPE);
21+
}
22+
}

src/main/java/dev/ai4j/openai4j/Json.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
5+
import dev.ai4j.openai4j.chat.Role;
56

67
import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES;
78
import static dev.ai4j.openai4j.MessageTypeAdapter.MESSAGE_TYPE_ADAPTER_FACTORY;
@@ -11,6 +12,7 @@ class Json {
1112
static final Gson GSON = new GsonBuilder()
1213
.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES)
1314
.registerTypeAdapterFactory(MESSAGE_TYPE_ADAPTER_FACTORY)
15+
.registerTypeAdapter(Role.class, new RoleAdapter())
1416
.create();
1517

1618
static String toJson(Object o) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dev.ai4j.openai4j;
2+
3+
import com.google.gson.TypeAdapter;
4+
import com.google.gson.stream.JsonReader;
5+
import com.google.gson.stream.JsonWriter;
6+
import dev.ai4j.openai4j.chat.Role;
7+
import java.io.IOException;
8+
9+
public class RoleAdapter extends TypeAdapter<Role> {
10+
11+
@Override
12+
public void write(final JsonWriter jsonWriter, final Role role) throws IOException {
13+
jsonWriter.value(role.toString());
14+
}
15+
16+
@Override
17+
public Role read(JsonReader jsonReader) throws IOException {
18+
return Role.from(jsonReader.nextString());
19+
}
20+
}

src/main/java/dev/ai4j/openai4j/chat/FunctionCall.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
package dev.ai4j.openai4j.chat;
22

3-
import com.google.gson.Gson;
4-
import com.google.gson.reflect.TypeToken;
5-
6-
import java.lang.reflect.Type;
7-
import java.util.Map;
83
import java.util.Objects;
94

105
public class FunctionCall {
116

12-
private static final Gson GSON = new Gson();
13-
private static final Type MAP_TYPE = new TypeToken<Map<String, Object>>() {
14-
}.getType();
15-
167
private final String name;
178
private final String arguments;
189

@@ -29,15 +20,6 @@ public String arguments() {
2920
return arguments;
3021
}
3122

32-
public Map<String, Object> argumentsAsMap() {
33-
return GSON.fromJson(arguments, MAP_TYPE);
34-
}
35-
36-
public <T> T argument(String name) {
37-
Map<String, Object> arguments = argumentsAsMap(); // TODO cache
38-
return (T) arguments.get(name);
39-
}
40-
4123
@Override
4224
public boolean equals(Object another) {
4325
if (this == another) return true;
Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
package dev.ai4j.openai4j.chat;
22

3-
import com.google.gson.TypeAdapter;
4-
import com.google.gson.annotations.JsonAdapter;
5-
import com.google.gson.stream.JsonReader;
6-
import com.google.gson.stream.JsonWriter;
7-
8-
import java.io.IOException;
9-
10-
@JsonAdapter(Role.RoleAdapter.class)
113
public enum Role {
124

135
SYSTEM("system"),
@@ -26,7 +18,7 @@ public String toString() {
2618
return stringValue;
2719
}
2820

29-
static Role from(String stringValue) {
21+
public static Role from(String stringValue) {
3022
for (Role role : Role.values()) {
3123
if (role.stringValue.equals(stringValue)) {
3224
return role;
@@ -35,16 +27,4 @@ static Role from(String stringValue) {
3527
throw new IllegalArgumentException("Unknown role: '" + stringValue + "'");
3628
}
3729

38-
static class RoleAdapter extends TypeAdapter<Role> {
39-
40-
@Override
41-
public void write(final JsonWriter jsonWriter, final Role role) throws IOException {
42-
jsonWriter.value(role.toString());
43-
}
44-
45-
@Override
46-
public Role read(JsonReader jsonReader) throws IOException {
47-
return Role.from(jsonReader.nextString());
48-
}
49-
}
50-
}
30+
}

src/test/java/dev/ai4j/openai4j/chat/ChatCompletionAsyncTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.ai4j.openai4j.chat;
22

3+
import dev.ai4j.openai4j.FunctionCallUtil;
34
import dev.ai4j.openai4j.OpenAiClient;
45
import dev.ai4j.openai4j.RateLimitAwareTest;
56
import org.junit.jupiter.api.Test;
@@ -119,8 +120,8 @@ void testFunctions() throws Exception {
119120
assertThat(functionCall.name()).isEqualTo("get_current_weather");
120121
assertThat(functionCall.arguments()).isNotBlank();
121122

122-
Map<String, Object> arguments = functionCall.argumentsAsMap();
123+
Map<String, Object> arguments = FunctionCallUtil.argumentsAsMap(functionCall.arguments());
123124
assertThat(arguments).hasSize(1);
124125
assertThat(arguments.get("location").toString()).contains("Boston");
125126
}
126-
}
127+
}

src/test/java/dev/ai4j/openai4j/chat/ChatCompletionTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.ai4j.openai4j.chat;
22

3+
import dev.ai4j.openai4j.FunctionCallUtil;
34
import dev.ai4j.openai4j.OpenAiClient;
45
import dev.ai4j.openai4j.RateLimitAwareTest;
56
import org.junit.jupiter.api.Test;
@@ -95,12 +96,12 @@ void testFunctions() {
9596
assertThat(functionCall.name()).isEqualTo("get_current_weather");
9697
assertThat(functionCall.arguments()).isNotBlank();
9798

98-
Map<String, Object> arguments = functionCall.argumentsAsMap();
99+
Map<String, Object> arguments = FunctionCallUtil.argumentsAsMap(functionCall.arguments());
99100
assertThat(arguments).hasSize(1);
100101
assertThat(arguments.get("location").toString()).contains("Boston");
101102

102-
String location = functionCall.argument("location");
103-
String unit = functionCall.argument("unit");
103+
String location = FunctionCallUtil.argument(functionCall, "location");
104+
String unit = FunctionCallUtil.argument(functionCall, "unit");
104105

105106
String weatherApiResponse = getCurrentWeather(location, unit == null ? null : Unit.valueOf(unit));
106107

0 commit comments

Comments
 (0)