-
Notifications
You must be signed in to change notification settings - Fork 189
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
[C#] feat: Implemented basic changes needed to support streaming tool calls #2214
Merged
singhk97
merged 18 commits into
microsoft:dotnet-dev
from
Stevenic:stevenic/dotnet-streaming
Dec 9, 2024
Merged
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
17ab55a
[repo] fix: Update CODEOWNERS (#2181)
singhk97 7d4b02b
[C#] bump: dotnet to v1.8.1 (#2180)
singhk97 650a6a3
[repo] bump: (deps): Bump the production group across 1 directory wit…
dependabot[bot] be7c894
[JS] bump: (deps): Bump the production group across 1 directory with …
dependabot[bot] e6186c1
[JS] bump: (deps-dev): Bump the development group across 1 directory …
dependabot[bot] 524f08c
[PY] fix: content safety public preview deprecation (#2207)
lilyydu bfba04b
Implemented basic changes needed to support streaming tool calls
Stevenic cd919c0
[JS] feat: support for v2 Assistants (#2193)
corinagum a8cc2c2
[JS] feat: add helper functions to to get Teams channels, members, an…
yiqing-zhao d7446e2
[repo] bump: (deps): Bump the production group with 3 updates (#2203)
dependabot[bot] 60209fc
Merge branch 'main' into stevenic/dotnet-streaming
Stevenic d8f56ae
Updated LightBot sample
Stevenic ad65ed8
Merge branch 'stevenic/dotnet-streaming' of https://github.com/Steven…
Stevenic eb63a75
DO NOT MERGE [JS] feat: custom feedback form + citation changes (#2182)
aacebo 8e33ff8
Applied fixes from Kavin
Stevenic 0671c79
Merge branch 'main' into stevenic/dotnet-streaming
Stevenic 8669f7a
Added missed removal to MaxOutputTokenCount
Stevenic 7cde4d8
Merge branch 'dotnet-dev' into stevenic/dotnet-streaming
Stevenic 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
Validating CODEOWNERS rules …
This file contains 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 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 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 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 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 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 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 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 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
65 changes: 65 additions & 0 deletions
65
dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs
This file contains 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,65 @@ | ||
using System.Buffers; | ||
using System.Diagnostics; | ||
|
||
namespace Microsoft.Teams.AI.AI.Models | ||
{ | ||
public class SequenceBuilder<T> | ||
{ | ||
private Segment _first; | ||
private Segment _last; | ||
|
||
public void Append(ReadOnlyMemory<T> data) | ||
{ | ||
if (_first == null) | ||
{ | ||
Debug.Assert(_last == null); | ||
_first = new Segment(data); | ||
_last = _first; | ||
} | ||
else | ||
{ | ||
_last = _last!.Append(data); | ||
} | ||
} | ||
|
||
public ReadOnlySequence<T> Build() | ||
{ | ||
if (_first == null) | ||
{ | ||
Debug.Assert(_last == null); | ||
return ReadOnlySequence<T>.Empty; | ||
} | ||
|
||
if (_first == _last) | ||
{ | ||
Debug.Assert(_first.Next == null); | ||
return new ReadOnlySequence<T>(_first.Memory); | ||
} | ||
|
||
return new ReadOnlySequence<T>(_first, 0, _last!, _last!.Memory.Length); | ||
} | ||
|
||
private sealed class Segment : ReadOnlySequenceSegment<T> | ||
{ | ||
public Segment(ReadOnlyMemory<T> items) : this(items, 0) | ||
{ | ||
} | ||
|
||
private Segment(ReadOnlyMemory<T> items, long runningIndex) | ||
{ | ||
Debug.Assert(runningIndex >= 0); | ||
Memory = items; | ||
RunningIndex = runningIndex; | ||
} | ||
|
||
public Segment Append(ReadOnlyMemory<T> items) | ||
{ | ||
long runningIndex; | ||
checked { runningIndex = RunningIndex + Memory.Length; } | ||
Segment segment = new(items, runningIndex); | ||
Next = segment; | ||
return segment; | ||
} | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...t/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs
This file contains 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,59 @@ | ||
using OpenAI.Chat; | ||
using System.Buffers; | ||
|
||
namespace Microsoft.Teams.AI.AI.Models | ||
{ | ||
public class StreamingChatToolCallsBuilder | ||
{ | ||
private readonly Dictionary<int, string> _indexToToolCallId = []; | ||
private readonly Dictionary<int, string> _indexToFunctionName = []; | ||
private readonly Dictionary<int, SequenceBuilder<byte>> _indexToFunctionArguments = []; | ||
|
||
public void Append(StreamingChatToolCallUpdate toolCallUpdate) | ||
{ | ||
// Keep track of which tool call ID belongs to this update index. | ||
if (toolCallUpdate.ToolCallId != null) | ||
{ | ||
_indexToToolCallId[toolCallUpdate.Index] = toolCallUpdate.ToolCallId; | ||
} | ||
|
||
// Keep track of which function name belongs to this update index. | ||
if (toolCallUpdate.FunctionName != null) | ||
{ | ||
_indexToFunctionName[toolCallUpdate.Index] = toolCallUpdate.FunctionName; | ||
} | ||
|
||
// Keep track of which function arguments belong to this update index, | ||
// and accumulate the arguments as new updates arrive. | ||
if (toolCallUpdate.FunctionArgumentsUpdate != null && !toolCallUpdate.FunctionArgumentsUpdate.ToMemory().IsEmpty) | ||
{ | ||
if (!_indexToFunctionArguments.TryGetValue(toolCallUpdate.Index, out SequenceBuilder<byte> argumentsBuilder)) | ||
{ | ||
argumentsBuilder = new SequenceBuilder<byte>(); | ||
_indexToFunctionArguments[toolCallUpdate.Index] = argumentsBuilder; | ||
} | ||
|
||
argumentsBuilder.Append(toolCallUpdate.FunctionArgumentsUpdate); | ||
} | ||
} | ||
|
||
public IReadOnlyList<ChatToolCall> Build() | ||
{ | ||
List<ChatToolCall> toolCalls = []; | ||
|
||
foreach (KeyValuePair<int, string> indexToToolCallIdPair in _indexToToolCallId) | ||
{ | ||
ReadOnlySequence<byte> sequence = _indexToFunctionArguments[indexToToolCallIdPair.Key].Build(); | ||
|
||
ChatToolCall toolCall = ChatToolCall.CreateFunctionToolCall( | ||
id: indexToToolCallIdPair.Value, | ||
functionName: _indexToFunctionName[indexToToolCallIdPair.Key], | ||
functionArguments: BinaryData.FromBytes(sequence.ToArray())); | ||
|
||
toolCalls.Add(toolCall); | ||
} | ||
|
||
return toolCalls; | ||
} | ||
} | ||
} |
This file contains 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this being used? if not can you remove it