-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c05f5f
commit fd9cc57
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
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,34 @@ | ||
# In order to run this example, you will need to have an instance of Ollama running with the | ||
# nomic-embed-text model downloaded. Use the following commands to serve an nomic-embed-text model from Ollama | ||
# | ||
# docker run -d -p 11434:11434 --name ollama ollama/ollama:latest | ||
# docker exec ollama ollama pull nomic-embed-text | ||
|
||
from haystack import Document, Pipeline | ||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever | ||
from haystack.document_stores.in_memory import InMemoryDocumentStore | ||
from haystack_integrations.components.embedders.ollama.document_embedder import OllamaDocumentEmbedder | ||
from haystack_integrations.components.embedders.ollama.text_embedder import OllamaTextEmbedder | ||
|
||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine") | ||
|
||
documents = [ | ||
Document(content="I saw a black horse running"), | ||
Document(content="Germany has many big cities"), | ||
Document(content="My name is Wolfgang and I live in Berlin"), | ||
] | ||
|
||
document_embedder = OllamaDocumentEmbedder() | ||
documents_with_embeddings = document_embedder.run(documents)["documents"] | ||
document_store.write_documents(documents_with_embeddings) | ||
|
||
query_pipeline = Pipeline() | ||
query_pipeline.add_component("text_embedder", OllamaTextEmbedder()) | ||
query_pipeline.add_component("retriever", InMemoryEmbeddingRetriever(document_store=document_store)) | ||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding") | ||
|
||
query = "Who lives in Berlin?" | ||
|
||
result = query_pipeline.run({"text_embedder": {"text": query}}) | ||
|
||
print(result["retriever"]["documents"][0]) |