Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

Commit 111426a

Browse files
authored
Merge pull request #9 from YarikVor/main
Changed base code
2 parents c82e293 + 93758e3 commit 111426a

File tree

67 files changed

+1339
-948
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1339
-948
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Text.Json;
2+
3+
namespace ManagedCode.OpenAI.Chats.Enums;
4+
5+
internal class DictionaryLookupNamingPolicy : JsonNamingPolicyDecorator
6+
{
7+
readonly Dictionary<string, string> dictionary;
8+
9+
public DictionaryLookupNamingPolicy(Dictionary<string, string> dictionary, JsonNamingPolicy underlyingNamingPolicy)
10+
: base(underlyingNamingPolicy) => this.dictionary = dictionary ?? throw new ArgumentNullException();
11+
12+
public override string ConvertName (string name)
13+
=> dictionary.TryGetValue(name, out var value) ? value : base.ConvertName(name);
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Text.Json;
2+
3+
namespace ManagedCode.OpenAI.Chats.Enums;
4+
5+
public class JsonNamingPolicyDecorator : JsonNamingPolicy
6+
{
7+
readonly JsonNamingPolicy underlyingNamingPolicy;
8+
9+
public JsonNamingPolicyDecorator(JsonNamingPolicy underlyingNamingPolicy)
10+
=> this.underlyingNamingPolicy = underlyingNamingPolicy;
11+
12+
public override string ConvertName (string name) =>
13+
underlyingNamingPolicy?.ConvertName(name) ?? name;
14+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Reflection;
2+
using System.Runtime.Serialization;
3+
using System.Text.Json;
4+
using System.Text.Json.Serialization;
5+
6+
namespace ManagedCode.OpenAI.Chats.Enums;
7+
8+
public class JsonStringEnumMemberConverter : JsonConverterFactory
9+
{
10+
private readonly JsonNamingPolicy namingPolicy;
11+
private readonly bool allowIntegerValues;
12+
private readonly JsonStringEnumConverter jsonStringEnumConverter;
13+
14+
public JsonStringEnumMemberConverter() : this(null, true) { }
15+
16+
public JsonStringEnumMemberConverter(JsonNamingPolicy namingPolicy = null, bool allowIntegerValues = true)
17+
{
18+
this.namingPolicy = namingPolicy;
19+
this.allowIntegerValues = allowIntegerValues;
20+
jsonStringEnumConverter = new JsonStringEnumConverter(namingPolicy, allowIntegerValues);
21+
}
22+
23+
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
24+
{
25+
var query = typeToConvert
26+
.GetFields(BindingFlags.Public | BindingFlags.Static)
27+
.Select(field =>
28+
new Tuple<string, string>(
29+
field.Name,
30+
field.GetCustomAttribute<EnumMemberAttribute>()?.Value ?? null
31+
)
32+
)
33+
.Where(tuple => tuple.Item2 != null);
34+
35+
var dictionary = query.ToDictionary(p => p.Item1, p => p.Item2);
36+
if (dictionary.Count > 0)
37+
{
38+
return new JsonStringEnumConverter(new DictionaryLookupNamingPolicy(dictionary, namingPolicy), allowIntegerValues).CreateConverter(typeToConvert, options);
39+
}
40+
else
41+
{
42+
return jsonStringEnumConverter.CreateConverter(typeToConvert, options);
43+
}
44+
}
45+
46+
public override bool CanConvert(Type typeToConvert) => this.jsonStringEnumConverter.CanConvert(typeToConvert);
47+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Net.Http;
6+
using System.Net.Http.Json;
7+
using System.Text;
8+
using System.Text.Json;
9+
using System.Text.Json.Serialization;
10+
using System.Threading.Tasks;
11+
using ManagedCode.OpenAI.Chats.Enums;
12+
using ManagedCode.OpenAI.Client;
13+
using ManagedCode.OpenAI.Exceptions;
14+
using ManagedCode.OpenAI.Models;
15+
16+
namespace ManagedCode.OpenAI.Chats;
17+
18+
public class ChatRequestBuilder
19+
{
20+
private OpenAIClient _client;
21+
private ChatRequestOptions _option;
22+
23+
public ChatRequestBuilder(OpenAIClient client, ChatModel model)
24+
{
25+
this._client = client;
26+
_option = new ChatRequestOptions()
27+
{
28+
Model = model
29+
};
30+
}
31+
32+
public ChatRequestBuilder AskUser(string text)
33+
{
34+
_option.Messages.Add(new Message(RoleType.user, text));
35+
36+
return this;
37+
}
38+
39+
public ChatRequestBuilder AskAssistant(string text)
40+
{
41+
_option.Messages.Add(new Message(RoleType.assistant, text));
42+
43+
return this;
44+
}
45+
46+
47+
48+
49+
public ChatRequestBuilder WithMaxTockens(int maxTokens)
50+
{
51+
if (maxTokens is < 0 or > 2048)
52+
throw new ArgumentOutOfRangeException();
53+
54+
_option.MaxTokens = maxTokens;
55+
56+
return this;
57+
}
58+
59+
public ChatRequestBuilder WithTemperature(float temperature)
60+
{
61+
if (temperature is < 0f or > 2)
62+
throw new ArgumentOutOfRangeException();
63+
64+
_option.Temperature = temperature;
65+
66+
return this;
67+
}
68+
69+
public ChatRequestBuilder WithNucleus(float value)
70+
{
71+
_option.TopP = value;
72+
73+
return this;
74+
}
75+
76+
public ChatRequestBuilder WithResultCount(int count)
77+
{
78+
if (count is < 0)
79+
throw new ArgumentOutOfRangeException();
80+
81+
_option.N = count;
82+
83+
return this;
84+
}
85+
86+
public ChatRequestBuilder WithStream()
87+
{
88+
_option.Stream = true;
89+
90+
return this;
91+
}
92+
93+
94+
public ChatRequestBuilder WithPresencePenalty(float number)
95+
{
96+
if (number is < -2f or > 2f)
97+
throw new ArgumentOutOfRangeException();
98+
99+
_option.PresencePenalty = number;
100+
101+
return this;
102+
}
103+
104+
public ChatRequestBuilder WithFrequencyPenalty(float number)
105+
{
106+
if (number is < -2f or > 2f)
107+
throw new ArgumentOutOfRangeException();
108+
109+
_option.FrequencyPenalty = number;
110+
111+
return this;
112+
}
113+
114+
115+
public ChatRequestBuilder WithLogitBias(Dictionary<string, int> dictionary)
116+
{
117+
_option.LogitBias = dictionary;
118+
119+
return this;
120+
}
121+
122+
public ChatRequestBuilder AddLogitBias(string key, int value)
123+
{
124+
var dict = _option.LogitBias ??= new Dictionary<string, int>();
125+
126+
dict.Add(key, value);
127+
128+
return this;
129+
}
130+
131+
public ChatRequestBuilder WithUsername(string user)
132+
{
133+
_option.User = user;
134+
135+
return this;
136+
}
137+
138+
139+
public ChatRequestBuilder Reset()
140+
{
141+
_option = new ChatRequestOptions()
142+
{
143+
Model = _option.Model
144+
};
145+
146+
return this;
147+
}
148+
149+
public ChatRequestManager Create()
150+
{
151+
152+
return new ChatRequestManager(_option.DeepClone(), _client);;
153+
}
154+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using ManagedCode.OpenAI.Chats.Enums;
2+
using ManagedCode.OpenAI.Client;
3+
using ManagedCode.OpenAI.Models;
4+
5+
namespace ManagedCode.OpenAI.Chats;
6+
7+
public static class ChatRequestBuilderMethods
8+
{
9+
10+
public static ChatRequestBuilder CreateChatBuilder(this OpenAIClient client, ChatModel model)
11+
{
12+
return new ChatRequestBuilder(client, model);
13+
}
14+
15+
}

ManagedCode.OpenAI/Chats/ChatRequest.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)