Skip to content

Commit 1413bb0

Browse files
authored
Merge pull request #1242 from iceljc/features/refine-tokenizer
Features/refine tokenizer
2 parents c690884 + 98e8e60 commit 1413bb0

File tree

69 files changed

+1357
-705
lines changed

Some content is hidden

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

69 files changed

+1357
-705
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/TokenStatsModel.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,31 @@ public class TokenStatsModel
66
public string Model { get; set; }
77
public string Prompt { get; set; }
88

9-
#region Input
9+
#region Text token
1010
public int TextInputTokens { get; set; }
1111
public int CachedTextInputTokens { get; set; }
12+
public int TextOutputTokens { get; set; }
13+
#endregion
14+
15+
#region Audio token
1216
public int AudioInputTokens { get; set; }
1317
public int CachedAudioInputTokens { get; set; }
18+
public int AudioOutputTokens { get; set; }
1419
#endregion
1520

16-
#region Output
17-
public int TextOutputTokens { get; set; }
18-
public int AudioOutputTokens { get; set; }
21+
#region Image token
22+
public int ImageInputTokens { get; set; }
23+
public int CachedImageInputTokens { get; set; }
24+
public int ImageOutputTokens { get; set; }
1925
#endregion
2026

27+
#region Image
28+
public int ImageGenerationCount { get; set; }
29+
public float ImageGenerationUnitCost { get; set; }
30+
#endregion
2131

22-
public int TotalInputTokens => TextInputTokens + CachedTextInputTokens + AudioInputTokens + CachedAudioInputTokens;
23-
public int TotalOutputTokens => TextOutputTokens + AudioOutputTokens;
32+
public int TotalInputTokens => TextInputTokens + CachedTextInputTokens
33+
+ AudioInputTokens + CachedAudioInputTokens
34+
+ ImageInputTokens + CachedImageInputTokens;
35+
public int TotalOutputTokens => TextOutputTokens + AudioOutputTokens + ImageOutputTokens;
2436
}
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using BotSharp.Abstraction.Files.Options;
22
using BotSharp.Abstraction.Files.Responses;
3-
using BotSharp.Abstraction.Knowledges.Options;
4-
using BotSharp.Abstraction.Knowledges.Responses;
53

64
namespace BotSharp.Abstraction.Files.Proccessors;
75

@@ -11,7 +9,4 @@ public interface IFileProcessor
119

1210
Task<FileHandleResponse> HandleFilesAsync(Agent agent, string text, IEnumerable<InstructFileModel> files, FileHandleOptions? options = null)
1311
=> throw new NotImplementedException();
14-
15-
Task<FileKnowledgeResponse> GetFileKnowledgeAsync(FileBinaryDataModel file, FileKnowledgeHandleOptions? options = null)
16-
=> throw new NotImplementedException();
1712
}

src/Infrastructure/BotSharp.Abstraction/Knowledges/IPhraseCollection.cs

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

src/Infrastructure/BotSharp.Abstraction/Knowledges/IPhraseService.cs

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

src/Infrastructure/BotSharp.Abstraction/Knowledges/Models/SearchPhrasesResult.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using BotSharp.Abstraction.Knowledges.Options;
2+
using BotSharp.Abstraction.Knowledges.Responses;
3+
4+
namespace BotSharp.Abstraction.Knowledges.Processors;
5+
6+
public interface IKnowledgeProcessor
7+
{
8+
public string Provider { get; }
9+
10+
Task<FileKnowledgeResponse> GetFileKnowledgeAsync(FileBinaryDataModel file, FileKnowledgeHandleOptions? options = null)
11+
=> throw new NotImplementedException();
12+
}

src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmModelSetting.cs

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,27 +106,37 @@ public class ImageSetting
106106
public ImageVariationSetting? Variation { get; set; }
107107
}
108108

109+
/// <summary>
110+
/// Image generationg setting
111+
/// </summary>
109112
public class ImageGenerationSetting
110113
{
111-
public ModelSettingBase? Style { get; set; }
112-
public ModelSettingBase? Size { get; set; }
113-
public ModelSettingBase? Quality { get; set; }
114-
public ModelSettingBase? ResponseFormat { get; set; }
115-
public ModelSettingBase? Background { get; set; }
114+
/// <summary>
115+
/// Parameters: quality, size, style, background, response format
116+
/// </summary>
117+
public Dictionary<string, ModelParamSetting>? Parameters { get; set; }
116118
}
117119

120+
/// <summary>
121+
/// Image editing setting
122+
/// </summary>
118123
public class ImageEditSetting
119124
{
120-
public ModelSettingBase? Size { get; set; }
121-
public ModelSettingBase? Quality { get; set; }
122-
public ModelSettingBase? ResponseFormat { get; set; }
123-
public ModelSettingBase? Background { get; set; }
125+
/// <summary>
126+
/// Parameters: quality, size, background, response format
127+
/// </summary>
128+
public Dictionary<string, ModelParamSetting>? Parameters { get; set; }
124129
}
125130

