agent_chain_core/
lib.rs

1//! Agent Chain Core - A Rust implementation of LangChain core library.
2//!
3//! This crate provides:
4//! - Message types for LLM conversations (human, AI, system, tool)
5//! - Tool trait and `#[tool]` macro for function calling
6//! - Chat model abstractions
7//! - Callback system for tracking and monitoring operations
8//! - Prompt templates for flexible prompt construction
9//! - Support for multiple providers (Anthropic, OpenAI, etc.)
10//!
11//! # Architecture
12//!
13//! The architecture follows LangChain's pattern:
14//!
15//! - **Core layer** ([`chat_models`]): Base `ChatModel` trait that all providers implement
16//! - **Message layer** ([`messages`]): Message types for conversations
17//! - **Prompts layer** ([`prompts`]): Prompt templates for constructing prompts
18//! - **Tools layer** ([`tools`]): Tool definitions and the `#[tool]` macro
19//! - **Callbacks layer** ([`callbacks`]): Callback handlers and managers for monitoring
20//!
21//! # Feature Flags
22//!
23//! - `default`: Includes all providers
24//! - `specta`: Specta derive support
25
26pub mod api;
27pub mod caches;
28pub mod callbacks;
29pub mod chat_history;
30pub mod chat_loaders;
31pub mod chat_sessions;
32pub mod documents;
33pub mod env;
34pub mod error;
35pub mod globals;
36pub mod language_models;
37pub mod load;
38pub mod messages;
39pub mod output_parsers;
40pub mod outputs;
41pub mod prompt_values;
42pub mod prompts;
43pub mod rate_limiters;
44pub mod retrievers;
45pub mod runnables;
46pub mod stores;
47pub mod structured_query;
48pub mod sys_info;
49pub mod tools;
50pub mod tracers;
51pub mod utils;
52
53// Keep chat_models as a backward-compatible re-export
54pub mod chat_models {
55    //! Re-export of language_models types for backward compatibility.
56    //!
57    //! This module re-exports types from [`language_models`] to maintain
58    //! backward compatibility with code using the old `chat_models` module.
59    //!
60    //! New code should use [`language_models`] directly.
61
62    pub use crate::language_models::{
63        BaseChatModel, ChatChunk, ChatModelConfig, ChatStream, DisableStreaming, LangSmithParams,
64        ToolChoice, UsageMetadata,
65    };
66
67    // Re-export ChatResult from outputs for backward compatibility
68    pub use crate::outputs::ChatResult;
69}
70
71// Re-export env types
72pub use env::{VERSION, get_runtime_environment};
73
74// Re-export error types
75pub use error::{Error, Result};
76
77// Re-export core language model types
78pub use language_models::{
79    // Chat model types
80    AIMessageChunkStream,
81    BaseChatModel,
82    // LLM types
83    BaseLLM,
84    // Base types
85    BaseLanguageModel,
86    ChatChunk,
87    ChatGenerationStream,
88    ChatModelConfig,
89    ChatStream,
90    DisableStreaming,
91    FakeListChatModel,
92    FakeListChatModelError,
93    // Fake implementations for testing
94    FakeListLLM,
95    FakeListLLMError,
96    FakeMessagesListChatModel,
97    FakeStreamingListLLM,
98    GenericFakeChatModel,
99    LLM,
100    LLMConfig,
101    LangSmithParams,
102    LanguageModelConfig,
103    LanguageModelInput,
104    LanguageModelOutput,
105
106    // Model profile types
107    ModelProfile,
108    ModelProfileRegistry,
109
110    OpenAiDataBlockFilter,
111    ParrotFakeChatModel,
112
113    ParsedDataUri,
114    SimpleChatModel,
115    ToolChoice,
116    UsageMetadata,
117    agenerate_from_stream,
118    collect_and_merge_stream,
119    generate_from_stream,
120
121    get_prompts_from_cache,
122    // Utility functions
123    is_openai_data_block,
124    parse_data_uri,
125    update_cache,
126};
127
128// Re-export message types
129pub use messages::{
130    AIMessage, AnyMessage, BaseMessage, ContentPart, HasId, HumanMessage, ImageDetail, ImageSource,
131    MessageContent, SystemMessage, ToolCall, ToolMessage,
132};
133
134// Re-export tool types
135pub use tools::{BaseTool, Tool, ToolDefinition};
136
137// Re-export chat history types
138pub use chat_history::{
139    AIMessageInput, BaseChatMessageHistory, HumanMessageInput, InMemoryChatMessageHistory,
140};
141
142// Re-export chat session types
143pub use chat_sessions::ChatSession;
144
145// Re-export chat loader types
146pub use chat_loaders::BaseChatLoader;
147
148// Re-export cache types
149pub use caches::{BaseCache, CacheReturnValue, InMemoryCache};
150
151// Re-export global functions
152pub use globals::{get_debug, get_llm_cache, get_verbose, set_debug, set_llm_cache, set_verbose};
153
154// Re-export output parser types
155pub use output_parsers::{
156    BaseCumulativeTransformOutputParser, BaseLLMOutputParser, BaseOutputParser,
157    BaseTransformOutputParser, CommaSeparatedListOutputParser, JsonOutputParser, ListOutputParser,
158    MarkdownListOutputParser, NumberedListOutputParser, OutputParserError, SimpleJsonOutputParser,
159    StrOutputParser, XMLOutputParser,
160};
161
162// Re-export output types
163pub use outputs::{
164    ChatGeneration, ChatGenerationChunk, ChatResult, Generation, GenerationChunk, GenerationType,
165    LLMResult, RunInfo, merge_chat_generation_chunks,
166};
167
168// Re-export callback types
169pub use callbacks::{
170    AsyncCallbackHandler, AsyncCallbackManager, AsyncCallbackManagerForChainRun,
171    AsyncCallbackManagerForLLMRun, BaseCallbackHandler, BaseCallbackManager, CallbackManager,
172    CallbackManagerForChainRun, CallbackManagerForLLMRun, Callbacks, StdOutCallbackHandler,
173    StreamingStdOutCallbackHandler, UsageMetadataCallbackHandler, add_usage,
174};
175
176// Re-export prompt types
177pub use prompts::{
178    AIMessagePromptTemplate, BaseChatPromptTemplate, BaseMessagePromptTemplate, BasePromptTemplate,
179    ChatMessagePromptTemplate, ChatPromptTemplate, DictPromptTemplate,
180    FewShotChatMessagePromptTemplate, FewShotPromptTemplate, FewShotPromptWithTemplates,
181    HumanMessagePromptTemplate, ImagePromptTemplate, MessagesPlaceholder, PromptTemplate,
182    PromptTemplateFormat, StringPromptTemplate, SystemMessagePromptTemplate, load_prompt,
183};
184
185// Re-export load types
186pub use load::{
187    ConstructorInfo, RevivedValue, Reviver, ReviverConfig, Serializable, Serialized,
188    SerializedConstructor, SerializedNotImplemented, SerializedSecret, dumpd, dumps,
189    load as load_json, loads,
190};
191
192// Re-export prompt value types
193pub use prompt_values::{
194    ChatPromptValue, ChatPromptValueConcrete, ImageDetailLevel, ImagePromptValue, ImageURL,
195    PromptValue, StringPromptValue,
196};
197
198// Re-export tracer types
199pub use tracers::{
200    AsyncBaseTracer, AsyncListener, AsyncRootListenersTracer, BaseTracer, ConsoleCallbackHandler,
201    FunctionCallbackHandler, Listener, PassthroughStreamingHandler, RootListenersTracer, Run,
202    RunCollectorCallbackHandler, RunEvent, RunType, SchemaFormat, StreamingCallbackHandler,
203    TracerCore, TracerCoreConfig, TracerError,
204};
205
206// Re-export rate limiter types
207pub use rate_limiters::{BaseRateLimiter, InMemoryRateLimiter, InMemoryRateLimiterConfig};
208
209// Re-export document types
210pub use documents::{
211    BaseDocumentCompressor, BaseDocumentTransformer, BaseMedia, Blob, BlobBuilder, BlobData,
212    Document, FilterTransformer, FunctionTransformer,
213};
214
215// Re-export retriever types
216pub use retrievers::{
217    BaseRetriever, DynRetriever, FilterRetriever, LangSmithRetrieverParams, RetrieverInput,
218    RetrieverOutput, SimpleRetriever,
219};
220
221// Re-export store types
222pub use stores::{
223    BaseStore, InMemoryBaseStore, InMemoryByteStore, InMemoryStore, InvalidKeyException,
224};
225
226// Re-export runnable types
227pub use runnables::{
228    AddableDict, BaseStreamEvent, CUSTOM_EVENT_TYPE, ConfigOrList, CustomStreamEvent,
229    DynRouterRunnable, DynRunnable, EventData, PickKeys, RouterInput, RouterRunnable, Runnable,
230    RunnableAssign, RunnableAssignBuilder, RunnableBinding, RunnableConfig, RunnableEach,
231    RunnableLambda, RunnableParallel, RunnablePassthrough, RunnablePick, RunnableRetry,
232    RunnableSequence, RunnableSerializable, StandardStreamEvent, StreamEvent, coerce_to_runnable,
233    ensure_config, get_config_list, graph_passthrough, merge_configs, patch_config, pipe,
234    runnable_lambda, to_dyn,
235};
236
237// Re-export structured query types
238pub use structured_query::{
239    Comparator, Comparison, Expr, FilterDirective, FilterDirectiveEnum, Operation, Operator,
240    OperatorOrComparator, StructuredQuery, Visitor,
241};
242
243// Re-export sys_info types
244pub use sys_info::{PackageInfo, SystemInfo, get_sys_info, get_sys_info_map, print_sys_info};
245
246// Re-export async_trait for use in generated code
247pub use async_trait::async_trait;