<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Archives des chatgpt - ji-ka</title>
	<atom:link href="https://www.ji-ka.tn/tag/chatgpt/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.ji-ka.tn/tag/chatgpt/</link>
	<description></description>
	<lastBuildDate>Sun, 08 Oct 2023 09:26:07 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>

<image>
	<url>https://www.ji-ka.tn/wp-content/uploads/2023/10/cropped-icon-32x32.png</url>
	<title>Archives des chatgpt - ji-ka</title>
	<link>https://www.ji-ka.tn/tag/chatgpt/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Premier pas avec Spring et ChatGPT</title>
		<link>https://www.ji-ka.tn/premier-pas-avec-spring-et-chatgpt/</link>
					<comments>https://www.ji-ka.tn/premier-pas-avec-spring-et-chatgpt/#respond</comments>
		
		<dc:creator><![CDATA[Jihed Kaouech]]></dc:creator>
		<pubDate>Tue, 03 Oct 2023 20:40:44 +0000</pubDate>
				<category><![CDATA[spring boot]]></category>
		<category><![CDATA[chatgpt]]></category>
		<guid isPermaLink="false">http://jikatnr.cluster028.hosting.ovh.net/?p=1106</guid>

					<description><![CDATA[<p>Dans ce tutoriel, nous allons découvrir l&#8217;api ChatGPT d&#8217;openAI. D&#8217;abord, on va voir comment tester l&#8217;api ChatGPT et comprendre son model de donnée. Ensuite, nous allons créer une application Spring Boot qui génère des réponses à partir de cette API. 1 API ChatGPT l&#8217;API qu&#8217;on va utiliser dans ce tutorial s&#8217;appelle chat completation. Commençant par [&#8230;]</p>
<p>L’article <a href="https://www.ji-ka.tn/premier-pas-avec-spring-et-chatgpt/">Premier pas avec Spring et ChatGPT</a> est apparu en premier sur <a href="https://www.ji-ka.tn">ji-ka</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Dans ce tutoriel, nous allons découvrir l&rsquo;api ChatGPT d&rsquo;openAI. D&rsquo;abord, on va voir comment tester l&rsquo;api ChatGPT et comprendre son model de donnée. Ensuite, nous allons créer une application Spring Boot qui génère des réponses à partir de cette API.</p>



<span id="more-1106"></span>



<h2 class="wp-block-heading">1 API ChatGPT</h2>



<p>l&rsquo;API qu&rsquo;on va utiliser dans ce tutorial s&rsquo;appelle <a href="https://platform.openai.com/docs/api-reference/chat/create">chat completation</a>. Commençant par jeter un coup d&rsquo;œil à cette API pour comprendre son modèle.</p>



<h3 class="wp-block-heading">1.1 Accès à l&rsquo;API</h3>



<p>Pour utiliser l&rsquo;API <a href="https://platform.openai.com/docs/api-reference/chat/create">chat completation</a>, vous devez obtenir un accès auprès d&rsquo;<a href="https://platform.openai.com/account/api-keys">OpenAI</a></p>



<figure class="wp-block-image size-full is-style-default"><img fetchpriority="high" decoding="async" width="909" height="595" src="http://jikatnr.cluster028.hosting.ovh.net/wp-content/uploads/2023/10/cle-API-OpenAI.png" alt="" class="wp-image-1107" srcset="https://www.ji-ka.tn/wp-content/uploads/2023/10/cle-API-OpenAI.png 909w, https://www.ji-ka.tn/wp-content/uploads/2023/10/cle-API-OpenAI-300x196.png 300w, https://www.ji-ka.tn/wp-content/uploads/2023/10/cle-API-OpenAI-768x503.png 768w" sizes="(max-width: 909px) 100vw, 909px" /><figcaption class="wp-element-caption">Configuration Clé ChatGPT</figcaption></figure>



<h3 class="wp-block-heading">1.2 <strong>Utilisation de l&rsquo;AP</strong>I</h3>



<p>Utilisez la clé API pour faire des requêtes à l&rsquo;API « <em>completions</em> » en utilisant des appels HTTP. Il faut fournir un message qui est le texte d&rsquo;entrée ou la question que vous souhaitez poser et l&rsquo;API renvoie une réponse générée par le moteur GPT.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:0;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="curl -X POST -s https://api.openai.com/v1/chat/completions \
-H &quot;Content-Type: application/json&quot; \
-H &quot;Authorization: Bearer ${CLE_API}&quot; \
-d '{
    &quot;model&quot;: &quot;gpt-3.5-turbo&quot;,
    &quot;messages&quot;: [
        {
            &quot;role&quot;: &quot;user&quot;,
            &quot;content&quot;: &quot;Hello!&quot;
        }
    ]
}'" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">curl</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">-X</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">POST</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">-s</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">https://api.openai.com/v1/chat/completions</span><span style="color: #E6E6E6"> </span><span style="color: #D7BA7D">\</span></span>
<span class="line"><span style="color: #E6E6E6">-H </span><span style="color: #CE9178">&quot;Content-Type: application/json&quot;</span><span style="color: #E6E6E6"> </span><span style="color: #D7BA7D">\</span></span>
<span class="line"><span style="color: #E6E6E6">-H </span><span style="color: #CE9178">&quot;Authorization: Bearer ${</span><span style="color: #9CDCFE">CLE_API</span><span style="color: #CE9178">}&quot;</span><span style="color: #E6E6E6"> </span><span style="color: #D7BA7D">\</span></span>
<span class="line"><span style="color: #E6E6E6">-d </span><span style="color: #CE9178">&#39;{</span></span>
<span class="line"><span style="color: #CE9178">    &quot;model&quot;: &quot;gpt-3.5-turbo&quot;,</span></span>
<span class="line"><span style="color: #CE9178">    &quot;messages&quot;: [</span></span>
<span class="line"><span style="color: #CE9178">        {</span></span>
<span class="line"><span style="color: #CE9178">            &quot;role&quot;: &quot;user&quot;,</span></span>
<span class="line"><span style="color: #CE9178">            &quot;content&quot;: &quot;Hello!&quot;</span></span>
<span class="line"><span style="color: #CE9178">        }</span></span>
<span class="line"><span style="color: #CE9178">    ]</span></span>
<span class="line"><span style="color: #CE9178">}&#39;</span></span></code></pre></div>



