|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
| 17 | +using System; |
17 | 18 | using System.Collections.Generic;
|
18 | 19 | using System.Collections.ObjectModel;
|
19 | 20 | using System.Linq;
|
@@ -180,6 +181,256 @@ internal static PromptFeedback FromJson(Dictionary<string, object> jsonDict) {
|
180 | 181 | }
|
181 | 182 | }
|
182 | 183 |
|
| 184 | +/// <summary> |
| 185 | +/// Metadata returned to the client when grounding is enabled. |
| 186 | +/// |
| 187 | +/// > Important: If using Grounding with Google Search, you are required to comply with the |
| 188 | +/// "Grounding with Google Search" usage requirements for your chosen API provider: |
| 189 | +/// [Gemini Developer API](https://ai.google.dev/gemini-api/terms#grounding-with-google-search) |
| 190 | +/// or Vertex AI Gemini API (see [Service Terms](https://cloud.google.com/terms/service-terms) |
| 191 | +/// section within the Service Specific Terms). |
| 192 | +/// </summary> |
| 193 | +public readonly struct GroundingMetadata { |
| 194 | + private readonly ReadOnlyCollection<string> _webSearchQueries; |
| 195 | + private readonly ReadOnlyCollection<GroundingChunk> _groundingChunks; |
| 196 | + private readonly ReadOnlyCollection<GroundingSupport> _groundingSupports; |
| 197 | + |
| 198 | + /// <summary> |
| 199 | + /// A list of web search queries that the model performed to gather the grounding information. |
| 200 | + /// These can be used to allow users to explore the search results themselves. |
| 201 | + /// </summary> |
| 202 | + public IEnumerable<string> WebSearchQueries { |
| 203 | + get { |
| 204 | + return _webSearchQueries ?? new ReadOnlyCollection<string>(new List<string>()); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + /// <summary> |
| 209 | + /// A list of `GroundingChunk` structs. Each chunk represents a piece of retrieved content |
| 210 | + /// (e.g., from a web page) that the model used to ground its response. |
| 211 | + /// </summary> |
| 212 | + public IEnumerable<GroundingChunk> GroundingChunks { |
| 213 | + get { |
| 214 | + return _groundingChunks ?? new ReadOnlyCollection<GroundingChunk>(new List<GroundingChunk>()); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + /// <summary> |
| 219 | + /// A list of `GroundingSupport` structs. Each object details how specific segments of the |
| 220 | + /// model's response are supported by the `groundingChunks`. |
| 221 | + /// </summary> |
| 222 | + public IEnumerable<GroundingSupport> GroundingSupports { |
| 223 | + get { |
| 224 | + return _groundingSupports ?? new ReadOnlyCollection<GroundingSupport>(new List<GroundingSupport>()); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + /// <summary> |
| 229 | + /// Google Search entry point for web searches. |
| 230 | + /// This contains an HTML/CSS snippet that **must** be embedded in an app to display a Google |
| 231 | + /// Search entry point for follow-up web searches related to the model's "Grounded Response". |
| 232 | + /// </summary> |
| 233 | + public SearchEntryPoint? SearchEntryPoint { get; } |
| 234 | + |
| 235 | + private GroundingMetadata(List<string> webSearchQueries, List<GroundingChunk> groundingChunks, |
| 236 | + List<GroundingSupport> groundingSupports, SearchEntryPoint? searchEntryPoint) { |
| 237 | + _webSearchQueries = new ReadOnlyCollection<string>(webSearchQueries ?? new List<string>()); |
| 238 | + _groundingChunks = new ReadOnlyCollection<GroundingChunk>(groundingChunks ?? new List<GroundingChunk>()); |
| 239 | + _groundingSupports = new ReadOnlyCollection<GroundingSupport>(groundingSupports ?? new List<GroundingSupport>()); |
| 240 | + SearchEntryPoint = searchEntryPoint; |
| 241 | + } |
| 242 | + |
| 243 | + internal static GroundingMetadata FromJson(Dictionary<string, object> jsonDict) { |
| 244 | + List<GroundingSupport> supports = null; |
| 245 | + if (jsonDict.TryParseValue("groundingSupports", out List<object> supportListRaw)) |
| 246 | + { |
| 247 | + supports = supportListRaw |
| 248 | + .OfType<Dictionary<string, object>>() |
| 249 | + .Where(d => d.ContainsKey("segment")) // Filter out if segment is missing |
| 250 | + .Select(GroundingSupport.FromJson) |
| 251 | + .ToList(); |
| 252 | + } |
| 253 | + |
| 254 | + return new GroundingMetadata( |
| 255 | + jsonDict.ParseStringList("webSearchQueries"), |
| 256 | + jsonDict.ParseObjectList("groundingChunks", GroundingChunk.FromJson), |
| 257 | + supports, |
| 258 | + jsonDict.ParseNullableObject("searchEntryPoint", Firebase.AI.SearchEntryPoint.FromJson) |
| 259 | + ); |
| 260 | + } |
| 261 | +} |
| 262 | + |
| 263 | +/// <summary> |
| 264 | +/// A struct representing the Google Search entry point. |
| 265 | +/// </summary> |
| 266 | +public readonly struct SearchEntryPoint { |
| 267 | + /// <summary> |
| 268 | + /// An HTML/CSS snippet that can be embedded in your app. |
| 269 | + /// |
| 270 | + /// To ensure proper rendering, it's recommended to display this content within a web view component. |
| 271 | + /// </summary> |
| 272 | + public string RenderedContent { get; } |
| 273 | + |
| 274 | + private SearchEntryPoint(string renderedContent) { |
| 275 | + RenderedContent = renderedContent; |
| 276 | + } |
| 277 | + |
| 278 | + internal static SearchEntryPoint FromJson(Dictionary<string, object> jsonDict) { |
| 279 | + return new SearchEntryPoint( |
| 280 | + jsonDict.ParseValue<string>("renderedContent", JsonParseOptions.ThrowEverything) |
| 281 | + ); |
| 282 | + } |
| 283 | +} |
| 284 | + |
| 285 | +/// <summary> |
| 286 | +/// Represents a chunk of retrieved data that supports a claim in the model's response. This is |
| 287 | +/// part of the grounding information provided when grounding is enabled. |
| 288 | +/// </summary> |
| 289 | +public readonly struct GroundingChunk { |
| 290 | + /// <summary> |
| 291 | + /// Contains details if the grounding chunk is from a web source. |
| 292 | + /// </summary> |
| 293 | + public WebGroundingChunk? Web { get; } |
| 294 | + |
| 295 | + private GroundingChunk(WebGroundingChunk? web) { |
| 296 | + Web = web; |
| 297 | + } |
| 298 | + |
| 299 | + internal static GroundingChunk FromJson(Dictionary<string, object> jsonDict) { |
| 300 | + return new GroundingChunk( |
| 301 | + jsonDict.ParseNullableObject("web", WebGroundingChunk.FromJson) |
| 302 | + ); |
| 303 | + } |
| 304 | +} |
| 305 | + |
| 306 | +/// <summary> |
| 307 | +/// A grounding chunk sourced from the web. |
| 308 | +/// </summary> |
| 309 | +public readonly struct WebGroundingChunk { |
| 310 | + /// <summary> |
| 311 | + /// The URI of the retrieved web page. |
| 312 | + /// </summary> |
| 313 | + public System.Uri Uri { get; } |
| 314 | + /// <summary> |
| 315 | + /// The title of the retrieved web page. |
| 316 | + /// </summary> |
| 317 | + public string Title { get; } |
| 318 | + /// <summary> |
| 319 | + /// The domain of the original URI from which the content was retrieved. |
| 320 | + /// |
| 321 | + /// This field is only populated when using the Vertex AI Gemini API. |
| 322 | + /// </summary> |
| 323 | + public string Domain { get; } |
| 324 | + |
| 325 | + private WebGroundingChunk(System.Uri uri, string title, string domain) { |
| 326 | + Uri = uri; |
| 327 | + Title = title; |
| 328 | + Domain = domain; |
| 329 | + } |
| 330 | + |
| 331 | + internal static WebGroundingChunk FromJson(Dictionary<string, object> jsonDict) { |
| 332 | + Uri uri = null; |
| 333 | + if (jsonDict.TryParseValue("uri", out string uriString)) { |
| 334 | + uri = new Uri(uriString); |
| 335 | + } |
| 336 | + |
| 337 | + return new WebGroundingChunk( |
| 338 | + uri, |
| 339 | + jsonDict.ParseValue<string>("title"), |
| 340 | + jsonDict.ParseValue<string>("domain") |
| 341 | + ); |
| 342 | + } |
| 343 | +} |
| 344 | + |
| 345 | +/// <summary> |
| 346 | +/// Provides information about how a specific segment of the model's response is supported by the |
| 347 | +/// retrieved grounding chunks. |
| 348 | +/// </summary> |
| 349 | +public readonly struct GroundingSupport { |
| 350 | + private readonly ReadOnlyCollection<int> _groundingChunkIndices; |
| 351 | + |
| 352 | + /// <summary> |
| 353 | + /// Specifies the segment of the model's response content that this grounding support pertains |
| 354 | + /// to. |
| 355 | + /// </summary> |
| 356 | + public Segment Segment { get; } |
| 357 | + |
| 358 | + /// <summary> |
| 359 | + /// A list of indices that refer to specific `GroundingChunk` structs within the |
| 360 | + /// `GroundingMetadata.GroundingChunks` array. These referenced chunks are the sources that |
| 361 | + /// support the claim made in the associated `segment` of the response. For example, an array |
| 362 | + /// `[1, 3, 4]` |
| 363 | + /// means that `groundingChunks[1]`, `groundingChunks[3]`, `groundingChunks[4]` are the |
| 364 | + /// retrieved content supporting this part of the response. |
| 365 | + /// </summary> |
| 366 | + public IEnumerable<int> GroundingChunkIndices { |
| 367 | + get { |
| 368 | + return _groundingChunkIndices ?? new ReadOnlyCollection<int>(new List<int>()); |
| 369 | + } |
| 370 | + } |
| 371 | + |
| 372 | + private GroundingSupport(Segment segment, List<int> groundingChunkIndices) { |
| 373 | + Segment = segment; |
| 374 | + _groundingChunkIndices = new ReadOnlyCollection<int>(groundingChunkIndices ?? new List<int>()); |
| 375 | + } |
| 376 | + |
| 377 | + internal static GroundingSupport FromJson(Dictionary<string, object> jsonDict) { |
| 378 | + List<int> indices = new List<int>(); |
| 379 | + if (jsonDict.TryParseValue("groundingChunkIndices", out List<object> indicesRaw)) { |
| 380 | + indices = indicesRaw.OfType<long>().Select(l => (int)l).ToList(); |
| 381 | + } |
| 382 | + |
| 383 | + return new GroundingSupport( |
| 384 | + jsonDict.ParseObject("segment", Segment.FromJson, JsonParseOptions.ThrowEverything), |
| 385 | + indices |
| 386 | + ); |
| 387 | + } |
| 388 | +} |
| 389 | + |
| 390 | +/// <summary> |
| 391 | +/// Represents a specific segment within a `ModelContent` struct, often used to pinpoint the |
| 392 | +/// exact location of text or data that grounding information refers to. |
| 393 | +/// </summary> |
| 394 | +public readonly struct Segment { |
| 395 | + /// <summary> |
| 396 | + /// The zero-based index of the `Part` object within the `parts` array of its parent |
| 397 | + /// `ModelContent` object. This identifies which part of the content the segment belongs to. |
| 398 | + /// </summary> |
| 399 | + public int PartIndex { get; } |
| 400 | + /// <summary> |
| 401 | + /// The zero-based start index of the segment within the specified `Part`, measured in UTF-8 |
| 402 | + /// bytes. This offset is inclusive, starting from 0 at the beginning of the part's content. |
| 403 | + /// </summary> |
| 404 | + public int StartIndex { get; } |
| 405 | + /// <summary> |
| 406 | + /// The zero-based end index of the segment within the specified `Part`, measured in UTF-8 |
| 407 | + /// bytes. This offset is exclusive, meaning the character at this index is not included in the |
| 408 | + /// segment. |
| 409 | + /// </summary> |
| 410 | + public int EndIndex { get; } |
| 411 | + /// <summary> |
| 412 | + /// The text corresponding to the segment from the response. |
| 413 | + /// </summary> |
| 414 | + public string Text { get; } |
| 415 | + |
| 416 | + private Segment(int partIndex, int startIndex, int endIndex, string text) { |
| 417 | + PartIndex = partIndex; |
| 418 | + StartIndex = startIndex; |
| 419 | + EndIndex = endIndex; |
| 420 | + Text = text; |
| 421 | + } |
| 422 | + |
| 423 | + internal static Segment FromJson(Dictionary<string, object> jsonDict) { |
| 424 | + return new Segment( |
| 425 | + jsonDict.ParseValue<int>("partIndex"), |
| 426 | + jsonDict.ParseValue<int>("startIndex"), |
| 427 | + jsonDict.ParseValue<int>("endIndex"), |
| 428 | + jsonDict.ParseValue<string>("text") |
| 429 | + ); |
| 430 | + } |
| 431 | +} |
| 432 | + |
| 433 | + |
183 | 434 | /// <summary>
|
184 | 435 | /// Token usage metadata for processing the generate content request.
|
185 | 436 | /// </summary>
|
|
0 commit comments