diff --git a/nx-dev/data-access-ai/project.json b/nx-dev/data-access-ai/project.json index 804ace199f51f7..346c09e383fda3 100644 --- a/nx-dev/data-access-ai/project.json +++ b/nx-dev/data-access-ai/project.json @@ -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}"], 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 06d0571f5e4ce2..0e9ce7fccb33ea 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 @@ -1,46 +1,11 @@ -// based on: -// https://github.com/supabase-community/nextjs-openai-doc-search/blob/main/pages/api/vector-search.ts - -import { - PostgrestSingleResponse, - SupabaseClient, - createClient, -} from '@supabase/supabase-js'; -import GPT3Tokenizer from 'gpt3-tokenizer'; -import { CreateEmbeddingResponse, CreateCompletionResponseUsage } from 'openai'; -import { - ApplicationError, - ChatItem, - PageSection, - UserError, - checkEnvVariables, - getListOfSources, - getMessageFromResponse, - initializeChat, - openAiCall, - sanitizeLinksInResponse, - toMarkdownList, -} from './utils'; - -const DEFAULT_MATCH_THRESHOLD = 0.78; -const DEFAULT_MATCH_COUNT = 15; -const MIN_CONTENT_LENGTH = 50; - -// This limits history to 30 messages back and forth -// It's arbitrary, but also generous -// History length should be based on token count -// This is a temporary solution -const MAX_HISTORY_LENGTH = 30; - -const supabaseUrl = process.env['NX_NEXT_PUBLIC_SUPABASE_URL']; -const supabaseServiceKey = process.env['NX_SUPABASE_SERVICE_ROLE_KEY']; +import { CreateCompletionResponseUsage } from 'openai'; +import { MAX_HISTORY_LENGTH, ChatItem } from '@nx/nx-dev/util-ai'; +import { getChatResponse } from './utils'; let chatFullHistory: ChatItem[] = []; let totalTokensSoFar = 0; -let supabaseClient: SupabaseClient; - export async function queryAi( query: string, aiResponse?: string @@ -50,194 +15,37 @@ 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); } try { - checkEnvVariables(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(); - const moderationResponseObj = await openAiCall( - { input: sanitizedQuery }, - 'moderation' - ); - - const moderationResponse = await moderationResponseObj.json(); - const [results] = moderationResponse.results; - - if (results.flagged) { - throw new UserError('Flagged content', { - flagged: true, - categories: results.categories, - }); - } - - // Create embedding from query - // NOTE: Here, we may or may not want to include the previous AI response - /** - * For retrieving relevant Nx documentation sections via embeddings, it's a design decision. - * Including the prior response might give more contextually relevant sections, - * but just sending the query might suffice for many cases. - * - * We can experiment with this. - * - * How the solution looks like with previous response: - * - * const embeddingResponse = await openAiCall( - * { input: sanitizedQuery + aiResponse }, - * 'embedding' - * ); - * - * This costs more tokens, so if we see costs skyrocket we remove it. - * As it says in the docs, it's a design decision, and it may or may not really improve results. - */ - const embeddingResponseObj = await openAiCall( - { input: sanitizedQuery + aiResponse, model: 'text-embedding-ada-002' }, - 'embedding' - ); - - if (!embeddingResponseObj.ok) { - throw new ApplicationError('Failed to create embedding for question', { - data: embeddingResponseObj.status, - }); - } - - const embeddingResponse = await embeddingResponseObj.json(); - const { - data: [{ embedding }], - }: CreateEmbeddingResponse = embeddingResponse; - - const { error: matchError, data: pageSections } = await supabaseClient.rpc( - 'match_page_sections_2', - { - embedding, - match_threshold: DEFAULT_MATCH_THRESHOLD, - match_count: DEFAULT_MATCH_COUNT, - min_content_length: MIN_CONTENT_LENGTH, - } - ); - - if (matchError) { - throw new ApplicationError('Failed to match page sections', matchError); - } - - // Note: this is experimental. I think it should work - // mainly because we're testing previous response + query. - if (!pageSections || pageSections.length === 0) { - throw new UserError('No results found.', { no_results: true }); - } - - const tokenizer = new GPT3Tokenizer({ type: 'gpt3' }); - let tokenCount = 0; - let contextText = ''; - - for (let i = 0; i < (pageSections as PageSection[]).length; i++) { - const pageSection: PageSection = pageSections[i]; - const content = pageSection.content; - const encoded = tokenizer.encode(content); - tokenCount += encoded.text.length; - - if (tokenCount >= 2500) { - break; - } - - contextText += `${content.trim()}\n---\n`; - } - - const prompt = ` - ${` - You are a knowledgeable Nx representative. - Your knowledge is based entirely on the official Nx Documentation. - You can answer queries using ONLY that information. - You cannot answer queries using your own knowledge or experience. - Answer in markdown format. Always give an example, answer as thoroughly as you can, and - always provide a link to relevant documentation - on the https://nx.dev website. All the links you find or post - that look like local or relative links, always prepend with "https://nx.dev". - Your answer should be in the form of a Markdown article - (including related code snippets if available), much like the - existing Nx documentation. Mark the titles and the subsections with the appropriate markdown syntax. - If you are unsure and cannot find an answer in the Nx Documentation, say - "Sorry, I don't know how to help with that. You can visit the [Nx documentation](https://nx.dev/getting-started/intro) for more info." - Remember, answer the question using ONLY the information provided in the Nx Documentation. - ` - .replace(/\s+/g, ' ') - .trim()} - `; - - const { chatMessages: chatGptMessages, chatHistory } = initializeChat( - chatFullHistory, + const responseObj = await getChatResponse( query, - contextText, - prompt, + chatFullHistory, aiResponse ); - chatFullHistory = chatHistory; - - const responseObj = await openAiCall( - { - model: 'gpt-3.5-turbo-16k', - messages: chatGptMessages, - temperature: 0, - stream: false, - }, - 'chatCompletion' - ); - if (!responseObj.ok) { - throw new ApplicationError('Failed to generate completion', { - data: responseObj.status, - }); + throw await responseObj.json(); } - const response = await responseObj.json(); - - // 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)].join(''); - - const responseWithoutBadLinks = await sanitizeLinksInResponse(message); + const response: { + textResponse: string; + usage?: CreateCompletionResponseUsage; + sources: { heading: string; url: string }[]; + sourcesMarkdown: string; + chatHistory: ChatItem[]; + requestTokens: number; + } = await responseObj.json(); - const sources = getListOfSources(pageSections); - - totalTokensSoFar += response.usage?.total_tokens ?? 0; - - return { - textResponse: responseWithoutBadLinks, - usage: response.usage as CreateCompletionResponseUsage, - sources, - sourcesMarkdown: toMarkdownList(sources), - }; - } catch (err: unknown) { - if (err instanceof UserError) { - console.error(err.message); - } else if (err instanceof ApplicationError) { - // Print out application errors with their additional data - console.error(`${err.message}: ${JSON.stringify(err.data)}`); - } else { - // Print out unexpected errors as is to help with debugging - console.error(err); - } + totalTokensSoFar += response.requestTokens; + chatFullHistory = response.chatHistory; - // TODO: include more response info in debug environments - console.error(err); - throw err; + return response; + } catch (e: any) { + console.error('Error: ', e?.['message'] || e); + throw e; } } @@ -249,17 +57,3 @@ export function resetHistory() { export function getHistory(): ChatItem[] { return chatFullHistory; } - -export async function sendFeedbackAnalytics(feedback: {}): Promise< - PostgrestSingleResponse -> { - return supabaseClient.from('feedback').insert(feedback); -} - -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); - } -} diff --git a/nx-dev/data-access-ai/src/lib/utils.ts b/nx-dev/data-access-ai/src/lib/utils.ts index 1a6cbbe95472d4..caa7516b7c5f92 100644 --- a/nx-dev/data-access-ai/src/lib/utils.ts +++ b/nx-dev/data-access-ai/src/lib/utils.ts @@ -1,184 +1,6 @@ -import { - ChatCompletionRequestMessageRoleEnum, - CreateChatCompletionResponse, -} from 'openai'; +import { ChatCompletionRequestMessageRoleEnum } from 'openai'; import { getHistory } from './data-access-ai'; -export interface PageSection { - id: number; - page_id: number; - content: string; - heading: string; - similarity: number; - slug: string; - url_partial: string | null; -} - -export function getMessageFromResponse( - response: CreateChatCompletionResponse -): string { - return response.choices[0].message?.content ?? ''; -} - -export function getListOfSources( - pageSections: PageSection[] -): { heading: string; url: string }[] { - const uniqueUrlPartials = new Set(); - const result = pageSections - .filter((section) => { - if (section.url_partial && !uniqueUrlPartials.has(section.url_partial)) { - uniqueUrlPartials.add(section.url_partial); - return true; - } - return false; - }) - .map((section) => { - const url = new URL('https://nx.dev'); - url.pathname = section.url_partial as string; - if (section.slug) { - url.hash = section.slug; - } - return { - heading: section.heading, - url: url.toString(), - }; - }); - - return result; -} - -export function toMarkdownList( - sections: { heading: string; url: string }[] -): string { - return sections - .map((section) => `- [${section.heading}](${section.url})`) - .join('\n'); -} - -export async function sanitizeLinksInResponse( - response: string -): Promise { - const regex = /https:\/\/nx\.dev[^) \n]*[^).]/g; - const urls = response.match(regex); - - if (urls) { - for (const url of urls) { - const linkIsWrong = await is404(url); - if (linkIsWrong) { - response = response.replace( - url, - 'https://nx.dev/getting-started/intro' - ); - } - } - } - - return response; -} - -async function is404(url: string): Promise { - try { - const response = await fetch(url.replace('https://nx.dev', '')); - if (response.status === 404) { - return true; - } else { - return false; - } - } catch (error) { - if ((error as any)?.response?.status === 404) { - return true; - } else { - return false; - } - } -} - -export function checkEnvVariables( - supabaseUrl?: string, - supabaseServiceKey?: string -) { - if (!supabaseUrl) { - throw new ApplicationError( - 'Missing environment variable NX_NEXT_PUBLIC_SUPABASE_URL' - ); - } - if (!supabaseServiceKey) { - throw new ApplicationError( - 'Missing environment variable NX_SUPABASE_SERVICE_ROLE_KEY' - ); - } -} - -export class ApplicationError extends Error { - public type: string = 'application_error'; - constructor(message: string, public data: Record = {}) { - super(message); - } -} - -export class UserError extends ApplicationError { - public override type: string = 'user_error'; - constructor(message: string, data: Record = {}) { - super(message, data); - } -} - -/** - * Initializes a chat session by generating the initial chat messages based on the given parameters. - * - * @param {ChatItem[]} chatFullHistory - The full chat history. - * @param {string} query - The user's query. - * @param {string} contextText - The context text or Nx Documentation. - * @param {string} prompt - The prompt message displayed to the user. - * @param {string} [aiResponse] - The AI assistant's response. - * @returns {Object} - An object containing the generated chat messages and updated chat history. - * - chatMessages: An array of chat messages for the chat session. - * - chatHistory: The updated chat history. - */ -export function initializeChat( - chatFullHistory: ChatItem[], - query: string, - contextText: string, - prompt: string, - aiResponse?: string -): { chatMessages: ChatItem[]; chatHistory: ChatItem[] } { - const finalQuery = ` - You will be provided the Nx Documentation. - Answer my message provided by following the approach below: - - - Step 1: Identify CLUES (keywords, phrases, contextual information, references) in the input that you could use to generate an answer. - - Step 2: Deduce the diagnostic REASONING process from the premises (clues, question), relying ONLY on the information provided in the Nx Documentation. If you recognize vulgar language, answer the question if possible, and educate the user to stay polite. - - Step 3: EVALUATE the reasoning. If the reasoning aligns with the Nx Documentation, accept it. Do not use any external knowledge or make assumptions outside of the provided Nx documentation. If the reasoning doesn't strictly align with the Nx Documentation or relies on external knowledge or inference, reject it and answer with the exact string: - "Sorry, I don't know how to help with that. You can visit the [Nx documentation](https://nx.dev/getting-started/intro) for more info." - - Final Step: You can also rely on the messages we have exchanged so far. Do NOT reveal the approach to the user. - Nx Documentation: - ${contextText} - - ---- My message: ${query} - `; - let chatGptMessages: ChatItem[] = []; - let messages: ChatItem[] = []; - - if (chatFullHistory.length > 0) { - messages = [ - { - role: ChatCompletionRequestMessageRoleEnum.Assistant, - content: aiResponse ?? '', - }, - { role: ChatCompletionRequestMessageRoleEnum.User, content: finalQuery }, - ]; - chatGptMessages = [...chatFullHistory, ...messages]; - } else { - messages = [ - { role: ChatCompletionRequestMessageRoleEnum.System, content: prompt }, - { role: ChatCompletionRequestMessageRoleEnum.User, content: finalQuery }, - ]; - chatGptMessages = [...messages]; - } - - chatFullHistory.push(...messages); - - return { chatMessages: chatGptMessages, chatHistory: chatFullHistory }; -} +import { ChatItem } from '@nx/nx-dev/util-ai'; export function extractQuery(text: string) { const regex = /---- My message: (.+)/; @@ -203,21 +25,18 @@ export function getProcessedHistory(): ChatItem[] { return history; } -export interface ChatItem { - role: ChatCompletionRequestMessageRoleEnum; - content: string; -} - -export function openAiCall( - input: object, - action: 'moderation' | 'embedding' | 'chatCompletion' -) { - return fetch('/api/openai-handler', { +export function getChatResponse( + query: string, + chatFullHistory: ChatItem[], + aiResponse?: string +): Promise { + return fetch('/api/query-ai-handler', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ - action, - input: { ...input }, + query, + chatFullHistory, + aiResponse, }), }); } diff --git a/nx-dev/feature-ai/src/lib/error-message.tsx b/nx-dev/feature-ai/src/lib/error-message.tsx index 233c3610aaaf6e..9e46d6aeef3adc 100644 --- a/nx-dev/feature-ai/src/lib/error-message.tsx +++ b/nx-dev/feature-ai/src/lib/error-message.tsx @@ -4,7 +4,7 @@ import { } from '@heroicons/react/24/outline'; export function ErrorMessage({ error }: { error: any }): JSX.Element { - if (error.data.no_results) { + if (error?.data?.no_results) { return (
diff --git a/nx-dev/feature-ai/src/lib/feed-container.tsx b/nx-dev/feature-ai/src/lib/feed-container.tsx index ee96f6517c5e41..2aa1dfe9cfb662 100644 --- a/nx-dev/feature-ai/src/lib/feed-container.tsx +++ b/nx-dev/feature-ai/src/lib/feed-container.tsx @@ -1,10 +1,4 @@ -import { - ChatItem, - getProcessedHistory, - queryAi, - sendFeedbackAnalytics, - sendQueryAnalytics, -} from '@nx/nx-dev/data-access-ai'; +import { getProcessedHistory, queryAi } from '@nx/nx-dev/data-access-ai'; import { sendCustomEvent } from '@nx/nx-dev/feature-analytics'; import { RefObject, useEffect, useRef, useState } from 'react'; import { ErrorMessage } from './error-message'; @@ -12,6 +6,7 @@ import { Feed } from './feed/feed'; import { LoadingState } from './loading-state'; import { Prompt } from './prompt'; import { formatMarkdownSources } from './utils'; +import { ChatItem } from '@nx/nx-dev/util-ai'; interface LastQueryMetadata { sources: string[]; @@ -86,11 +81,6 @@ export function FeedContainer(): JSX.Element { query, ...aiResponse.usage, }); - sendQueryAnalytics({ - action: 'ai_query', - query, - ...aiResponse.usage, - }); } catch (error: any) { setQueryError(error); } @@ -109,15 +99,6 @@ export function FeedContainer(): JSX.Element { ? JSON.stringify(lastQueryMetadata.sources) : 'Could not retrieve last answer sources', }); - sendFeedbackAnalytics({ - action: 'evaluation', - result: answer ? answer.content : 'Could not retrieve the answer', - query: question ? question.content : 'Could not retrieve the question', - response: null, // TODO: Use query metadata here - sources: lastQueryMetadata - ? JSON.stringify(lastQueryMetadata.sources) - : 'Could not retrieve last answer sources', - }); }; return ( diff --git a/nx-dev/nx-dev/pages/api/openai-handler.ts b/nx-dev/nx-dev/pages/api/openai-handler.ts deleted file mode 100644 index 3c2fbcaaa04a96..00000000000000 --- a/nx-dev/nx-dev/pages/api/openai-handler.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { NextRequest } from 'next/server'; - -const openAiKey = process.env['NX_OPENAI_KEY']; -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') { - apiUrl += 'embeddings'; - } else if (action === 'chatCompletion') { - apiUrl += 'chat/completions'; - } else if (action === 'moderation') { - 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 }); - } -} diff --git a/nx-dev/nx-dev/pages/api/query-ai-handler.ts b/nx-dev/nx-dev/pages/api/query-ai-handler.ts new file mode 100644 index 00000000000000..de801d5cde7beb --- /dev/null +++ b/nx-dev/nx-dev/pages/api/query-ai-handler.ts @@ -0,0 +1,208 @@ +// based on: +// https://github.com/supabase-community/nextjs-openai-doc-search/blob/main/pages/api/vector-search.ts + +import { NextRequest } from 'next/server'; +import { + CustomError, + DEFAULT_MATCH_COUNT, + DEFAULT_MATCH_THRESHOLD, + MIN_CONTENT_LENGTH, + PROMPT, + PageSection, + checkEnvVariables, + getListOfSources, + getMessageFromResponse, + initializeChat, + moderateContent, + openAiAPICall, + sanitizeLinksInResponse, + toMarkdownList, +} from '@nx/nx-dev/util-ai'; +import { SupabaseClient, createClient } from '@supabase/supabase-js'; +import { CreateCompletionResponseUsage, CreateEmbeddingResponse } from 'openai'; +import GPT3Tokenizer from 'gpt3-tokenizer'; + +const supabaseUrl = process.env['NX_NEXT_PUBLIC_SUPABASE_URL']; +const supabaseServiceKey = process.env['NX_SUPABASE_SERVICE_ROLE_KEY_ACTUAL']; +const openAiKey = process.env['NX_OPENAI_KEY']; + +export const config = { + runtime: 'edge', +}; + +export default async function handler(request: NextRequest) { + try { + checkEnvVariables(openAiKey, supabaseUrl, supabaseServiceKey); + const { query, aiResponse, chatFullHistory } = await request.json(); + + const supabaseClient: SupabaseClient = createClient( + supabaseUrl as string, + supabaseServiceKey as string + ); + + if (!query) { + throw new CustomError('user_error', 'Missing query in request data', { + missing_query: true, + }); + } + + // Moderate the content to comply with OpenAI T&C + const sanitizedQuery = query.trim(); + + await moderateContent(sanitizedQuery, openAiKey as string); + + // Create embedding from query + // NOTE: Here, we may or may not want to include the previous AI response + /** + * For retrieving relevant Nx documentation sections via embeddings, it's a design decision. + * Including the prior response might give more contextually relevant sections, + * but just sending the query might suffice for many cases. + * + * We can experiment with this. + * + * How the solution looks like with previous response: + * + * const embeddingResponse = await openAiCall( + * { input: sanitizedQuery + aiResponse }, + * 'embedding' + * ); + * + * This costs more tokens, so if we see costs skyrocket we remove it. + * As it says in the docs, it's a design decision, and it may or may not really improve results. + */ + const embeddingResponseObj = await openAiAPICall( + { input: sanitizedQuery + aiResponse, model: 'text-embedding-ada-002' }, + 'embedding', + openAiKey as string + ); + + const embeddingResponse = await embeddingResponseObj.json(); + + if (!embeddingResponseObj.ok) { + throw new CustomError( + 'application_error', + 'Failed to create embedding for question', + { + data: embeddingResponse, + } + ); + } + + const { + data: [{ embedding }], + }: CreateEmbeddingResponse = embeddingResponse; + + const { error: matchError, data: pageSections } = await supabaseClient.rpc( + 'match_page_sections_2', + { + embedding, + match_threshold: DEFAULT_MATCH_THRESHOLD, + match_count: DEFAULT_MATCH_COUNT, + min_content_length: MIN_CONTENT_LENGTH, + } + ); + + if (matchError) { + throw new CustomError( + 'application_error', + 'Failed to match page sections', + matchError + ); + } + + // Note: this is experimental. I think it should work + // mainly because we're testing previous response + query. + if (!pageSections || pageSections.length === 0) { + throw new CustomError('user_error', 'No results found.', { + no_results: true, + }); + } + + const tokenizer = new GPT3Tokenizer({ type: 'gpt3' }); + let tokenCount = 0; + let contextText = ''; + + for (let i = 0; i < (pageSections as PageSection[]).length; i++) { + const pageSection: PageSection = pageSections[i]; + const content = pageSection.content; + const encoded = tokenizer.encode(content); + tokenCount += encoded.text.length; + + if (tokenCount >= 2500) { + break; + } + + contextText += `${content.trim()}\n---\n`; + } + + const { chatMessages: chatGptMessages, chatHistory } = initializeChat( + chatFullHistory, + query, + contextText, + PROMPT, + aiResponse + ); + + const responseObj = await openAiAPICall( + { + model: 'gpt-3.5-turbo-16k', + messages: chatGptMessages, + temperature: 0, + stream: false, + }, + 'chatCompletion', + openAiKey as string + ); + + const response = await responseObj.json(); + + if (!responseObj.ok) { + throw new CustomError( + 'application_error', + 'Failed to generate completion', + { + data: response, + } + ); + } + // 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)].join(''); + + const responseWithoutBadLinks = await sanitizeLinksInResponse(message); + + const sources = getListOfSources(pageSections); + + 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) { + console.error('Error: ', err); + + return new Response( + JSON.stringify({ + ...JSON.parse(JSON.stringify(err)), + message: err?.['message'], + }), + { + status: 500, + headers: { + 'content-type': 'application/json', + }, + } + ); + } +} diff --git a/nx-dev/util-ai/.eslintrc.json b/nx-dev/util-ai/.eslintrc.json new file mode 100644 index 00000000000000..adbe7ae2dfabd4 --- /dev/null +++ b/nx-dev/util-ai/.eslintrc.json @@ -0,0 +1,25 @@ +{ + "extends": ["../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], + "rules": {} + }, + { + "files": ["*.ts", "*.tsx"], + "rules": {} + }, + { + "files": ["*.js", "*.jsx"], + "rules": {} + }, + { + "files": ["*.json"], + "parser": "jsonc-eslint-parser", + "rules": { + "@nx/dependency-checks": "error" + } + } + ] +} diff --git a/nx-dev/util-ai/README.md b/nx-dev/util-ai/README.md new file mode 100644 index 00000000000000..a1d32844b41e3d --- /dev/null +++ b/nx-dev/util-ai/README.md @@ -0,0 +1,11 @@ +# nx-dev-util-ai + +This library was generated with [Nx](https://nx.dev). + +## Building + +Run `nx build nx-dev-util-ai` to build the library. + +## Running unit tests + +Run `nx test nx-dev-util-ai` to execute the unit tests via [Jest](https://jestjs.io). diff --git a/nx-dev/util-ai/jest.config.ts b/nx-dev/util-ai/jest.config.ts new file mode 100644 index 00000000000000..4bcab610fa70c0 --- /dev/null +++ b/nx-dev/util-ai/jest.config.ts @@ -0,0 +1,11 @@ +/* eslint-disable */ +export default { + displayName: 'nx-dev-util-ai', + preset: '../../jest.preset.js', + testEnvironment: 'node', + transform: { + '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '/tsconfig.spec.json' }], + }, + moduleFileExtensions: ['ts', 'js', 'html'], + coverageDirectory: '../../coverage/nx-dev/util-ai', +}; diff --git a/nx-dev/util-ai/package.json b/nx-dev/util-ai/package.json new file mode 100644 index 00000000000000..aa059580b8e195 --- /dev/null +++ b/nx-dev/util-ai/package.json @@ -0,0 +1,11 @@ +{ + "name": "@nx/nx-dev/util-ai", + "version": "0.0.1", + "dependencies": { + "tslib": "^2.3.0", + "openai": "~3.3.0" + }, + "type": "commonjs", + "main": "./src/index.js", + "typings": "./src/index.d.ts" +} diff --git a/nx-dev/util-ai/project.json b/nx-dev/util-ai/project.json new file mode 100644 index 00000000000000..51972b68f128f4 --- /dev/null +++ b/nx-dev/util-ai/project.json @@ -0,0 +1,43 @@ +{ + "name": "nx-dev-util-ai", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "nx-dev/util-ai/src", + "projectType": "library", + "targets": { + "build": { + "executor": "@nx/js:tsc", + "outputs": ["{options.outputPath}"], + "options": { + "outputPath": "dist/nx-dev/util-ai", + "main": "nx-dev/util-ai/src/index.ts", + "tsConfig": "nx-dev/util-ai/tsconfig.lib.json", + "assets": ["nx-dev/util-ai/*.md"] + } + }, + "lint": { + "executor": "@nx/linter:eslint", + "outputs": ["{options.outputFile}"], + "options": { + "lintFilePatterns": [ + "nx-dev/util-ai/**/*.ts", + "nx-dev/util-ai/package.json" + ] + } + }, + "test": { + "executor": "@nx/jest:jest", + "outputs": ["{workspaceRoot}/coverage/{projectRoot}"], + "options": { + "jestConfig": "nx-dev/util-ai/jest.config.ts", + "passWithNoTests": true + }, + "configurations": { + "ci": { + "ci": true, + "codeCoverage": true + } + } + } + }, + "tags": [] +} diff --git a/nx-dev/util-ai/src/index.ts b/nx-dev/util-ai/src/index.ts new file mode 100644 index 00000000000000..2941e8aa809a65 --- /dev/null +++ b/nx-dev/util-ai/src/index.ts @@ -0,0 +1,5 @@ +export * from './lib/utils'; +export * from './lib/constants'; +export * from './lib/openai-call'; +export * from './lib/moderation'; +export * from './lib/chat-utils'; diff --git a/nx-dev/util-ai/src/lib/chat-utils.ts b/nx-dev/util-ai/src/lib/chat-utils.ts new file mode 100644 index 00000000000000..522139c4d2844e --- /dev/null +++ b/nx-dev/util-ai/src/lib/chat-utils.ts @@ -0,0 +1,142 @@ +import { + ChatCompletionRequestMessageRoleEnum, + CreateChatCompletionResponse, +} from 'openai'; +import { ChatItem, PageSection } from './utils'; + +/** + * Initializes a chat session by generating the initial chat messages based on the given parameters. + * + * @param {ChatItem[]} chatFullHistory - The full chat history. + * @param {string} query - The user's query. + * @param {string} contextText - The context text or Nx Documentation. + * @param {string} prompt - The prompt message displayed to the user. + * @param {string} [aiResponse] - The AI assistant's response. + * @returns {Object} - An object containing the generated chat messages and updated chat history. + * - chatMessages: An array of chat messages for the chat session. + * - chatHistory: The updated chat history. + */ +export function initializeChat( + chatFullHistory: ChatItem[], + query: string, + contextText: string, + prompt: string, + aiResponse?: string +): { chatMessages: ChatItem[]; chatHistory: ChatItem[] } { + const finalQuery = ` + You will be provided the Nx Documentation. + Answer my message provided by following the approach below: + + - Step 1: Identify CLUES (keywords, phrases, contextual information, references) in the input that you could use to generate an answer. + - Step 2: Deduce the diagnostic REASONING process from the premises (clues, question), relying ONLY on the information provided in the Nx Documentation. If you recognize vulgar language, answer the question if possible, and educate the user to stay polite. + - Step 3: EVALUATE the reasoning. If the reasoning aligns with the Nx Documentation, accept it. Do not use any external knowledge or make assumptions outside of the provided Nx documentation. If the reasoning doesn't strictly align with the Nx Documentation or relies on external knowledge or inference, reject it and answer with the exact string: + "Sorry, I don't know how to help with that. You can visit the [Nx documentation](https://nx.dev/getting-started/intro) for more info." + - Final Step: You can also rely on the messages we have exchanged so far. Do NOT reveal the approach to the user. + Nx Documentation: + ${contextText} + + ---- My message: ${query} + `; + let chatGptMessages: ChatItem[] = []; + let messages: ChatItem[] = []; + + if (chatFullHistory.length > 0) { + messages = [ + { + role: ChatCompletionRequestMessageRoleEnum.Assistant, + content: aiResponse ?? '', + }, + { role: ChatCompletionRequestMessageRoleEnum.User, content: finalQuery }, + ]; + chatGptMessages = [...chatFullHistory, ...messages]; + } else { + messages = [ + { role: ChatCompletionRequestMessageRoleEnum.System, content: prompt }, + { role: ChatCompletionRequestMessageRoleEnum.User, content: finalQuery }, + ]; + chatGptMessages = [...messages]; + } + + chatFullHistory.push(...messages); + + return { chatMessages: chatGptMessages, chatHistory: chatFullHistory }; +} + +export function getMessageFromResponse( + response: CreateChatCompletionResponse +): string { + return response.choices[0].message?.content ?? ''; +} + +export async function sanitizeLinksInResponse( + response: string +): Promise { + const regex = /https:\/\/nx\.dev[^) \n]*[^).]/g; + const urls = response.match(regex); + + if (urls) { + for (const url of urls) { + const linkIsWrong = await is404(url); + if (linkIsWrong) { + response = response.replace( + url, + 'https://nx.dev/getting-started/intro' + ); + } + } + } + + return response; +} + +async function is404(url: string): Promise { + try { + const response = await fetch(url.replace('https://nx.dev', '')); + if (response.status === 404) { + return true; + } else { + return false; + } + } catch (error) { + if ((error as any)?.response?.status === 404) { + return true; + } else { + return false; + } + } +} + +export function getListOfSources( + pageSections: PageSection[] +): { heading: string; url: string }[] { + const uniqueUrlPartials = new Set(); + const result = pageSections + .filter((section) => { + if (section.url_partial && !uniqueUrlPartials.has(section.url_partial)) { + uniqueUrlPartials.add(section.url_partial); + return true; + } + return false; + }) + .map((section) => { + const url = new URL('https://nx.dev'); + url.pathname = section.url_partial as string; + if (section.slug) { + url.hash = section.slug; + } + return { + heading: section.heading, + url: url.toString(), + }; + }); + + return result; +} + +export function toMarkdownList( + sections: { heading: string; url: string }[] +): string { + return sections + .map((section) => `- [${section.heading}](${section.url})`) + .join('\n'); +} diff --git a/nx-dev/util-ai/src/lib/constants.ts b/nx-dev/util-ai/src/lib/constants.ts new file mode 100644 index 00000000000000..33736250974d7a --- /dev/null +++ b/nx-dev/util-ai/src/lib/constants.ts @@ -0,0 +1,30 @@ +export const DEFAULT_MATCH_THRESHOLD = 0.78; +export const DEFAULT_MATCH_COUNT = 15; +export const MIN_CONTENT_LENGTH = 50; + +// This limits history to 30 messages back and forth +// It's arbitrary, but also generous +// History length should be based on token count +// This is a temporary solution +export const MAX_HISTORY_LENGTH = 30; + +export const PROMPT = ` +${` +You are a knowledgeable Nx representative. +Your knowledge is based entirely on the official Nx Documentation. +You can answer queries using ONLY that information. +You cannot answer queries using your own knowledge or experience. +Answer in markdown format. Always give an example, answer as thoroughly as you can, and +always provide a link to relevant documentation +on the https://nx.dev website. All the links you find or post +that look like local or relative links, always prepend with "https://nx.dev". +Your answer should be in the form of a Markdown article +(including related code snippets if available), much like the +existing Nx documentation. Mark the titles and the subsections with the appropriate markdown syntax. +If you are unsure and cannot find an answer in the Nx Documentation, say +"Sorry, I don't know how to help with that. You can visit the [Nx documentation](https://nx.dev/getting-started/intro) for more info." +Remember, answer the question using ONLY the information provided in the Nx Documentation. +` + .replace(/\s+/g, ' ') + .trim()} +`; diff --git a/nx-dev/util-ai/src/lib/moderation.ts b/nx-dev/util-ai/src/lib/moderation.ts new file mode 100644 index 00000000000000..73240ddb607209 --- /dev/null +++ b/nx-dev/util-ai/src/lib/moderation.ts @@ -0,0 +1,23 @@ +import { openAiAPICall } from './openai-call'; +import { CustomError } from './utils'; + +export async function moderateContent( + sanitizedQuery: string, + openAiKey: string +) { + const moderationResponseObj = await openAiAPICall( + { input: sanitizedQuery }, + 'moderation', + openAiKey as string + ); + + const moderationResponse = await moderationResponseObj.json(); + const [results] = moderationResponse.results; + + if (results.flagged) { + throw new CustomError('user_error', 'Flagged content', { + flagged: true, + categories: results.categories, + }); + } +} diff --git a/nx-dev/util-ai/src/lib/openai-call.ts b/nx-dev/util-ai/src/lib/openai-call.ts new file mode 100644 index 00000000000000..9916848ce52483 --- /dev/null +++ b/nx-dev/util-ai/src/lib/openai-call.ts @@ -0,0 +1,26 @@ +export async function openAiAPICall( + input: object, + action: 'moderation' | 'embedding' | 'chatCompletion', + openAiKey: string +) { + let apiUrl = 'https://api.openai.com/v1/'; + + if (action === 'embedding') { + apiUrl += 'embeddings'; + } else if (action === 'chatCompletion') { + apiUrl += 'chat/completions'; + } else if (action === 'moderation') { + apiUrl += 'moderations'; + } else { + return new Response('Invalid action', { status: 400 }); + } + + return fetch(apiUrl, { + method: 'POST', + headers: { + Authorization: `Bearer ${openAiKey}`, + 'Content-Type': 'application/json', + }, + body: JSON.stringify(input), + }); +} diff --git a/nx-dev/util-ai/src/lib/utils.ts b/nx-dev/util-ai/src/lib/utils.ts new file mode 100644 index 00000000000000..6a8a6b5ba58363 --- /dev/null +++ b/nx-dev/util-ai/src/lib/utils.ts @@ -0,0 +1,62 @@ +import { ChatCompletionRequestMessageRoleEnum } from 'openai'; + +export function checkEnvVariables( + openAiKey?: string, + supabaseUrl?: string, + supabaseServiceKey?: string +) { + if (!openAiKey) { + throw new CustomError( + 'application_error', + 'Missing environment variable NX_OPENAI_KEY', + { + missing_key: true, + } + ); + } + + if (!supabaseUrl) { + throw new CustomError( + 'application_error', + 'Missing environment variable NX_NEXT_PUBLIC_SUPABASE_URL', + { missing_key: true } + ); + } + if (!supabaseServiceKey) { + throw new CustomError( + 'application_error', + 'Missing environment variable NX_SUPABASE_SERVICE_ROLE_KEY', + { missing_key: true } + ); + } +} + +export class CustomError extends Error { + public type: string; + public data: Record; + + constructor( + type: string = 'application_error', + message: string, + data: Record = {} + ) { + super(message); + this.type = type; + this.data = data; + } +} + +export interface PageSection { + id: number; + page_id: number; + content: string; + heading: string; + similarity: number; + slug: string; + url_partial: string | null; +} + +export interface ChatItem { + role: ChatCompletionRequestMessageRoleEnum; + content: string; +} diff --git a/nx-dev/util-ai/tsconfig.json b/nx-dev/util-ai/tsconfig.json new file mode 100644 index 00000000000000..f5b85657a88323 --- /dev/null +++ b/nx-dev/util-ai/tsconfig.json @@ -0,0 +1,22 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "module": "commonjs", + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitOverride": true, + "noPropertyAccessFromIndexSignature": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/nx-dev/util-ai/tsconfig.lib.json b/nx-dev/util-ai/tsconfig.lib.json new file mode 100644 index 00000000000000..0c2eb588e02511 --- /dev/null +++ b/nx-dev/util-ai/tsconfig.lib.json @@ -0,0 +1,11 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "declaration": true, + "lib": ["dom", "es2019"], + "types": ["node"] + }, + "include": ["src/**/*.ts"], + "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"] +} diff --git a/nx-dev/util-ai/tsconfig.spec.json b/nx-dev/util-ai/tsconfig.spec.json new file mode 100644 index 00000000000000..9b2a121d114b68 --- /dev/null +++ b/nx-dev/util-ai/tsconfig.spec.json @@ -0,0 +1,14 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": [ + "jest.config.ts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.d.ts" + ] +} diff --git a/tsconfig.base.json b/tsconfig.base.json index 27bc4a9441913a..75742af2184d1d 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -87,6 +87,7 @@ "@nx/nx-dev/ui-references": ["nx-dev/ui-references/src/index.ts"], "@nx/nx-dev/ui-sponsor-card": ["nx-dev/ui-sponsor-card/src/index.ts"], "@nx/nx-dev/ui-theme": ["nx-dev/ui-theme/src/index.ts"], + "@nx/nx-dev/util-ai": ["nx-dev/util-ai/src/index.ts"], "@nx/playwright": ["packages/playwright/index.ts"], "@nx/plugin": ["packages/plugin"], "@nx/plugin/*": ["packages/plugin/*"],