Redmind commited on
Commit
a9016f4
·
verified ·
1 Parent(s): 47da451

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -56,7 +56,7 @@ load_dotenv()
56
 
57
  # langfuse analytics
58
  from langfuse.callback import CallbackHandler
59
-
60
  # Inventory API data table
61
  from tabulate import tabulate
62
 
@@ -121,6 +121,13 @@ branch = os.getenv("branch")
121
  langfuse_handler = CallbackHandler()
122
  langfuse_handler.auth_check() # Optional: Checks if the authentication is successful
123
 
 
 
 
 
 
 
 
124
  nltk.download('punkt')
125
 
126
  open_api_key_token = os.getenv("OPENAI_API_KEY")
@@ -156,7 +163,18 @@ apis = [
156
  # LLM setup
157
  llm = ChatOpenAI(model="gpt-4o-mini", max_tokens=300, temperature=0.1)
158
  llm_chart = OpenAI(is_safe=False)
159
-
 
 
 
 
 
 
 
 
 
 
 
160
  def get_schema(_):
161
  schema_info = db.get_table_info() # This should be a string of your SQL schema
162
  return schema_info
@@ -705,9 +723,10 @@ def answer_question_thread(user_question, chatbot,audio=None):
705
 
706
  while iterations < max_iterations:
707
 
708
- response = agent_executor.invoke({"input": user_question}, config={"callbacks": [langfuse_handler]}, early_stopping_method="generate")
 
709
  # Track the response
710
- agent_responses.append(agent_output)
711
 
712
  #create_file_HF()
713
  if isinstance(response, dict):
@@ -771,9 +790,10 @@ def answer_question_thread(user_question, chatbot,audio=None):
771
  else:
772
  if("max iterations" in response_text):
773
  print("max iterations error in 2")
774
- print(agent_responses[-1])
775
- if agent_executor.history:
776
- print(agent_executor.history[-1])
 
777
  return user_question, response_text
778
  # response_text = response_text.replace('\n', ' ').replace(' ', ' ').strip()
779
  # return response_text
 
56
 
57
  # langfuse analytics
58
  from langfuse.callback import CallbackHandler
59
+ from langchain.callbacks import CallbackManager, BaseCallbackHandler
60
  # Inventory API data table
61
  from tabulate import tabulate
62
 
 
121
  langfuse_handler = CallbackHandler()
122
  langfuse_handler.auth_check() # Optional: Checks if the authentication is successful
123
 
124
+ # Create the callback manager and add both the response history and langfuse handler
125
+ callback_handler = ResponseHistoryCallback()
126
+
127
+
128
+ callback_manager = CallbackManager([callback_handler, langfuse_handler])
129
+
130
+
131
  nltk.download('punkt')
132
 
133
  open_api_key_token = os.getenv("OPENAI_API_KEY")
 
163
  # LLM setup
164
  llm = ChatOpenAI(model="gpt-4o-mini", max_tokens=300, temperature=0.1)
165
  llm_chart = OpenAI(is_safe=False)
166
+ # Define a custom callback to capture responses and track history
167
+ class ResponseHistoryCallback(BaseCallbackHandler):
168
+ def __init__(self):
169
+ self.history = [] # Initialize history to store responses
170
+
171
+ def on_agent_finish(self, output):
172
+ # Capture the response when the agent finishes
173
+ self.history.append(output)
174
+ print(f"Agent's response: {output}") # Optionally print
175
+
176
+ def get_history(self):
177
+ return self.history # Return the captured history
178
  def get_schema(_):
179
  schema_info = db.get_table_info() # This should be a string of your SQL schema
180
  return schema_info
 
723
 
724
  while iterations < max_iterations:
725
 
726
+ #response = agent_executor.invoke({"input": user_question}, config={"callbacks": [langfuse_handler]}, early_stopping_method="generate")
727
+ response = agent_executor.invoke({"input": user_question}, config={"callbacks": [callback_manager]}, early_stopping_method="generate")
728
  # Track the response
729
+ agent_responses.append(response)
730
 
731
  #create_file_HF()
732
  if isinstance(response, dict):
 
790
  else:
791
  if("max iterations" in response_text):
792
  print("max iterations error in 2")
793
+ # Access the response history after execution
794
+ response_history = callback_handler.get_history()
795
+ print("Response History:", response_history)
796
+
797
  return user_question, response_text
798
  # response_text = response_text.replace('\n', ' ').replace(' ', ' ').strip()
799
  # return response_text