diff --git a/baseai/memory/docs/index.ts b/baseai/memory/docs/index.ts index 1d9112e0a..ab36814ca 100644 --- a/baseai/memory/docs/index.ts +++ b/baseai/memory/docs/index.ts @@ -7,7 +7,7 @@ const memoryDocs = (): MemoryI => ({ enabled: true, include: ['**/*.mdx'], gitignore: true, - deployedAt: '39f2778ad2dce348bb762a85f765c21453cec4fe', + deployedAt: 'dd4e4714696856a7160aded10308b08304ca3739', embeddedAt: '' }, documents: { diff --git a/docs-chatbot.md b/docs-chatbot.md new file mode 100644 index 000000000..f36e0730e --- /dev/null +++ b/docs-chatbot.md @@ -0,0 +1,11 @@ +# How we use Docs Chatbot? + +Contact the docs team for any more questions on this. + +## To sync all the latest docs changes in the `main` branch check out to the `main` branch and run + +```sh +pnpm langbase-sync +``` + +This will verify all the changes since the last sync, update these files, and then write the commit hash to `baseai/memory/docs/index.ts` file which you should commit to keep track. diff --git a/src/app/api/chat/route.relevancy.ts b/src/app/api/chat/route.relevancy.ts new file mode 100644 index 000000000..73e6bcff2 --- /dev/null +++ b/src/app/api/chat/route.relevancy.ts @@ -0,0 +1,111 @@ +import { Langbase } from 'langbase'; +import { NextRequest } from 'next/server'; + +const apiKey = process.env.LANGBASE_API_KEY!; + +const langbase = new Langbase({ + apiKey +}); + +export async function POST(req: NextRequest) { + const options = await req.json(); + + // Check if relevant + let parsedResponse; + let attempts = 0; + const maxAttempts = 5; + + while (attempts < maxAttempts) { + try { + const router = await langbase.pipe.run({ + ...options, + name: 'agent-router-related-to-sourcegraph', + stream: false, + variables: [{ name: 'userQuery', value: options.messages[0].content }], + }); + + // @ts-expect-error — TODO: Fix by reporting to Langbase + parsedResponse = JSON.parse(router.completion); + break; + } catch (error) { + attempts++; + if (attempts === maxAttempts) { + return new Response(JSON.stringify({ error: "Service not working at the moment. Please refresh and try again." }), { + status: 500, + headers: { + 'Content-Type': 'application/json' + } + }); + } + } + } + + // console.log("🚀 ~ parsedResponse:", parsedResponse) + + + // If not relevant, return a stream mimicking Langbase's structure + if (!parsedResponse.relevant) { + + // console.log("🚀 Asking non relevant agent") + + // Ask not relevant questions from agent ask-sourcegraph-docs-unrelated-queries + const { stream, threadId } = await langbase.pipe.run({ + ...options, + name: 'ask-sourcegraph-docs-unrelated-queries' + }); + + return new Response(stream, { + status: 200, + headers: { + 'lb-thread-id': threadId ?? '' + } + }); + + } + + // Handle relevant question + // console.log("🚀 Asking relevant agent") + + const { stream, threadId } = await langbase.pipe.run({ + ...options, + name: 'ask-sourcegraph-docs' + }); + + return new Response(stream, { + status: 200, + headers: { + 'lb-thread-id': threadId ?? '' + } + }); +} + + + + +// Pretend answer is not relevant. +// const encoder = new TextEncoder(); +// const stream = new ReadableStream({ +// start(controller) { +// const chunk = { +// id: `cmpl-${Math.random().toString(36).substr(2, 10)}`, +// object: 'chat.completion.chunk' as const, +// created: Math.floor(Date.now() / 1000), +// model: 'gpt-3.5-turbo', +// choices: [{ +// index: 0, +// delta: { content: 'Please ask a relevant question to Sourcegraph.' }, +// logprobs: null, +// finish_reason: 'stop' +// }] +// }; +// controller.enqueue(encoder.encode(JSON.stringify(chunk))); +// controller.close(); +// } +// }); + +// return new Response(stream, { +// status: 200, +// headers: { +// // Omit 'Content-Type' to allow stream processing +// } +// }); diff --git a/src/app/api/chat/route.ts b/src/app/api/chat/route.ts index 73e6bcff2..3cffd3dcf 100644 --- a/src/app/api/chat/route.ts +++ b/src/app/api/chat/route.ts @@ -10,62 +10,6 @@ const langbase = new Langbase({ export async function POST(req: NextRequest) { const options = await req.json(); - // Check if relevant - let parsedResponse; - let attempts = 0; - const maxAttempts = 5; - - while (attempts < maxAttempts) { - try { - const router = await langbase.pipe.run({ - ...options, - name: 'agent-router-related-to-sourcegraph', - stream: false, - variables: [{ name: 'userQuery', value: options.messages[0].content }], - }); - - // @ts-expect-error — TODO: Fix by reporting to Langbase - parsedResponse = JSON.parse(router.completion); - break; - } catch (error) { - attempts++; - if (attempts === maxAttempts) { - return new Response(JSON.stringify({ error: "Service not working at the moment. Please refresh and try again." }), { - status: 500, - headers: { - 'Content-Type': 'application/json' - } - }); - } - } - } - - // console.log("🚀 ~ parsedResponse:", parsedResponse) - - - // If not relevant, return a stream mimicking Langbase's structure - if (!parsedResponse.relevant) { - - // console.log("🚀 Asking non relevant agent") - - // Ask not relevant questions from agent ask-sourcegraph-docs-unrelated-queries - const { stream, threadId } = await langbase.pipe.run({ - ...options, - name: 'ask-sourcegraph-docs-unrelated-queries' - }); - - return new Response(stream, { - status: 200, - headers: { - 'lb-thread-id': threadId ?? '' - } - }); - - } - - // Handle relevant question - // console.log("🚀 Asking relevant agent") - const { stream, threadId } = await langbase.pipe.run({ ...options, name: 'ask-sourcegraph-docs' @@ -78,34 +22,3 @@ export async function POST(req: NextRequest) { } }); } - - - - -// Pretend answer is not relevant. -// const encoder = new TextEncoder(); -// const stream = new ReadableStream({ -// start(controller) { -// const chunk = { -// id: `cmpl-${Math.random().toString(36).substr(2, 10)}`, -// object: 'chat.completion.chunk' as const, -// created: Math.floor(Date.now() / 1000), -// model: 'gpt-3.5-turbo', -// choices: [{ -// index: 0, -// delta: { content: 'Please ask a relevant question to Sourcegraph.' }, -// logprobs: null, -// finish_reason: 'stop' -// }] -// }; -// controller.enqueue(encoder.encode(JSON.stringify(chunk))); -// controller.close(); -// } -// }); - -// return new Response(stream, { -// status: 200, -// headers: { -// // Omit 'Content-Type' to allow stream processing -// } -// });