Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion samples/cs/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<ItemGroup>
<PackageVersion Include="Microsoft.AI.Foundry.Local" Version="0.9.0-dev" />
<PackageVersion Include="Microsoft.AI.Foundry.Local.WinML" Version="0.9.0-dev-20260324" />
<PackageVersion Include="Betalgo.Ranul.OpenAI" Version="9.1.1" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="9.0.10" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.10" />
<PackageVersion Include="NAudio" Version="2.2.1" />
Expand Down
2 changes: 1 addition & 1 deletion samples/cs/model-management-example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.AI.Foundry.Local;
using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
using Microsoft.AI.Foundry.Local.OpenAI;
using System.Diagnostics;

CancellationToken ct = new CancellationToken();
Expand Down Expand Up @@ -123,7 +123,7 @@
var streamingResponse = chatClient.CompleteChatStreamingAsync(messages, ct);
await foreach (var chunk in streamingResponse)
{
Console.Write(chunk.Choices[0].Message.Content);

Check warning on line 126 in samples/cs/model-management-example/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (windows)

Dereference of a possibly null reference.

Check warning on line 126 in samples/cs/model-management-example/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (windows)

Dereference of a possibly null reference.
Console.Out.Flush();
}
Console.WriteLine();
Expand Down
2 changes: 1 addition & 1 deletion samples/cs/native-chat-completions/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// <complete_code>
// <imports>
using Microsoft.AI.Foundry.Local;
using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
using Microsoft.AI.Foundry.Local.OpenAI;
// </imports>

// <init>
Expand Down Expand Up @@ -98,7 +98,7 @@
var streamingResponse = chatClient.CompleteChatStreamingAsync(messages, ct);
await foreach (var chunk in streamingResponse)
{
Console.Write(chunk.Choices[0].Message.Content);

Check warning on line 101 in samples/cs/native-chat-completions/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (windows)

Dereference of a possibly null reference.

Check warning on line 101 in samples/cs/native-chat-completions/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (windows)

Dereference of a possibly null reference.
Console.Out.Flush();
}
Console.WriteLine();
Expand Down
4 changes: 1 addition & 3 deletions samples/cs/tool-calling-foundry-local-sdk/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// <complete_code>
// <imports>
using Microsoft.AI.Foundry.Local;
using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
using Betalgo.Ranul.OpenAI.ObjectModels.ResponseModels;
using Betalgo.Ranul.OpenAI.ObjectModels.SharedModels;
using Microsoft.AI.Foundry.Local.OpenAI;
using System.Text.Json;
// </imports>

