Skip to content
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

Add AsChatClient for OpenAI's AssistantClient #5852

Merged
merged 3 commits into from
Feb 10, 2025

Conversation

stephentoub
Copy link
Member

@stephentoub stephentoub commented Feb 6, 2025

  • Adds ChatOptions/ChatCompletion/StreamingChatCompletionUpdate.ChatThreadId.
  • Adds an AsChatClient extension method for creating an IChatClient from an AssistantClient.
  • Updates FunctionInvokingChatClient to have KeepFunctionCallingMessages
    be false by default, and to handle ChatThreadId.
  • Fixes handling of ChatCompletion.Usage in ToChatCompletion{Async}.

@dotnet-comment-bot
Copy link
Collaborator

‼️ Found issues ‼️

Project Coverage Type Expected Actual
Microsoft.Extensions.Caching.Hybrid Line 86 82.77 🔻
Microsoft.Extensions.AI.OpenAI Line 77 71.76 🔻
Microsoft.Extensions.AI.OpenAI Branch 77 53.47 🔻
Microsoft.Gen.MetadataExtractor Line 98 57.35 🔻
Microsoft.Gen.MetadataExtractor Branch 98 62.5 🔻
Microsoft.Extensions.AI.Ollama Line 80 78.25 🔻

🎉 Good job! The coverage increased 🎉
Update MinCodeCoverage in the project files.

Project Expected Actual
Microsoft.Extensions.AI 88 89
Microsoft.Extensions.AI.Abstractions 83 84

Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=943532&view=codecoverage-tab

- Adds ChatOptions/ChatCompletion/StreamingChatCompletionUpdate.ChatThreadId.
- Adds an AsChatClient extension method for creating an IChatClient from an AssistantClient.
- Updates FunctionInvokingChatClient to have KeepFunctionCallingMessages
  be false by default, and to handle ChatThreadId.
- Fixes handling of ChatCompletion.Usage in ToChatCompletion{Async}.
@stephentoub stephentoub force-pushed the openaiassistantsichatclient branch from 368e9d9 to a7826db Compare February 7, 2025 18:58
@dotnet-comment-bot
Copy link
Collaborator

‼️ Found issues ‼️

Project Coverage Type Expected Actual
Microsoft.Extensions.Caching.Hybrid Line 86 82.92 🔻
Microsoft.Extensions.AI.Ollama Line 80 78.11 🔻
Microsoft.Extensions.AI.OpenAI Line 77 71.92 🔻
Microsoft.Extensions.AI.OpenAI Branch 77 53.71 🔻
Microsoft.Gen.MetadataExtractor Line 98 57.35 🔻
Microsoft.Gen.MetadataExtractor Branch 98 62.5 🔻

🎉 Good job! The coverage increased 🎉
Update MinCodeCoverage in the project files.

Project Expected Actual
Microsoft.Extensions.AI.AzureAIInference 91 92
Microsoft.Extensions.AI.Abstractions 83 85

Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=944576&view=codecoverage-tab

@stephentoub stephentoub requested a review from Copilot February 7, 2025 19:34

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Overview

This pull request adds chat thread IDs for managing stateful conversations, introduces a new AsChatClient extension method for AssistantClient, and modifies the default value for KeepFunctionCallingMessages in FunctionInvokingChatClient from true to false. It also refactors how function-calling messages are retained or cleared, updates the handling of usage data aggregation, and includes various serialization, testing, and maintenance improvements.

  • Adds ChatThreadId properties to ChatOptions, ChatCompletion, and StreamingChatCompletionUpdate.
  • Changes the default KeepFunctionCallingMessages to false and removes the message-tracking cleanup code.
  • Provides a new AsChatClient(AssistantClient, string, string?) extension method returning OpenAIAssistantClient.
  • Enhances usage content coalescing in streaming updates and aggregates usage details properly in ChatCompletion.

Changes

