Skip to content

Commit

Permalink
[ModelFactory] Implemented Files, Models, and Moderations factories
Browse files Browse the repository at this point in the history
  • Loading branch information
joseharriaga committed Aug 14, 2024
1 parent 32bfd2b commit 0c0c30c
Show file tree
Hide file tree
Showing 10 changed files with 1,037 additions and 109 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

### Features Added

- Added `OpenAIAudioModelFactory`, `OpenAIEmbeddingsModelFactory`, and `OpenAIImagesModelFactory` static classes to the `Audio`, `Embeddings`, and `Images` namespaces, respectively. Model factories can be used to instantiate OpenAI models for mocking in non-live test scenarios.
- Added the following model factories (static classes that can be used to instantiate OpenAI models for mocking in non-live test scenarios):
- `OpenAIAudioModelFactory` in the `OpenAI.Audio` namespace
- `OpenAIEmbeddingsModelFactory` in the `OpenAI.Embeddings` namespace
- `OpenAIFilesModelFactory` in the `OpenAI.Files` namespace
- `OpenAIImagesModelFactory` in the `OpenAI.Images` namespace
- `OpenAIModelsModelFactory` in the `OpenAI.Models` namespace
- `OpenAIModerationsModelFactory` in the `OpenAI.Moderations` namespace

### Breaking Changes

