This repository was archived by the owner on Feb 11, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 7 files changed +53
-45
lines changed
main/java/dev/ai4j/openai4j
test/java/dev/ai4j/openai4j/chat Expand file tree Collapse file tree 7 files changed +53
-45
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 22
33import com .google .gson .Gson ;
44import com .google .gson .GsonBuilder ;
5+ import dev .ai4j .openai4j .chat .Role ;
56
67import static com .google .gson .FieldNamingPolicy .LOWER_CASE_WITH_UNDERSCORES ;
78import 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 ) {
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11package 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 ;
83import java .util .Objects ;
94
105public 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 ;
Original file line number Diff line number Diff line change 11package 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 )
113public 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+ }
Original file line number Diff line number Diff line change 11package dev .ai4j .openai4j .chat ;
22
3+ import dev .ai4j .openai4j .FunctionCallUtil ;
34import dev .ai4j .openai4j .OpenAiClient ;
45import dev .ai4j .openai4j .RateLimitAwareTest ;
56import 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+ }
Original file line number Diff line number Diff line change 11package dev .ai4j .openai4j .chat ;
22
3+ import dev .ai4j .openai4j .FunctionCallUtil ;
34import dev .ai4j .openai4j .OpenAiClient ;
45import dev .ai4j .openai4j .RateLimitAwareTest ;
56import 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
You can’t perform that action at this time.
0 commit comments