Skip to content

Commit

Permalink
Fixes. compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Jul 29, 2023
1 parent 4308caa commit 8ca272f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion OpenAI.Chat.Functions.Samples.pas
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function TChatFunctionWeather.GetName: string;

function TChatFunctionWeather.GetParameters: string;
begin
Result :=
Result := // json scheme
'{' +
' "type": "object",' +
' "properties": {' +
Expand Down
17 changes: 14 additions & 3 deletions OpenAI.Chat.Functions.pas
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ TChatFunction = class abstract(TInterfacedObject, IChatFunction)

implementation

uses
System.SysUtils;

{ TChatFunction }

constructor TChatFunction.Create;
Expand All @@ -48,9 +51,17 @@ function TChatFunction.Execute(const Args: string): string;
class function TChatFunction.ToJson(Value: IChatFunction): TJSONObject;
begin
Result := TJSONObject.Create;
Result.AddPair('name', Value.GetName);
Result.AddPair('description', Value.GetDescription);
Result.AddPair('parameters', TJSONObject.ParseJSONValue(Value.GetParameters));
try
Result.AddPair('name', Value.GetName);
Result.AddPair('description', Value.GetDescription);
Result.AddPair('parameters', TJSONObject.ParseJSONValue(Value.GetParameters));
except
on E: Exception do
begin
Result.Free;
raise;
end;
end;
end;

end.
Expand Down
4 changes: 2 additions & 2 deletions OpenAI.Chat.pas
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,8 @@ destructor TChat.Destroy;
constructor TChatParams.Create;
begin
inherited;
// Model('gpt-3.5-turbo');
Model('gpt-3.5-turbo-0613');
Model('gpt-3.5-turbo');
// Model('gpt-3.5-turbo-0613');
// Model('gpt-3.5-turbo-16k');
end;

Expand Down
3 changes: 1 addition & 2 deletions OpenAI.Embeddings.pas
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ destructor TEmbeddings.Destroy;
if Assigned(FUsage) then
FUsage.Free;
for Item in FData do
if Assigned(Item) then
Item.Free;
Item.Free;
inherited;
end;

Expand Down
18 changes: 13 additions & 5 deletions OpenAI.Utils.ChatHistory.pas
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ constructor TChatHistory.Create;
end;

procedure TChatHistory.DeleteByTag(const Tag: string);
var
i: Integer;
begin
for var i := 0 to Count - 1 do
for i := 0 to Count - 1 do
if Items[i].Tag = Tag then
begin
Delete(i);
Expand All @@ -66,11 +68,14 @@ procedure TChatHistory.DeleteByTag(const Tag: string);
end;

procedure TChatHistory.SetContentByTag(const Tag, Text: string);
var
i: Integer;
Item: TChatMessageBuild;
begin
for var i := 0 to Count - 1 do
for i := 0 to Count - 1 do
if Items[i].Tag = Tag then
begin
var Item := Items[i];
Item := Items[i];
Item.Content := Text;
Items[i] := Item;
Exit;
Expand Down Expand Up @@ -105,15 +110,18 @@ procedure TChatHistory.NewFunc(const FuncName, FuncResult, Tag: string);
end;

procedure TChatHistory.Notify(const Item: TChatMessageBuild; Action: TCollectionNotification);
var
LastItem: TChatMessageBuild;
NeedLen: Int64;
begin
inherited;
if FAutoTrim and (Action = TCollectionNotification.cnAdded) then
begin
while TextLength + FMaxTokensForQuery > FMaxTokensOfModel do
if Count = 1 then
begin
var LastItem := Items[0];
var NeedLen := LastItem.Content.Length - FMaxTokensForQuery;
LastItem := Items[0];
NeedLen := LastItem.Content.Length - FMaxTokensForQuery;
LastItem.Content := LastItem.Content.Substring(LastItem.Content.Length - NeedLen, NeedLen);
Items[0] := LastItem;
end
Expand Down

0 comments on commit 8ca272f

Please sign in to comment.