├── .gitignore ├── .streamlit └── config.toml ├── README.md ├── app.py ├── requirements.txt └── utils ├── config.py ├── haystack.py └── ui.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | .env -------------------------------------------------------------------------------- /.streamlit/config.toml: -------------------------------------------------------------------------------- 1 | [server] 2 | enableCORS = false 3 | enableXsrfProtection = false -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Haystack Search Pipeline with Streamlit 3 | emoji: 👑 4 | colorFrom: indigo 5 | colorTo: indigo 6 | sdk: streamlit 7 | sdk_version: 1.23.0 8 | app_file: app.py 9 | pinned: false 10 | --- 11 | 12 | # Template Streamlit App for Haystack Search Pipelines 13 | 14 | 15 | > [!WARNING] 16 | > **This template is for Haystack version 1.x**. Use this template: [Haystack Streamlit App](https://github.com/deepset-ai/haystack-streamlit-app) for Haystack 2.x applications. 17 | 18 | This template [Streamlit](https://docs.streamlit.io/) app set up for simple [Haystack search applications](https://docs.haystack.deepset.ai/docs/semantic_search). The template is ready to do QA with **Retrievel Augmented Generation**, or **Extractive QA** 19 | 20 | See the ['How to use this template'](#how-to-use-this-template) instructions below to create a simple UI for your own Haystack search pipelines. 21 | 22 | Below you will also find instructions on how you could [push this to Hugging Face Spaces 🤗](#pushing-to-hugging-face-spaces-). 23 | 24 | ## Installation and Running 25 | To run the bare application which does _nothing_: 26 | 1. Install requirements: `pip install -r requirements.txt` 27 | 2. Run the streamlit app: `streamlit run app.py` 28 | 29 | This will start up the app on `localhost:8501` where you will find a simple search bar. Before you start editing, you'll notice that the app will only show you instructions on what to edit. 30 | 31 | ### Optional Configurations 32 | 33 | You can set optional cofigurations to set the: 34 | - `--task` you want to start the app with: `rag` or `extractive` (default: rag) 35 | - `--store` you want to use: `inmemory`, `opensearch`, `weaviate` or `milvus` (default: inmemory) 36 | - `--name` you want to have for the app. (default: 'My Search App') 37 | 38 | E.g.: 39 | 40 | ```bash 41 | streamlit run app.py -- --store opensearch --task extractive --name 'My Opensearch Documentation Search' 42 | ``` 43 | 44 | In a `.env` file, include all the config settings that you would like to use based on: 45 | - The DocumentStore of your choice 46 | - The Extractive/Generative model of your choice 47 | 48 | While the `/utils/config.py` will create default values for some configurations, others have to be set in the `.env` such as the `OPENAI_KEY` 49 | 50 | Example `.env` 51 | 52 | ``` 53 | OPENAI_KEY=YOUR_KEY 54 | EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L12-v2 55 | GENERATIVE_MODEL=text-davinci-003 56 | ``` 57 | 58 | 59 | ## How to use this template 60 | 1. Create a new repository from this template or simply open it in a codespace to start playing around 💙 61 | 2. Make sure your `requirements.txt` file includes the Haystack and Streamlit versions you would like to use. 62 | 3. Change the code in `utils/haystack.py` if you would like a different pipeline. 63 | 4. Create a `.env`file with all of your configuration settings. 64 | 5. Make any UI edits you'd like to and [share with the Haystack community](https://haystack.deepeset.ai/community) 65 | 6. Run the app as show in [installation and running](#installation-and-running) 66 | 67 | ### Repo structure 68 | - `./utils`: This is where we have 3 files: 69 | - `config.py`: This file extracts all of the configuration settings from a `.env` file. For some config settings, it uses default values. An example of this is in [this demo project](https://github.com/TuanaCelik/should-i-follow/blob/main/utils/config.py). 70 | - `haystack.py`: Here you will find some functions already set up for you to start creating your Haystack search pipeline. It includes 2 main functions called `start_haystack()` which is what we use to create a pipeline and cache it, and `query()` which is the function called by `app.py` once a user query is received. 71 | - `ui.py`: Use this file for any UI and initial value setups. 72 | - `app.py`: This is the main Streamlit application file that we will run. In its current state it has a simple search bar, a 'Run' button, and a response that you can highlight answers with. 73 | 74 | ### What to edit? 75 | There are default pipelines both in `start_haystack_extractive()` and `start_haystack_rag()` 76 | 77 | - Change the pipelines to use the embedding models, extractive or generative models as you need. 78 | - If using the `rag` task, change the `default_prompt_template` to use one of our available ones on [PromptHub](https://prompthub.deepset.ai) or create your own `PromptTemplate` 79 | 80 | 81 | ## Pushing to Hugging Face Spaces 🤗 82 | 83 | Below is an example GitHub action that will let you push your Streamlit app straight to the Hugging Face Hub as a Space. 84 | 85 | A few things to pay attention to: 86 | 87 | 1. Create a New Space on Hugging Face with the Streamlit SDK. 88 | 2. Create a Hugging Face token on your HF account. 89 | 3. Create a secret on your GitHub repo called `HF_TOKEN` and put your Hugging Face token here. 90 | 4. If you're using DocumentStores or APIs that require some keys/tokens, make sure these are provided as a secret for your HF Space too! 91 | 5. This readme is set up to tell HF spaces that it's using streamlit and that the app is running on `app.py`, make any changes to the frontmatter of this readme to display the title, emoji etc you desire. 92 | 6. Create a file in `.github/workflows/hf_sync.yml`. Here's an example that you can change with your own information, and an [example workflow](https://github.com/TuanaCelik/should-i-follow/blob/main/.github/workflows/hf_sync.yml) working for the [Should I Follow demo](https://huggingface.co/spaces/deepset/should-i-follow) 93 | 94 | ```yaml 95 | name: Sync to Hugging Face hub 96 | on: 97 | push: 98 | branches: [main] 99 | 100 | # to run this workflow manually from the Actions tab 101 | workflow_dispatch: 102 | 103 | jobs: 104 | sync-to-hub: 105 | runs-on: ubuntu-latest 106 | steps: 107 | - uses: actions/checkout@v2 108 | with: 109 | fetch-depth: 0 110 | lfs: true 111 | - name: Push to hub 112 | env: 113 | HF_TOKEN: ${{ secrets.HF_TOKEN }} 114 | run: git push --force https://{YOUR_HF_USERNAME}:$HF_TOKEN@{YOUR_HF_SPACE_REPO} main 115 | ``` 116 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | 2 | import streamlit as st 3 | import logging 4 | import os 5 | 6 | from annotated_text import annotation 7 | from json import JSONDecodeError 8 | from markdown import markdown 9 | from utils.config import parser 10 | from utils.haystack import start_document_store, start_haystack_extractive, start_haystack_rag, query 11 | from utils.ui import reset_results, set_initial_state 12 | 13 | try: 14 | args = parser.parse_args() 15 | document_store = start_document_store(type = args.store) 16 | if args.task == 'extractive': 17 | pipeline = start_haystack_extractive(document_store) 18 | else: 19 | pipeline = start_haystack_rag(document_store) 20 | 21 | set_initial_state() 22 | 23 | st.write('# '+args.name) 24 | 25 | # Search bar 26 | question = st.text_input("Ask a question", value=st.session_state.question, max_chars=100, on_change=reset_results) 27 | 28 | run_pressed = st.button("Run") 29 | 30 | run_query = ( 31 | run_pressed or question != st.session_state.question 32 | ) 33 | 34 | # Get results for query 35 | if run_query and question: 36 | reset_results() 37 | st.session_state.question = question 38 | with st.spinner("🔎    Running your pipeline"): 39 | try: 40 | st.session_state.results = query(pipeline, question) 41 | except JSONDecodeError as je: 42 | st.error( 43 | "👓    An error occurred reading the results. Is the document store working?" 44 | ) 45 | except Exception as e: 46 | logging.exception(e) 47 | st.error("🐞    An error occurred during the request.") 48 | 49 | 50 | 51 | if st.session_state.results: 52 | results = st.session_state.results 53 | 54 | if args.task == 'extractive': 55 | answers = results['answers'] 56 | for count, answer in enumerate(answers): 57 | if answer.answer: 58 | text, context = answer.answer, answer.context 59 | start_idx = context.find(text) 60 | end_idx = start_idx + len(text) 61 | st.write( 62 | markdown(context[:start_idx] + str(annotation(body=text, label="ANSWER", background="#964448", color='#ffffff')) + context[end_idx:]), 63 | unsafe_allow_html=True, 64 | ) 65 | else: 66 | st.info( 67 | "🤔    Haystack is unsure whether any of the documents contain an answer to your question. Try to reformulate it!" 68 | ) 69 | elif args.task == 'rag': 70 | st.write(results['results'][0]) 71 | except SystemExit as e: 72 | # This exception will be raised if --help or invalid command line arguments 73 | # are used. Currently streamlit prevents the program from exiting normally 74 | # so we have to do a hard exit. 75 | os._exit(e.code) -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | safetensors==0.3.3.post1 2 | farm-haystack[inference,weaviate,opensearch]==1.20.0 3 | milvus-haystack 4 | streamlit==1.23.0 5 | markdown 6 | st-annotated-text 7 | datasets -------------------------------------------------------------------------------- /utils/config.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import os 3 | import os 4 | from dotenv import load_dotenv 5 | 6 | load_dotenv() 7 | parser = argparse.ArgumentParser(description='This app lists animals') 8 | 9 | document_store_choices = ('inmemory', 'weaviate', 'milvus', 'opensearch') 10 | task_choices = ('extractive', 'rag') 11 | parser.add_argument('--store', choices=document_store_choices, default='inmemory', help='DocumentStore selection (default: %(default)s)') 12 | parser.add_argument('--task', choices=task_choices, default='rag', help='Task selection (default: %(default)s)') 13 | parser.add_argument('--name', default="My Search App") 14 | 15 | model_configs = { 16 | 'EMBEDDING_MODEL': os.getenv("EMBEDDING_MODEL", "sentence-transformers/all-MiniLM-L12-v2"), 17 | 'GENERATIVE_MODEL': os.getenv("GENERATIVE_MODEL", "gpt-4"), 18 | 'EXTRACTIVE_MODEL': os.getenv("EXTRACTIVE_MODEL", "deepset/roberta-base-squad2"), 19 | 'OPENAI_KEY': os.getenv("OPENAI_KEY"), 20 | 'COHERE_KEY': os.getenv("COHERE_KEY"), 21 | } 22 | 23 | document_store_configs = { 24 | # Weaviate Config 25 | 'WEAVIATE_HOST': os.getenv("WEAVIATE_HOST", "http://localhost"), 26 | 'WEAVIATE_PORT': os.getenv("WEAVIATE_PORT", 8080), 27 | 'WEAVIATE_INDEX': os.getenv("WEAVIATE_INDEX", "Document"), 28 | 'WEAVIATE_EMBEDDING_DIM': os.getenv("WEAVIATE_EMBEDDING_DIM", 768), 29 | 30 | # OpenSearch Config 31 | 'OPENSEARCH_SCHEME': os.getenv("OPENSEARCH_SCHEME", "https"), 32 | 'OPENSEARCH_USERNAME': os.getenv("OPENSEARCH_USERNAME", "admin"), 33 | 'OPENSEARCH_PASSWORD': os.getenv("OPENSEARCH_PASSWORD", "admin"), 34 | 'OPENSEARCH_HOST': os.getenv("OPENSEARCH_HOST", "localhost"), 35 | 'OPENSEARCH_PORT': os.getenv("OPENSEARCH_PORT", 9200), 36 | 'OPENSEARCH_INDEX': os.getenv("OPENSEARCH_INDEX", "document"), 37 | 'OPENSEARCH_EMBEDDING_DIM': os.getenv("OPENSEARCH_EMBEDDING_DIM", 768), 38 | 39 | # Milvus Config 40 | 'MILVUS_URI': os.getenv("MILVUS_URI", "http://localhost:19530/default"), 41 | 'MILVUS_INDEX': os.getenv("MILVUS_INDEX", "document"), 42 | 'MILVUS_EMBEDDING_DIM': os.getenv("MILVUS_EMBEDDING_DIM", 768), 43 | } -------------------------------------------------------------------------------- /utils/haystack.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | 3 | from utils.config import document_store_configs, model_configs 4 | from haystack import Pipeline 5 | from haystack.schema import Answer 6 | from haystack.document_stores import BaseDocumentStore 7 | from haystack.document_stores import InMemoryDocumentStore, OpenSearchDocumentStore, WeaviateDocumentStore 8 | from haystack.nodes import EmbeddingRetriever, FARMReader, PromptNode 9 | from milvus_haystack import MilvusDocumentStore 10 | #Use this file to set up your Haystack pipeline and querying 11 | 12 | @st.cache_resource(show_spinner=False) 13 | def start_document_store(type: str): 14 | #This function starts the documents store of your choice based on your command line preference 15 | if type == 'inmemory': 16 | document_store = InMemoryDocumentStore(use_bm25=True) 17 | elif type == 'opensearch': 18 | document_store = OpenSearchDocumentStore(scheme = document_store_configs['OPENSEARCH_SCHEME'], 19 | username = document_store_configs['OPENSEARCH_USERNAME'], 20 | password = document_store_configs['OPENSEARCH_PASSWORD'], 21 | host = document_store_configs['OPENSEARCH_HOST'], 22 | port = document_store_configs['OPENSEARCH_PORT'], 23 | index = document_store_configs['OPENSEARCH_INDEX'], 24 | embedding_dim = document_store_configs['OPENSEARCH_EMBEDDING_DIM']) 25 | elif type == 'weaviate': 26 | document_store = WeaviateDocumentStore(host = document_store_configs['WEAVIATE_HOST'], 27 | port = document_store_configs['WEAVIATE_PORT'], 28 | index = document_store_configs['WEAVIATE_INDEX'], 29 | embedding_dim = document_store_configs['WEAVIATE_EMBEDDING_DIM']) 30 | elif type == 'milvus': 31 | document_store = MilvusDocumentStore(uri = document_store_configs['MILVUS_URI'], 32 | index = document_store_configs['MILVUS_INDEX'], 33 | embedding_dim = document_store_configs['MILVUS_EMBEDDING_DIM'], 34 | return_embedding=True) 35 | return document_store 36 | 37 | # cached to make index and models load only at start 38 | @st.cache_resource(show_spinner=False) 39 | def start_haystack_extractive(_document_store: BaseDocumentStore): 40 | retriever = EmbeddingRetriever(document_store=_document_store, 41 | embedding_model=model_configs['EMBEDDING_MODEL'], 42 | top_k=5) 43 | 44 | reader = FARMReader(model_name_or_path=model_configs['EXTRACTIVE_MODEL']) 45 | 46 | pipe = Pipeline() 47 | pipe.add_node(component=retriever, name="Retriever", inputs=["Query"]) 48 | pipe.add_node(component=reader, name="Reader", inputs=["Retriever"]) 49 | 50 | return pipe 51 | 52 | @st.cache_resource(show_spinner=False) 53 | def start_haystack_rag(_document_store: BaseDocumentStore): 54 | retriever = EmbeddingRetriever(document_store=_document_store, 55 | embedding_model=model_configs['EMBEDDING_MODEL'], 56 | top_k=5) 57 | 58 | prompt_node = PromptNode(default_prompt_template="deepset/question-answering", 59 | model_name_or_path=model_configs['GENERATIVE_MODEL'], 60 | api_key=model_configs['OPENAI_KEY']) 61 | pipe = Pipeline() 62 | 63 | pipe.add_node(component=retriever, name="Retriever", inputs=["Query"]) 64 | pipe.add_node(component=prompt_node, name="PromptNode", inputs=["Retriever"]) 65 | 66 | return pipe 67 | 68 | @st.cache_data(show_spinner=True) 69 | def query(_pipeline, question): 70 | params = {} 71 | results = _pipeline.run(question, params=params) 72 | return results -------------------------------------------------------------------------------- /utils/ui.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | 3 | def set_state_if_absent(key, value): 4 | if key not in st.session_state: 5 | st.session_state[key] = value 6 | 7 | def set_initial_state(): 8 | set_state_if_absent("question", "Ask something here?") 9 | set_state_if_absent("results", None) 10 | 11 | def reset_results(*args): 12 | st.session_state.results = None --------------------------------------------------------------------------------