Skip to content

Commit

Permalink
feat(nx-dev): request is IncomingMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Aug 23, 2023
1 parent aaa073b commit a2cc439
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 64 deletions.
34 changes: 11 additions & 23 deletions nx-dev/data-access-ai/src/lib/data-access-ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,14 @@ export async function queryAi(
method: 'POST',
body: JSON.stringify({
action: 'moderation',
input: sanitizedQuery,
input: { input: sanitizedQuery },
}),
headers: {
'Content-Type': 'application/json',
},
});

const moderationResponse = await moderationResponseObj.json();

const [results] = moderationResponse.results;

if (results.flagged) {
Expand Down Expand Up @@ -123,23 +122,15 @@ export async function queryAi(
});

if (!embeddingResponseObj.ok) {
throw new Error(
`API call failed with status ${embeddingResponseObj.status}`
);
throw new ApplicationError('Failed to create embedding for question', {
data: embeddingResponseObj.status,
});
}

const embeddingResponse = await embeddingResponseObj.json();

if (embeddingResponse.status !== 200) {
throw new ApplicationError(
'Failed to create embedding for question',
embeddingResponse
);
}

const {
data: [{ embedding }],
}: CreateEmbeddingResponse = embeddingResponse.data;
}: CreateEmbeddingResponse = embeddingResponse;

const { error: matchError, data: pageSections } = await supabaseClient.rpc(
'match_page_sections_2',
Expand Down Expand Up @@ -224,31 +215,28 @@ export async function queryAi(
});

if (!responseObj.ok) {
throw new Error(`API call failed with status ${responseObj.status}`);
throw new ApplicationError('Failed to generate completion', {
data: responseObj.status,
});
}

const response = await responseObj.json();

if (response.status !== 200) {
const error = response.data;
throw new ApplicationError('Failed to generate completion', error);
}

// Message asking to double-check
const callout: string =
'{% callout type="warning" title="Always double-check!" %}The results may not be accurate, so please always double check with our documentation.{% /callout %}\n';
// Append the warning message asking to double-check!
const message = [callout, getMessageFromResponse(response.data)].join('');
const message = [callout, getMessageFromResponse(response)].join('');

const responseWithoutBadLinks = await sanitizeLinksInResponse(message);

const sources = getListOfSources(pageSections);

totalTokensSoFar += response.data.usage?.total_tokens ?? 0;
totalTokensSoFar += response.usage?.total_tokens ?? 0;

return {
textResponse: responseWithoutBadLinks,
usage: response.data.usage as CreateCompletionResponseUsage,
usage: response.usage as CreateCompletionResponseUsage,
sources,
sourcesMarkdown: toMarkdownList(sources),
};
Expand Down
72 changes: 31 additions & 41 deletions nx-dev/nx-dev/pages/api/openai-handler.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,45 @@
import { NextRequest } from 'next/server';
import { Configuration, OpenAIApi } from 'openai';

const openAiKey = process.env['NX_OPENAI_KEY'];
const config = new Configuration({
apiKey: openAiKey,
});
const openai = new OpenAIApi(config);
export const config = {
runtime: 'edge',
};

export default async function handler(request: NextRequest) {
const { action, input } = await request.json();

let apiUrl = 'https://api.openai.com/v1/';

if (action === 'embedding') {
try {
const embeddingResponse = await openai.createEmbedding(input);
return new Response(JSON.stringify(embeddingResponse.data), {
status: embeddingResponse.status,
headers: {
'content-type': 'application/json',
},
});
} catch (e) {
console.error('Error processing the request:', e.message);
return new Response(e.message, { status: 500 });
}
apiUrl += 'embeddings';
} else if (action === 'chatCompletion') {
try {
const chatCompletionResponse = await openai.createChatCompletion(input);
return new Response(JSON.stringify(chatCompletionResponse.data), {
status: chatCompletionResponse.status,
headers: {
'content-type': 'application/json',
},
});
} catch (e) {
console.error('Error processing the request:', e.message);
return new Response(e.message, { status: 500 });
}
apiUrl += 'chat/completions';
} else if (action === 'moderation') {
try {
const moderationResponse = await openai.createModeration(input);
return new Response(JSON.stringify(moderationResponse.data), {
status: moderationResponse.status,
headers: {
'content-type': 'application/json',
},
});
} catch (e) {
console.error('Error processing the request:', e.message);
return new Response(e.message, { status: 500 });
}
apiUrl += 'moderations';
} else {
return new Response('Invalid action', { status: 400 });
}

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) {
console.error('Error processing the request:', e.message);
return new Response(e.message, { status: 500 });
}
}

0 comments on commit a2cc439

Please sign in to comment.