Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

## Usage

This is the github repo for our NPM package. Refer to usage examples here: https://www.npmjs.com/package/@virtuals-protocol/game
Expand All @@ -7,7 +6,6 @@ Get a GAME API key in the console https://console.game.virtuals.io/

If you have any trouble, contact Virtuals support or DevRel team members via Discord or Telegram.


## Installation

To install the package, run:
Expand All @@ -17,49 +15,52 @@ npm install @virtuals-protocol/game
```

## Game-starter
In the `game-starter` folder is a starter project that will get you up and running with a working agent in minutes.

Go into the folder's readme for instructions are on how to get started.
In the `game-starter` folder is a starter project that will get you up and running with a working agent in minutes.

Go into the folder's readme for instructions are on how to get started.

## Examples
In the `examples` folder, there are two self contained examples: a twitter agent and a telegram agent.

In the `examples` folder, there are two self contained examples: a twitter agent and a telegram agent.

Just compile with `npm run build` and `npm start` to run! (make sure you have an API key first!)

## Plugins
In the `plugins` folder are various plugins that can give your agent more functionality.

Each plugin comes with an example file. Click into the plugin's src folder to run the `example.ts` file!
In the `plugins` folder are various plugins that can give your agent more functionality.

Plugins are always open source and we welcome contributions!
Each plugin comes with an example file. Click into the plugin's src folder to run the `example.ts` file!

Plugins are always open source and we welcome contributions!

## Components and Architecture Overview

At a high level, this SDK allows you to develop your agents powered by the GAME architecture in its most full and flexible form. The SDK is made up of 3 main components (Agent, Worker, function), each with configurable arguments. Our docs expands in greater depth [G.A.M.E Docs](https://docs.game.virtuals.io/game-sdk).



![New SDK visual](docs/imgs/new_sdk_visual.png)


Agent (a.k.a. [high level planner](https://docs.game.virtuals.io/game-cloud#high-level-planner-context))

- Takes in a <b>Goal</b>
- Drives the agent's behavior through the high-level plan which influences the thinking and creation of tasks that would contribute towards this goal
- Takes in a <b>Description</b>
- Combination of what was previously known as World Info + Agent Description
- This includes a description of the "world" the agent lives in, and the personality and background of the agent

Worker (a.k.a. [low-level planner](https://docs.game.virtuals.io/game-cloud#low-level-planner-context))
Worker (a.k.a. [low-level planner](https://docs.game.virtuals.io/game-cloud#low-level-planner-context))

- Takes in a <b>Description</b>
- Used to control which workers are called by the agent, based on the high-level plan and tasks created to contribute to the goal

Function

- Takes in a <b>Description</b>
- Used to control which functions are called by the workers, based on each worker's low-level plan
- This can be any executable

Chat Agents
-Chat Agents enable interactive conversations with AI agents that can execute functions. They are simpler to use than full Agents and are ideal for chatbot-like interactions where the agent can perform actions.

## License

Expand Down
109 changes: 109 additions & 0 deletions examples/chatAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import {
ChatAgent,
FunctionResultStatus,
} from "@virtuals-protocol/game";

import GameFunction, { ExecutableGameFunctionResponse, ExecutableGameFunctionStatus } from "../src/function";

type FunctionResult = [FunctionResultStatus, string, Record<string, any>];

// Action Functions
const generatePicture = async (): Promise<ExecutableGameFunctionResponse> => {
return new ExecutableGameFunctionResponse(ExecutableGameFunctionStatus.Done, "Picture generated and presented to the user");
};

const generateMusic = async (): Promise<ExecutableGameFunctionResponse> => {
return new ExecutableGameFunctionResponse(ExecutableGameFunctionStatus.Done, "Music generated and presented to the user");
}



// Action Space
const actionSpace: GameFunction<any>[] = [
new GameFunction({
name: "generate_picture",
description: "Generate a picture",
args: [{ name: "prompt", description: "The prompt for the picture" }],
executable: generatePicture,
}),
new GameFunction({
name: "generate_music",
description: "Generate a music",
args: [{ name: "prompt", description: "The prompt for the music" }],
executable: generateMusic,
}),
new GameFunction({
name: "check_crypto_price",
description: "Check the price of a crypto currency",
args: [{ name: "currency", description: "The currency to check the price of" }],
executable: async (args, logger) => {
const prices: Record<string, number> = {
bitcoin: 100000,
ethereum: 20000,
};


const result = prices[args.currency!.toLowerCase()];

if (!result) {
return new ExecutableGameFunctionResponse(ExecutableGameFunctionStatus.Failed, "The price of the currency is not available");
}

return new ExecutableGameFunctionResponse(ExecutableGameFunctionStatus.Done, `The price of ${args.currency} is ${result}`);
}
}),
];

// Environment check
const apiKey = "API_KEY";
if (!apiKey) {
throw new Error("GAME_API_KEY is not set");
}
// Create agent
const agent = new ChatAgent(apiKey, "You are helpful assistant");

const main = async () => {
const chat = await agent.createChat({
partnerId: "tom",
partnerName: "Tom",
actionSpace: actionSpace,
});
let chatContinue = true;

while (chatContinue) {
const userMessage = await getUserInput("Enter a message: ");

const response = await chat.next(userMessage);

if (response.functionCall) {
console.log(`Function call: ${response.functionCall.fn_name}`);
}

if (response.message) {
console.log(`Response: ${response.message}`);
}

if (response.isFinished) {
chatContinue = false;
break;
}
}

console.log("Chat ended");
};

const getUserInput = async (prompt: string): Promise<string> => {
const readline = (await import("readline")).default.createInterface({
input: process.stdin,
output: process.stdout,
});

return new Promise((resolve) => {
readline.question(prompt, (answer) => {
readline.close();
resolve(answer);
});
});
};

main().catch(console.error);
Loading