From 8ca272f01443101ceebca7fbda7280bebb995a24 Mon Sep 17 00:00:00 2001 From: "alinvip@gmail.com" Date: Sun, 30 Jul 2023 00:30:10 +0500 Subject: [PATCH] Fixes. compatibility --- OpenAI.Chat.Functions.Samples.pas | 2 +- OpenAI.Chat.Functions.pas | 17 ++++++++++++++--- OpenAI.Chat.pas | 4 ++-- OpenAI.Embeddings.pas | 3 +-- OpenAI.Utils.ChatHistory.pas | 18 +++++++++++++----- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/OpenAI.Chat.Functions.Samples.pas b/OpenAI.Chat.Functions.Samples.pas index 2a4665d..fc6ed11 100644 --- a/OpenAI.Chat.Functions.Samples.pas +++ b/OpenAI.Chat.Functions.Samples.pas @@ -81,7 +81,7 @@ function TChatFunctionWeather.GetName: string; function TChatFunctionWeather.GetParameters: string; begin - Result := + Result := // json scheme '{' + ' "type": "object",' + ' "properties": {' + diff --git a/OpenAI.Chat.Functions.pas b/OpenAI.Chat.Functions.pas index c4d86f7..26e5712 100644 --- a/OpenAI.Chat.Functions.pas +++ b/OpenAI.Chat.Functions.pas @@ -33,6 +33,9 @@ TChatFunction = class abstract(TInterfacedObject, IChatFunction) implementation +uses + System.SysUtils; + { TChatFunction } constructor TChatFunction.Create; @@ -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. diff --git a/OpenAI.Chat.pas b/OpenAI.Chat.pas index 22fa42e..bb9f0ac 100644 --- a/OpenAI.Chat.pas +++ b/OpenAI.Chat.pas @@ -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; diff --git a/OpenAI.Embeddings.pas b/OpenAI.Embeddings.pas index 6c7e627..5663971 100644 --- a/OpenAI.Embeddings.pas +++ b/OpenAI.Embeddings.pas @@ -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; diff --git a/OpenAI.Utils.ChatHistory.pas b/OpenAI.Utils.ChatHistory.pas index e11d92d..42a26c1 100644 --- a/OpenAI.Utils.ChatHistory.pas +++ b/OpenAI.Utils.ChatHistory.pas @@ -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); @@ -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; @@ -105,6 +110,9 @@ 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 @@ -112,8 +120,8 @@ procedure TChatHistory.Notify(const Item: TChatMessageBuild; Action: TCollection 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