-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathagent.py
More file actions
78 lines (59 loc) · 2.52 KB
/
agent.py
File metadata and controls
78 lines (59 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import json
import os
import requests
RUN_ID = os.getenv("RUN_ID")
if not RUN_ID:
print("[AGENT] WARNING: RUN_ID is not set")
SANDBOX_PROXY_URL = os.getenv("SANDBOX_PROXY_URL")
if not SANDBOX_PROXY_URL:
print("[AGENT] WARNING: SANDBOX_PROXY_URL is not set")
def inference(model, temperature, messages):
try:
payload = {"run_id": RUN_ID, "model": model, "temperature": temperature, "messages": messages}
print(
f"[AGENT] inference(): Sending inference request for model {model} (temperature {temperature}) with {len(messages)} messages"
)
response = requests.post(
f"{SANDBOX_PROXY_URL}/api/inference", headers={"Content-Type": "application/json"}, data=json.dumps(payload)
)
if response.status_code == 200:
result = response.text.strip('"')
print(f"[AGENT] inference(): Inference response: {len(result)} characters")
return result
else:
print(f"[AGENT] inference(): Inference failed with status {response.status_code}: {response.text}")
return None
except Exception as e:
print(f"[AGENT] inference(): Inference request failed: {e}")
return None
def embedding(input):
try:
payload = {"run_id": RUN_ID, "input": input}
print("[AGENT] embedding(): Sending embedding request...")
response = requests.post(
f"{SANDBOX_PROXY_URL}/api/embedding", headers={"Content-Type": "application/json"}, data=json.dumps(payload)
)
if response.status_code == 200:
result = response.json()
print(f"[AGENT] embedding(): Embedding response: {len(result)} dimensions")
return result
else:
print(f"[AGENT] embedding(): Embedding failed with status {response.status_code}: {response.text}")
return None
except Exception as e:
print(f"[AGENT] embedding(): Embedding request failed: {e}")
return None
def agent_main(input):
print("[AGENT] Entered agent_main()")
# Test inference function
message = "What is 2+2?"
print(f"[AGENT] <-- '{message}'")
messages = [{"role": "user", "content": message}]
inference_result = inference("moonshotai/Kimi-K2-Instruct", 0.5, messages)
if inference_result:
print(f"[AGENT] --> '{inference_result}'")
print("[AGENT] Reading solution from /sandbox/solution.diff")
with open("/sandbox/solution.diff", "r") as f:
diff = f.read()
print("[AGENT] Exiting agent_main()")
return diff