File size: 1,747 Bytes
a73e8b4
 
 
 
 
 
 
33ee259
13e7011
a73e8b4
 
 
 
 
 
 
2a6e164
a73e8b4
 
13e7011
a73e8b4
13e7011
a73e8b4
 
 
 
 
9c9d521
2a6e164
a73e8b4
2a6e164
a73e8b4
2a6e164
a73e8b4
2a6e164
a73e8b4
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;

        // 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')) {
            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}&regions=${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 };