Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(swiftide): documented file swiftide/src/integrations/openai/mod.rs #21

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion swiftide/src/integrations/openai/mod.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,61 @@
use std::sync::Arc;
//! This module provides integration with OpenAI's API, enabling the use of language models and embeddings within the Swiftide project.
//! It includes the `OpenAI` struct for managing API clients and default options for embedding and prompt models.
//! The module is conditionally compiled based on the "openai" feature flag.

use derive_builder::Builder;
use std::sync::Arc;

mod embed;
mod simple_prompt;

/// The `OpenAI` struct encapsulates an OpenAI client and default options for embedding and prompt models.
/// It uses the `Builder` pattern for flexible and customizable instantiation.
#[derive(Debug, Builder, Clone)]
pub struct OpenAI {
/// The OpenAI client, wrapped in an `Arc` for thread-safe reference counting.
/// Defaults to a new instance of `async_openai::Client`.
#[builder(default = "Arc::new(async_openai::Client::new())", setter(custom))]
client: Arc<async_openai::Client<async_openai::config::OpenAIConfig>>,
/// Default options for embedding and prompt models.
#[builder(default)]
default_options: Options,
}

/// The `Options` struct holds configuration options for the `OpenAI` client.
/// It includes optional fields for specifying the embedding and prompt models.
#[derive(Debug, Default, Clone, Builder)]
#[builder(setter(into, strip_option))]
pub struct Options {
/// The default embedding model to use, if specified.
#[builder(default)]
pub embed_model: Option<String>,
/// The default prompt model to use, if specified.
#[builder(default)]
pub prompt_model: Option<String>,
}

impl Options {
/// Creates a new `OptionsBuilder` for constructing `Options` instances.
pub fn builder() -> OptionsBuilder {
OptionsBuilder::default()
}
}

impl OpenAI {
/// Creates a new `OpenAIBuilder` for constructing `OpenAI` instances.
pub fn builder() -> OpenAIBuilder {
OpenAIBuilder::default()
}
}

impl OpenAIBuilder {
/// Sets the OpenAI client for the `OpenAI` instance.
///
/// # Parameters
/// - `client`: The OpenAI client to set.
///
/// # Returns
/// A mutable reference to the `OpenAIBuilder`.
pub fn client(
&mut self,
client: async_openai::Client<async_openai::config::OpenAIConfig>,
Expand All @@ -43,6 +64,13 @@ impl OpenAIBuilder {
self
}

/// Sets the default embedding model for the `OpenAI` instance.
///
/// # Parameters
/// - `model`: The embedding model to set.
///
/// # Returns
/// A mutable reference to the `OpenAIBuilder`.
pub fn default_embed_model(&mut self, model: impl Into<String>) -> &mut Self {
if let Some(options) = self.default_options.as_mut() {
options.embed_model = Some(model.into());
Expand All @@ -55,6 +83,13 @@ impl OpenAIBuilder {
self
}

/// Sets the default prompt model for the `OpenAI` instance.
///
/// # Parameters
/// - `model`: The prompt model to set.
///
/// # Returns
/// A mutable reference to the `OpenAIBuilder`.
pub fn default_prompt_model(&mut self, model: impl Into<String>) -> &mut Self {
if let Some(options) = self.default_options.as_mut() {
options.prompt_model = Some(model.into());
Expand Down
Loading