forked from HemulGM/DelphiOpenAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenAI.Embeddings.pas
121 lines (102 loc) · 3.49 KB
/
OpenAI.Embeddings.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
unit OpenAI.Embeddings;
interface
uses
System.SysUtils, OpenAI.API.Params, OpenAI.API;
type
TEmbeddingParams = class(TJSONParam)
/// <summary>
/// ID of the model to use. You can use the List models API to see all of your available models,
/// or see our Model overview for descriptions of them.
/// </summary>
function Model(const Value: string): TEmbeddingParams;
/// <summary>
/// Input text to get embeddings for, encoded as a string or array of tokens.
/// To get embeddings for multiple inputs in a single request, pass an array of strings or array of token arrays.
/// Each input must not exceed 8192 tokens in length.
/// </summary>
function Input(const Value: string): TEmbeddingParams; overload;
/// <summary>
/// Input text to get embeddings for, encoded as a string or array of tokens.
/// To get embeddings for multiple inputs in a single request, pass an array of strings or array of token arrays.
/// Each input must not exceed 8192 tokens in length.
/// </summary>
function Input(const Value: TArray<string>): TEmbeddingParams; overload;
/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </summary>
function User(const Value: string): TEmbeddingParams; overload;
end;
TEmbeddingUsage = class
private
FPrompt_tokens: Int64;
FTotal_tokens: Int64;
public
property PromptTokens: Int64 read FPrompt_tokens write FPrompt_tokens;
property TotalTokens: Int64 read FTotal_tokens write FTotal_tokens;
end;
TEmbeddingData = class
private
FIndex: Int64;
FObject: string;
FEmbedding: TArray<Extended>;
public
property &Object: string read FObject write FObject;
property Index: Int64 read FIndex write FIndex;
property Embedding: TArray<Extended> read FEmbedding write FEmbedding;
end;
TEmbeddings = class
private
FData: TArray<TEmbeddingData>;
FObject: string;
FUsage: TEmbeddingUsage;
FModel: string;
public
property &Object: string read FObject write FObject;
property Data: TArray<TEmbeddingData> read FData write FData;
property Usage: TEmbeddingUsage read FUsage write FUsage;
property Model: string read FModel write FModel;
destructor Destroy; override;
end;
TEmbeddingsRoute = class(TOpenAIAPIRoute)
public
/// <summary>
/// Creates an embedding vector representing the input text.
/// </summary>
function Create(ParamProc: TProc<TEmbeddingParams>): TEmbeddings;
end;
implementation
{ TEmbeddingsRoute }
function TEmbeddingsRoute.Create(ParamProc: TProc<TEmbeddingParams>): TEmbeddings;
begin
Result := API.Post<TEmbeddings, TEmbeddingParams>('embeddings', ParamProc);
end;
{ TEmbeddings }
destructor TEmbeddings.Destroy;
var
Item: TEmbeddingData;
begin
if Assigned(FUsage) then
FUsage.Free;
for Item in FData do
if Assigned(Item) then
Item.Free;
inherited;
end;
{ TEmbeddingParams }
function TEmbeddingParams.Input(const Value: TArray<string>): TEmbeddingParams;
begin
Result := TEmbeddingParams(Add('input', Value));
end;
function TEmbeddingParams.Model(const Value: string): TEmbeddingParams;
begin
Result := TEmbeddingParams(Add('model', Value));
end;
function TEmbeddingParams.Input(const Value: string): TEmbeddingParams;
begin
Result := TEmbeddingParams(Add('input', Value));
end;
function TEmbeddingParams.User(const Value: string): TEmbeddingParams;
begin
Result := TEmbeddingParams(Add('user', Value));
end;
end.