Skip to main content

rig_core/client/
completion.rs

1use crate::agent::AgentBuilder;
2use crate::completion::CompletionModel;
3use crate::extractor::ExtractorBuilder;
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7/// A provider client with completion capabilities.
8/// Clone is required for conversions between client types.
9pub trait CompletionClient {
10    /// The type of CompletionModel used by the client.
11    type CompletionModel: CompletionModel<Client = Self>;
12
13    /// Create a completion model with the given model.
14    ///
15    /// # Example with OpenAI
16    /// ```no_run
17    /// use rig_core::prelude::*;
18    /// use rig_core::providers::openai::{Client, self};
19    ///
20    /// # fn run() -> Result<(), Box<dyn std::error::Error>> {
21    /// // Initialize the OpenAI client
22    /// let openai = Client::new("your-open-ai-api-key")?;
23    ///
24    /// let gpt = openai.completion_model(openai::GPT_5_2);
25    /// # Ok(())
26    /// # }
27    /// ```
28    fn completion_model(&self, model: impl Into<String>) -> Self::CompletionModel {
29        Self::CompletionModel::make(self, model)
30    }
31
32    /// Create an agent builder with the given completion model.
33    ///
34    /// # Example with OpenAI
35    /// ```no_run
36    /// use rig_core::prelude::*;
37    /// use rig_core::providers::openai::{Client, self};
38    ///
39    /// # fn run() -> Result<(), Box<dyn std::error::Error>> {
40    /// // Initialize the OpenAI client
41    /// let openai = Client::new("your-open-ai-api-key")?;
42    ///
43    /// let agent = openai.agent(openai::GPT_5_2)
44    ///    .preamble("You are comedian AI with a mission to make people laugh.")
45    ///    .temperature(0.0)
46    ///    .build();
47    /// # Ok(())
48    /// # }
49    /// ```
50    fn agent(&self, model: impl Into<String>) -> AgentBuilder<Self::CompletionModel> {
51        AgentBuilder::new(self.completion_model(model))
52    }
53
54    /// Create an extractor builder with the given completion model.
55    fn extractor<T>(&self, model: impl Into<String>) -> ExtractorBuilder<Self::CompletionModel, T>
56    where
57        T: JsonSchema + for<'a> Deserialize<'a> + Serialize + Send + Sync,
58    {
59        ExtractorBuilder::new(self.completion_model(model))
60    }
61}