-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathsdk.py
More file actions
136 lines (107 loc) · 4.04 KB
/
sdk.py
File metadata and controls
136 lines (107 loc) · 4.04 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import requests
class GameSDK:
api_url: str = "https://game-api.virtuals.io/api"
api_key: str
def __init__(self, api_key: str):
self.api_key = api_key
def functions(self):
"""
Get all default functions
"""
response = requests.get(
f"{self.api_url}/functions", headers={"x-api-key": self.api_key})
if (response.status_code != 200):
raise Exception(response.json())
functions = {}
for x in response.json()["data"]:
functions[x["fn_name"]] = x["fn_description"]
return functions
def simulate(self, session_id: str, goal: str, description: str, functions: list, custom_functions: list):
"""
Simulate the agent configuration
"""
response = requests.post(
f"{self.api_url}/simulate",
json={
"data": {
"sessionId": session_id,
"goal": goal,
"description": description,
"worldInfo": "",
"functions": functions,
"customFunctions": [x.toJson() for x in custom_functions]
}
},
headers={"x-api-key": self.api_key}
)
if (response.status_code != 200):
raise Exception(response.json())
return response.json()["data"]
def react(self, session_id: str, platform: str, goal: str,
description: str, functions: list, custom_functions: list,
event: str = None, task: str = None, tweet_id: str = None):
"""
Simulate the agent configuration
"""
url = f"{self.api_url}/react/{platform}"
payload = {
"sessionId": session_id,
"goal": goal,
"description": description,
"worldInfo": "",
"functions": functions,
"customFunctions": [x.toJson() for x in custom_functions]
}
if (event):
payload["event"] = event
if (task):
payload["task"] = task
if (tweet_id):
payload["tweetId"] = tweet_id
print(payload)
response = requests.post(
url,
json={
"data": payload
},
headers={"x-api-key": self.api_key}
)
if (response.status_code != 200):
raise Exception(response.json())
return response.json()["data"]
def deploy(self, goal: str, description: str, functions: list, custom_functions: list, main_heartbeat: int, reaction_heartbeat: int, tweet_usernames: list = None, templates: list = None, game_engine_model: str = "llama_3_1_405b"):
"""
Simulate the agent configuration
"""
payload = {
"goal": goal,
"description": description,
"functions": functions,
"customFunctions": [x.toJson() for x in custom_functions],
"gameState": {
"mainHeartbeat": main_heartbeat,
"reactionHeartbeat": reaction_heartbeat,
},
"gameEngineModel": game_engine_model
}
if tweet_usernames is not None:
payload["tweetUsernames"] = tweet_usernames
# Add templates to payload if provided
if templates:
payload["templates"] = [template.to_dict() for template in templates]
response = requests.post(
f"{self.api_url}/deploy",
json={
"data": payload
},
headers={"x-api-key": self.api_key}
)
if (response.status_code != 200):
raise Exception(response.json())
return response.json()["data"]
def reset_memory(self):
response = requests.get(
f"{self.api_url}/reset-session", headers={"x-api-key": self.api_key})
if (response.status_code != 200):
raise Exception("Failed to reset memory.")
return "Memory reset successfully."