Skip to content

Commit c1c6db8

Browse files
committed
Merge branch 'main' of github.com:Virtual-Protocol/virtuals-python
2 parents dff2d61 + 41bce19 commit c1c6db8

File tree

6 files changed

+253
-83
lines changed

6 files changed

+253
-83
lines changed

README.md

Lines changed: 164 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,46 @@
1-
# Virtuals Python API Library
2-
The Virtuals Python API Library is a Python library that provides a simple way to interact with the Virtuals API.
1+
# Overview
32

4-
Currently, this library supports the GAME API for configuring and deploying agents on the [Virtuals Platform](https://virtuals.io). This complements and extends the functionality of the [Agent Sandbox](https://sandbox.virtuals.io) by providing another way to evaluate and develop agents on Virtuals. With more programmatic interactions, this opens up more functionalities and possibilities to integrate it into your own applications.
3+
If you wish to have your applications powered by Agent using GAME, you may use GAME python SDK to build your application.
54

6-
## Documentation
7-
For a walkthrough of some documentation and SDK and usage, please refer to the notion [documentation]() page.
5+
<aside>
6+
💡 To best understand this walkthrough, please go through the previous section on
7+
[agent configurations in GAME](https://www.notion.so/1592d2a429e98016b389ea26b53686a3) to better understand the configurable components.
8+
9+
</aside>
10+
11+
## Create an API key
12+
13+
Open the [Virtuals Platform](https://app.virtuals.io/) and create/get an API key from the Virtuals Sandbox by clicking “Access G.A.M.E API”
14+
15+
![getGAMEApi](/accesskey.png)
16+
17+
Store the key in a safe location, like a `.bashrc` or a `.zshrc` file.
18+
19+
```bash
20+
export VIRTUALS_API_KEY="your_virtuals_api_key"
21+
```
22+
23+
Alternatively, you can also use a `.env` file ([`python-dotenv` package](https://github.com/theskumar/python-dotenv) to store and load the key) if you are using the Virtuals Python SDK.
824

925
## Installation
26+
27+
install our python SDK using pip
28+
1029
```bash
1130
pip install virtuals_sdk
1231
```
1332

14-
## Usage
33+
## Usage (GAME)
34+
35+
SDK can be used for multiple use cases:
36+
37+
1. Update the existing Agent in Twitter environment
38+
2. Build on other platforms using GAME.
39+
40+
### Update the existing Agent in Twitter environment
41+
42+
The SDK provides another interface to configure agents that is more friendly to developers. This is similar to configuring your agent in the [Agent Sandbox](https://game-lite.virtuals.io/).
43+
1544
```python
1645
from virtuals_sdk.game import Agent
1746

@@ -22,16 +51,140 @@ agent = Agent(
2251
description="HODL-9000: A meme-loving trading bot powered by hopium and ramen",
2352
world_info="Virtual crypto trading environment where 1 DOGE = 1 DOGE"
2453
)
54+
```
2555

26-
# list available functions that can be added to agent
27-
print(agent.list_available_default_twitter_functions()) # ['wait', 'reply_tweet', 'retweet', 'like_tweet', ...]
56+
You can also initialize the agent first with just the API key and set the goals, descriptions and world information separately and check the current agent descriptions if needed.
2857

29-
# selects functions to be enabled
58+
```python
59+
60+
agent = Agent(api_key=VIRTUALS_API_KEY)
61+
62+
# check what is current goal, descriptions and world_info
63+
agent.get_goal()
64+
agent.get_description()
65+
agent.get_world_info()
66+
67+
# Set components individually - set change the agent goal/description/worldinfo
68+
agent.set_goal("Autonomously analyze crypto markets and provide trading insights")
69+
agent.set_description("HODL-9000: A meme-loving trading bot powered by hopium and ramen")
70+
agent.set_world_info("Virtual crypto trading environment where 1 DOGE = 1 DOGE")
71+
72+
# check what is current goal, descriptions and world_info
73+
agent.get_goal()
74+
agent.get_description()
75+
agent.get_world_info()
76+
```
77+
78+
### Functions
79+
80+
By default, there are no functions enabled when the agent is initialized (i.e. the agent has no actions/functions it can execute). There are a list of available and provided functions for the Twitter/X platform and you can set them.
81+
82+
```python
83+
print(agent.list_available_default_twitter_functions())
84+
85+
# enable some functions for the agent to use
3086
agent.use_default_twitter_functions(["wait", "reply_tweet"])
87+
```
88+
89+
You can then equip the agent with some custom functions as follows:
90+
91+
```python
92+
93+
search_function = game.Function(
94+
fn_name="custom_search_internet",
95+
fn_description="search the internet for the best songs",
96+
args=[
97+
game.FunctionArgument(
98+
name="query",
99+
type="string",
100+
description="The query to search for"
101+
)
102+
],
103+
config=game.FunctionConfig(
104+
method="get",
105+
url="https://google.com",
106+
platform="twitter", # specify which platform this function is for, in this case this function is for twitter only
107+
success_feedback="I found the best songs",
108+
error_feedback="I couldn't find the best songs",
109+
)
110+
)
111+
112+
# adding custom functions only for platform twitter
113+
agent.add_custom_function(search_function)
114+
```
115+
116+
### Evaluate with Simulate, Deploy
117+
118+
You can simulate one step of the agentic loop on Twitter/X with your new configurations and see the outputs. This is similar to the simulate button on the [Agent Sandbox](https://game-lite.virtuals.io/). Hence, when running
31119

32-
# Simulate one step of the full agentic loop on Twitter/X from the HLP -> LLP -> action
120+
```python
121+
# Simulate one step of the full agentic loop on Twitter/X from the HLP -> LLP -> action (NOTE: supported for Twitter/X only now)
33122
response = agent.simulate_twitter(session_id="123")
34123

124+
# To more realistically simulate deployment you can
125+
```
126+
127+
```python
128+
# Simulate response to a certain event
129+
response = agent.react(
130+
session_id="567", # string identifier that you decide
131+
task_description="",
132+
context="",
133+
platform="",
134+
)
135+
```
136+
137+
Once you are happy, `deploy_twitter` will push your agent configurations to production and run your agent on Twitter/X autonomously.
138+
139+
```python
35140
# deploy agent! (NOTE: supported for Twitter/X only now)
36141
agent.deploy_twitter()
37-
```
142+
```
143+
144+
## Build on other platforms using GAME
145+
146+
`simulate_twitter` and `deploy_twitter` runs through the entire GAME stack from HLP → LLP→ action/function selected. However, these agent functionalities are currently for the Twitter/X platform. You may utilize Task-based Agent with Low-Level Planner and Reaction Module to develop applications that are powered by GAME. The Low Level Planner (LLP) of the agent (please see [documentation](https://www.notion.so/1592d2a429e98016b389ea26b53686a3?pvs=21) for more details on GAME and LLP) can separately act as a decision making engine based on a task description and event occurring. This agentic architecture is simpler but also sufficient for many applications.
147+
148+
We are releasing this simpler setup as a more generalised/platform agnostic framework (not specific to Twitter/X). The entire GAME stack along with the HLP will be opened up to be fully configurable and platform agnostic in the coming weeks.
149+
150+
<aside>
151+
🖥️ Low-Level Planner (LLP) as a Task-based Agent
152+
153+
![llp.png](/llp.png)
154+
155+
</aside>
156+
157+
After configuring the agent’s character card or description and setting up the agents functions, we can then use the `react` method to get an agent to respond and execute a sequence of actions based on the task description provided and the context. Between each action in the sequence, the agent only receives the `success_feedback` and `error_feedback` of each function executed.
158+
159+
```python
160+
# React/respond to a certain event
161+
response = agent.react(
162+
session_id="567", # string identifier that you decide
163+
task_description="",
164+
context="Hi how are you?",
165+
platform="TELEGRAM",
166+
)
167+
```
168+
169+
<aside>
170+
⚠️ Remember that the `platform` tag determines what functions are available to the agent. The agent will have access to functions that have the same `platform` tag. All the default available functions listed on `agent.list_available_default_twitter_functions()` and set via `agent.use_default_twitter_functions()` have the `platform` tag of “twitter”.
171+
172+
</aside>
173+
174+
## Arguments Definition
175+
176+
### Session ID
177+
178+
The session ID is an identifier for an instance of the agent. When using the same session ID, it maintains and picks up from where it last left off, continuing the session/instance. It should be split per user/ conversation that you are maintaining on your platform. For different platforms, different session ID can be used.
179+
180+
### Platform Tag
181+
182+
When adding custom functions, and when calling the react agent (i.e. LLP), there is a platform tag that can be defined. This acts like a filter for the functions available that is passed to the agent. You should define the platform when passing in the events.
183+
184+
### Task Description
185+
186+
Task description serves as the prompt for the agent to respond. Since the reaction can be platform-based, you can define task description based on the platforms. In the task description, you should pass in any related info that require agent to make decision. That should include:
187+
188+
- User message
189+
- Conversation history
190+
- Instructions

accesskey.png

188 KB
Loading

examples/example-custom.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
from virtuals_sdk import game
3+
4+
agent = game.Agent(
5+
api_key=os.environ.get("VIRTUALS_API_KEY"),
6+
goal="search for best songs",
7+
description="Test Description",
8+
world_info="Test World Info"
9+
)
10+
11+
# running reaction module for other platforms
12+
# adding custom functions for platform specifics
13+
agent.add_custom_function(
14+
game.Function(
15+
fn_name="custom_search_internet",
16+
fn_description="search the internet for the best songs",
17+
args=[
18+
game.FunctionArgument(
19+
name="query",
20+
type="string",
21+
description="The query to search for"
22+
)
23+
],
24+
config=game.FunctionConfig(
25+
method="get",
26+
url="https://google.com",
27+
platform="telegram", # this function will only be used for telegram
28+
success_feedback="I found the best songs",
29+
error_feedback="I couldn't find the best songs",
30+
)
31+
)
32+
)
33+
34+
# running reaction module only for platform telegram
35+
agent.react(
36+
session_id="session-telegram",
37+
# specify the platform telegram
38+
platform="telegram",
39+
# specify the event that triggers the reaction
40+
event="message from user: give me some great music?",
41+
# specify the task that the agent should do
42+
task="reply with a music recommendation",
43+
)

examples/example-twitter.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
from virtuals_sdk import game
3+
4+
agent = game.Agent(
5+
api_key=os.environ.get("VIRTUALS_API_KEY"),
6+
goal="search for best songs",
7+
description="Test Description",
8+
world_info="Test World Info"
9+
)
10+
11+
# applicable only for platform twitter
12+
agent.list_available_default_twitter_functions()
13+
agent.use_default_twitter_functions(["wait", "reply_tweet"])
14+
15+
# adding custom functions only for platform twitter
16+
agent.add_custom_function(
17+
game.Function(
18+
fn_name="custom_search_internet",
19+
fn_description="search the internet for the best songs",
20+
args=[
21+
game.FunctionArgument(
22+
name="query",
23+
type="string",
24+
description="The query to search for"
25+
)
26+
],
27+
config=game.FunctionConfig(
28+
method="get",
29+
url="https://google.com",
30+
# specify which platform this function is for, in this case this function is for twitter only
31+
platform="twitter",
32+
success_feedback="I found the best songs",
33+
error_feedback="I couldn't find the best songs",
34+
)
35+
)
36+
)
37+
38+
# running reaction module only for platform twitter
39+
agent.react(
40+
session_id="session-twitter",
41+
platform="twitter",
42+
tweet_id="1869281466628349975",
43+
)
44+
45+
# running simulation module only for platform twitter
46+
agent.simulate_twitter(session_id="session-twitter")

examples/test.py

Lines changed: 0 additions & 72 deletions
This file was deleted.

llp.png

246 KB
Loading

0 commit comments

Comments
 (0)