forked from nsbradford/TalkFormAI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathotherData.ts
31 lines (29 loc) · 1.04 KB
/
otherData.ts
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
import { NextApiRequest, NextApiResponse } from 'next';
import OpenAI from 'openai';
import { LLMRequest, LLMResponse } from '../../types';
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: 'https://oai.hconeai.com/v1',
defaultHeaders: {
'Helicone-Cache-Enabled': 'true',
'Helicone-Auth': `Bearer ${process.env.HELICONE_API_KEY}`,
},
});
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<LLMResponse | { error: string }>
) {
const auth = req.headers.authorization;
if (!auth || auth !== `Bearer ${process.env.NEXT_PUBLIC_LLM_API_KEY}`) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
const llmRequest: LLMRequest = req.body;
console.log(`LLM middleware: got request: ${JSON.stringify(llmRequest)}`);
const completion = await openai.chat.completions.create(
llmRequest.completion_create
);
console.log(`LLM middleware: got completion: ${JSON.stringify(completion)}`);
const response: LLMResponse = { completion };
res.status(200).json(response);
}