apify-haystack 0.1.2__tar.gz → 0.1.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: apify_haystack
3
- Version: 0.1.2
3
+ Version: 0.1.3
4
4
  Summary: Apify-haystack integration
5
5
  Home-page: https://apify.com
6
6
  License: Apache-2.0
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
4
4
 
5
5
  [tool.poetry]
6
6
  name = "apify_haystack"
7
- version = "0.1.2"
7
+ version = "0.1.3"
8
8
  description = "Apify-haystack integration"
9
9
  authors = ["Apify Technologies s.r.o. <support@apify.com>"]
10
10
  homepage = "https://apify.com"
@@ -14,13 +14,14 @@ Haystack is an open-source framework fo...', meta: {'url': 'https://docs.haystac
14
14
  .....
15
15
  """
16
16
 
17
- from dotenv import load_dotenv
17
+ import os
18
+
18
19
  from haystack import Document
19
20
 
20
21
  from apify_haystack import ApifyDatasetFromActorCall
21
22
 
22
- # Set APIFY_API_TOKEN here or load it from .env file
23
- apify_api_token = "" or load_dotenv()
23
+ # Set API keys here
24
+ os.environ["APIFY_API_TOKEN"] = ""
24
25
 
25
26
  actor_id = "apify/website-content-crawler"
26
27
  run_input = {
@@ -37,7 +38,6 @@ actor = ApifyDatasetFromActorCall(
37
38
  actor_id=actor_id,
38
39
  run_input=run_input,
39
40
  dataset_mapping_function=dataset_mapping_function,
40
- apify_api_token=str(apify_api_token),
41
41
  )
42
42
  print(f"Calling the Apify actor {actor_id} ... crawling will take some time ...")
43
43
  print("You can monitor the progress at: https://console.apify.com/actors/runs")
@@ -2,7 +2,7 @@
2
2
  Crawl websites, scrape text content, and store it in the InMemoryDocumentStore.
3
3
 
4
4
  This script demonstrates how to extract content from a website using Apify's Website Content Crawler.
5
- The content is then cleaned, split into smaller chunks, embedded, and stored in the InMemoryDocumentStore.
5
+ The content is split into smaller chunks, embedded, and stored in the InMemoryDocumentStore.
6
6
 
7
7
  After the pipeline is executed, the documents are retrieved from the document store
8
8
  using BM25 retrieval and vector similarity.
@@ -16,80 +16,68 @@ meta: {'url': 'https://haystack.deepset.ai/overview/quick-start',
16
16
  'split_id': 0, 'split_idx_start': 0, '_split_overlap': ......
17
17
  .....
18
18
  """
19
-
20
19
  import os
21
20
 
22
- from dotenv import load_dotenv
23
21
  from haystack import Document, Pipeline
24
22
  from haystack.components.embedders import OpenAIDocumentEmbedder, OpenAITextEmbedder
25
- from haystack.components.preprocessors import DocumentCleaner, DocumentSplitter
23
+ from haystack.components.preprocessors import DocumentSplitter
24
+ from haystack.components.retrievers import InMemoryBM25Retriever, InMemoryEmbeddingRetriever
26
25
  from haystack.components.writers import DocumentWriter
27
26
  from haystack.document_stores.in_memory import InMemoryDocumentStore
28
- from haystack.utils.auth import Secret
29
27
 
30
28
  from apify_haystack import ApifyDatasetFromActorCall
31
29
 
32
- load_dotenv()
33
-
34
- # Set APIFY_API_TOKEN here or use it from .env file
35
- apify_api_token = "" or os.getenv("APIFY_API_TOKEN")
36
- openai_api_key = "" or os.getenv("OPENAI_API_KEY")
37
-
38
- actor_id = "apify/website-content-crawler"
39
- run_input = {
40
- "maxCrawlPages": 3, # limit the number of pages to crawl
41
- "startUrls": [{"url": "https://haystack.deepset.ai/"}],
42
- }
43
-
44
-
45
- def dataset_mapping_function(dataset_item: dict) -> Document:
46
- return Document(content=dataset_item.get("text"), meta={"url": dataset_item.get("url")})
47
-
30
+ os.environ["APIFY_API_TOKEN"] = "YOUR-APIFY-API-TOKEN"
31
+ os.environ["OPENAI_API_KEY"] = "YOUR-OPENAI-API-KEY"
48
32
 
49
33
  document_loader = ApifyDatasetFromActorCall(
50
- actor_id=actor_id,
51
- run_input=run_input,
52
- dataset_mapping_function=dataset_mapping_function,
53
- apify_api_token=apify_api_token,
34
+ actor_id="apify/website-content-crawler",
35
+ run_input={
36
+ "maxCrawlPages": 3, # limit the number of pages to crawl
37
+ "startUrls": [{"url": "https://haystack.deepset.ai/"}],
38
+ },
39
+ dataset_mapping_function=lambda item: Document(content=item["text"] or "", meta={"url": item["url"]}),
54
40
  )
55
41
 
56
42
  document_store = InMemoryDocumentStore()
57
43
  print(f"Initialized InMemoryDocumentStore with {document_store.count_documents()} documents")
58
44
 
59
- document_cleaner = DocumentCleaner()
60
45
  document_splitter = DocumentSplitter(split_by="word", split_length=150, split_overlap=50)
