Spaces:
Sleeping
Sleeping
Merge pull request #5 from almutareb/agent_as_tools
Browse files- .devcontainer/Dockerfile +3 -1
- None +0 -0
- app_gui.py +14 -11
- core-langchain-rag.py +1 -1
- example.env +14 -12
- rag_app/agents/__init__.py +0 -0
- rag_app/agents/kb_retriever_agent.py +72 -0
- rag_app/agents/react_agent.py +10 -6
- rag_app/database/__init__.py +0 -0
- rag_app/knowledge_base/__init__.py +0 -0
- rag_app/knowledge_base/build_vector_store.py +1 -0
- rag_app/loading_data/load_S3_vector_stores.py +8 -42
- rag_app/structured_tools/agent_tools.py +31 -0
- rag_app/structured_tools/structured_tools.py +13 -13
- rag_app/templates/react_json_ger.py +45 -0
- rag_app/templates/react_json_with_memory.py +2 -0
- rag_app/templates/react_json_with_memory_ger.py +51 -0
.devcontainer/Dockerfile
CHANGED
@@ -44,4 +44,6 @@ 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
|
|
|
|
|
|
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 |
+
&& echo "done 4" \
|
49 |
+
&& pip install -r requirements.txt
|
None
DELETED
Binary file (12.3 kB)
|
|
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 |
|
@@ -28,13 +27,16 @@ def bot(history):
|
|
28 |
def infer(question, history):
|
29 |
# Use the question and history to query the RAG model
|
30 |
#result = qa({"query": question, "history": history, "question": question})
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
# CSS styling for the Gradio interface
|
40 |
css = """
|
@@ -53,9 +55,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 |
|
|
|
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 = """
|
|
|
55 |
with gr.Column(elem_id="col-container"):
|
56 |
gr.HTML(title) # Add the HTML title to the interface
|
57 |
chatbot = gr.Chatbot([], elem_id="chatbot",
|
58 |
+
label="BotTina 2.0",
|
59 |
+
bubble_full_width=False,
|
60 |
+
avatar_images=(None, "https://dacodi-production.s3.amazonaws.com/store/87bc00b6727589462954f2e3ff6f531c.png"),
|
61 |
+
height=680,) # Initialize the chatbot component
|
62 |
clear = gr.Button("Clear") # Add a button to clear the chat
|
63 |
|
64 |
# 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(
|
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
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# HF libraries
|
2 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
3 |
+
from langchain.agents import AgentExecutor
|
4 |
+
from langchain.agents.format_scratchpad import format_log_to_str
|
5 |
+
from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser
|
6 |
+
# Import things that are needed generically
|
7 |
+
from langchain.tools.render import render_text_description
|
8 |
+
import os
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
from rag_app.structured_tools.structured_tools import (
|
11 |
+
google_search, knowledgeBase_search
|
12 |
+
)
|
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")
|
20 |
+
|
21 |
+
config = load_dotenv(".env")
|
22 |
+
HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
|
23 |
+
GOOGLE_CSE_ID = os.getenv('GOOGLE_CSE_ID')
|
24 |
+
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
25 |
+
LLM_MODEL = os.getenv('LLM_MODEL')
|
26 |
+
|
27 |
+
# Load the model from the Hugging Face Hub
|
28 |
+
llm = HuggingFaceEndpoint(repo_id=LLM_MODEL,
|
29 |
+
temperature=0.1,
|
30 |
+
max_new_tokens=1024,
|
31 |
+
repetition_penalty=1.2,
|
32 |
+
return_full_text=False
|
33 |
+
)
|
34 |
+
|
35 |
+
|
36 |
+
tools = [
|
37 |
+
knowledgeBase_search,
|
38 |
+
google_search,
|
39 |
+
]
|
40 |
+
|
41 |
+
prompt = PromptTemplate.from_template(
|
42 |
+
template=template_system
|
43 |
+
)
|
44 |
+
prompt = prompt.partial(
|
45 |
+
tools=render_text_description(tools),
|
46 |
+
tool_names=", ".join([t.name for t in tools]),
|
47 |
+
)
|
48 |
+
|
49 |
+
|
50 |
+
# define the agent
|
51 |
+
chat_model_with_stop = llm.bind(stop=["\nObservation"])
|
52 |
+
agent = (
|
53 |
+
{
|
54 |
+
"input": lambda x: x["input"],
|
55 |
+
"agent_scratchpad": lambda x: format_log_to_str(x["intermediate_steps"]),
|
56 |
+
#"chat_history": lambda x: x["chat_history"],
|
57 |
+
}
|
58 |
+
| prompt
|
59 |
+
| chat_model_with_stop
|
60 |
+
| ReActJsonSingleInputOutputParser()
|
61 |
+
)
|
62 |
+
|
63 |
+
# instantiate AgentExecutor
|
64 |
+
agent_worker = AgentExecutor(
|
65 |
+
agent=agent,
|
66 |
+
tools=tools,
|
67 |
+
verbose=True,
|
68 |
+
max_iterations=10, # cap number of iterations
|
69 |
+
#max_execution_time=60, # timout at 60 sec
|
70 |
+
return_intermediate_steps=True,
|
71 |
+
handle_parsing_errors=True,
|
72 |
+
)
|
rag_app/agents/react_agent.py
CHANGED
@@ -7,12 +7,12 @@ from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser
|
|
7 |
from langchain.tools.render import render_text_description
|
8 |
import os
|
9 |
from dotenv import load_dotenv
|
10 |
-
from rag_app.structured_tools.
|
11 |
-
|
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,
|
@@ -39,8 +40,11 @@ llm = HuggingFaceEndpoint(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
|
39 |
|
40 |
|
41 |
tools = [
|
42 |
-
knowledgeBase_search,
|
43 |
-
google_search,
|
|
|
|
|
|
|
44 |
]
|
45 |
|
46 |
prompt = PromptTemplate.from_template(
|
|
|
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, ask_user, get_email
|
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,
|
|
|
40 |
|
41 |
|
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/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,12 +5,13 @@ 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
|
12 |
import logging
|
13 |
-
from pathlib import Path
|
14 |
|
15 |
# Load environment variables from a .env file
|
16 |
config = load_dotenv(".env")
|
@@ -27,10 +28,7 @@ EMBEDDING_MODEL = os.getenv("EMBEDDING_MODEL")
|
|
27 |
model_name = EMBEDDING_MODEL
|
28 |
#model_kwargs = {"device": "cuda"}
|
29 |
|
30 |
-
embeddings =
|
31 |
-
model_name=model_name,
|
32 |
-
# model_kwargs=model_kwargs
|
33 |
-
)
|
34 |
|
35 |
## FAISS
|
36 |
def get_faiss_vs():
|
@@ -39,7 +37,6 @@ def get_faiss_vs():
|
|
39 |
|
40 |
# Define the destination for the downloaded file
|
41 |
VS_DESTINATION = FAISS_INDEX_PATH + ".zip"
|
42 |
-
|
43 |
try:
|
44 |
# Download the pre-prepared vectorized index from the S3 bucket
|
45 |
print("Downloading the pre-prepared FAISS vectorized index from S3...")
|
@@ -49,36 +46,11 @@ def get_faiss_vs():
|
|
49 |
with zipfile.ZipFile(VS_DESTINATION, 'r') as zip_ref:
|
50 |
zip_ref.extractall('./vectorstore/')
|
51 |
print("Download and extraction completed.")
|
52 |
-
return FAISS.load_local(FAISS_INDEX_PATH, embeddings,allow_dangerous_deserialization=True)
|
53 |
|
54 |
except Exception as e:
|
55 |
print(f"Error during downloading or extracting from S3: {e}", file=sys.stderr)
|
56 |
-
#
|
57 |
-
|
58 |
-
|
59 |
-
def get_faiss_vs_from_s3(s3_loc:str,
|
60 |
-
s3_vs_name:str,
|
61 |
-
vs_dir:str='vectorstore') -> None:
|
62 |
-
""" Download the FAISS vector store from S3 bucket
|
63 |
-
|
64 |
-
Args:
|
65 |
-
s3_loc (str): Name of the S3 bucket
|
66 |
-
s3_vs_name (str): Name of the file to be downloaded
|
67 |
-
vs_dir (str): The name of the directory where the file is to be saved
|
68 |
-
"""
|
69 |
-
# Initialize an S3 client with unsigned configuration for public access
|
70 |
-
s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED))
|
71 |
-
# Destination folder
|
72 |
-
vs_dir_path = Path("..") / vs_dir
|
73 |
-
assert vs_dir_path.is_dir(), "Cannot find vs_dir folder"
|
74 |
-
try:
|
75 |
-
vs_destination = Path("..") / vs_dir / "faiss-insurance-agent-500.zip"
|
76 |
-
s3.download_file(s3_loc, s3_vs_name, vs_destination)
|
77 |
-
# Extract the downloaded zip file
|
78 |
-
with zipfile.ZipFile(file=vs_destination, mode='r') as zip_ref:
|
79 |
-
zip_ref.extractall(path=vs_dir_path.as_posix())
|
80 |
-
except Exception as e:
|
81 |
-
print(f"Error during downloading or extracting from S3: {e}", file=sys.stderr)
|
82 |
|
83 |
|
84 |
## Chroma DB
|
@@ -95,12 +67,6 @@ def get_chroma_vs():
|
|
95 |
zip_ref.extractall('./vectorstore/')
|
96 |
print("Download and extraction completed.")
|
97 |
chromadb = Chroma(persist_directory=CHROMA_DIRECTORY, embedding_function=embeddings)
|
98 |
-
chromadb.get()
|
99 |
except Exception as e:
|
100 |
-
print(f"Error during downloading or extracting from S3: {e}", file=sys.stderr)
|
101 |
-
|
102 |
-
|
103 |
-
if __name__ == "__main__":
|
104 |
-
# get_faiss_vs_from_s3(s3_loc=S3_LOCATION, s3_vs_name=FAISS_VS_NAME)
|
105 |
-
pass
|
106 |
-
|
|
|
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
|
14 |
import logging
|
|
|
15 |
|
16 |
# Load environment variables from a .env file
|
17 |
config = load_dotenv(".env")
|
|
|
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():
|
|
|
37 |
|
38 |
# Define the destination for the downloaded file
|
39 |
VS_DESTINATION = FAISS_INDEX_PATH + ".zip"
|
|
|
40 |
try:
|
41 |
# Download the pre-prepared vectorized index from the S3 bucket
|
42 |
print("Downloading the pre-prepared FAISS vectorized index from S3...")
|
|
|
46 |
with zipfile.ZipFile(VS_DESTINATION, 'r') as zip_ref:
|
47 |
zip_ref.extractall('./vectorstore/')
|
48 |
print("Download and extraction completed.")
|
49 |
+
return FAISS.load_local(FAISS_INDEX_PATH, embeddings, allow_dangerous_deserialization=True)
|
50 |
|
51 |
except Exception as e:
|
52 |
print(f"Error during downloading or extracting from S3: {e}", file=sys.stderr)
|
53 |
+
#faissdb = FAISS.load_local(FAISS_INDEX_PATH, embeddings)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
|
56 |
## Chroma DB
|
|
|
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/agent_tools.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(
|
12 |
+
{
|
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(
|
@@ -54,24 +55,24 @@ 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
|
63 |
|
64 |
embedding_function = SentenceTransformerEmbeddings(
|
65 |
-
model_name=
|
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()
|
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)
|
@@ -83,7 +84,6 @@ def knowledgeBase_search(query:str) -> str:
|
|
83 |
@tool
|
84 |
def google_search(query: str) -> str:
|
85 |
"""Verbessere die Ergebnisse durch eine Suche über die Webseite der Versicherung. Erstelle eine neue Suchanfrage, um die Erfolgschancen zu verbesseren."""
|
86 |
-
global all_sources
|
87 |
|
88 |
websearch = GoogleSearchAPIWrapper()
|
89 |
search_results:dict = websearch.results(query, 3)
|
|
|
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(
|
|
|
55 |
def knowledgeBase_search(query:str) -> str:
|
56 |
"""Suche die interne Datenbank nach passenden Versicherungsprodukten und Informationen zu den Versicherungen"""
|
57 |
# Since we have more than one collections we should change the name of this tool
|
58 |
+
# client = chromadb.PersistentClient(
|
59 |
+
# path=persist_directory,
|
60 |
+
# )
|
61 |
|
62 |
#collection_name="ArxivPapers"
|
63 |
#store using envar
|
64 |
|
65 |
embedding_function = SentenceTransformerEmbeddings(
|
66 |
+
model_name=embedding_model
|
67 |
)
|
68 |
|
69 |
+
# vector_db = Chroma(
|
70 |
+
# client=client, # client for Chroma
|
71 |
+
# #collection_name=collection_name,
|
72 |
+
# embedding_function=embedding_function,
|
73 |
+
# )
|
74 |
+
vector_db = Chroma(persist_directory=persist_directory, embedding_function=embedding_function)
|
75 |
+
retriever = vector_db.as_retriever(search_type="mmr", search_kwargs={'k':5, 'fetch_k':10})
|
76 |
# This is deprecated, changed to invoke
|
77 |
# 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.
|
78 |
docs = retriever.invoke(query)
|
|
|
84 |
@tool
|
85 |
def google_search(query: str) -> str:
|
86 |
"""Verbessere die Ergebnisse durch eine Suche über die Webseite der Versicherung. Erstelle eine neue Suchanfrage, um die Erfolgschancen zu verbesseren."""
|
|
|
87 |
|
88 |
websearch = GoogleSearchAPIWrapper()
|
89 |
search_results:dict = websearch.results(query, 3)
|
rag_app/templates/react_json_ger.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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}
|
42 |
+
</NEW_INPUT>
|
43 |
+
|
44 |
+
{agent_scratchpad}
|
45 |
+
"""
|
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,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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}
|
9 |
+
</TOOLS>
|
10 |
+
|
11 |
+
Sie verwenden die Tools, indem Sie einen JSON-Blob angeben.
|
12 |
+
|
13 |
+
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.
|
14 |
+
|
15 |
+
Die einzigen Werte, die im Feld „action“ enthalten sein sollten, sind: {tool_names}
|
16 |
+
|
17 |
+
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:
|
18 |
+
|
19 |
+
```
|
20 |
+
{{
|
21 |
+
"action": $TOOL_NAME,
|
22 |
+
"action_input": $INPUT
|
23 |
+
}}
|
24 |
+
```
|
25 |
+
|
26 |
+
Verwenden Sie IMMER das folgende Format:
|
27 |
+
|
28 |
+
Frage: die Eingabefrage, die Sie beantworten müssen
|
29 |
+
Gedanke: Sie sollten immer darüber nachdenken, was zu tun ist
|
30 |
+
Aktion:
|
31 |
+
```
|
32 |
+
$JSON_BLOB
|
33 |
+
```
|
34 |
+
Beobachtung: das Ergebnis der Aktion
|
35 |
+
... (dieser Gedanke/diese Aktion/diese Beobachtung kann N-mal wiederholt werden)
|
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>
|
43 |
+
{chat_history}
|
44 |
+
</CONVERSATION_HISTORY>
|
45 |
+
|
46 |
+
<NEW_INPUT>
|
47 |
+
{input}
|
48 |
+
</NEW_INPUT>
|
49 |
+
|
50 |
+
{agent_scratchpad}
|
51 |
+
"""
|