Neurolingua commited on
Commit
f85bc8f
·
verified ·
1 Parent(s): c8af05e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -13
app.py CHANGED
@@ -15,6 +15,20 @@ app = Flask(__name__)
15
  CHROMA_PATH = '/code/chroma_db'
16
  if not os.path.exists(CHROMA_PATH):
17
  os.makedirs(CHROMA_PATH)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  # Set AI71 API key
20
  AI71_API_KEY = os.environ.get('AI71_API_KEY')
@@ -72,19 +86,28 @@ def generate_response(query, chat_history):
72
 
73
  # Query the RAG system
74
  def query_rag(query_text: str, chat_history):
75
- db = Chroma(persist_directory=CHROMA_PATH, embedding_function=HuggingFaceEmbeddings())
76
-
77
- results = db.similarity_search_with_score(query_text, k=5)
78
-
79
- if not results:
80
- return "Sorry, I couldn't find any relevant information."
81
-
82
- context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
83
-
84
- prompt = f"Context:\n{context_text}\n\nQuestion:\n{query_text}"
85
- response = generate_response(prompt, chat_history)
86
-
87
- return response
 
 
 
 
 
 
 
 
 
88
 
89
  # Flask route to handle WhatsApp webhook
90
  @app.route('/whatsapp', methods=['POST'])
 
15
  CHROMA_PATH = '/code/chroma_db'
16
  if not os.path.exists(CHROMA_PATH):
17
  os.makedirs(CHROMA_PATH)
18
+ def initialize_chroma():
19
+ try:
20
+ # Initialize Chroma
21
+ embedding_function = HuggingFaceEmbeddings() # Use your desired embedding function
22
+ db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
23
+
24
+ # Perform an initial operation to ensure the database is correctly initialized
25
+ db.similarity_search_with_score("test query", k=1)
26
+ print("Chroma initialized successfully.")
27
+ except Exception as e:
28
+ print(f"Error initializing Chroma: {e}")
29
+
30
+ initialize_chroma()
31
+
32
 
33
  # Set AI71 API key
34
  AI71_API_KEY = os.environ.get('AI71_API_KEY')
 
86
 
87
  # Query the RAG system
88
  def query_rag(query_text: str, chat_history):
89
+ try:
90
+ # Ensure the database is initialized
91
+ initialize_chroma()
92
+
93
+ embedding_function = HuggingFaceEmbeddings()
94
+ db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
95
+
96
+ results = db.similarity_search_with_score(query_text, k=5)
97
+
98
+ if not results:
99
+ return "Sorry, I couldn't find any relevant information."
100
+
101
+ context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
102
+
103
+ prompt = f"Context:\n{context_text}\n\nQuestion:\n{query_text}"
104
+ response = generate_response(prompt, chat_history)
105
+
106
+ return response
107
+ except Exception as e:
108
+ print(f"Error querying RAG system: {e}")
109
+ return "An error occurred while querying the RAG system."
110
+
111
 
112
  # Flask route to handle WhatsApp webhook
113
  @app.route('/whatsapp', methods=['POST'])