From 645625963c40252398e362435e717838efaabce1 Mon Sep 17 00:00:00 2001 From: sedanah-m Date: Thu, 3 Sep 2026 08:40:26 -0700 Subject: [PATCH 1/7] feat: add grounding with google search --- .../grounding-with-google-search/index.tsx | 103 ++++++++++++++++++ .../grounding-with-google-search/service.ts | 67 ++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 ai/ai-samples/src/features/grounding-with-google-search/index.tsx create mode 100644 ai/ai-samples/src/features/grounding-with-google-search/service.ts diff --git a/ai/ai-samples/src/features/grounding-with-google-search/index.tsx b/ai/ai-samples/src/features/grounding-with-google-search/index.tsx new file mode 100644 index 000000000..e51a07470 --- /dev/null +++ b/ai/ai-samples/src/features/grounding-with-google-search/index.tsx @@ -0,0 +1,103 @@ +import { useState } from 'react'; +import { generateGroundedContent, GroundedResult } from './service'; + +export default function GroundingWithGoogleSearchView() { + const [prompt, setPrompt] = useState('Who won the world cup 2026?'); + const [result, setResult] = useState(null); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + + const handleGenerate = async () => { + const cleanedPrompt = prompt.trim(); + + if (!cleanedPrompt) { + setError('Please enter a prompt.'); + return; + } + + setLoading(true); + setError(null); + setResult(null); + + try { + const data = await generateGroundedContent(cleanedPrompt); + setResult(data); + } catch (err: unknown) { + const message = err instanceof Error ? err.message : 'An error occurred during grounded generation.'; + setError(message); + } finally { + setLoading(false); + } + }; + + const sources = result?.groundingMetadata?.groundingChunks?.filter((chunk) => chunk.web) ?? []; + + return ( +
+

Grounding with Google Search

+

+ Connects Gemini to real-time Google Search to provide up-to-date answers and sources. +

+ +
+ +