-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson_2_filtering_with_meta.py
52 lines (47 loc) · 2.53 KB
/
lesson_2_filtering_with_meta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import json
from datetime import datetime
from haystack import Document
from haystack import Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
# perform search on a specific group of documents in your document store
# We will do metadata filtering to make sure we are answering the question based only on information about Haystack 2.0.
try:
print("Creating haystack documents from training data")
documents = [
Document(
content="Use pip to install a basic version of Haystack's latest release: pip install farm-haystack. All the core Haystack components live in the haystack repo. But there's also the haystack-extras repo which contains components that are not as widely used, and you need to install them separately.",
meta={"version": 1.15, "date": datetime(2023, 3, 30)},
),
Document(
content="Use pip to install a basic version of Haystack's latest release: pip install farm-haystack[inference]. All the core Haystack components live in the haystack repo. But there's also the haystack-extras repo which contains components that are not as widely used, and you need to install them separately.",
meta={"version": 1.22, "date": datetime(2023, 11, 7)},
),
Document(
content="Use pip to install only the Haystack 2.0 code: pip install haystack-ai. The haystack-ai package is built on the main branch which is an unstable beta version, but it's useful if you want to try the new features as soon as they are merged.",
meta={"version": 2.0, "date": datetime(2023, 12, 4)},
),
]
document_store = InMemoryDocumentStore(bm25_algorithm="BM25Plus")
document_store.write_documents(documents=documents)
pipeline = Pipeline()
# There is a retriever, but no generator. This means that we will be able to retrieve relevant documents but not generate answers to prompted questions.
pipeline.add_component(
instance=InMemoryBM25Retriever(document_store=document_store), name="retriever"
)
query = "Haystack installation"
document = pipeline.run(
data={
"retriever": {
"query": query,
"filters": {"field": "meta.version", "operator": ">", "value": 1.21},
}
}
)
print(f"The answer is '{document['retriever']['documents'][0].content}'")
except Exception as e:
raise e
finally:
# Can we delete the dataset or something?
pass