Use streamlit session cache to avoid reloading the db for each question
This commit is contained in:
parent
82830cd8d6
commit
0c88cfe15f
1 changed files with 45 additions and 15 deletions
|
|
@ -3,15 +3,26 @@ import tempfile
|
||||||
from embedchain import App
|
from embedchain import App
|
||||||
from embedchain.loaders.github import GithubLoader
|
from embedchain.loaders.github import GithubLoader
|
||||||
import streamlit as st
|
import streamlit as st
|
||||||
|
import os
|
||||||
|
|
||||||
loader = GithubLoader(
|
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
|
||||||
config={
|
def get_loader():
|
||||||
"token":"Your GitHub Token",
|
print("Creating GithubLoader")
|
||||||
|
loader = GithubLoader(
|
||||||
|
config={
|
||||||
|
"token": GITHUB_TOKEN
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
return loader
|
||||||
|
|
||||||
|
if "loader" not in st.session_state:
|
||||||
|
st.session_state['loader'] = get_loader()
|
||||||
|
|
||||||
|
loader = st.session_state.loader
|
||||||
|
|
||||||
# Define the embedchain_bot function
|
# Define the embedchain_bot function
|
||||||
def embedchain_bot(db_path):
|
def embedchain_bot(db_path):
|
||||||
|
print("Creating Embedchain App")
|
||||||
return App.from_config(
|
return App.from_config(
|
||||||
config={
|
config={
|
||||||
"llm": {"provider": "ollama", "config": {"model": "llama3:instruct", "max_tokens": 250, "temperature": 0.5, "stream": True, "base_url": 'http://localhost:11434'}},
|
"llm": {"provider": "ollama", "config": {"model": "llama3:instruct", "max_tokens": 250, "temperature": 0.5, "stream": True, "base_url": 'http://localhost:11434'}},
|
||||||
|
|
@ -20,24 +31,43 @@ def embedchain_bot(db_path):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def load_repo(git_repo):
|
||||||
|
global app
|
||||||
|
# Add the repo to the knowledge base
|
||||||
|
print(f"Adding {git_repo} to knowledge base!")
|
||||||
|
app.add("repo:" + git_repo + " " + "type:repo", data_type="github", loader=loader)
|
||||||
|
# st.success(f"Added {git_repo} to knowledge base!")
|
||||||
|
|
||||||
|
|
||||||
|
def make_db_path():
|
||||||
|
ret = tempfile.mkdtemp(suffix="chroma")
|
||||||
|
print(f"Created Chroma DB at {ret}")
|
||||||
|
return ret
|
||||||
|
|
||||||
# Create Streamlit app
|
# Create Streamlit app
|
||||||
st.title("Chat with GitHub Repository 💬")
|
st.title("Chat with GitHub Repository 💬")
|
||||||
st.caption("This app allows you to chat with a GitHub Repo using Llama-3 running with Ollama")
|
st.caption("This app allows you to chat with a GitHub Repo using Llama-3 running with Ollama")
|
||||||
|
|
||||||
# Initialize the Embedchain App
|
# Initialize the Embedchain App
|
||||||
db_path = tempfile.mkdtemp()
|
if "app" not in st.session_state:
|
||||||
app = embedchain_bot(db_path)
|
st.session_state['app'] = embedchain_bot(make_db_path())
|
||||||
|
|
||||||
|
app = st.session_state.app
|
||||||
|
|
||||||
# Get the GitHub repo from the user
|
# Get the GitHub repo from the user
|
||||||
git_repo = st.text_input("Enter the GitHub Repo", type="default")
|
git_repo = st.text_input("Enter the GitHub Repo", type="default")
|
||||||
|
|
||||||
if git_repo:
|
if git_repo and ("repos" not in st.session_state or git_repo not in st.session_state.repos):
|
||||||
# Add the repo to the knowledge base
|
if "repos" not in st.session_state:
|
||||||
app.add("repo:" + git_repo + " " + "type:repo", data_type="github", loader=loader)
|
st.session_state["repos"] = [git_repo]
|
||||||
st.success(f"Added {git_repo} to knowledge base!")
|
else:
|
||||||
# Ask a question about the Github Repo
|
st.session_state.repos.append(git_repo)
|
||||||
prompt = st.text_input("Ask any question about the GitHub Repo")
|
load_repo(git_repo)
|
||||||
# Chat with the GitHub Repo
|
|
||||||
if prompt:
|
|
||||||
answer = app.chat(prompt)
|
# Ask a question about the Github Repo
|
||||||
st.write(answer)
|
prompt = st.text_input("Ask any question about the GitHub Repo")
|
||||||
|
# Chat with the GitHub Repo
|
||||||
|
if prompt:
|
||||||
|
answer = st.session_state.app.chat(prompt)
|
||||||
|
st.write(answer)
|
||||||
Loading…
Reference in a new issue