-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
467 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This directory contains static assets for the Streamlit app: | ||
|
||
- `lambda_f.png`: Lambda character avatar for user messages | ||
- `neron_eye.gif`: NERON eye avatar for assistant messages |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import asyncio | ||
import json | ||
from pathlib import Path | ||
from typing import Dict, List | ||
|
||
import httpx | ||
import streamlit as st | ||
from pydantic import BaseModel | ||
|
||
# Configure page and paths | ||
STATIC_DIR = Path(__file__).parent / "static" | ||
st.set_page_config( | ||
page_title="eMush RAG Chatbot", | ||
page_icon="🍄", | ||
layout="wide", | ||
) | ||
|
||
|
||
class ChatMessage(BaseModel): | ||
"""Chat message model for history""" | ||
|
||
human: str | ||
assistant: str | ||
|
||
|
||
def initialize_session_state(): | ||
"""Initialize session state variables""" | ||
if "messages" not in st.session_state: | ||
st.session_state.messages = [] | ||
if "chat_history" not in st.session_state: | ||
st.session_state.chat_history = [] | ||
|
||
|
||
def display_chat_history(): | ||
"""Display chat history""" | ||
for message in st.session_state.messages: | ||
with st.chat_message("user", avatar=str(STATIC_DIR / "lambda_f.png")): | ||
st.markdown(message["human"]) | ||
with st.chat_message("assistant", avatar=str(STATIC_DIR / "neron_eye.gif")): | ||
st.markdown(message["assistant"]) | ||
if "sources" in message: | ||
with st.expander("View sources"): | ||
for source in message["sources"]: | ||
st.markdown(f"**{source['source']}** ([link]({source['link']}))\n\n{source['content']}\n\n---") | ||
|
||
|
||
async def query_chatbot(question: str, chat_history: List[Dict[str, str]]) -> Dict: | ||
"""Query the chatbot API""" | ||
async with httpx.AsyncClient() as client: | ||
try: | ||
response = await client.post( | ||
"http://localhost:8000/chat", | ||
json={"query": question, "chat_history": chat_history}, | ||
timeout=30.0, | ||
) | ||
response.raise_for_status() # Raise an error for bad status codes | ||
return response.json() | ||
except httpx.HTTPError as e: | ||
st.error(f"HTTP Error: {str(e)}") | ||
return {"error": str(e)} | ||
except Exception as e: | ||
st.error(f"Error: {str(e)}") | ||
return {"error": str(e)} | ||
|
||
|
||
def main(): | ||
"""Main Streamlit app""" | ||
st.title("🍄 eMush RAG Chatbot") | ||
st.markdown( | ||
""" | ||
Ask questions about the eMush game! The chatbot uses Retrieval-Augmented Generation (RAG) | ||
to provide accurate answers based on wikis, tutorials and QA Mush forums. | ||
""" | ||
) | ||
|
||
initialize_session_state() | ||
display_chat_history() | ||
|
||
# Chat input | ||
if question := st.chat_input("Ask a question about eMush"): | ||
with st.chat_message("user", avatar=str(STATIC_DIR / "lambda_f.png")): | ||
st.markdown(question) | ||
|
||
with st.chat_message("assistant", avatar=str(STATIC_DIR / "neron_eye.gif")): | ||
with st.spinner("Thinking..."): | ||
try: | ||
response = asyncio.run(query_chatbot(question, st.session_state.chat_history)) | ||
|
||
if "error" in response: | ||
st.error(response["error"]) | ||
return | ||
|
||
# Display response | ||
st.markdown(response["response"]) | ||
|
||
# Show sources | ||
if response["sources"]: | ||
with st.expander("View sources"): | ||
for source in response["sources"]: | ||
st.markdown( | ||
f"**{source['source']}** ([link]({source['link']}))\n\n{source['content']}\n\n---" | ||
) | ||
|
||
# Update chat history | ||
st.session_state.messages.append( | ||
{ | ||
"human": question, | ||
"assistant": response["response"], | ||
"sources": response["sources"], | ||
} | ||
) | ||
st.session_state.chat_history.append({"human": question, "assistant": response["response"]}) | ||
|
||
except Exception as e: | ||
st.error(f"Error: {str(e)}") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[project] | ||
name = "emush-rag-chatbot" | ||
version = "0.1.0" | ||
version = "0.2.0" | ||
description = "A RAG which can answer questions about eMush game." | ||
readme = "README.md" | ||
requires-python = ">=3.12,<3.13" | ||
|
@@ -9,16 +9,15 @@ authors = [ | |
{name = "Charles-Meldhine Madi Mnemoi", email = "[email protected]"} | ||
] | ||
dependencies = [ | ||
"fastapi>=0.109.0", | ||
"langchain>=0.1.0", | ||
"langchain-openai>=0.0.2", | ||
"chromadb>=0.4.0", | ||
"python-dotenv>=1.0.0", | ||
"uvicorn>=0.24.0", | ||
"pydantic>=2.5.0", | ||
"tiktoken>=0.5.0", | ||
"langchain-chroma>=0.1.4", | ||
"fastapi>=0.110.0", | ||
"httpx>=0.27.0", | ||
"langchain>=0.1.13", | ||
"langchain-chroma>=0.1.2", | ||
"langchain-core>=0.1.32", | ||
"langchain-openai>=0.0.8", | ||
"pydantic>=2.6.4", | ||
"pydantic-settings>=2.6.1", | ||
"streamlit>=1.32.2", | ||
] | ||
|
||
[dependency-groups] | ||
|
@@ -30,7 +29,7 @@ dev = [ | |
lint = [ | ||
"mypy>=1.13.0", | ||
"pytest-mypy>=0.10.3", | ||
"ruff>=0.7.2", | ||
"ruff>=0.8.0", | ||
"types-tqdm>=4.67.0.20241119", | ||
] | ||
test = [ | ||
|
Oops, something went wrong.