Skip to content

Commit

Permalink
upgrade RAGs (run-llama#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryjliu authored Nov 26, 2023
1 parent 55c95f1 commit 49b8679
Show file tree
Hide file tree
Showing 12 changed files with 829 additions and 234 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Linting

on:
push:
branches:
- main
pull_request:

jobs:
build:
runs-on: ubuntu-latest
strategy:
# You can use PyPy versions in python-version.
# For example, pypy-2.7 and pypy-3.8
matrix:
python-version: ["3.9"]
poetry-version: [1.5.1]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Run image
uses: abatilo/[email protected]
with:
poetry-version: ${{ matrix.poetry-version }}
- name: Install deps
run: |
poetry install --with dev
- name: Run Linting
run: poetry run make lint
89 changes: 64 additions & 25 deletions 1_🏠_Home.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

from agent_utils import (
load_meta_agent_and_tools,
load_agent_ids_from_directory,
)
from st_utils import add_sidebar
from constants import (
AGENT_CACHE_DIR,
)


Expand All @@ -11,23 +16,41 @@
####################


st.set_page_config(page_title="Build a RAGs bot, powered by LlamaIndex", page_icon="🦙", layout="centered", initial_sidebar_state="auto", menu_items=None)
st.set_page_config(
page_title="Build a RAGs bot, powered by LlamaIndex",
page_icon="🦙",
layout="centered",
initial_sidebar_state="auto",
menu_items=None,
)
st.title("Build a RAGs bot, powered by LlamaIndex 💬🦙")
st.info(
"Use this page to build your RAG bot over your data! "
"Once the agent is finished creating, check out the `RAG Config` and `Generated RAG Agent` pages.",
icon="ℹ️"
"Once the agent is finished creating, check out the `RAG Config` and "
"`Generated RAG Agent` pages.\n"
"To build a new agent, please make sure that 'Create a new agent' is selected.",
icon="ℹ️",
)

# TODO: noodle on this
# with st.sidebar:
# openai_api_key_st = st.text_input("OpenAI API Key (optional, not needed if you filled in secrets.toml)", value="", type="password")
# if st.button("Save"):
# # save api key
# st.session_state.openai_api_key = openai_api_key_st

#### load builder agent and its tool spec (the agent_builder)
builder_agent, agent_builder = load_meta_agent_and_tools()
add_sidebar()


if (
"selected_cache" in st.session_state.keys()
and st.session_state.selected_cache is not None
):
# create builder agent / tools from selected cache
builder_agent, agent_builder = load_meta_agent_and_tools(
cache=st.session_state.selected_cache
)
else:
# create builder agent / tools from new cache
builder_agent, agent_builder = load_meta_agent_and_tools()


st.info(f"Currently building/editing agent: {agent_builder.cache.agent_id}", icon="ℹ️")


if "builder_agent" not in st.session_state.keys():
st.session_state.builder_agent = builder_agent
Expand All @@ -36,27 +59,34 @@

# add pills
selected = pills(
"Outline your task!",
["I want to analyze this PDF file (data/invoices.pdf)",
"I want to search over my CSV documents."
], clearable=True, index=None
"Outline your task!",
[
"I want to analyze this PDF file (data/invoices.pdf)",
"I want to search over my CSV documents.",
],
clearable=True,
index=None,
)

if "messages" not in st.session_state.keys(): # Initialize the chat messages history
if "messages" not in st.session_state.keys(): # Initialize the chat messages history
st.session_state.messages = [
{"role": "assistant", "content": "What RAG bot do you want to build?"}
]

def add_to_message_history(role, content):

def add_to_message_history(role: str, content: str) -> None:
message = {"role": role, "content": str(content)}
st.session_state.messages.append(message) # Add response to message history
st.session_state.messages.append(message) # Add response to message history


for message in st.session_state.messages: # Display the prior chat messages
for message in st.session_state.messages: # Display the prior chat messages
with st.chat_message(message["role"]):
st.write(message["content"])

# handle user input
if prompt := st.chat_input("Your question"): # Prompt for user input and save to chat history
if prompt := st.chat_input(
"Your question"
): # Prompt for user input and save to chat history
add_to_message_history("user", prompt)
with st.chat_message("user"):
st.write(prompt)
Expand All @@ -67,9 +97,18 @@ def add_to_message_history(role, content):
with st.spinner("Thinking..."):
response = st.session_state.builder_agent.chat(prompt)
st.write(str(response))
add_to_message_history("assistant", response)
add_to_message_history("assistant", str(response))

# check agent_ids again, if it doesn't match, add to directory and refresh
agent_ids = load_agent_ids_from_directory(str(AGENT_CACHE_DIR))
# check diff between agent_ids and cur agent ids
diff_ids = list(set(agent_ids) - set(st.session_state.cur_agent_ids))
if len(diff_ids) > 0:
# clear streamlit cache, to allow you to generate a new agent
st.cache_resource.clear()

# trigger refresh
st.rerun()

# # check cache
print(st.session_state.agent_builder.cache)
# if "agent" in cache:
# st.session_state.agent = cache["agent"]
else:
pass
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.PHONY: format lint

format:
black .
lint:
mypy .
black --check .
ruff check .
test:
pytest tests
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ This project is inspired by [GPTs](https://openai.com/blog/introducing-gpts), la

## Installation and Setup

Clone this project, go into the `rags` project folder.
Clone this project, go into the `rags` project folder. We recommend creating a virtual env for dependencies (`python3 -m venv .venv`).

```
pip install -r requirements.txt
poetry install --with dev
```

By default, we use OpenAI for both the builder agent as well as the generated RAG agent.
Expand Down
Loading

0 comments on commit 49b8679

Please sign in to comment.