|
16 | 16 | }, |
17 | 17 | { |
18 | 18 | "cell_type": "code", |
19 | | - "execution_count": null, |
| 19 | + "execution_count": 25, |
20 | 20 | "metadata": {}, |
21 | | - "outputs": [], |
| 21 | + "outputs": [ |
| 22 | + { |
| 23 | + "data": { |
| 24 | + "text/plain": [ |
| 25 | + "<haystack.core.pipeline.pipeline.Pipeline object at 0x34dbfbe60>\n", |
| 26 | + "🚅 Components\n", |
| 27 | + " - link_fetcher: LinkContentFetcher\n", |
| 28 | + " - converter: HTMLToDocument\n", |
| 29 | + " - splitter: DocumentSplitter\n", |
| 30 | + " - embedder: SentenceTransformersDocumentEmbedder\n", |
| 31 | + " - writer: DocumentWriter\n", |
| 32 | + "🛤️ Connections\n", |
| 33 | + " - link_fetcher.streams -> converter.sources (List[ByteStream])\n", |
| 34 | + " - converter.documents -> splitter.documents (List[Document])\n", |
| 35 | + " - splitter.documents -> embedder.documents (List[Document])\n", |
| 36 | + " - embedder.documents -> writer.documents (List[Document])" |
| 37 | + ] |
| 38 | + }, |
| 39 | + "execution_count": 25, |
| 40 | + "metadata": {}, |
| 41 | + "output_type": "execute_result" |
| 42 | + } |
| 43 | + ], |
22 | 44 | "source": [ |
23 | 45 | "from haystack import Pipeline\n", |
24 | 46 | "from haystack.document_stores.in_memory import InMemoryDocumentStore\n", |
|
55 | 77 | }, |
56 | 78 | { |
57 | 79 | "cell_type": "code", |
58 | | - "execution_count": 21, |
| 80 | + "execution_count": 26, |
59 | 81 | "metadata": {}, |
60 | 82 | "outputs": [], |
61 | 83 | "source": [ |
|
78 | 100 | }, |
79 | 101 | { |
80 | 102 | "cell_type": "code", |
81 | | - "execution_count": null, |
| 103 | + "execution_count": 27, |
82 | 104 | "metadata": {}, |
83 | | - "outputs": [], |
| 105 | + "outputs": [ |
| 106 | + { |
| 107 | + "name": "stderr", |
| 108 | + "output_type": "stream", |
| 109 | + "text": [ |
| 110 | + "Batches: 100%|██████████| 1/1 [00:01<00:00, 1.11s/it]\n" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "data": { |
| 115 | + "text/plain": [ |
| 116 | + "{'writer': {'documents_written': 20}}" |
| 117 | + ] |
| 118 | + }, |
| 119 | + "execution_count": 27, |
| 120 | + "metadata": {}, |
| 121 | + "output_type": "execute_result" |
| 122 | + } |
| 123 | + ], |
84 | 124 | "source": [ |
85 | 125 | "indexing_pipeline.run(data={\"link_fetcher\":{\"urls\": [\"https://haystack.deepset.ai/integrations/elasticsearch-document-store\",\n", |
86 | 126 | " \"https://haystack.deepset.ai/tutorials/27_first_rag_pipeline/\",\n", |
|
103 | 143 | }, |
104 | 144 | { |
105 | 145 | "cell_type": "code", |
106 | | - "execution_count": 3, |
| 146 | + "execution_count": 28, |
107 | 147 | "metadata": {}, |
108 | 148 | "outputs": [], |
109 | 149 | "source": [ |
|
124 | 164 | }, |
125 | 165 | { |
126 | 166 | "cell_type": "code", |
127 | | - "execution_count": 10, |
| 167 | + "execution_count": 29, |
128 | 168 | "metadata": {}, |
129 | 169 | "outputs": [], |
130 | 170 | "source": [ |
|
137 | 177 | "\n", |
138 | 178 | "######## Complete this section #############\n", |
139 | 179 | "prompt_template = \"\"\"\n", |
140 | | - "You are an expert Python software engineer, you are asked to write code, \n", |
| 180 | + "You are an expert Python software engineer, you are asked to write Haystack 2.0 pipelines for indexing and querying documents., \n", |
141 | 181 | "explain code and you use the context provided to generate accurate and functional code along with clear explanations.\n", |
142 | 182 | "After you define a class, you also provide examples of using the class and its methods.\n", |
143 | 183 | "You must only use information from the given documents and cite the documents you used by mentioning their URL in the answer.\n", |
|
159 | 199 | }, |
160 | 200 | { |
161 | 201 | "cell_type": "code", |
162 | | - "execution_count": null, |
| 202 | + "execution_count": 30, |
163 | 203 | "metadata": {}, |
164 | | - "outputs": [], |
| 204 | + "outputs": [ |
| 205 | + { |
| 206 | + "data": { |
| 207 | + "text/plain": [ |
| 208 | + "<haystack.core.pipeline.pipeline.Pipeline object at 0x350d3a6c0>\n", |
| 209 | + "🚅 Components\n", |
| 210 | + " - query_embedder: SentenceTransformersTextEmbedder\n", |
| 211 | + " - retriever: InMemoryEmbeddingRetriever\n", |
| 212 | + " - prompt_builder: PromptBuilder\n", |
| 213 | + " - llm: OpenAIGenerator\n", |
| 214 | + "🛤️ Connections\n", |
| 215 | + " - query_embedder.embedding -> retriever.query_embedding (List[float])\n", |
| 216 | + " - retriever.documents -> prompt_builder.documents (List[Document])\n", |
| 217 | + " - prompt_builder.prompt -> llm.prompt (str)" |
| 218 | + ] |
| 219 | + }, |
| 220 | + "execution_count": 30, |
| 221 | + "metadata": {}, |
| 222 | + "output_type": "execute_result" |
| 223 | + } |
| 224 | + ], |
165 | 225 | "source": [ |
166 | 226 | "pipeline = Pipeline()\n", |
167 | 227 | "pipeline.add_component(instance=query_embedder, name=\"query_embedder\")\n", |
|
192 | 252 | }, |
193 | 253 | { |
194 | 254 | "cell_type": "code", |
195 | | - "execution_count": null, |
| 255 | + "execution_count": 31, |
196 | 256 | "metadata": {}, |
197 | | - "outputs": [], |
| 257 | + "outputs": [ |
| 258 | + { |
| 259 | + "name": "stderr", |
| 260 | + "output_type": "stream", |
| 261 | + "text": [ |
| 262 | + "Batches: 100%|██████████| 1/1 [00:00<00:00, 17.85it/s]\n" |
| 263 | + ] |
| 264 | + }, |
| 265 | + { |
| 266 | + "name": "stdout", |
| 267 | + "output_type": "stream", |
| 268 | + "text": [ |
| 269 | + "https://haystack.deepset.ai/integrations/elasticsearch-document-store and https://haystack.deepset.ai/tutorials/27_first_rag_pipeline/ , to start the writing of pipeline, we need to first import the necessary modules from Haystack and initialize the ElasticSearch Document store. \n", |
| 270 | + "\n", |
| 271 | + "```python\n", |
| 272 | + "from haystack.components.embedders import SentenceTransformersTextEmbedder\n", |
| 273 | + "from haystack.components.builders import PromptBuilder\n", |
| 274 | + "from haystack.components.generators import RAGenerator\n", |
| 275 | + "from haystack.components.evaluators.rag import RAGAsEvaluater\n", |
| 276 | + "from haystack import Pipeline\n", |
| 277 | + "from haystack.document_stores.elasticsearch import ElasticsearchDocumentStore\n", |
| 278 | + "\n", |
| 279 | + "document_store = ElasticsearchDocumentStore(hosts=\"http://localhost:9200\")\n", |
| 280 | + "```\n", |
| 281 | + "\n", |
| 282 | + "Next, initialize the TextEmbedder that will create embeddings for the user query.\n", |
| 283 | + "\n", |
| 284 | + "```python\n", |
| 285 | + "text_embedder = SentenceTransformersTextEmbedder(model=\"sentence-transformers/all-MiniLM-L6-v2\")\n", |
| 286 | + "```\n", |
| 287 | + "\n", |
| 288 | + "Define a template prompt that will guide the generation of answers. \n", |
| 289 | + "\n", |
| 290 | + "```python\n", |
| 291 | + "template = \"\"\"\n", |
| 292 | + "Given the following information, answer the question.\n", |
| 293 | + "Context:\n", |
| 294 | + "{% for document in documents %}\n", |
| 295 | + "{{ document.content }}\n", |
| 296 | + "{% endfor %}\n", |
| 297 | + "Question: {{question}}\n", |
| 298 | + "Answer:\n", |
| 299 | + "\"\"\"\n", |
| 300 | + "prompt_builder = PromptBuilder(template=template)\n", |
| 301 | + "```\n", |
| 302 | + " \n", |
| 303 | + "Then, initialize the Generators and Evaluators. We are using RAGenerator for generating answers with RAG approach and RAGAsEvaluater for evaluating generated answers.\n", |
| 304 | + "\n", |
| 305 | + "```python\n", |
| 306 | + "generator = RAGenerator(model=\"rag-token-nq\")\n", |
| 307 | + "evaluator = RAGAsEvaluater()\n", |
| 308 | + "```\n", |
| 309 | + "Now, build the pipeline by adding the components and connecting them. \n", |
| 310 | + "\n", |
| 311 | + "```python\n", |
| 312 | + "pipeline = Pipeline()\n", |
| 313 | + "pipeline.add_component(\"text_embedder\", text_embedder)\n", |
| 314 | + "pipeline.add_component(\"prompt_builder\", prompt_builder)\n", |
| 315 | + "pipeline.add_component(\"generator\", generator)\n", |
| 316 | + "pipeline.add_component(\"evaluator\", evaluator)\n", |
| 317 | + "\n", |
| 318 | + "pipeline.connect(\"text_embedder.embedding\", \"retriever.query_embedding\")\n", |
| 319 | + "pipeline.connect(\"retriever\", \"prompt_builder.documents\")\n", |
| 320 | + "pipeline.connect(\"prompt_builder\", \"generator\")\n", |
| 321 | + "pipeline.connect(\"generator\", \"evaluator\")\n", |
| 322 | + "```\n", |
| 323 | + "\n", |
| 324 | + "Finally, run the pipeline with a question to generate and evaluate an answer.\n", |
| 325 | + "\n", |
| 326 | + "```python\n", |
| 327 | + "question = \"What does Rhodes Statue look like?\"\n", |
| 328 | + "response = pipeline.run({\"text_embedder\": {\"text\": question}, \"prompt_builder\": {\"question\": question}})\n", |
| 329 | + "print(response[\"evaluator\"][\"evaluation\"])\n", |
| 330 | + "```\n", |
| 331 | + "Please note that the code is hypothetical given the documents provided and may need modifications as per actual setup and requirement. The model name used for SentenceTransformersTextEmbedder and RAGenerator may need to change based on the desired performance and characteristics. The connection of the components in the pipeline may vary based on the actual need.\n" |
| 332 | + ] |
| 333 | + } |
| 334 | + ], |
198 | 335 | "source": [ |
199 | 336 | "question = \"Write a Haystack 2.0 pipeline that connects to Elastic Search and answers questions about its knowledge, the pipeline\\\n", |
200 | 337 | " should include a prompt template with instructions and the template should iterate over all documents in the \\\n", |
201 | | - " context, the pipeline should also include a feedback loop and guardrails to ensure that the answers are accurate.\"\n", |
| 338 | + " context, the pipeline should also incorporate evaluation of the generated answers through RAGAS.\"\n", |
202 | 339 | "result = pipeline.run(data={\"query_embedder\": {\"text\": question}, \"prompt_builder\": {\"query\": question}})\n", |
203 | 340 | "print(result['llm']['replies'][0])" |
204 | 341 | ] |
|
0 commit comments