-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
20535d9
commit 518e304
Showing
4 changed files
with
342 additions
and
29 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
program test; | ||
|
||
uses System.Net.HttpClientComponent; | ||
|
||
procedure MakeChatGPTRestAPICall(const Endpoint: String; const Payload: String); | ||
var | ||
HTTPRequest: TNetHttpRequest; | ||
HTTPResponse: TNetHttpResponse; | ||
JSONData: TJsonObject; | ||
begin | ||
// Create a new HTTP request | ||
HTTPRequest := TNetHttpRequest.Create(nil); | ||
HTTPRequest.Open('POST', Endpoint, False); | ||
|
||
// Set the request headers | ||
HTTPRequest.AddHeader('Content-Type', 'application/json'); | ||
|
||
// Set the request payload | ||
HTTPRequest.Write(Payload); | ||
|
||
// Send the request | ||
HTTPResponse := TNetHttpResponse.Create(nil); | ||
HTTPResponse.StatusCode := HTTPRequest.Send; | ||
|
||
// Check the response status code | ||
if HTTPResponse.StatusCode = 200 then | ||
begin | ||
// Parse the response JSON data | ||
JSONData := TJsonObject.Parse(HTTPResponse.Content); | ||
|
||
// Return the JSON data as a string | ||
Result := JSONData.ToString; | ||
end | ||
else | ||
begin | ||
// Return an error message | ||
Result := 'Error: ' + HTTPResponse.StatusText; | ||
end; | ||
end; | ||
|
||
end. | ||
|
||
// Example usage: | ||
|
||
procedure MyProcedure; | ||
begin | ||
// Call the MakeChatGPTRestAPICall function | ||
MakeChatGPTRestAPICall('https://api.openai.com/v1/chat/gpt', '{"prompt": "Hello, chatbot! How are you today?"}'); | ||
end; | ||
|
||
end. |
Oops, something went wrong.