|
| 1 | +import { Langbase } from 'langbase'; |
| 2 | +import { NextRequest } from 'next/server'; |
| 3 | + |
| 4 | +const apiKey = process.env.LANGBASE_API_KEY!; |
| 5 | + |
| 6 | +const langbase = new Langbase({ |
| 7 | + apiKey |
| 8 | +}); |
| 9 | + |
| 10 | +export async function POST(req: NextRequest) { |
| 11 | + const options = await req.json(); |
| 12 | + |
| 13 | + // Check if relevant |
| 14 | + let parsedResponse; |
| 15 | + let attempts = 0; |
| 16 | + const maxAttempts = 5; |
| 17 | + |
| 18 | + while (attempts < maxAttempts) { |
| 19 | + try { |
| 20 | + const router = await langbase.pipe.run({ |
| 21 | + ...options, |
| 22 | + name: 'agent-router-related-to-sourcegraph', |
| 23 | + stream: false, |
| 24 | + variables: [{ name: 'userQuery', value: options.messages[0].content }], |
| 25 | + }); |
| 26 | + |
| 27 | + // @ts-expect-error — TODO: Fix by reporting to Langbase |
| 28 | + parsedResponse = JSON.parse(router.completion); |
| 29 | + break; |
| 30 | + } catch (error) { |
| 31 | + attempts++; |
| 32 | + if (attempts === maxAttempts) { |
| 33 | + return new Response(JSON.stringify({ error: "Service not working at the moment. Please refresh and try again." }), { |
| 34 | + status: 500, |
| 35 | + headers: { |
| 36 | + 'Content-Type': 'application/json' |
| 37 | + } |
| 38 | + }); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // console.log("🚀 ~ parsedResponse:", parsedResponse) |
| 44 | + |
| 45 | + |
| 46 | + // If not relevant, return a stream mimicking Langbase's structure |
| 47 | + if (!parsedResponse.relevant) { |
| 48 | + |
| 49 | + // console.log("🚀 Asking non relevant agent") |
| 50 | + |
| 51 | + // Ask not relevant questions from agent ask-sourcegraph-docs-unrelated-queries |
| 52 | + const { stream, threadId } = await langbase.pipe.run({ |
| 53 | + ...options, |
| 54 | + name: 'ask-sourcegraph-docs-unrelated-queries' |
| 55 | + }); |
| 56 | + |
| 57 | + return new Response(stream, { |
| 58 | + status: 200, |
| 59 | + headers: { |
| 60 | + 'lb-thread-id': threadId ?? '' |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + // Handle relevant question |
| 67 | + // console.log("🚀 Asking relevant agent") |
| 68 | + |
| 69 | + const { stream, threadId } = await langbase.pipe.run({ |
| 70 | + ...options, |
| 71 | + name: 'ask-sourcegraph-docs' |
| 72 | + }); |
| 73 | + |
| 74 | + return new Response(stream, { |
| 75 | + status: 200, |
| 76 | + headers: { |
| 77 | + 'lb-thread-id': threadId ?? '' |
| 78 | + } |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +// Pretend answer is not relevant. |
| 86 | +// const encoder = new TextEncoder(); |
| 87 | +// const stream = new ReadableStream({ |
| 88 | +// start(controller) { |
| 89 | +// const chunk = { |
| 90 | +// id: `cmpl-${Math.random().toString(36).substr(2, 10)}`, |
| 91 | +// object: 'chat.completion.chunk' as const, |
| 92 | +// created: Math.floor(Date.now() / 1000), |
| 93 | +// model: 'gpt-3.5-turbo', |
| 94 | +// choices: [{ |
| 95 | +// index: 0, |
| 96 | +// delta: { content: 'Please ask a relevant question to Sourcegraph.' }, |
| 97 | +// logprobs: null, |
| 98 | +// finish_reason: 'stop' |
| 99 | +// }] |
| 100 | +// }; |
| 101 | +// controller.enqueue(encoder.encode(JSON.stringify(chunk))); |
| 102 | +// controller.close(); |
| 103 | +// } |
| 104 | +// }); |
| 105 | + |
| 106 | +// return new Response(stream, { |
| 107 | +// status: 200, |
| 108 | +// headers: { |
| 109 | +// // Omit 'Content-Type' to allow stream processing |
| 110 | +// } |
| 111 | +// }); |
0 commit comments