-
Notifications
You must be signed in to change notification settings - Fork 1
Examples: (Ollama and) Vector search without an account or an API key #134
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,95 @@ | ||||||
| """ | ||||||
| Use CrateDB Vector Search with embeddings computed by a local Ollama server. | ||||||
|
|
||||||
| Ollama runs the embedding model in its own process and speaks HTTP, so this | ||||||
| program needs no account, no API key, and no machine learning stack of its | ||||||
| own. `vector_ollama.py` and `vector_openai.py` are otherwise the same program. | ||||||
|
|
||||||
| - https://ollama.com/library/nomic-embed-text | ||||||
| - https://python.langchain.com/docs/integrations/text_embedding/ollama/ | ||||||
|
|
||||||
| As input data, the example uses the canonical `state_of_the_union.txt`. | ||||||
|
|
||||||
| Synopsis:: | ||||||
|
|
||||||
| # Install prerequisites. | ||||||
| pip install --upgrade langchain-cratedb langchain-ollama langchain-text-splitters | ||||||
|
|
||||||
| # Start database. | ||||||
| docker run --rm -it --publish=4200:4200 crate/crate:nightly | ||||||
|
|
||||||
| # Serve the embedding model. `nomic-embed-text` produces 768 dimensions, | ||||||
| # well within the 2048 a CrateDB FLOAT_VECTOR column accepts. | ||||||
| ollama serve | ||||||
| ollama pull nomic-embed-text | ||||||
|
|
||||||
| # Optionally set environment variables to configure the Ollama and CrateDB | ||||||
| # endpoints. | ||||||
| export OLLAMA_BASE_URL="http://localhost:11434" | ||||||
| export CRATEDB_SQLALCHEMY_URL="crate://crate@localhost/?schema=doc" | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep the schema separate from the one
Suggested change
|
||||||
|
|
||||||
| # Run program. | ||||||
| python examples/basic/vector_ollama.py | ||||||
| """ # noqa: E501 | ||||||
| # /// script | ||||||
| # requires-python = ">=3.10" | ||||||
| # dependencies = [ | ||||||
| # "langchain-cratedb", | ||||||
| # "langchain-ollama", | ||||||
| # "langchain-text-splitters", | ||||||
| # ] | ||||||
| # /// | ||||||
|
|
||||||
| import os | ||||||
| import typing as t | ||||||
|
|
||||||
| import requests | ||||||
| from langchain_core.documents import Document | ||||||
| from langchain_ollama import OllamaEmbeddings | ||||||
| from langchain_text_splitters import RecursiveCharacterTextSplitter | ||||||
|
|
||||||
| from langchain_cratedb import CrateDBVectorStore | ||||||
|
|
||||||
| CRATEDB_SQLALCHEMY_URL = os.environ.get( | ||||||
| "CRATEDB_SQLALCHEMY_URL", "crate://crate@localhost/?schema=testdrive" | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ) | ||||||
| OLLAMA_BASE_URL = os.environ.get("OLLAMA_BASE_URL", "http://localhost:11434") | ||||||
| EMBEDDING_MODEL = "nomic-embed-text" | ||||||
|
|
||||||
|
|
||||||
| def get_documents() -> t.List[Document]: | ||||||
| """ | ||||||
| Acquire data, return as LangChain documents. | ||||||
| """ | ||||||
|
|
||||||
| # Define text splitter. | ||||||
| text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0) | ||||||
|
|
||||||
| # Load a document, and split it into chunks. | ||||||
| url = "https://github.com/langchain-ai/langchain/raw/v0.0.325/docs/docs/modules/state_of_the_union.txt" | ||||||
| text = requests.get(url, timeout=10).text | ||||||
| return text_splitter.create_documents([text]) | ||||||
|
|
||||||
|
|
||||||
| def main() -> None: | ||||||
| # Set up the embedding model. | ||||||
| embeddings = OllamaEmbeddings(model=EMBEDDING_MODEL, base_url=OLLAMA_BASE_URL) | ||||||
|
|
||||||
| # Acquire documents. | ||||||
| documents = get_documents() | ||||||
|
|
||||||
| # Embed each chunk, and load them into the vector store. | ||||||
| vector_store = CrateDBVectorStore.from_documents( | ||||||
| documents=documents, | ||||||
| embedding=embeddings, | ||||||
| connection=CRATEDB_SQLALCHEMY_URL, | ||||||
| ) | ||||||
|
|
||||||
| # Invoke a query, and display the first result. | ||||||
| query = "What did the president say about Ketanji Brown Jackson" | ||||||
| docs = vector_store.similarity_search(query) | ||||||
| print(docs[0].page_content) | ||||||
|
|
||||||
|
|
||||||
| if __name__ == "__main__": | ||||||
| main() | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,8 @@ def test_file(run_file: t.Callable, file: Path) -> None: | |
| """ | ||
| Execute Python code, one test case per .py file. | ||
|
|
||
| Skip test cases that trip when no OpenAI API key is configured. | ||
| Skip test cases that need a service this machine does not provide: an | ||
| OpenAI API key, or a reachable Ollama server. | ||
| """ | ||
| if file.name in SKIP_FILES: | ||
| raise pytest.skip(f"FIXME: Skipping file: {file.name}") | ||
|
|
@@ -47,3 +48,7 @@ def test_file(run_file: t.Callable, file: Path) -> None: | |
| raise pytest.skip( | ||
| "Skipping test because `OPENAI_API_KEY` is not defined" | ||
| ) from ex | ||
| except ConnectionError as ex: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If model didn't pulled with |
||
| if "Failed to connect to Ollama" not in str(ex): | ||
| raise | ||
| raise pytest.skip("Skipping test because Ollama is not reachable") from ex | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ollama serve waits forever so pull does not work.