Skip to content

Conversation

DeagleGross
Copy link
Contributor

Description

Current PR provides the API to register custom endpoint to access the AIAgent via OpenAI ChatCompletions protocol:

app.MapOpenAIChatCompletions(agentName: ..., path: ...);

It supports the streaming and non-streaming chat completion request as per spec.

Because at the moment there is no API to restore the conversation/response/chat-completion, it does not support some of protocol APIs like get chat completion.

Implementation details

It is a similar PR to support of the OpenAI Responses protocol: #947.
I am using types defined in OpenAI SDK and using custom serialization implemented in the SDK itself. Also relying on the helpful MEAI extensions to convert input/output properly.

Validation

Here is the example of streaming events output (the most complicated scenario here):

data: {"id":"","choices":[],"created":0,"model":"","object":"","prompt_filter_results":[{"prompt_index":0,"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"jailbreak":{"filtered":false,"detected":false},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}}}]}

data: {"id":"chatcmpl-COOAs81LB4NvO877tA5kHst4NizLF","choices":[{"delta":{"content":"","role":"assistant"},"index":0,"content_filter_results":{}}],"created":1759928834,"model":"gpt-4o-2024-11-20","system_fingerprint":"fp_ee1d74bde0","object":"chat.completion.chunk","obfuscation":"XmCgtQd"}

data: {"id":"chatcmpl-COOAs81LB4NvO877tA5kHst4NizLF","choices":[{"delta":{"content":"Ah"},"index":0,"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}}}],"created":1759928834,"model":"gpt-4o-2024-11-20","system_fingerprint":"fp_ee1d74bde0","object":"chat.completion.chunk","obfuscation":"pcI44uB"}

....

data: {"id":"chatcmpl-COOAs81LB4NvO877tA5kHst4NizLF","choices":[{"delta":{},"finish_reason":"stop","index":0,"content_filter_results":{}}],"created":1759928834,"model":"gpt-4o-2024-11-20","system_fingerprint":"fp_ee1d74bde0","object":"chat.completion.chunk","obfuscation":"hZp"}

data: {"id":"chatcmpl-COOAs81LB4NvO877tA5kHst4NizLF","choices":[],"created":1759928834,"model":"gpt-4o-2024-11-20","system_fingerprint":"fp_ee1d74bde0","object":"chat.completion.chunk","usage":{"completion_tokens":40,"prompt_tokens":28,"total_tokens":68,"completion_tokens_details":{"accepted_prediction_tokens":0,"audio_tokens":0,"reasoning_tokens":0,"rejected_prediction_tokens":0},"prompt_tokens_details":{"audio_tokens":0,"cached_tokens":0}},"obfuscation":"X6YBnrwC"}

@DeagleGross DeagleGross self-assigned this Oct 8, 2025
@Copilot Copilot AI review requested due to automatic review settings October 8, 2025 13:29
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for OpenAI ChatCompletions protocol to enable access to hosted AI agents via the standard OpenAI API endpoints. The implementation supports both streaming and non-streaming chat completion requests as per the OpenAI specification.

  • Maps OpenAI ChatCompletions endpoints via MapOpenAIChatCompletions extension method
  • Implements streaming and non-streaming chat completion responses using OpenAI SDK types
  • Adds sample client demonstrating ChatCompletions protocol usage

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
EndpointRouteBuilderExtensions.Responses.cs Updated class to partial and generalized documentation for multiple OpenAI protocols
EndpointRouteBuilderExtensions.ChatCompletions.cs New extension methods for mapping ChatCompletions endpoints to AI agents
ChatCompletionsOptionsExtensions.cs Utility class using reflection to access internal OpenAI SDK properties
OpenAIChatCompletionsRunOptions.cs Simple sealed class extending AgentRunOptions for ChatCompletions
AIAgentChatCompletionsProcessor.cs Core processor handling both streaming and non-streaming ChatCompletions requests
Program.cs (AgentHost) Registers ChatCompletions endpoints for sample agents
Program.cs (Web) Adds ChatCompletions client to dependency injection
OpenAIChatCompletionsAgentClient.cs Sample client implementation using OpenAI SDK to communicate via ChatCompletions protocol
Home.razor UI updates to support ChatCompletions protocol selection

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@DeagleGross DeagleGross added this pull request to the merge queue Oct 14, 2025
Merged via the queue into main with commit f42a3ee Oct 14, 2025
14 checks passed
@ReubenBond ReubenBond deleted the dmkorolev/chat-completions branch October 14, 2025 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants