use std::marker::PhantomData;
use schemars::JsonSchema;
use serde::{Serialize, de::DeserializeOwned};
use rig_core::{
message::{Message, ToolChoice},
vector_store::{VectorStoreIndex, request::DynamicSearchFilter},
wasm_compat::{WasmCompatSend, WasmCompatSync},
};
use crate::agent::{Agent, AgentBuilder, AgentHook, ModelRef, OutputMode, TypedRun};
const SUBMIT_TOOL_NAME: &str = "submit";
pub struct Extractor<T>
where
T: JsonSchema + DeserializeOwned + WasmCompatSend + WasmCompatSync,
{
agent: Agent,
retries: usize,
_t: PhantomData<T>,
}
impl<T> Extractor<T>
where
T: JsonSchema + DeserializeOwned + WasmCompatSend + WasmCompatSync,
{
pub fn with_model_label(mut self, label: impl Into<ModelRef>) -> Self {
self.agent.set_model_label(label);
self
}
pub fn with_model(
mut self,
model: impl Into<rig_core::DynModel<rig_core::operation::Completion>>,
) -> Self {
self.agent.set_model(model);
self
}
pub fn agent(&self) -> &Agent {
&self.agent
}
pub fn extract(&self, text: impl Into<Message>) -> TypedRun<T> {
let runner = self.agent.prompt(text).max_turns(1).output_tool(
SUBMIT_TOOL_NAME,
"Submit the structured data you extracted from the provided text.",
false,
);
TypedRun::output_tool(runner).retries(self.retries)
}
}
pub struct ExtractorBuilder<T>
where
T: JsonSchema + DeserializeOwned + Serialize + WasmCompatSend + WasmCompatSync + 'static,
{
agent_builder: AgentBuilder,
_t: PhantomData<T>,
retries: Option<usize>,
}
macro_rules! forward_agent_builder {
($( $(#[$attr:meta])* $name:ident $([$gen:ident : $($bound:tt)+])?
( $($arg:ident : $ty:ty),* );)+) => {$(
$(#[$attr])*
pub fn $name $(<$gen>)? (mut self, $($arg: $ty),*) -> Self
$(where $gen: $($bound)+)?
{
self.agent_builder = self.agent_builder.$name($($arg),*);
self
}
)+};
}
impl<T> ExtractorBuilder<T>
where
T: JsonSchema + DeserializeOwned + Serialize + WasmCompatSend + WasmCompatSync + 'static,
{
pub fn new(model: impl Into<rig_core::DynModel<rig_core::operation::Completion>>) -> Self {
Self::from_agent_builder(AgentBuilder::new(model))
}
pub fn from_agent_builder(builder: AgentBuilder) -> Self {
Self {
agent_builder: builder
.preamble("\
You are an AI assistant whose purpose is to extract structured data from the provided text.\n\
You will have access to a `submit` function that defines the structure of the data to extract from the provided text.\n\
Use the `submit` function to submit the structured data.\n\
Be sure to fill out every field and ALWAYS CALL THE `submit` function, even with default values!!!.
")
.output_schema::<T>()
.tool_choice(ToolChoice::Required)
.output_mode(OutputMode::Tool),
retries: None,
_t: PhantomData,
}
}
pub fn append_preamble(mut self, preamble: &str) -> Self {
self.agent_builder = self.agent_builder.append_preamble(&format!(
"\n=============== ADDITIONAL INSTRUCTIONS ===============\n{preamble}"
));
self
}
forward_agent_builder! {
context(doc: &str);
additional_params(params: serde_json::Value);
max_tokens(max_tokens: u64);
add_hook[H: AgentHook + 'static](hook: H);
}
pub fn dynamic_context<I, F>(mut self, samples: usize, index: I) -> Self
where
I: VectorStoreIndex<Filter = F> + 'static,
F: DynamicSearchFilter + WasmCompatSend + WasmCompatSync + 'static,
{
self.agent_builder = self.agent_builder.dynamic_context(samples, index);
self
}
pub fn retries(mut self, retries: usize) -> Self {
self.retries = Some(retries);
self
}
pub fn build(self) -> Extractor<T> {
Extractor {
agent: self.agent_builder.build(),
_t: PhantomData,
retries: self.retries.unwrap_or(0),
}
}
}
#[cfg(test)]
mod tests;