<p></p>



<p>Assurez vous de remplacer « ${CLE_API} » par votre clé API réelle et de personnaliser les autres paramètres de la requête en fonction de vos besoins.</p>



<ul class="wp-block-list">
<li><strong><em>model </em></strong>: ID / version du modèle à utiliser. Pour cet exemple, on va utilisée le denier model de la version publique  <em>(gpt-3.5-turbo)</em>, ceci dit, il existe d&rsquo;autres models aussi, <a href="https://platform.openai.com/docs/models/model-endpoint-compatibility">voir ici</a>.</li>



<li><em><strong>messages</strong></em> : Une liste de messages comprenant la conversation. Chaque message nécessite deux champs:
<ul class="wp-block-list">
<li>role : spécifie l&rsquo;expéditeur du message</li>



<li>content: le message réel à envoyer</li>
</ul>
</li>
</ul>



<h3 class="wp-block-heading">1.3 Réponse de l&rsquo;API</h3>



<p>La réponse de l&rsquo;API sera au format JSON avec des métadonnées et un&nbsp; champ&nbsp;« <em>choices</em>« . Ce champ est une liste qui contient un index et une liste de messages.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="{
  &quot;id&quot;: &quot;chatcmpl-123&quot;,
  &quot;object&quot;: &quot;chat.completion&quot;,
  &quot;created&quot;: 1589478378,
  &quot;model&quot;: &quot;gpt-3.5-turbo&quot;,
  &quot;choices&quot;: [{
    &quot;index&quot;: 0,
    &quot;message&quot;: {
      &quot;role&quot;: &quot;assistant&quot;,
      &quot;content&quot;: &quot;\n\nHello there, how may I assist you today?&quot;,
    },
    &quot;finish_reason&quot;: &quot;stop&quot;
  }],
  &quot;usage&quot;: {
    &quot;prompt_tokens&quot;: 5,
    &quot;completion_tokens&quot;: 7,
    &quot;total_tokens&quot;: 12
  }
}" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">{</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #9CDCFE">&quot;id&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;chatcmpl-123&quot;</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #9CDCFE">&quot;object&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;chat.completion&quot;</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #9CDCFE">&quot;created&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">1589478378</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #9CDCFE">&quot;model&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;gpt-3.5-turbo&quot;</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #9CDCFE">&quot;choices&quot;</span><span style="color: #E6E6E6">: [{</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #9CDCFE">&quot;index&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">0</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #9CDCFE">&quot;message&quot;</span><span style="color: #E6E6E6">: {</span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #9CDCFE">&quot;role&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;assistant&quot;</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">      </span><span style="color: #9CDCFE">&quot;content&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;</span><span style="color: #D7BA7D">\n\n</span><span style="color: #CE9178">Hello there, how may I assist you today?&quot;</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">    },</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #9CDCFE">&quot;finish_reason&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">&quot;stop&quot;</span></span>
<span class="line"><span style="color: #E6E6E6">  }],</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #9CDCFE">&quot;usage&quot;</span><span style="color: #E6E6E6">: {</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #9CDCFE">&quot;prompt_tokens&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">5</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #9CDCFE">&quot;completion_tokens&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">7</span><span style="color: #E6E6E6">,</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #9CDCFE">&quot;total_tokens&quot;</span><span style="color: #E6E6E6">: </span><span style="color: #B5CEA8">12</span></span>
<span class="line"><span style="color: #E6E6E6">  }</span></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



