-
Notifications
You must be signed in to change notification settings - Fork 856
Add web search tool call content #7276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.Shared.DiagnosticIds; | ||
|
|
||
| namespace Microsoft.Extensions.AI; | ||
|
|
||
| /// <summary> | ||
| /// Represents an individual web search result. | ||
| /// </summary> | ||
| [Experimental(DiagnosticIds.Experiments.AIWebSearch, UrlFormat = DiagnosticIds.UrlFormat)] | ||
| public sealed class WebSearchResult | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="WebSearchResult"/> class. | ||
| /// </summary> | ||
| public WebSearchResult() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the title of the web page. | ||
| /// </summary> | ||
| public string? Title { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the URL of the web page. | ||
| /// </summary> | ||
| public Uri? Url { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a text snippet or excerpt from the web page. | ||
| /// </summary> | ||
| public string? Snippet { get; set; } | ||
|
|
||
| /// <summary>Gets or sets additional properties for the result.</summary> | ||
| public AdditionalPropertiesDictionary? AdditionalProperties { get; set; } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.Shared.DiagnosticIds; | ||
|
|
||
| namespace Microsoft.Extensions.AI; | ||
|
|
||
| /// <summary> | ||
| /// Represents a web search tool call invocation by a hosted service. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This content type represents when a hosted AI service invokes a web search tool. | ||
| /// It is informational only and represents the call itself, not the result. | ||
| /// </remarks> | ||
| [Experimental(DiagnosticIds.Experiments.AIWebSearch, UrlFormat = DiagnosticIds.UrlFormat)] | ||
| public sealed class WebSearchToolCallContent : AIContent | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="WebSearchToolCallContent"/> class. | ||
| /// </summary> | ||
| public WebSearchToolCallContent() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the tool call ID. | ||
| /// </summary> | ||
| public string? CallId { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the search queries issued by the service. | ||
| /// </summary> | ||
| public IList<string>? Queries { get; set; } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.Shared.DiagnosticIds; | ||
|
|
||
| namespace Microsoft.Extensions.AI; | ||
|
|
||
| /// <summary> | ||
| /// Represents the result of a web search tool invocation by a hosted service. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This content type represents the results found by a hosted AI service's web search tool. | ||
| /// The results contain a list of <see cref="WebSearchResult"/> items, each describing a web page | ||
| /// found during the search. | ||
| /// </remarks> | ||
| [Experimental(DiagnosticIds.Experiments.AIWebSearch, UrlFormat = DiagnosticIds.UrlFormat)] | ||
| public sealed class WebSearchToolResultContent : AIContent | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="WebSearchToolResultContent"/> class. | ||
| /// </summary> | ||
| public WebSearchToolResultContent() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the tool call ID that this result corresponds to. | ||
| /// </summary> | ||
| public string? CallId { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the web search results. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Each item represents a web page found during the search. | ||
| /// </remarks> | ||
| public IList<WebSearchResult>? Results { get; set; } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -244,6 +244,26 @@ internal static IEnumerable<ChatMessage> ToChatMessages(IEnumerable<ResponseItem | |
| AddImageGenerationContents(imageGenItem, options, message.Contents); | ||
| break; | ||
|
|
||
| case WebSearchCallResponseItem wscri: | ||
| _ = wscri.Patch.TryGetValue("$.action.query"u8, out string? wsQuery); | ||
|
|
||
| message.Contents.Add(new WebSearchToolCallContent | ||
| { | ||
| CallId = wscri.Id, | ||
| Queries = wsQuery is not null ? [wsQuery] : null, | ||
|
|
||
| // We purposefully do not set the RawRepresentation on the WebSearchToolCallContent, only on the WebSearchToolResultContent, to avoid | ||
| // the same WebSearchCallResponseItem being included on two different AIContent instances. When these are roundtripped, we want only one | ||
| // WebSearchCallResponseItem sent back for the pair. | ||
| }); | ||
|
|
||
| message.Contents.Add(new WebSearchToolResultContent | ||
| { | ||
| CallId = wscri.Id, | ||
| RawRepresentation = wscri, | ||
| }); | ||
| break; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How should |
||
|
|
||
| default: | ||
| message.Contents.Add(new() { RawRepresentation = outputItem }); | ||
| break; | ||
|
|
@@ -436,6 +456,14 @@ ChatResponseUpdate CreateUpdate(AIContent? content = null) => | |
| }); | ||
| break; | ||
|
|
||
| case StreamingResponseWebSearchCallInProgressUpdate webSearchInProgressUpdate: | ||
| yield return CreateUpdate(new WebSearchToolCallContent | ||
| { | ||
| CallId = webSearchInProgressUpdate.ItemId, | ||
| RawRepresentation = webSearchInProgressUpdate, | ||
| }); | ||
| break; | ||
|
|
||
| case StreamingResponseOutputItemDoneUpdate outputItemDoneUpdate: | ||
| switch (outputItemDoneUpdate.Item) | ||
| { | ||
|
|
@@ -472,6 +500,24 @@ ChatResponseUpdate CreateUpdate(AIContent? content = null) => | |
| yield return CreateUpdate(CreateCodeInterpreterResultContent(cicri)); | ||
| break; | ||
|
|
||
| case WebSearchCallResponseItem wscri: | ||
| // The WebSearchToolCallContent has already been yielded as part of in-progress updates. | ||
| // Yield a second one here with queries populated, which coalescing will merge with the first. | ||
| _ = wscri.Patch.TryGetValue("$.action.query"u8, out string? wsStreamQuery); | ||
| yield return CreateUpdate(new WebSearchToolCallContent | ||
| { | ||
| CallId = wscri.Id, | ||
| Queries = wsStreamQuery is not null ? [wsStreamQuery] : null, | ||
| }); | ||
|
|
||
| // Also yield the WebSearchToolResultContent. | ||
| yield return CreateUpdate(new WebSearchToolResultContent | ||
| { | ||
| CallId = wscri.Id, | ||
| RawRepresentation = wscri, | ||
| }); | ||
| break; | ||
|
|
||
| // MessageResponseItems will have already had their content yielded as part of delta updates. | ||
| // However, those deltas didn't yield annotations. If there are any annotations, yield them now. | ||
| case MessageResponseItem mri when mri.Content is { Count: > 0 } mriContent && mriContent.Any(c => c.OutputTextAnnotations is { Count: > 0 }): | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.