{"id":13643903,"url":"https://github.com/hemulgm/delphiopenai","last_synced_at":"2025-10-10T16:33:57.982Z","repository":{"id":65631375,"uuid":"587303331","full_name":"HemulGM/DelphiOpenAI","owner":"HemulGM","description":"OpenAI (and DeepSeek, Azure OpenAI, YandexGPT, Ollama) API wrapper for Delphi. Use ChatGPT, DALL-E, Whisper and other products.","archived":false,"fork":false,"pushed_at":"2025-08-31T10:33:52.000Z","size":1143,"stargazers_count":277,"open_issues_count":2,"forks_count":76,"subscribers_count":29,"default_branch":"main","last_synced_at":"2025-08-31T12:14:01.566Z","etag":null,"topics":["api-wrapper","azure","chatgpt","dall-e","deepseek","delphi","gigachat","gpt","gpt-3","gpt-4","gpt-functions","ollama","openai","openai-api","openai-whisper","openaiapi","qwen","yandex-gpt"],"latest_commit_sha":null,"homepage":"","language":"Pascal","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HemulGM.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-01-10T12:48:40.000Z","updated_at":"2025-08-31T10:59:15.000Z","dependencies_parsed_at":"2023-11-14T18:44:00.974Z","dependency_job_id":"d2db3211-8186-454c-89cf-f74d66573f0f","html_url":"https://github.com/HemulGM/DelphiOpenAI","commit_stats":{"total_commits":138,"total_committers":8,"mean_commits":17.25,"dds":0.1594202898550725,"last_synced_commit":"6b35f37f8bb9e17b0b81c2132f55060ba1a6efbe"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/HemulGM/DelphiOpenAI","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HemulGM%2FDelphiOpenAI","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HemulGM%2FDelphiOpenAI/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HemulGM%2FDelphiOpenAI/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HemulGM%2FDelphiOpenAI/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HemulGM","download_url":"https://codeload.github.com/HemulGM/DelphiOpenAI/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HemulGM%2FDelphiOpenAI/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279004698,"owners_count":26083750,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api-wrapper","azure","chatgpt","dall-e","deepseek","delphi","gigachat","gpt","gpt-3","gpt-4","gpt-functions","ollama","openai","openai-api","openai-whisper","openaiapi","qwen","yandex-gpt"],"created_at":"2024-08-02T01:01:54.582Z","updated_at":"2025-10-10T16:33:57.976Z","avatar_url":"https://github.com/HemulGM.png","language":"Pascal","funding_links":[],"categories":["CLIs"],"sub_categories":[],"readme":"# Delphi OpenAI API\n\n![logo](https://github.com/HemulGM/DelphiOpenAI/blob/main/openai+delphi.png?raw=true)\n\n___\n![GitHub](https://img.shields.io/github/license/hemulgm/DelphiOpenAI)\n![GitHub](https://img.shields.io/github/last-commit/hemulgm/DelphiOpenAI)\n![GitHub](https://img.shields.io/badge/coverage-100%25-green)\n![GitHub](https://img.shields.io/badge/IDE%20Version-Delphi%2011+-yellow)\n![GitHub](https://img.shields.io/badge/platform-all%20platforms-green)\n\nThis repositorty contains Delphi implementation over [OpenAI](https://beta.openai.com/docs/api-reference/) public API.\n\n❗*This is an unofficial library. OpenAI does not provide any official library for Delphi.*\n\n### Compatibility\nAlso, the library is compatible with the following AI APIs (tested): \n- OpenAI Azure\n- DeepSeek\n- YandexGPT\n- Qwen\n- GigaChat\n\nand other compatible with the OpenAI API.\n\n### Table of contents\n\n- [What is OpenAI](#what-is-openai)\n- [Installation](#installation)\n- [Usage](#usage)\n    - [Initialization](#initialization)\n    - [Models](#models)\n    - [Completions](#completions)\n    - [Chats](#chats)\n    - [Images](#images)\n    - [Function calling](#function-calling)\n    - [Errors](#errors)\n    - [Exceptions](#exceptions)\n    - [Usage proxy](#proxy)\n    - [Preparing mechanism](#preparing-mechanism)\n- [Examples](#examples)\n- [Requirements](#requirements)\n- [Links](#links)\n- [License](#license)\n\n\u003cdetails\u003e\n  \u003csummary\u003e Coverage \u003c/summary\u003e\n\n|API|Status|\n|---|---|\n|Models|🟢 Done|\n|Completions (Legacy)|🟢 Done|\n|Chat|🟢 Done|\n|Chat Vision|🟢 Done|\n|Edits|🟢 Done|\n|Images|🟢 Done|\n|Embeddings|🟢 Done|\n|Audio|🟢 Done|\n|Files|🟢 Done|\n|Fine-tunes (Depricated)|🟢 Done|\n|Fine-tuning|🟢 Done|\n|Moderations|🟢 Done|\n|Engines (Depricated)|🟢 Done|\n|Assistants|🟠 In progress|\n|Threads|🟠 In progress|\n|Messages|🟠 In progress|\n|Runs|🟠 In progress|\n\n\u003c/details\u003e\n\n## What is OpenAI\n\nOpenAI is a non-profit artificial intelligence research organization founded in San Francisco, California in 2015. It was created with the purpose of advancing digital intelligence in ways that benefit humanity as a whole and promote societal progress. The organization strives to develop AI (Artificial Intelligence) programs and systems that can think, act and adapt quickly on their own – autonomously. OpenAI's mission is to ensure safe and responsible use of AI for civic good, economic growth and other public benefits; this includes cutting-edge research into important topics such as general AI safety, natural language processing, applied reinforcement learning methods, machine vision algorithms etc.\n\n\u003eThe OpenAI API can be applied to virtually any task that involves understanding or generating natural language or code. We offer a spectrum of models with different levels of power suitable for different tasks, as well as the ability to fine-tune your own custom models. These models can be used for everything from content generation to semantic search and classification.\n\nThis library provides access to the API of the [OpenAI service](https://openai.com/api/), on the basis of which [ChatGPT](https://openai.com/blog/chatgpt) works and, for example, the generation of images from text using `DALL-E`.\n\n## Installation\n\nYou can install the package from `GetIt` [directly](https://getitnow.embarcadero.com/openai-for-delphi) in the IDE. Or, to use the library, just add the `root` folder to the IDE library path, or your project source path.\n\n## Usage\n\n### Initialization\n\nTo initialize API instance you need to [obtain](https://beta.openai.com/account/api-keys) API token from your Open AI organization.\n\nOnce you have a token, you can initialize `TOpenAI` class, which is an entry point to the API.\n\nDue to the fact that there can be many parameters and not all of them are required, they are configured using an anonymous function.\n\n```Pascal\nuses OpenAI;\n\nvar OpenAI := TOpenAIComponent.Create(Self, API_TOKEN);\n```\n\nor \n\n```Pascal\nuses OpenAI;\n\nvar OpenAI: IOpenAI := TOpenAI.Create(API_TOKEN);\n```\n\nOnce token you posses the token, and the instance is initialized you are ready to make requests.\n\n### Models\nList and describe the various models available in the API. You can refer to the Models documentation to understand what models are available and the differences between them.\n\n```Pascal\nvar Models := OpenAI.Model.List();\ntry\n  for var Model in Models.Data do\n    MemoChat.Lines.Add(Model.Id);\nfinally\n  Models.Free;\nend;\n```\n\nReview [Models Documentation](https://platform.openai.com/docs/api-reference/models) for more info.\n\n### Completions\nGiven a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.\n\n```Pascal\nvar Completions := OpenAI.Completion.Create(\n  procedure(Params: TCompletionParams)\n  begin\n    Params.Prompt(MemoPrompt.Text);\n    Params.MaxTokens(2048);\n  end);\ntry\n  for var Choice in Completions.Choices do\n    MemoChat.Lines.Add(Choice.Index.ToString + ' ' + Choice.Text);\nfinally\n  Completions.Free;\nend;\n```\n\nReview [Completions Documentation](https://platform.openai.com/docs/api-reference/completions) for more info.\n\n### Chats\nGiven a chat conversation, the model will return a chat completion response.\nChatGPT is powered by gpt-3.5-turbo, OpenAI’s most advanced language model.\n\nUsing the OpenAI API, you can build your own applications with gpt-3.5-turbo to do things like:\n- Draft an email or other piece of writing\n- Write Python code\n- Answer questions about a set of documents\n- Create conversational agents\n- Give your software a natural language interface\n- Tutor in a range of subjects\n- Translate languages\n- Simulate characters for video games and much more\n\nThis guide explains how to make an API call for chat-based language models and shares tips for getting good results.\n\n```Pascal\nvar Chat := OpenAI.Chat.Create(\n  procedure(Params: TChatParams)\n  begin\n    Params.Messages([TChatMessageBuild.Create(TMessageRole.User, Text)]);\n    Params.MaxTokens(1024);\n  end);\ntry\n  for var Choice in Chat.Choices do\n    MemoChat.Lines.Add(Choice.Message.Content);\nfinally\n  Chat.Free;\nend;\n```\n\n#### Stream mode\n```Pascal\nOpenAI.Chat.CreateStream(\n  procedure(Params: TChatParams)\n  begin\n    Params.Messages([TchatMessageBuild.User(Buf.Text)]);\n    Params.MaxTokens(1024);\n    Params.Stream;\n  end,\n  procedure(Chat: TChat; IsDone: Boolean; var Cancel: Boolean)\n  begin\n    if (not IsDone) and Assigned(Chat) then\n      Writeln(Chat.Choices[0].Delta.Content)\n    else if IsDone then\n      Writeln('DONE!');\n    Writeln('-------');\n    Sleep(100);\n  end);\n```\n\n#### Vision\n```Pascal\nvar Chat := OpenAI.Chat.Create(\n  procedure(Params: TChatParams)\n  begin\n    Params.Model('gpt-4-vision-preview');\n\n    var Content: TArray\u003cTMessageContent\u003e;\n    Content := Content + [TMessageContent.CreateText(Text)];\n    Content := Content + [TMessageContent.CreateImage(FileToBase64('file path'))];\n\n    Params.Messages([TChatMessageBuild.User(Content)]);\n    Params.MaxTokens(1024);\n  end);\ntry\n  for var Choice in Chat.Choices do\n    MemoChat.Lines.Add(Choice.Message.Content);\nfinally\n  Chat.Free;\nend;\n```\n\nReview [Chat Documentation](https://platform.openai.com/docs/api-reference/chat) for more info.\n\n### Images\nGiven a prompt and/or an input image, the model will generate a new image.\n\n```Pascal\nvar Images := OpenAI.Image.Create(\n  procedure(Params: TImageCreateParams)\n  begin\n    Params.Prompt(MemoPrompt.Text);\n    Params.ResponseFormat('url');\n  end);\ntry\n  for var Image in Images.Data do\n    Image1.Bitmap.LoadFromUrl(Image.Url);\nfinally\n  Images.Free;\nend;\n```\n\nReview [Images Documentation](https://platform.openai.com/docs/api-reference/images) for more info.\n\n### Function Calling\nIn an API call, you can describe functions to gpt-3.5-turbo-0613 and gpt-4-0613, and have the model intelligently choose to output a JSON object containing arguments to call those functions. The Chat Completions API does not call the function; instead, the model generates JSON that you can use to call the function in your code.\n\nThe latest models (gpt-3.5-turbo-0613 and gpt-4-0613) have been fine-tuned to both detect when a function should to be called (depending on the input) and to respond with JSON that adheres to the function signature. With this capability also comes potential risks. We strongly recommend building in user confirmation flows before taking actions that impact the world on behalf of users (sending an email, posting something online, making a purchase, etc).\n\n```Pascal\nvar Chat := OpenAI.Chat.Create(\n  procedure(Params: TChatParams)\n  begin\n    Params.Functions(Funcs);  //list of functions (TArray\u003cIChatFunction\u003e)\n    Params.FunctionCall(TFunctionCall.Auto);\n    Params.Messages([TChatMessageBuild.User(Text)]);\n    Params.MaxTokens(1024);\n  end);\ntry\n  for var Choice in Chat.Choices do\n    if Choice.FinishReason = TFinishReason.FunctionCall then\n      ProcFunction(Choice.Message.FunctionCall)  // execute function (send result to chat, and continue)\n    else\n      MemoChat.Lines.Add(Choice.Message.Content);\nfinally\n  Chat.Free;\nend;\n\n...\n\nprocedure ProcFunction(Func: TChatFunctionCall);\nbegin\n  var FuncResult := Execute(Func.Name, Func.Arguments);  //execute function and get result (json)\n  var Chat := OpenAI.Chat.Create(\n    procedure(Params: TChatParams)\n    begin\n      Params.Functions(Funcs);  //list of functions (TArray\u003cIChatFunction\u003e)\n      Params.FunctionCall(TFunctionCall.Auto);\n      Params.Messages([  //need all history\n         TChatMessageBuild.User(Text), \n         TChatMessageBuild.NewAsistantFunc(Func.Name, Func.Arguments), \n         TChatMessageBuild.Func(FuncResult, Func.Name)]);\n      Params.MaxTokens(1024);\n    end);\n  try\n    for var Choice in Chat.Choices do\n      MemoChat.Lines.Add(Choice.Message.Content);\n  finally\n    Chat.Free;\n  end;\nend;\n```\n\nReview [Functions Documentation](https://platform.openai.com/docs/guides/gpt/function-calling) for more info.\n\n### Errors\n\n```Pascal\ntry\n  var Images := OpenAI.Image.Create(...);\nexcept\n  on E: OpenAIExceptionRateLimitError do\n    ShowError('OpenAI Limit Error: ' + E.Message);\n  on E: OpenAIException do\n    ShowError('OpenAI Error: ' + E.Message);  \nend;\n```\n\n#### Exceptions\n\n* OpenAIExceptionAPI - errors of wrapper\n* OpenAIException - base exception\n* OpenAIExceptionInvalidRequestError\n* OpenAIExceptionRateLimitError\n* OpenAIExceptionAuthenticationError\n* OpenAIExceptionPermissionError\n* OpenAIExceptionTryAgain\n* OpenAIExceptionInvalidResponse - parse error\n\n### Proxy\n\n```Pascal\nOpenAI.API.ProxySettings := TProxySettings.Create(\n    ProxyHost,\n    ProxyPort,\n    ProxyUserName,\n    ProxyPassword);\n```\n\n### Preparing mechanism\n\nIf a third-party API requires some actions before making a request, then use the IAPIPrepare interface and the Prepare property.\n\n```Pascal\nvar client := TOpenAI.Create('https://gigachat.devices.sberbank.ru/api/v1', '');\nсlient.Prepare := TApiPrepareGigaChat.Create(\n  '\u003cclient_id\u003e',\n  '\u003cauth_key\u003e');\nvar response := client.Chat.Create(...);\n```\n\n## Examples\n\u003cimg src=\"https://github.com/HemulGM/ChatGPT/raw/main/preview.png?raw=true\" width=\"100%\"\u003e\n\n[ChatGPT (FMX)](https://github.com/HemulGM/ChatGPT)\n\n## Requirements\nThis library does not require any 3rd party library. It works on recent Delphi versions (10.3+). Althought not fully tested, it should also work on all supported platforms (Windows, Linux, macOS, Android, iOS).\n\nSince the library requires your secret API key, it's not recommended you use it on client applications, as your secret key will be exposed, unless you are sure about the security risks.\n\n## Links\n\n- [OpenAI Documentation](https://platform.openai.com/docs/introduction)\n- [OpenAI Playground](https://beta.openai.com/playground)\n- [OpenAI Examples](https://beta.openai.com/examples)\n- [Dall-E](https://labs.openai.com/)\n\n## License\n\n```\nMIT License\n\nCopyright (c) 2023 HemulGM\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n\n\n\u003chr\u003e\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://d2ohlsp9gwqc7h.cloudfront.net/images/rad-studio/rad-12/Delphi_12_256x2561x.webp\" width=128px alt=\"Delphi\"\u003e\n\u003c/p\u003e\n\u003ch5 align=\"center\"\u003e\nMade with :heart: on Delphi\n\u003c/h5\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhemulgm%2Fdelphiopenai","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhemulgm%2Fdelphiopenai","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhemulgm%2Fdelphiopenai/lists"}