Skip to content

Commit

Permalink
feat: openai example (#9)
Browse files Browse the repository at this point in the history
* feat: bump dependencies and readme

* add citation cff

* small tweaks

* small tweaks to readme

* feat: minor tweaks

* small tweaks
  • Loading branch information
homanp authored Nov 25, 2024
1 parent af9dad1 commit bd0104c
Show file tree
Hide file tree
Showing 10 changed files with 465 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Coming soon...
We've created some examples using populat agent frameworks you can use as inspiration (feel free to contribute):

- [Vercel AI SDK](https://github.com/superagent-ai/poker-eval/tree/main/examples/ai-sdk)
- [OpeaAI]() Coming soon
- [OpenAI](https://github.com/superagent-ai/poker-eval/tree/main/examples/openai)
- [Mastra]() Coming soon
- [LlamaIndex]() Coming soon
- [Langchain]() Coming soon
Expand Down
3 changes: 1 addition & 2 deletions examples/ai-sdk/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { config } from "dotenv";
import { generateObject } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";

import { PlayerAction, TableState } from "../../src/types";
import { PlayerAction, TableState } from "@superagent-ai/poker-eval/dist/types";

config();

Expand Down
1 change: 1 addition & 0 deletions examples/openai/.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/openai/.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/openai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# PokerEval and Vercel AI SDK

This examples shows how to integrate an Agent built with the [Vercel AI SDK](https://github.com/vercel/ai) and PokerEval.

## How to use
1. Install dependencies
```
npm install
```
2. Update environment variables and rename `.env.example` to `.env`
3. Run evaluation
```
ts-node index.ts
```
71 changes: 71 additions & 0 deletions examples/openai/agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { config } from "dotenv";
import OpenAI from "openai";
import { z } from "zod";
import { zodResponseFormat } from "openai/helpers/zod";
import { PlayerAction, TableState } from "@superagent-ai/poker-eval/dist/types";

config();

const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});

const ActionEnum = z
.enum(["bet", "fold", "call", "check", "raise"])
.describe("The action to take");

const responseSchema = z.object({
action: ActionEnum,
bet: z
.number()
.describe("The amount to bet, call, raise, or check")
.optional(),
});

export const generateAction = async (
state: TableState
): Promise<PlayerAction> => {
const ActionEnum = z
.enum(["bet", "fold", "call", "check", "raise"] as const)
.describe("The action to take");

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 completion = await openai.beta.chat.completions.parse({
model: "gpt-4o",
messages: [{ role: "user", content: prompt }],
response_format: zodResponseFormat(responseSchema, "action"),
});

const { action, bet } = completion.choices[0].message.parsed;

return {
action,
bet: bet ?? 0,
} as PlayerAction;
};
40 changes: 40 additions & 0 deletions examples/openai/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 bd0104c

Please sign in to comment.