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
// src/core/tools/structured_output.rs //! Structured output utilities use crate::core::language_models::LLMResult; use schemars::JsonSchema; use serde::de::DeserializeOwned; use std::marker::PhantomData; pub struct StructuredOutput<T> { result: LLMResult, schema: serde_json::Value, _phantom: PhantomData<T>, } impl<T: DeserializeOwned + JsonSchema> StructuredOutput<T> { pub fn new(result: LLMResult) -> Self { use schemars::schema_for; let schema = serde_json::to_value(schema_for!(T)).unwrap_or(serde_json::Value::Null); Self { result, schema, _phantom: PhantomData, } } pub fn parse(&self) -> Result<T, serde_json::Error> { serde_json::from_str(&self.result.content) } pub fn raw_content(&self) -> &str { &self.result.content } pub fn schema(&self) -> &serde_json::Value { &self.schema } }