{"id":24929722,"url":"https://github.com/matanyap/llamaindex-streamlit-demo","last_synced_at":"2025-04-15T01:34:44.205Z","repository":{"id":194531433,"uuid":"691036536","full_name":"MatanyaP/llamaindex-streamlit-demo","owner":"MatanyaP","description":null,"archived":false,"fork":false,"pushed_at":"2023-09-13T11:33:41.000Z","size":3,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T13:33:24.207Z","etag":null,"topics":["demo-app","llamaindex","streamlit"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MatanyaP.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-13T11:29:36.000Z","updated_at":"2024-11-12T02:13:15.000Z","dependencies_parsed_at":"2023-09-13T22:17:08.980Z","dependency_job_id":null,"html_url":"https://github.com/MatanyaP/llamaindex-streamlit-demo","commit_stats":null,"previous_names":["matanyap/llamaindex-streamlit-demo"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatanyaP%2Fllamaindex-streamlit-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatanyaP%2Fllamaindex-streamlit-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatanyaP%2Fllamaindex-streamlit-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatanyaP%2Fllamaindex-streamlit-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MatanyaP","download_url":"https://codeload.github.com/MatanyaP/llamaindex-streamlit-demo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248989604,"owners_count":21194620,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["demo-app","llamaindex","streamlit"],"created_at":"2025-02-02T13:51:42.837Z","updated_at":"2025-04-15T01:34:44.189Z","avatar_url":"https://github.com/MatanyaP.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Langchain Streamlit App with GPT-3.5\n\n## 1. Configure app secrets\n\nThis app will use GPT-3.5, so we'll also need an OpenAI API key.\n\nCreate a `.streamlit` folder and a `secrets.toml` file with the following contents.\n\n```toml\nopenai_key = \"\u003cyour OpenAI API key here\u003e\"\n```\n\n**Note**: If you're using Git, be sure to add the name of this file to your `.gitignore` so you don't accidentally expose your API key. If you plan to deploy this app on Streamlit Community Cloud, the following contents should be added to your app's secrets via the Community Cloud modal.\n\n## 2. Install dependencies\n\n### 2.1. Local development\n\nIf you're working on your local machine, install dependencies using pip:\n\n```bash\npip install streamlit openai llama-index nltk\n```\n\n### 2.2. Cloud development\n\nIf you're planning to deploy this app on Streamlit Community Cloud, create a `requirements.txt` file with the following contents:\n\n```bash\nstreamlit\nopenai\nllama-index\nnltk\n```\n\n## 3. Build the app\n\nThe full app is only ~50 lines of code. Let's break down each section:\n\n### 3.1. Import libraries\n\nRequired Python libraries for this app: streamlit, llama_index, openai, and nltk.\n\n```python\nimport streamlit as st\nfrom llama_index import VectorStoreIndex, ServiceContext, Document\nfrom llama_index.llms import OpenAI\nimport openai\nfrom llama_index import SimpleDirectoryReader\n```\n\n# ←←\n\n### 3.2. Initialize message history\n\n- Set our OpenAI API key from the app's secrets.\n- Add a heading for our app.\n- Use session state to keep track of your chatbot's message history.\n- Initialize the value of `st.session_state.messages` to include the chatbot's starting message, such as, \"Hey, I'm a chatbot! Ask me a question.\"\n\n# ←←\n\n### 3.3. Load and index data\n\n- We'll Store our Knowledge Base files in a folder called `data` within the app.\n\n- We'll define a function `load_data()` to load and index data using LlamaIndex:\n\n#### Function: `load_data()`\n\nThe `load_data()` function is responsible for loading and indexing data stored in a specific directory, typically the `data` directory at the base level of your repository.\n\nHere's a step-by-step breakdown of what the function does:\n\n1. **Using SimpleDirectoryReader**: LlamaIndex's `SimpleDirectoryReader` will take the path to your data directory as an argument. It's designed to automatically choose the right file reader based on the file extensions within the directory. In our case, it will select the reader for `.pdf` files. The reader will then load all the files recursively when you call `reader.load_data()`.\n\n2. **Creating a ServiceContext Instance**: Construct an instance of LlamaIndex’s `ServiceContext`. This encapsulates a collection of resources utilized during a RAG pipeline's indexing and querying processes. One of the key features of `ServiceContext` is the ability to adjust settings. For instance, you can specify which LLM and embedding model you want to use.\n\n3. **Setting up VectorStoreIndex**: Use LlamaIndex’s `VectorStoreIndex` to create an in-memory `SimpleVectorStore`. This structures your data in a manner optimized for quick context retrieval by your model. If you're interested in diving deeper, you can explore more about LlamaIndex’s indices and their inner workings.\n\n4. **Caching**: This function is wrapped in Streamlit’s caching decorator `st.cache_resource` to minimize the number of times the data is loaded and indexed.\n\nThe function will return the `VectorStoreIndex` object upon completion.\n\n# ←←\n\n---\n\n### 3.4. Create the chat engine\n\nLlamaIndex offers various modes of chat engines. Here we will use the `condense_question` mode as it always queries the knowledge base which is optimal for our use-case.\n\n# ←←\n\n### 3.5. Prompt for user input and display message history\n\nUse Streamlit's UI elements to gather user input and display the chatbot's message history.\n\n# ←←\n\n### 3.6. Pass query to chat engine and display response\n\nAfter gathering the user's question, pass it to the chat engine to get a response, then display both the question and the answer in a chat-like interface.\n\n# ←←\n\n### 3.7. Run the app\n\nRun the app with `streamlit run app.py`.\n\n# ←←\n\n### 3.8. Deploy the app\n\nDeploy the app on Streamlit Community Cloud following this [guide](https://blog.streamlit.io/host-your-streamlit-app-for-free/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatanyap%2Fllamaindex-streamlit-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatanyap%2Fllamaindex-streamlit-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatanyap%2Fllamaindex-streamlit-demo/lists"}