Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,65 @@
|
|
1 |
import os
|
2 |
import requests
|
3 |
import json
|
4 |
-
from
|
5 |
-
from ddtrace
|
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 |
-
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
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
|
27 |
'Content-Type': 'application/json'
|
28 |
}
|
29 |
-
|
30 |
"message": message,
|
31 |
"mode": "query"
|
32 |
}
|
33 |
-
data_json = json.dumps(data)
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
span.
|
|
|
38 |
|
39 |
try:
|
40 |
-
|
|
|
|
|
41 |
response_data = response.json()
|
42 |
bot_response = response_data.get("textResponse")
|
43 |
|
44 |
-
span.
|
45 |
|
46 |
-
|
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.
|
54 |
-
|
55 |
-
return
|
56 |
|
57 |
except Exception as e:
|
58 |
-
span.
|
59 |
-
|
60 |
-
return
|
61 |
|
62 |
-
if __name__ == "__main__":
|
63 |
-
message = input("You: ")
|
64 |
-
|
65 |
-
while message.lower() != "exit":
|
66 |
-
message = send_message(message)
|
67 |
|
68 |
-
|
|
|
|
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)
|