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

Commit 89c5251

Browse files
author
deep-learning-dynamo
committed
added support for functions
1 parent 0817c99 commit 89c5251

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,24 @@ public Builder parameters(Parameters parameters) {
9090
}
9191

9292
@Experimental
93-
public Builder addParameter(String name, Constraint... constraints) {
94-
addOptionalParameter(name, constraints);
93+
public Builder addParameter(String name, JsonSchemaProperty... jsonSchemaProperties) {
94+
addOptionalParameter(name, jsonSchemaProperties);
9595
this.parameters.required().add(name);
9696
return this;
9797
}
9898

9999
@Experimental
100-
public Builder addOptionalParameter(String name, Constraint... constraints) {
100+
public Builder addOptionalParameter(String name, JsonSchemaProperty... jsonSchemaProperties) {
101101
if (this.parameters == null) {
102102
this.parameters = Parameters.builder().build();
103103
}
104104

105-
Map<String, Object> constraintMap = new HashMap<>();
106-
for (Constraint constraint : constraints) {
107-
constraintMap.put(constraint.key(), constraint.value());
105+
Map<String, Object> jsonSchemaPropertiesMap = new HashMap<>();
106+
for (JsonSchemaProperty jsonSchemaProperty : jsonSchemaProperties) {
107+
jsonSchemaPropertiesMap.put(jsonSchemaProperty.key(), jsonSchemaProperty.value());
108108
}
109109

110-
this.parameters.properties().put(name, constraintMap);
110+
this.parameters.properties().put(name, jsonSchemaPropertiesMap);
111111
return this;
112112
}
113113

src/main/java/dev/ai4j/openai4j/chat/Constraint.java renamed to src/main/java/dev/ai4j/openai4j/chat/JsonSchemaProperty.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
import java.util.Objects;
66

77
@Experimental
8-
public class Constraint {
8+
public class JsonSchemaProperty {
99

10-
public static final Constraint STRING = type("string");
11-
public static final Constraint NUMBER = type("number");
12-
public static final Constraint OBJECT = type("object");
13-
public static final Constraint ARRAY = type("array");
14-
public static final Constraint BOOLEAN = type("boolean");
15-
public static final Constraint NULL = type("null");
10+
public static final JsonSchemaProperty STRING = type("string");
11+
public static final JsonSchemaProperty NUMBER = type("number");
12+
public static final JsonSchemaProperty OBJECT = type("object");
13+
public static final JsonSchemaProperty ARRAY = type("array");
14+
public static final JsonSchemaProperty BOOLEAN = type("boolean");
15+
public static final JsonSchemaProperty NULL = type("null");
1616

1717
private final String key;
1818
private final Object value;
1919

20-
public Constraint(String key, Object value) {
20+
public JsonSchemaProperty(String key, Object value) {
2121
this.key = key;
2222
this.value = value;
2323
}
@@ -33,11 +33,11 @@ public Object value() {
3333
@Override
3434
public boolean equals(Object another) {
3535
if (this == another) return true;
36-
return another instanceof Constraint
37-
&& equalTo((Constraint) another);
36+
return another instanceof JsonSchemaProperty
37+
&& equalTo((JsonSchemaProperty) another);
3838
}
3939

40-
private boolean equalTo(Constraint another) {
40+
private boolean equalTo(JsonSchemaProperty another) {
4141
return Objects.equals(key, another.key)
4242
&& Objects.equals(value, another.value);
4343
}
@@ -52,39 +52,39 @@ public int hashCode() {
5252

5353
@Override
5454
public String toString() {
55-
return "Constraint{"
55+
return "JsonSchemaProperty{"
5656
+ "key=" + key
5757
+ ", value=" + value
5858
+ "}";
5959
}
6060

6161
@Experimental
62-
public static Constraint from(String key, Object value) {
63-
return new Constraint(key, value);
62+
public static JsonSchemaProperty from(String key, Object value) {
63+
return new JsonSchemaProperty(key, value);
6464
}
6565

6666
@Experimental
67-
public static Constraint constraint(String key, Object value) {
67+
public static JsonSchemaProperty property(String key, Object value) {
6868
return from(key, value);
6969
}
7070

7171
@Experimental
72-
public static Constraint type(String value) {
72+
public static JsonSchemaProperty type(String value) {
7373
return from("type", value);
7474
}
7575

7676
@Experimental
77-
public static Constraint description(String value) {
77+
public static JsonSchemaProperty description(String value) {
7878
return from("description", value);
7979
}
8080

8181
@Experimental
82-
public static Constraint enums(String... enumValues) { // TODO names
82+
public static JsonSchemaProperty enums(String... enumValues) {
8383
return from("enum", enumValues);
8484
}
8585

8686
@Experimental
87-
public static Constraint enums(Object... enumValues) { // TODO names
87+
public static JsonSchemaProperty enums(Object... enumValues) {
8888
for (Object enumValue : enumValues) {
8989
if (!enumValue.getClass().isEnum()) {
9090
throw new RuntimeException("Value " + enumValue.getClass().getName() + " should be enum");
@@ -95,7 +95,7 @@ public static Constraint enums(Object... enumValues) { // TODO names
9595
}
9696

9797
@Experimental
98-
public static Constraint enums(Class<?> enumClass) { // TODO names
98+
public static JsonSchemaProperty enums(Class<?> enumClass) {
9999
if (!enumClass.isEnum()) {
100100
throw new RuntimeException("Class " + enumClass.getName() + " should be enum");
101101
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.Map;
1111
import java.util.stream.Stream;
1212

13-
import static dev.ai4j.openai4j.chat.Constraint.*;
13+
import static dev.ai4j.openai4j.chat.JsonSchemaProperty.*;
1414
import static dev.ai4j.openai4j.chat.Message.functionMessage;
1515
import static dev.ai4j.openai4j.chat.Message.userMessage;
1616
import static dev.ai4j.openai4j.chat.Role.ASSISTANT;
@@ -116,7 +116,7 @@ void testFunctions() {
116116
assertThat(secondResponse.content()).contains("22");
117117
}
118118

119-
public static String getCurrentWeather(String location, Unit unit) { // TODO return POJO
119+
public static String getCurrentWeather(String location, Unit unit) {
120120
System.out.println(location);
121121
System.out.println(unit);
122122
return "{ \"temperature\": 22, \"unit\": \"celsius\", \"description\": \"Sunny\" }";

0 commit comments

Comments
 (0)