Skip to content

Commit

Permalink
implement #49;
Browse files Browse the repository at this point in the history
  • Loading branch information
ellipsis-dev[bot] authored Jan 20, 2024
1 parent d7dd976 commit ff2782b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
15 changes: 14 additions & 1 deletion src/pages/api/llm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';
import OpenAI from 'openai';
import { LLMRequest, LLMResponse } from '../../types';
Expand Down Expand Up @@ -30,3 +29,17 @@ export default async function handler(
const response: LLMResponse = { completion };
res.status(200).json(response);
}

export async function getDiffData(
req: NextApiRequest,
res: NextApiResponse<LLMResponse | { error: string }>
) {
// TODO: Implement the logic to get the diff data
}

export async function getAllOtherData(
req: NextApiRequest,
res: NextApiResponse<LLMResponse | { error: string }>
) {
// TODO: Implement the logic to get all other data
}
7 changes: 4 additions & 3 deletions 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, 'getDiffData');
if (assistantResponse instanceof Error) {
setError(assistantResponse);
return;
Expand All @@ -125,7 +125,8 @@ export function InnerChat(props: {
submitResponseToSupabase(
form.id,
parsed.submission,
props.supabase
props.supabase,
'getAllOtherData'
).then((maybeError) => {
setIsDone(true);
setSubmission(parsed.submission);
Expand Down Expand Up @@ -239,4 +240,4 @@ function SubmissionBox(submission: object): React.ReactNode {
</p>
</div>
);
}
}
32 changes: 28 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import { Session, SupabaseClient } from '@supabase/auth-helpers-nextjs';
import { v4 } from 'uuid';
import { Database, Json } from '../types/supabase';

export const callLLM = async (
export const callLLM = async (
systemPrompt: string,
messages: ChatMessage[]
messages: ChatMessage[],
endpoint: string
) => {
const data: LLMRequest = {
completion_create: {
Expand All @@ -21,7 +23,7 @@ export const callLLM = async (
messages: [{ role: 'system', content: systemPrompt }, ...messages],
},
};
const response = await fetch('/api/llm', {
const response = await fetch(`/api/${endpoint}`, {
method: 'POST',
body: JSON.stringify(data),
headers: {
Expand All @@ -35,7 +37,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 @@ -130,10 +131,33 @@ export async function submitResponseToSupabase(
return response;
}
}
export async function submitResponseToSupabase(
formId: string,
responseJson: Json,
supabase: SupabaseClient<Database>,
endpoint: string
): Promise<Response | Error> {
const response: Response = {
id: v4(),
form_id: formId,
fields: responseJson,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
};
console.log('Submitting response to Supabase', { formId, response });
const { error } = await supabase.from('responses').insert(response);

if (error) {
console.error(`Error creating response`, { response, error });
return Error(error.message, { cause: error });
} else {
console.log('Successfully created response', response);
return response;
}
}
export const removeStartAndEndQuotes = (str: string | null) => {
if (!str) {
return str;
}
return str.replace(/^"(.*)"$/, '$1');
};
};

0 comments on commit ff2782b

Please sign in to comment.