Severian commited on
Commit
4e59592
1 Parent(s): 074e56f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -36
app.py CHANGED
@@ -1,68 +1,65 @@
1
  import os
2
  import requests
3
  import json
4
- from ddtrace import patch_all
5
- from ddtrace.opentelemetry import TracerProvider, trace
6
- from opentelemetry import trace as ot_trace
7
 
8
  # Initialize Datadog tracing
9
  patch_all()
10
- provider = TracerProvider()
11
- ot_trace.set_tracer_provider(provider)
12
- tracer = ot_trace.get_tracer(__name__)
13
 
14
- # Set up environment variables
 
 
15
  os.environ['DD_LLMOBS_ENABLED'] = '1'
16
  os.environ['DD_LLMOBS_ML_APP'] = 'anything-api'
17
  os.environ['DD_LLMOBS_AGENTLESS_ENABLED'] = '1'
18
 
19
- # Ensure DD_API_KEY and DD_SITE are set in your environment variables
 
 
 
 
 
 
 
20
 
21
- @trace
22
- def send_message(message):
23
- url = 'https://severian-anything.hf.space/api/v1/workspace/Scoreboard/chat'
24
  headers = {
25
  'accept': 'application/json',
26
- 'Authorization': 'Bearer TYQYM46-RPCMQ98-GCGJMNB-Q23K6HC',
27
  'Content-Type': 'application/json'
28
  }
29
- data = {
30
  "message": message,
31
  "mode": "query"
32
  }
33
- data_json = json.dumps(data)
34
 
35
- with tracer.start_as_current_span("llm_api_call") as span:
36
- span.set_attribute("llm.request.model", "anything-api")
37
- span.set_attribute("llm.request.input", message)
 
38
 
39
  try:
40
- response = requests.post(url, headers=headers, data=data_json)
 
 
41
  response_data = response.json()
42
  bot_response = response_data.get("textResponse")
43
 
44
- span.set_attribute("llm.response.output", bot_response)
45
 
46
- if bot_response:
47
- print(f"Bot: {bot_response}")
48
-
49
- new_message = input("You: ")
50
- return new_message
51
 
52
  except requests.RequestException as e:
53
- span.record_exception(e)
54
- print(f"Request failed: {e}")
55
- return None
56
 
57
  except Exception as e:
58
- span.record_exception(e)
59
- print(f"An error occurred: {e}")
60
- return None
61
 
62
- if __name__ == "__main__":
63
- message = input("You: ")
64
-
65
- while message.lower() != "exit":
66
- message = send_message(message)
67
 
68
- print("Chat ended.")
 
 
1
  import os
2
  import requests
3
  import json
4
+ from flask import Flask, request, jsonify
5
+ from ddtrace import patch_all, tracer
 
6
 
7
  # Initialize Datadog tracing
8
  patch_all()
 
 
 
9
 
10
+ app = Flask(__name__)
11
+
12
+ # Set up environment variables for Datadog tracing
13
  os.environ['DD_LLMOBS_ENABLED'] = '1'
14
  os.environ['DD_LLMOBS_ML_APP'] = 'anything-api'
15
  os.environ['DD_LLMOBS_AGENTLESS_ENABLED'] = '1'
16
 
17
+ @app.route('/llm_call', methods=['POST'])
18
+ def handle_llm_call():
19
+ # Extract data from the incoming request (e.g., message to LLM)
20
+ data = request.get_json()
21
+ message = data.get("message")
22
+
23
+ if not message:
24
+ return jsonify({"error": "No message provided"}), 400
25
 
26
+ url = 'https://severian-anything.hf.space/api/v1/workspace/scoreboard/chat'
 
 
27
  headers = {
28
  'accept': 'application/json',
29
+ 'Authorization': 'Bearer YOUR_TOKEN_HERE',
30
  'Content-Type': 'application/json'
31
  }
32
+ payload = {
33
  "message": message,
34
  "mode": "query"
35
  }
 
36
 
37
+ # Trace the LLM API call
38
+ with tracer.trace("llm_api_call", service="anything-api", resource="chat", span_type="http") as span:
39
+ span.set_tag("llm.request.model", "anything-api")
40
+ span.set_tag("llm.request.input", message)
41
 
42
  try:
43
+ # Make the actual call to the LLM API
44
+ response = requests.post(url, headers=headers, data=json.dumps(payload))
45
+ response.raise_for_status()
46
  response_data = response.json()
47
  bot_response = response_data.get("textResponse")
48
 
49
+ span.set_tag("llm.response.output", bot_response)
50
 
51
+ return jsonify({"bot_response": bot_response})
 
 
 
 
52
 
53
  except requests.RequestException as e:
54
+ span.set_tag("error", True)
55
+ span.set_tag("error.msg", str(e))
56
+ return jsonify({"error": f"Request failed: {e}"}), 500
57
 
58
  except Exception as e:
59
+ span.set_tag("error", True)
60
+ span.set_tag("error.msg", str(e))
61
+ return jsonify({"error": f"An error occurred: {e}"}), 500
62
 
 
 
 
 
 
63
 
64
+ if __name__ == "__main__":
65
+ app.run(host='0.0.0.0', port=7860)