Skip to main content

chat_core/chat/
mod.rs

1use tools_rs::ToolCollection;
2
3use crate::{
4    chat::state::Unstructured,
5    error::ChatError,
6    types::{
7        callback::{CallbackStrategy, RetryStrategy},
8        messages::{
9            content::Content,
10            parts::{PartEnum, Parts},
11        },
12        options::ChatOptions,
13    },
14};
15
16pub mod completion;
17pub mod embed;
18pub mod state;
19#[cfg(feature = "stream")]
20pub mod stream;
21
22#[derive(Default)]
23pub struct Chat<CP, Output = Unstructured> {
24    pub(crate) model: CP,
25    pub(crate) output_shape: Option<schemars::Schema>,
26    pub(crate) model_options: Option<ChatOptions>,
27    pub(crate) max_steps: Option<u16>,
28    pub(crate) max_retries: Option<u16>,
29    pub(crate) retry_strategy: Option<RetryStrategy>,
30    pub(crate) before_strategy: Option<CallbackStrategy>,
31    pub(crate) after_strategy: Option<CallbackStrategy>,
32    pub(crate) tools: Option<ToolCollection>,
33    pub(crate) _output: std::marker::PhantomData<Output>,
34}
35
36impl<P, Output> Chat<P, Output> {
37    pub(crate) async fn tool_call(&self, content: &Content) -> Result<Parts, ChatError> {
38        let mut frs: Parts = Parts::default();
39        for fc in content.parts.function_calls() {
40            frs.push(PartEnum::from_function_response(
41                self.tools
42                    .as_ref()
43                    .ok_or(ChatError::InvalidResponse(
44                        "Attempted to call tool but no tool collection has been set.".to_string(),
45                    ))?
46                    .call(fc.clone())
47                    .await
48                    .map_err(|_err| ChatError::InvalidResponse("Tools error".to_string()))?,
49            ));
50        }
51        Ok(frs)
52    }
53}