<p></p>



<h2 class="wp-block-heading">2 Spring et ChatGPT</h2>



<p>Nous allons créer une application Spring Boot contenant une API Rest qui accepte un message dans les paramètres de la requête, le transmet à l&rsquo;API ChatGPT et renvoie le message de retour.</p>



<h3 class="wp-block-heading">2.1 Dépendances</h3>



<p>Créer un projet du starter Spring boot et ajouter la dépendance « <a href="https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web">spring-boot-starter-web</a>« </p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
		&lt;/dependency&gt;" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">		</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">			</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">org.springframework.boot</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">			</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">spring-boot-starter-web</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span></code></pre></div>



<p></p>



<p>Aussi, on a besoin de Lombok pour faciliter la génération du getter et setter. </p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="		&lt;dependency&gt;
			&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
			&lt;artifactId&gt;lombok&lt;/artifactId&gt;
			&lt;optional&gt;true&lt;/optional&gt;
		&lt;/dependency&gt;" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">		</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">			</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">org.projectlombok</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">groupId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">			</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">lombok</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">artifactId</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">			</span><span style="color: #808080">&lt;</span><span style="color: #569CD6">optional</span><span style="color: #808080">&gt;</span><span style="color: #E6E6E6">true</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">optional</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">dependency</span><span style="color: #808080">&gt;</span></span></code></pre></div>



<p></p>



<h3 class="wp-block-heading">2.2 Configuration</h3>



<p>Dans cette article on va faire simple du coup on va utiliser ResTemplate pour s&rsquo;interfacer avec OpenAI. On doit ajouter un intercepteur pour ajouter la clé.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="@Configuration
public class OpenAIConfig {

    @Value(&quot;${application.openai.key}&quot;)
    private String openaiApiKey;

