2.6 KiB
2.6 KiB
Provider Implementation Guide
This guide explains how to implement a new provider for Owlen. Providers are the components that connect to different LLM APIs.
The Provider Trait
The core of the provider system is the Provider trait, located in owlen-core. Any new provider must implement this trait.
Here is a simplified version of the trait:
use async_trait::async_trait;
use owlen_core::model::Model;
use owlen_core::session::Session;
#[async_trait]
pub trait Provider {
/// Returns the name of the provider.
fn name(&self) -> &str;
/// Sends the session to the provider and returns the response.
async fn chat(&self, session: &Session, model: &Model) -> Result<String, anyhow::Error>;
}
Creating a New Crate
- Create a new crate in the
crates/directory. For example,owlen-myprovider. - Add dependencies to your new crate's
Cargo.toml. You will needowlen-core,async-trait,tokio, and any crates required for interacting with the new API (e.g.,reqwest). - Add the new crate to the workspace in the root
Cargo.toml.
Implementing the Trait
In your new crate's lib.rs, you will define a struct for your provider and implement the Provider trait for it.
use async_trait::async_trait;
use owlen_core::model::Model;
use owlen_core::Provider;
use owlen_core::session::Session;
pub struct MyProvider;
#[async_trait]
impl Provider for MyProvider {
fn name(&self) -> &str {
"my-provider"
}
async fn chat(&self, session: &Session, model: &Model) -> Result<String, anyhow::Error> {
// 1. Get the conversation history from the session.
let history = session.get_messages();
// 2. Format the request for your provider's API.
// This might involve creating a JSON body with the messages.
// 3. Send the request to the API using a client like reqwest.
// 4. Parse the response from the API.
// 5. Return the content of the response as a String.
Ok("Hello from my provider!".to_string())
}
}
Integrating with Owlen
Once your provider is implemented, you will need to integrate it into the main Owlen application.
- Add your provider crate as a dependency to
owlen-cli. - In
owlen-cli, modify the provider registration to include your new provider. This will likely involve adding it to a list of available providers that the user can select from in the configuration.
This guide provides a basic outline. For more detailed examples, you can look at the existing provider implementations, such as owlen-ollama.