-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathneuroengineapi.pas
121 lines (105 loc) · 3.85 KB
/
neuroengineapi.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 neuroengineapi;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, fpjson, jsonparser,fphttpclient;
Type
Pchar = ^char;
function QueryAI(LLMName: String;Prompt: UnicodeString;temperature: Double;top_p: Double;top_k:Double;repetition_penalty:Double;max_new_len:Integer;seed:Integer;raw:Boolean;streamkey:String;gettokens:Integer): UnicodeString;
function QueryAPI(LLMName: String;APIMessage: String):UnicodeString;
implementation
{ Neuroengine API implementation}
// Constructs and sends an API request to the designated LLMName endpoint while handling exceptions and returning the decoded JSON response.
function QueryAPI(LLMName: String;APIMessage: String):UnicodeString;
var
URL: string;
RawByteStr: RawByteString;
JSONData: TJSONData;
RegularStr: String;
begin
URL := format('https://api.neuroengine.ai/%s',[LLMName]); // Neuroengine endpoint
try
// Create an HTTP client and set up the request
RawByteStr := TFPHTTPClient.SimpleFormPost(URL,APIMessage);
RegularStr := RawByteStr;
// Parse the regular string as JSON data
JSONData := GetJSON(RegularStr);
QueryAPI:=JSONData.AsJSON;
JSONData.Free;
except
// on E: Exception do
QueryAPI:='';
end;
end;
// Escapes special characters within a given Unicode string by replacing them with their respective escape sequences.
function EscapeString(const AValue: Unicodestring): Unicodestring;
const
ESCAPE = '\';
QUOTATION_MARK = '"';
REVERSE_SOLIDUS = '\';
SOLIDUS = '/';
BACKSPACE = #8;
FORM_FEED = #12;
NEW_LINE = #10;
CARRIAGE_RETURN = #13;
HORIZONTAL_TAB = #9;
var
AChar: Char;
begin
Result := '';
for AChar in AValue do
begin
case AChar of
QUOTATION_MARK: Result := Result + ESCAPE + QUOTATION_MARK;
REVERSE_SOLIDUS: Result := Result + ESCAPE + REVERSE_SOLIDUS;
SOLIDUS: Result := Result + ESCAPE + SOLIDUS;
BACKSPACE: Result := Result + ESCAPE + 'b';
FORM_FEED: Result := Result + ESCAPE + 'f';
NEW_LINE: Result := Result + ESCAPE + 'n';
CARRIAGE_RETURN: Result := Result + ESCAPE + 'r';
HORIZONTAL_TAB: Result := Result + ESCAPE + 't';
else
begin
if (Integer(AChar) < 32) or (Integer(AChar) > 126) then
Result := Result + ESCAPE + 'u' + IntToHex(Integer(AChar), 4)
else
Result := Result + AChar;
end;
end;
end;
end;
// Sends a query to a specific AI language model using provided parameters and returns the response as a raw string.
function QueryAI(LLMName: String;Prompt: UnicodeString;temperature: Double;top_p: Double;top_k:Double;repetition_penalty:Double;max_new_len:Integer;seed:Integer;raw:Boolean;streamkey:String;gettokens:Integer): UnicodeString;
var
JSONToSend: Unicodestring;
JSONData: TJSONData;
rawString:String;
errorcode:Integer;
begin
QueryAI:='';
if (raw=True) then
rawString:='"True"'
else rawString:='"False"';
Prompt:=EscapeString(Prompt);
// Prepare the JSON data to send in the POST request
JSONToSend := format('{"message": "%s",'+
'"temperature": %f,'+
'"top_p":%f,'+
'"top_k":%f,'+
'"repetition_penalty":%f,'+
'"max_new_len":%d,'+
'"seed":%d,'+
'"raw" :'+rawString+','+
'"key": "%s",'+
'"gettokens": "%d"'+
'}'
,[Prompt,temperature,top_p,top_k,repetition_penalty,max_new_len,seed,streamkey,gettokens]);
QueryAI:=QueryAPI(LLMName,JSONToSend);
if QueryAI='' then exit;
JSONData := GetJSON(QueryAI);
errorcode:= TJSONObject(JSONData).Find('errorcode').AsInteger;
if (errorcode=0) then // No error
QueryAI:=TJSONObject(JSONData).Find('reply').AsUnicodeString
else QueryAI:='';
end;
end.