Expand Down Expand Up @@ -104,7 +102,7 @@
var streamingResponse = chatClient.CompleteChatStreamingAsync(messages, tools, ct);
await foreach (var chunk in streamingResponse)
{
var content = chunk.Choices[0].Message.Content;

Check warning on line 105 in samples/cs/tool-calling-foundry-local-sdk/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (windows)

Dereference of a possibly null reference.

Check warning on line 105 in samples/cs/tool-calling-foundry-local-sdk/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (windows)

Dereference of a possibly null reference.
Console.Write(content);
Console.Out.Flush();

Expand All @@ -119,7 +117,7 @@
// Invoke tools called and append responses to the chat
foreach (var chunk in toolCallResponses)
{
var call = chunk?.Choices[0].Message.ToolCalls?[0].FunctionCall;

Check warning on line 120 in samples/cs/tool-calling-foundry-local-sdk/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (windows)

Dereference of a possibly null reference.

Check warning on line 120 in samples/cs/tool-calling-foundry-local-sdk/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (windows)

Dereference of a possibly null reference.
if (call?.Name == "multiply_numbers")
{
var arguments = JsonSerializer.Deserialize<Dictionary<string, int>>(call.Arguments!)!;
Expand Down Expand Up @@ -155,7 +153,7 @@
streamingResponse = chatClient.CompleteChatStreamingAsync(messages, tools, ct);
await foreach (var chunk in streamingResponse)
{
var content = chunk.Choices[0].Message.Content;

Check warning on line 156 in samples/cs/tool-calling-foundry-local-sdk/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (windows)

Dereference of a possibly null reference.

Check warning on line 156 in samples/cs/tool-calling-foundry-local-sdk/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (windows)

Dereference of a possibly null reference.
Console.Write(content);
Console.Out.Flush();
}
Expand Down
2 changes: 1 addition & 1 deletion samples/cs/tutorial-chat-assistant/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// <complete_code>
// <imports>
using Microsoft.AI.Foundry.Local;
using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
using Microsoft.AI.Foundry.Local.OpenAI;
using Microsoft.Extensions.Logging;
// </imports>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

<!-- Packages -->
<ItemGroup>
<PackageReference Include="Betalgo.Ranul.OpenAI" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion samples/cs/tutorial-document-summarizer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// <complete_code>
// <imports>
using Microsoft.AI.Foundry.Local;
using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
using Microsoft.AI.Foundry.Local.OpenAI;
using Microsoft.Extensions.Logging;
// </imports>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

<!-- Packages -->
<ItemGroup>
<PackageReference Include="Betalgo.Ranul.OpenAI" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
</ItemGroup>
Expand Down
4 changes: 1 addition & 3 deletions samples/cs/tutorial-tool-calling/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// <imports>
using System.Text.Json;
using Microsoft.AI.Foundry.Local;
using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
using Betalgo.Ranul.OpenAI.ObjectModels.ResponseModels;
using Betalgo.Ranul.OpenAI.ObjectModels.SharedModels;
using Microsoft.AI.Foundry.Local.OpenAI;
using Microsoft.Extensions.Logging;
// </imports>

Expand Down Expand Up @@ -175,21 +173,21 @@

var choice = response.Choices[0].Message;

if (choice.ToolCalls is { Count: > 0 })

Check warning on line 176 in samples/cs/tutorial-tool-calling/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (macos)

Dereference of a possibly null reference.

Check warning on line 176 in samples/cs/tutorial-tool-calling/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (macos)

Dereference of a possibly null reference.
{
messages.Add(choice);

foreach (var toolCall in choice.ToolCalls)
{
var toolArgs = JsonDocument.Parse(
toolCall.FunctionCall.Arguments

Check warning on line 183 in samples/cs/tutorial-tool-calling/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (macos)

Possible null reference argument for parameter 'json' in 'JsonDocument JsonDocument.Parse(string json, JsonDocumentOptions options = default(JsonDocumentOptions))'.

Check warning on line 183 in samples/cs/tutorial-tool-calling/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (macos)

Dereference of a possibly null reference.

Check warning on line 183 in samples/cs/tutorial-tool-calling/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (macos)

Possible null reference argument for parameter 'json' in 'JsonDocument JsonDocument.Parse(string json, JsonDocumentOptions options = default(JsonDocumentOptions))'.

Check warning on line 183 in samples/cs/tutorial-tool-calling/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (macos)

Dereference of a possibly null reference.
).RootElement;
Console.WriteLine(
$" Tool call: {toolCall.FunctionCall.Name}({toolArgs})"
);

var result = ExecuteTool(
toolCall.FunctionCall.Name, toolArgs

Check warning on line 190 in samples/cs/tutorial-tool-calling/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (macos)

Possible null reference argument for parameter 'functionName' in 'string ExecuteTool(string functionName, JsonElement arguments)'.

Check warning on line 190 in samples/cs/tutorial-tool-calling/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (macos)

Possible null reference argument for parameter 'functionName' in 'string ExecuteTool(string functionName, JsonElement arguments)'.
);
messages.Add(new ChatMessage
{
Expand All @@ -202,7 +200,7 @@
var finalResponse = await chatClient.CompleteChatAsync(
messages, tools, ct
);
var answer = finalResponse.Choices[0].Message.Content ?? "";

Check warning on line 203 in samples/cs/tutorial-tool-calling/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (macos)

Dereference of a possibly null reference.

Check warning on line 203 in samples/cs/tutorial-tool-calling/Program.cs

View workflow job for this annotation

GitHub Actions / cs-samples (macos)

Dereference of a possibly null reference.
messages.Add(new ChatMessage
{
Role = "assistant",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

<!-- Packages -->
<ItemGroup>
<PackageReference Include="Betalgo.Ranul.OpenAI" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion samples/cs/tutorial-voice-to-text/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// <complete_code>
// <imports>
using Microsoft.AI.Foundry.Local;
using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
using Microsoft.AI.Foundry.Local.OpenAI;
using Microsoft.Extensions.Logging;
using System.Text;
// </imports>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

<!-- Packages -->
<ItemGroup>
<PackageReference Include="Betalgo.Ranul.OpenAI" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions sdk/cs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Catalog access no longer blocks on EP downloads. Call `DownloadAndRegisterEpsAsy
using Microsoft.AI.Foundry.Local;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
using Microsoft.AI.Foundry.Local.OpenAI;

// 1. Initialize the singleton manager
await FoundryLocalManager.CreateAsync(
Expand Down Expand Up @@ -276,7 +276,7 @@ audioClient.Settings.Temperature = 0.0f;

For real-time microphone-to-text transcription, use `CreateLiveTranscriptionSession()`. Audio is pushed as raw PCM chunks and transcription results stream back as an `IAsyncEnumerable`.

The streaming result type (`LiveAudioTranscriptionResponse`) extends `ConversationItem` from the Betalgo OpenAI SDK's Realtime models, so it's compatible with the OpenAI Realtime API pattern. Access transcribed text via `result.Content[0].Text` or `result.Content[0].Transcript`.
The streaming result type (`LiveAudioTranscriptionResponse`) extends `ConversationItem`, which is compatible with the OpenAI Realtime API pattern. Access transcribed text via `result.Content[0].Text` or `result.Content[0].Transcript`.

```csharp
var audioClient = await model.GetAudioClientAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Namespace: Microsoft.AI.Foundry.Local

Audio Client that uses the OpenAI API.
Implemented using Betalgo.Ranul.OpenAI SDK types.

```csharp
public class OpenAIAudioClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Namespace: Microsoft.AI.Foundry.Local

Chat Client that uses the OpenAI API.
Implemented using Betalgo.Ranul.OpenAI SDK types.

```csharp
public class OpenAIChatClient
Expand Down
11 changes: 3 additions & 8 deletions sdk/cs/src/Detail/JsonSerializationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,28 @@ namespace Microsoft.AI.Foundry.Local.Detail;
using System.Text.Json;
using System.Text.Json.Serialization;

using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
using Betalgo.Ranul.OpenAI.ObjectModels.ResponseModels;
using Betalgo.Ranul.OpenAI.ObjectModels.SharedModels;

using Microsoft.AI.Foundry.Local.OpenAI;

[JsonSerializable(typeof(ModelInfo))]
[JsonSerializable(typeof(List<ModelInfo>))]
[JsonSerializable(typeof(CoreInteropRequest))]
[JsonSerializable(typeof(ChatCompletionCreateRequestExtended))]
[JsonSerializable(typeof(ChatCompletionCreateRequest))]
[JsonSerializable(typeof(ChatCompletionCreateResponse))]
[JsonSerializable(typeof(AudioCreateTranscriptionRequest))]
[JsonSerializable(typeof(AudioCreateTranscriptionResponse))]
[JsonSerializable(typeof(string[]))] // list loaded or cached models
[JsonSerializable(typeof(EpInfo[]))]
[JsonSerializable(typeof(EpDownloadResult))]
[JsonSerializable(typeof(JsonElement))]
[JsonSerializable(typeof(ResponseFormatExtended))]
[JsonSerializable(typeof(ResponseFormat))]
[JsonSerializable(typeof(ToolChoice))]
[JsonSerializable(typeof(ToolDefinition))]
[JsonSerializable(typeof(IList<ToolDefinition>))]
[JsonSerializable(typeof(FunctionDefinition))]
[JsonSerializable(typeof(IList<FunctionDefinition>))]
[JsonSerializable(typeof(PropertyDefinition))]
[JsonSerializable(typeof(IList<PropertyDefinition>))]
// --- Audio streaming types (LiveAudioTranscriptionResponse inherits ConversationItem
// which has AOT-incompatible JsonConverters, so we only register the raw deserialization type) ---
// --- Audio streaming types (we only register the raw deserialization type used by LiveAudioTranscriptionResponse.FromJson) ---
[JsonSerializable(typeof(LiveAudioTranscriptionRaw))]
[JsonSerializable(typeof(CoreErrorResponse))]
[JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Expand Down
1 change: 0 additions & 1 deletion sdk/cs/src/Microsoft.AI.Foundry.Local.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
<PackageReference Condition="'$(UseWinML)' != 'true'"
Include="Microsoft.AI.Foundry.Local.Core" Version="$(FoundryLocalCoreVersion)" />

<PackageReference Include="Betalgo.Ranul.OpenAI" Version="9.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.9" />
<!-- specify PrivateAssets to exclude from nuget dependencies -->
<PackageReference Include="IDisposableAnalyzers" Version="4.0.8" PrivateAssets="all"/>
Expand Down
8 changes: 2 additions & 6 deletions sdk/cs/src/OpenAI/AudioClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ namespace Microsoft.AI.Foundry.Local;

using System.Runtime.CompilerServices;
using System.Threading.Channels;
using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
using Betalgo.Ranul.OpenAI.ObjectModels.ResponseModels;

using Microsoft.AI.Foundry.Local.Detail;
using Microsoft.AI.Foundry.Local.OpenAI;
using Microsoft.Extensions.Logging;

/// <summary>
/// Audio Client that uses the OpenAI API.
/// Implemented using Betalgo.Ranul.OpenAI SDK types.
/// </summary>
public class OpenAIAudioClient
{
Expand Down Expand Up @@ -97,8 +94,7 @@ public LiveAudioTranscriptionSession CreateLiveTranscriptionSession()
private async Task<AudioCreateTranscriptionResponse> TranscribeAudioImplAsync(string audioFilePath,
CancellationToken? ct)
{
var openaiRequest = AudioTranscriptionCreateRequestExtended.FromUserInput(_modelId, audioFilePath, Settings);

var openaiRequest = AudioTranscriptionRequestResponseExtensions.CreateAudioRequest(_modelId, audioFilePath, Settings);

var request = new CoreInteropRequest
{
Expand All @@ -120,7 +116,7 @@ private async Task<AudioCreateTranscriptionResponse> TranscribeAudioImplAsync(st
private async IAsyncEnumerable<AudioCreateTranscriptionResponse> TranscribeAudioStreamingImplAsync(
string audioFilePath, [EnumeratorCancellation] CancellationToken ct)
{
var openaiRequest = AudioTranscriptionCreateRequestExtended.FromUserInput(_modelId, audioFilePath, Settings);
var openaiRequest = AudioTranscriptionRequestResponseExtensions.CreateAudioRequest(_modelId, audioFilePath, Settings);

var request = new CoreInteropRequest
{
Expand Down
29 changes: 7 additions & 22 deletions sdk/cs/src/OpenAI/AudioTranscriptionRequestResponseTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,21 @@ namespace Microsoft.AI.Foundry.Local.OpenAI;

using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
using Betalgo.Ranul.OpenAI.ObjectModels.ResponseModels;

using Microsoft.AI.Foundry.Local;
using Microsoft.AI.Foundry.Local.Detail;

using Microsoft.Extensions.Logging;

internal record AudioTranscriptionCreateRequestExtended : AudioCreateTranscriptionRequest
internal static class AudioTranscriptionRequestResponseExtensions
{
// Valid entries:
// int language
// int temperature
[JsonPropertyName("metadata")]
public Dictionary<string, string>? Metadata { get; set; }

internal static AudioTranscriptionCreateRequestExtended FromUserInput(string modelId,
string audioFilePath,
OpenAIAudioClient.AudioSettings settings)
internal static AudioCreateTranscriptionRequest CreateAudioRequest(string modelId,
string audioFilePath,
OpenAIAudioClient.AudioSettings settings)
{
var request = new AudioTranscriptionCreateRequestExtended
var request = new AudioCreateTranscriptionRequest
{
Model = modelId,
FileName = audioFilePath,

// apply our specific settings
Language = settings.Language,
Temperature = settings.Temperature
};
Expand All @@ -57,16 +44,14 @@ internal static AudioTranscriptionCreateRequestExtended FromUserInput(string mod
request.Metadata = metadata;
}


return request;
}
}
internal static class AudioTranscriptionRequestResponseExtensions
{

internal static string ToJson(this AudioCreateTranscriptionRequest request)
{
return JsonSerializer.Serialize(request, JsonSerializationContext.Default.AudioCreateTranscriptionRequest);
}

internal static AudioCreateTranscriptionResponse ToAudioTranscription(this ICoreInterop.Response response,
ILogger logger)
{
Expand Down
10 changes: 3 additions & 7 deletions sdk/cs/src/OpenAI/ChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@ namespace Microsoft.AI.Foundry.Local;
using System.Runtime.CompilerServices;
using System.Threading.Channels;

using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
using Betalgo.Ranul.OpenAI.ObjectModels.ResponseModels;

using Microsoft.AI.Foundry.Local.Detail;
using Microsoft.AI.Foundry.Local.OpenAI;
using Microsoft.Extensions.Logging;

/// <summary>
/// Chat Client that uses the OpenAI API.
/// Implemented using Betalgo.Ranul.OpenAI SDK types.
/// </summary>
public class OpenAIChatClient
{
Expand Down Expand Up @@ -48,7 +44,7 @@ public record ChatSettings
public int? TopK { get; set; }
public float? TopP { get; set; }
// Settings for tool calling and structured outputs
public ResponseFormatExtended? ResponseFormat { get; set; }
public ResponseFormat? ResponseFormat { get; set; }
public ToolChoice? ToolChoice { get; set; }
}

Expand Down Expand Up @@ -132,7 +128,7 @@ private async Task<ChatCompletionCreateResponse> CompleteChatImplAsync(IEnumerab
{
Settings.Stream = false;

var chatRequest = ChatCompletionCreateRequestExtended.FromUserInput(_modelId, messages, tools, Settings);
var chatRequest = ChatCompletionsRequestResponseExtensions.CreateChatRequest(_modelId, messages, tools, Settings);
var chatRequestJson = chatRequest.ToJson();

var request = new CoreInteropRequest { Params = new() { { "OpenAICreateRequest", chatRequestJson } } };
Expand All @@ -150,7 +146,7 @@ private async IAsyncEnumerable<ChatCompletionCreateResponse> ChatStreamingImplAs
{
Settings.Stream = true;

var chatRequest = ChatCompletionCreateRequestExtended.FromUserInput(_modelId, messages, tools, Settings);
var chatRequest = ChatCompletionsRequestResponseExtensions.CreateChatRequest(_modelId, messages, tools, Settings);
var chatRequestJson = chatRequest.ToJson();
var request = new CoreInteropRequest { Params = new() { { "OpenAICreateRequest", chatRequestJson } } };

Expand Down
38 changes: 8 additions & 30 deletions sdk/cs/src/OpenAI/ChatCompletionRequestResponseTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,30 @@ namespace Microsoft.AI.Foundry.Local.OpenAI;

using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
using Betalgo.Ranul.OpenAI.ObjectModels.ResponseModels;

using Microsoft.AI.Foundry.Local;
using Microsoft.AI.Foundry.Local.Detail;
using Microsoft.Extensions.Logging;

// https://platform.openai.com/docs/api-reference/chat/create
// Using the Betalgo ChatCompletionCreateRequest and extending with the `metadata` field for additional parameters
// which is part of the OpenAI spec but for some reason not part of the Betalgo request object.
internal class ChatCompletionCreateRequestExtended : ChatCompletionCreateRequest
internal static class ChatCompletionsRequestResponseExtensions
{
// Valid entries:
// int top_k
// int random_seed
[JsonPropertyName("metadata")]
public Dictionary<string, string>? Metadata { get; set; }

[JsonPropertyName("response_format")]
public new ResponseFormatExtended? ResponseFormat { get; set; }

internal static ChatCompletionCreateRequestExtended FromUserInput(string modelId,
IEnumerable<ChatMessage> messages,
IEnumerable<ToolDefinition>? tools,
OpenAIChatClient.ChatSettings settings)
internal static ChatCompletionCreateRequest CreateChatRequest(string modelId,
IEnumerable<ChatMessage> messages,
IEnumerable<ToolDefinition>? tools,
OpenAIChatClient.ChatSettings settings)
{
var request = new ChatCompletionCreateRequestExtended
var request = new ChatCompletionCreateRequest
{
Model = modelId,
Messages = messages.ToList(),
Tools = tools?.ToList(),
// Apply our specific settings
FrequencyPenalty = settings.FrequencyPenalty,
MaxTokens = settings.MaxTokens,
N = settings.N,
Temperature = settings.Temperature,
PresencePenalty = settings.PresencePenalty,
Stream = settings.Stream,
TopP = settings.TopP,
// Apply tool calling and structured output settings
ResponseFormat = settings.ResponseFormat,
ToolChoice = settings.ToolChoice
};
Expand All @@ -71,16 +53,12 @@ internal static ChatCompletionCreateRequestExtended FromUserInput(string modelId
request.Metadata = metadata;
}


return request;
}
}

internal static class ChatCompletionsRequestResponseExtensions
{
internal static string ToJson(this ChatCompletionCreateRequestExtended request)
internal static string ToJson(this ChatCompletionCreateRequest request)
{
return JsonSerializer.Serialize(request, JsonSerializationContext.Default.ChatCompletionCreateRequestExtended);
return JsonSerializer.Serialize(request, JsonSerializationContext.Default.ChatCompletionCreateRequest);
}

internal static ChatCompletionCreateResponse ToChatCompletion(this ICoreInterop.Response response, ILogger logger)
Expand Down
Loading
Loading