File size: 1,479 Bytes
640c3b0
c2df9c2
caaca78
a73e8b4
9c9d521
7ec7db5
c2df9c2
caaca78
9c9d521
a73e8b4
7ec7db5
c2df9c2
 
a73e8b4
9c9d521
7ec7db5
 
c2df9c2
 
 
e842b6d
 
66a7625
e842b6d
 
c2df9c2
 
640c3b0
5ea401a
c2df9c2
e861798
 
 
 
 
3bee3c0
c2df9c2
 
 
 
 
 
 
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
import OpenAI from "openai";
import { OpenAIStream, StreamingTextResponse } from "ai";
import { createSearchApi } from "@/app/tools/search";
import { createOddsApi } from "@/app/tools/odds";
import { createSportsResultsApi } from "@/app/tools/scores";
import { createCoinMarketCapApi } from "@/app/tools/coin";

const [, serpApiSchema] = createSearchApi({ apiKey: process.env.SERP_API_KEY || '' });
const [, sportsApiResultsSchema] = createSportsResultsApi({ apiKey: process.env.SERP_API_KEY || '' });
const [, oddsApiSchema] = createOddsApi({ apiKey: process.env.ODDS_API_KEY || '' });
const [, coinMarketCapApiSchema] = createCoinMarketCapApi({ apiKey: process.env.COINMARKETCAP_API_KEY || '' });

const functions: any[] = [
  serpApiSchema,
  oddsApiSchema,
  sportsApiResultsSchema,
  coinMarketCapApiSchema
];

export async function POST(req: Request) {
  const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY,
    baseURL: "https://api.mistral.ai/v1"
  });

  const { messages, function_call } = await req.json()

  const response = await openai.chat.completions.create({
    model: 'mistral-large-latest',
    stream: true,
    messages: messages
      .map(message => ({
        // map "assistant" to "agent" for Mistral:
        role: message.role === 'assistant' ? 'agent' : 'user',
        content: message.content,
    }) as any),
    functions,
    function_call
  })

  const stream = OpenAIStream(response)
  return new StreamingTextResponse(stream)
}