    @Bean
    @Qualifier(&quot;openaiRestTemplate&quot;)
    public RestTemplate openaiRestTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getInterceptors().add((request, body, execution) -&gt; {
            request.getHeaders().add(&quot;Authorization&quot;, &quot;Bearer &quot; + openaiApiKey);
            return execution.execute(request, body);
        });
        return restTemplate;
    }
}" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">Configuration</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">OpenAIConfig</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">    @</span><span style="color: #4EC9B0">Value</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;${application.openai.key}&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">openaiApiKey</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">    @</span><span style="color: #4EC9B0">Bean</span></span>
<span class="line"><span style="color: #E6E6E6">    @</span><span style="color: #4EC9B0">Qualifier</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;openaiRestTemplate&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">RestTemplate</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">openaiRestTemplate</span><span style="color: #E6E6E6">() {</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #4EC9B0">RestTemplate</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">restTemplate</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #C586C0">new</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">RestTemplate</span><span style="color: #E6E6E6">();</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #9CDCFE">restTemplate</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">getInterceptors</span><span style="color: #E6E6E6">().</span><span style="color: #DCDCAA">add</span><span style="color: #E6E6E6">((request, body, execution) </span><span style="color: #569CD6">-&gt;</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #9CDCFE">request</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">getHeaders</span><span style="color: #E6E6E6">().</span><span style="color: #DCDCAA">add</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;Authorization&quot;</span><span style="color: #E6E6E6">, </span><span style="color: #CE9178">&quot;Bearer &quot;</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">+</span><span style="color: #E6E6E6"> openaiApiKey);</span></span>
<span class="line"><span style="color: #E6E6E6">            </span><span style="color: #C586C0">return</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">execution</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">execute</span><span style="color: #E6E6E6">(request, body);</span></span>
<span class="line"><span style="color: #E6E6E6">        });</span></span>
<span class="line"><span style="color: #E6E6E6">        </span><span style="color: #C586C0">return</span><span style="color: #E6E6E6"> restTemplate;</span></span>
<span class="line"><span style="color: #E6E6E6">    }</span></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



<p></p>



<h3 class="wp-block-heading">2.3 Modèle</h3>



<p>Créons maintenant le modèle correspondant au modèle de l&rsquo;api ChatGPT.</p>



<ul class="wp-block-list">
<li>Classe Message :</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="@Data
@AllArgsConstructor
@NoArgsConstructor
public class Message {

		private String role;
		private String content;

}" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">Data</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">AllArgsConstructor</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">NoArgsConstructor</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">Message</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">role</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">content</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



<ul class="wp-block-list">
<li>Classe  Choice :</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="@Data
@AllArgsConstructor
@NoArgsConstructor
public class Choice {

    private int index;
    private Message message;
}
" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">Data</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">AllArgsConstructor</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">NoArgsConstructor</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">Choice</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">int</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">index</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">Message</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">message</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">}</span></span>
<span class="line"></span></code></pre></div>



<p></p>



<p>Ensuite, créons note model request et response.</p>



<ul class="wp-block-list">
<li>Classe  ChatRequest :</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="@Data
@AllArgsConstructor
@NoArgsConstructor
public class ChatRequest {

	private String model;
	private List&lt;Message&gt; messages;

	@Builder
	public ChatRequest(String model, String message) {
		this.model = model;
		this.messages = new ArrayList&lt;&gt;();
		this.messages.add(new Message(&quot;user&quot;, message));
	}

}" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">Data</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">AllArgsConstructor</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">NoArgsConstructor</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">ChatRequest</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">model</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">List</span><span style="color: #E6E6E6">&lt;</span><span style="color: #4EC9B0">Message</span><span style="color: #E6E6E6">&gt; </span><span style="color: #9CDCFE">messages</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	@</span><span style="color: #4EC9B0">Builder</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">ChatRequest</span><span style="color: #E6E6E6">(</span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">model</span><span style="color: #E6E6E6">, </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">message</span><span style="color: #E6E6E6">) {</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #569CD6">this</span><span style="color: #E6E6E6">.</span><span style="color: #9CDCFE">model</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> model;</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #569CD6">this</span><span style="color: #E6E6E6">.</span><span style="color: #9CDCFE">messages</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #C586C0">new</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">ArrayList</span><span style="color: #E6E6E6">&lt;&gt;();</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #569CD6">this</span><span style="color: #E6E6E6">.</span><span style="color: #9CDCFE">messages</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">add</span><span style="color: #E6E6E6">(</span><span style="color: #C586C0">new</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">Message</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;user&quot;</span><span style="color: #E6E6E6">, message));</span></span>
<span class="line"><span style="color: #E6E6E6">	}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



<ul class="wp-block-list">
<li>Classe  de réponse :</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="@Data
@AllArgsConstructor
@NoArgsConstructor
public class ChatResponse {

