-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Integrate Ollama backend support
- Added `ollama-rs` crate dependency for leveraging the Ollama AI service. - Introduced `async-stream` and `tokio-stream` dependencies to support async data streaming. - Updated `Cargo.toml` and `Cargo.lock` for new dependencies. docs: Update documentation for Ollama integration - Added instructions in `docs/getting_started.md` for using the Ollama backend. - Expanded `docs/installation.md` with details on additional setup steps needed for the Ollama service, including installation and firewall configuration. feat: Implement OllamaInterface for chat service - Created `OllamaInterface` in `src/provider/ollama/ollama_interface.rs` with methods for interacting with the Ollama backend. - Updated the chat service builder to handle the Ollama backend selection based on configuration. refactor: Cleanup Model structure in configuration - Removed unused `port` field in `Model` struct within `src/config/config_file.rs`. - Ensured optional URL handling for dynamic configuration of service endpoints. init: Include Ollama model configuration in default setup - Enhanced `src/cli/init/mod.rs` to add default configurations for Ollama models in the initialization step. This update allows for greater flexibility in choosing AI providers by supporting the Ollama service, enhancing potential use cases for Rusty Buddy.
- Loading branch information
Christian Stolz
committed
Oct 6, 2024
1 parent
d455727
commit 190f38a
Showing
10 changed files
with
168 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod ollama; | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod ollama_interface; |
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,78 @@ | ||
// src/provider/ollama/ollama_interface.rs | ||
|
||
use crate::chat::interface::{ChatBackend, Message, MessageRole}; | ||
use async_trait::async_trait; | ||
use ollama_rs::{ | ||
generation::chat::{request::ChatMessageRequest, ChatMessage, ChatMessageResponseStream}, | ||
IntoUrlSealed, Ollama, | ||
}; | ||
use std::error::Error; | ||
use tokio_stream::StreamExt; | ||
|
||
pub struct OllamaInterface { | ||
ollama: Ollama, | ||
model: String, | ||
} | ||
|
||
impl OllamaInterface { | ||
pub fn new(model: String, ourl: Option<String>) -> Self { | ||
let url = ourl.unwrap_or("http://localhost:11434".into()); | ||
OllamaInterface { | ||
ollama: Ollama::from_url(url.clone().into_url().unwrap()), | ||
model, | ||
} | ||
} | ||
|
||
fn convert_messages(messages: &[Message]) -> Vec<ChatMessage> { | ||
let mut chat_messages: Vec<ChatMessage> = Vec::new(); | ||
|
||
// Convert Message into ChatMessage for ollama | ||
for msg in messages { | ||
match msg.role { | ||
MessageRole::User => { | ||
chat_messages.push(ChatMessage::user(msg.content.clone())); | ||
} | ||
MessageRole::Assistant => { | ||
chat_messages.push(ChatMessage::assistant(msg.content.clone())); | ||
} | ||
MessageRole::Context => { | ||
chat_messages.push(ChatMessage::system(msg.content.clone())); | ||
} | ||
MessageRole::System => { | ||
chat_messages.push(ChatMessage::system(msg.content.clone())); | ||
} | ||
} | ||
} | ||
chat_messages | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl ChatBackend for OllamaInterface { | ||
async fn send_request( | ||
&mut self, | ||
messages: &[Message], | ||
_use_tools: bool, | ||
) -> Result<String, Box<dyn Error>> { | ||
let chat_messages = Self::convert_messages(messages); | ||
|
||
let request = ChatMessageRequest::new(self.model.clone(), chat_messages.clone()); | ||
|
||
let mut stream: ChatMessageResponseStream = | ||
self.ollama.send_chat_messages_stream(request).await?; | ||
|
||
let mut response = String::new(); | ||
|
||
while let Some(Ok(res)) = stream.next().await { | ||
if let Some(assistant_message) = res.message { | ||
response += &assistant_message.content; | ||
} | ||
} | ||
Ok(response) | ||
} | ||
|
||
fn print_statistics(&self) { | ||
// Implement statistics if required | ||
println!("Using Ollama model: {}", self.model); | ||
} | ||
} |