isayahc commited on
Commit
ceaa546
2 Parent(s): 6592db0 0e10735

merging from main

Browse files
.github/{ISSUE_TEMPLATE/issue_template.md → issue_template.md} RENAMED
File without changes
.github/{ISSUE_TEMPLATE/pullrequest_template.md → pullrequest_template.md} RENAMED
File without changes
.github/workflows/sync_2_hf.yaml CHANGED
@@ -17,4 +17,4 @@ jobs:
17
  - name: Push to hub
18
  env:
19
  HF_TOKEN: ${{ secrets.HF_TOKEN }}
20
- run: git push https://sabazo:$HF_TOKEN@huggingface.co/spaces/sabazo/insurance-advisor-agents main
 
17
  - name: Push to hub
18
  env:
19
  HF_TOKEN: ${{ secrets.HF_TOKEN }}
20
+ run: git push https://sabazo:$HF_TOKEN@huggingface.co/spaces/sabazo/insurance_advisor_wb main
.gitignore CHANGED
@@ -166,4 +166,6 @@ cython_debug/
166
  *.pickle
167
 
168
 
 
 
169
  *.db
 
166
  *.pickle
167
 
168
 
169
+ # Databases
170
+
171
  *.db
README.md CHANGED
@@ -3,7 +3,7 @@ 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
 
3
  emoji: 🤖
4
  colorFrom: red
5
  colorTo: indigo
6
+ sdk: gradio
7
  python: 3.11
8
  app_file: app_gui.py
9
  pinned: false
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 rag_app.loading_data.load_S3_vector_stores import get_chroma_vs
 
4
  from rag_app.agents.react_agent import agent_executor
5
  from config import db
6
 
 
1
  # Import Gradio for UI, along with other necessary libraries
2
  import gradio as gr
3
  from rag_app.loading_data.load_S3_vector_stores import get_chroma_vs
4
+ from rag_app.loading_data.load_S3_vector_stores import get_chroma_vs
5
  from rag_app.agents.react_agent import agent_executor
6
  from config import db
7
 
example.env CHANGED
@@ -5,14 +5,12 @@ GOOGLE_API_KEY=""
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"
 
5
 
6
  # Vectorstore storage on S3 and locally
7
  S3_LOCATION="rad-rag-demos"
 
 
8
  FAISS_VS_NAME="vectorstores/faiss-insurance-agent-multilingual-cased-1500.zip"
9
+ CHROMA_VS_NAME="vectorstores/chroma-zurich-mpnet-1500.zip"
10
+ # directories that need to be adjusted for windows
 
11
  FAISS_INDEX_PATH = "./vectorstore/faiss-insurance-agent-multilingual-cased-1500"
12
  CHROMA_DIRECTORY = "./vectorstore/chroma-insurance-agent-multilingual-cased-500"
13
+ VS_DESTINATION_FOLDER="./vectorstore/"
14
 
15
  # for chromadb
16
  VECTOR_DATABASE_LOCATION="./vectorstore/chroma-insurance-agent-multilingual-cased-500"
rag_app/agents/react_agent.py CHANGED
@@ -7,17 +7,17 @@ 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.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
19
-
20
- # set_llm_cache(SQLiteCache(database_path=".cache.db"))
21
  # logger = logger.get_console_logger("hf_mixtral_agent")
22
 
23
  config = load_dotenv(".env")
