forked from graniet/llm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_schema_nested_example.rs
More file actions
186 lines (173 loc) · 8.26 KB
/
json_schema_nested_example.rs
File metadata and controls
186 lines (173 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/// Example demonstrating complex nested JSON schema handling with LLM function calls
///
/// This example shows how to:
/// - Define a complex JSON schema for event creation with nested data structures
/// - Process chat messages with function calls
/// - Handle nested JSON responses
/// - Manage a multi-turn conversation with tool results
use llm::{
builder::{FunctionBuilder, LLMBackend, LLMBuilder},
chat::ChatMessage,
FunctionCall, ToolCall,
};
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = std::env::var("GOOGLE_API_KEY").unwrap_or("test-key".to_string());
let llm = LLMBuilder::new()
.backend(LLMBackend::Google)
.api_key(api_key)
.model("gemini-2.0-flash")
.function(
FunctionBuilder::new("create_event")
.description("Creates a complex event with deeply nested data structures")
.json_schema(json!({
"type": "object",
"properties": {
"event": {
"type": "object",
"properties": {
"title": {"type": "string"},
"location": {
"type": "object",
"properties": {
"venue": {"type": "string"},
"address": {
"type": "object",
"properties": {
"street": {"type": "string"},
"city": {"type": "string"},
"coordinates": {
"type": "object",
"properties": {
"lat": {"type": "number"},
"lng": {"type": "number"}
}
}
}
}
}
},
"attendees": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"email": {"type": "string"},
"role": {"type": "string"},
"preferences": {
"type": "object",
"properties": {
"dietary": {"type": "string"},
"accessibility": {"type": "boolean"},
"notifications": {
"type": "object",
"properties": {
"email": {"type": "boolean"},
"sms": {"type": "boolean"},
"schedule": {
"type": "array",
"items": {
"type": "object",
"properties": {
"time": {"type": "string"},
"type": {"type": "string"}
}
}
}
}
}
}
}
}
}
}
}
}
},
"required": ["event"]
})),
)
.build()?;
let messages = vec![ChatMessage::user()
.content("Create a team meeting at Google HQ in Mountain View for Alice (alice@corp.com, manager, vegetarian, needs accessibility, wants email and SMS notifications 1 hour before) and Bob (bob@corp.com, developer, no dietary restrictions, only email notifications 30 minutes before)")
.build()];
let response = llm.chat_with_tools(&messages, llm.tools()).await?;
if let Some(tool_calls) = response.tool_calls() {
println!("Complex nested schema handled successfully!");
for call in &tool_calls {
println!("Function: {}", call.function.name);
let args: serde_json::Value = serde_json::from_str(&call.function.arguments)?;
println!("Nested arguments: {}", serde_json::to_string_pretty(&args)?);
let result = process_tool_call(call)?;
println!("Result: {}", serde_json::to_string_pretty(&result)?);
}
let mut conversation = messages;
conversation.push(
ChatMessage::assistant()
.tool_use(tool_calls.clone())
.build(),
);
let tool_results: Vec<ToolCall> = tool_calls
.iter()
.map(|call| {
let result = process_tool_call(call).unwrap();
ToolCall {
id: call.id.clone(),
call_type: "function".to_string(),
function: FunctionCall {
name: call.function.name.clone(),
arguments: serde_json::to_string(&result).unwrap(),
},
}
})
.collect();
conversation.push(ChatMessage::user().tool_result(tool_results).build());
let final_response = llm.chat_with_tools(&conversation, llm.tools()).await?;
println!("\nFinal response: {final_response}");
} else {
println!("Direct response: {response}");
}
Ok(())
}
/// Processes a tool call and returns a simulated response
///
/// # Arguments
/// * `tool_call` - The tool call to process containing function name and arguments
///
/// # Returns
/// * JSON response containing event details or error message
fn process_tool_call(
tool_call: &ToolCall,
) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
match tool_call.function.name.as_str() {
"create_event" => {
let args: serde_json::Value = serde_json::from_str(&tool_call.function.arguments)?;
Ok(json!({
"event_id": "evt_12345",
"status": "created",
"created_at": "2025-01-06T10:30:00Z",
"calendar_links": {
"google": "https://calendar.google.com/event/evt_12345",
"outlook": "https://outlook.com/event/evt_12345"
},
"notifications_scheduled": args["event"]["attendees"]
.as_array()
.unwrap_or(&vec![])
.iter()
.map(|attendee| {
json!({
"attendee": attendee["email"],
"notifications": attendee["preferences"]["notifications"]
})
})
.collect::<Vec<_>>()
}))
}
_ => Ok(json!({
"error": "Unknown function",
"function": tool_call.function.name
})),
}
}