mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
fix(python): accept null Ollama client options (#5278)
## What changed Treat explicit null for `ollama_client_kwargs` the same as omitted options. The field is optional, but the first embedding call tried to unpack `None` as keyword arguments and failed before making a request. Existing option mappings still pass through to the Ollama SDK. ## Verification - The new regression uses the real Ollama 0.6.2 SDK and a loopback HTTP server. Main's Python provider raises `TypeError` before any request. The patched call sends one `POST /api/embed` and returns the expected vector. - All 16 Ollama tests passed with the 0.42.0b38 native wheel and current Python sources. An independent rerun of the exact Python provider also passed all 16 tests. - Ruff formatting, Ruff lint, and the Git diff check passed. Closes #5277. Co-authored-by: Divyam Talwar <DivyamTalwar@users.noreply.github.com>
This commit is contained in:
co-authored by
Divyam Talwar
parent
760a6ac2a8
commit
e2f6ba1b69
@@ -104,4 +104,4 @@ class OllamaEmbeddings(TextEmbeddingFunction):
|
||||
def _ollama_client(self) -> "ollama.Client":
|
||||
ollama = attempt_import_or_raise("ollama")
|
||||
# ToDo explore ollama.AsyncClient
|
||||
return ollama.Client(host=self.host, **self.ollama_client_kwargs)
|
||||
return ollama.Client(host=self.host, **(self.ollama_client_kwargs or {}))
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
import json
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from threading import Thread
|
||||
|
||||
import numpy as np
|
||||
import pyarrow as pa
|
||||
@@ -144,6 +146,46 @@ def test_ollama_query_embeddings(ollama_embeddings):
|
||||
assert requests[0]["input"] == ["hello"]
|
||||
|
||||
|
||||
def test_ollama_accepts_null_client_kwargs(monkeypatch):
|
||||
pytest.importorskip("ollama")
|
||||
monkeypatch.setenv("NO_PROXY", "127.0.0.1")
|
||||
monkeypatch.setenv("no_proxy", "127.0.0.1")
|
||||
requests = []
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
def do_POST(self):
|
||||
body = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
|
||||
requests.append((self.path, body))
|
||||
response = json.dumps({"embeddings": [[0.0, 5.0]]}).encode()
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(response)))
|
||||
self.end_headers()
|
||||
self.wfile.write(response)
|
||||
|
||||
with ThreadingHTTPServer(("127.0.0.1", 0), Handler) as server:
|
||||
thread = Thread(target=server.serve_forever)
|
||||
thread.start()
|
||||
try:
|
||||
function = (
|
||||
get_registry()
|
||||
.get("ollama")
|
||||
.create(
|
||||
host=f"http://127.0.0.1:{server.server_port}",
|
||||
ollama_client_kwargs=None,
|
||||
)
|
||||
)
|
||||
assert function.compute_query_embeddings("hello") == [[0.0, 5.0]]
|
||||
function._ollama_client._client.close()
|
||||
finally:
|
||||
server.shutdown()
|
||||
thread.join()
|
||||
|
||||
assert requests == [
|
||||
("/api/embed", {"model": "nomic-embed-text", "input": ["hello"]})
|
||||
]
|
||||
|
||||
|
||||
def test_ollama_source_embeddings_preserves_api_error(ollama_embeddings):
|
||||
ollama = pytest.importorskip("ollama")
|
||||
function, requests = ollama_embeddings
|
||||
|
||||
Reference in New Issue
Block a user