@@ -25,10 +25,8 @@ 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,
@@ -40,11 +38,10 @@ llm = HuggingFaceEndpoint(repo_id=LLM_MODEL,
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(
@@ -74,8 +71,8 @@ agent_executor = AgentExecutor(
74
  agent=agent,
75
  tools=tools,
76
  verbose=True,
77
- max_iterations=10, # cap number of iterations
78
- #max_execution_time=60, # timout at 60 sec
79
  return_intermediate_steps=True,
80
  handle_parsing_errors=True,
81
  )
 
7
  from langchain.tools.render import render_text_description
8
  import os
9
  from dotenv import load_dotenv
10
+ # local cache
11
+ from langchain.globals import set_llm_cache
12
+ from langchain.cache import SQLiteCache # sqlite
13
+ #from langchain.cache import InMemoryCache # in memory cache
14
+ from rag_app.structured_tools.structured_tools import (
15
+ google_search, knowledgeBase_search
16
  )
17
 
18
  from langchain.prompts import PromptTemplate
19
  from rag_app.templates.react_json_with_memory_ger import template_system
20
  # from innovation_pathfinder_ai.utils import logger
 
 
 
 
21
  # logger = logger.get_console_logger("hf_mixtral_agent")
22
 
23
  config = load_dotenv(".env")
 
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
+ set_llm_cache(SQLiteCache(database_path=".cache.db"))
 
 
30
 
31
  # Load the model from the Hugging Face Hub
32
  llm = HuggingFaceEndpoint(repo_id=LLM_MODEL,
 
38
 
39
 
40
  tools = [
41
+ knowledgeBase_search,
42
+ google_search,
43
+ #web_research,
44
+ #ask_user
 
45
  ]
46
 
47
  prompt = PromptTemplate.from_template(
 
71
  agent=agent,
72
  tools=tools,
73
  verbose=True,
74
+ max_iterations=20, # cap number of iterations
75
+ max_execution_time=90, # timout at 60 sec
76
  return_intermediate_steps=True,
77
  handle_parsing_errors=True,
78
  )
rag_app/chains/__init__.py CHANGED
@@ -1,2 +1 @@
1
- # from rag_app.chains.s
2
  from rag_app.chains.user_response_sentiment_chain import user_response_sentiment_prompt
 
 
1
  from rag_app.chains.user_response_sentiment_chain import user_response_sentiment_prompt
rag_app/loading_data/load_S3_vector_stores.py CHANGED
@@ -32,41 +32,43 @@ embeddings = SentenceTransformerEmbeddings(model_name=model_name)
32
 
33
  ## FAISS
34
  def get_faiss_vs():
35
- # Initialize an S3 client with unsigned configuration for public access
36
- s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED))
 
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...")
43
- s3.download_file(S3_LOCATION, FAISS_VS_NAME, VS_DESTINATION)
44
 
45
- # Extract the downloaded zip file
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
57
  def get_chroma_vs():
58
- # Initialize an S3 client with unsigned configuration for public access
59
- s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED))
 
60
 
61
- VS_DESTINATION = CHROMA_DIRECTORY+".zip"
62
- try:
63
- # Download the pre-prepared vectorized index from the S3 bucket
64
- print("Downloading the pre-prepared chroma vectorstore from S3...")
65
- s3.download_file(S3_LOCATION, CHROMA_VS_NAME, VS_DESTINATION)
66
- with zipfile.ZipFile(VS_DESTINATION, 'r') as zip_ref:
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)
 
32
 
33
  ## FAISS
34
  def get_faiss_vs():
35
+ if not os.path.exists(FAISS_INDEX_PATH):
36
+ # Initialize an S3 client with unsigned configuration for public access
37
+ s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED))
38
 
39
+ # Define the destination for the downloaded file
40
+ VS_DESTINATION = FAISS_INDEX_PATH + ".zip"
41
+ try:
42
+ # Download the pre-prepared vectorized index from the S3 bucket
43
+ print("Downloading the pre-prepared FAISS vectorized index from S3...")
44
+ s3.download_file(S3_LOCATION, FAISS_VS_NAME, VS_DESTINATION)
45
 
46
+ # Extract the downloaded zip file
47
+ with zipfile.ZipFile(VS_DESTINATION, 'r') as zip_ref:
48
+ zip_ref.extractall('./vectorstore/')
49
+ print("Download and extraction completed.")
50
+ return FAISS.load_local(FAISS_INDEX_PATH, embeddings, allow_dangerous_deserialization=True)
51
+
52
+ except Exception as e:
53
+ print(f"Error during downloading or extracting from S3: {e}", file=sys.stderr)
54
+ #faissdb = FAISS.load_local(FAISS_INDEX_PATH, embeddings)
55
 
56
 
57
  ## Chroma DB
58
  def get_chroma_vs():
59
+ if not os.path.exists(CHROMA_DIRECTORY):
60
+ # Initialize an S3 client with unsigned configuration for public access
61
+ s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED))
62
 
