From 4079f203483ca9bf38052b641bb7d46d7e593c64 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Fri, 25 Aug 2023 12:35:03 +0300 Subject: [PATCH] feat(nx-dev): fix response format --- .../data-access-ai/src/lib/data-access-ai.ts | 28 ++++++------------- nx-dev/nx-dev/pages/api/query-ai-handler.ts | 26 ++++++++--------- nx-dev/util-ai/src/lib/openai-call.ts | 23 --------------- 3 files changed, 20 insertions(+), 57 deletions(-) diff --git a/nx-dev/data-access-ai/src/lib/data-access-ai.ts b/nx-dev/data-access-ai/src/lib/data-access-ai.ts index 737a3e3d5dc7b1..0991abc9186350 100644 --- a/nx-dev/data-access-ai/src/lib/data-access-ai.ts +++ b/nx-dev/data-access-ai/src/lib/data-access-ai.ts @@ -10,15 +10,10 @@ import { CreateCompletionResponseUsage } from 'openai'; import { MAX_HISTORY_LENGTH, ChatItem } from '@nx/nx-dev/util-ai'; import { getChatResponse } from './utils'; -const supabaseUrl = process.env['NX_NEXT_PUBLIC_SUPABASE_URL']; -const supabaseServiceKey = process.env['NX_SUPABASE_SERVICE_ROLE_KEY']; - let chatFullHistory: ChatItem[] = []; let totalTokensSoFar = 0; -let supabaseClient: SupabaseClient; - export async function queryAi( query: string, aiResponse?: string @@ -28,13 +23,6 @@ export async function queryAi( sources: { heading: string; url: string }[]; sourcesMarkdown: string; }> { - if (!supabaseClient) { - supabaseClient = createClient( - supabaseUrl as string, - supabaseServiceKey as string - ); - } - if (chatFullHistory.length > MAX_HISTORY_LENGTH) { chatFullHistory.slice(0, MAX_HISTORY_LENGTH - 4); } @@ -47,7 +35,6 @@ export async function queryAi( ); if (!responseObj.ok) { - console.error('Error KATERINA', responseObj.statusText); throw new Error(responseObj.statusText); } @@ -65,6 +52,7 @@ export async function queryAi( return response; } catch (e) { + // TODO(katerina): Fix this to show the actual error console.error('Error in fetch', e); throw e; } @@ -79,16 +67,18 @@ export function getHistory(): ChatItem[] { return chatFullHistory; } +//TODO(katerina): move this to the edge function export async function sendFeedbackAnalytics(feedback: {}): Promise< PostgrestSingleResponse > { - return supabaseClient.from('feedback').insert(feedback); + return {} as any; + // return supabaseClient.from('feedback').insert(feedback); } +//TODO(katerina): move this to the edge function export async function sendQueryAnalytics(queryInfo: {}) { - const { error } = await supabaseClient.from('user_queries').insert(queryInfo); - - if (error) { - console.error('Error storing the query info in Supabase: ', error); - } + // const { error } = await supabaseClient.from('user_queries').insert(queryInfo); + // if (error) { + // console.error('Error storing the query info in Supabase: ', error); + // } } diff --git a/nx-dev/nx-dev/pages/api/query-ai-handler.ts b/nx-dev/nx-dev/pages/api/query-ai-handler.ts index a3042f430da451..faf12c19cbbbf1 100644 --- a/nx-dev/nx-dev/pages/api/query-ai-handler.ts +++ b/nx-dev/nx-dev/pages/api/query-ai-handler.ts @@ -1,7 +1,6 @@ import { NextRequest } from 'next/server'; import { ApplicationError, - ChatItem, DEFAULT_MATCH_COUNT, DEFAULT_MATCH_THRESHOLD, MIN_CONTENT_LENGTH, @@ -30,17 +29,8 @@ export const config = { runtime: 'edge', }; -export default async function handler(request: NextRequest): Promise< - | { - textResponse: string; - usage?: CreateCompletionResponseUsage; - sources: { heading: string; url: string }[]; - sourcesMarkdown: string; - chatHistory: ChatItem[]; - requestTokens: number; - } - | undefined -> { +export default async function handler(request: NextRequest) { + checkEnvVariables(openAiKey, supabaseUrl, supabaseServiceKey); const { query, aiResponse, chatFullHistory } = await request.json(); // This does not make sense since Edge functions are containerized? @@ -52,14 +42,13 @@ export default async function handler(request: NextRequest): Promise< } try { - checkEnvVariables(openAiKey, supabaseUrl, supabaseServiceKey); - if (!query) { throw new UserError('Missing query in request data'); } // Moderate the content to comply with OpenAI T&C const sanitizedQuery = query.trim(); + await moderateContent(sanitizedQuery, openAiKey as string); // Create embedding from query @@ -172,7 +161,7 @@ export default async function handler(request: NextRequest): Promise< const sources = getListOfSources(pageSections); - return { + const responseData = { textResponse: responseWithoutBadLinks, usage: response.usage as CreateCompletionResponseUsage, sources, @@ -180,6 +169,12 @@ export default async function handler(request: NextRequest): Promise< chatHistory, requestTokens: response.usage?.total_tokens, }; + return new Response(JSON.stringify(responseData), { + status: 200, + headers: { + 'content-type': 'application/json', + }, + }); } catch (err: unknown) { if (err instanceof UserError) { console.error(err.message); @@ -192,6 +187,7 @@ export default async function handler(request: NextRequest): Promise< } // TODO: include more response info in debug environments + // OR RETURN RESPONSE WITH DIFFERENT ERROR STATUS console.error(err); throw err; } diff --git a/nx-dev/util-ai/src/lib/openai-call.ts b/nx-dev/util-ai/src/lib/openai-call.ts index c9ff9597e9f124..9916848ce52483 100644 --- a/nx-dev/util-ai/src/lib/openai-call.ts +++ b/nx-dev/util-ai/src/lib/openai-call.ts @@ -23,27 +23,4 @@ export async function openAiAPICall( }, body: JSON.stringify(input), }); - - // try { - // const response = await fetch(apiUrl, { - // method: 'POST', - // headers: { - // Authorization: `Bearer ${openAiKey}`, - // 'Content-Type': 'application/json', - // }, - // body: JSON.stringify(input), - // }); - - // const responseData = await response.json(); - - // return new Response(JSON.stringify(responseData), { - // status: response.status, - // headers: { - // 'content-type': 'application/json', - // }, - // }); - // } catch (e: any) { - // console.error('Error processing the request:', e.message); - // return new Response(e.message, { status: 500 }); - // } }