This repository was archived by the owner on Apr 14, 2026. It is now read-only.
forked from Abraxas-365/langchain-rust
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstructured_output.rs
More file actions
50 lines (42 loc) · 1.58 KB
/
structured_output.rs
File metadata and controls
50 lines (42 loc) · 1.58 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
use langchain_ai_rust::{
agent::create_agent_with_structured_output,
schemas::structured_output::{StructuredOutputSchema, ToolStrategy},
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
/// Example structured output schema for contact information.
#[derive(Serialize, Deserialize, JsonSchema, Debug)]
struct ContactInfo {
/// The name of the person
name: String,
/// The email address
email: String,
/// The phone number
phone: String,
}
impl StructuredOutputSchema for ContactInfo {}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create an agent with structured output using ToolStrategy
let strategy = ToolStrategy::<ContactInfo>::new()
.with_tool_message_content("Contact information extracted successfully!".to_string());
let agent = create_agent_with_structured_output(
"gpt-4o-mini",
&[],
Some("You are a helpful assistant that extracts contact information from text."),
Some(Box::new(strategy)),
None, // Middleware (optional)
)?;
// Use the agent to extract structured information
let result = agent
.invoke_messages(vec![
langchain_ai_rust::schemas::Message::new_human_message(
"Extract contact info from: John Doe, [email protected], (555) 123-4567",
),
])
.await?;
println!("Agent response: {}", result);
// Note: In a full implementation, the structured_response would be available
// in the agent state after execution. This is a conceptual example.
Ok(())
}