pub trait Provider: Send + Sync {
// Required methods
fn complete<'life0, 'async_trait>(
&'life0 self,
req: Request,
) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn stream<'life0, 'async_trait>(
&'life0 self,
req: Request,
) -> Pin<Box<dyn Future<Output = Result<BoxStream<'static, Result<Chunk>>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
}Expand description
Core trait for all LLM providers.
Implement this trait to add support for a new LLM provider. The trait is object-safe and supports both full completion and streaming responses.
§Example
use cognate_core::{Provider, Request, Message};
async fn example<P: Provider>(provider: &P) -> cognate_core::Result<()> {
let request = Request::new()
.with_model("gpt-4o")
.with_messages(vec![
Message::system("You are a helpful assistant"),
Message::user("Hello!"),
]);
let response = provider.complete(request).await?;
println!("{}", response.content());
Ok(())
}Required Methods§
Sourcefn complete<'life0, 'async_trait>(
&'life0 self,
req: Request,
) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn complete<'life0, 'async_trait>(
&'life0 self,
req: Request,
) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Send a completion request and wait for the full response.
Sourcefn stream<'life0, 'async_trait>(
&'life0 self,
req: Request,
) -> Pin<Box<dyn Future<Output = Result<BoxStream<'static, Result<Chunk>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn stream<'life0, 'async_trait>(
&'life0 self,
req: Request,
) -> Pin<Box<dyn Future<Output = Result<BoxStream<'static, Result<Chunk>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Send a completion request and return a streaming response.
Returns a stream of Chunks as they are generated by the provider.
§Example
use cognate_core::{Provider, Request, Message};
use futures::StreamExt;
async fn stream_example<P: Provider>(provider: &P) -> cognate_core::Result<()> {
let mut stream = provider
.stream(Request::new().with_model("gpt-4o").with_message(Message::user("Hi")))
.await?;
while let Some(chunk) = stream.next().await {
print!("{}", chunk?.content());
}
Ok(())
}