Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ellipsis] Split GitHub router's admin endpoint into two #58

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/pages/api/diffData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { 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);
}
31 changes: 31 additions & 0 deletions src/pages/api/otherData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}
2 changes: 1 addition & 1 deletion src/pages/forms/fill/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function InnerChat(props: {
setMessages(messagesToSend);
setInputValue('');
setIsWaiting(true);
const assistantResponse = await callLLM(PROMPT_FILL(form), messagesToSend);
const assistantResponse = await callLLM(PROMPT_FILL(form), messagesToSend, '/api/diffData');
if (assistantResponse instanceof Error) {
setError(assistantResponse);
return;
Expand Down
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { Database, Json } from '../types/supabase';

export const callLLM = async (
systemPrompt: string,
messages: ChatMessage[]
messages: ChatMessage[],
endpoint: string
) => {
const data: LLMRequest = {
completion_create: {
Expand All @@ -21,7 +22,7 @@ export const callLLM = async (
messages: [{ role: 'system', content: systemPrompt }, ...messages],
},
};
const response = await fetch('/api/llm', {
const response = await fetch(endpoint, {
method: 'POST',
body: JSON.stringify(data),
headers: {
Expand All @@ -35,7 +36,6 @@ export const callLLM = async (
const json: LLMResponse = await response.json();
return json.completion.choices[0].message;
};

export async function getUserFromSupabase(
session: Session | null,
supabase: SupabaseClient<Database>,
Expand Down Expand Up @@ -136,4 +136,4 @@ export const removeStartAndEndQuotes = (str: string | null) => {
return str;
}
return str.replace(/^"(.*)"$/, '$1');
};
};
Loading