131+
/// <summary>
132+
/// Image variation setting
133+
/// </summary>
126134
public class ImageVariationSetting
127135
{
128-
public ModelSettingBase? Size { get; set; }
129-
public ModelSettingBase? ResponseFormat { get; set; }
136+
/// <summary>
137+
/// Parameters: size, response format
138+
/// </summary>
139+
public Dictionary<string, ModelParamSetting>? Parameters { get; set; }
130140
}
131141
#endregion
132142

@@ -136,15 +146,21 @@ public class AudioSetting
136146
public AudioTranscriptionSetting? Transcription { get; set; }
137147
}
138148

149+
/// <summary>
150+
/// Audio transcription setting
151+
/// </summary>
139152
public class AudioTranscriptionSetting
140153
{
141154
public float? Temperature { get; set; }
142-
public ModelSettingBase? ResponseFormat { get; set; }
143-
public ModelSettingBase? Granularity { get; set; }
155+
156+
/// <summary>
157+
/// Parameters: granularity, response format
158+
/// </summary>
159+
public Dictionary<string, ModelParamSetting>? Parameters { get; set; }
144160
}
145161
#endregion
146162

147-
public class ModelSettingBase
163+
public class ModelParamSetting
148164
{
149165
public string? Default { get; set; }
150166
public IEnumerable<string>? Options { get; set; }
@@ -156,15 +172,36 @@ public class ModelSettingBase
156172
/// </summary>
157173
public class LlmCostSetting
158174
{
159-
// Input
175+
#region Text token
160176
public float TextInputCost { get; set; } = 0f;
161177
public float CachedTextInputCost { get; set; } = 0f;
178+
public float TextOutputCost { get; set; } = 0f;
179+
#endregion
180+
181+
#region Audio token
162182
public float AudioInputCost { get; set; } = 0f;
163183
public float CachedAudioInputCost { get; set; } = 0f;
164-
165-
// Output
166-
public float TextOutputCost { get; set; } = 0f;
167184
public float AudioOutputCost { get; set; } = 0f;
185+
#endregion
186+
187+
#region Image token
188+
public float ImageInputCost { get; set; } = 0f;
189+
public float CachedImageInputCost { get; set; } = 0f;
190+
public float ImageOutputCost { get; set; } = 0f;
191+
#endregion
192+
193+
#region Image
194+
public IList<LlmImageCost>? ImageCosts { get; set; }
195+
#endregion
196+
}
197+
198+
public class LlmImageCost
199+
{
200+
/// <summary>
201+
/// Attributes: e.g., [quality]: "medium", [size] = "1024x1024"
202+
/// </summary>
203+
public Dictionary<string, string> Attributes { get; set; } = [];
204+
public float Cost { get; set; } = 0f;
168205
}
169206

170207
public enum LlmModelType
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
1+
using BotSharp.Abstraction.MLTasks.Settings;
2+
13
namespace BotSharp.Abstraction.MLTasks.Utilities;
24

35
public static class LlmUtility
46
{
7+
public static string? GetModelParameter(IDictionary<string, ModelParamSetting>? settings, string key, string curVal)
8+
{
9+
string? res = null;
10+
11+
if (settings != null
12+
&& settings.TryGetValue(key, out var value)
13+
&& value != null)
14+
{
15+
res = VerifyModelParameter(curVal, value.Default, value.Options);
16+
}
17+
18+
return res;
19+
}
20+
521
public static string? VerifyModelParameter(string? curVal, string? defaultVal, IEnumerable<string>? options = null)
622
{
723
if (options.IsNullOrEmpty())
824
{
925
return curVal.IfNullOrEmptyAs(defaultVal);
1026
}
1127

12-
return options.Contains(curVal) ? curVal : defaultVal;
28+
return options!.Contains(curVal) ? curVal : defaultVal;
1329
}
1430
}

src/Infrastructure/BotSharp.Abstraction/Statistics/Models/BotSharpStats.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ public class StatsCount
8080
{
8181
[JsonPropertyName("agent_call_count")]
8282
public long AgentCallCount { get; set; }
83+
84+
[JsonPropertyName("image_generation_total_count")]
85+
public int ImageGenerationTotalCount { get; set; }
8386
}
8487

8588
public class StatsLlmCost
@@ -95,4 +98,7 @@ public class StatsLlmCost
9598

9699
[JsonPropertyName("completion_total_cost")]
97100
public float CompletionTotalCost { get; set; }
101+
102+
[JsonPropertyName("image_generation_total_cost")]
103+
public float ImageGenerationTotalCost { get; set; }
98104
}

src/Infrastructure/BotSharp.Abstraction/Statistics/Models/BotSharpStatsDelta.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public string Interval
2929
public class StatsCountDelta
3030
{
3131
public int AgentCallCountDelta { get; set; }
32+
public int ImageGenerationTotalCountDelta { get; set; }
3233
}
3334

3435
public class StatsLlmCostDelta
@@ -37,4 +38,5 @@ public class StatsLlmCostDelta
3738
public int CompletionTokensDelta { get; set; }
3839
public float PromptTotalCostDelta { get; set; }
3940
public float CompletionTotalCostDelta { get; set; }
41+
public float ImageGenerationTotalCostDelta { get; set; }
4042
}

0 commit comments

Comments
 (0)