matt HOFFNER commited on
Commit
7ec7db5
β€’
1 Parent(s): b6fedb1

include coinmarketcap

Browse files
app/api/chat/route.ts CHANGED
@@ -3,10 +3,12 @@ import { OpenAIStream, StreamingTextResponse } from "ai";
3
  import { createSearchApi } from "@/app/tools/search";
4
  import { createOddsApi } from "@/app/tools/odds";
5
  import { createSportsResultsApi } from "@/app/tools/scores";
 
6
 
7
  const [, serpApiSchema] = createSearchApi({ apiKey: process.env.SERP_API_KEY || '' });
8
  const [, sportsApiResultsSchema] = createSportsResultsApi({ apiKey: process.env.SERP_API_KEY || '' });
9
  const [, oddsApiSchema] = createOddsApi({ apiKey: process.env.ODDS_API_KEY || '' });
 
10
 
11
  const config = new Configuration({
12
  apiKey: process.env.OPENAI_API_KEY,
@@ -16,7 +18,8 @@ const openai = new OpenAIApi(config);
16
  const functions: any[] = [
17
  serpApiSchema,
18
  oddsApiSchema,
19
- sportsApiResultsSchema
 
20
  ];
21
 
22
  export async function POST(req: Request) {
 
3
  import { createSearchApi } from "@/app/tools/search";
4
  import { createOddsApi } from "@/app/tools/odds";
5
  import { createSportsResultsApi } from "@/app/tools/scores";
6
+ import { createCoinMarketCapApi } from "@/app/tools/coin";
7
 
8
  const [, serpApiSchema] = createSearchApi({ apiKey: process.env.SERP_API_KEY || '' });
9
  const [, sportsApiResultsSchema] = createSportsResultsApi({ apiKey: process.env.SERP_API_KEY || '' });
10
  const [, oddsApiSchema] = createOddsApi({ apiKey: process.env.ODDS_API_KEY || '' });
11
+ const [, coinMarketCapApiSchema] = createCoinMarketCapApi({ apiKey: process.env.COINMARKETCAP_API_KEY || '' });
12
 
13
  const config = new Configuration({
14
  apiKey: process.env.OPENAI_API_KEY,
 
18
  const functions: any[] = [
19
  serpApiSchema,
20
  oddsApiSchema,
21
+ sportsApiResultsSchema,
22
+ coinMarketCapApiSchema
23
  ];
24
 
25
  export async function POST(req: Request) {
app/tools/coin.ts ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Tool } from 'openai-function-calling-tools';
2
+ import { z } from 'zod';
3
+
4
+ function createCoinMarketCapApi({ apiKey }: { apiKey: string }) {
5
+ const paramsSchema = z.object({
6
+ input: z.string(), // Assuming this is used to pass query parameters, though you might want to define these more specifically
7
+ });
8
+ const name = 'coinmarketcap';
9
+ const description = 'A realtime cryptocurrency API. Useful for when you need to answer questions about latest crypto';
10
+
11
+ const execute = async ({ input }: z.infer<typeof paramsSchema>) => {
12
+ try {
13
+ // Parse the input to get parameters, or define them directly if not using 'input' for this
14
+ // For demonstration, I'm using static parameters that you might want to customize
15
+ const queryParams = new URLSearchParams({
16
+ start: '1',
17
+ limit: '5000',
18
+ convert: 'USD'
19
+ });
20
+
21
+ const response = await fetch(`https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?${queryParams}`, {
22
+ method: 'GET', // Method is GET by default for fetch, specified here for clarity
23
+ headers: {
24
+ 'X-CMC_PRO_API_KEY': apiKey, // Use the passed apiKey for authentication
25
+ 'Accept': 'application/json',
26
+ },
27
+ });
28
+
29
+ if (!response.ok) {
30
+ throw new Error('Network response was not ok');
31
+ }
32
+
33
+ const data = await response.json();
34
+ return JSON.stringify(data);
35
+ } catch (error) {
36
+ throw new Error(`Error in coinMarketCapApi: ${error}`);
37
+ }
38
+ };
39
+
40
+ return new Tool(paramsSchema, name, description, execute).tool;
41
+ }
42
+
43
+ export { createCoinMarketCapApi };
pages/api/functions/embed.ts CHANGED
@@ -2,8 +2,7 @@ import { createSearchApi } from '../../../app/tools/search';
2
  import { createOddsApi } from '@/app/tools/odds';
3
  import { createSportsResultsApi } from '@/app/tools/scores';
4
  import { similaritySearch } from './vector-store';
5
-
6
- const urlRegex = /(https?:\/\/[^\s]+)/g;
7
 
8
  const [serpApi] =
9
  createSearchApi({
@@ -13,6 +12,8 @@ const [serpApi] =
13
  const [sportsScoresApi] = createSportsResultsApi({ apiKey: process.env.SERP_API_KEY || "",});
14
 
15
  const [oddsApi] = createOddsApi({ apiKey: process.env.ODDS_API_KEY || "" });
 
 
16
  type FunctionOutput = any;
17
  type FunctionInput = any;
18
 
@@ -32,3 +33,9 @@ export const sports: FunctionOutput = async ({ input }: FunctionInput) => {
32
  const content: string = await sportsScoresApi({input})
33
  return content;
34
  }
 
 
 
 
 
 
 
2
  import { createOddsApi } from '@/app/tools/odds';
3
  import { createSportsResultsApi } from '@/app/tools/scores';
4
  import { similaritySearch } from './vector-store';
5
+ import { createCoinMarketCapApi } from '@/app/tools/coin';
 
6
 
7
  const [serpApi] =
8
  createSearchApi({
 
12
  const [sportsScoresApi] = createSportsResultsApi({ apiKey: process.env.SERP_API_KEY || "",});
13
 
14
  const [oddsApi] = createOddsApi({ apiKey: process.env.ODDS_API_KEY || "" });
15
+
16
+ const [coinMarketCapApi] = createCoinMarketCapApi({ apiKey: process.env.COINMARKETCAP_API_KEY || "" });
17
  type FunctionOutput = any;
18
  type FunctionInput = any;
19
 
 
33
  const content: string = await sportsScoresApi({input})
34
  return content;
35
  }
36
+
37
+
38
+ export const coin: FunctionOutput = async ({ input }: FunctionInput) => {
39
+ const content: string = await coinMarketCapApi({input})
40
+ return content;
41
+ }
pages/api/functions/index.ts CHANGED
@@ -1,5 +1,5 @@
1
  import { NextApiRequest, NextApiResponse } from 'next';
2
- import { odds, serp, sports } from './embed';
3
 
4
  export const config = {
5
  api: {
@@ -14,7 +14,8 @@ type FunctionHandler = any;
14
  const handlers: FunctionHandler = {
15
  'search': serp,
16
  'sports_odds': odds,
17
- 'sports_results': sports
 
18
  };
19
 
20
  export default async function handler(req: NextApiRequest, res: NextApiResponse) {
 
1
  import { NextApiRequest, NextApiResponse } from 'next';
2
+ import { odds, serp, sports, coin } from './embed';
3
 
4
  export const config = {
5
  api: {
 
14
  const handlers: FunctionHandler = {
15
  'search': serp,
16
  'sports_odds': odds,
17
+ 'sports_results': sports,
18
+ 'coinmarketcap': coin
19
  };
20
 
21
  export default async function handler(req: NextApiRequest, res: NextApiResponse) {