Expand Down
14 changes: 14 additions & 0 deletions api/OpenAI.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,10 @@ public class OpenAIFileInfoCollection : ObjectModel.ReadOnlyCollection<OpenAIFil
public static bool operator !=(OpenAIFilePurpose left, OpenAIFilePurpose right);
public override readonly string ToString();
}
public static class OpenAIFilesModelFactory {
public static OpenAIFileInfo OpenAIFileInfo(string id = null, long? sizeInBytes = null, DateTimeOffset createdAt = default, string filename = null, OpenAIFilePurpose purpose = default, OpenAIFileStatus status = default, string statusDetails = null);
public static OpenAIFileInfoCollection OpenAIFileInfoCollection(IEnumerable<OpenAIFileInfo> items = null);
}
public readonly partial struct OpenAIFileStatus : IEquatable<OpenAIFileStatus> {
private readonly object _dummy;
private readonly int _dummyPrimitive;
Expand Down Expand Up @@ -1927,6 +1931,10 @@ public class OpenAIModelInfoCollection : ObjectModel.ReadOnlyCollection<OpenAIMo
string IPersistableModel<OpenAIModelInfoCollection>.GetFormatFromOptions(ModelReaderWriterOptions options);
BinaryData IPersistableModel<OpenAIModelInfoCollection>.Write(ModelReaderWriterOptions options);
}
public static class OpenAIModelsModelFactory {
public static OpenAIModelInfo OpenAIModelInfo(string id = null, DateTimeOffset createdAt = default, string ownedBy = null);
public static OpenAIModelInfoCollection OpenAIModelInfoCollection(IEnumerable<OpenAIModelInfo> items = null);
}
}
namespace OpenAI.Moderations {
public class ModerationCategories : IJsonModel<ModerationCategories>, IPersistableModel<ModerationCategories> {
Expand Down Expand Up @@ -1999,6 +2007,12 @@ public class ModerationResult : IJsonModel<ModerationResult>, IPersistableModel<
string IPersistableModel<ModerationResult>.GetFormatFromOptions(ModelReaderWriterOptions options);
BinaryData IPersistableModel<ModerationResult>.Write(ModelReaderWriterOptions options);
}
public static class OpenAIModerationsModelFactory {
public static ModerationCategories ModerationCategories(bool hate = false, bool hateThreatening = false, bool harassment = false, bool harassmentThreatening = false, bool selfHarm = false, bool selfHarmIntent = false, bool selfHarmInstructions = false, bool sexual = false, bool sexualMinors = false, bool violence = false, bool violenceGraphic = false);
public static ModerationCategoryScores ModerationCategoryScores(float hate = 0, float hateThreatening = 0, float harassment = 0, float harassmentThreatening = 0, float selfHarm = 0, float selfHarmIntent = 0, float selfHarmInstructions = 0, float sexual = 0, float sexualMinors = 0, float violence = 0, float violenceGraphic = 0);
public static ModerationCollection ModerationCollection(string id = null, string model = null, IEnumerable<ModerationResult> items = null);
public static ModerationResult ModerationResult(bool flagged = false, ModerationCategories categories = null, ModerationCategoryScores categoryScores = null);
}
}
namespace OpenAI.VectorStores {
public abstract class FileChunkingStrategy : IJsonModel<FileChunkingStrategy>, IPersistableModel<FileChunkingStrategy> {
Expand Down
2 changes: 1 addition & 1 deletion src/Custom/Embeddings/OpenAIEmbeddingsModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static EmbeddingCollection EmbeddingCollection(IEnumerable<Embedding> ite
return new EmbeddingCollection(
items.ToList(),
model,
@object: default,
InternalCreateEmbeddingResponseObject.List,
usage,
serializedAdditionalRawData: null);
}
Expand Down
37 changes: 37 additions & 0 deletions src/Custom/Files/OpenAIFilesModelFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace OpenAI.Files;

/// <summary> Model factory for models. </summary>
public static partial class OpenAIFilesModelFactory
{
/// <summary> Initializes a new instance of <see cref="OpenAI.Files.OpenAIFileInfo"/>. </summary>
/// <returns> A new <see cref="OpenAI.Files.OpenAIFileInfo"/> instance for mocking. </returns>
public static OpenAIFileInfo OpenAIFileInfo(string id = null, long? sizeInBytes = null, DateTimeOffset createdAt = default, string filename = null, OpenAIFilePurpose purpose = default, OpenAIFileStatus status = default, string statusDetails = null)
{
return new OpenAIFileInfo(
id,
sizeInBytes,
createdAt,
filename,
@object: InternalOpenAIFileObject.File,
purpose,
status,
statusDetails,
serializedAdditionalRawData: null);
}

/// <summary> Initializes a new instance of <see cref="OpenAI.Files.OpenAIFileInfoCollection"/>. </summary>
/// <returns> A new <see cref="OpenAI.Files.OpenAIFileInfoCollection"/> instance for mocking. </returns>
public static OpenAIFileInfoCollection OpenAIFileInfoCollection(IEnumerable<OpenAIFileInfo> items = null)
{
items ??= new List<OpenAIFileInfo>();

return new OpenAIFileInfoCollection(
items.ToList(),
InternalListFilesResponseObject.List,
serializedAdditionalRawData: null);
}
}
33 changes: 33 additions & 0 deletions src/Custom/Models/OpenAIModelsModelFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace OpenAI.Models;

/// <summary> Model factory for models. </summary>
public static partial class OpenAIModelsModelFactory
{
/// <summary> Initializes a new instance of <see cref="OpenAI.Models.OpenAIModelInfo"/>. </summary>
/// <returns> A new <see cref="OpenAI.Models.OpenAIModelInfo"/> instance for mocking. </returns>
public static OpenAIModelInfo OpenAIModelInfo(string id = null, DateTimeOffset createdAt = default, string ownedBy = null)
{
return new OpenAIModelInfo(
id,
createdAt,
InternalModelObject.Model,
ownedBy,
serializedAdditionalRawData: null);
}

/// <summary> Initializes a new instance of <see cref="OpenAI.Models.OpenAIModelInfoCollection"/>. </summary>
/// <returns> A new <see cref="OpenAI.Models.OpenAIModelInfoCollection"/> instance for mocking. </returns>
public static OpenAIModelInfoCollection OpenAIModelInfoCollection(IEnumerable<OpenAIModelInfo> items = null)
{
items ??= new List<OpenAIModelInfo>();

return new OpenAIModelInfoCollection(
InternalListModelsResponseObject.List,
items.ToList(),
serializedAdditionalRawData: null);
}
}
70 changes: 70 additions & 0 deletions src/Custom/Moderations/OpenAIModerationsModelFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Collections.Generic;
using System.Linq;

namespace OpenAI.Moderations;

/// <summary> Model factory for models. </summary>
public static partial class OpenAIModerationsModelFactory
{
/// <summary> Initializes a new instance of <see cref="OpenAI.Moderations.ModerationCategories"/>. </summary>
/// <returns> A new <see cref="OpenAI.Moderations.ModerationCategories"/> instance for mocking. </returns>
public static ModerationCategories ModerationCategories(bool hate = default, bool hateThreatening = default, bool harassment = default, bool harassmentThreatening = default, bool selfHarm = default, bool selfHarmIntent = default, bool selfHarmInstructions = default, bool sexual = default, bool sexualMinors = default, bool violence = default, bool violenceGraphic = default)
{
return new ModerationCategories(
hate,
hateThreatening,
harassment,
harassmentThreatening,
selfHarm,
selfHarmIntent,
selfHarmInstructions,
sexual,
sexualMinors,
violence,
violenceGraphic,
serializedAdditionalRawData: null);
}

/// <summary> Initializes a new instance of <see cref="OpenAI.Moderations.ModerationCategoryScores"/>. </summary>
/// <returns> A new <see cref="OpenAI.Moderations.ModerationCategoryScores"/> instance for mocking. </returns>
public static ModerationCategoryScores ModerationCategoryScores(float hate = default, float hateThreatening = default, float harassment = default, float harassmentThreatening = default, float selfHarm = default, float selfHarmIntent = default, float selfHarmInstructions = default, float sexual = default, float sexualMinors = default, float violence = default, float violenceGraphic = default)
{
return new ModerationCategoryScores(
hate,
hateThreatening,
harassment,
harassmentThreatening,
selfHarm,
selfHarmIntent,
selfHarmInstructions,
sexual,
sexualMinors,
violence,
violenceGraphic,
serializedAdditionalRawData: null);
}

/// <summary> Initializes a new instance of <see cref="OpenAI.Moderations.ModerationCollection"/>. </summary>
/// <returns> A new <see cref="OpenAI.Moderations.ModerationCollection"/> instance for mocking. </returns>
public static ModerationCollection ModerationCollection(string id = null, string model = null, IEnumerable<ModerationResult> items = null)
{
items ??= new List<ModerationResult>();

return new ModerationCollection(
id,
model,
items.ToList(),
serializedAdditionalRawData: null);
}

/// <summary> Initializes a new instance of <see cref="OpenAI.Moderations.ModerationResult"/>. </summary>
/// <returns> A new <see cref="OpenAI.Moderations.ModerationResult"/> instance for mocking. </returns>
public static ModerationResult ModerationResult(bool flagged = default, ModerationCategories categories = null, ModerationCategoryScores categoryScores = null)
{
return new ModerationResult(
flagged,
categories,
categoryScores,
serializedAdditionalRawData: null);
}
}
Loading

0 comments on commit 0c0c30c

Please sign in to comment.