Hey everyone!
The official Java SDK for the OpenAI API is now available on GitHub and Maven Central. This SDK is still in beta, and we are actively making changes to the API shape. While we iterate on the design, we would love your thoughts and feedback!
As you try the new SDK and put it through its paces, please post your thoughts and feedback on this thread. The SDK team will be actively monitoring this thread for your input, and will look to incorporate it into our ongoing development and design efforts.
Thanks for your help, and we hope to make a Java SDK you’ll love using.
5 Likes
Great sdk, but I’m getting an error when I add parameters to funtions ChatCompletionCreateParams.Function, the putAdditionalProperty required a JsonValue so when I add the Schema as JsonValue I get a BadRequestException 400 schema must be a JSON Schema of ‘type: “object”’, got ‘type: “None”’
The code block of the code I’m runnig
JsonNode parsedSchema;
byte[] schemaBytes = nameSchema.toString().getBytes(StandardCharsets.UTF_8); // Get the byte array.
if (schemaBytes == null || schemaBytes.length == 0) {
System.err.println("Schema bytes are missing! Using an empty schema.");
parsedSchema = objectMapper.createObjectNode(); // Default to an empty schema.
} else {
try {
String schemaString = new String(schemaBytes, StandardCharsets.UTF_8); // Convert bytes to string, specifying UTF-8.
parsedSchema = objectMapper.readTree(schemaString);
} catch (JsonProcessingException e) {
System.err.println("Invalid schema: " + e.getMessage());
parsedSchema = objectMapper.createObjectNode();
}
}
if (parsedSchema.isMissingNode()) {
System.err.println("Schema is missing! Using an empty schema.");
}
ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.setAll((ObjectNode) parsedSchema);
JsonValue jsonValueSchema = JsonValue.fromJsonNode(objectNode);
System.out.println("Passed in parameters as "+ JsonValue.fromJsonNode(nameSchema));
ChatCompletionCreateParams.Function functionName = ChatCompletionCreateParams.Function.builder()
.name("buscar_cliente_x_nombre")
.description("Buscar un cliente por nombre o razon social.")
.parameters(FunctionParameters.builder().putAdditionalProperty("nombre",jsonValueSchema).build())
.build();
Any help is appreciated, I’ve been trying to figure it out but I’m stuck.
Thanks
Do you all have any guidance of how to deserialize a “ChatCompletion” object after serializing and storing it as JSON? Jackson has no problem serializing it to a JSON string, but deserializing back into a ChatCompletion is proving to be quite challenging. I have tried using Jackson MixIns and custom JsonField and JsonValue deserializers to assist but I still cannot get it working. Do you all have any tooling around this with the SDK?
You can probably use:
com.openai.core.ObjectMappers.jsonMapper().readValue(input, ChatCompletion.class)
what has changed here? my code worked a few days ago and now im getting an OpenAIInvalidDataException: message
is invalid, received {role=assistant, content=Hello! That’s correct; I have knowledge up to October 2023. How can I assist you today?, refusal=null, annotations=}
caused by this line where I call .message() - Optional aiResponseOptional = chatCompletion.choices().get(0).message().content();
This is a bug caused by depending on an older Jackson version than the one required by the SDK.
Upgrade to at least 2.18.1. See here for more info: Depending on older Jackson version causes deserialization to fail with extraneous fields · Issue #301 · openai/openai-java · GitHub
We are looking into making this less likely to happen for users.
3 Likes
ah, that was it. thanks for getting back so quickly, appreciate the help