-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from douglasmakey/feature/use-rust-genai
feat: add rust-genai.
- Loading branch information
Showing
9 changed files
with
85 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use crate::processor::CompletionGenerator; | ||
use crate::{Error, Result}; | ||
use async_stream::stream; | ||
use futures::{stream::LocalBoxStream, StreamExt}; | ||
use genai::{ | ||
chat::{ChatMessage, ChatRequest, ChatStreamEvent, StreamChunk}, | ||
client::Client, | ||
}; | ||
|
||
pub struct GenAI { | ||
client: Client, | ||
} | ||
|
||
impl GenAI { | ||
pub fn new() -> Self { | ||
Self { | ||
client: Client::default(), | ||
} | ||
} | ||
} | ||
|
||
impl CompletionGenerator for GenAI { | ||
async fn generate_completion( | ||
&self, | ||
model: &str, | ||
_temperature: f32, | ||
prompt: &str, | ||
input: &str, | ||
) -> crate::Result<String> { | ||
let req = ChatRequest::new(vec![ChatMessage::system(prompt), ChatMessage::user(input)]); | ||
let resp = self.client.exec_chat(model, req.clone(), None).await?; | ||
resp.content.ok_or(Error::EmptyResponse) | ||
} | ||
|
||
async fn stream_completion( | ||
&self, | ||
model: &str, | ||
_temperature: f32, | ||
prompt: &str, | ||
input: &str, | ||
) -> Result<LocalBoxStream<String>> { | ||
let req = ChatRequest::new(vec![ChatMessage::system(prompt), ChatMessage::user(input)]); | ||
let resp = self | ||
.client | ||
.exec_chat_stream(model, req.clone(), None) | ||
.await?; | ||
|
||
let async_stream = stream! { | ||
let mut stream = resp.stream; | ||
while let Some(Ok(stream_event)) = stream.next().await { | ||
if let ChatStreamEvent::Chunk(StreamChunk { content }) = stream_event { | ||
yield content; | ||
} | ||
}; | ||
}; | ||
|
||
Ok(Box::pin(async_stream)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod genai; | ||
pub mod openai; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters