Skip to content

Commit

Permalink
Implement OpenAIChatModelFactory, add Experimental to `OpenAI.Ass…
Browse files Browse the repository at this point in the history
…istants` and `OpenAI.VectorStores` APIs (openai#197)

* Added the `Experimental` attribute to all public APIs in the `OpenAI.Assistants` and `OpenAI.VectorStores` namespaces.
* Added the `OpenAIChatModelFactory` static class, which can be used to instantiate the output models from the `OpenAI.Chat` namespace for mocking in non-live test scenarios.
* Made `GeneratedSpeechVoice` an extensible enum.

Co-authored-by: ShivangiReja <[email protected]>
  • Loading branch information
ShivangiReja and ShivangiReja authored Aug 29, 2024
1 parent 583e9f6 commit 79014ab
Show file tree
Hide file tree
Showing 147 changed files with 1,447 additions and 227 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@

### Features Added

- Added the following model factory (a static class that can be used to instantiate OpenAI models for mocking in non-live test scenarios):
- `OpenAIChatModelFactory` in the `OpenAI.Chat` namespace

### Breaking Changes

- Updated fine-tuning pagination methods `GetJobs`, `GetEvents`, and `GetJobCheckpoints` to return `IEnumerable<ClientResult>` instead of `ClientResult`. (commit_hash)
- Updated the batching pagination method `GetBatches` to return `IEnumerable<ClientResult>` instead of `ClientResult`. (commit_hash)
- Changed `GeneratedSpeechVoice` from an enum to an "extensible enum". (commit_hash)

### Bugs Fixed

### Other Changes

- Reverted the removal of the version path parameter "v1" from the default endpoint URL. (commit_hash)
- Added the `Experimental` attribute to all public APIs in the `OpenAI.Assistants` namespace. (commit_hash)
- Added the `Experimental` attribute to all public APIs in the `OpenAI.VectorStores` namespace. (commit_hash)


## 2.0.0-beta.10 (2024-08-26)

Expand Down
35 changes: 28 additions & 7 deletions api/OpenAI.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,13 +1092,25 @@ public enum GeneratedSpeechFormat {
Wav = 4,
Pcm = 5
}
public enum GeneratedSpeechVoice {
Alloy = 0,
Echo = 1,
Fable = 2,
Onyx = 3,
Nova = 4,
Shimmer = 5
public readonly partial struct GeneratedSpeechVoice : IEquatable<GeneratedSpeechVoice> {
private readonly object _dummy;
private readonly int _dummyPrimitive;
public GeneratedSpeechVoice(string value);
public static GeneratedSpeechVoice Alloy { get; }
public static GeneratedSpeechVoice Echo { get; }
public static GeneratedSpeechVoice Fable { get; }
public static GeneratedSpeechVoice Nova { get; }
public static GeneratedSpeechVoice Onyx { get; }
public static GeneratedSpeechVoice Shimmer { get; }
public readonly bool Equals(GeneratedSpeechVoice other);
[EditorBrowsable(EditorBrowsableState.Never)]
public override readonly bool Equals(object obj);
[EditorBrowsable(EditorBrowsableState.Never)]
public override readonly int GetHashCode();
public static bool operator ==(GeneratedSpeechVoice left, GeneratedSpeechVoice right);
public static implicit operator GeneratedSpeechVoice(string value);
public static bool operator !=(GeneratedSpeechVoice left, GeneratedSpeechVoice right);
public override readonly string ToString();
}
public static class OpenAIAudioModelFactory {
public static AudioTranscription AudioTranscription(string language = null, TimeSpan? duration = null, string text = null, IEnumerable<TranscribedWord> words = null, IEnumerable<TranscribedSegment> segments = null);
Expand Down Expand Up @@ -1517,6 +1529,15 @@ public class FunctionChatMessage : ChatMessage, IJsonModel<FunctionChatMessage>,
public static bool operator !=(ImageChatMessageContentPartDetail left, ImageChatMessageContentPartDetail right);
public override readonly string ToString();
}
public static class OpenAIChatModelFactory {
public static ChatCompletion ChatCompletion(string id = null, ChatFinishReason finishReason = ChatFinishReason.Stop, IEnumerable<ChatMessageContentPart> content = null, string refusal = null, IEnumerable<ChatToolCall> toolCalls = null, ChatMessageRole role = ChatMessageRole.System, ChatFunctionCall functionCall = null, IEnumerable<ChatTokenLogProbabilityInfo> contentTokenLogProbabilities = null, IEnumerable<ChatTokenLogProbabilityInfo> refusalTokenLogProbabilities = null, DateTimeOffset createdAt = default, string model = null, string systemFingerprint = null, ChatTokenUsage usage = null);
public static ChatTokenLogProbabilityInfo ChatTokenLogProbabilityInfo(string token = null, float logProbability = 0, IEnumerable<int> utf8ByteValues = null, IEnumerable<ChatTokenTopLogProbabilityInfo> topLogProbabilities = null);
public static ChatTokenTopLogProbabilityInfo ChatTokenTopLogProbabilityInfo(string token = null, float logProbability = 0, IEnumerable<int> utf8ByteValues = null);
public static ChatTokenUsage ChatTokenUsage(int outputTokens = 0, int inputTokens = 0, int totalTokens = 0);
public static StreamingChatCompletionUpdate StreamingChatCompletionUpdate(string id = null, IEnumerable<ChatMessageContentPart> contentUpdate = null, StreamingChatFunctionCallUpdate functionCallUpdate = null, IEnumerable<StreamingChatToolCallUpdate> toolCallUpdates = null, ChatMessageRole? role = null, string refusalUpdate = null, IEnumerable<ChatTokenLogProbabilityInfo> contentTokenLogProbabilities = null, IEnumerable<ChatTokenLogProbabilityInfo> refusalTokenLogProbabilities = null, ChatFinishReason? finishReason = null, DateTimeOffset createdAt = default, string model = null, string systemFingerprint = null, ChatTokenUsage usage = null);
public static StreamingChatFunctionCallUpdate StreamingChatFunctionCallUpdate(string functionArgumentsUpdate = null, string functionName = null);
public static StreamingChatToolCallUpdate StreamingChatToolCallUpdate(int index = 0, string id = null, ChatToolCallKind kind = default, string functionName = null, string functionArgumentsUpdate = null);
}
public class StreamingChatCompletionUpdate : IJsonModel<StreamingChatCompletionUpdate>, IPersistableModel<StreamingChatCompletionUpdate> {
public IReadOnlyList<ChatTokenLogProbabilityInfo> ContentTokenLogProbabilities { get; }
public IReadOnlyList<ChatMessageContentPart> ContentUpdate { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public partial class AssistantExamples
public void Example01_RetrievalAugmentedGeneration()
{
// Assistants is a beta API and subject to change; acknowledge its experimental status by suppressing the matching warning.
#pragma warning disable OPENAI001
OpenAIClient openAIClient = new(Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
FileClient fileClient = openAIClient.GetFileClient();
AssistantClient assistantClient = openAIClient.GetAssistantClient();
Expand Down Expand Up @@ -99,7 +98,7 @@ public void Example01_RetrievalAugmentedGeneration()

// Finally, we'll print out the full history for the thread that includes the augmented generation
PageCollection<ThreadMessage> messagePages
= assistantClient.GetMessages(threadRun.ThreadId, new MessageCollectionOptions() { Order = ListOrder.OldestFirst });
= assistantClient.GetMessages(threadRun.ThreadId, new MessageCollectionOptions() { Order = ListOrder.OldestFirst });
IEnumerable<ThreadMessage> messages = messagePages.GetAllValues();

foreach (ThreadMessage message in messages)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public partial class AssistantExamples
public async Task Example01_RetrievalAugmentedGenerationAsync()
{
// Assistants is a beta API and subject to change; acknowledge its experimental status by suppressing the matching warning.
#pragma warning disable OPENAI001
OpenAIClient openAIClient = new(Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
FileClient fileClient = openAIClient.GetFileClient();
AssistantClient assistantClient = openAIClient.GetAssistantClient();
Expand Down
1 change: 0 additions & 1 deletion examples/Assistants/Example02_FunctionCalling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ string GetCurrentWeather(string location, string unit = "celsius")
#endregion

// Assistants is a beta API and subject to change; acknowledge its experimental status by suppressing the matching warning.
#pragma warning disable OPENAI001
AssistantClient client = new(Environment.GetEnvironmentVariable("OPENAI_API_KEY"));

#region
Expand Down
1 change: 0 additions & 1 deletion examples/Assistants/Example02_FunctionCallingAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ string GetCurrentWeather(string location, string unit = "celsius")
#endregion

// Assistants is a beta API and subject to change; acknowledge its experimental status by suppressing the matching warning.
#pragma warning disable OPENAI001
AssistantClient client = new(Environment.GetEnvironmentVariable("OPENAI_API_KEY"));

#region
Expand Down
1 change: 0 additions & 1 deletion examples/Assistants/Example02b_FunctionCallingStreaming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public async Task Example02b_FunctionCallingStreaming()
#endregion

// Assistants is a beta API and subject to change; acknowledge its experimental status by suppressing the matching warning.
#pragma warning disable OPENAI001
AssistantClient client = new(Environment.GetEnvironmentVariable("OPENAI_API_KEY"));

#region Create a new assistant with function tools
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public partial class AssistantExamples
public void Example03_ListAssistantsWithPagination()
{
// Assistants is a beta API and subject to change; acknowledge its experimental status by suppressing the matching warning.
#pragma warning disable OPENAI001
AssistantClient client = new(Environment.GetEnvironmentVariable("OPENAI_API_KEY"));

int count = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public partial class AssistantExamples
public async Task Example03_ListAssistantsWithPaginationAsync()
{
// Assistants is a beta API and subject to change; acknowledge its experimental status by suppressing the matching warning.
#pragma warning disable OPENAI001
AssistantClient client = new(Environment.GetEnvironmentVariable("OPENAI_API_KEY"));

int count = 0;
Expand Down
2 changes: 0 additions & 2 deletions examples/Assistants/Example04_AllTheTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public partial class AssistantExamples
[Test]
public void Example04_AllTheTools()
{
#pragma warning disable OPENAI001

#region Define a function tool
static string GetNameOfFamilyMember(string relation)
=> relation switch
Expand Down
1 change: 0 additions & 1 deletion examples/Assistants/Example05_AssistantsWithVision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public partial class AssistantExamples
public void Example05_AssistantsWithVision()
{
// Assistants is a beta API and subject to change; acknowledge its experimental status by suppressing the matching warning.
#pragma warning disable OPENAI001
OpenAIClient openAIClient = new(Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
FileClient fileClient = openAIClient.GetFileClient();
AssistantClient assistantClient = openAIClient.GetAssistantClient();
Expand Down
1 change: 0 additions & 1 deletion examples/Assistants/Example05_AssistantsWithVisionAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public partial class AssistantExamples
public async Task Example05_AssistantsWithVisionAsync()
{
// Assistants is a beta API and subject to change; acknowledge its experimental status by suppressing the matching warning.
#pragma warning disable OPENAI001
OpenAIClient openAIClient = new(Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
FileClient fileClient = openAIClient.GetFileClient();
AssistantClient assistantClient = openAIClient.GetAssistantClient();
Expand Down
7 changes: 4 additions & 3 deletions examples/Chat/Example01_SimpleChat_Cancellations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ public void Example01_SimpleChat_Cancellations()
RequestOptions options = new() { CancellationToken = ct.Token };

ChatMessage message = ChatMessage.CreateUserMessage("Say 'this is a test.'");
var body = new {
var body = new
{
model = "gpt-4o",
messages = new[] {
messages = new[] {
new
{
role = "user",
content = "Say \u0027this is a test.\u0027"
}
}
};

BinaryData json = BinaryData.FromObjectAsJson(body);
ClientResult result = client.CompleteChat(BinaryContent.Create(json), options);

Expand Down
2 changes: 1 addition & 1 deletion examples/Chat/Example07_StructuredOutputs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void Example07_StructuredOutputs()
options);

using JsonDocument structuredJson = JsonDocument.Parse(chatCompletion.ToString());

Console.WriteLine($"Final answer: {structuredJson.RootElement.GetProperty("final_answer").GetString()}");
Console.WriteLine("Reasoning steps:");

Expand Down
2 changes: 1 addition & 1 deletion examples/Chat/Example07_StructuredOutputsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task Example07_StructuredOutputsAsync()
options);

using JsonDocument structuredJson = JsonDocument.Parse(chatCompletion.ToString());

Console.WriteLine($"Final answer: {structuredJson.RootElement.GetProperty("final_answer").GetString()}");
Console.WriteLine("Reasoning steps:");

Expand Down
2 changes: 0 additions & 2 deletions examples/ClientExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public void CreateAssistantAndFileClients()
{
OpenAIClient openAIClient = new(Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
FileClient fileClient = openAIClient.GetFileClient();
#pragma warning disable OPENAI001
AssistantClient assistantClient = openAIClient.GetAssistantClient();
#pragma warning restore OPENAI001
}
}
11 changes: 6 additions & 5 deletions examples/Embeddings/Example04_SimpleEmbeddingProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ public void Example04_SimpleEmbeddingProtocol()
+ " and a really helpful concierge. The location is perfect -- right downtown, close to all the tourist"
+ " attractions. We highly recommend this hotel.";

BinaryData input = BinaryData.FromObjectAsJson(new {
model = "text-embedding-3-small",
input = description,
encoding_format = "float"
});
BinaryData input = BinaryData.FromObjectAsJson(new
{
model = "text-embedding-3-small",
input = description,
encoding_format = "float"
});

using BinaryContent content = BinaryContent.Create(input);
ClientResult result = client.GenerateEmbeddings(content);
Expand Down
9 changes: 7 additions & 2 deletions examples/OpenAI.Examples.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<!--Ignore XML doc comments on test types and members-->
<NoWarn>$(NoWarn);CS1591</NoWarn>

<!-- CS1591 - Ignore XML doc comments on test types and members-->
<NoWarn>$(NoWarn);CS1591;</NoWarn>

<!-- OPENAI001 - Ignore experimental warnings-->
<NoWarn>$(NoWarn);OPENAI001;</NoWarn>

<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/Custom/Assistants/Assistant.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Diagnostics.CodeAnalysis;

namespace OpenAI.Assistants;

[Experimental("OPENAI001")]
[CodeGenModel("AssistantObject")]
public partial class Assistant
{
Expand Down
2 changes: 1 addition & 1 deletion src/Custom/Assistants/AssistantClient.Convenience.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public virtual AsyncPageCollection<ThreadMessage> GetMessagesAsync(
/// page of values, call <see cref="PageCollection{T}.GetCurrentPage"/>.</remarks>
/// <returns> A collection of pages of <see cref="ThreadMessage"/>. </returns>
public virtual PageCollection<ThreadMessage> GetMessages(
AssistantThread thread,
AssistantThread thread,
MessageCollectionOptions options = default)
{
Argument.AssertNotNull(thread, nameof(thread));
Expand Down
3 changes: 3 additions & 0 deletions src/Custom/Assistants/AssistantCollectionOptions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Diagnostics.CodeAnalysis;

namespace OpenAI.Assistants;

/// <summary>
/// Represents addition options available when requesting a collection of <see cref="Assistant"/> instances.
/// </summary>
[Experimental("OPENAI001")]
public class AssistantCollectionOptions
{
/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions src/Custom/Assistants/AssistantCreationOptions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace OpenAI.Assistants;

/// <summary>
/// Represents additional options available when creating a new <see cref="Assistant"/>.
/// </summary>
[Experimental("OPENAI001")]
[CodeGenModel("CreateAssistantRequest")]
[CodeGenSuppress(nameof(AssistantCreationOptions), typeof(string))]
public partial class AssistantCreationOptions
Expand Down Expand Up @@ -36,7 +38,7 @@ public partial class AssistantCreationOptions
/// </summary>
[CodeGenMember("TopP")]
public float? NucleusSamplingFactor { get; set; }

internal AssistantCreationOptions(InternalCreateAssistantRequestModel model)
: this()
{
Expand All @@ -51,4 +53,4 @@ public AssistantCreationOptions()
Metadata = new ChangeTrackingDictionary<string, string>();
Tools = new ChangeTrackingList<ToolDefinition>();
}
}
}
4 changes: 3 additions & 1 deletion src/Custom/Assistants/AssistantModificationOptions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace OpenAI.Assistants;

/// <summary>
/// Represents additional options available when modifying an existing <see cref="Assistant"/>.
/// </summary>
[Experimental("OPENAI001")]
[CodeGenModel("ModifyAssistantRequest")]
public partial class AssistantModificationOptions
{
Expand Down Expand Up @@ -40,4 +42,4 @@ public partial class AssistantModificationOptions
/// </summary>
[CodeGenMember("TopP")]
public float? NucleusSamplingFactor { get; set; }
}
}
4 changes: 3 additions & 1 deletion src/Custom/Assistants/AssistantResponseFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using System;
using System.ClientModel.Primitives;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace OpenAI.Assistants;

[Experimental("OPENAI001")]
[CodeGenModel("AssistantResponseFormat")]
public partial class AssistantResponseFormat : IEquatable<AssistantResponseFormat>, IEquatable<string>
{
Expand Down Expand Up @@ -106,4 +108,4 @@ public override string ToString()
return ModelReaderWriter.Write(this).ToString();
}
}
}
}
3 changes: 3 additions & 0 deletions src/Custom/Assistants/AssistantThread.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Diagnostics.CodeAnalysis;

namespace OpenAI.Assistants;

[Experimental("OPENAI001")]
[CodeGenModel("ThreadObject")]
public partial class AssistantThread
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Text.Json;

namespace OpenAI.Assistants;
Expand All @@ -23,4 +20,4 @@ protected override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOption
writer.WriteSerializedAdditionalRawData(SerializedAdditionalRawData, options);
writer.WriteEndObject();
}
}
}
2 changes: 2 additions & 0 deletions src/Custom/Assistants/CodeInterpreterToolResources.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace OpenAI.Assistants;

/// <summary> The AssistantObjectToolResourcesCodeInterpreter. </summary>
[Experimental("OPENAI001")]
[CodeGenModel("AssistantObjectToolResourcesCodeInterpreter")]
public partial class CodeInterpreterToolResources
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Text.Json;

namespace OpenAI.Assistants;
Expand Down
3 changes: 1 addition & 2 deletions src/Custom/Assistants/FileSearchToolDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace OpenAI.Assistants;

[Experimental("OPENAI001")]
[CodeGenModel("AssistantToolsFileSearch")]
[CodeGenSuppress(nameof(FileSearchToolDefinition))]
public partial class FileSearchToolDefinition : ToolDefinition
Expand Down
Loading

0 comments on commit 79014ab

Please sign in to comment.