|
| 1 | +--- |
| 2 | +slug: /integrations/haystack |
| 3 | +title: 💙 Haystack |
| 4 | +--- |
| 5 | + |
| 6 | +[Haystack](https://github.com/deepset-ai/haystack) is an open-source LLM framework in Python. It provides [embedders](https://docs.haystack.deepset.ai/v2.0/docs/embedders), [generators](https://docs.haystack.deepset.ai/v2.0/docs/generators) and [rankers](https://docs.haystack.deepset.ai/v2.0/docs/rankers) via a number of LLM providers, tooling for [preprocessing](https://docs.haystack.deepset.ai/v2.0/docs/preprocessors) and data preparation, connectors to a number of vector databases including Chroma and more. Haystack allows you to build custom LLM applications using both components readily available in Haystack and [custom components](https://docs.haystack.deepset.ai/v2.0/docs/custom-components). Some of the most common applications you can build with Haystack are retrieval-augmented generation pipelines (RAG), question-answering and semantic search. |
| 7 | + |
| 8 | +<img src="https://img.shields.io/github/stars/deepset-ai/haystack.svg?style=social&label=Star&maxAge=2400"/> |
| 9 | + |
| 10 | +|[Docs](https://docs.haystack.deepset.ai/v2.0/docs) | [Github](https://github.com/deepset-ai/haystack) | [Haystack Integrations](https://haystack.deepset.ai/integrations) | [Tutorials](https://haystack.deepset.ai/tutorials) | |
| 11 | + |
| 12 | +You can use Chroma together with Haystack by installing the integration and using the `ChromaDocumentStore` |
| 13 | + |
| 14 | +### Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +pip install chroma-haystack |
| 18 | +``` |
| 19 | + |
| 20 | +### Usage |
| 21 | + |
| 22 | +- The [Chroma Integration page](https://haystack.deepset.ai/integrations/chroma-documentstore) |
| 23 | +- [Chroma + Haystack Example](https://colab.research.google.com/drive/1YpDetI8BRbObPDEVdfqUcwhEX9UUXP-m?usp=sharing) |
| 24 | + |
| 25 | +#### Write documents into a ChromaDocumentStore |
| 26 | + |
| 27 | +```python |
| 28 | +import os |
| 29 | +from pathlib import Path |
| 30 | + |
| 31 | +from haystack import Pipeline |
| 32 | +from haystack.components.converters import TextFileToDocument |
| 33 | +from haystack.components.writers import DocumentWriter |
| 34 | +from chroma_haystack import ChromaDocumentStore |
| 35 | + |
| 36 | +file_paths = ["data" / Path(name) for name in os.listdir("data")] |
| 37 | + |
| 38 | +document_store = ChromaDocumentStore() |
| 39 | + |
| 40 | +indexing = Pipeline() |
| 41 | +indexing.add_component("converter", TextFileToDocument()) |
| 42 | +indexing.add_component("writer", DocumentWriter(document_store)) |
| 43 | + |
| 44 | +indexing.connect("converter", "writer") |
| 45 | +indexing.run({"converter": {"sources": file_paths}}) |
| 46 | +``` |
| 47 | + |
| 48 | +#### Build RAG on top of Chroma |
| 49 | + |
| 50 | +```python |
| 51 | +from chroma_haystack.retriever import ChromaQueryRetriever |
| 52 | +from haystack.components.generators import HuggingFaceTGIGenerator |
| 53 | +from haystack.components.builders import PromptBuilder |
| 54 | + |
| 55 | +prompt = """ |
| 56 | +Answer the query based on the provided context. |
| 57 | +If the context does not contain the answer, say 'Answer not found'. |
| 58 | +Context: |
| 59 | +{% for doc in documents %} |
| 60 | + {{ doc.content }} |
| 61 | +{% endfor %} |
| 62 | +query: {{query}} |
| 63 | +Answer: |
| 64 | +""" |
| 65 | +prompt_builder = PromptBuilder(template=prompt) |
| 66 | + |
| 67 | +llm = HuggingFaceTGIGenerator(model="mistralai/Mixtral-8x7B-Instruct-v0.1", token='YOUR_HF_TOKEN') |
| 68 | +llm.warm_up() |
| 69 | +retriever = ChromaQueryRetriever(document_store) |
| 70 | + |
| 71 | +querying = Pipeline() |
| 72 | +querying.add_component("retriever", retriever) |
| 73 | +querying.add_component("prompt_builder", prompt_builder) |
| 74 | +querying.add_component("llm", llm) |
| 75 | + |
| 76 | +querying.connect("retriever.documents", "prompt_builder.documents") |
| 77 | +querying.connect("prompt_builder", "llm") |
| 78 | + |
| 79 | +results = querying.run({"retriever": {"queries": [query], "top_k": 3}, |
| 80 | + "prompt_builder": {"query": query}}) |
| 81 | +``` |
0 commit comments