Spaces:
Sleeping
Sleeping
initaialized chain and merged from main
Browse files- .devcontainer/Dockerfile +12 -3
- .devcontainer/devcontainer.json +1 -1
- .github/ISSUE_TEMPLATE/issue_template.md +34 -0
- .github/ISSUE_TEMPLATE/pullrequest_template.md +28 -0
- .github/workflows/check_file_size.yaml +16 -0
- .github/workflows/sync_2_hf.yaml +20 -0
- README.md +12 -0
- app_gui.py +73 -9
- rag_app/chains/__init__.py +2 -1
- rag_app/hybrid_search.py +63 -0
.devcontainer/Dockerfile
CHANGED
@@ -44,6 +44,15 @@ RUN echo "done 0" \
|
|
44 |
&& pyenv global ${PYTHON_VERSION} \
|
45 |
&& echo "done 3" \
|
46 |
&& curl -sSL https://install.python-poetry.org | python3 - \
|
47 |
-
&& poetry config virtualenvs.in-project true
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
&& pyenv global ${PYTHON_VERSION} \
|
45 |
&& echo "done 3" \
|
46 |
&& curl -sSL https://install.python-poetry.org | python3 - \
|
47 |
+
&& poetry config virtualenvs.in-project true
|
48 |
+
|
49 |
+
COPY requirements.txt /tmp/
|
50 |
+
RUN DEBIAN_FRONTEND=noninteractive \
|
51 |
+
&& pip install --requirements /tmp/requirements.txt
|
52 |
+
COPY . /tmp/
|
53 |
+
|
54 |
+
RUN DEBIAN_FRONTEND=noninteractive \
|
55 |
+
&& python -m pip install --upgrade pip
|
56 |
+
|
57 |
+
ARG USERNAME
|
58 |
+
COPY C:/Users/${USERNAME}/.ssh/id_ed25519 ${HOME}/.ssh/
|
.devcontainer/devcontainer.json
CHANGED
@@ -16,7 +16,7 @@
|
|
16 |
// 👇 Configure tool-specific properties.
|
17 |
"customizations": {
|
18 |
"vscode": {
|
19 |
-
"extensions":["ms-python.python",
|
20 |
}
|
21 |
}
|
22 |
|
|
|
16 |
// 👇 Configure tool-specific properties.
|
17 |
"customizations": {
|
18 |
"vscode": {
|
19 |
+
"extensions":["ms-python.python","njpwerner.autodocstring","ms-azuretools.vscode-docker","qwtel.sqlite-viewer"]
|
20 |
}
|
21 |
}
|
22 |
|
.github/ISSUE_TEMPLATE/issue_template.md
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Issue Template for LLM + RAG Applications
|
2 |
+
|
3 |
+
## Description
|
4 |
+
Please provide a clear and concise description of the issue. Include what you expected to happen versus what actually happened. If the issue is related to retrieval results or generative outputs, specify the discrepancies.
|
5 |
+
|
6 |
+
## Steps to Reproduce
|
7 |
+
1. Detail the exact steps to reproduce the problem. Include any specific inputs given to the system.
|
8 |
+
2.
|
9 |
+
3.
|
10 |
+
(Continue as needed)
|
11 |
+
|
12 |
+
## Expected vs. Actual Results
|
13 |
+
- **Expected Results**: Describe what you expected to happen.
|
14 |
+
- **Actual Results**: Detail what actually happened, including any unexpected outputs or errors.
|
15 |
+
|
16 |
+
## Environment
|
17 |
+
- Application version:
|
18 |
+
- LLM model version:
|
19 |
+
- RAG component (if applicable):
|
20 |
+
- OS:
|
21 |
+
- Additional tools/libraries:
|
22 |
+
|
23 |
+
## Query or Input Details
|
24 |
+
- **Input Query/Text**: Provide the exact text or input given to the system.
|
25 |
+
- **Retrieval Source(s)**: Specify the datasets or sources queried by the RAG component, if relevant.
|
26 |
+
|
27 |
+
## Logs and Error Messages
|
28 |
+
Please include any relevant logs, error messages, or stack traces that could help diagnose the issue.
|
29 |
+
|
30 |
+
## Screenshots
|
31 |
+
If applicable, add screenshots to help explain the issue, especially if it involves UI elements.
|
32 |
+
|
33 |
+
## Additional Context
|
34 |
+
Add any other context about the problem here, such as specific configurations of the LLM or RAG components that might be relevant.
|
.github/ISSUE_TEMPLATE/pullrequest_template.md
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Pull Request Template
|
2 |
+
|
3 |
+
## Description
|
4 |
+
Please include a brief description of the changes introduced by this PR.
|
5 |
+
|
6 |
+
## Related Issue(s)
|
7 |
+
- If this PR addresses a particular issue, please reference it here using GitHub's linking syntax, e.g., "Fixes #123".
|
8 |
+
- If there's no related issue, briefly explain the motivation behind these changes.
|
9 |
+
|
10 |
+
## Changes Made
|
11 |
+
Please provide a list of the changes made in this PR.
|
12 |
+
|
13 |
+
## Screenshots (if applicable)
|
14 |
+
If the changes include UI updates or visual changes, please attach relevant screenshots here.
|
15 |
+
|
16 |
+
## Checklist
|
17 |
+
- [ ] I have tested my changes locally and ensured that they work as expected.
|
18 |
+
- [ ] I have updated the documentation (if applicable).
|
19 |
+
- [ ] My code follows the project's coding conventions and style guidelines.
|
20 |
+
- [ ] I have added appropriate test cases (if applicable).
|
21 |
+
- [ ] I have reviewed my own code to ensure its quality.
|
22 |
+
|
23 |
+
## Additional Notes
|
24 |
+
Add any additional notes or context about this PR here.
|
25 |
+
|
26 |
+
## Reviewer(s)
|
27 |
+
- @reviewer1
|
28 |
+
- @reviewer2
|
.github/workflows/check_file_size.yaml
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: Check file size
|
2 |
+
on: # or directly `on: [push]` to run the action on every push on any branch
|
3 |
+
pull_request:
|
4 |
+
branches: [main]
|
5 |
+
|
6 |
+
# to run this workflow manually from the Actions tab
|
7 |
+
workflow_dispatch:
|
8 |
+
|
9 |
+
jobs:
|
10 |
+
sync-to-hub:
|
11 |
+
runs-on: ubuntu-latest
|
12 |
+
steps:
|
13 |
+
- name: Check large files
|
14 |
+
uses: ActionsDesk/[email protected]
|
15 |
+
with:
|
16 |
+
filesizelimit: 10485760 # this is 10MB so we can sync to HF Spaces
|
.github/workflows/sync_2_hf.yaml
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: Sync to Hugging Face hub
|
2 |
+
on:
|
3 |
+
push:
|
4 |
+
branches: [main]
|
5 |
+
|
6 |
+
# to run this workflow manually from the Actions tab
|
7 |
+
workflow_dispatch:
|
8 |
+
|
9 |
+
jobs:
|
10 |
+
sync-to-hub:
|
11 |
+
runs-on: ubuntu-latest
|
12 |
+
steps:
|
13 |
+
- uses: actions/checkout@v3
|
14 |
+
with:
|
15 |
+
fetch-depth: 0
|
16 |
+
lfs: true
|
17 |
+
- name: Push to hub
|
18 |
+
env:
|
19 |
+
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
20 |
+
run: git push https://sabazo:[email protected]/spaces/sabazo/insurance-advisor-agents main
|
README.md
CHANGED
@@ -1,3 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# Insurance Advisor Agent(s)
|
2 |
|
3 |
Setup a modular, multi-agent system to handle inqueries to an insurance company. The system utilizes different approachs to find reliable answers regarding the insurance products
|
|
|
1 |
+
---
|
2 |
+
title: Insurance Advisor Agents PoC
|
3 |
+
emoji: 🤖
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: indigo
|
6 |
+
sdk: docker
|
7 |
+
python: 3.11
|
8 |
+
app_file: app_gui.py
|
9 |
+
pinned: false
|
10 |
+
---
|
11 |
+
|
12 |
+
|
13 |
# Insurance Advisor Agent(s)
|
14 |
|
15 |
Setup a modular, multi-agent system to handle inqueries to an insurance company. The system utilizes different approachs to find reliable answers regarding the insurance products
|
app_gui.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
# Import Gradio for UI, along with other necessary libraries
|
2 |
import gradio as gr
|
3 |
from fastapi import FastAPI
|
|
|
4 |
from rag_app.agents.react_agent import agent_executor, llm
|
5 |
from rag_app.chains import user_response_sentiment_prompt
|
6 |
from typing import Dict
|
@@ -10,27 +11,36 @@ from rag_app.loading_data.load_S3_vector_stores import get_chroma_vs
|
|
10 |
from rag_app.agents.react_agent import agent_executor
|
11 |
# need to import the qa!
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
app = FastAPI()
|
17 |
get_chroma_vs()
|
18 |
-
user_sentiment_chain = user_response_sentiment_prompt | llm
|
19 |
-
# data = user_sentiment_chain.invoke({"user_reponse":"thanks for the help"})
|
20 |
-
data = user_sentiment_chain.invoke({"user_reponse":"OMG I AM SO LOST!!! HELP!!!"})
|
21 |
-
responses = extract_responses(data)
|
22 |
-
if responses['AI'] == "1":
|
23 |
-
print("GG")
|
24 |
|
25 |
if __name__ == "__main__":
|
26 |
|
27 |
# Function to add a new input to the chat history
|
|
|
|
|
|
|
|
|
|
|
28 |
def add_text(history, text):
|
29 |
# Append the new text to the history with a placeholder for the response
|
30 |
history = history + [(text, None)]
|
31 |
return history, ""
|
32 |
|
33 |
# Function representing the bot's response mechanism
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
def bot(history):
|
35 |
# Obtain the response from the 'infer' function using the latest input
|
36 |
response = infer(history[-1][0], history)
|
@@ -38,6 +48,26 @@ if __name__ == "__main__":
|
|
38 |
return history
|
39 |
|
40 |
# Function to infer the response using the RAG model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
def infer(question, history):
|
42 |
# Use the question and history to query the RAG model
|
43 |
#result = qa({"query": question, "history": history, "question": question})
|
@@ -72,7 +102,17 @@ if __name__ == "__main__":
|
|
72 |
css = """
|
73 |
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
74 |
"""
|
|
|
|
|
|
|
|
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
# HTML content for the Gradio interface title
|
77 |
title = """
|
78 |
<div style="text-align:left;">
|
@@ -80,6 +120,17 @@ if __name__ == "__main__":
|
|
80 |
</div>
|
81 |
"""
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
# Building the Gradio interface
|
84 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
85 |
with gr.Column(elem_id="col-container"):
|
@@ -95,6 +146,9 @@ if __name__ == "__main__":
|
|
95 |
# Create a row for the question input
|
96 |
with gr.Row():
|
97 |
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
|
|
|
|
|
|
|
98 |
|
99 |
# Define the action when the question is submitted
|
100 |
question.submit(add_text, [chatbot, question], [chatbot, question], queue=False).then(
|
@@ -102,6 +156,16 @@ if __name__ == "__main__":
|
|
102 |
)
|
103 |
# Define the action for the clear button
|
104 |
clear.click(lambda: None, None, chatbot, queue=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
|
|
106 |
# Launch the Gradio demo interface
|
107 |
demo.queue().launch(share=False, debug=True)
|
|
|
1 |
# Import Gradio for UI, along with other necessary libraries
|
2 |
import gradio as gr
|
3 |
from fastapi import FastAPI
|
4 |
+
from fastapi import FastAPI
|
5 |
from rag_app.agents.react_agent import agent_executor, llm
|
6 |
from rag_app.chains import user_response_sentiment_prompt
|
7 |
from typing import Dict
|
|
|
11 |
from rag_app.agents.react_agent import agent_executor
|
12 |
# need to import the qa!
|
13 |
|
|
|
|
|
|
|
14 |
app = FastAPI()
|
15 |
get_chroma_vs()
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
if __name__ == "__main__":
|
18 |
|
19 |
# Function to add a new input to the chat history
|
20 |
+
def add_text(history, text):
|
21 |
+
# Append the new text to the history with a placeholder for the response
|
22 |
+
history = history + [(text, None)]
|
23 |
+
return history, ""
|
24 |
+
# Function to add a new input to the chat history
|
25 |
def add_text(history, text):
|
26 |
# Append the new text to the history with a placeholder for the response
|
27 |
history = history + [(text, None)]
|
28 |
return history, ""
|
29 |
|
30 |
# Function representing the bot's response mechanism
|
31 |
+
def bot(history):
|
32 |
+
# Obtain the response from the 'infer' function using the latest input
|
33 |
+
response = infer(history[-1][0], history)
|
34 |
+
#sources = [doc.metadata.get("source") for doc in response['source_documents']]
|
35 |
+
#src_list = '\n'.join(sources)
|
36 |
+
#print_this = response['result'] + "\n\n\n Sources: \n\n\n" + src_list
|
37 |
+
|
38 |
+
|
39 |
+
#history[-1][1] = print_this #response['answer']
|
40 |
+
# Update the history with the bot's response
|
41 |
+
history[-1][1] = response['output']
|
42 |
+
return history
|
43 |
+
# Function representing the bot's response mechanism
|
44 |
def bot(history):
|
45 |
# Obtain the response from the 'infer' function using the latest input
|
46 |
response = infer(history[-1][0], history)
|
|
|
48 |
return history
|
49 |
|
50 |
# Function to infer the response using the RAG model
|
51 |
+
def infer(question, history):
|
52 |
+
# Use the question and history to query the RAG model
|
53 |
+
#result = qa({"query": question, "history": history, "question": question})
|
54 |
+
try:
|
55 |
+
result = agent_executor.invoke(
|
56 |
+
{
|
57 |
+
"input": question,
|
58 |
+
"chat_history": history
|
59 |
+
}
|
60 |
+
)
|
61 |
+
return result
|
62 |
+
except Exception:
|
63 |
+
raise gr.Error("Model is Overloaded, Please retry later!")
|
64 |
+
|
65 |
+
def vote(data: gr.LikeData):
|
66 |
+
if data.liked:
|
67 |
+
print("You upvoted this response: " + data.value)
|
68 |
+
else:
|
69 |
+
print("You downvoted this response: " + data.value)
|
70 |
+
# Function to infer the response using the RAG model
|
71 |
def infer(question, history):
|
72 |
# Use the question and history to query the RAG model
|
73 |
#result = qa({"query": question, "history": history, "question": question})
|
|
|
102 |
css = """
|
103 |
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
104 |
"""
|
105 |
+
# CSS styling for the Gradio interface
|
106 |
+
css = """
|
107 |
+
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
108 |
+
"""
|
109 |
|
110 |
+
# HTML content for the Gradio interface title
|
111 |
+
title = """
|
112 |
+
<div style="text-align:left;">
|
113 |
+
<p>Hello, I BotTina 2.0, your intelligent AI assistant. I can help you explore Wuerttembergische Versicherungs products.<br />
|
114 |
+
</div>
|
115 |
+
"""
|
116 |
# HTML content for the Gradio interface title
|
117 |
title = """
|
118 |
<div style="text-align:left;">
|
|
|
120 |
</div>
|
121 |
"""
|
122 |
|
123 |
+
# Building the Gradio interface
|
124 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
125 |
+
with gr.Column(elem_id="col-container"):
|
126 |
+
gr.HTML(title) # Add the HTML title to the interface
|
127 |
+
chatbot = gr.Chatbot([], elem_id="chatbot",
|
128 |
+
label="BotTina 2.0",
|
129 |
+
bubble_full_width=False,
|
130 |
+
avatar_images=(None, "https://dacodi-production.s3.amazonaws.com/store/87bc00b6727589462954f2e3ff6f531c.png"),
|
131 |
+
height=680,) # Initialize the chatbot component
|
132 |
+
chatbot.like(vote, None, None)
|
133 |
+
clear = gr.Button("Clear") # Add a button to clear the chat
|
134 |
# Building the Gradio interface
|
135 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
136 |
with gr.Column(elem_id="col-container"):
|
|
|
146 |
# Create a row for the question input
|
147 |
with gr.Row():
|
148 |
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
|
149 |
+
# Create a row for the question input
|
150 |
+
with gr.Row():
|
151 |
+
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
|
152 |
|
153 |
# Define the action when the question is submitted
|
154 |
question.submit(add_text, [chatbot, question], [chatbot, question], queue=False).then(
|
|
|
156 |
)
|
157 |
# Define the action for the clear button
|
158 |
clear.click(lambda: None, None, chatbot, queue=False)
|
159 |
+
# Define the action when the question is submitted
|
160 |
+
question.submit(add_text, [chatbot, question], [chatbot, question], queue=False).then(
|
161 |
+
bot, chatbot, chatbot
|
162 |
+
)
|
163 |
+
# Define the action for the clear button
|
164 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
165 |
+
|
166 |
+
# Launch the Gradio demo interface
|
167 |
+
demo.queue().launch(share=False, debug=True)
|
168 |
|
169 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
170 |
# Launch the Gradio demo interface
|
171 |
demo.queue().launch(share=False, debug=True)
|
rag_app/chains/__init__.py
CHANGED
@@ -1 +1,2 @@
|
|
1 |
-
# from rag_app.chains.s
|
|
|
|
1 |
+
# from rag_app.chains.s
|
2 |
+
from rag_app.chains.user_response_sentiment_chain import user_response_sentiment_prompt
|
rag_app/hybrid_search.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
from langchain_community.vectorstores import FAISS
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import os
|
5 |
+
from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
|
6 |
+
from langchain.retrievers import EnsembleRetriever
|
7 |
+
from langchain_community.retrievers import BM25Retriever
|
8 |
+
|
9 |
+
|
10 |
+
def get_hybrid_search_results(query:str,
|
11 |
+
path_to_db:str,
|
12 |
+
embedding_model:str,
|
13 |
+
hf_api_key:str,
|
14 |
+
num_docs:int=5) -> list:
|
15 |
+
""" Uses an ensemble retriever of BM25 and FAISS to return k num documents
|
16 |
+
|
17 |
+
Args:
|
18 |
+
query (str): The search query
|
19 |
+
path_to_db (str): Path to the vectorstore database
|
20 |
+
embedding_model (str): Embedding model used in the vector store
|
21 |
+
num_docs (int): Number of documents to return
|
22 |
+
|
23 |
+
Returns
|
24 |
+
List of documents
|
25 |
+
|
26 |
+
"""
|
27 |
+
|
28 |
+
embeddings = HuggingFaceInferenceAPIEmbeddings(api_key=hf_api_key,
|
29 |
+
model_name=embedding_model)
|
30 |
+
# Load the vectorstore database
|
31 |
+
db = FAISS.load_local(folder_path=path_to_db,
|
32 |
+
embeddings=embeddings,
|
33 |
+
allow_dangerous_deserialization=True)
|
34 |
+
|
35 |
+
all_docs = db.similarity_search("", k=db.index.ntotal)
|
36 |
+
|
37 |
+
bm25_retriever = BM25Retriever.from_documents(all_docs)
|
38 |
+
bm25_retriever.k = num_docs # How many results you want
|
39 |
+
|
40 |
+
faiss_retriever = db.as_retriever(search_kwargs={'k': num_docs})
|
41 |
+
|
42 |
+
ensemble_retriever = EnsembleRetriever(retrievers=[bm25_retriever, faiss_retriever],
|
43 |
+
weights=[0.5,0.5])
|
44 |
+
|
45 |
+
results = ensemble_retriever.invoke(input=query)
|
46 |
+
return results
|
47 |
+
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
query = "Haustierversicherung"
|
51 |
+
HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
|
52 |
+
EMBEDDING_MODEL = os.getenv("EMBEDDING_MODEL")
|
53 |
+
|
54 |
+
path_to_vector_db = Path("..")/'vectorstore/faiss-insurance-agent-500'
|
55 |
+
|
56 |
+
results = get_hybrid_search_results(query=query,
|
57 |
+
path_to_db=path_to_vector_db,
|
58 |
+
embedding_model=EMBEDDING_MODEL,
|
59 |
+
hf_api_key=HUGGINGFACEHUB_API_TOKEN)
|
60 |
+
|
61 |
+
for doc in results:
|
62 |
+
print(doc)
|
63 |
+
print()
|