File Description
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIAssistantClient.cs Implements an IChatClient that uses an AssistantClient, optionally with a shared thread ID.
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs Updates default KeepFunctionCallingMessages, refactors function call message handling and usage updates.
src/Libraries/Microsoft.Extensions.AI.Abstractions/ChatCompletion/ChatCompletion.cs Adds ChatThreadId property and usage aggregation improvements.
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatOptionsTests.cs Tests for new ChatThreadId property.
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIClientExtensions.cs Implements the new AsChatClient method for AssistantClient.
src/Libraries/Microsoft.Extensions.AI.Abstractions/ChatCompletion/ChatOptions.cs Adds ChatThreadId property to ChatOptions and updates clone logic.
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIChatClient.cs Minor internal refactoring for default endpoint handling.
test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientTests.cs Updates tests to cover the new KeepFunctionCallingMessages default and thread ID behaviors.
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIModelMapper.ChatCompletion.cs Exposes DefaultParameterSchema as a property and updates usage accordingly.
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIJsonContext.cs Adds support for string array serialization in JSON context.
src/Libraries/Microsoft.Extensions.AI.Abstractions/ChatCompletion/StreamingChatCompletionUpdate.cs Adds ChatThreadId property for streaming completion updates.
src/Libraries/Microsoft.Extensions.AI.Abstractions/ChatCompletion/ChatMessage.cs Adds a shallow Clone method for ChatMessage.
src/Libraries/Microsoft.Extensions.AI.Abstractions/ChatCompletion/StreamingChatCompletionUpdateExtensions.cs Improves how usage content is extracted and aggregated in streaming updates.

Copilot reviewed 14 out of 14 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs:283

  • Fix the typo 'Expecteded' to 'Expected'.
Debug.Assert(functionCount > 0, "Expecteded {nameof(functionCount)} to be > 0, got {functionCount}.");

Tip: Turn on automatic Copilot reviews for this repository to get quick feedback on every pull request. Learn more

@stephentoub stephentoub enabled auto-merge (squash) February 7, 2025 22:31
Copy link
Member

@SteveSandersonMS SteveSandersonMS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

  • Personally I'd be more inclined to leave KeepFunctionCallingMessages on by default as per https://github.com/dotnet/extensions/pull/5852/files?w=1#r1948821474 but I know this is a subjective call
  • Should there be some additional tests covering the FunctionInvokingChatClient behaviors around its detection/handling of ChatThreadId?
  • Technically also we have a load of integration tests for OpenAIChatClient that don't have an equivalent in the new OpenAIAssistantClient. However I know it's a huge pain to create them so will leave it with you to decide if there's enough value in doing so.

@stephentoub
Copy link
Member Author

Should there be some additional tests covering the FunctionInvokingChatClient behaviors around its detection/handling of ChatThreadId?

Yes, added. Thanks.

@dotnet-comment-bot
Copy link
Collaborator

‼️ Found issues ‼️

Project Coverage Type Expected Actual
Microsoft.Gen.MetadataExtractor Line 98 57.35 🔻
Microsoft.Gen.MetadataExtractor Branch 98 62.5 🔻
Microsoft.Extensions.Caching.Hybrid Line 86 82.77 🔻
Microsoft.Extensions.AI.OpenAI Line 77 71.71 🔻
Microsoft.Extensions.AI.OpenAI Branch 77 53.28 🔻
Microsoft.Extensions.AI.Ollama Line 80 78.2 🔻

🎉 Good job! The coverage increased 🎉
Update MinCodeCoverage in the project files.

Project Expected Actual
Microsoft.Extensions.AI.AzureAIInference 91 92
Microsoft.Extensions.AI.Abstractions 83 85

Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=946767&view=codecoverage-tab

@stephentoub stephentoub merged commit ecdc326 into dotnet:main Feb 10, 2025
6 checks passed
@jeffhandley jeffhandley added the area-ai Microsoft.Extensions.AI libraries label Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-ai Microsoft.Extensions.AI libraries
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants