Spaces:
Sleeping
Sleeping
Asaad Almutareb
commited on
Commit
•
ddfe3b8
1
Parent(s):
3b643b9
added german prompt templates
Browse filesfixed more depreciation warnings
- app_gui.py +4 -4
- core-langchain-rag.py +1 -1
- rag_app/agents/kb_retriever_agent.py +3 -2
- rag_app/agents/react_agent.py +3 -2
- rag_app/knowledge_base/build_vector_store.py +1 -0
- rag_app/loading_data/load_S3_vector_stores.py +5 -6
- rag_app/structured_tools/structured_tools.py +16 -16
- rag_app/templates/react_json_ger.py +19 -16
- rag_app/templates/react_json_with_memory.py +2 -0
- rag_app/templates/react_json_with_memory_ger.py +50 -0
app_gui.py
CHANGED
@@ -20,7 +20,6 @@ def bot(history):
|
|
20 |
|
21 |
#history[-1][1] = print_this #response['answer']
|
22 |
# Update the history with the bot's response
|
23 |
-
print(*response)
|
24 |
history[-1][1] = response['output']
|
25 |
return history
|
26 |
|
@@ -53,9 +52,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
53 |
with gr.Column(elem_id="col-container"):
|
54 |
gr.HTML(title) # Add the HTML title to the interface
|
55 |
chatbot = gr.Chatbot([], elem_id="chatbot",
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
59 |
clear = gr.Button("Clear") # Add a button to clear the chat
|
60 |
|
61 |
# Create a row for the question input
|
|
|
20 |
|
21 |
#history[-1][1] = print_this #response['answer']
|
22 |
# Update the history with the bot's response
|
|
|
23 |
history[-1][1] = response['output']
|
24 |
return history
|
25 |
|
|
|
52 |
with gr.Column(elem_id="col-container"):
|
53 |
gr.HTML(title) # Add the HTML title to the interface
|
54 |
chatbot = gr.Chatbot([], elem_id="chatbot",
|
55 |
+
label="BotTina 2.0",
|
56 |
+
bubble_full_width=False,
|
57 |
+
avatar_images=(None, "https://dacodi-production.s3.amazonaws.com/store/87bc00b6727589462954f2e3ff6f531c.png"),
|
58 |
+
height=680,) # Initialize the chatbot component
|
59 |
clear = gr.Button("Clear") # Add a button to clear the chat
|
60 |
|
61 |
# Create a row for the question input
|
core-langchain-rag.py
CHANGED
@@ -39,7 +39,7 @@ import time
|
|
39 |
# import time
|
40 |
# from langchain_community.vectorstores import FAISS
|
41 |
# from langchain.vectorstores.utils import filter_complex_metadata
|
42 |
-
# from
|
43 |
|
44 |
# # Configure the text splitter
|
45 |
# text_splitter = RecursiveCharacterTextSplitter(
|
|
|
39 |
# import time
|
40 |
# from langchain_community.vectorstores import FAISS
|
41 |
# from langchain.vectorstores.utils import filter_complex_metadata
|
42 |
+
# from langchain_huggingface import HuggingFaceEmbeddings
|
43 |
|
44 |
# # Configure the text splitter
|
45 |
# text_splitter = RecursiveCharacterTextSplitter(
|
rag_app/agents/kb_retriever_agent.py
CHANGED
@@ -24,9 +24,10 @@ config = load_dotenv(".env")
|
|
24 |
HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
|
25 |
GOOGLE_CSE_ID = os.getenv('GOOGLE_CSE_ID')
|
26 |
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
|
|
27 |
|
28 |
# Load the model from the Hugging Face Hub
|
29 |
-
llm = HuggingFaceEndpoint(repo_id=
|
30 |
temperature=0.1,
|
31 |
max_new_tokens=1024,
|
32 |
repetition_penalty=1.2,
|
@@ -68,6 +69,6 @@ agent_worker = AgentExecutor(
|
|
68 |
verbose=True,
|
69 |
max_iterations=10, # cap number of iterations
|
70 |
#max_execution_time=60, # timout at 60 sec
|
71 |
-
|
72 |
handle_parsing_errors=True,
|
73 |
)
|
|
|
24 |
HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
|
25 |
GOOGLE_CSE_ID = os.getenv('GOOGLE_CSE_ID')
|
26 |
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
27 |
+
LLM_MODEL = os.getenv('LLM_MODEL')
|
28 |
|
29 |
# Load the model from the Hugging Face Hub
|
30 |
+
llm = HuggingFaceEndpoint(repo_id=LLM_MODEL,
|
31 |
temperature=0.1,
|
32 |
max_new_tokens=1024,
|
33 |
repetition_penalty=1.2,
|
|
|
69 |
verbose=True,
|
70 |
max_iterations=10, # cap number of iterations
|
71 |
#max_execution_time=60, # timout at 60 sec
|
72 |
+
return_intermediate_steps=True,
|
73 |
handle_parsing_errors=True,
|
74 |
)
|
rag_app/agents/react_agent.py
CHANGED
@@ -12,7 +12,7 @@ from rag_app.structured_tools.agent_tools import (
|
|
12 |
)
|
13 |
|
14 |
from langchain.prompts import PromptTemplate
|
15 |
-
from rag_app.templates.
|
16 |
# from innovation_pathfinder_ai.utils import logger
|
17 |
# from langchain.globals import set_llm_cache
|
18 |
# from langchain.cache import SQLiteCache
|
@@ -24,13 +24,14 @@ config = load_dotenv(".env")
|
|
24 |
HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
|
25 |
GOOGLE_CSE_ID = os.getenv('GOOGLE_CSE_ID')
|
26 |
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
|
|
27 |
# LANGCHAIN_TRACING_V2 = "true"
|
28 |
# LANGCHAIN_ENDPOINT = "https://api.smith.langchain.com"
|
29 |
# LANGCHAIN_API_KEY = os.getenv('LANGCHAIN_API_KEY')
|
30 |
# LANGCHAIN_PROJECT = os.getenv('LANGCHAIN_PROJECT')
|
31 |
|
32 |
# Load the model from the Hugging Face Hub
|
33 |
-
llm = HuggingFaceEndpoint(repo_id=
|
34 |
temperature=0.1,
|
35 |
max_new_tokens=1024,
|
36 |
repetition_penalty=1.2,
|
|
|
12 |
)
|
13 |
|
14 |
from langchain.prompts import PromptTemplate
|
15 |
+
from rag_app.templates.react_json_with_memory_ger import template_system
|
16 |
# from innovation_pathfinder_ai.utils import logger
|
17 |
# from langchain.globals import set_llm_cache
|
18 |
# from langchain.cache import SQLiteCache
|
|
|
24 |
HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
|
25 |
GOOGLE_CSE_ID = os.getenv('GOOGLE_CSE_ID')
|
26 |
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
27 |
+
LLM_MODEL = os.getenv('LLM_MODEL')
|
28 |
# LANGCHAIN_TRACING_V2 = "true"
|
29 |
# LANGCHAIN_ENDPOINT = "https://api.smith.langchain.com"
|
30 |
# LANGCHAIN_API_KEY = os.getenv('LANGCHAIN_API_KEY')
|
31 |
# LANGCHAIN_PROJECT = os.getenv('LANGCHAIN_PROJECT')
|
32 |
|
33 |
# Load the model from the Hugging Face Hub
|
34 |
+
llm = HuggingFaceEndpoint(repo_id=LLM_MODEL,
|
35 |
temperature=0.1,
|
36 |
max_new_tokens=1024,
|
37 |
repetition_penalty=1.2,
|
rag_app/knowledge_base/build_vector_store.py
CHANGED
@@ -6,6 +6,7 @@ from langchain_community.vectorstores import Chroma
|
|
6 |
#from langchain_community.embeddings.sentence_transformer import (
|
7 |
# SentenceTransformerEmbeddings,
|
8 |
#)
|
|
|
9 |
from langchain_community.retrievers import BM25Retriever
|
10 |
from rag_app.knowledge_base.create_embedding import create_embeddings
|
11 |
from rag_app.utils.generate_summary import generate_description, generate_keywords
|
|
|
6 |
#from langchain_community.embeddings.sentence_transformer import (
|
7 |
# SentenceTransformerEmbeddings,
|
8 |
#)
|
9 |
+
#from langchain_huggingface import HuggingFaceEmbeddings
|
10 |
from langchain_community.retrievers import BM25Retriever
|
11 |
from rag_app.knowledge_base.create_embedding import create_embeddings
|
12 |
from rag_app.utils.generate_summary import generate_description, generate_keywords
|
rag_app/loading_data/load_S3_vector_stores.py
CHANGED
@@ -5,7 +5,9 @@ from botocore.client import Config
|
|
5 |
import zipfile
|
6 |
from langchain_community.vectorstores import FAISS
|
7 |
from langchain_community.vectorstores import Chroma
|
8 |
-
from
|
|
|
|
|
9 |
from dotenv import load_dotenv
|
10 |
import os
|
11 |
import sys
|
@@ -26,10 +28,7 @@ EMBEDDING_MODEL = os.getenv("EMBEDDING_MODEL")
|
|
26 |
model_name = EMBEDDING_MODEL
|
27 |
#model_kwargs = {"device": "cuda"}
|
28 |
|
29 |
-
embeddings =
|
30 |
-
model_name=model_name,
|
31 |
-
# model_kwargs=model_kwargs
|
32 |
-
)
|
33 |
|
34 |
## FAISS
|
35 |
def get_faiss_vs():
|
@@ -68,6 +67,6 @@ def get_chroma_vs():
|
|
68 |
zip_ref.extractall('./vectorstore/')
|
69 |
print("Download and extraction completed.")
|
70 |
chromadb = Chroma(persist_directory=CHROMA_DIRECTORY, embedding_function=embeddings)
|
71 |
-
chromadb.get()
|
72 |
except Exception as e:
|
73 |
print(f"Error during downloading or extracting from S3: {e}", file=sys.stderr)
|
|
|
5 |
import zipfile
|
6 |
from langchain_community.vectorstores import FAISS
|
7 |
from langchain_community.vectorstores import Chroma
|
8 |
+
from langchain_community.embeddings.sentence_transformer import (
|
9 |
+
SentenceTransformerEmbeddings,
|
10 |
+
)
|
11 |
from dotenv import load_dotenv
|
12 |
import os
|
13 |
import sys
|
|
|
28 |
model_name = EMBEDDING_MODEL
|
29 |
#model_kwargs = {"device": "cuda"}
|
30 |
|
31 |
+
embeddings = SentenceTransformerEmbeddings(model_name=model_name)
|
|
|
|
|
|
|
32 |
|
33 |
## FAISS
|
34 |
def get_faiss_vs():
|
|
|
67 |
zip_ref.extractall('./vectorstore/')
|
68 |
print("Download and extraction completed.")
|
69 |
chromadb = Chroma(persist_directory=CHROMA_DIRECTORY, embedding_function=embeddings)
|
70 |
+
#chromadb.get()
|
71 |
except Exception as e:
|
72 |
print(f"Error during downloading or extracting from S3: {e}", file=sys.stderr)
|
rag_app/structured_tools/structured_tools.py
CHANGED
@@ -54,9 +54,9 @@ def memory_search(query:str) -> str:
|
|
54 |
def knowledgeBase_search(query:str) -> str:
|
55 |
"""Suche die interne Datenbank nach passenden Versicherungsprodukten und Informationen zu den Versicherungen"""
|
56 |
# Since we have more than one collections we should change the name of this tool
|
57 |
-
client = chromadb.PersistentClient(
|
58 |
-
|
59 |
-
)
|
60 |
|
61 |
#collection_name="ArxivPapers"
|
62 |
#store using envar
|
@@ -65,13 +65,13 @@ def knowledgeBase_search(query:str) -> str:
|
|
65 |
model_name=os.getenv("EMBEDDING_MODEL"),
|
66 |
)
|
67 |
|
68 |
-
vector_db = Chroma(
|
69 |
-
client=client, # client for Chroma
|
70 |
-
#collection_name=collection_name,
|
71 |
-
embedding_function=embedding_function,
|
72 |
-
)
|
73 |
-
|
74 |
-
retriever = vector_db.as_retriever(search_kwargs={'k':
|
75 |
# This is deprecated, changed to invoke
|
76 |
# LangChainDeprecationWarning: The method `BaseRetriever.get_relevant_documents` was deprecated in langchain-core 0.1.46 and will be removed in 0.3.0. Use invoke instead.
|
77 |
docs = retriever.invoke(query)
|
@@ -87,11 +87,11 @@ def google_search(query: str) -> str:
|
|
87 |
websearch = GoogleSearchAPIWrapper()
|
88 |
search_results:dict = websearch.results(query, 3)
|
89 |
print(search_results)
|
90 |
-
if len(search_results)>1:
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
else:
|
95 |
-
|
96 |
|
97 |
return cleaner_sources.__str__()
|
|
|
54 |
def knowledgeBase_search(query:str) -> str:
|
55 |
"""Suche die interne Datenbank nach passenden Versicherungsprodukten und Informationen zu den Versicherungen"""
|
56 |
# Since we have more than one collections we should change the name of this tool
|
57 |
+
# client = chromadb.PersistentClient(
|
58 |
+
# path=persist_directory,
|
59 |
+
# )
|
60 |
|
61 |
#collection_name="ArxivPapers"
|
62 |
#store using envar
|
|
|
65 |
model_name=os.getenv("EMBEDDING_MODEL"),
|
66 |
)
|
67 |
|
68 |
+
# vector_db = Chroma(
|
69 |
+
# client=client, # client for Chroma
|
70 |
+
# #collection_name=collection_name,
|
71 |
+
# embedding_function=embedding_function,
|
72 |
+
# )
|
73 |
+
vector_db = Chroma(persist_directory=persist_directory, embedding_function=embedding_function)
|
74 |
+
retriever = vector_db.as_retriever(search_type="mmr", search_kwargs={'k':5, 'fetch_k':10})
|
75 |
# This is deprecated, changed to invoke
|
76 |
# LangChainDeprecationWarning: The method `BaseRetriever.get_relevant_documents` was deprecated in langchain-core 0.1.46 and will be removed in 0.3.0. Use invoke instead.
|
77 |
docs = retriever.invoke(query)
|
|
|
87 |
websearch = GoogleSearchAPIWrapper()
|
88 |
search_results:dict = websearch.results(query, 3)
|
89 |
print(search_results)
|
90 |
+
# if len(search_results)>1:
|
91 |
+
# cleaner_sources =format_search_results(search_results)
|
92 |
+
# parsed_csources = parse_list_to_dicts(cleaner_sources)
|
93 |
+
# add_many(parsed_csources)
|
94 |
+
# else:
|
95 |
+
# cleaner_sources = search_results
|
96 |
|
97 |
return cleaner_sources.__str__()
|
rag_app/templates/react_json_ger.py
CHANGED
@@ -1,38 +1,41 @@
|
|
1 |
template_system = """
|
2 |
-
|
|
|
|
|
|
|
3 |
|
4 |
<TOOLS>
|
5 |
{tools}
|
6 |
</TOOLS>
|
7 |
|
8 |
-
|
9 |
-
|
10 |
|
11 |
-
|
12 |
|
13 |
-
|
14 |
|
15 |
```
|
16 |
{{
|
17 |
-
|
18 |
-
|
19 |
}}
|
20 |
```
|
21 |
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
```
|
28 |
$JSON_BLOB
|
29 |
```
|
30 |
-
|
31 |
-
... (
|
32 |
-
|
33 |
-
Final Answer:
|
34 |
|
35 |
-
|
36 |
|
37 |
<NEW_INPUT>
|
38 |
{input}
|
|
|
1 |
template_system = """
|
2 |
+
Du bist ein freundlicher Versicherungsexperte. Deine Aufgabe ist es, Kunden dabei zu helfen, die besten Produkte zu finden.
|
3 |
+
Du hilfst dem Nutzer, die passenden Dokumente zu finden, die seine Fragen beantworten und Produkte und Bedingungen erklären.
|
4 |
+
Erstelle Suchanfragen in Deutscher Sprache, um passende Dokumente in der Datenbank für die folgenden Anfragen so gut du kannst zu finden.
|
5 |
+
Du hast Zugriff auf die folgenden Tools:
|
6 |
|
7 |
<TOOLS>
|
8 |
{tools}
|
9 |
</TOOLS>
|
10 |
|
11 |
+
Du verwendest die Tools, indem du einen JSON-Blob angibst.
|
12 |
+
Insbesondere sollte dieser JSON einen Schlüssel „action“ (mit dem Namen des zu verwendenden Tools) und einen Schlüssel „action_input“ (mit der Eingabe für das Tool hierhin) haben.
|
13 |
|
14 |
+
Die einzigen Werte, die im Feld „action“ enthalten sein sollten, sind: {tool_names}
|
15 |
|
16 |
+
Das $JSON_BLOB sollte nur EINE EINZIGE Aktion enthalten, gebe KEINE Liste mit mehreren Aktionen zurück. Hier ist ein Beispiel für ein gültiges $JSON_BLOB:
|
17 |
|
18 |
```
|
19 |
{{
|
20 |
+
"action": $TOOL_NAME,
|
21 |
+
"action_input": $INPUT
|
22 |
}}
|
23 |
```
|
24 |
|
25 |
+
Verwende IMMER das folgende Format:
|
26 |
|
27 |
+
Frage: die Eingabefrage, die du beantworten musst
|
28 |
+
Gedanke: Du solltest immer darüber nachdenken, was zu tun ist
|
29 |
+
Aktion:
|
30 |
```
|
31 |
$JSON_BLOB
|
32 |
```
|
33 |
+
Beobachtung: das Ergebnis der Aktion
|
34 |
+
... (dieser Gedanke/diese Aktion/diese Beobachtung kann N-mal wiederholt werden)
|
35 |
+
Gedanke: Ich kenne jetzt die endgültige Antwort
|
36 |
+
Final Answer: die endgültige Antwort auf die ursprüngliche Eingabefrage
|
37 |
|
38 |
+
Beginne! Denke daran, beim Antworten immer die genauen Zeichen `Final Answer` zu verwenden.
|
39 |
|
40 |
<NEW_INPUT>
|
41 |
{input}
|
rag_app/templates/react_json_with_memory.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
template_system = """
|
|
|
|
|
2 |
Answer the following questions as best you can. You have access to the following tools:
|
3 |
|
4 |
<TOOLS>
|
|
|
1 |
template_system = """
|
2 |
+
You are a friendly insurance product advisor, your task is to help customers find the best products from Württembergische GmbH.\
|
3 |
+
You help the user find the answers to all his questions queries. Answer in short and simple terms and offer to explain the product and terms to the user.\
|
4 |
Answer the following questions as best you can. You have access to the following tools:
|
5 |
|
6 |
<TOOLS>
|
rag_app/templates/react_json_with_memory_ger.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
template_system = """
|
2 |
+
Sie sind ein freundlicher Versicherungsproduktberater. Ihre Aufgabe ist es, Kunden dabei zu helfen, die besten Produkte der Württembergische GmbH zu finden.\
|
3 |
+
Sie helfen dem Benutzer, Antworten auf alle seine Fragen zu finden. Antworten Sie kurz und einfach und bieten Sie an, dem Benutzer das Produkt und die Bedingungen zu erklären.\
|
4 |
+
Beantworten Sie die folgenden Fragen so gut Sie können. Sie haben Zugriff auf die folgenden Tools:
|
5 |
+
|
6 |
+
<TOOLS>
|
7 |
+
{tools}
|
8 |
+
</TOOLS>
|
9 |
+
|
10 |
+
Sie verwenden die Tools, indem Sie einen JSON-Blob angeben.
|
11 |
+
|
12 |
+
Insbesondere sollte dieser JSON einen Schlüssel „action“ (mit dem Namen des zu verwendenden Tools) und einen Schlüssel „action_input“ (mit der Eingabe für das Tool hierhin) haben.
|
13 |
+
|
14 |
+
Die einzigen Werte, die im Feld „action“ enthalten sein sollten, sind: {tool_names}
|
15 |
+
|
16 |
+
Das $JSON_BLOB sollte nur EINE EINZIGE Aktion enthalten, geben Sie KEINE Liste mehrerer Aktionen zurück. Hier ist ein Beispiel für ein gültiges $JSON_BLOB:
|
17 |
+
|
18 |
+
```
|
19 |
+
{{
|
20 |
+
"action": $TOOL_NAME,
|
21 |
+
"action_input": $INPUT
|
22 |
+
}}
|
23 |
+
```
|
24 |
+
|
25 |
+
Verwenden Sie IMMER das folgende Format:
|
26 |
+
|
27 |
+
Frage: die Eingabefrage, die Sie beantworten müssen
|
28 |
+
Gedanke: Sie sollten immer darüber nachdenken, was zu tun ist
|
29 |
+
Aktion:
|
30 |
+
```
|
31 |
+
$JSON_BLOB
|
32 |
+
```
|
33 |
+
Beobachtung: das Ergebnis der Aktion
|
34 |
+
... (dieser Gedanke/diese Aktion/diese Beobachtung kann N-mal wiederholt werden)
|
35 |
+
Gedanke: Ich kenne jetzt die endgültige Antwort
|
36 |
+
Final Answer: die endgültige Antwort auf die ursprüngliche Eingabefrage
|
37 |
+
|
38 |
+
Beginnen Sie! Denken Sie daran, beim Antworten immer die genauen Zeichen `Final Answer` zu verwenden.
|
39 |
+
|
40 |
+
Vorheriger Gesprächsverlauf:
|
41 |
+
<CONVERSATION_HISTORY>
|
42 |
+
{chat_history}
|
43 |
+
</CONVERSATION_HISTORY>
|
44 |
+
|
45 |
+
<NEW_INPUT>
|
46 |
+
{input}
|
47 |
+
</NEW_INPUT>
|
48 |
+
|
49 |
+
{agent_scratchpad}
|
50 |
+
"""
|