├── .DS_Store ├── .gitignore ├── README.md ├── data ├── AINews.png └── ai_news_rss.txt ├── get_data_from_rss.py ├── main.py ├── make_collection.py ├── query_gradient.py └── requirements.txt /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkamradt/RAGWithGradient/460260049589dd3675cff02b5503afbf30843a37/.DS_Store -------------------------------------------------------------------------------- /.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 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 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 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Greg's 132 | scratch/ 133 | 134 | # Spyder project settings 135 | .spyderproject 136 | .spyproject 137 | 138 | # Rope project settings 139 | .ropeproject 140 | 141 | # mkdocs documentation 142 | /site 143 | 144 | # mypy 145 | .mypy_cache/ 146 | .dmypy.json 147 | dmypy.json 148 | 149 | # Pyre type checker 150 | .pyre/ 151 | 152 | # pytype static type analyzer 153 | .pytype/ 154 | 155 | # Cython debug symbols 156 | cython_debug/ 157 | 158 | # PyCharm 159 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 160 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 161 | # and can be added to the global gitignore or merged into this file. For a more nuclear 162 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 163 | #.idea/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RAGWithGradient 2 | 3 | Check out the [tutorial video](https://youtu.be/Hkgz1ysv9Fk). 4 | 5 | This wouldn't be possible without [Gradient](https://gradient.ai/). 6 | 7 | Key you'll need: 8 | 9 | * `GRADIENT_ACCESS_TOKEN` - Get this at https://gradient.ai/ 10 | * `GRADIENT_WORKSPACE_ID` - Get this at https://gradient.ai/ 11 | * `GRADIENT_RAG_ID` - Get this as a response when you make your first collection 12 | 13 | Made with ❤️ by [Greg Kamradt](https://gregkamradt.com), get updates as [new projects come out](https://mail.gregkamradt.com/signup) -------------------------------------------------------------------------------- /data/AINews.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gkamradt/RAGWithGradient/460260049589dd3675cff02b5503afbf30843a37/data/AINews.png -------------------------------------------------------------------------------- /get_data_from_rss.py: -------------------------------------------------------------------------------- 1 | import xml.etree.ElementTree as ET 2 | import requests 3 | 4 | # URL of the XML data 5 | url = "https://buttondown.email/ainews/rss" 6 | 7 | # Fetch the XML data 8 | response = requests.get(url) 9 | xml_data = response.text 10 | 11 | # Parse the XML 12 | root = ET.fromstring(xml_data) 13 | 14 | # Function to recursively extract text from the XML tree 15 | def extract_text(element): 16 | text = '' 17 | if element.text: 18 | text += element.text.strip() + ' ' 19 | for child in element: 20 | text += extract_text(child) 21 | if element.tail: 22 | text += element.tail.strip() + ' ' 23 | return text 24 | 25 | # Extract text from the root element 26 | text_content = extract_text(root) 27 | 28 | with open("data/ai_news_rss.txt", "w") as file: 29 | file.write(text_content) -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from gradientai import Gradient 2 | from dotenv import load_dotenv 3 | import os 4 | import streamlit as st 5 | 6 | load_dotenv() 7 | 8 | gradient = Gradient( 9 | access_token=os.getenv("GRADIENT_ACCESS_TOKEN"), 10 | workspace_id=os.getenv("GRADIENT_WORKSPACE_ID") 11 | ) 12 | 13 | st.title("AI News Bot") 14 | st.subheader('Pulling data from [AI News](https://buttondown.email/ainews/)') 15 | st.markdown('Using [Gradient](https://gradient.com) for hosted RAG and answer questions about AI News (from [@swyx](https://twitter.com/swyx)). Check out the video [here](https://www.youtube.com/@DataIndependent)') 16 | st.image('data/AINews.png') 17 | st.subheader("", divider='rainbow') 18 | 19 | # Initialize chat history 20 | if "messages" not in st.session_state: 21 | st.session_state.messages = [] 22 | 23 | # Display chat messages from history on app rerun 24 | for message in st.session_state.messages: 25 | with st.chat_message(message["role"]): 26 | st.markdown(message["content"]) 27 | 28 | # React to user input 29 | if prompt := st.chat_input("What is up?"): 30 | # Display user message in chat message container 31 | st.chat_message("user").markdown(prompt) 32 | # Add user message to chat history 33 | st.session_state.messages.append({"role": "user", "content": f"Be concise in your reponse. Give bullet points when possible \n\n User prompt: {prompt}"}) 34 | 35 | result = gradient.answer( 36 | question=prompt, 37 | source={ 38 | "type": "rag", 39 | "collectionId" : os.getenv("GRADIENT_RAG_ID") 40 | } 41 | )['answer'] 42 | 43 | response = f"{result}" 44 | # Display assistant response in chat message container 45 | with st.chat_message("assistant"): 46 | st.markdown(response) 47 | # Add assistant response to chat history 48 | st.session_state.messages.append({"role": "assistant", "content": response}) -------------------------------------------------------------------------------- /make_collection.py: -------------------------------------------------------------------------------- 1 | from gradientai import Gradient 2 | from dotenv import load_dotenv 3 | import os 4 | 5 | load_dotenv() 6 | 7 | gradient = Gradient( 8 | access_token=os.getenv("GRADIENT_ACCESS_TOKEN"), 9 | workspace_id=os.getenv("GRADIENT_WORKSPACE_ID") 10 | ) 11 | 12 | rag_collection = gradient.create_rag_collection( 13 | name="RAG with AI News RSS V3", 14 | slug="bge-large", 15 | filepaths=[ 16 | "data/ai_news_rss.txt", 17 | ], 18 | ) 19 | 20 | print (f"Your rag_collection.id_ is {rag_collection.id_}") 21 | 22 | # # Add more files to your collection if you would like 23 | # rag_id = "your_rag_collection_id" 24 | # rag_collection = gradient.get_rag_collection(id_=rag_id) 25 | 26 | # rag_collection.add_files(filepaths = ["docs/one.txt", "docs/two.txt"]) -------------------------------------------------------------------------------- /query_gradient.py: -------------------------------------------------------------------------------- 1 | from gradientai import Gradient 2 | from dotenv import load_dotenv 3 | import os 4 | 5 | load_dotenv() 6 | 7 | gradient = Gradient( 8 | access_token=os.getenv("GRADIENT_ACCESS_TOKEN"), 9 | workspace_id=os.getenv("GRADIENT_WORKSPACE_ID") 10 | ) 11 | 12 | result = gradient.answer( 13 | question="What is the latest on mistral?", 14 | source={ 15 | "type": "rag", 16 | "collectionId" : os.getenv("GRADIENT_RAG_ID") 17 | } 18 | )['answer'] 19 | 20 | print (result) -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | aenum==3.1.15 2 | altair==5.3.0 3 | attrs==23.2.0 4 | blinker==1.7.0 5 | cachetools==5.3.3 6 | certifi==2024.2.2 7 | charset-normalizer==3.3.2 8 | click==8.1.7 9 | gitdb==4.0.11 10 | GitPython==3.1.43 11 | gradientai==1.9.1 12 | idna==3.7 13 | Jinja2==3.1.3 14 | jsonschema==4.21.1 15 | jsonschema-specifications==2023.12.1 16 | markdown-it-py==3.0.0 17 | MarkupSafe==2.1.5 18 | mdurl==0.1.2 19 | numpy==1.26.4 20 | packaging==24.0 21 | pandas==2.2.2 22 | pillow==10.3.0 23 | protobuf==4.25.3 24 | pyarrow==15.0.2 25 | pydantic==1.10.15 26 | pydeck==0.8.1b0 27 | Pygments==2.17.2 28 | python-dateutil==2.9.0.post0 29 | python-dotenv==1.0.1 30 | pytz==2024.1 31 | referencing==0.34.0 32 | requests==2.31.0 33 | rich==13.7.1 34 | rpds-py==0.18.0 35 | six==1.16.0 36 | smmap==5.0.1 37 | streamlit==1.33.0 38 | tenacity==8.2.3 39 | toml==0.10.2 40 | toolz==0.12.1 41 | tornado==6.4 42 | typing_extensions==4.11.0 43 | tzdata==2024.1 44 | urllib3==2.2.1 45 | --------------------------------------------------------------------------------