mirror of
https://github.com/deepset-ai/haystack.git
synced 2026-01-05 03:28:09 +00:00
105 lines
3.8 KiB
Plaintext
105 lines
3.8 KiB
Plaintext
---
|
|
title: "Get Started"
|
|
id: get-started
|
|
slug: "/get-started"
|
|
description: "Have a look at this page to learn how to quickly get up and running with Haystack. It contains instructions for installing, running your first RAG pipeline, adding data and further resources."
|
|
---
|
|
|
|
# Get Started
|
|
|
|
Have a look at this page to learn how to quickly get up and running with Haystack. It contains instructions for installing, running your first RAG pipeline, adding data and further resources.
|
|
|
|
## Build your first RAG application
|
|
|
|
Let's build your first Retrieval Augmented Generation (RAG) pipeline and see how Haystack answers questions.
|
|
|
|
First, install the minimal form of Haystack:
|
|
|
|
```shell
|
|
pip install haystack-ai
|
|
```
|
|
|
|
<details>
|
|
|
|
<summary>Were you already using Haystack 1.x?</summary>
|
|
|
|
:::warning
|
|
|
|
Installing `farm-haystack` and `haystack-ai` in the same Python environment (virtualenv, Colab, or system) causes problems.
|
|
|
|
Installing both packages in the same environment can somehow work or fail in obscure ways. We suggest installing only one of these packages per Python environment. Make sure that you remove both packages if they are installed in the same environment, followed by installing only one of them:
|
|
|
|
```bash
|
|
pip uninstall -y farm-haystack haystack-ai
|
|
pip install haystack-ai
|
|
```
|
|
|
|
If you have any questions, please reach out to us on the [GitHub Discussion](https://github.com/deepset-ai/haystack/discussions) or [Discord](https://discord.com/invite/VBpFzsgRVF).
|
|
:::
|
|
|
|
</details>
|
|
|
|
In the example below, we show how to set an API key using a Haystack [Secret](../concepts/secret-management.mdx). However, for easier use, you can also set an OpenAI key as an `OPENAI_API_KEY` environment variable.
|
|
|
|
```python
|
|
from haystack import Pipeline, Document
|
|
from haystack.utils import Secret
|
|
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
|
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
|
|
from haystack.components.generators.chat import OpenAIChatGenerator
|
|
from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder
|
|
from haystack.dataclasses import ChatMessage
|
|
|
|
## Write documents to InMemoryDocumentStore
|
|
document_store = InMemoryDocumentStore()
|
|
document_store.write_documents([
|
|
Document(content="My name is Jean and I live in Paris."),
|
|
Document(content="My name is Mark and I live in Berlin."),
|
|
Document(content="My name is Giorgio and I live in Rome.")
|
|
])
|
|
|
|
## Build a RAG pipeline
|
|
prompt_template = [
|
|
ChatMessage.from_system("You are a helpful assistant."),
|
|
ChatMessage.from_user(
|
|
"Given these documents, answer the question.\n"
|
|
"Documents:\n{% for doc in documents %}{{ doc.content }}{% endfor %}\n"
|
|
"Question: {{question}}\n"
|
|
"Answer:"
|
|
)
|
|
]
|
|
|
|
## Define required variables explicitly
|
|
prompt_builder = ChatPromptBuilder(template=prompt_template, required_variables={"question", "documents"})
|
|
|
|
retriever = InMemoryBM25Retriever(document_store=document_store)
|
|
llm = OpenAIChatGenerator(api_key=Secret.from_env_var("OPENAI_API_KEY"))
|
|
|
|
rag_pipeline = Pipeline()
|
|
rag_pipeline.add_component("retriever", retriever)
|
|
rag_pipeline.add_component("prompt_builder", prompt_builder)
|
|
rag_pipeline.add_component("llm", llm)
|
|
rag_pipeline.connect("retriever", "prompt_builder.documents")
|
|
rag_pipeline.connect("prompt_builder", "llm.messages")
|
|
|
|
## Ask a question
|
|
question = "Who lives in Paris?"
|
|
results = rag_pipeline.run(
|
|
{
|
|
"retriever": {"query": question},
|
|
"prompt_builder": {"question": question},
|
|
}
|
|
)
|
|
|
|
print(results["llm"]["replies"])
|
|
|
|
```
|
|
|
|
Are you curious about what each step does in this code example? Check out the recipe below for details:
|
|
|
|
RECIPE MISSING
|
|
|
|
### Adding Your Data
|
|
|
|
Instead of running the RAG pipeline on example data, learn how you can add your own custom data using [Document Stores](../concepts/document-store.mdx).
|