import { Tool } from 'openai-function-calling-tools'; import { z } from 'zod'; function createOddsApi({ apiKey }: { apiKey: string }) { const paramsSchema = z.object({ input: z.string(), }); const name = 'sports_odds'; const description = 'A realtime Sports Odds API. Useful for when you need to answer questions about sports odds, currently NBA and NFL. Input should be a string including a sport (nba or nfl), optionally including type (h2h, spread or o/u). Outputs a JSON array of results.'; const execute = async ({ input }: z.infer) => { try { const oddsFormat = 'american'; const dateFormat = 'iso'; const regions = 'us'; let sportKey; let market; // americanfootball_nfl_super_bowl_winner if (input.toLowerCase().includes('nba')) { sportKey = 'basketball_nba'; } else if (input.toLowerCase().includes('nfl')) { sportKey = 'americanfootball_nfl'; } else { sportKey = 'upcoming'; } if (input.includes('money line')) { market = 'spread'; } else if (input.includes('o/u')) { market = 'totals'; } else { market = 'spread'; } const activeSports = await fetch(`https://api.the-odds-api.com/v4/sports/${sportKey}/odds?apiKey=${apiKey}&oddsFormat=${oddsFormat}&dateFormat=${dateFormat}&market=${market}®ions=${regions}`); const oddsResponse = await activeSports.json(); console.log(oddsResponse); return JSON.stringify(oddsResponse); } catch (error) { throw new Error(`Error in oddsApi: ${error}`); } }; return new Tool(paramsSchema, name, description, execute).tool; } export { createOddsApi };