-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
## Linked issues closes: #minor ## Details - #2210 - #2214 - #2227 --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: Lily Du <[email protected]> Co-authored-by: lilydu <[email protected]> Co-authored-by: Tarek Mahmoud Sayed <[email protected]> Co-authored-by: Steven Ickman <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Corina <[email protected]> Co-authored-by: Yiqing Zhao <[email protected]> Co-authored-by: Yiqing Zhao <[email protected]> Co-authored-by: Alex Acebo <[email protected]>
- Loading branch information
There are no files selected for viewing
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> | ||
Check warning on line 6 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (6.0)
Check warning on line 6 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Analyze
Check warning on line 6 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint (7.0)
Check warning on line 6 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (7.0)
Check warning on line 6 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint (6.0)
Check warning on line 6 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Publish (7.0)
|
||
{ | ||
private Segment _first; | ||
Check warning on line 8 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (6.0)
Check warning on line 8 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (6.0)
Check warning on line 8 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Analyze
Check warning on line 8 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Analyze
Check warning on line 8 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint (7.0)
Check warning on line 8 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint (7.0)
Check warning on line 8 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (7.0)
Check warning on line 8 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (7.0)
Check warning on line 8 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint (6.0)
Check warning on line 8 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint (6.0)
Check warning on line 8 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Publish (7.0)
Check warning on line 8 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Publish (6.0)
|
||
private Segment _last; | ||
Check warning on line 9 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (6.0)
Check warning on line 9 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (6.0)
Check warning on line 9 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Analyze
Check warning on line 9 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Analyze
Check warning on line 9 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint (7.0)
Check warning on line 9 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint (7.0)
Check warning on line 9 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (7.0)
Check warning on line 9 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (7.0)
Check warning on line 9 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint (6.0)
Check warning on line 9 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint (6.0)
Check warning on line 9 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Publish (7.0)
Check warning on line 9 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Publish (6.0)
|
||
|
||
public void Append(ReadOnlyMemory<T> data) | ||
Check warning on line 11 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (6.0)
Check warning on line 11 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Analyze
Check warning on line 11 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint (7.0)
Check warning on line 11 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (7.0)
Check warning on line 11 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint (6.0)
Check warning on line 11 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Publish (7.0)
|
||
{ | ||
if (_first == null) | ||
{ | ||
Debug.Assert(_last == null); | ||
_first = new Segment(data); | ||
_last = _first; | ||
} | ||
else | ||
{ | ||
_last = _last!.Append(data); | ||
} | ||
} | ||
|
||
public ReadOnlySequence<T> Build() | ||
Check warning on line 25 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (6.0)
Check warning on line 25 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Analyze
Check warning on line 25 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint (7.0)
Check warning on line 25 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (7.0)
Check warning on line 25 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Build/Test/Lint (6.0)
Check warning on line 25 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/SequenceBuilder.cs GitHub Actions / Publish (7.0)
|
||
{ | ||
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; | ||
} | ||
} | ||
} | ||
} |
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 | ||
Check warning on line 6 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (6.0)
Check warning on line 6 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Analyze
Check warning on line 6 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Build/Test/Lint (7.0)
Check warning on line 6 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (7.0)
Check warning on line 6 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Build/Test/Lint (6.0)
Check warning on line 6 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Publish (7.0)
|
||
{ | ||
private readonly Dictionary<int, string> _indexToToolCallId = []; | ||
private readonly Dictionary<int, string> _indexToFunctionName = []; | ||
private readonly Dictionary<int, SequenceBuilder<byte>> _indexToFunctionArguments = []; | ||
|
||
public void Append(StreamingChatToolCallUpdate toolCallUpdate) | ||
Check warning on line 12 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (6.0)
Check warning on line 12 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Analyze
Check warning on line 12 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Build/Test/Lint (7.0)
Check warning on line 12 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (7.0)
Check warning on line 12 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Build/Test/Lint (6.0)
Check warning on line 12 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Publish (7.0)
Check warning on line 12 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Publish (6.0)
|
||
{ | ||
// 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() | ||
Check warning on line 40 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (6.0)
Check warning on line 40 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Analyze
Check warning on line 40 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Build/Test/Lint (7.0)
Check warning on line 40 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Build/Test/Lint / Build/Test/Lint (7.0)
Check warning on line 40 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Build/Test/Lint (6.0)
Check warning on line 40 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/StreamingChatToolCallsBuilder.cs GitHub Actions / Publish (7.0)
|
||
{ | ||
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; | ||
} | ||
} | ||
} |