Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion baseai/memory/docs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const memoryDocs = (): MemoryI => ({
enabled: true,
include: ['**/*.mdx'],
gitignore: true,
deployedAt: '39f2778ad2dce348bb762a85f765c21453cec4fe',
deployedAt: 'dd4e4714696856a7160aded10308b08304ca3739',
embeddedAt: ''
},
documents: {
Expand Down
11 changes: 11 additions & 0 deletions docs-chatbot.md
Original file line number Diff line number Diff line change
@@ -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.
111 changes: 111 additions & 0 deletions src/app/api/chat/route.relevancy.ts
Original file line number Diff line number Diff line change
@@ -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
// }
// });
87 changes: 0 additions & 87 deletions src/app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
// }
// });