| 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<typeof paramsSchema>) => { |
| try { |
| const oddsFormat = 'american'; |
| const dateFormat = 'iso'; |
| const regions = 'us'; |
| let sportKey; |
| let markets; |
|
|
| |
| 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')) { |
| markets = 'spread'; |
| } else if (input.includes('o/u')) { |
| markets = 'totals'; |
| } else { |
| markets = 'spreads'; |
| } |
| const activeSports = await fetch(`https://api.the-odds-api.com/v4/sports/${sportKey}/odds?apiKey=${apiKey}&oddsFormat=${oddsFormat}&dateFormat=${dateFormat}&markets=${markets}®ions=${regions}`); |
| const oddsResponse = await activeSports.json(); |
| return JSON.stringify(oddsResponse); |
| } catch (error) { |
| throw new Error(`Error in oddsApi: ${error}`); |
| } |
| }; |
|
|
| return new Tool(paramsSchema, name, description, execute).tool; |
| } |
|
|
| export { createOddsApi }; |