Spaces:
Build error
Build error
start implementing csv agent
Browse files- .gitignore +1 -0
- src/chatbot_csv.py +10 -3
- src/csv_agent.py +19 -0
- src/modules/chatbot.py +4 -7
- src/modules/layout.py +4 -1
- src/modules/sidebar.py +5 -2
.gitignore
CHANGED
|
@@ -154,3 +154,4 @@ cython_debug/
|
|
| 154 |
#venv
|
| 155 |
|
| 156 |
*.pkl
|
|
|
|
|
|
| 154 |
#venv
|
| 155 |
|
| 156 |
*.pkl
|
| 157 |
+
*.csv
|
src/chatbot_csv.py
CHANGED
|
@@ -3,12 +3,11 @@ import streamlit as st
|
|
| 3 |
import asyncio
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
-
|
| 7 |
from modules.history import ChatHistory
|
| 8 |
from modules.layout import Layout
|
| 9 |
from modules.utils import Utilities
|
| 10 |
from modules.sidebar import Sidebar
|
| 11 |
-
|
| 12 |
|
| 13 |
def init():
|
| 14 |
load_dotenv()
|
|
@@ -16,6 +15,7 @@ def init():
|
|
| 16 |
|
| 17 |
|
| 18 |
async def main():
|
|
|
|
| 19 |
init()
|
| 20 |
layout, sidebar, utils = Layout(), Sidebar(), Utilities()
|
| 21 |
layout.show_header()
|
|
@@ -31,6 +31,8 @@ async def main():
|
|
| 31 |
history = ChatHistory()
|
| 32 |
sidebar.show_options()
|
| 33 |
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
chatbot = await utils.setup_chatbot(
|
| 36 |
uploaded_file, st.session_state["model"], st.session_state["temperature"]
|
|
@@ -56,9 +58,14 @@ async def main():
|
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
st.error(f"Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
sidebar.about()
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 64 |
asyncio.run(main())
|
|
|
|
| 3 |
import asyncio
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
|
|
|
| 6 |
from modules.history import ChatHistory
|
| 7 |
from modules.layout import Layout
|
| 8 |
from modules.utils import Utilities
|
| 9 |
from modules.sidebar import Sidebar
|
| 10 |
+
from csv_agent import extra_chatbot_page
|
| 11 |
|
| 12 |
def init():
|
| 13 |
load_dotenv()
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
async def main():
|
| 18 |
+
|
| 19 |
init()
|
| 20 |
layout, sidebar, utils = Layout(), Sidebar(), Utilities()
|
| 21 |
layout.show_header()
|
|
|
|
| 31 |
history = ChatHistory()
|
| 32 |
sidebar.show_options()
|
| 33 |
|
| 34 |
+
|
| 35 |
+
|
| 36 |
try:
|
| 37 |
chatbot = await utils.setup_chatbot(
|
| 38 |
uploaded_file, st.session_state["model"], st.session_state["temperature"]
|
|
|
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
st.error(f"Error: {str(e)}")
|
| 61 |
+
# Ajout du bouton pour naviguer vers la page du chatbot supplémentaire
|
| 62 |
+
if st.sidebar.button("Aller au chatbot supplémentaire"):
|
| 63 |
+
st.session_state["extra_chatbot"] = True
|
| 64 |
+
extra_chatbot_page()
|
| 65 |
|
| 66 |
sidebar.about()
|
| 67 |
|
|
|
|
| 68 |
if __name__ == "__main__":
|
| 69 |
+
if "page" not in st.session_state:
|
| 70 |
+
st.session_state.page = "main"
|
| 71 |
asyncio.run(main())
|
src/csv_agent.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.agents import create_csv_agent
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
from langchain.llms import OpenAI
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def extra_chatbot_page():
|
| 8 |
+
|
| 9 |
+
query = st.text_input(label="Use CSV agent for precise informations about the csv file itself")
|
| 10 |
+
|
| 11 |
+
if query :
|
| 12 |
+
agent = create_csv_agent(OpenAI(temperature=0), 'poto-associations-sample.csv', verbose=True)
|
| 13 |
+
if agent :
|
| 14 |
+
st.write(agent.run(query))
|
| 15 |
+
|
| 16 |
+
if __name__ == "__main__":
|
| 17 |
+
extra_chatbot_page()
|
| 18 |
+
|
| 19 |
+
|
src/modules/chatbot.py
CHANGED
|
@@ -5,8 +5,7 @@ from langchain.prompts.prompt import PromptTemplate
|
|
| 5 |
|
| 6 |
|
| 7 |
class Chatbot:
|
| 8 |
-
_template = """Given the following conversation and a follow-up question, rephrase the follow-up question to be a
|
| 9 |
-
You can assume that the question is about the information in a CSV file.
|
| 10 |
Chat History:
|
| 11 |
{chat_history}
|
| 12 |
Follow-up entry: {question}
|
|
@@ -14,13 +13,12 @@ class Chatbot:
|
|
| 14 |
|
| 15 |
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
|
| 16 |
|
| 17 |
-
qa_template = """"You are an AI conversational assistant to answer questions based on
|
| 18 |
You are given data from a csv file and a question, you must help the user find the information they need.
|
| 19 |
-
|
| 20 |
-
Your answers should be short,friendly, in the same language.
|
| 21 |
question: {question}
|
| 22 |
=========
|
| 23 |
-
{context}
|
| 24 |
=======
|
| 25 |
"""
|
| 26 |
|
|
@@ -35,7 +33,6 @@ class Chatbot:
|
|
| 35 |
"""
|
| 36 |
Starts a conversational chat with a model via Langchain
|
| 37 |
"""
|
| 38 |
-
|
| 39 |
chain = ConversationalRetrievalChain.from_llm(
|
| 40 |
llm=ChatOpenAI(model_name=self.model_name, temperature=self.temperature),
|
| 41 |
condense_question_prompt=self.CONDENSE_QUESTION_PROMPT,
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
class Chatbot:
|
| 8 |
+
_template = """Given the following conversation and a follow-up question, rephrase the follow-up question to be a standalone question.
|
|
|
|
| 9 |
Chat History:
|
| 10 |
{chat_history}
|
| 11 |
Follow-up entry: {question}
|
|
|
|
| 13 |
|
| 14 |
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
|
| 15 |
|
| 16 |
+
qa_template = """"You are an AI conversational assistant to answer questions based on a context.
|
| 17 |
You are given data from a csv file and a question, you must help the user find the information they need.
|
| 18 |
+
Your answers should be friendly, in the same language.
|
|
|
|
| 19 |
question: {question}
|
| 20 |
=========
|
| 21 |
+
context: {context}
|
| 22 |
=======
|
| 23 |
"""
|
| 24 |
|
|
|
|
| 33 |
"""
|
| 34 |
Starts a conversational chat with a model via Langchain
|
| 35 |
"""
|
|
|
|
| 36 |
chain = ConversationalRetrievalChain.from_llm(
|
| 37 |
llm=ChatOpenAI(model_name=self.model_name, temperature=self.temperature),
|
| 38 |
condense_question_prompt=self.CONDENSE_QUESTION_PROMPT,
|
src/modules/layout.py
CHANGED
|
@@ -2,13 +2,14 @@ import streamlit as st
|
|
| 2 |
|
| 3 |
|
| 4 |
class Layout:
|
|
|
|
| 5 |
def show_header(self):
|
| 6 |
"""
|
| 7 |
Displays the header of the app
|
| 8 |
"""
|
| 9 |
st.markdown(
|
| 10 |
"""
|
| 11 |
-
<h1 style='text-align: center;'>ChatBot-CSV, Talk with your
|
| 12 |
""",
|
| 13 |
unsafe_allow_html=True,
|
| 14 |
)
|
|
@@ -38,5 +39,7 @@ class Layout:
|
|
| 38 |
label_visibility="collapsed",
|
| 39 |
)
|
| 40 |
submit_button = st.form_submit_button(label="Send")
|
|
|
|
| 41 |
is_ready = submit_button and user_input
|
| 42 |
return is_ready, user_input
|
|
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
class Layout:
|
| 5 |
+
|
| 6 |
def show_header(self):
|
| 7 |
"""
|
| 8 |
Displays the header of the app
|
| 9 |
"""
|
| 10 |
st.markdown(
|
| 11 |
"""
|
| 12 |
+
<h1 style='text-align: center;'>ChatBot-CSV, Talk with the content of your csv data! 💬</h1>
|
| 13 |
""",
|
| 14 |
unsafe_allow_html=True,
|
| 15 |
)
|
|
|
|
| 39 |
label_visibility="collapsed",
|
| 40 |
)
|
| 41 |
submit_button = st.form_submit_button(label="Send")
|
| 42 |
+
|
| 43 |
is_ready = submit_button and user_input
|
| 44 |
return is_ready, user_input
|
| 45 |
+
|
src/modules/sidebar.py
CHANGED
|
@@ -2,7 +2,7 @@ import streamlit as st
|
|
| 2 |
|
| 3 |
|
| 4 |
class Sidebar:
|
| 5 |
-
MODEL_OPTIONS = ["gpt-3.5-turbo"]
|
| 6 |
TEMPERATURE_MIN_VALUE = 0.0
|
| 7 |
TEMPERATURE_MAX_VALUE = 1.0
|
| 8 |
TEMPERATURE_DEFAULT_VALUE = 0.0
|
|
@@ -39,11 +39,14 @@ class Sidebar:
|
|
| 39 |
step=self.TEMPERATURE_STEP,
|
| 40 |
)
|
| 41 |
st.session_state["temperature"] = temperature
|
| 42 |
-
|
| 43 |
def show_options(self):
|
| 44 |
with st.sidebar.expander("🛠️ Settings", expanded=False):
|
|
|
|
| 45 |
self.reset_chat_button()
|
| 46 |
self.model_selector()
|
| 47 |
self.temperature_slider()
|
| 48 |
st.session_state.setdefault("model", self.MODEL_OPTIONS[0])
|
| 49 |
st.session_state.setdefault("temperature", self.TEMPERATURE_DEFAULT_VALUE)
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
class Sidebar:
|
| 5 |
+
MODEL_OPTIONS = ["gpt-3.5-turbo", "gpt-4"]
|
| 6 |
TEMPERATURE_MIN_VALUE = 0.0
|
| 7 |
TEMPERATURE_MAX_VALUE = 1.0
|
| 8 |
TEMPERATURE_DEFAULT_VALUE = 0.0
|
|
|
|
| 39 |
step=self.TEMPERATURE_STEP,
|
| 40 |
)
|
| 41 |
st.session_state["temperature"] = temperature
|
| 42 |
+
|
| 43 |
def show_options(self):
|
| 44 |
with st.sidebar.expander("🛠️ Settings", expanded=False):
|
| 45 |
+
|
| 46 |
self.reset_chat_button()
|
| 47 |
self.model_selector()
|
| 48 |
self.temperature_slider()
|
| 49 |
st.session_state.setdefault("model", self.MODEL_OPTIONS[0])
|
| 50 |
st.session_state.setdefault("temperature", self.TEMPERATURE_DEFAULT_VALUE)
|
| 51 |
+
|
| 52 |
+
|