File size: 1,682 Bytes
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
47
48
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 = 'oddsApi';
  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 sport and a corresponding game. 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 market;

        // americanfootball_nfl_super_bowl_winner
        if (input.includes('nba')) {
            sportKey = 'basketball_nba';
        } else if (input.includes('nfl')) {
            sportKey = 'americanfootball_nfl';
        } else {
            sportKey = 'upcoming';
        }

        if (input.includes('spread')) {
            market = 'spread';
        } else if (input.includes('o/u')) {
            market = 'totals';
        } else {
            market = 'h2h';
        }
        const activeSports = await fetch(`https://api.the-odds-api.com/v4/sports/${sportKey}/odds?apiKey=${apiKey}&oddsFormat=${oddsFormat}&dateFormat=${dateFormat}&market=${market}&regions=${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 };