Skip to content

Commit

Permalink
feat: add embeddings support
Browse files Browse the repository at this point in the history
  • Loading branch information
covercash2 committed Sep 7, 2024
1 parent 34eee72 commit a15b42b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
27 changes: 25 additions & 2 deletions ollama/ollama-cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ollama/ollama-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
anyhow = "1.0.87"
clap = { version = "4.5.16", features = ["derive", "string"] }
futures = "0.3.30"
ollama-rs = { version = "0.2.0", features = ["stream"] }
ollama-rs = { version = "0.2.1", features = ["stream"] }
tokio = { version = "1.40.0", features = ["full"] }
tokio-stream = { version = "0.1.16", features = ["full"] }
tracing = "0.1.40"
Expand Down
5 changes: 5 additions & 0 deletions ollama/ollama-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Cli {
#[derive(Subcommand)]
enum Command {
Generate(ollama::generate::Request),
Embed(ollama::generate::Request),
}

#[tokio::main]
Expand All @@ -26,6 +27,10 @@ async fn main() -> anyhow::Result<()> {
Command::Generate(request) => {
client.generate(request).await?;
}
Command::Embed(request) => {
let embedding = client.embed(request).await?;
println!("{embedding:?}");
}
}

Ok(())
Expand Down
19 changes: 19 additions & 0 deletions ollama/ollama-cli/src/ollama/embeddings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use ollama_rs::generation::embeddings::request::GenerateEmbeddingsRequest;

use super::{generate::Request, Client};

impl From<Request> for GenerateEmbeddingsRequest {
fn from(value: Request) -> Self {
GenerateEmbeddingsRequest::new(value.model.to_string(), value.prompt.into())
}
}

impl Client {
pub async fn embed(&self, request: Request) -> anyhow::Result<Vec<Vec<f32>>> {
let request: GenerateEmbeddingsRequest = request.into();

let response = self.client.generate_embeddings(request).await?;

Ok(response.embeddings)
}
}
1 change: 1 addition & 0 deletions ollama/ollama-cli/src/ollama/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::anyhow;
use ollama_rs::Ollama;
use url::Url;

pub mod embeddings;
pub mod generate;

pub const DEFAULT_MODEL: &str = "mistral-nemo";
Expand Down

0 comments on commit a15b42b

Please sign in to comment.