name observation process
Browse files
chat_3.py
CHANGED
@@ -95,7 +95,7 @@ class Chat:
|
|
95 |
history_str_parts.append(f"{role}: {msg.content}")
|
96 |
return "\n".join(history_str_parts)
|
97 |
|
98 |
-
@observe()
|
99 |
def classify_input(self, user_input: str) -> str:
|
100 |
history_content_list = [msg.content for msg in self.history]
|
101 |
return classify_input_type(user_input, history=history_content_list)
|
@@ -103,7 +103,7 @@ class Chat:
|
|
103 |
def format_docs(self, docs: list) -> str:
|
104 |
return "\n\n".join(doc.page_content for doc in docs)
|
105 |
|
106 |
-
@observe()
|
107 |
def get_retriever_and_prompt(self, lang_code: str):
|
108 |
"""
|
109 |
Returns the appropriate retriever and RAG prompt based on the language.
|
@@ -139,7 +139,7 @@ class Chat:
|
|
139 |
|
140 |
return retriever, prompt_template
|
141 |
|
142 |
-
@observe()
|
143 |
def call_non_rag(self, user_input: str, input_lang: str) -> str:
|
144 |
try:
|
145 |
if hasattr(NON_RAG_PROMPT, "format_messages"):
|
@@ -157,13 +157,13 @@ class Chat:
|
|
157 |
print(f"Error during Non-RAG LLM call: {e}")
|
158 |
return "Sorry, I had trouble processing your general request."
|
159 |
|
160 |
-
@observe()
|
161 |
def _observe_detect_language(self, user_input: str) -> str:
|
162 |
"""Wraps the detect_language call for Langfuse observation."""
|
163 |
return detect_language(user_input)
|
164 |
|
165 |
# If the main chat method itself should be a trace, uncomment @observe() below
|
166 |
-
|
167 |
def chat(self, user_input: str) -> str:
|
168 |
# print(f"\n\n-- USER INPUT: {user_input} --")
|
169 |
try:
|
@@ -194,10 +194,23 @@ class Chat:
|
|
194 |
|
195 |
self.append_history(AIMessage(content=ai_response_content))
|
196 |
|
197 |
-
# print(f"AI:::: {ai_response_content}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
return ai_response_content
|
199 |
|
200 |
-
|
|
|
|
|
201 |
def call_rag_v2(self, user_input: str, input_lang: str, history_for_rewrite: list) -> str:
|
202 |
try:
|
203 |
retriever, selected_rag_prompt = self.get_retriever_and_prompt(input_lang)
|
@@ -238,7 +251,7 @@ class Chat:
|
|
238 |
print(f"Error during RAG LLM call: {e}")
|
239 |
return "Sorry, I encountered an error while generating the response."
|
240 |
|
241 |
-
@observe()
|
242 |
def _rewrite_query_if_needed_v2(self, user_input: str, history_list: list) -> str:
|
243 |
if not history_list:
|
244 |
# self.langfuse_handler.trace(name="rewrite_query_skipped_no_history", input={"user_input": user_input}, output=user_input)
|
|
|
95 |
history_str_parts.append(f"{role}: {msg.content}")
|
96 |
return "\n".join(history_str_parts)
|
97 |
|
98 |
+
@observe(name="ClassifyInput")
|
99 |
def classify_input(self, user_input: str) -> str:
|
100 |
history_content_list = [msg.content for msg in self.history]
|
101 |
return classify_input_type(user_input, history=history_content_list)
|
|
|
103 |
def format_docs(self, docs: list) -> str:
|
104 |
return "\n\n".join(doc.page_content for doc in docs)
|
105 |
|
106 |
+
@observe(name="GetRetrieverandPrompt")
|
107 |
def get_retriever_and_prompt(self, lang_code: str):
|
108 |
"""
|
109 |
Returns the appropriate retriever and RAG prompt based on the language.
|
|
|
139 |
|
140 |
return retriever, prompt_template
|
141 |
|
142 |
+
@observe(name="Non-Rag-Flow")
|
143 |
def call_non_rag(self, user_input: str, input_lang: str) -> str:
|
144 |
try:
|
145 |
if hasattr(NON_RAG_PROMPT, "format_messages"):
|
|
|
157 |
print(f"Error during Non-RAG LLM call: {e}")
|
158 |
return "Sorry, I had trouble processing your general request."
|
159 |
|
160 |
+
@observe(name="DetectLang")
|
161 |
def _observe_detect_language(self, user_input: str) -> str:
|
162 |
"""Wraps the detect_language call for Langfuse observation."""
|
163 |
return detect_language(user_input)
|
164 |
|
165 |
# If the main chat method itself should be a trace, uncomment @observe() below
|
166 |
+
@observe("MainChatFlow")
|
167 |
def chat(self, user_input: str) -> str:
|
168 |
# print(f"\n\n-- USER INPUT: {user_input} --")
|
169 |
try:
|
|
|
194 |
|
195 |
self.append_history(AIMessage(content=ai_response_content))
|
196 |
|
197 |
+
# # print(f"AI:::: {ai_response_content}")
|
198 |
+
# return ai_response_content
|
199 |
+
|
200 |
+
try:
|
201 |
+
full_conversation = "\n".join(
|
202 |
+
f"{'User' if isinstance(m, HumanMessage) else 'AI'}: {m.content}"
|
203 |
+
for m in self.history
|
204 |
+
)
|
205 |
+
self.langfuse_handler.update_current_trace(metadata={"full_conversation": full_conversation})
|
206 |
+
except Exception as e:
|
207 |
+
print(f"Failed to update Langfuse metadata: {e}")
|
208 |
+
|
209 |
return ai_response_content
|
210 |
|
211 |
+
|
212 |
+
|
213 |
+
@observe("Rag-Flow")
|
214 |
def call_rag_v2(self, user_input: str, input_lang: str, history_for_rewrite: list) -> str:
|
215 |
try:
|
216 |
retriever, selected_rag_prompt = self.get_retriever_and_prompt(input_lang)
|
|
|
251 |
print(f"Error during RAG LLM call: {e}")
|
252 |
return "Sorry, I encountered an error while generating the response."
|
253 |
|
254 |
+
@observe("RewriteQuery")
|
255 |
def _rewrite_query_if_needed_v2(self, user_input: str, history_list: list) -> str:
|
256 |
if not history_list:
|
257 |
# self.langfuse_handler.trace(name="rewrite_query_skipped_no_history", input={"user_input": user_input}, output=user_input)
|