Skip to content
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

Feature #896: Support custom-type tool parameter #941

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

limoiie
Copy link

@limoiie limoiie commented Apr 16, 2024

Context

Langchain4j currently only supports Java built-in types as tool parameters.
While it's advisable to maintain a flat and simple tool interface,
there are situations where using custom-type tool parameters is preferred.

Change

This PR delegates the generation of ToolSpecification and the deserialization of ToolExecutionRequest's arguments to ToolJsonSchemas using JsonSchemaService. JsonSchemaService is a separate pluggable dependency via SPI. This PR created two implementations for JsonSchemaService:

  • DefaultJsonSchemaService (in module langchain4j-core) is consistent with the old behavior of Langchain4j. It supports only Java Built-in types as tool parameters, and sanitizes the tool execution arguments before deserialization
  • VictoolsJsonSchemaService (in a separated model langchain4j-jsonschema-service-victools) adopts victools/jsonschema-generator for JSON Schema generation and arguments deserialization.

Besides SPI, the user can override the default by customizing an implementation of JsonSchemaService via AiServices#jsonSchemaService

The original ToolSpecification is kept for now to stay compatible with the current codebase. It might be replaced by ToolJsonSchema in the future if needed.

Checklist

Before submitting this PR, please check the following points:

  • I have added unit and integration tests for my change
  • All unit and integration tests in the module I have added/changed are green
  • All unit and integration tests in the core and main modules are green
  • I have added/updated the documentation
  • I have added an example in the examples repo (only for "big" features)
  • I have added my new module in the BOM (only when a new module is added)

…gchain4j#896)

This commit adds the JsonSchemaService concept to:
-  Create JsonSchemas for types of tool parameters, which will form ToolSpecification
-  Convert String arguments in ToolExecutionRequest to the specified type instance.

JSON Schema defines JSON data structure for validation and documentation.
When using OpenAI tools/functions,
you need to define the tool specifications in JSON Schema,
and to parse the response from the language model with respect to that JSON Schema.
By using JsonSchemaService instead of the old method,
we can utilize an existing open-source JsonSchema generator and validator for tool/function calls.
The DefaultJsonSchemaService is implemented to be consistent with the old behavior. It:
- Support only Java Built-in types as tool parameters
- Sanitize the tool execution arguments before deserialization
hotfix: Make jsonschema-module-jackson dependency required

hotfix: Throw exception if meet custom type
@langchain4j
Copy link
Owner

Issue: #896

@langchain4j langchain4j added the P3 Medium priority label Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 Medium priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants