Skip to content

Commit

Permalink
Add a few missing options to OpenAIChatclient.ToOpenAIOptions (#5727)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Dec 9, 2024
1 parent 0bf057b commit eb383d4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PropertyGroup>
<Stage>preview</Stage>
<SuppressFinalPackageVersion>true</SuppressFinalPackageVersion>
<MinCodeCoverage>66</MinCodeCoverage>
<MinCodeCoverage>72</MinCodeCoverage>
<MinMutationScore>0</MinMutationScore>
</PropertyGroup>

Expand Down
13 changes: 13 additions & 0 deletions src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,19 @@ private static ChatCompletionOptions ToOpenAIOptions(ChatOptions? options)
{
result.TopLogProbabilityCount = topLogProbabilityCountInt;
}

if (additionalProperties.TryGetValue(nameof(result.Metadata), out IDictionary<string, string>? metadata))
{
foreach (KeyValuePair<string, string> kvp in metadata)
{
result.Metadata[kvp.Key] = kvp.Value;
}
}

if (additionalProperties.TryGetValue(nameof(result.StoredOutputEnabled), out bool storeOutputEnabled))
{
result.StoredOutputEnabled = storeOutputEnabled;
}
}

if (options.Tools is { Count: > 0 } tools)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,60 @@ public async Task BasicRequestResponse_Streaming()
}, usage.Details.AdditionalCounts);
}

[Fact]
public async Task NonStronglyTypedOptions_AllSent()
{
const string Input = """
{"messages":[{"role":"user","content":"hello"}],
"model":"gpt-4o-mini",
"store":true,
"metadata":{"something":"else"},
"logit_bias":{"12":34},
"logprobs":true,
"top_logprobs":42,
"parallel_tool_calls":false,
"user":"12345"}
""";

const string Output = """
{
"id": "chatcmpl-ADx3PvAnCwJg0woha4pYsBTi3ZpOI",
"object": "chat.completion",
"model": "gpt-4o-mini-2024-07-18",
"choices": [
{
"message": {
"role": "assistant",
"content": "Hello! How can I assist you today?"
},
"finish_reason": "stop"
}
]
}
""";

using VerbatimHttpHandler handler = new(Input, Output);
using HttpClient httpClient = new(handler);
using IChatClient client = CreateChatClient(httpClient, "gpt-4o-mini");

Assert.NotNull(await client.CompleteAsync("hello", new()
{
AdditionalProperties = new()
{
["StoredOutputEnabled"] = true,
["Metadata"] = new Dictionary<string, string>
{
["something"] = "else",
},
["LogitBiases"] = new Dictionary<int, int> { { 12, 34 } },
["IncludeLogProbabilities"] = true,
["TopLogProbabilityCount"] = 42,
["AllowParallelToolCalls"] = false,
["EndUserId"] = "12345",
},
}));
}

[Fact]
public async Task MultipleMessages_NonStreaming()
{
Expand Down

0 comments on commit eb383d4

Please sign in to comment.