|
| 1 | +import { env } from "node:process"; |
| 2 | +import { createVertex } from "@ai-sdk/google-vertex"; |
| 3 | +import { streamText, convertToCoreMessages } from "ai"; |
| 4 | +import { StatusCodes, ReasonPhrases } from "http-status-codes"; |
| 5 | +import { smartSearchTool } from "@/lib/rag.mjs"; |
| 6 | + |
| 7 | +// Ensure all required environment variables are set |
| 8 | +if (!env.GOOGLE_VERTEX_PROJECT) { |
| 9 | + throw new Error("GOOGLE_VERTEX_PROJECT is not set"); |
| 10 | +} |
| 11 | + |
| 12 | +if (!env.GOOGLE_VERTEX_LOCATION) { |
| 13 | + throw new Error("GOOGLE_VERTEX_LOCATION is not set"); |
| 14 | +} |
| 15 | + |
| 16 | +if (!env.GOOGLE_VERTEX_CLIENT_EMAIL) { |
| 17 | + throw new Error("GOOGLE_VERTEX_CLIENT_EMAIL is not set"); |
| 18 | +} |
| 19 | + |
| 20 | +if (!env.GOOGLE_VERTEX_PRIVATE_KEY) { |
| 21 | + throw new Error("GOOGLE_VERTEX_PRIVATE_KEY is not set"); |
| 22 | +} |
| 23 | + |
| 24 | +if (!env.GOOGLE_VERTEX_PRIVATE_KEY.includes("-----BEGIN PRIVATE KEY-----")) { |
| 25 | + throw new Error("GOOGLE_VERTEX_PRIVATE_KEY is not formatted correctly"); |
| 26 | +} |
| 27 | + |
| 28 | +const vertex = createVertex({ |
| 29 | + project: env.GOOGLE_VERTEX_PROJECT, |
| 30 | + location: env.GOOGLE_VERTEX_LOCATION, |
| 31 | + googleAuthOptions: { |
| 32 | + credentials: { |
| 33 | + client_email: env.GOOGLE_VERTEX_CLIENT_EMAIL, |
| 34 | + private_key: env.GOOGLE_VERTEX_PRIVATE_KEY.replaceAll( |
| 35 | + String.raw`\n`, |
| 36 | + "\n", |
| 37 | + ), // Ensure newlines are correctly formatted |
| 38 | + }, |
| 39 | + }, |
| 40 | +}); |
| 41 | + |
| 42 | +const smartSearchPrompt = ` |
| 43 | + - You can use the 'smartSearchTool' to find information relating to Faust. |
| 44 | + - WP Engine Smart Search is a powerful tool for finding information about Faust. |
| 45 | + - After the 'smartSearchTool' provides results (even if it's an error or no information found) |
| 46 | + - You MUST then formulate a conversational response to the user based on those results but also use the tool if the users query is deemed plausible. |
| 47 | + - If search results are found, summarize them for the user. |
| 48 | + - If no information is found or an error occurs, inform the user clearly. |
| 49 | + - IMPORTANT: Don't prefix root-relative links in post_url so client-side routing works. If you find links other places that are at the "faustjs.org" domain, you can make them root-relative. |
| 50 | +`; |
| 51 | + |
| 52 | +const systemPromptContent = ` |
| 53 | + - You are a friendly and helpful AI assistant that provides Developers help with their coding tasks and learning, as relevant to Faust.js, WPGraphQL, and headless WordPress. |
| 54 | + - Format your responses using Github Flavored Markdown. |
| 55 | + - Make sure to format links as [link text](path). |
| 56 | + - Make sure to link out to the source of the information you provide. |
| 57 | + - Prefer new information over old information. |
| 58 | + - Do not invent information. Stick to the data provided by the tool. |
| 59 | +`; |
| 60 | + |
| 61 | +export async function POST(req) { |
| 62 | + try { |
| 63 | + const { messages } = await req.json(); |
| 64 | + |
| 65 | + if (!messages || !Array.isArray(messages) || messages.length === 0) { |
| 66 | + return new Response(ReasonPhrases.BAD_REQUEST, { |
| 67 | + status: StatusCodes.BAD_REQUEST, |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + const coreMessages = convertToCoreMessages(messages); |
| 72 | + |
| 73 | + const response = await streamText({ |
| 74 | + model: vertex("gemini-2.5-flash"), |
| 75 | + system: [systemPromptContent, smartSearchPrompt].join("\n"), |
| 76 | + messages: coreMessages, |
| 77 | + tools: { |
| 78 | + smartSearchTool, |
| 79 | + }, |
| 80 | + onError: (error) => { |
| 81 | + console.error("Error during streaming:", error); |
| 82 | + return new Response(ReasonPhrases.INTERNAL_SERVER_ERROR, { |
| 83 | + status: StatusCodes.INTERNAL_SERVER_ERROR, |
| 84 | + }); |
| 85 | + }, |
| 86 | + onToolCall: async (toolCall) => { |
| 87 | + console.log("Tool call initiated:", toolCall); |
| 88 | + }, |
| 89 | + onStepFinish: async (result) => { |
| 90 | + if (result.usage) { |
| 91 | + console.log( |
| 92 | + `[Token Usage] Prompt tokens: ${result.usage.promptTokens}, Completion tokens: ${result.usage.completionTokens}, Total tokens: ${result.usage.totalTokens}`, |
| 93 | + ); |
| 94 | + } |
| 95 | + }, |
| 96 | + maxSteps: 5, |
| 97 | + }); |
| 98 | + |
| 99 | + return response.toDataStreamResponse(); |
| 100 | + } catch (error) { |
| 101 | + console.error("Error in chat API:", error); |
| 102 | + return new Response(ReasonPhrases.INTERNAL_SERVER_ERROR, { |
| 103 | + status: StatusCodes.INTERNAL_SERVER_ERROR, |
| 104 | + }); |
| 105 | + } |
| 106 | +} |
0 commit comments