76 lines
2.6 KiB
Markdown
76 lines
2.6 KiB
Markdown
# 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:
|
|
|
|
```rust
|
|
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
|
|
|
|
1. **Create a new crate** in the `crates/` directory. For example, `owlen-myprovider`.
|
|
2. **Add dependencies** to your new crate's `Cargo.toml`. You will need `owlen-core`, `async-trait`, `tokio`, and any crates required for interacting with the new API (e.g., `reqwest`).
|
|
3. **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.
|
|
|
|
```rust
|
|
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.
|
|
|
|
1. **Add your provider crate** as a dependency to `owlen-cli`.
|
|
2. **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`.
|