	private String id;
	private List&lt;Choice&gt; choices;
	@JsonProperty(&quot;finish_reason&quot;)
	private String finishReason;
}" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">Data</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">AllArgsConstructor</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">NoArgsConstructor</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">ChatResponse</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">id</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">List</span><span style="color: #E6E6E6">&lt;</span><span style="color: #4EC9B0">Choice</span><span style="color: #E6E6E6">&gt; </span><span style="color: #9CDCFE">choices</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">	@</span><span style="color: #4EC9B0">JsonProperty</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;finish_reason&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">finishReason</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



<p></p>



<h3 class="wp-block-heading">2.4 Service</h3>



<p>La classe service permet d&rsquo;appeler notre client OpenAI :</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="@Service
@RequiredArgsConstructor
public class ChatService {

	@Qualifier(&quot;openaiRestTemplate&quot;)
	private final RestTemplate restTemplate;

	@Value(&quot;${application.openai.model}&quot;)
	private String model;

	@Value(&quot;${application.openai.url}&quot;)
	private String apiUrl;

	public String sendMessage(String message) {
		ChatRequest request = ChatRequest.builder()
				.model(model)
				.message(message)
				.build();
				
		ChatResponse response = restTemplate.postForObject(apiUrl, request, ChatResponse.class);
		if (response == null || CollectionUtils.isEmpty(response.getChoices())) {
			return &quot;No response&quot;;
		}
		return response.getChoices().get(0).getMessage().getContent();
	}

}" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">Service</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">RequiredArgsConstructor</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">ChatService</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	@</span><span style="color: #4EC9B0">Qualifier</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;openaiRestTemplate&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">final</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">RestTemplate</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">restTemplate</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	@</span><span style="color: #4EC9B0">Value</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;${application.openai.model}&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">model</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	@</span><span style="color: #4EC9B0">Value</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;${application.openai.url}&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">apiUrl</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">sendMessage</span><span style="color: #E6E6E6">(</span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">message</span><span style="color: #E6E6E6">) {</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #4EC9B0">ChatRequest</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">request</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">ChatRequest</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">builder</span><span style="color: #E6E6E6">()</span></span>
<span class="line"><span style="color: #E6E6E6">				.</span><span style="color: #DCDCAA">model</span><span style="color: #E6E6E6">(model)</span></span>
<span class="line"><span style="color: #E6E6E6">				.</span><span style="color: #DCDCAA">message</span><span style="color: #E6E6E6">(message)</span></span>
<span class="line"><span style="color: #E6E6E6">				.</span><span style="color: #DCDCAA">build</span><span style="color: #E6E6E6">();</span></span>
<span class="line"><span style="color: #E6E6E6">				</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #4EC9B0">ChatResponse</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">response</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">=</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">restTemplate</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">postForObject</span><span style="color: #E6E6E6">(apiUrl, request, </span><span style="color: #9CDCFE">ChatResponse</span><span style="color: #E6E6E6">.</span><span style="color: #9CDCFE">class</span><span style="color: #E6E6E6">);</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #C586C0">if</span><span style="color: #E6E6E6"> (response </span><span style="color: #D4D4D4">==</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">null</span><span style="color: #E6E6E6"> </span><span style="color: #D4D4D4">||</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">CollectionUtils</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">isEmpty</span><span style="color: #E6E6E6">(</span><span style="color: #9CDCFE">response</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">getChoices</span><span style="color: #E6E6E6">())) {</span></span>
<span class="line"><span style="color: #E6E6E6">			</span><span style="color: #C586C0">return</span><span style="color: #E6E6E6"> </span><span style="color: #CE9178">&quot;No response&quot;</span><span style="color: #E6E6E6">;</span></span>
<span class="line"><span style="color: #E6E6E6">		}</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #C586C0">return</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">response</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">getChoices</span><span style="color: #E6E6E6">().</span><span style="color: #DCDCAA">get</span><span style="color: #E6E6E6">(</span><span style="color: #B5CEA8">0</span><span style="color: #E6E6E6">).</span><span style="color: #DCDCAA">getMessage</span><span style="color: #E6E6E6">().</span><span style="color: #DCDCAA">getContent</span><span style="color: #E6E6E6">();</span></span>
<span class="line"><span style="color: #E6E6E6">	}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



