Skip to content

Commit

Permalink
chore: add mastra ai example (#17)
Browse files Browse the repository at this point in the history
* chore: add mastra ai example

* small tweaks
  • Loading branch information
homanp authored Nov 26, 2024
1 parent 53f664d commit a4acc69
Show file tree
Hide file tree
Showing 16 changed files with 3,297 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ We've created some examples using populat agent frameworks you can use as inspir

- [Vercel AI SDK](https://github.com/superagent-ai/poker-eval/tree/main/examples/ai-sdk)
- [OpenAI](https://github.com/superagent-ai/poker-eval/tree/main/examples/openai)
- [Mastra]() Coming soon
- [Mastra](https://github.com/superagent-ai/poker-eval/tree/main/examples/mastra)
- [LlamaIndex](https://github.com/superagent-ai/poker-eval/tree/main/examples/llama-index)
- [Langchain](https://github.com/superagent-ai/poker-eval/tree/main/examples/langchain)

Expand Down
2 changes: 1 addition & 1 deletion examples/ai-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ This examples shows how to integrate an Agent built with the [Vercel AI SDK](htt
3. Run evaluation
```
ts-node index.ts
npm run start
```
3 changes: 2 additions & 1 deletion examples/ai-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Agent evals with Vercel AI SDK",
"main": "index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "ts-node index.ts"
},
"author": "",
"license": "ISC",
Expand Down
2 changes: 1 addition & 1 deletion examples/langchain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ This examples shows how to integrate an Agent built with the [Langchain TS SDK](
3. Run evaluation
```
ts-node index.ts
npm run start
```
3 changes: 2 additions & 1 deletion examples/langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Poker agent evals with Langchain",
"main": "index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "ts-node index.ts"
},
"author": "",
"license": "ISC",
Expand Down
2 changes: 1 addition & 1 deletion examples/llama-index/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ This examples shows how to integrate an Agent built with the [Llama Index TS SDK
3. Run evaluation
```
ts-node index.ts
npm run start
```
3 changes: 2 additions & 1 deletion examples/llama-index/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Poker agent evals with Llama Index",
"main": "index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "ts-node index.ts"
},
"author": "",
"license": "ISC",
Expand Down
1 change: 1 addition & 0 deletions examples/mastra/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_API_KEY=
2 changes: 2 additions & 0 deletions examples/mastra/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
15 changes: 15 additions & 0 deletions examples/mastra/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# PokerEval and Mastr AI SDK

This examples shows how to integrate an Agent built with the [Mastra AI SDK](https://mastra.ai/docs/guide) and PokerEval.

## How to use
1. Install dependencies
```
npm install
```
2. Update environment variables and rename `.env.example` to `.env`
3. Run evaluation
```
npm run start
```
65 changes: 65 additions & 0 deletions examples/mastra/agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { config } from "dotenv";
import { PlayerAction, TableState } from "@superagent-ai/poker-eval/dist/types";
import { Agent } from "@mastra/core";

config();

const pokerAgent = new Agent({
name: "PokerAgent",
instructions: "You are an AI Assistant playing No Limit Texas Holdem.",
model: {
provider: "OPEN_AI",
name: "gpt-4o",
toolChoice: "auto",
},
});

export const generateAction = async (
state: TableState
): Promise<PlayerAction> => {
const prompt = `
You are a poker assistant named ${state.playerName}.
You are playing at a table with ${state.tableInfo.length} players.
Act based on the information below.
Players:
${state.tableInfo
.map(
(player) =>
`${player.name}${player.hasButton ? " (Button)" : ""}, Stack: $${
player.stack
}, Bet size: $${player.betSize}`
)
.join("\n")}
Game stage: ${state.gameStage}
Your cards: ${state.playerCards}
Cards on the table: ${state.tableCards}
Legal actions: ${state.legalActions.actions.join(", ")}
Min bet: $${state.minRaise}
Max bet: $${state.maxRaise}
Considering the hand strength, potential opponent hands, and optimal strategy, what action should the player take? Explain your reasoning.
Make sure to return the correct amount when calling, raising or bettings.
`;

const { object } = await pokerAgent.textObject({
messages: [prompt],
structuredOutput: {
action: {
type: "string",
},
bet: {
type: "number",
},
},
});

const { action, bet } = object;

return {
action,
bet: bet ?? 0,
} as PlayerAction;
};
40 changes: 40 additions & 0 deletions examples/mastra/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { generateAction } from "./agent";

import { PokerGame } from "@superagent-ai/poker-eval";
import { Player, PlayerAction } from "@superagent-ai/poker-eval/dist/types";

async function executeGameSimulation(numHands: number): Promise<void> {
// Setup AI players
const players: Player[] = [
{
name: "GPT 1",
action: async (state): Promise<PlayerAction> => {
const action = await generateAction(state);
return action;
},
},
{
name: "GPT 2",
action: async (state): Promise<PlayerAction> => {
const action = await generateAction(state);
return action;
},
},
];

// Setup a game
const game = new PokerGame(players, {
defaultChipSize: 1000,
smallBlind: 1,
bigBlind: 2,
});

// Set the output director for stats collection
const results = await game.runSimulation(numHands, { outputPath: "./stats" });

console.log(`Simulation completed for ${numHands} hands.`);
console.log("Results:", results);
}

// Execute the function with ts-node index.ts
executeGameSimulation(5).catch(console.error);
Loading

0 comments on commit a4acc69

Please sign in to comment.