<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lesego Letimela</title>
    <description>The latest articles on DEV Community by Lesego Letimela (@lesego).</description>
    <link>https://dev.to/lesego</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F209897%2Ffe391969-31a7-4b2b-8eba-bf9a6dfa4f2b.jpg</url>
      <title>DEV Community: Lesego Letimela</title>
      <link>https://dev.to/lesego</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lesego"/>
    <language>en</language>
    <item>
      <title>OpenAI API Requests in JavaScript 2023</title>
      <dc:creator>Lesego Letimela</dc:creator>
      <pubDate>Sun, 27 Aug 2023 08:05:56 +0000</pubDate>
      <link>https://dev.to/lesego/openai-api-requests-in-javascript-2023-2lpe</link>
      <guid>https://dev.to/lesego/openai-api-requests-in-javascript-2023-2lpe</guid>
      <description>&lt;p&gt;Ok so I recently tried my hand at using the OpenAI api for the first time and discovered, the recent migration from v3 to v4.&lt;br&gt;
The version change introduces syntax and breaking changes. If you are in the process of migrating check   &lt;a href="https://github.com/openai/openai-node/discussions/217"&gt;this&lt;/a&gt; out.&lt;/p&gt;

&lt;h3&gt;
  
  
  Make sure you have an API key for &lt;a href="https://openai.com/"&gt;OpenAI&lt;/a&gt;, as it is required for the client.
&lt;/h3&gt;

&lt;h2&gt;
  
  
  Installing openai-node
&lt;/h2&gt;

&lt;p&gt;First, install the openai-node package using npm:&lt;br&gt;
&lt;code&gt;npm install openai&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Query in JavaScript
&lt;/h2&gt;

&lt;p&gt;Create an app.js file.&lt;/p&gt;

&lt;p&gt;In this example we're using an environment variable for our API key using the dotenv package. You can also hardcode the key, but it's not recommended.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;require("dotenv").config();&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Next we'll bring in the OpenAI package:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const OpenAI = require('openai');&lt;br&gt;
const openai = new OpenAI({&lt;br&gt;
    apiKey: process.env.OPENAI_API_KEY, // This is also the default, can be omitted&lt;br&gt;
});&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Okay now we can start consulting the api...&lt;/p&gt;

&lt;h2&gt;
  
  
  Making a Request
&lt;/h2&gt;

&lt;p&gt;I'm using the free-tier gpt-3.5-turbo model. The model will return an object with a choices key, with a nested message(s) array.&lt;br&gt;
The array will have at least one object with a role and content.&lt;/p&gt;

&lt;p&gt;Let's get a poem about the future of web development from gpt-3.5-turbo.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;async function getPoem() {&lt;br&gt;
    const response = await openai.chat.completions.create({&lt;br&gt;
        model: 'gpt-3.5-turbo',&lt;br&gt;
    // content is hard coded but can come dynamically from user&lt;br&gt;
        messages: [&lt;br&gt;
            { role: 'user', content: 'generate a short &lt;br&gt;
     poem about the future of web development' },&lt;br&gt;
        ],&lt;br&gt;
    });&lt;br&gt;
    // Log first response&lt;br&gt;
    console.log(response.choices[0]);&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This returns an object with role: 'assistant' and the generated response under the key content. So to log only the assistants response,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(response.choices[0].content&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And that's it for the most basic of requests from OpenAI GPT API's using the openai-node client. Now everytime we run the getPoem function, our assistant will create a new poem about the future of web development.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>ai</category>
      <category>chatgpt</category>
    </item>
  </channel>
</rss>
