-
Notifications
You must be signed in to change notification settings - Fork 4.4k
.Net - Agents KernelFunction Strategies #5895
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
Merged
Merged
Changes from all commits
Commits
Show all changes
246 commits
Select commit
Hold shift + click to select a range
ed2bd33
Agent framework
crickman 33f1c0e
Project structure
crickman ca4c1a6
Readme cleanup
crickman d58e416
Build
crickman 74d8deb
Tests and examples
crickman de71d83
Test coverage
crickman 5236c28
Update tests
crickman 308d4b8
Spelling
crickman 905e438
Namespace
crickman d86380d
Coverage
crickman 4273aae
PlugIn Example
crickman 2a1aba2
Merge branch 'main' into feature_agent_framework
crickman bd98124
Test comments & clean-up
crickman 070f82d
Merge branch 'feature_agent_framework' of https://github.com/microsof…
crickman ca53ecd
MVP
crickman fbdf862
Format Fix
crickman 72b720e
Remove System.Linq.Async
crickman 425af3c
Build GD
crickman 000f079
Rollback
crickman e7d365a
Merge branch 'main' into feature_agent_framework
crickman 6a4fb99
Build plz
crickman 4e880e3
Format
crickman 1e01292
Project form
crickman 264cc82
Extra instructions
crickman f3c54c4
ChatCompletionAgent UT
crickman 0e44caf
Coverage
crickman 7225767
Coverage
crickman 22fc872
TODO - Render Filters
crickman 6416ea6
Experiment def
crickman 7dbe37a
Bootstrap AgentChat
crickman ba16b05
Fix experiments
crickman 2ac41f0
Merge branch 'feature_agent_framework' into feature_agent_chat
crickman 8ba1c91
Introducing AgentChat
crickman 42e0bcd
Comment
crickman 56bc185
Tuning
crickman 2cc1686
Merge branch 'main' into feature_agent_framework
crickman aa7231c
Spelling
crickman 01c7d85
Merge branch 'feature_agent_framework' of https://github.com/microsof…
crickman 8f99aa6
TODO Comments
crickman 9048a69
Merge branch 'feature_agent_framework' into feature_agent_chat
crickman cd5ed82
Example
crickman 7977d0a
Spelling
crickman b931c48
More tests
crickman f334ccc
Merge branch 'main' into feature_agent_framework
crickman f57d1ca
Refine
crickman 75fc135
Merge branch 'main' into feature_agent_framework
crickman ac090fd
Merge branch 'main' into feature_agent_framework
crickman dec0de2
Checkpoint
crickman 0984733
Less experiment ids
crickman f8a8365
Identity
crickman 4f436a3
Merge from main
crickman 0438231
Update test
crickman 737cd2f
Test checkpoint
crickman acc0d2a
Encoder fix
crickman 180d37d
Encoder2
crickman 713d20c
Test cleanup
crickman 7fe0af3
Needs unit test
crickman afde6b3
Yeah
crickman ab1383c
BroadcastQueue Failure Propagation
crickman fdedc20
Merge branch 'main' into feature_agent_framework
crickman 7144324
Update from PR Comments
crickman cc5c211
Merge branch 'feature_agent_framework' of https://github.com/microsof…
crickman 143394b
Merge branch 'main' into feature_agent_framework
crickman 8908968
Merge branch 'feature_agent_framework' into feature_agent_chat
crickman a562a7e
Merged from main
crickman 6277f59
Fix merge
crickman c0a62d0
Merge branch 'feature_agent_framework' of https://github.com/microsof…
crickman ffeeeff
Merge branch 'feature_agent_framework' into feature_agent_chat
crickman 1aa05cd
Fix merge
crickman e9736ec
Namespace
crickman 4466048
Coverage
crickman 5a021a8
Update from PR comments
crickman 8dfcf5d
PR Comments
crickman c1dae3f
Resolve merge
crickman 60fe17f
Typo
crickman dd50899
Cleanup
crickman c22176f
Merge branch 'main' into feature_agent_framework
crickman 10823e2
Heck ya
crickman 104ebbd
Fix
crickman 5587125
Termination Strategies
crickman 07c9774
Ack
crickman 9a2b4e2
Strategies
crickman c17fcb4
Merge branch 'main' into feature_agent_framework
crickman c58191c
CancellationToken default
crickman 7ac3051
Merge branch 'feature_agent_framework' of https://github.com/microsof…
crickman 16b438b
Merge branch 'main' into feature_agent_framework
crickman 92b9da7
Merge branch 'main' into feature_agent_framework
crickman 0878281
Merge branch 'main' into feature_agent_framework
crickman 734656e
Type/concept rename
crickman 769d658
Merge branch 'feature_agent_framework' of https://github.com/microsof…
crickman 70871f9
Latest and greatest
crickman b9706e4
Nexus scrub
crickman 5436fed
Abstraction rename
crickman c2020fd
One more (Abstractions folder name)
crickman de2dad0
Remove connector dependency
crickman e2860cc
Re-add connector reference downstream
crickman adf0bee
Review
crickman af41586
Comment
crickman 73079ed
Comments
crickman 6b7a0b7
Support 'init'
crickman df620ef
Resolve merge
crickman a463b36
AgentGroupChat
crickman 2e60f01
Clean
crickman e46cf2d
Comment
crickman 503dd98
Merge branch 'feature_agent_framework' into feature_agent_chat
crickman 4a0dd62
Nuget description
crickman 9a2c957
Nuget
crickman 0482a96
Assembly naming
crickman 90e642a
Merge branch 'feature_agent_framework' into feature_agent_chat
crickman 69ec8c6
Build
crickman 6d446c7
Build
crickman d5be2ca
Comments
crickman 75226d2
Merge branch 'main' into feature_agent_framework
crickman 9478524
Comments Checkpoint
crickman 6fe407e
Dead code
crickman 1143f5a
More comments + typo
crickman 5d37582
More PR updates
crickman 9479b49
Don't copy
crickman f8d758c
Blank line
crickman b7f35f7
Merge from feature
crickman b711530
More
crickman 14929f0
Namespace
crickman e6c6991
public => private: perfect
crickman 0e8b981
Merge branch 'feature_agent_framework' into feature_agent_chat
crickman b06b7e2
Remove namespace
crickman b6566ff
Merge branch 'feature_agent_framework' into feature_agent_chat
crickman 8291e0f
init KernelArguments
crickman d8f4c2e
Signatures
crickman 510751f
Merge branch 'feature_agent_framework' into feature_agent_chat
crickman 822f494
Test fix
crickman b3da6a6
Merge branch 'feature_agent_framework' into feature_agent_chat
crickman 46f7060
Build
crickman 23395cf
Fix mock
crickman 546e5f0
Merge branch 'main' into feature_agent_framework
crickman 83613aa
Sans templatization
crickman e7f2294
Merge branch 'feature_agent_framework' into feature_agent_chat
crickman bbe584b
Namespace
crickman 09677a2
Merge branch 'feature_agent_framework' into feature_agent_chat
crickman 6a13d00
Merge branch 'main' into feature_agent_framework
crickman 7f98506
Merge branch 'feature_agent_framework' into feature_agent_chat
crickman 2585798
Rename local function for agent example
crickman 0ad4b0b
Remove `AddUserMessage`
crickman 919b8f8
Test fix
crickman 1fc19eb
Merge branch 'main' into feature_agent_framework
crickman 031c28a
Merge branch 'feature_agent_framework' of https://github.com/microsof…
crickman 865b725
Update project/package reference
crickman 5e47fb5
Remove constructors
crickman 5ef126b
Spelling
crickman d47271b
Merge branch 'feature_agent_framework' into feature_agent_chat
crickman 6556a25
Use real AgentChat in examples
crickman 28a3428
Namespace
crickman 3f87008
Namespace
crickman 8c8da67
Channel comment clarification
crickman a8ee4cf
Merge branch 'main' into feature_agent_framework
crickman 811b100
Agent examples build directive clean-up
crickman 4595945
Merge branch 'feature_agent_framework' of https://github.com/microsof…
crickman 270590e
Merge branch 'feature_agent_framework' into feature_agent_chat
crickman b45dc56
Resolve merge
crickman 4c3c2ae
Namespace / Merge fix
crickman 0bd61fd
Last merge fix
crickman f7cf4e1
Merge branch 'main' into feature_agent_chat
crickman 948cea7
Merge branch 'main' into feature_agent_chat
crickman 6ef5dbc
Merge branch 'main' into feature_agent_chat
crickman 45abc89
First update from PR comments.
crickman 88f59e3
Consolidate AgentBoundTerminationStrategy contract
crickman 59d13e4
Typos and namespaces
crickman 639a4de
SequentialSelectionStrategy.Reset()
crickman 4d3418d
Blank line
crickman 40d22bd
Removed null defaults for execution settings / strategies
crickman 2e8eb1c
Typos
crickman 31778cc
Namespace
crickman 6d7baa0
Merge branch 'main' into feature_agent_chat
crickman 4c4729d
Throw on no selection-strategy
crickman 0dd303c
Selection failure contract
crickman 9ab4130
Reset termination / iscomplete option
crickman cb843b9
Clean
crickman 616366d
Clean-up
crickman 28e37f1
Improvement
crickman 7b73c4c
dotnet version for UT
crickman 758bfd3
Merge branch 'main' into feature_agent_chat
crickman 6158c97
ConfigureAwait
crickman 5c9ae72
Merge branch 'feature_agent_chat' of https://github.com/microsoft/sem…
crickman 3952a0c
Merge from main
crickman cc7291d
Fix merge
crickman 7f6d04e
Clean-up
crickman 11db2b6
Namespace
crickman bdceeb5
Update example
crickman f79a086
Lil' luv
crickman 2a75117
Onward
crickman 10d6578
Merge branch 'main' into feature_agent_chat
crickman af9a6b3
Merge branch 'main' into feature_agent_chat
crickman 01ec7d6
Merge branch 'main' into feature_agent_chat
crickman cfd2337
Comments
crickman eeb5317
Merge branch 'feature_agent_chat' of https://github.com/microsoft/sem…
crickman d5c3ff6
SelectionStrategy default
crickman 0f4418e
Iterating on max-turns
crickman 971b7e6
Namespace
crickman 7bfc7b8
Checkpoint
crickman fde6614
Merge from parent
crickman 70eaf2a
Comments
crickman 39fd77c
Typos
crickman 646d09f
Fix name
crickman f85ff03
Backport fix
crickman 5dcdfa9
Tune
crickman ce3987a
Merge branch 'feature_agent_chat' into feature_chat_strategy
crickman 5bfbe5c
Test update
crickman c56c7f8
Another test
crickman 65d9784
Comment fix
crickman 6c67b8b
Spelling
crickman acf0eea
Spot fix
crickman 22ea261
Another test
crickman efc8ae8
Almost done with tests.
crickman 8f913fe
Reconcile
crickman c5d60bf
Remove dead test
crickman a6adc16
Merge from main
crickman b8bd338
Merge branch 'main' into feature_chat_strategy
crickman b0ec0de
Convention
crickman 451d5f1
Merge branch 'feature_chat_strategy' of https://github.com/microsoft/…
crickman 7ea3ef8
Merge branch 'main' into feature_chat_strategy
crickman cc3b85e
Merge branch 'main' into feature_chat_strategy
crickman 9ee36c9
Minor
crickman 82298b1
Merge branch 'main' into feature_chat_strategy
crickman a4136ea
Merge from main
crickman 4d1e049
Comment fix
crickman 7d66c3f
Clean
crickman 5f1d111
Even more simple
crickman f1db3f9
Even more simple
crickman 0b7d555
Merge branch 'main' into feature_chat_strategy
crickman 8d6eb50
Sample Alignment
crickman dd0bddf
Merge branch 'main' into feature_chat_strategy
crickman e8a718f
Project clean-up
crickman 489ff8d
ykw
crickman 6b160e9
Merge branch 'main' into feature_chat_strategy
crickman 184eded
Merge branch 'main' into feature_chat_strategy
crickman 25f8365
NoWarn
crickman 0bdcfce
Merge branch 'feature_chat_strategy' of https://github.com/microsoft/…
crickman 74c1de5
Move result translator
crickman 25848da
Updated
crickman e1703c5
this.
crickman 0c49422
Result processing
crickman 1d73574
Updated
crickman 6c3abfb
Remove debugging
crickman 1dd0532
Blank line
crickman bd22c2e
Update Kernel
crickman 689c280
Sample utility comment
crickman d7f6545
Final
crickman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
dotnet/samples/Concepts/AgentSyntax/Getting_Started/Step4_KernelFunctionStrategies.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Examples; | ||
| using Microsoft.SemanticKernel; | ||
| using Microsoft.SemanticKernel.Agents; | ||
| using Microsoft.SemanticKernel.Agents.Chat; | ||
| using Microsoft.SemanticKernel.ChatCompletion; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace GettingStarted; | ||
|
|
||
| /// <summary> | ||
| /// Demonstrate usage of <see cref="KernelFunctionTerminationStrategy"/> and <see cref="KernelFunctionSelectionStrategy"/> | ||
| /// to manage <see cref="AgentGroupChat"/> execution. | ||
| /// </summary> | ||
| public class Step4_KernelFunctionStrategies(ITestOutputHelper output) : BaseTest(output) | ||
| { | ||
| private const string ReviewerName = "ArtDirector"; | ||
| private const string ReviewerInstructions = | ||
| """ | ||
| You are an art director who has opinions about copywriting born of a love for David Ogilvy. | ||
| The goal is to determine if the given copy is acceptable to print. | ||
| If so, state that it is approved. | ||
| If not, provide insight on how to refine suggested copy without examples. | ||
| """; | ||
|
|
||
| private const string CopyWriterName = "Writer"; | ||
| private const string CopyWriterInstructions = | ||
| """ | ||
| You are a copywriter with ten years of experience and are known for brevity and a dry humor. | ||
| You're laser focused on the goal at hand. Don't waste time with chit chat. | ||
| The goal is to refine and decide on the single best copy as an expert in the field. | ||
| Consider suggestions when refining an idea. | ||
| """; | ||
|
|
||
| [Fact] | ||
| public async Task RunAsync() | ||
| { | ||
| // Define the agents | ||
| ChatCompletionAgent agentReviewer = | ||
| new() | ||
| { | ||
| Instructions = ReviewerInstructions, | ||
| Name = ReviewerName, | ||
| Kernel = this.CreateKernelWithChatCompletion(), | ||
| }; | ||
|
|
||
| ChatCompletionAgent agentWriter = | ||
| new() | ||
| { | ||
| Instructions = CopyWriterInstructions, | ||
| Name = CopyWriterName, | ||
| Kernel = this.CreateKernelWithChatCompletion(), | ||
| }; | ||
|
|
||
| KernelFunction terminationFunction = | ||
| KernelFunctionFactory.CreateFromPrompt( | ||
| """ | ||
| Determine if the copy has been approved. If so, respond with a single word: yes | ||
|
|
||
| History: | ||
| {{$history}} | ||
| """); | ||
|
|
||
| KernelFunction selectionFunction = | ||
| KernelFunctionFactory.CreateFromPrompt( | ||
| """ | ||
| You are in a role playing game. | ||
| Carefully read the conversation history and carry on the conversation by specifying only the name of player to take the next turn. | ||
|
|
||
| The available names are: | ||
| {{$agents}} | ||
|
|
||
| History: | ||
| {{$history}} | ||
| """); | ||
markwallace-microsoft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Create a chat for agent interaction. | ||
| AgentGroupChat chat = | ||
| new(agentWriter, agentReviewer) | ||
| { | ||
| ExecutionSettings = | ||
| new() | ||
| { | ||
| // Here KernelFunctionTerminationStrategy will terminate | ||
| // when the art-director has given their approval. | ||
| TerminationStrategy = | ||
| new KernelFunctionTerminationStrategy(terminationFunction, CreateKernelWithChatCompletion()) | ||
| { | ||
| // Only the art-director may approve. | ||
| Agents = [agentReviewer], | ||
| // Customer result parser to determine if the response is "yes" | ||
| ResultParser = (result) => result.GetValue<string>()?.Contains("yes", StringComparison.OrdinalIgnoreCase) ?? false, | ||
| // The prompt variable name for the history argument. | ||
| HistoryVariableName = "history", | ||
| // Limit total number of turns | ||
| MaximumIterations = 10, | ||
| }, | ||
| // Here a KernelFunctionSelectionStrategy selects agents based on a prompt function. | ||
| SelectionStrategy = | ||
| new KernelFunctionSelectionStrategy(selectionFunction, CreateKernelWithChatCompletion()) | ||
| { | ||
| // Returns the entire result value as a string. | ||
| ResultParser = (result) => result.GetValue<string>() ?? string.Empty, | ||
| // The prompt variable name for the agents argument. | ||
| AgentsVariableName = "agents", | ||
| // The prompt variable name for the history argument. | ||
| HistoryVariableName = "history", | ||
| }, | ||
| } | ||
| }; | ||
|
|
||
| // Invoke chat and display messages. | ||
| string input = "concept: maps made out of egg cartons."; | ||
| chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input)); | ||
| this.WriteLine($"# {AuthorRole.User}: '{input}'"); | ||
|
|
||
| await foreach (var content in chat.InvokeAsync()) | ||
| { | ||
| this.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); | ||
| } | ||
|
|
||
| this.WriteLine($"# IS COMPLETE: {chat.IsComplete}"); | ||
| } | ||
| } | ||
92 changes: 92 additions & 0 deletions
92
dotnet/samples/Concepts/AgentSyntax/Getting_Started/Step5_JsonResult.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Examples; | ||
| using Microsoft.SemanticKernel; | ||
| using Microsoft.SemanticKernel.Agents; | ||
| using Microsoft.SemanticKernel.Agents.Chat; | ||
| using Microsoft.SemanticKernel.ChatCompletion; | ||
| using Resources; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace GettingStarted; | ||
|
|
||
| /// <summary> | ||
| /// Demonstrate parsing JSON response. | ||
| /// </summary> | ||
| public class Step5_JsonResult(ITestOutputHelper output) : BaseTest(output) | ||
| { | ||
| private const string TutorName = "Tutor"; | ||
| private const string TutorInstructions = | ||
| """ | ||
| Think step-by-step and rate the user input on creativity and expressivness from 1-100. | ||
|
|
||
| Respond in JSON format with the following JSON schema: | ||
|
|
||
| { | ||
| "score": "integer (1-100)", | ||
| "notes": "the reason for your score" | ||
| } | ||
| """; | ||
|
|
||
| [Fact] | ||
| public async Task RunAsync() | ||
| { | ||
| // Define the agents | ||
| ChatCompletionAgent agent = | ||
| new() | ||
| { | ||
| Instructions = TutorInstructions, | ||
| Name = TutorName, | ||
| Kernel = this.CreateKernelWithChatCompletion(), | ||
| }; | ||
|
|
||
| // Create a chat for agent interaction. | ||
| AgentGroupChat chat = | ||
| new() | ||
| { | ||
| ExecutionSettings = | ||
| new() | ||
| { | ||
| // Here a TerminationStrategy subclass is used that will terminate when | ||
| // the response includes a score that is greater than or equal to 70. | ||
| TerminationStrategy = new ThresholdTerminationStrategy() | ||
| } | ||
| }; | ||
|
|
||
| // Respond to user input | ||
| await InvokeAgentAsync("The sunset is very colorful."); | ||
| await InvokeAgentAsync("The sunset is setting over the mountains."); | ||
| await InvokeAgentAsync("The sunset is setting over the mountains and filled the sky with a deep red flame, setting the clouds ablaze."); | ||
|
|
||
| // Local function to invoke agent and display the conversation messages. | ||
| async Task InvokeAgentAsync(string input) | ||
| { | ||
| chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input)); | ||
|
|
||
| this.WriteLine($"# {AuthorRole.User}: '{input}'"); | ||
|
|
||
| await foreach (var content in chat.InvokeAsync(agent)) | ||
| { | ||
| this.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); | ||
| this.WriteLine($"# IS COMPLETE: {chat.IsComplete}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private record struct InputScore(int score, string notes); | ||
|
|
||
| private sealed class ThresholdTerminationStrategy : TerminationStrategy | ||
| { | ||
| protected override Task<bool> ShouldAgentTerminateAsync(Agent agent, IReadOnlyList<ChatMessageContent> history, CancellationToken cancellationToken) | ||
| { | ||
| string lastMessageContent = history[history.Count - 1].Content ?? string.Empty; | ||
|
|
||
| InputScore? result = JsonResultTranslator.Translate<InputScore>(lastMessageContent); | ||
|
|
||
| return Task.FromResult((result?.score ?? 0) >= 70); | ||
| } | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
dotnet/samples/Concepts/AgentSyntax/RepoUtils/JsonResultTranslator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| using System.Text.Json; | ||
| using Microsoft.SemanticKernel; | ||
|
|
||
| namespace Resources; | ||
| /// <summary> | ||
| /// Supports parsing json from a text block that may contain literals delimiters: | ||
| /// <list type="table"> | ||
| /// <item> | ||
| /// <code> | ||
| /// [json] | ||
| /// </code> | ||
| /// </item> | ||
| /// <item> | ||
| /// <code> | ||
| /// ``` | ||
| /// [json] | ||
| /// ``` | ||
| /// </code> | ||
| /// </item> | ||
| /// <item> | ||
| /// <code> | ||
| /// ```json | ||
| /// [json] | ||
| /// ``` | ||
| /// </code> | ||
| /// </item> | ||
| /// </list> | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Encountering json with this form of delimiters is not uncommon for agent scenarios. | ||
| /// </remarks> | ||
| public static class JsonResultTranslator | ||
| { | ||
| private const string LiteralDelimiter = "```"; | ||
| private const string JsonPrefix = "json"; | ||
|
|
||
| /// <summary> | ||
| /// Utility method for extracting a JSON result from an agent response. | ||
| /// </summary> | ||
| /// <param name="result">A text result</param> | ||
| /// <typeparam name="TResult">The target type of the <see cref="FunctionResult"/>.</typeparam> | ||
| /// <returns>The JSON translated to the requested type.</returns> | ||
| public static TResult? Translate<TResult>(string result) | ||
| { | ||
| string rawJson = ExtractJson(result); | ||
|
|
||
| return JsonSerializer.Deserialize<TResult>(rawJson); | ||
| } | ||
|
|
||
| private static string ExtractJson(string result) | ||
| { | ||
| // Search for initial literal delimiter: ``` | ||
| int startIndex = result.IndexOf(LiteralDelimiter, System.StringComparison.Ordinal); | ||
| if (startIndex < 0) | ||
| { | ||
| // No initial delimiter, return entire expression. | ||
| return result; | ||
| } | ||
|
|
||
| startIndex += LiteralDelimiter.Length; | ||
|
|
||
| // Accommodate "json" prefix, if present. | ||
| if (JsonPrefix.Equals(result.Substring(startIndex, JsonPrefix.Length), System.StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| startIndex += JsonPrefix.Length; | ||
| } | ||
|
|
||
| // Locate final literal delimiter | ||
| int endIndex = result.IndexOf(LiteralDelimiter, startIndex, System.StringComparison.OrdinalIgnoreCase); | ||
| if (endIndex < 0) | ||
| { | ||
| endIndex = result.Length; | ||
| } | ||
|
|
||
| // Extract JSON | ||
| return result.Substring(startIndex, endIndex - startIndex); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.