Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for bulk embedding in Ollama #735

Merged
merged 8 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Unreleased]
- Improve the Langchain::Tool::Database tool
- Allow explictly setting tool_choice on the Assistant instance
- Add support for bulk embedding in Ollama

## [0.15.3] - 2024-08-27
- Fix OpenAI#embed when text-embedding-ada-002 is used
Expand Down
6 changes: 3 additions & 3 deletions lib/langchain/llm/ollama.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ def embed(
top_p: nil
)
parameters = {
prompt: text,
model: model
model: model,
input: Array(text)
}.compact

llm_parameters = {
Expand All @@ -243,7 +243,7 @@ def embed(

parameters[:options] = llm_parameters.compact

response = client.post("api/embeddings") do |req|
response = client.post("api/embed") do |req|
req.body = parameters
end

Expand Down
2 changes: 1 addition & 1 deletion lib/langchain/llm/response/ollama_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def embedding
end

def embeddings
[raw_response&.dig("embedding")]
raw_response&.dig("embeddings") || []
end

def role
Expand Down
22 changes: 21 additions & 1 deletion spec/langchain/llm/ollama_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

describe "#embed" do
let(:response_body) {
{"embedding" => [0.1, 0.2, 0.3]}
{"embeddings" => [[0.1, 0.2, 0.3]]}
}

before do
Expand All @@ -36,6 +36,26 @@
expect(subject.embed(text: "Hello, world!")).to be_a(Langchain::LLM::OllamaResponse)
expect(subject.embed(text: "Hello, world!").embedding.count).to eq(3)
end

it "sends an input array to the client" do
subject.embed(text: "Hello, world!")

expect(client).to have_received(:post) do |path, &block|
req = double("request").as_null_object
block.call(req)
expect(req).to have_received(:body=).with(hash_including(input: ["Hello, world!"]))
end
end

context "when the JSON response contains no embeddings" do
let(:response_body) {
{"embeddings" => []}
}

it "#embedding returns nil" do
expect(subject.embed(text: "Hello, world!").embedding).to be nil
end
end
end

describe "#complete" do
Expand Down