Welcome to our blog post on using the ChatGPT API with Delphi!
Have you ever wanted to incorporate the power of state-of-the-art language processing into your Delphi applications? ChatGPT, developed by OpenAI, is a cutting-edge language model that allows for natural language communication with a conversational AI. And with the ChatGPT API, you can easily use this technology in your Delphi projects.
In this blog post, we'll explore how to use the ChatGPT API with Delphi to build chatbots and other language-based applications. We'll go over the basics of setting up the API and making requests.
So if you're a Delphi developer looking to add the power of natural language processing to your projects, this blog post is for you! We'll show you how to get started with ChatGPT and the Delphi API and provide you with the tools and knowledge you need to take your first steps in using this powerful technology.
OpenAI Delphi and Lazarus client
OpenAI for Delphi is a client library to connect to the OpenAI API from Delphi and Lazarus/FPC (Free Pascal Compiler) applications. It does not require any 3rd-party library. It works on all recent Delphi versions that provides unit (
System.Net.HttpClient) and Lazarus/FPC. Although not fully tested, it should also work on all supported platforms (Windows, Linux, macOS, etc.).
You can simply clone the source code from the OpenAI for Delphi official repository at GitHub and follow the installation instructions there:
Official repository: https://github.com/landgraf-dev/openai-delphi
The library needs to be configured with your account's secret API key, which is available on the website. We recommend setting it as an environment variable named
OPENAI_API_KEY. Once you have the API key, just create the client and set the key as the following:
Client := TOpenAIClient.Create;
Client.Config.AccessToken := GetEnvironmentVariable('OPENAI_API_KEY');
Most of OpenAI API endpoints are available in the client, under the interface provided in the
OpenAI property. The following example shows how to ask a question and receive an answer using the CreateCompletion endpoint:
function AskQuestion(const Question: string): string;
Response := nil;
Request := TCreateCompletionRequest.Create;
Request.Prompt := Question;
Request.Model := 'text-davinci-003';
Request.MaxTokens := 2048; // Be careful as this can quickly consume your API quota.
Response := Client.OpenAI.CreateCompletion(Request);
if Assigned(Response.Choices) and (Response.Choices.Count > 0) then
Result := Response.Choices.Text
Result := '';
Using the demo
The repository provides a simple demo console application that you can use to make questions and receive answers from ChatGPT. All you need is to create your access key at OpenAI and provide it to the console application, either manually or by creating the
OPENAI_API_KEY environment variable.
The demo is cross-platform, so we used ran it on Linux and asked ChatGPT a few paramount questions:
In case you prefer the text transcription, here it is:
OpenAI ChatGPT Sample Application version 1.0
Using OpenAI API Client for Delphi and Lazarus/FPC
Copyright (c) Landgraf.dev - all rights reserved.
API Key loaded from environment variable OPENAI_API_KEY.
Write your question: What can I use to develop software using Pascal?
You can use a variety of development environments and compilers to develop software using Pascal. Popular choices include Embarcadero's Delphi and Free Pascal. Additionally, the Lazarus IDE is a free and open source option available for a variety of platforms. For a full list of software available, visit http://www.freepascal.org/development.html.
Write your question: Can you please tell me more about Embacadero's Delphi and Free Pascal then?
Delphi is a general-purpose programming language and software development tool created by Embarcadero Technologies. It helps developers create native Windows, macOS, iOS, and Android applications using the same source code. Delphi’s language is Object Pascal, a descendant of the procedural language Pascal. Free Pascal is a GPL-licensed compiler for the closely related Object Pascal language. It is open-source, and its latest version supports Windows, macOS, and Linux for 32-bit and 64-bit development. It is primarily used to create Pascal-based applications, making it somewhat behind the more powerful Delphi.
Write your question: Do you have any information about "Landgraf.dev"? I read somewhere they have a blog about software development with Pascal (at https://landgraf.dev) and a training courses site (https://courses.landgraf.dev). What can you tell me about it?
Landgraf.dev is a blog and educational platform that focuses on software development with Pascal. It contains tutorials and articles with step-by-step instructions on how to create programs in the language. The blog's main goal is to provide a working knowledge of Pascal in a logical and easy-to-understand way. The courses section on the platform features paid courses covering object-oriented programming, debugging, working with databases, and more. Additionally, they offer free Pascal programming practice exercises and free downloadables related to the language.
Write your question: Ok, thank you for your answers. Anything else I should know about Pascal software development in general?
Pascal software development is quite versatile and can be used for many different purposes. To get the most out of it, it is important to be familiar with the language and its features, including object-oriented programming and generics. Additionally, it is also important to have a good knowledge of data structures, algorithms, and debugging. Finally, a good understanding of the various libraries and frameworks available can help streamline your development process.
Write your question: Can you then provide the source code of a minimal application written in Pascal so I have an idea about what does it look like?
Write your question: Thank you very much for your help.
It was my pleasure.
Write your question:
Smart guy, isn't it?
The sky is the limit
Now, it's up to you to check if and how using ChatGPT and other OpenAI technologies will be useful in your applications. The use cases are many, and actually I'm sure that with some creativity you can find some original uses for it.
The "traditional" uses I can think of are using it to help support, answer frequently asked question, fix text typos and grammar errors, give suggestions about how to do something, among others. Take into account that you can train your model with specific information you might have.
Oh, and by the way, here is another use case: writing blog posts! Maybe you have noticed due to the different style, but in case you haven't: the first four paragraphs of this blog post were written 100% by ChatGPT. I asked it to write it for me. Even though the very first version was not exactly what I wanted, I was too lazy to fix it myself, adding hyperlinks, so I just asked it to improve it. I used the web version, and here are the screenshots of the interactions:
Well, again, feel free to use the OpenAI API client in your applications, use it from the GitHib repo: https://github.com/landgraf-dev/openai-delphi.
You are also welcome to discuss about this article in our forum. Let us know your thoughts and ideas about this new exciting technology!
(*) Photo by Tara Winstead