Skip to content

Commit

Permalink
feat: add search grounding support in chat route
Browse files Browse the repository at this point in the history
Co-Authored-By: ben <ben@prologe.io>
  • Loading branch information
devin-ai-integration[bot] and benjaminshafii committed Dec 23, 2024
1 parent 61d74d1 commit e409019
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions web/app/api/(newai)/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { NextResponse, NextRequest } from "next/server";
import { incrementAndLogTokenUsage } from "@/lib/incrementAndLogTokenUsage";
import { handleAuthorization } from "@/lib/handleAuthorization";
import { z } from "zod";
import { google } from "@ai-sdk/google";
import type { GoogleGenerativeAIProviderMetadata } from "@ai-sdk/google";

import { getModel } from "@/lib/models";
import { getChatSystemPrompt } from "@/lib/prompts/chat-prompt";
Expand Down Expand Up @@ -38,6 +40,9 @@ export async function POST(req: NextRequest) {
})
.join("\n\n");

// Extract metadata for Gemini model if applicable
const isGeminiModel = MODEL_NAME === "gemini-2.0-flash-exp";

const result = await streamText({
model,
system: getChatSystemPrompt(
Expand All @@ -47,6 +52,12 @@ export async function POST(req: NextRequest) {
),
maxSteps: 3,
messages: convertToCoreMessages(messages),
onMetadata: isGeminiModel ? (metadata) => {
const geminiMetadata = metadata?.google as GoogleGenerativeAIProviderMetadata | undefined;
if (geminiMetadata?.groundingMetadata) {
console.log("Search grounding metadata:", geminiMetadata.groundingMetadata);
}
} : undefined,
tools: {
getSearchQuery: {
description: "Extract queries to search for notes",
Expand Down

0 comments on commit e409019

Please sign in to comment.