<p></p>



<h3 class="wp-block-heading">2.5 Api d&rsquo;entré</h3>



<p>Exposant maintenant notre Api :</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="@RestController
@RequiredArgsConstructor
public class ChatController {

	private final ChatService chatService;

	@GetMapping(&quot;/chat&quot;)
	public String chat(@RequestParam String message) {
		return chatService.sendMessage(message);
	}

}" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">RestController</span></span>
<span class="line"><span style="color: #E6E6E6">@</span><span style="color: #4EC9B0">RequiredArgsConstructor</span></span>
<span class="line"><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">class</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">ChatController</span><span style="color: #E6E6E6"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">private</span><span style="color: #E6E6E6"> </span><span style="color: #569CD6">final</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">ChatService</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">chatService</span><span style="color: #E6E6E6">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">	@</span><span style="color: #4EC9B0">GetMapping</span><span style="color: #E6E6E6">(</span><span style="color: #CE9178">&quot;/chat&quot;</span><span style="color: #E6E6E6">)</span></span>
<span class="line"><span style="color: #E6E6E6">	</span><span style="color: #569CD6">public</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #DCDCAA">chat</span><span style="color: #E6E6E6">(@</span><span style="color: #4EC9B0">RequestParam</span><span style="color: #E6E6E6"> </span><span style="color: #4EC9B0">String</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">message</span><span style="color: #E6E6E6">) {</span></span>
<span class="line"><span style="color: #E6E6E6">		</span><span style="color: #C586C0">return</span><span style="color: #E6E6E6"> </span><span style="color: #9CDCFE">chatService</span><span style="color: #E6E6E6">.</span><span style="color: #DCDCAA">sendMessage</span><span style="color: #E6E6E6">(message);</span></span>
<span class="line"><span style="color: #E6E6E6">	}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E6E6E6">}</span></span></code></pre></div>



<p></p>



<h3 class="wp-block-heading">2.6 Propriétés</h3>



<p>Ajoutons dans le fichier « <em>application.</em>yml » les paramètres qu&rsquo;on a déclaré.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="application:
  openai:
    model: gpt-3.5-turbo
    url: https://api.openai.com/v1/chat/completions
    key: ${your_api_key}" style="color:#E6E6E6;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-dark" style="background-color: #222222" tabindex="0"><code><span class="line"><span style="color: #569CD6">application</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">  </span><span style="color: #569CD6">openai</span><span style="color: #E6E6E6">:</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">model</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">gpt-3.5-turbo</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">url</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">https://api.openai.com/v1/chat/completions</span></span>
<span class="line"><span style="color: #E6E6E6">    </span><span style="color: #569CD6">key</span><span style="color: #E6E6E6">: </span><span style="color: #CE9178">${your_api_key}</span></span></code></pre></div>



<p></p>



<h2 class="wp-block-heading">3 Exécution</h2>



<p>Désormé, on peut lancer l&rsquo;application et appeler notre api via<a href="http://localhost:8080/chat?message=Hello!"> http://localhost:8080/chat?message=Hello!</a></p>



<h2 class="wp-block-heading">4 Code source</h2>



<p>L&rsquo;exemple complet de ce tutorial est disponible sur <a href="https://github.com/JKaouech/spring-boot/tree/master/spring-openai">GitHub</a></p>



<h2 class="wp-block-heading">5. Référence</h2>



<p><a href="https://platform.openai.com/docs/models/model-endpoint-compatibility">https://platform.openai.com/docs/models/model-endpoint-compatibility</a></p>



<p><a href="https://platform.openai.com/docs/api-reference/completions">https://platform.openai.com/docs/api-reference/completions</a></p>



<p></p>
<p>L’article <a href="https://www.ji-ka.tn/premier-pas-avec-spring-et-chatgpt/">Premier pas avec Spring et ChatGPT</a> est apparu en premier sur <a href="https://www.ji-ka.tn">ji-ka</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ji-ka.tn/premier-pas-avec-spring-et-chatgpt/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
