Spaces:
Sleeping
Sleeping
Asaad Almutareb
commited on
Commit
·
42f834a
1
Parent(s):
ddfe3b8
added human_as_tool
Browse filesedited prompt to include overall goal of getting email address
added a simple try/catch for the infer function
- app_gui.py +10 -7
- example.env +14 -12
- rag_app/agents/__init__.py +0 -0
- rag_app/agents/kb_retriever_agent.py +1 -3
- rag_app/agents/react_agent.py +4 -2
- rag_app/database/__init__.py +0 -0
- rag_app/knowledge_base/__init__.py +0 -0
- rag_app/structured_tools/agent_tools.py +20 -3
- rag_app/structured_tools/structured_tools.py +9 -8
- rag_app/templates/react_json_with_memory_ger.py +5 -4
app_gui.py
CHANGED
@@ -27,13 +27,16 @@ def bot(history):
|
|
27 |
def infer(question, history):
|
28 |
# Use the question and history to query the RAG model
|
29 |
#result = qa({"query": question, "history": history, "question": question})
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
37 |
|
38 |
# CSS styling for the Gradio interface
|
39 |
css = """
|
|
|
27 |
def infer(question, history):
|
28 |
# Use the question and history to query the RAG model
|
29 |
#result = qa({"query": question, "history": history, "question": question})
|
30 |
+
try:
|
31 |
+
result = agent_executor.invoke(
|
32 |
+
{
|
33 |
+
"input": question,
|
34 |
+
"chat_history": history
|
35 |
+
}
|
36 |
+
)
|
37 |
+
return result
|
38 |
+
except Exception:
|
39 |
+
raise gr.Error("Model is Overloaded, Please retry later!")
|
40 |
|
41 |
# CSS styling for the Gradio interface
|
42 |
css = """
|
example.env
CHANGED
@@ -5,23 +5,25 @@ GOOGLE_API_KEY=""
|
|
5 |
|
6 |
# Vectorstore storage on S3 and locally
|
7 |
S3_LOCATION="rad-rag-demos"
|
8 |
-
#faiss-insurance-agent-mpnet-1500.zip
|
9 |
-
FAISS_VS_NAME="vectorstores/faiss-insurance-agent-MiniLM-1500.zip"
|
10 |
-
|
11 |
-
CHROMA_VS_NAME="
|
12 |
-
|
13 |
-
|
|
|
|
|
14 |
|
15 |
# for chromadb
|
16 |
-
VECTOR_DATABASE_LOCATION="./vectorstore/chroma-insurance-agent-
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
|
21 |
# llm and embedding models
|
22 |
#EMBEDDING_MODEL="sentence-transformers/multi-qa-mpnet-base-dot-v1"
|
23 |
-
EMBEDDING_MODEL="sentence-transformers/distiluse-base-multilingual-cased-v2"
|
24 |
-
#
|
|
|
25 |
LLM_MODEL="mistralai/Mixtral-8x7B-Instruct-v0.1"
|
26 |
LLM_MODEL_ARGS=
|
27 |
-
|
|
|
5 |
|
6 |
# Vectorstore storage on S3 and locally
|
7 |
S3_LOCATION="rad-rag-demos"
|
8 |
+
#FAISS_VS_NAME="vectorstores/faiss-insurance-agent-mpnet-1500.zip"
|
9 |
+
#FAISS_VS_NAME="vectorstores/faiss-insurance-agent-MiniLM-L12-1500.zip"
|
10 |
+
FAISS_VS_NAME="vectorstores/faiss-insurance-agent-multilingual-cased-1500.zip"
|
11 |
+
CHROMA_VS_NAME="vectorstores/chroma-insurance-agent-multilingual-cased-1500.zip"
|
12 |
+
#CHROMA_VS_NAME="vectorstores/chroma-insurance-agent-mpnet-1500.zip"
|
13 |
+
#CHROMA_VS_NAME="vectorstore/chroma-insurance-agent-MiniLM-L12-1500.zip"
|
14 |
+
FAISS_INDEX_PATH = "./vectorstore/faiss-insurance-agent-multilingual-cased-1500"
|
15 |
+
CHROMA_DIRECTORY = "./vectorstore/chroma-insurance-agent-multilingual-cased-500"
|
16 |
|
17 |
# for chromadb
|
18 |
+
VECTOR_DATABASE_LOCATION="./vectorstore/chroma-insurance-agent-multilingual-cased-500"
|
19 |
|
20 |
+
# for storing search results
|
21 |
+
SOURCES_CACHE = 'rag_app/database/source_cache.sqlite3'
|
22 |
|
23 |
# llm and embedding models
|
24 |
#EMBEDDING_MODEL="sentence-transformers/multi-qa-mpnet-base-dot-v1"
|
25 |
+
EMBEDDING_MODEL="sentence-transformers/distiluse-base-multilingual-cased-v2"
|
26 |
+
#384 dims
|
27 |
+
#EMBEDDING_MODEL="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
|
28 |
LLM_MODEL="mistralai/Mixtral-8x7B-Instruct-v0.1"
|
29 |
LLM_MODEL_ARGS=
|
|
rag_app/agents/__init__.py
ADDED
File without changes
|
rag_app/agents/kb_retriever_agent.py
CHANGED
@@ -13,9 +13,7 @@ from rag_app.structured_tools.structured_tools import (
|
|
13 |
|
14 |
from langchain.prompts import PromptTemplate
|
15 |
from rag_app.templates.react_json_ger import template_system
|
16 |
-
# from
|
17 |
-
# from langchain.globals import set_llm_cache
|
18 |
-
# from langchain.cache import SQLiteCache
|
19 |
|
20 |
# set_llm_cache(SQLiteCache(database_path=".cache.db"))
|
21 |
# logger = logger.get_console_logger("hf_mixtral_agent")
|
|
|
13 |
|
14 |
from langchain.prompts import PromptTemplate
|
15 |
from rag_app.templates.react_json_ger import template_system
|
16 |
+
# from rag_app.utils import logger
|
|
|
|
|
17 |
|
18 |
# set_llm_cache(SQLiteCache(database_path=".cache.db"))
|
19 |
# logger = logger.get_console_logger("hf_mixtral_agent")
|
rag_app/agents/react_agent.py
CHANGED
@@ -8,7 +8,7 @@ from langchain.tools.render import render_text_description
|
|
8 |
import os
|
9 |
from dotenv import load_dotenv
|
10 |
from rag_app.structured_tools.agent_tools import (
|
11 |
-
web_research
|
12 |
)
|
13 |
|
14 |
from langchain.prompts import PromptTemplate
|
@@ -42,7 +42,9 @@ llm = HuggingFaceEndpoint(repo_id=LLM_MODEL,
|
|
42 |
tools = [
|
43 |
#knowledgeBase_search,
|
44 |
#google_search,
|
45 |
-
web_research
|
|
|
|
|
46 |
]
|
47 |
|
48 |
prompt = PromptTemplate.from_template(
|
|
|
8 |
import os
|
9 |
from dotenv import load_dotenv
|
10 |
from rag_app.structured_tools.agent_tools import (
|
11 |
+
web_research, ask_user, get_email
|
12 |
)
|
13 |
|
14 |
from langchain.prompts import PromptTemplate
|
|
|
42 |
tools = [
|
43 |
#knowledgeBase_search,
|
44 |
#google_search,
|
45 |
+
web_research,
|
46 |
+
ask_user,
|
47 |
+
get_email
|
48 |
]
|
49 |
|
50 |
prompt = PromptTemplate.from_template(
|
rag_app/database/__init__.py
ADDED
File without changes
|
rag_app/knowledge_base/__init__.py
ADDED
File without changes
|
rag_app/structured_tools/agent_tools.py
CHANGED
@@ -1,8 +1,11 @@
|
|
1 |
from langchain.tools import BaseTool, StructuredTool, tool
|
|
|
2 |
from rag_app.agents.kb_retriever_agent import agent_worker
|
|
|
|
|
3 |
|
4 |
@tool
|
5 |
-
def web_research(query: str) ->
|
6 |
"""Verbessere die Ergebnisse durch eine Suche über die Webseite der Versicherung. Erstelle eine neue Suchanfrage, um die Erfolgschancen zu verbesseren."""
|
7 |
|
8 |
result = agent_worker.invoke(
|
@@ -10,5 +13,19 @@ def web_research(query: str) -> str:
|
|
10 |
"input": query
|
11 |
}
|
12 |
)
|
13 |
-
print(result)
|
14 |
-
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from langchain.tools import BaseTool, StructuredTool, tool
|
2 |
+
from langchain_community.tools import HumanInputRun
|
3 |
from rag_app.agents.kb_retriever_agent import agent_worker
|
4 |
+
from operator import itemgetter
|
5 |
+
from typing import Dict, List
|
6 |
|
7 |
@tool
|
8 |
+
def web_research(query: str) -> List[dict]:
|
9 |
"""Verbessere die Ergebnisse durch eine Suche über die Webseite der Versicherung. Erstelle eine neue Suchanfrage, um die Erfolgschancen zu verbesseren."""
|
10 |
|
11 |
result = agent_worker.invoke(
|
|
|
13 |
"input": query
|
14 |
}
|
15 |
)
|
16 |
+
#print(result)
|
17 |
+
return result
|
18 |
+
|
19 |
+
@tool
|
20 |
+
def ask_user(query: str) -> str:
|
21 |
+
"""Frage den Benutzer direkt wenn du nicht sicher bist was er meint oder du eine Entscheidung brauchst."""
|
22 |
+
|
23 |
+
result = HumanInputRun.invoke(query)
|
24 |
+
return result
|
25 |
+
|
26 |
+
@tool
|
27 |
+
def get_email(query: str) -> str:
|
28 |
+
"""Frage den Benutzer nach seiner EMail Adresse, wenn du denkst du hast seine Anfrage beantwortet hast, damit wir ihm mehr Informationen im Anschluss zu senden kannst."""
|
29 |
+
|
30 |
+
result = HumanInputRun.invoke(query)
|
31 |
+
return result
|
rag_app/structured_tools/structured_tools.py
CHANGED
@@ -22,6 +22,7 @@ import os
|
|
22 |
# from innovation_pathfinder_ai.utils import create_wikipedia_urls_from_text
|
23 |
|
24 |
persist_directory = os.getenv('VECTOR_DATABASE_LOCATION')
|
|
|
25 |
|
26 |
@tool
|
27 |
def memory_search(query:str) -> str:
|
@@ -36,7 +37,7 @@ def memory_search(query:str) -> str:
|
|
36 |
#store using envar
|
37 |
|
38 |
embedding_function = SentenceTransformerEmbeddings(
|
39 |
-
model_name=
|
40 |
)
|
41 |
|
42 |
vector_db = Chroma(
|
@@ -62,7 +63,7 @@ def knowledgeBase_search(query:str) -> str:
|
|
62 |
#store using envar
|
63 |
|
64 |
embedding_function = SentenceTransformerEmbeddings(
|
65 |
-
model_name=
|
66 |
)
|
67 |
|
68 |
# vector_db = Chroma(
|
@@ -87,11 +88,11 @@ def google_search(query: str) -> str:
|
|
87 |
websearch = GoogleSearchAPIWrapper()
|
88 |
search_results:dict = websearch.results(query, 3)
|
89 |
print(search_results)
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
|
97 |
return cleaner_sources.__str__()
|
|
|
22 |
# from innovation_pathfinder_ai.utils import create_wikipedia_urls_from_text
|
23 |
|
24 |
persist_directory = os.getenv('VECTOR_DATABASE_LOCATION')
|
25 |
+
embedding_model = os.getenv("EMBEDDING_MODEL")
|
26 |
|
27 |
@tool
|
28 |
def memory_search(query:str) -> str:
|
|
|
37 |
#store using envar
|
38 |
|
39 |
embedding_function = SentenceTransformerEmbeddings(
|
40 |
+
model_name=embedding_model,
|
41 |
)
|
42 |
|
43 |
vector_db = Chroma(
|
|
|
63 |
#store using envar
|
64 |
|
65 |
embedding_function = SentenceTransformerEmbeddings(
|
66 |
+
model_name=embedding_model
|
67 |
)
|
68 |
|
69 |
# vector_db = Chroma(
|
|
|
88 |
websearch = GoogleSearchAPIWrapper()
|
89 |
search_results:dict = websearch.results(query, 3)
|
90 |
print(search_results)
|
91 |
+
if len(search_results)>1:
|
92 |
+
cleaner_sources =format_search_results(search_results)
|
93 |
+
parsed_csources = parse_list_to_dicts(cleaner_sources)
|
94 |
+
add_many(parsed_csources)
|
95 |
+
else:
|
96 |
+
cleaner_sources = search_results
|
97 |
|
98 |
return cleaner_sources.__str__()
|
rag_app/templates/react_json_with_memory_ger.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
template_system = """
|
2 |
-
|
3 |
-
|
4 |
-
|
|
|
5 |
|
6 |
<TOOLS>
|
7 |
{tools}
|
@@ -35,7 +36,7 @@ Beobachtung: das Ergebnis der Aktion
|
|
35 |
Gedanke: Ich kenne jetzt die endgültige Antwort
|
36 |
Final Answer: die endgültige Antwort auf die ursprüngliche Eingabefrage
|
37 |
|
38 |
-
|
39 |
|
40 |
Vorheriger Gesprächsverlauf:
|
41 |
<CONVERSATION_HISTORY>
|
|
|
1 |
template_system = """
|
2 |
+
Du bist ein freundlicher Versicherungsproduktberater. Deine Aufgabe ist es, Kunden dabei zu helfen, die besten Produkte der Württembergische GmbH zu finden\
|
3 |
+
und ihnen mehr informationen dazu per Email zusenden, wenn du seine Fragen beanwortest hast.\
|
4 |
+
Hilfe dem Benutzer, Antworten auf seine Fragen zu finden. Antworte kurz und einfach und biete an, dem Benutzer das Produkt und die Bedingungen zu erklären.\
|
5 |
+
Beantworte die folgenden Fragen so gut du kannst. Du hast Zugriff auf die folgenden Tools:
|
6 |
|
7 |
<TOOLS>
|
8 |
{tools}
|
|
|
36 |
Gedanke: Ich kenne jetzt die endgültige Antwort
|
37 |
Final Answer: die endgültige Antwort auf die ursprüngliche Eingabefrage
|
38 |
|
39 |
+
Beginne! Denke daran, beim Antworten immer die genauen Zeichen `Final Answer` zu verwenden.
|
40 |
|
41 |
Vorheriger Gesprächsverlauf:
|
42 |
<CONVERSATION_HISTORY>
|