63
+ VS_DESTINATION = CHROMA_DIRECTORY+".zip"
64
+ try:
65
+ # Download the pre-prepared vectorized index from the S3 bucket
66
+ print("Downloading the pre-prepared chroma vectorstore from S3...")
67
+ s3.download_file(S3_LOCATION, CHROMA_VS_NAME, VS_DESTINATION)
68
+ with zipfile.ZipFile(VS_DESTINATION, 'r') as zip_ref:
69
+ zip_ref.extractall('./vectorstore/')
70
+ print("Download and extraction completed.")
71
+ chromadb = Chroma(persist_directory=CHROMA_DIRECTORY, embedding_function=embeddings)
72
+ #chromadb.get()
73
+ except Exception as e:
74
+ print(f"Error during downloading or extracting from S3: {e}", file=sys.stderr)
rag_app/loading_data/load_chroma_db_cross_platform.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ import boto3
3
+ from botocore.client import Config
4
+ from botocore import UNSIGNED
5
+ from dotenv import load_dotenv
6
+ import os
7
+ import sys
8
+ import zipfile
9
+
10
+
11
+ def download_chroma_from_s3(s3_location:str,
12
+ chroma_vs_name:str,
13
+ vectorstore_folder:str,
14
+ vs_save_name:str) -> None:
15
+ """
16
+ Downloads the Chroma DB from an S3 storage to local folder
17
+
18
+ Args
19
+ s3_location (str): The name of S3 bucket
20
+ chroma_vs_name (str): The name of the file to download from S3
21
+ vectorstore_folder (str): The filepath to vectorstore folder in project dir
22
+ vs_save_name (str): The name of the vector store
23
+
24
+ """
25
+ vs_destination = Path()/vectorstore_folder/vs_save_name
26
+ vs_save_path = vs_destination.with_suffix('.zip')
27
+
28
+ try:
29
+ # Initialize an S3 client with unsigned configuration for public access
30
+ s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED))
31
+ s3.download_file(s3_location, chroma_vs_name, vs_save_path)
32
+ print('Downloaded file from S3')
33
+
34
+ # Extract the zip file
35
+ with zipfile.ZipFile(file=str(vs_save_path), mode='r') as zip_ref:
36
+ zip_ref.extractall(path=vectorstore_folder)
37
+ print("Extracted zip file")
38
+
39
+ except Exception as e:
40
+ print(f"Error during downloading or extracting from S3: {e}", file=sys.stderr)
41
+
42
+ # Delete the zip file
43
+ vs_save_path.unlink()
44
+ print("Deleting zip file")
45
+
46
+ if __name__ == "__main__":
47
+
48
+ S3_LOCATION = os.getenv("S3_LOCATION")
49
+
50
+ chroma_vs_name = "vectorstores/chroma-zurich-mpnet-1500.zip"
51
+
52
+ project_dir = Path().cwd().parent.parent
53
+ vs_destination = str(project_dir / 'vectorstore')
54
+ assert Path(vs_destination).is_dir(), "Cannot find vectorstore folder"
55
+
56
+ download_chroma_from_s3(s3_location=S3_LOCATION,
57
+ chroma_vs_name=chroma_vs_name,
58
+ vectorstore_folder=vs_destination,
59
+ vs_save_name='chroma-zurich-mpnet-1500')
rag_app/reranking.py CHANGED
@@ -5,11 +5,13 @@ from dotenv import load_dotenv
5
  import os
6
  from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
7
  import requests
 
 
8
 
9
  load_dotenv()
10
 
11
 
