-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7f7bd5
commit ed6c392
Showing
15 changed files
with
266 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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
68 changes: 68 additions & 0 deletions
68
src/main/java/io/github/stefanbratanov/jvm/openai/JsonSchema.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public record JsonSchema( | ||
String name, | ||
Optional<String> description, | ||
Optional<Map<String, Object>> schema, | ||
Optional<Boolean> strict) { | ||
|
||
public JsonSchema { | ||
schema = schema.map(Utils::mapWithoutJsonEscaping); | ||
} | ||
|
||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private String name; | ||
private Optional<String> description = Optional.empty(); | ||
private Optional<Map<String, Object>> schema = Optional.empty(); | ||
private Optional<Boolean> strict = Optional.empty(); | ||
|
||
/** | ||
* @param name The name of the response format. | ||
*/ | ||
public Builder name(String name) { | ||
this.name = name; | ||
return this; | ||
} | ||
|
||
/** | ||
* @param description A description of what the response format is for, used by the model to | ||
* determine how to respond in the format. | ||
*/ | ||
public Builder description(String description) { | ||
this.description = Optional.of(description); | ||
return this; | ||
} | ||
|
||
/** | ||
* @param schema The schema for the response format, described as a JSON Schema object. The JSON | ||
* schema should be defined as {@link Map} where a value could be a raw escaped JSON {@link | ||
* String} and it will be serialized without escaping. | ||
*/ | ||
public Builder schema(Map<String, Object> schema) { | ||
this.schema = Optional.of(schema); | ||
return this; | ||
} | ||
|
||
/** | ||
* @param strict Whether to enable strict schema adherence when generating the output. If set to | ||
* true, the model will always follow the exact schema defined in the schema field. Only a | ||
* subset of JSON Schema is supported when strict is true. | ||
*/ | ||
public Builder strict(boolean strict) { | ||
this.strict = Optional.of(strict); | ||
return this; | ||
} | ||
|
||
public JsonSchema build() { | ||
return new JsonSchema(name, description, schema, strict); | ||
} | ||
} | ||
} |
This file contains 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
13 changes: 10 additions & 3 deletions
13
src/main/java/io/github/stefanbratanov/jvm/openai/ResponseFormat.java
This file contains 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,12 +1,19 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
import java.util.Optional; | ||
|
||
/** An object specifying the format that the model must output. */ | ||
public record ResponseFormat(String type) implements AssistantsResponseFormat { | ||
public record ResponseFormat(String type, Optional<JsonSchema> jsonSchema) | ||
implements AssistantsResponseFormat { | ||
public static ResponseFormat text() { | ||
return new ResponseFormat("text"); | ||
return new ResponseFormat("text", Optional.empty()); | ||
} | ||
|
||
public static ResponseFormat json() { | ||
return new ResponseFormat("json_object"); | ||
return new ResponseFormat("json_object", Optional.empty()); | ||
} | ||
|
||
public static ResponseFormat jsonSchema(JsonSchema jsonSchema) { | ||
return new ResponseFormat("json_schema", Optional.of(jsonSchema)); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/io/github/stefanbratanov/jvm/openai/Utils.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import java.io.IOException; | ||
import java.util.AbstractMap; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
class Utils { | ||
|
||
private Utils() {} | ||
|
||
static Map<String, Object> mapWithoutJsonEscaping(Map<String, Object> map) { | ||
return map.entrySet().stream() | ||
.map( | ||
entry -> { | ||
if (entry.getValue() instanceof String value) { | ||
try { | ||
JsonNode node = ObjectMapperSingleton.getInstance().readTree(value); | ||
if (node != null && !node.isNull()) { | ||
return new AbstractMap.SimpleEntry<>(entry.getKey(), node); | ||
} | ||
} catch (IOException ex) { | ||
return entry; | ||
} | ||
} | ||
return entry; | ||
}) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.