-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from webdevcody/feat-single-prompt
Move prompt/checks to models/index.ts
- Loading branch information
Showing
3 changed files
with
70 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,75 @@ | ||
import { gemini15pro } from "./gemini-1.5-pro"; | ||
import { gpt4o } from "./gpt-4o"; | ||
|
||
export interface ModelResult { | ||
solution: string[][]; | ||
export type ModelHandler = ( | ||
prompt: string, | ||
map: string[][], | ||
) => Promise<{ | ||
boxCoordinates: number[][]; | ||
playerCoordinates: number[]; | ||
reasoning: string; | ||
} | ||
}>; | ||
|
||
const prompt = `You're given a 2d grid of nums such that. | ||
" " represents an empty space. | ||
"Z" represents a zombie. Zombies move one Manhattan step every turn and aim to reach the player. | ||
"R" represents rocks, which players can shoot over but zombies cannot pass through or break. | ||
"P" represents the player, who cannot move. The player's goal is to shoot and kill zombies before they reach them. | ||
"B" represents blocks that can be placed before the round begins to hinder the zombies. | ||
Your goal is to place the player ("P") in a location which maximize the player's survival. | ||
You must place two blocks ("B") in locations which maximize the player's survival. | ||
You can shoot any zombie regardless of where it is on the grid. | ||
Returning a 2d grid with the player and blocks placed in the optimal locations, with the coordinates player ("P") and the blocks ("B"), also provide reasoning for the choices. | ||
Zombies can only move horizontally or vertically, not diagonally. | ||
You can't replace rocks R or zombies Z with blocks. | ||
Players will always shoot at the closest zombie each turn. | ||
If there is no room to place a block, do not place any.`; | ||
|
||
export async function runModel( | ||
modelId: string, | ||
map: string[][], | ||
): Promise<ModelResult> { | ||
): Promise<{ | ||
solution: string[][]; | ||
reasoning: string; | ||
}> { | ||
let result; | ||
|
||
switch (modelId) { | ||
case "gemini-1.5-pro": { | ||
return gemini15pro(map); | ||
result = await gemini15pro(prompt, map); | ||
break; | ||
} | ||
case "gpt-4o": { | ||
return gpt4o(map); | ||
result = await gpt4o(prompt, map); | ||
break; | ||
} | ||
default: { | ||
throw new Error(`Tried running unknown model '${modelId}'`); | ||
} | ||
} | ||
|
||
const originalMap = JSON.parse(JSON.stringify(map)); | ||
const [playerRow, playerCol] = result.playerCoordinates; | ||
|
||
if (originalMap[playerRow][playerCol] !== " ") { | ||
throw new Error("Cannot place player in a non-empty space"); | ||
} | ||
|
||
originalMap[playerRow][playerCol] = "P"; | ||
|
||
for (const block of result.boxCoordinates) { | ||
const [blockRow, blockCol] = block; | ||
|
||
if (originalMap[blockRow][blockCol] !== " ") { | ||
throw new Error("Cannot place block in a non-empty space"); | ||
} | ||
|
||
originalMap[blockRow][blockCol] = "B"; | ||
} | ||
|
||
return { | ||
solution: originalMap, | ||
reasoning: result.reasoning, | ||
}; | ||
} |