{"id":329565,"date":"2024-05-24T21:52:37","date_gmt":"2024-05-24T21:52:37","guid":{"rendered":"https:\/\/www.baeldung.com\/java-openai-api-client"},"modified":"2024-05-24T21:52:37","modified_gmt":"2024-05-24T21:52:37","slug":"openai-api-client-in-java","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2024\/05\/24\/openai-api-client-in-java\/","title":{"rendered":"OpenAI API Client in Java"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<p><img src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/09\/Java-5-Featured-1024x536.png\" class=\"webfeedsFeaturedVisual wp-post-image\" alt=\"\" style=\"float: left; margin-right: 5px;\" decoding=\"async\" srcset=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/09\/Java-5-Featured-1024x536.png 1024w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/09\/Java-5-Featured-300x157.png 300w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/09\/Java-5-Featured-768x402.png 768w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/09\/Java-5-Featured-100x52.png 100w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/09\/Java-5-Featured.png 1200w\" sizes=\"(max-width: 580px) 100vw, 580px\" \/><\/p>\n<h2 id=\"bd-overview\" data-id=\"overview\">1. Overview<\/h2>\n<div class=\"bd-anchor\" id=\"overview\"><\/div>\n<p>With the widespread use of generative AI and ChatGPT, in particular, many languages have started to provide libraries that interact with their <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/openai.com\/\">OpenAI API<\/a>. Java isn&#8217;t an exception.<\/p>\n<p>In this tutorial, we&#8217;ll talk about <em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/TheoKanning\/openai-java\">openai-java<\/a><\/em>. This is a client that allows more convenient communication with OpenAI API. However, reviewing the entire library in a single article is impossible. <strong>Thus, we&#8217;ll use a practical example and build a simple <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/java-console-input-output\">console tool<\/a> connected to ChatGPT.<\/strong><\/p>\n<h2 id=\"bd-dependencies\" data-id=\"dependencies\">2. Dependencies<\/h2>\n<div class=\"bd-anchor\" id=\"dependencies\"><\/div>\n<p>First, we must import the required dependencies for our project. We can find the libraries in the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/maven\">Maven<\/a> <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/mvnrepository.com\/artifact\/com.theokanning.openai-gpt3-java\">repository<\/a>. <strong>These three modules are dedicated to different aspects of the interaction:<\/strong><\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\r\n    &lt;groupId&gt;com.theokanning.openai-gpt3-java&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;service&lt;\/artifactId&gt;\r\n    &lt;version&gt;0.18.2&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n&lt;dependency&gt;\r\n    &lt;groupId&gt;com.theokanning.openai-gpt3-java&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;api&lt;\/artifactId&gt;\r\n    &lt;version&gt;0.18.2&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n&lt;dependency&gt;\r\n    &lt;groupId&gt;com.theokanning.openai-gpt3-java&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;client&lt;\/artifactId&gt;\r\n    &lt;version&gt;0.18.2&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n<\/code><\/pre>\n<p>Please note that the name explicitly mentions GPT3, but it <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/TheoKanning\/openai-java?tab=readme-ov-file#does-this-support-gpt-4\">works with GPT4<\/a> as well.<\/p>\n<h2 id=\"bd-baeldung-tutor\" data-id=\"baeldung-tutor\">3. Baeldung Tutor<\/h2>\n<div class=\"bd-anchor\" id=\"baeldung-tutor\"><\/div>\n<p><strong>In this tutorial, we&#8217;ll build a tool that helps us to create our curriculum based on the articles and tutorials from <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/\">our favorite learning platform<\/a>, or at least try to do so.<\/strong> While the internet provides us with unlimited resources and we can find almost anything online, curating the information has become much harder.<\/p>\n<p>Trying to learn new things is increasingly overwhelming as it&#8217;s hard to identify the best learning path and filter things that won&#8217;t benefit us. <strong>To resolve this problem, we&#8217;ll build a simple client to interact with ChatGPT and ask it to guide us in the vast ocean of Baeldung articles.<\/strong><\/p>\n<h2 id=\"bd-openai-api-token\" data-id=\"openai-api-token\">4. OpenAI API Token<\/h2>\n<div class=\"bd-anchor\" id=\"openai-api-token\"><\/div>\n<p>The first step is to connect our application to the OpenAI API. To do so, we need to provide an OpenAI token, which can be generated on the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/platform.openai.com\/api-keys\">website<\/a>:<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/ChatGPT-API-Key-Generation.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-200757\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/ChatGPT-API-Key-Generation.png\" alt=\"\" width=\"600\" height=\"555\" \/><\/a><\/p>\n<p><strong>However, we should be careful about the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/cs\/tokens-vs-sessions#token-based-authentication\">token<\/a> and avoid exposing it.<\/strong> The <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/TheoKanning\/openai-java\"><em>openai-java<\/em> examples<\/a> use the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/java-system-get-property-vs-system-getenv#using-systemgetenv\">environment variables<\/a> for this. <strong>This might not be the best solution for production, but it works well for small experiments.<\/strong><\/p>\n<p>During the runs, we don&#8217;t necessarily need to identify the environment variable for our entire machine; we can use configurations in our IDE. For example, IntelliJ IDEA provides a <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/intellij-idea-environment-variables\">simple way<\/a> to do so.<\/p>\n<p><strong>We can generate two types of tokens: personal and service accounts.<\/strong> The personal token is self-explanatory. The tokens for service accounts are used for bots or applications that can be connected to the OpenAI projects. While both would work, a personal token is good enough for our purpose.<\/p>\n<h2 id=\"bd-openaiservice\" data-id=\"openaiservice\">5. <em>OpenAiService<\/em><\/h2>\n<div class=\"bd-anchor\" id=\"openaiservice\"><\/div>\n<p>The entry point to OpenAI APIs is the class conveniently named <em>OpenAiService<\/em>. <strong>The instance of this class allows us to interact with the APIs and receive responses from the ChatGPT.<\/strong> To create it, we should pass the token we&#8217;ve generated in the previous step:<\/p>\n<pre><code class=\"language-java\">String token = System.getenv(&quot;OPENAI_TOKEN&quot;);\r\nOpenAiService service = new OpenAiService(token);<\/code><\/pre>\n<p>This is the first step in our journey; we need to identify the information and populate the request.<\/p>\n<h3 id=\"bd-1-chatcompletionrequest\" data-id=\"1-chatcompletionrequest\">5.1. <em>ChatCompletionRequest<\/em><\/h3>\n<div class=\"bd-anchor\" id=\"1-chatcompletionrequest\"><\/div>\n<p>We create a request using <em>ChatCompletionRequest<\/em>. The minimal setup <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/platform.openai.com\/docs\/guides\/text-generation\/chat-completions-api\">requires<\/a> us to provide only messages and a model :<\/p>\n<pre><code class=\"language-java\">ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest\r\n  .builder()\r\n  .model(GPT_3_5_TURBO_0301.getName())\r\n  .messages(messages)\r\n  .build();<\/code><\/pre>\n<p>Let&#8217;s review these parameters step-by-step.<\/p>\n<h3 id=\"bd-2-model\" data-id=\"2-model\">5.2. Model<\/h3>\n<div class=\"bd-anchor\" id=\"2-model\"><\/div>\n<p><strong>It&#8217;s essential to pick the model that would fit our requirements, and also it affects the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/openai.com\/api\/pricing\/\">costs<\/a>.<\/strong> Thus, we need to make a reasonable choice. For example, often, there&#8217;s no need to use the most advanced model to clean text or parse it based on some simple formats.\u00a0At the same time, more complex or important tasks require more advanced models to reach our goals.<\/p>\n<p>While we can pass the model name directly, it&#8217;s better to use the <em>ModelEnum:<\/em><\/p>\n<pre><code class=\"language-java\">@Getter\r\n@AllArgsConstructor\r\npublic enum ModelEnum {         \r\n    GPT_3_5_TURBO(&quot;gpt-3.5-turbo&quot;),\r\n    GPT_3_5_TURBO_0301(&quot;gpt-3.5-turbo-0301&quot;),\r\n    GPT_4(&quot;gpt-4&quot;),\r\n    GPT_4_0314(&quot;gpt-4-0314&quot;),\r\n    GPT_4_32K(&quot;gpt-4-32k&quot;),\r\n    GPT_4_32K_0314(&quot;gpt-4-32k-0314&quot;),\r\n    GPT_4_1106_preview(&quot;gpt-4-1106-preview&quot;);\r\n    private String name;\r\n}<\/code><\/pre>\n<p>It doesn&#8217;t contain <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/java-string\">all the models<\/a>, but in our case, it&#8217;s enough. If we want to use a different model, we can provide its name as a <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/java-string\"><em>String<\/em><\/a>.<\/p>\n<h3 id=\"bd-3-messages\" data-id=\"3-messages\">5.3. Messages<\/h3>\n<div class=\"bd-anchor\" id=\"3-messages\"><\/div>\n<p>The next thing is the messages we&#8217;ve created. We use the <em>ChatMessage<\/em> class for it. In our case, we pass only the role and the message itself:<\/p>\n<pre><code class=\"language-java\">List&lt;ChatMessage&gt; messages = new ArrayList&lt;&gt;();\r\nChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), PROMPT);\r\nmessages.add(systemMessage);<\/code><\/pre>\n<p><strong>The interesting part is that we send a collection of messages.<\/strong> Although in usual chats, we communicate by sending messages one by one, in this case, it&#8217;s more similar to email threads.<\/p>\n<p>The system works on completion and appends the next message to the chain. This way, we can maintain the context of the conversation. <strong>We can think about this as a stateless service. <\/strong>However, this means we must pass the messages to keep the context.<\/p>\n<p><strong>At the same time, we can go another way and create an <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/platform.openai.com\/docs\/assistants\/overview\">assistant<\/a>.<\/strong> With this approach, we would store the messages in the threads, and it doesn&#8217;t require sending the entire history back and forth.<\/p>\n<p><strong>While passing, the content of the messages is reasonable, but the purpose of the roles isn&#8217;t.<\/strong> Because we send all the messages at once, we need to provide some way to identify the\u00a0relationships between the messages and users based on their roles.<\/p>\n<h3 id=\"bd-4-roles\" data-id=\"4-roles\">5.4. Roles<\/h3>\n<div class=\"bd-anchor\" id=\"4-roles\"><\/div>\n<p><strong>As was mentioned, roles are crucial for ChatGPT to understand the context of the conversation.<\/strong> We can use them to identify the actors behind the messages. This way, we can help ChatGPT interpret the messages correctly. <strong><em>ChatMessages<\/em> support four roles: chat, system, assistant, and function:<\/strong><\/p>\n<pre><code class=\"language-java\">public enum ChatMessageRole {\r\n    SYSTEM(&quot;system&quot;),\r\n    USER(&quot;user&quot;),\r\n    ASSISTANT(&quot;assistant&quot;),\r\n    FUNCTION(&quot;function&quot;);\r\n    private final String value;\r\n    ChatMessageRole(final String value) {\r\n        this.value = value;\r\n    }\r\n    public String value() {\r\n        return value;\r\n    }\r\n}<\/code><\/pre>\n<p><strong>Usually, the SYSTEM role refers to the initial context or prompt.<\/strong> The user represents the user of the ChatGPT, and the assistant is a ChatGPT itself. This means that, technically, we can also write the messages from the assistant&#8217;s standpoint. As its name suggests, the function role identifies the functions the assistant can use.<\/p>\n<h3 id=\"bd-5-tokens\" data-id=\"5-tokens\">5.5. Tokens<\/h3>\n<div class=\"bd-anchor\" id=\"5-tokens\"><\/div>\n<p>While we previously talked about access tokens to the API, the meaning is different in the context of models and messages. <strong>We can think about tokens as the amount of information we can process and the amount we want to get in response.<\/strong><\/p>\n<p>We can restrict the model from generating huge responses by limiting the number of tokens in the response:<\/p>\n<pre><code class=\"language-java\">ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest\r\n  .builder()\r\n  .model(MODEL)\r\n  .maxTokens(MAX_TOKENS)\r\n  .messages(messages)\r\n  .build();<\/code><\/pre>\n<p>There&#8217;s no direct mapping between the words and tokens because each model processes them slightly differently. This parameter restricts the answer to a specific number of tokens. <strong>Using the default values might allow excessive responses and increase the bills for usage.<\/strong> Thus, it&#8217;s a good practice to configure it explicitly.<\/p>\n<p>We can add the information about used tokens after each response:<\/p>\n<pre><code class=\"language-java\">long usedTokens = result.getUsage().getTotalTokens();\r\nSystem.out.println(&quot;Total tokens used: &quot; + usedTokens);<\/code><\/pre>\n<h3 id=\"bd-6-tokenization\" data-id=\"6-tokenization\">5.6. Tokenization<\/h3>\n<div class=\"bd-anchor\" id=\"6-tokenization\"><\/div>\n<p>In the previous example, we displayed the number of tokens used in the response. While this information is valuable, we often need to estimate the size of the request as well. To achieve this, we can use <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/platform.openai.com\/tokenizer\">tokenizers<\/a> provided by OpenAI.<\/p>\n<p>To do this in a more automated way, openai-java provides us with <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/TheoKanning\/openai-java\/blob\/main\/example\/src\/main\/java\/example\/TikTokensExample.java\"><em>TikTokensUtil<\/em><\/a>, to which we can pass the name of the model and the messages and get the number of tokens as a result.<\/p>\n<h3 id=\"bd-7-options\" data-id=\"7-options\">5.7. Options<\/h3>\n<div class=\"bd-anchor\" id=\"7-options\"><\/div>\n<p>An additional method we can use to configure our request is mysteriously named <em>n(). <\/em>It controls how many responses we want to get for each request. <strong>Simply put, we can have two different answers to the same request.<\/strong> By\u00a0default, we&#8217;ll have only one.<\/p>\n<p>Sometimes, it could be useful for bots and website assistants. <strong>However, the responses are billed based on the tokens across all the options.<\/strong><\/p>\n<h3 id=\"bd-8-bias-and-randomization\" data-id=\"8-bias-and-randomization\">5.8. Bias and Randomization<\/h3>\n<div class=\"bd-anchor\" id=\"8-bias-and-randomization\"><\/div>\n<p>We can use a couple of additional options to control the randomness and biases of ChatGPT answers. For example, <em>logitBias()<\/em> can make it more probable to see or not to see specific tokens. Please note that we&#8217;re talking about tokens and not particular words here. However, it doesn&#8217;t mean this token won&#8217;t appear 100%.<\/p>\n<p>Also, we can use <em>topP()\u00a0<\/em>and <em>temperature()<strong>\u00a0<\/strong><\/em>to randomize the responses. While it&#8217;s useful for some cases, we won&#8217;t change the defaults for our learning tool.<\/p>\n<h2 id=\"bd-curriculum\" data-id=\"curriculum\">6. Curriculum<\/h2>\n<div class=\"bd-anchor\" id=\"curriculum\"><\/div>\n<p>Now, let&#8217;s check our tool in action. We&#8217;ll have the following overall code:<\/p>\n<pre><code class=\"language-java\">public static void main(String[] args) {\r\n    String token = System.getenv(&quot;OPENAI_TOKEN&quot;);\r\n    OpenAiService service = new OpenAiService(token);\r\n    List&lt;ChatMessage&gt; messages = new ArrayList&lt;&gt;();\r\n    ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), PROMPT);\r\n    messages.add(systemMessage);\r\n    System.out.print(GREETING);\r\n    Scanner scanner = new Scanner(System.in);\r\n    ChatMessage firstMsg = new ChatMessage(ChatMessageRole.USER.value(), scanner.nextLine());\r\n    messages.add(firstMsg);\r\n    while (true) {\r\n        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest\r\n          .builder()\r\n          .model(GPT_3_5_TURBO_0301.getName())\r\n          .messages(messages)\r\n          .build();\r\n        ChatCompletionResult result = service.createChatCompletion(chatCompletionRequest);\r\n        long usedTokens = result.getUsage().getTotalTokens();\r\n        ChatMessage response = result.getChoices().get(0).getMessage();\r\n        messages.add(response);\r\n        System.out.println(response.getContent());\r\n        System.out.println(&quot;Total tokens used: &quot; + usedTokens);\r\n        System.out.print(&quot;Anything else?\\n&quot;);\r\n        String nextLine = scanner.nextLine();\r\n        if (nextLine.equalsIgnoreCase(&quot;exit&quot;)) {\r\n            System.exit(0);\r\n        }\r\n        messages.add(new ChatMessage(ChatMessageRole.USER.value(), nextLine));\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>If we run it, we can interact with it via the console:<\/p>\n<pre><code class=\"language-bash\">Hello!\r\nWhat do you want to learn?<\/code><\/pre>\n<p>In response, we can write the topics that we&#8217;re interested in:<\/p>\n<pre><code class=\"language-bash\">$ I would like to learn about binary trees.<\/code><\/pre>\n<p>As expected, the tool would provide us with some articles we can use to learn about the topics:<\/p>\n<pre><code class=\"language-bash\">Great! Here&#039;s a suggested order for Baeldung&#039;s articles on binary trees:\r\n1. Introduction to Binary Trees: https:\/\/www.baeldung.com\/java-binary-tree-intro\r\n2. Implementing a Binary Tree in Java: https:\/\/www.baeldung.com\/java-binary-tree\r\n3. Depth First Traversal of Binary Tree: https:\/\/www.baeldung.com\/java-depth-first-binary-tree-traversal\r\n4. Breadth First Traversal of Binary Tree: https:\/\/www.baeldung.com\/java-breadth-first-binary-tree-traversal\r\n5. Finding the Maximum Element in a Binary Tree: https:\/\/www.baeldung.com\/java-binary-tree-maximum-element\r\n6. Binary Search Trees in Java: https:\/\/www.baeldung.com\/java-binary-search-tree\r\n7. Deleting from a Binary Search Tree: https:\/\/www.baeldung.com\/java-binary-search-tree-delete\r\nI hope this helps you out!\r\nTotal tokens used: 288\r\nAnything else?<\/code><\/pre>\n<p>This way, we solved the problem by creating a curriculum and learning new things. <b>However, not everything is so bright; the problem is that only one article is real.<\/b> For the most part, ChatGPT listed non-existent articles with appropriate links. While the names and the links sound reasonable, they won&#8217;t lead us anywhere.<\/p>\n<p>This is a crucial aspect of any AI tool. <strong>Generative models have a hard time checking the validity of the information.<\/strong> As they are based on predicting and picking the most appropriate next word, it might be hard for them to verify the information. <strong>We cannot rely 100% on the information from generative models.<\/strong><\/p>\n<h2 id=\"bd-conclusion\" data-id=\"conclusion\"><b>7<\/b><b>. Conclusion<\/b><\/h2>\n<div class=\"bd-anchor\" id=\"conclusion\"><\/div>\n<p>AI tools are great and help us to improve applications and automate daily chores, from processing emails and creating shopping lists to optimizing education. Java provides a <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/spring-boot-chatgpt-api-openai\">couple of ways<\/a> to interact with OpenAI APIs, and <em>openai-java<\/em> is one such library.<\/p>\n<p><strong>However, it&#8217;s important to remember that generative models, despite being quite convincing, have trouble validating if the information is true.<\/strong> Thus, it&#8217;s our responsibility to either recheck crucial information or provide a model with enough information that it will be able to give us valid answers.<\/p>\n<p>As usual, all the code from this tutorial is available\u00a0<a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/libraries-ai\/\">over on GitHub<\/a>.<\/p>\n<p><Img align=\"left\" border=\"0\" height=\"1\" width=\"1\" alt=\"\" style=\"border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;\" hspace=\"0\" src=\"https:\/\/feeds.feedblitz.com\/~\/i\/897535991\/0\/baeldung\"><\/p>\n<div style=\"clear:both;padding-top:0.2em;\"><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/897535991\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/897535991\/baeldung,https%3A%2F%2Fwww.baeldung.com%2Fwp-content%2Fuploads%2F2021%2F09%2FJava-5-Featured-1024x536.png\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Post to X.com\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/897535991\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/x.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/897535991\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/897535991\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/java-openai-api-client#respond\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&#160;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/java-openai-api-client\/feed\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&#160;<\/div>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p><img decoding=\"async\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/09\/Java-5-Featured-1024x536.png\" class=\"webfeedsFeaturedVisual wp-post-image\" alt=\"\"><\/p>\n<p>Explore a practical example and build a simple console tool connected to ChatGPT.<\/p>\n<div><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/897535991\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\"><\/a>\u00a0<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/897535991\/baeldung,https%3A%2F%2Fwww.baeldung.com%2Fwp-content%2Fuploads%2F2021%2F09%2FJava-5-Featured-1024x536.png\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\"><\/a>\u00a0<a title=\"Post to X.com\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/897535991\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/x.png\"><\/a>\u00a0<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/897535991\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\"><\/a>\u00a0<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/897535991\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\"><\/a>\u00a0<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/java-openai-api-client#respond\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>\u00a0<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/java-openai-api-client\/feed\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>\u00a0<\/div>\n<\/div>","protected":false},"author":2004,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"OpenAI API Client in Java - ITTeacherITFreelance.hk","description":"Explore a practical example and build a simple console tool connected to ChatGPT. \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0"},"footnotes":""},"categories":[6,1307],"tags":[10804],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329565"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2004"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329565"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329565\/revisions"}],"predecessor-version":[{"id":329566,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329565\/revisions\/329566"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}