matt HOFFNER
commited on
Commit
·
9c9d521
1
Parent(s):
13e7011
updates
Browse files- app/api/chat/route.ts +4 -1
- app/tools/odds.ts +3 -3
- app/tools/scores.ts +30 -0
- app/tools/search.ts +1 -1
app/api/chat/route.ts
CHANGED
|
@@ -3,9 +3,11 @@ import { OpenAIStream, StreamingTextResponse } from "ai";
|
|
| 3 |
import { createUrlSurfer } from "@/app/tools/surfer";
|
| 4 |
import { createSearchApi } from "@/app/tools/search";
|
| 5 |
import { createOddsApi } from "@/app/tools/odds";
|
|
|
|
| 6 |
|
| 7 |
const [, urlSurferSchema] = createUrlSurfer();
|
| 8 |
const [, serpApiSchema] = createSearchApi({ apiKey: process.env.SERP_API_KEY || '' });
|
|
|
|
| 9 |
const [, oddsApiSchema] = createOddsApi({ apiKey: process.env.ODDS_API_KEY || '' });
|
| 10 |
|
| 11 |
const config = new Configuration({
|
|
@@ -16,7 +18,8 @@ const openai = new OpenAIApi(config);
|
|
| 16 |
const functions: any[] = [
|
| 17 |
urlSurferSchema,
|
| 18 |
serpApiSchema,
|
| 19 |
-
oddsApiSchema
|
|
|
|
| 20 |
];
|
| 21 |
|
| 22 |
export async function POST(req: Request) {
|
|
|
|
| 3 |
import { createUrlSurfer } from "@/app/tools/surfer";
|
| 4 |
import { createSearchApi } from "@/app/tools/search";
|
| 5 |
import { createOddsApi } from "@/app/tools/odds";
|
| 6 |
+
import { createSportsResultsApi } from "@/app/tools/scores";
|
| 7 |
|
| 8 |
const [, urlSurferSchema] = createUrlSurfer();
|
| 9 |
const [, serpApiSchema] = createSearchApi({ apiKey: process.env.SERP_API_KEY || '' });
|
| 10 |
+
const [, sportsApiResultsSchema] = createSportsResultsApi({ apiKey: process.env.SERP_API_KEY || '' });
|
| 11 |
const [, oddsApiSchema] = createOddsApi({ apiKey: process.env.ODDS_API_KEY || '' });
|
| 12 |
|
| 13 |
const config = new Configuration({
|
|
|
|
| 18 |
const functions: any[] = [
|
| 19 |
urlSurferSchema,
|
| 20 |
serpApiSchema,
|
| 21 |
+
oddsApiSchema,
|
| 22 |
+
sportsApiResultsSchema
|
| 23 |
];
|
| 24 |
|
| 25 |
export async function POST(req: Request) {
|
app/tools/odds.ts
CHANGED
|
@@ -5,7 +5,7 @@ function createOddsApi({ apiKey }: { apiKey: string }) {
|
|
| 5 |
const paramsSchema = z.object({
|
| 6 |
input: z.string(),
|
| 7 |
});
|
| 8 |
-
const name = '
|
| 9 |
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.';
|
| 10 |
|
| 11 |
const execute = async ({ input }: z.infer<typeof paramsSchema>) => {
|
|
@@ -26,12 +26,12 @@ function createOddsApi({ apiKey }: { apiKey: string }) {
|
|
| 26 |
sportKey = 'upcoming';
|
| 27 |
}
|
| 28 |
|
| 29 |
-
if (input.includes('
|
| 30 |
market = 'spread';
|
| 31 |
} else if (input.includes('o/u')) {
|
| 32 |
market = 'totals';
|
| 33 |
} else {
|
| 34 |
-
market = '
|
| 35 |
}
|
| 36 |
const activeSports = await fetch(`https://api.the-odds-api.com/v4/sports/${sportKey}/odds?apiKey=${apiKey}&oddsFormat=${oddsFormat}&dateFormat=${dateFormat}&market=${market}®ions=${regions}`);
|
| 37 |
const oddsResponse = await activeSports.json();
|
|
|
|
| 5 |
const paramsSchema = z.object({
|
| 6 |
input: z.string(),
|
| 7 |
});
|
| 8 |
+
const name = 'sports odds';
|
| 9 |
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.';
|
| 10 |
|
| 11 |
const execute = async ({ input }: z.infer<typeof paramsSchema>) => {
|
|
|
|
| 26 |
sportKey = 'upcoming';
|
| 27 |
}
|
| 28 |
|
| 29 |
+
if (input.includes('money line')) {
|
| 30 |
market = 'spread';
|
| 31 |
} else if (input.includes('o/u')) {
|
| 32 |
market = 'totals';
|
| 33 |
} else {
|
| 34 |
+
market = 'spread';
|
| 35 |
}
|
| 36 |
const activeSports = await fetch(`https://api.the-odds-api.com/v4/sports/${sportKey}/odds?apiKey=${apiKey}&oddsFormat=${oddsFormat}&dateFormat=${dateFormat}&market=${market}®ions=${regions}`);
|
| 37 |
const oddsResponse = await activeSports.json();
|
app/tools/scores.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Tool } from 'openai-function-calling-tools';
|
| 2 |
+
import { z } from 'zod';
|
| 3 |
+
import { getJson } from 'serpapi';
|
| 4 |
+
|
| 5 |
+
function createSportsResultsApi({ apiKey }: { apiKey: string }) {
|
| 6 |
+
const paramsSchema = z.object({
|
| 7 |
+
input: z.string(),
|
| 8 |
+
});
|
| 9 |
+
const name = 'sports results';
|
| 10 |
+
const description = 'A custom search engine for sports scores and results. Useful for when you need to answer questions about current sports scores (like nba or nfl). Input should be a search query. Outputs a JSON array of results.';
|
| 11 |
+
|
| 12 |
+
const execute = async ({ input }: z.infer<typeof paramsSchema>) => {
|
| 13 |
+
try {
|
| 14 |
+
const response = await getJson({
|
| 15 |
+
engine: "google",
|
| 16 |
+
api_key: apiKey,
|
| 17 |
+
q: input,
|
| 18 |
+
location: "Seattle, Washington",
|
| 19 |
+
});
|
| 20 |
+
|
| 21 |
+
return JSON.stringify(response['sports_results']);
|
| 22 |
+
} catch (error) {
|
| 23 |
+
throw new Error(`Error in serpApi: ${error}`);
|
| 24 |
+
}
|
| 25 |
+
};
|
| 26 |
+
|
| 27 |
+
return new Tool(paramsSchema, name, description, execute).tool;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
export { createSportsResultsApi };
|
app/tools/search.ts
CHANGED
|
@@ -6,7 +6,7 @@ function createSearchApi({ apiKey }: { apiKey: string }) {
|
|
| 6 |
const paramsSchema = z.object({
|
| 7 |
input: z.string(),
|
| 8 |
});
|
| 9 |
-
const name = '
|
| 10 |
const description = 'A custom search engine. Useful for when you need to answer questions about current events. Input should be a search query. Outputs a JSON array of results.';
|
| 11 |
|
| 12 |
const execute = async ({ input }: z.infer<typeof paramsSchema>) => {
|
|
|
|
| 6 |
const paramsSchema = z.object({
|
| 7 |
input: z.string(),
|
| 8 |
});
|
| 9 |
+
const name = 'search';
|
| 10 |
const description = 'A custom search engine. Useful for when you need to answer questions about current events. Input should be a search query. Outputs a JSON array of results.';
|
| 11 |
|
| 12 |
const execute = async ({ input }: z.infer<typeof paramsSchema>) => {
|