61
- document_embedder = OpenAIDocumentEmbedder(api_key=Secret.from_token(openai_api_key))
46
+ document_embedder = OpenAIDocumentEmbedder()
62
47
  document_writer = DocumentWriter(document_store)
63
48
 
64
49
  pipe = Pipeline()
65
50
  pipe.add_component("document_loader", document_loader)
66
- pipe.add_component("document_cleaner", document_cleaner)
67
51
  pipe.add_component("document_splitter", document_splitter)
68
52
  pipe.add_component("document_embedder", document_embedder)
69
53
  pipe.add_component("document_writer", document_writer)
70
54
 
71
- pipe.connect("document_loader", "document_cleaner")
72
- pipe.connect("document_cleaner", "document_splitter")
55
+ pipe.connect("document_loader", "document_splitter")
73
56
  pipe.connect("document_splitter", "document_embedder")
74
57
  pipe.connect("document_embedder", "document_writer")
75
58
 
76
- print(
77
- "Running pipeline the Apify document_loader -> document_cleaner -> document_splitter "
78
- "-> document_embedder -> document_writer"
79
- )
80
- print("Crawling will take some time ...")
81
- print("You can visit https://console.apify.com/actors/runs to monitor the progress")
82
-
83
- pipe.run({"document_loader": {}})
59
+ print("\nCrawling will take some time ...")
60
+ print("You can visit https://console.apify.com/actors/runs to monitor the progress\n")
84
61
 
62
+ pipe.run({})
85
63
  print(f"Added {document_store.count_documents()} to vector from Website Content Crawler")
86
64
 
87
- print("Retrieving documents from the document store using BM25")
88
- print("query='Haystack'")
89
- for doc in document_store.bm25_retrieval("Haystack", top_k=1):
90
- print(doc)
65
+ print("\n ### Retrieving documents from the document store using BM25 ###\n")
66
+ print("query='Haystack'\n")
67
+
68
+ bm25_retriever = InMemoryBM25Retriever(document_store)
69
+
70
+ for doc in bm25_retriever.run("Haystack", top_k=1)["documents"]:
71
+ print(doc.content)
72
+
73
+ print("\n ### Retrieving documents from the document store using vector similarity ###\n")
74
+ retrieval_pipe = Pipeline()
75
+ retrieval_pipe.add_component("embedder", OpenAITextEmbedder())
76
+ retrieval_pipe.add_component("retriever", InMemoryEmbeddingRetriever(document_store, top_k=1))
77
+
78
+ retrieval_pipe.connect("embedder.embedding", "retriever.query_embedding")
79
+
80
+ results = retrieval_pipe.run({"embedder": {"text": "What is Haystack?"}})
91
81
 
92
- print("Retrieving documents from the document store using vector similarity")
93
- print("query='What is Haystack'")
94
- for doc in document_store.embedding_retrieval(OpenAITextEmbedder().run("Haystack")["embedding"], top_k=1):
95
- print(doc)
82
+ for doc in results["retriever"]["documents"]:
83
+ print(doc.content)
@@ -20,21 +20,18 @@ Haystack is an open-source framework for building production-ready LLM applicati
20
20
 
21
21
  import os
22
22
 
23
- from dotenv import load_dotenv
24
23
  from haystack import Document, Pipeline
25
24
  from haystack.components.builders import PromptBuilder
26
25
  from haystack.components.embedders import OpenAIDocumentEmbedder, OpenAITextEmbedder
27
26
  from haystack.components.generators import OpenAIGenerator
28
27
  from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
29
28
  from haystack.document_stores.in_memory import InMemoryDocumentStore
30
- from haystack.utils.auth import Secret
31
29
 
32
30
  from apify_haystack import ApifyDatasetFromActorCall
33
31
 
34
- # Set APIFY_API_TOKEN here or use it from .env file
35
- load_dotenv()
36
- apify_api_token = "" or os.getenv("APIFY_API_TOKEN")
37
- openai_api_key = "" or os.getenv("OPENAI_API_KEY")
32
+ # Set API keys here
33
+ os.environ["APIFY_API_TOKEN"] = "YOUR-APIFY-API-TOKEN"
34
+ os.environ["OPENAI_API_KEY"] = "YOUR-OPENAI-API-KEY"
38
35
 
39
36
  actor_id = "apify/website-content-crawler"
40
37
  run_input = {
@@ -51,15 +48,14 @@ apify_dataset_loader = ApifyDatasetFromActorCall(
51
48
  actor_id=actor_id,
52
49
  run_input=run_input,
53
50
  dataset_mapping_function=dataset_mapping_function,
54
- apify_api_token=apify_api_token,
55
51
  )
56
52
 
57
53
  # Components
58
54
  print("Initializing components...")
59
55
  document_store = InMemoryDocumentStore()
60
56
 
61
- docs_embedder = OpenAIDocumentEmbedder(api_key=Secret.from_token(openai_api_key))
62
- text_embedder = OpenAITextEmbedder(api_key=Secret.from_token(openai_api_key))
57
+ docs_embedder = OpenAIDocumentEmbedder()
58
+ text_embedder = OpenAITextEmbedder()
63
59
  retriever = InMemoryEmbeddingRetriever(document_store)
64
60
  generator = OpenAIGenerator(model="gpt-3.5-turbo")
65
61
 
File without changes
File without changes