generated from kpavlov/awesome-kotlin-maven-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Support JSON schema for function parameters in chat models #413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
ai-mocks-core/src/commonMain/kotlin/me/kpavlov/aimocks/core/json/schema/SchemaHelper.kt
This file contains hidden or 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,116 @@ | ||
package me.kpavlov.aimocks.core.json.schema | ||
|
||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonElement | ||
|
||
/** | ||
* Helper object for working with JSON schemas. | ||
* | ||
* Provides utility functions for parsing and validating JSON Schema definitions. | ||
*/ | ||
public object SchemaHelper { | ||
private val json = | ||
Json { | ||
ignoreUnknownKeys = true | ||
} | ||
|
||
/** | ||
* Parses a [JsonElement] into a [SchemaDefinition]. | ||
* | ||
* @param schemaJson The JSON element containing the schema | ||
* @return The parsed schema definition, or null if parsing fails | ||
*/ | ||
public fun parseSchema(schemaJson: JsonElement?): SchemaDefinition? = | ||
schemaJson?.let { | ||
try { | ||
json.decodeFromJsonElement(SchemaDefinition.serializer(), it) | ||
} catch (_: SerializationException) { | ||
null | ||
} | ||
} | ||
|
||
/** | ||
* Checks if a schema has a property with the specified name. | ||
* | ||
* @param schema The schema definition to check | ||
* @param propertyName The name of the property | ||
* @return true if the property exists, false otherwise | ||
*/ | ||
public fun hasProperty( | ||
schema: SchemaDefinition, | ||
propertyName: String, | ||
): Boolean = schema.properties.containsKey(propertyName) | ||
|
||
/** | ||
* Gets the types of a property in the schema. | ||
* | ||
* @param schema The schema definition | ||
* @param propertyName The name of the property | ||
* @return The list of types for the property, or null if not found or not a value property | ||
*/ | ||
public fun getPropertyType( | ||
schema: SchemaDefinition, | ||
propertyName: String, | ||
): List<String>? = | ||
schema.properties[propertyName]?.let { prop -> | ||
when (prop) { | ||
is ValuePropertyDefinition -> prop.type | ||
else -> null | ||
} | ||
} | ||
|
||
/** | ||
* Checks if a property is required in the schema. | ||
* | ||
* @param schema The schema definition | ||
* @param propertyName The name of the property | ||
* @return true if the property is required, false otherwise | ||
*/ | ||
public fun isPropertyRequired( | ||
schema: SchemaDefinition, | ||
propertyName: String, | ||
): Boolean = schema.required.contains(propertyName) | ||
|
||
/** | ||
* Checks if all specified properties are required in the schema. | ||
* | ||
* @param schema The schema definition | ||
* @param propertyNames The names of properties to check | ||
* @return true if all properties are required, false otherwise | ||
*/ | ||
public fun hasAllRequiredProperties( | ||
schema: SchemaDefinition, | ||
vararg propertyNames: String, | ||
): Boolean = propertyNames.all { it in schema.required } | ||
|
||
/** | ||
* Gets the property definition for a given property name. | ||
* | ||
* @param schema The schema definition | ||
* @param propertyName The name of the property | ||
* @return The property definition, or null if not found | ||
*/ | ||
public fun getProperty( | ||
schema: SchemaDefinition, | ||
propertyName: String, | ||
): PropertyDefinition? = schema.properties[propertyName] | ||
|
||
/** | ||
* Gets the description of a property in the schema. | ||
* | ||
* @param schema The schema definition | ||
* @param propertyName The name of the property | ||
* @return The description of the property, or null if not found | ||
*/ | ||
public fun getPropertyDescription( | ||
schema: SchemaDefinition, | ||
propertyName: String, | ||
): String? = | ||
schema.properties[propertyName]?.let { prop -> | ||
when (prop) { | ||
is ValuePropertyDefinition -> prop.description | ||
else -> null | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Consider returning this for fluent API consistency
seed(...) is chainable; these new methods return Unit. Returning the spec improves ergonomics without behavior change.
📝 Committable suggestion
🤖 Prompt for AI Agents