-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from AIDotNet/feature/embedding_format
- 支持根据请求的向量类型自动帮忙转换返回的嵌入模型的类型
- Loading branch information
Showing
3 changed files
with
85 additions
and
25 deletions.
There are no files selected for viewing
88 changes: 77 additions & 11 deletions
88
src/Thor.Abstractions/ObjectModels/ObjectModels/ResponseModels/EmbeddingCreateResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,91 @@ | ||
using System.Text.Json.Serialization; | ||
using System.Buffers; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Thor.Abstractions.Dtos; | ||
|
||
namespace Thor.Abstractions.ObjectModels.ObjectModels.ResponseModels; | ||
|
||
public record EmbeddingCreateResponse : ThorBaseResponse | ||
{ | ||
[JsonPropertyName("model")] | ||
public string Model { get; set; } | ||
[JsonPropertyName("model")] public string Model { get; set; } | ||
|
||
[JsonPropertyName("data")] | ||
public List<EmbeddingResponse> Data { get; set; } | ||
[JsonPropertyName("data")] public List<EmbeddingResponse> Data { get; set; } = []; | ||
|
||
[JsonPropertyName("usage")] | ||
public ThorUsageResponse Usage { get; set; } | ||
/// <summary> | ||
/// 类型转换,如果类型是base64,则将float[]转换为base64,如果是空或是float和原始类型一样,则不转换 | ||
/// </summary> | ||
public void ConvertEmbeddingData(string? encodingFormat) | ||
{ | ||
if (Data.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
switch (encodingFormat) | ||
{ | ||
// 判断第一个是否是float[],如果是则不转换 | ||
case null or "float" when Data[0].Embedding is float[]: | ||
return; | ||
// 否则转换成float[] | ||
case null or "float": | ||
{ | ||
foreach (var embeddingResponse in Data) | ||
{ | ||
if (embeddingResponse.Embedding is string base64) | ||
{ | ||
embeddingResponse.Embedding = Convert.FromBase64String(base64); | ||
} | ||
} | ||
|
||
return; | ||
} | ||
// 判断第一个是否是string,如果是则不转换 | ||
case "base64" when Data[0].Embedding is string: | ||
return; | ||
// 否则转换成base64 | ||
case "base64": | ||
{ | ||
foreach (var embeddingResponse in Data) | ||
{ | ||
if (embeddingResponse.Embedding is JsonElement str) | ||
{ | ||
if (str.ValueKind == JsonValueKind.Array) | ||
{ | ||
var floats = str.EnumerateArray().Select(element => element.GetSingle()).ToArray(); | ||
|
||
embeddingResponse.Embedding = ConvertFloatArrayToBase64(floats); | ||
} | ||
} | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
public static string ConvertFloatArrayToBase64(float[] floatArray) | ||
{ | ||
// 将 float[] 转换成 byte[] | ||
byte[] byteArray = ArrayPool<byte>.Shared.Rent(floatArray.Length * sizeof(float)); | ||
try | ||
{ | ||
Buffer.BlockCopy(floatArray, 0, byteArray, 0, byteArray.Length); | ||
|
||
// 将 byte[] 转换成 base64 字符串 | ||
return Convert.ToBase64String(byteArray); | ||
} | ||
finally | ||
{ | ||
ArrayPool<byte>.Shared.Return(byteArray); | ||
} | ||
} | ||
|
||
[JsonPropertyName("usage")] public ThorUsageResponse Usage { get; set; } | ||
} | ||
|
||
public record EmbeddingResponse | ||
{ | ||
[JsonPropertyName("index")] | ||
public int? Index { get; set; } | ||
[JsonPropertyName("index")] public int? Index { get; set; } | ||
|
||
[JsonPropertyName("embedding")] | ||
public object Embedding { get; set; } | ||
[JsonPropertyName("embedding")] public object Embedding { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters