Skip to content

Commit

Permalink
feat(nx-dev): fix response format
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Aug 28, 2023
1 parent fbef03a commit 56e13fe
Show file tree
Hide file tree
Showing 5 changed files with 28,957 additions and 67 deletions.
10 changes: 0 additions & 10 deletions nx-dev/data-access-ai/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@
"sourceRoot": "nx-dev/data-access-ai/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/nx-dev/data-access-ai",
"main": "nx-dev/data-access-ai/src/index.ts",
"tsConfig": "nx-dev/data-access-ai/tsconfig.lib.json",
"assets": ["nx-dev/data-access-ai/*.md"]
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
Expand Down
28 changes: 9 additions & 19 deletions nx-dev/data-access-ai/src/lib/data-access-ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any, 'public', any>;

export async function queryAi(
query: string,
aiResponse?: string
Expand All @@ -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);
}
Expand All @@ -47,7 +35,6 @@ export async function queryAi(
);

if (!responseObj.ok) {
console.error('Error KATERINA', responseObj.statusText);
throw new Error(responseObj.statusText);
}

Expand All @@ -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;
}
Expand All @@ -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<null>
> {
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);
// }
}
26 changes: 11 additions & 15 deletions nx-dev/nx-dev/pages/api/query-ai-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { NextRequest } from 'next/server';
import {
ApplicationError,
ChatItem,
DEFAULT_MATCH_COUNT,
DEFAULT_MATCH_THRESHOLD,
MIN_CONTENT_LENGTH,
Expand Down Expand Up @@ -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?
Expand All @@ -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
Expand Down Expand Up @@ -172,14 +161,20 @@ export default async function handler(request: NextRequest): Promise<

const sources = getListOfSources(pageSections);

return {
const responseData = {
textResponse: responseWithoutBadLinks,
usage: response.usage as CreateCompletionResponseUsage,
sources,
sourcesMarkdown: toMarkdownList(sources),
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);
Expand All @@ -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;
}
Expand Down
23 changes: 0 additions & 23 deletions nx-dev/util-ai/src/lib/openai-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
// }
}
Loading

0 comments on commit 56e13fe

Please sign in to comment.