Severian commited on
Commit
542b57b
1 Parent(s): fce4937

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -3
app.py CHANGED
@@ -49,8 +49,8 @@ def handle_llm_call():
49
  "mode": "query"
50
  }
51
 
52
- # Trace the LLM API call
53
- with tracer.trace("llm_api_call", service="anything-api", resource="chat", span_type="http") as span:
54
  logging.debug("Starting trace for LLM API call.")
55
  span.set_tag("llm.request.model", "anything-api")
56
  span.set_tag("llm.request.input", message)
@@ -62,7 +62,12 @@ def handle_llm_call():
62
  response_data = response.json()
63
  bot_response = response_data.get("textResponse")
64
 
 
65
  span.set_tag("llm.response.output", bot_response)
 
 
 
 
66
  logging.debug(f"LLM API response: {bot_response}")
67
 
68
  return jsonify({"bot_response": bot_response})
@@ -79,7 +84,6 @@ def handle_llm_call():
79
  logging.error(f"An error occurred: {e}")
80
  return jsonify({"error": f"An error occurred: {e}"}), 500
81
 
82
-
83
  if __name__ == "__main__":
84
  logging.info("Starting Flask app on port 7860")
85
  app.run(host='0.0.0.0', port=7860)
 
49
  "mode": "query"
50
  }
51
 
52
+ # Use LLMObs to trace the LLM API call
53
+ with LLMObs.trace("llm_api_call", service="anything-api", resource="chat") as span:
54
  logging.debug("Starting trace for LLM API call.")
55
  span.set_tag("llm.request.model", "anything-api")
56
  span.set_tag("llm.request.input", message)
 
62
  response_data = response.json()
63
  bot_response = response_data.get("textResponse")
64
 
65
+ # Set LLM-specific tags and metrics
66
  span.set_tag("llm.response.output", bot_response)
67
+ span.set_metric("llm.tokens.prompt", len(message.split()))
68
+ span.set_metric("llm.tokens.completion", len(bot_response.split()))
69
+ span.set_metric("llm.tokens.total", len(message.split()) + len(bot_response.split()))
70
+
71
  logging.debug(f"LLM API response: {bot_response}")
72
 
73
  return jsonify({"bot_response": bot_response})
 
84
  logging.error(f"An error occurred: {e}")
85
  return jsonify({"error": f"An error occurred: {e}"}), 500
86
 
 
87
  if __name__ == "__main__":
88
  logging.info("Starting Flask app on port 7860")
89
  app.run(host='0.0.0.0', port=7860)