12
- def get_reranked_docs(query:str,
13
  path_to_db:str,
14
  embedding_model:str,
15
  hf_api_key:str,
@@ -59,22 +61,71 @@ def get_reranked_docs(query:str,
59
  ranked_results = sorted(zip(docs, passages, relevance_scores), key=lambda x: x[2], reverse=True)
60
  top_k_results = ranked_results[:num_docs]
61
  return [doc for doc, _, _ in top_k_results]
 
62
 
63
-
64
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
67
- EMBEDDING_MODEL = os.getenv("EMBEDDING_MODEL")
 
 
68
 
69
- path_to_vector_db = Path("..")/'vectorstore/faiss-insurance-agent-500'
 
 
 
 
 
70
 
71
- query = "Ich möchte wissen, ob ich meine geriatrische Haustier-Eidechse versichern kann"
 
 
 
 
72
 
73
- top_5_docs = get_reranked_docs(query=query,
74
- path_to_db=path_to_vector_db,
75
- embedding_model=EMBEDDING_MODEL,
76
- hf_api_key=HUGGINGFACEHUB_API_TOKEN,
77
- num_docs=5)
78
 
79
- for i, doc in enumerate(top_5_docs):
80
- print(f"{i}: {doc}\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  import os
6
  from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
7
  import requests
8
+ from langchain_community.vectorstores import Chroma
9
+
10
 
11
  load_dotenv()
12
 
13
 
14
+ def get_reranked_docs_faiss(query:str,
15
  path_to_db:str,
16
  embedding_model:str,
17
  hf_api_key:str,
 
61
  ranked_results = sorted(zip(docs, passages, relevance_scores), key=lambda x: x[2], reverse=True)
62
  top_k_results = ranked_results[:num_docs]
63
  return [doc for doc, _, _ in top_k_results]
64
+
65
 
66
+
67
+ def get_reranked_docs_chroma(query:str,
68
+ path_to_db:str,
69
+ embedding_model:str,
70
+ hf_api_key:str,
71
+ reranking_hf_url:str = "https://api-inference.huggingface.co/models/sentence-transformers/all-mpnet-base-v2",
72
+ num_docs:int=5) -> list:
73
+ """ Re-ranks the similarity search results and returns top-k highest ranked docs
74
+
75
+ Args:
76
+ query (str): The search query
77
+ path_to_db (str): Path to the vectorstore database
78
+ embedding_model (str): Embedding model used in the vector store
79
+ num_docs (int): Number of documents to return
80
+
81
+ Returns: A list of documents with the highest rank
82
+ """
83
+ embeddings = HuggingFaceInferenceAPIEmbeddings(api_key=hf_api_key,
84
+ model_name=embedding_model)
85
+ # Load the vectorstore database
86
+ db = Chroma(persist_directory=path_to_db, embedding_function=embeddings)
87
 
88
+ # Get k documents based on similarity search
89
+ sim_docs = db.similarity_search(query=query, k=10)
90
+
91
+ passages = [doc.page_content for doc in sim_docs]
92
 
93
+ # Prepare the payload
94
+ payload = {"inputs":
95
+ {"source_sentence": query,
96
+ "sentences": passages}}
97
+
98
+ headers = {"Authorization": f"Bearer {hf_api_key}"}
99
 
100
+ response = requests.post(url=reranking_hf_url, headers=headers, json=payload)
101
+ print(f'{response = }')
102
+ if response.status_code != 200:
103
+ print('Something went wrong with the response')
104
+ return
105
 
106
+ similarity_scores = response.json()
107
+ ranked_results = sorted(zip(sim_docs, passages, similarity_scores), key=lambda x: x[2], reverse=True)
108
+ top_k_results = ranked_results[:num_docs]
109
+ return [doc for doc, _, _ in top_k_results]
 
110
 
111
+
112
+
113
+ if __name__ == "__main__":
114
+
115
+
116
+ HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
117
+ EMBEDDING_MODEL = "sentence-transformers/multi-qa-mpnet-base-dot-v1"
118
+
119
+ project_dir = Path().cwd().parent
120
+ path_to_vector_db = str(project_dir/'vectorstore/chroma-zurich-mpnet-1500')
121
+ assert Path(path_to_vector_db).exists(), "Cannot access path_to_vector_db "
122
+
123
+ query = "I'm looking for student insurance"
124
+
125
+ re_ranked_docs = get_reranked_docs_chroma(query=query,
126
+ path_to_db= path_to_vector_db,
127
+ embedding_model=EMBEDDING_MODEL,
128
+ hf_api_key=HUGGINGFACEHUB_API_TOKEN)
129
+
130
+
131
+ print(f"{re_ranked_docs=}")
rag_app/structured_tools/structured_tools.py CHANGED
@@ -4,6 +4,10 @@ from langchain_community.embeddings.sentence_transformer import (
4
  SentenceTransformerEmbeddings,
5
  )
6
  from langchain_community.vectorstores import Chroma
 
 
 
 
7
  from rag_app.utils.utils import (
8
  parse_list_to_dicts, format_search_results
9
  )
@@ -11,6 +15,10 @@ import chromadb
11
  import os
12
  from config import db, PERSIST_DIRECTORY, EMBEDDING_MODEL
13
 
 
 
 
 
14
 
15
  @tool
16
  def memory_search(query:str) -> str:
 
4
  SentenceTransformerEmbeddings,
5
  )
6
  from langchain_community.vectorstores import Chroma
7
+ import ast
8
+ from rag_app.loading_data.load_S3_vector_stores import get_chroma_vs
9
+ import chromadb
10
+
11
  from rag_app.utils.utils import (
12
  parse_list_to_dicts, format_search_results
13
  )
 
15
  import os
16
  from config import db, PERSIST_DIRECTORY, EMBEDDING_MODEL
17
 
18
+ persist_directory = os.getenv('VECTOR_DATABASE_LOCATION')
19
+ embedding_model = os.getenv("EMBEDDING_MODEL")
20
+ if not os.path.exists(persist_directory):
21
+ get_chroma_vs()
22
 
23
  @tool
24
  def memory_search(query:str) -> str:
rag_app/templates/react_json_with_memory_ger.py CHANGED
@@ -1,7 +1,7 @@
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>
 
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
  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.\
4
+ Wenn du denkst, die Fragen des Benutzers ausreichend beantowrtet zu haben, Frage ihn nach seiner Email Addresse.\
5
  Beantworte die folgenden Fragen so gut du kannst. Du hast Zugriff auf die folgenden Tools:
6
 
7
  <TOOLS>