Spaces:
Runtime error
Runtime error
lgfunderburk
commited on
Commit
•
687e5f0
1
Parent(s):
4b2a9ae
add header
Browse files
README.md
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: {{Haystack RAG chainlit app}}
|
3 |
+
emoji: {{emoji}}
|
4 |
+
colorFrom: {{colorFrom}}
|
5 |
+
colorTo: {{colorTo}}
|
6 |
+
sdk: {{sdk}}
|
7 |
+
sdk_version: {{sdkVersion}}
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
---
|
11 |
+
|
12 |
+
# Welcome!
|
13 |
+
|
14 |
+
This chatbot uses RAG to answer questions about the Seven Wonders of the Ancient World.
|
15 |
+
|
16 |
+
Here are sample questions you can ask it:
|
17 |
+
|
18 |
+
1. What is the Great Pyramid of Giza?
|
19 |
+
2. What is the Hanging Gardens of Babylon?
|
20 |
+
3. What is the Temple of Artemis at Ephesus?
|
21 |
+
4. What is the Statue of Zeus at Olympia?
|
22 |
+
5. What is the Mausoleum at Halicarnassus?
|
23 |
+
6. Where is Gardens of Babylon?
|
24 |
+
7. Why did people build Great Pyramid of Giza?
|
25 |
+
8. What does Rhodes Statue look like?
|
26 |
+
9. Why did people visit the Temple of Artemis?
|
27 |
+
10. What is the importance of Colossus of Rhodes?
|
28 |
+
11. What happened to the Tomb of Mausolus?
|
29 |
+
12. How did Colossus of Rhodes collapse?
|
30 |
+
|
31 |
+
## How is it built?
|
32 |
+
|
33 |
+
### Poetry package management
|
34 |
+
|
35 |
+
This project uses [Poetry](https://python-poetry.org/) for package management.
|
36 |
+
|
37 |
+
It uses [this `pyproject.toml` file](pyproject.toml)
|
38 |
+
|
39 |
+
To install dependencies:
|
40 |
+
|
41 |
+
```bash
|
42 |
+
pip install poetry
|
43 |
+
poetry install
|
44 |
+
```
|
45 |
+
|
46 |
+
### Data source:
|
47 |
+
|
48 |
+
The data is from the [Seven Wonders dataset][1] on Hugging Face. https://huggingface.co/datasets/bilgeyucel/seven-wonders
|
49 |
+
|
50 |
+
### Method
|
51 |
+
|
52 |
+
The chatbots retrieval mechanism is developed using Retrieval Augmented Generation (RAG) with [Haystack](https://haystack.deepset.ai/tutorials/22_pipeline_with_promptnode) and its user interface is built with [Chainlit](https://docs.chainlit.io/overview). It is using OpenAI GPT-3.5-turbo.
|
53 |
+
|
54 |
+
### Pipeline steps (Haystack) - check the full script here: [src/app.py](src/app.py)
|
55 |
+
|
56 |
+
1. Initialize in-memory Document store
|
57 |
+
|
58 |
+
```python
|
59 |
+
# Initialize Haystack's QA system
|
60 |
+
document_store = InMemoryDocumentStore(use_bm25=True)
|
61 |
+
```
|
62 |
+
2. Load dataset from HF
|
63 |
+
|
64 |
+
```python
|
65 |
+
dataset = load_dataset("bilgeyucel/seven-wonders", split="train")
|
66 |
+
```
|
67 |
+
|
68 |
+
3. Transform documents and load into document store
|
69 |
+
|
70 |
+
```python
|
71 |
+
document_store.write_documents(dataset)
|
72 |
+
```
|
73 |
+
4. Initialize a RAG prompt
|
74 |
+
|
75 |
+
```
|
76 |
+
rag_prompt = PromptTemplate(
|
77 |
+
prompt="""Synthesize a brief answer from the following text for the given question.
|
78 |
+
Provide a clear and concise response that summarizes the key points and information presented in the text.
|
79 |
+
Your answer should be in your own words and be no longer than 50 words.
|
80 |
+
\n\n Related text: {join(documents)} \n\n Question: {query} \n\n Answer:""",
|
81 |
+
output_parser=AnswerParser(),
|
82 |
+
)
|
83 |
+
|
84 |
+
```
|
85 |
+
|
86 |
+
5. Set the nodes using GPT-3.5-turbo
|
87 |
+
|
88 |
+
```python
|
89 |
+
Set up nodes
|
90 |
+
retriever = BM25Retriever(document_store=document_store, top_k=2)
|
91 |
+
pn = PromptNode("gpt-3.5-turbo",
|
92 |
+
api_key=MY_API_KEY,
|
93 |
+
model_kwargs={"stream":False},
|
94 |
+
default_prompt_template=rag_prompt)
|
95 |
+
|
96 |
+
```
|
97 |
+
|
98 |
+
6. Build the pipeline
|
99 |
+
|
100 |
+
```python
|
101 |
+
# Set up pipeline
|
102 |
+
pipe = Pipeline()
|
103 |
+
pipe.add_node(component=retriever, name="retriever", inputs=["Query"])
|
104 |
+
pipe.add_node(component=pn, name="prompt_node", inputs=["retriever"])
|
105 |
+
```
|
106 |
+
|
107 |
+
### Connecting the pipeline to Chainlit
|
108 |
+
|
109 |
+
```python
|
110 |
+
|
111 |
+
@cl.on_message
|
112 |
+
async def main(message: str):
|
113 |
+
# Use the pipeline to get a response
|
114 |
+
output = pipe.run(query=message)
|
115 |
+
|
116 |
+
# Create a Chainlit message with the response
|
117 |
+
response = output['answers'][0].answer
|
118 |
+
msg = cl.Message(content=response)
|
119 |
+
|
120 |
+
# Send the message to the user
|
121 |
+
await msg.send()
|
122 |
+
```
|
123 |
+
|
124 |
+
### Run application
|
125 |
+
|
126 |
+
``` bash
|
127 |
+
poetry run chainlit run src/app.py --port 7860
|
128 |
+
```
|