Skip to content

Commit b1ce397

Browse files
authored
[ModelFactory] Implemented Files, Models, and Moderations factories (#172)
1 parent 32bfd2b commit b1ce397

File tree

10 files changed

+1037
-109
lines changed

10 files changed

+1037
-109
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44

55
### Features Added
66

7-
- 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.
7+
- Added the following model factories (static classes that can be used to instantiate OpenAI models for mocking in non-live test scenarios):
8+
- `OpenAIAudioModelFactory` in the `OpenAI.Audio` namespace
9+
- `OpenAIEmbeddingsModelFactory` in the `OpenAI.Embeddings` namespace
10+
- `OpenAIFilesModelFactory` in the `OpenAI.Files` namespace
11+
- `OpenAIImagesModelFactory` in the `OpenAI.Images` namespace
12+
- `OpenAIModelsModelFactory` in the `OpenAI.Models` namespace
13+
- `OpenAIModerationsModelFactory` in the `OpenAI.Moderations` namespace
814

915
### Breaking Changes
1016

api/OpenAI.netstandard2.0.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,10 @@ public class OpenAIFileInfoCollection : ObjectModel.ReadOnlyCollection<OpenAIFil
17111711
public static bool operator !=(OpenAIFilePurpose left, OpenAIFilePurpose right);
17121712
public override readonly string ToString();
17131713
}
1714+
public static class OpenAIFilesModelFactory {
1715+
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);
1716+
public static OpenAIFileInfoCollection OpenAIFileInfoCollection(IEnumerable<OpenAIFileInfo> items = null);
1717+
}
17141718
public readonly partial struct OpenAIFileStatus : IEquatable<OpenAIFileStatus> {
17151719
private readonly object _dummy;
17161720
private readonly int _dummyPrimitive;
@@ -1927,6 +1931,10 @@ public class OpenAIModelInfoCollection : ObjectModel.ReadOnlyCollection<OpenAIMo
19271931
string IPersistableModel<OpenAIModelInfoCollection>.GetFormatFromOptions(ModelReaderWriterOptions options);
19281932
BinaryData IPersistableModel<OpenAIModelInfoCollection>.Write(ModelReaderWriterOptions options);
19291933
}
1934+
public static class OpenAIModelsModelFactory {
1935+
public static OpenAIModelInfo OpenAIModelInfo(string id = null, DateTimeOffset createdAt = default, string ownedBy = null);
1936+
public static OpenAIModelInfoCollection OpenAIModelInfoCollection(IEnumerable<OpenAIModelInfo> items = null);
1937+
}
19301938
}
19311939
namespace OpenAI.Moderations {
19321940
public class ModerationCategories : IJsonModel<ModerationCategories>, IPersistableModel<ModerationCategories> {
@@ -1999,6 +2007,12 @@ public class ModerationResult : IJsonModel<ModerationResult>, IPersistableModel<
19992007
string IPersistableModel<ModerationResult>.GetFormatFromOptions(ModelReaderWriterOptions options);
20002008
BinaryData IPersistableModel<ModerationResult>.Write(ModelReaderWriterOptions options);
20012009
}
2010+
public static class OpenAIModerationsModelFactory {
2011+
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);
2012+
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);
2013+
public static ModerationCollection ModerationCollection(string id = null, string model = null, IEnumerable<ModerationResult> items = null);
2014+
public static ModerationResult ModerationResult(bool flagged = false, ModerationCategories categories = null, ModerationCategoryScores categoryScores = null);
2015+
}
20022016
}
20032017
namespace OpenAI.VectorStores {
20042018
public abstract class FileChunkingStrategy : IJsonModel<FileChunkingStrategy>, IPersistableModel<FileChunkingStrategy> {

src/Custom/Embeddings/OpenAIEmbeddingsModelFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static EmbeddingCollection EmbeddingCollection(IEnumerable<Embedding> ite
2626
return new EmbeddingCollection(
2727
items.ToList(),
2828
model,
29-
@object: default,
29+
InternalCreateEmbeddingResponseObject.List,
3030
usage,
3131
serializedAdditionalRawData: null);
3232
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace OpenAI.Files;
6+
7+
/// <summary> Model factory for models. </summary>
8+
public static partial class OpenAIFilesModelFactory
9+
{
10+
/// <summary> Initializes a new instance of <see cref="OpenAI.Files.OpenAIFileInfo"/>. </summary>
11+
/// <returns> A new <see cref="OpenAI.Files.OpenAIFileInfo"/> instance for mocking. </returns>
12+
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)
13+
{
14+
return new OpenAIFileInfo(
15+
id,
16+
sizeInBytes,
17+
createdAt,
18+
filename,
19+
@object: InternalOpenAIFileObject.File,
20+
purpose,
21+
status,
22+
statusDetails,
23+
serializedAdditionalRawData: null);
24+
}
25+
26+
/// <summary> Initializes a new instance of <see cref="OpenAI.Files.OpenAIFileInfoCollection"/>. </summary>
27+
/// <returns> A new <see cref="OpenAI.Files.OpenAIFileInfoCollection"/> instance for mocking. </returns>
28+
public static OpenAIFileInfoCollection OpenAIFileInfoCollection(IEnumerable<OpenAIFileInfo> items = null)
29+
{
30+
items ??= new List<OpenAIFileInfo>();
31+
32+
return new OpenAIFileInfoCollection(
33+
items.ToList(),
34+
InternalListFilesResponseObject.List,
35+
serializedAdditionalRawData: null);
36+
}
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace OpenAI.Models;
6+
7+
/// <summary> Model factory for models. </summary>
8+
public static partial class OpenAIModelsModelFactory
9+
{
10+
/// <summary> Initializes a new instance of <see cref="OpenAI.Models.OpenAIModelInfo"/>. </summary>
11+
/// <returns> A new <see cref="OpenAI.Models.OpenAIModelInfo"/> instance for mocking. </returns>
12+
public static OpenAIModelInfo OpenAIModelInfo(string id = null, DateTimeOffset createdAt = default, string ownedBy = null)
13+
{
14+
return new OpenAIModelInfo(
15+
id,
16+
createdAt,
17+
InternalModelObject.Model,
18+
ownedBy,
19+
serializedAdditionalRawData: null);
20+
}
21+
22+
/// <summary> Initializes a new instance of <see cref="OpenAI.Models.OpenAIModelInfoCollection"/>. </summary>
23+
/// <returns> A new <see cref="OpenAI.Models.OpenAIModelInfoCollection"/> instance for mocking. </returns>
24+
public static OpenAIModelInfoCollection OpenAIModelInfoCollection(IEnumerable<OpenAIModelInfo> items = null)
25+
{
26+
items ??= new List<OpenAIModelInfo>();
27+
28+
return new OpenAIModelInfoCollection(
29+
InternalListModelsResponseObject.List,
30+
items.ToList(),
31+
serializedAdditionalRawData: null);
32+
}
33+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace OpenAI.Moderations;
5+
6+
/// <summary> Model factory for models. </summary>
7+
public static partial class OpenAIModerationsModelFactory
8+
{
9+
/// <summary> Initializes a new instance of <see cref="OpenAI.Moderations.ModerationCategories"/>. </summary>
10+
/// <returns> A new <see cref="OpenAI.Moderations.ModerationCategories"/> instance for mocking. </returns>
11+
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)
12+
{
13+
return new ModerationCategories(
14+
hate,
15+
hateThreatening,
16+
harassment,
17+
harassmentThreatening,
18+
selfHarm,
19+
selfHarmIntent,
20+
selfHarmInstructions,
21+
sexual,
22+
sexualMinors,
23+
violence,
24+
violenceGraphic,
25+
serializedAdditionalRawData: null);
26+
}
27+
28+
/// <summary> Initializes a new instance of <see cref="OpenAI.Moderations.ModerationCategoryScores"/>. </summary>
29+
/// <returns> A new <see cref="OpenAI.Moderations.ModerationCategoryScores"/> instance for mocking. </returns>
30+
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)
31+
{
32+
return new ModerationCategoryScores(
33+
hate,
34+
hateThreatening,
35+
harassment,
36+
harassmentThreatening,
37+
selfHarm,
38+
selfHarmIntent,
39+
selfHarmInstructions,
40+
sexual,
41+
sexualMinors,
42+
violence,
43+
violenceGraphic,
44+
serializedAdditionalRawData: null);
45+
}
46+
47+
/// <summary> Initializes a new instance of <see cref="OpenAI.Moderations.ModerationCollection"/>. </summary>
48+
/// <returns> A new <see cref="OpenAI.Moderations.ModerationCollection"/> instance for mocking. </returns>
49+
public static ModerationCollection ModerationCollection(string id = null, string model = null, IEnumerable<ModerationResult> items = null)
50+
{
51+
items ??= new List<ModerationResult>();
52+
53+
return new ModerationCollection(
54+
id,
55+
model,
56+
items.ToList(),
57+
serializedAdditionalRawData: null);
58+
}
59+
60+
/// <summary> Initializes a new instance of <see cref="OpenAI.Moderations.ModerationResult"/>. </summary>
61+
/// <returns> A new <see cref="OpenAI.Moderations.ModerationResult"/> instance for mocking. </returns>
62+
public static ModerationResult ModerationResult(bool flagged = default, ModerationCategories categories = null, ModerationCategoryScores categoryScores = null)
63+
{
64+
return new ModerationResult(
65+
flagged,
66+
categories,
67+
categoryScores,
68+
serializedAdditionalRawData: null);
69+
}
70+
}

0 commit comments

Comments
 (0)