rig/agent/
mod.rs

1//! This module contains the implementation of the [Agent] struct and its builder.
2//!
3//! The [Agent] struct represents an LLM agent, which combines an LLM model with a preamble (system prompt),
4//! a set of context documents, and a set of tools. Note: both context documents and tools can be either
5//! static (i.e.: they are always provided) or dynamic (i.e.: they are RAGged at prompt-time).
6//!
7//! The [Agent] struct is highly configurable, allowing the user to define anything from
8//! a simple bot with a specific system prompt to a complex RAG system with a set of dynamic
9//! context documents and tools.
10//!
11//! The [Agent] struct implements the [crate::completion::Completion] and [crate::completion::Prompt] traits,
12//! allowing it to be used for generating completions responses and prompts. The [Agent] struct also
13//! implements the [crate::completion::Chat] trait, which allows it to be used for generating chat completions.
14//!
15//! The [AgentBuilder] implements the builder pattern for creating instances of [Agent].
16//! It allows configuring the model, preamble, context documents, tools, temperature, and additional parameters
17//! before building the agent.
18//!
19//! # Example
20//! ```rust
21//! use rig::{
22//!     completion::{Chat, Completion, Prompt},
23//!     providers::openai,
24//! };
25//!
26//! let openai = openai::Client::from_env();
27//!
28//! // Configure the agent
29//! let agent = openai.agent("gpt-4o")
30//!     .preamble("System prompt")
31//!     .context("Context document 1")
32//!     .context("Context document 2")
33//!     .tool(tool1)
34//!     .tool(tool2)
35//!     .temperature(0.8)
36//!     .additional_params(json!({"foo": "bar"}))
37//!     .build();
38//!
39//! // Use the agent for completions and prompts
40//! // Generate a chat completion response from a prompt and chat history
41//! let chat_response = agent.chat("Prompt", chat_history)
42//!     .await
43//!     .expect("Failed to chat with Agent");
44//!
45//! // Generate a prompt completion response from a simple prompt
46//! let chat_response = agent.prompt("Prompt")
47//!     .await
48//!     .expect("Failed to prompt the Agent");
49//!
50//! // Generate a completion request builder from a prompt and chat history. The builder
51//! // will contain the agent's configuration (i.e.: preamble, context documents, tools,
52//! // model parameters, etc.), but these can be overwritten.
53//! let completion_req_builder = agent.completion("Prompt", chat_history)
54//!     .await
55//!     .expect("Failed to create completion request builder");
56//!
57//! let response = completion_req_builder
58//!     .temperature(0.9) // Overwrite the agent's temperature
59//!     .send()
60//!     .await
61//!     .expect("Failed to send completion request");
62//! ```
63//!
64//! RAG Agent example
65//! ```rust
66//! use rig::{
67//!     completion::Prompt,
68//!     embeddings::EmbeddingsBuilder,
69//!     providers::openai,
70//!     vector_store::{in_memory_store::InMemoryVectorStore, VectorStore},
71//! };
72//!
73//! // Initialize OpenAI client
74//! let openai = openai::Client::from_env();
75//!
76//! // Initialize OpenAI embedding model
77//! let embedding_model = openai.embedding_model(openai::TEXT_EMBEDDING_ADA_002);
78//!
79//! // Create vector store, compute embeddings and load them in the store
80//! let mut vector_store = InMemoryVectorStore::default();
81//!
82//! let embeddings = EmbeddingsBuilder::new(embedding_model.clone())
83//!     .simple_document("doc0", "Definition of a *flurbo*: A flurbo is a green alien that lives on cold planets")
84//!     .simple_document("doc1", "Definition of a *glarb-glarb*: A glarb-glarb is a ancient tool used by the ancestors of the inhabitants of planet Jiro to farm the land.")
85//!     .simple_document("doc2", "Definition of a *linglingdong*: A term used by inhabitants of the far side of the moon to describe humans.")
86//!     .build()
87//!     .await
88//!     .expect("Failed to build embeddings");
89//!
90//! vector_store.add_documents(embeddings)
91//!     .await
92//!     .expect("Failed to add documents");
93//!
94//! // Create vector store index
95//! let index = vector_store.index(embedding_model);
96//!
97//! let agent = openai.agent(openai::GPT_4O)
98//!     .preamble("
99//!         You are a dictionary assistant here to assist the user in understanding the meaning of words.
100//!         You will find additional non-standard word definitions that could be useful below.
101//!     ")
102//!     .dynamic_context(1, index)
103//!     .build();
104//!
105//! // Prompt the agent and print the response
106//! let response = agent.prompt("What does \"glarb-glarb\" mean?").await
107//!     .expect("Failed to prompt the agent");
108//! ```
109mod builder;
110mod completion;
111pub(crate) mod prompt_request;
112mod tool;
113
114pub use crate::message::Text;
115pub use builder::{AgentBuilder, AgentBuilderSimple};
116pub use completion::Agent;
117pub use prompt_request::streaming::{
118    FinalResponse, MultiTurnStreamItem, StreamingPromptRequest, stream_to_stdout,
119};
120pub use prompt_request::{CancelSignal, PromptRequest, PromptResponse};
121pub use prompt_request::{PromptHook, StreamingPromptHook};