Include root schema as parameters for tool calling (#10914)

Allows `LanguageModelTool`s to include nested structures, by exposing
the definitions section of their JSON Schema.

Release Notes:

- N/A
This commit is contained in:
Kyle Kelley 2024-04-23 20:49:29 -07:00 committed by GitHub
parent 25981550d5
commit af5a9fabc6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 241 additions and 5 deletions

View file

@ -256,7 +256,7 @@ mod test {
let expected = ToolFunctionDefinition {
name: "get_current_weather".to_string(),
description: "Fetches the current weather for a given location.".to_string(),
parameters: schema_for!(WeatherQuery).schema,
parameters: schema_for!(WeatherQuery),
};
assert_eq!(tools[0].name, expected.name);
@ -267,6 +267,7 @@ mod test {
assert_eq!(
expected_schema,
json!({
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "WeatherQuery",
"type": "object",
"properties": {

View file

@ -1,8 +1,11 @@
use anyhow::Result;
use gpui::{div, AnyElement, AppContext, Element, ParentElement as _, Task, WindowContext};
use schemars::{schema::SchemaObject, schema_for, JsonSchema};
use schemars::{schema::RootSchema, schema_for, JsonSchema};
use serde::Deserialize;
use std::{any::Any, fmt::Debug};
use std::{
any::Any,
fmt::{Debug, Display},
};
#[derive(Default, Deserialize)]
pub struct ToolFunctionCall {
@ -89,7 +92,17 @@ impl ToolFunctionCallResult {
pub struct ToolFunctionDefinition {
pub name: String,
pub description: String,
pub parameters: SchemaObject,
pub parameters: RootSchema,
}
impl Display for ToolFunctionDefinition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let schema = serde_json::to_string(&self.parameters).ok();
let schema = schema.unwrap_or("None".to_string());
write!(f, "Name: {}:\n", self.name)?;
write!(f, "Description: {}\n", self.description)?;
write!(f, "Parameters: {}", schema)
}
}
impl Debug for ToolFunctionDefinition {
@ -124,10 +137,12 @@ pub trait LanguageModelTool {
/// The OpenAI Function definition for the tool, for direct use with OpenAI's API.
fn definition(&self) -> ToolFunctionDefinition {
let root_schema = schema_for!(Self::Input);
ToolFunctionDefinition {
name: self.name(),
description: self.description(),
parameters: schema_for!(Self::Input).schema,
parameters: root_schema,
}
}