<?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: jamesonkunkel</title>
    <description>The latest articles on DEV Community by jamesonkunkel (@jamesonkunkel).</description>
    <link>https://dev.to/jamesonkunkel</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1221173%2F2c137b6e-da99-4e3c-826e-c91822bf072c.jpeg</url>
      <title>DEV Community: jamesonkunkel</title>
      <link>https://dev.to/jamesonkunkel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jamesonkunkel"/>
    <language>en</language>
    <item>
      <title>Building an AI Translation App With NextJS by Extracting JSON From OpenAI's API</title>
      <dc:creator>jamesonkunkel</dc:creator>
      <pubDate>Wed, 29 Nov 2023 22:12:49 +0000</pubDate>
      <link>https://dev.to/jamesonkunkel/building-an-ai-translation-app-with-nextjs-by-extracting-json-from-openais-api-2nd7</link>
      <guid>https://dev.to/jamesonkunkel/building-an-ai-translation-app-with-nextjs-by-extracting-json-from-openais-api-2nd7</guid>
      <description>&lt;p&gt;Large language models (LLMs) such as OpenAI's GPT or Meta's LLaMA allow developers and everyday users to receive useful information, such as code, advice, or factual information by prompting the model using plain language. Realizing the value of such models, many developers have begun working closely with LLMs to enhance their development experience. Indeed, the notion of AI pair programming is no longer science fiction, with AI tools such as Github's Co-Pilot being adopted for daily work by 92 percent of developers according to &lt;a href="https://github.blog/2023-06-13-survey-reveals-ais-impact-on-the-developer-experience/" rel="noopener noreferrer"&gt;one survey&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One constraint with LLMs is that they rely on plain language as both input and output by design. While this is very useful for the average person interacting with chat bots such as ChatGPT, there are certain use cases where having a LLM consistently respond in a very particular format, such as XML or JSON, is preferred. To this end, the field of "prompt engineering" has emerged, where individuals seek to fine tune the content which LLMs respond with, for example to consistently retrieve JSON formatted text from a LLM for use in a web application. Prompt engineering and additional fine-tuning measures can increase the probability that the developer will receive a response from the model in the format they desire, but this approach is inherently inconsistent and feels shoddy or "hacky".  &lt;/p&gt;

&lt;p&gt;Addressing this constraint, Open AI recently released a &lt;a href="https://platform.openai.com/docs/guides/function-calling" rel="noopener noreferrer"&gt;function calling&lt;/a&gt; feature for its API. This feature allows developers to request JSON data that conforms to a specific schema from the GPT3.5 and GPT4 models. In this tutorial, we are going to exploit OpenAI's new function calling feature to build a simple web application that translates a user's English sentence to another language while identifying and translating nouns, verbs, and those verbs' conjugations. In this tutorial, we will be translating English to Spanish, but I encourage you to try to follow along with translating a different pair of languages if you're interested!&lt;/p&gt;

&lt;h2&gt;
  
  
  Purpose
&lt;/h2&gt;

&lt;p&gt;This tutorial is designed to teach you concepts about front end web development using technologies such as React, NextJS and TailwindCSS. It will also serve as an introduction to using the OpenAI API to enhance the functionality of your projects on the web. In particular, you can expect to learn to about how to use OpenAI's function calling feature to retrieve JSON formatted text from a LLM. &lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;p&gt;We are going to approach building this application using NextJS and Typescript. For this reason, it is important that you are familiar with basic principles of both React development and Typescript. &lt;/p&gt;

&lt;h4&gt;
  
  
  Querying OpenAI
&lt;/h4&gt;

&lt;p&gt;NextJS supports API routes, allowing us to build public APIs directly in our NextJS project. We will create a single API endpoint that is responsible for sending a request to the GPT3.5 LLM through OpenAI's API and awaiting a response that will contain the JSON we use in our translation application.&lt;/p&gt;

&lt;h4&gt;
  
  
  Managing State
&lt;/h4&gt;

&lt;p&gt;We will maintain global state for the JSON object representing our translated English sentence using Zustand. Zustand is a state management library for React applications that provides a simple and flexible API to manage and share state across components.&lt;/p&gt;

&lt;h4&gt;
  
  
  Styling
&lt;/h4&gt;

&lt;p&gt;We will scaffold our NextJS project with TailwindCSS. &lt;br&gt;
Tailwind CSS is a utility-first CSS framework that simplifies styling in web development by providing low-level utility classes for building designs directly in your markup. We will also be using DaisyUI which is a component library built on TailwindCSS.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step One: Create a NextJS Project
&lt;/h2&gt;

&lt;p&gt;Let's start building this application. If you don't already have node and an appropriate package manager like npm installed, &lt;a href="https://nodejs.org/en" rel="noopener noreferrer"&gt;see here&lt;/a&gt;. In this tutorial we will be using npm to manage our dependencies. NextJS maintains a CLI to scaffold a new NextJS project. In a terminal or command shell, navigate to a suitable directory to store your project and enter the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The create-next-app program will prompt you for several options when scaffolding your project. Name your project &lt;br&gt;
something (we named ours lang-ai-next) then select Yes when asked whether to use Typescript, ESLint, TailwindCSS, and the default /src directory. We will be using NextJS's original Pages Router rather than the newer App Router introduced in Version 13 of NextJS. See more about that &lt;a href="https://nextjs.org/docs/app" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Your completed prompts should look something like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpprtj007wdo3azdx07dy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpprtj007wdo3azdx07dy.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hit enter and create-next-app will generate a new NextJS project for you. Open the project in your favourite code editor and you should find the project structure looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|-- node_modules
|-- public
|-- src
|   |-- pages
|      |-- api
|          |-- hello.ts
|      |-- _app.tsx
|      |-- _document.tsx
|      |-- _index.tsx
|   |-- styles
|       |-- globals.css
|-- .eslintrc.json
|-- .gitignore
|-- next-env.d.ts
|-- next.config.js
|-- package-lock.json
|-- package.json
|-- postcss.config.js
|-- README.md
|-- tailwind.config.js
|-- tsconfig.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's install the dependencies needed for this project. Run this command to install DaisyUI as a dev dependency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -D daisyui@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next run the following command to install other dependencies, which we will describe as they are implemented:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i ajv axios openai zustand
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step Two: Creating Our API
&lt;/h2&gt;

&lt;p&gt;We will use NextJS's built in API router to create an endpoint that posts a prompt to the OpenAI API and returns JSON representing our translated sentence. Under &lt;code&gt;/src/pages/api&lt;/code&gt;, create new file called &lt;code&gt;translator.ts&lt;/code&gt;. This file will return a handler function that takes HTTP request and response objects as parameters. Let's import the Typescript types and packages we need to build the endpoint.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//translator.ts

import type { NextApiRequest, NextApiResponse } from "next";

import OpenAI from "openai";
import Ajv from "ajv";

const ajv = new Ajv();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here he have imported the OpenAI class which will be used to create an object that interfaces with the OpenAI API. We also imported &lt;code&gt;ajv&lt;/code&gt; which is JSON-validation package that we will use to validate the format of JSON returned to us through the OpenAI API. In addition, we imported type definitions for the request and response objects used as parameters for our NextJS endpoint handler function.&lt;/p&gt;

&lt;p&gt;Now let's create an instance of the OpenAI class and supply it an OpenAI API key to communicate with the LLM. &lt;strong&gt;Note&lt;/strong&gt;: OpenAI's API is not free but it is very inexpensive, especially when using the GPT3.5 model. Navigate to the OpenAI API site, create an account if you don't already have one and login to the &lt;a href="https://platform.openai.com/docs/overview" rel="noopener noreferrer"&gt;dashboard&lt;/a&gt;. You will need to supply a payment method in order to obtain an API key. You can create and find your API keys using the menu on the left of the dashboard: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F294rg52znga1uwq6jvn5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F294rg52znga1uwq6jvn5.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NextJS natively supports reading environmental variables from a &lt;code&gt;.env.local&lt;/code&gt; file in the root directory of your project. Go ahead and create that file with the following, replacing your &lt;code&gt;YOUR_KEY_HERE&lt;/code&gt; with the API key you obtained from OpenAI's dashboard,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//.env.local

API_KEY=[YOUR_KEY_HERE]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can update our translator API to include a new OpenAI object configured with our API key using the environmental variable we defined,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//translator.ts

import type { NextApiRequest, NextApiResponse } from "next";

import OpenAI from "openai";
import Ajv from "ajv";

const ajv = new Ajv();

const openai = new OpenAI({
  apiKey: process.env.API_KEY,
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's define a JSON schema that describes the shape of JSON we expect OpenAI API to return to us when we prompt it with an English sentence. We can use the &lt;code&gt;description&lt;/code&gt; property with the schema to supply additional information to the model about what it should look for when creating the JSON text. Let's first declare the schema as a Javascript object and add properties to the schema, flagging them as required so that the model always produces them in the final object,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const translationSchema = {
  type: "object",
  description:
    "A schema analyzing the verb and noun contents of a prompted Spanish sentence. Contains the infinitive forms of each verb in the sentence and its conjugations in the present tense. Contains a translation of the sentence in English and the English translations of each noun in the sentence.",
  properties: {
    sentence: {
      type: "string",
      description: "A sentence to analyze.",
    },
    translatedSentence: {
      type: "string",
      description: "The English translation of the sentence to be analyzed.",
    },
    nouns: {
      type: "array",
      description:
        "An array of nouns to be analyzed. These are to be identified from the prompt sentence",
      items: {
        type: "object",
        properties: {
          noun: {
            type: "string",
            description: "The Spanish noun to be translated",
          },
          translation: {
            type: "string",
            description: "The English translation of the noun",
          },
        },
        required: ["noun", "translation"],
      },
    },
    verbs: {
      type: "array",
      description:
        "An array of verbs to be analyzed. These are to be identified from the prompt sentence",
      items: {
        type: "object",
        description:
          "Each item is a verb to be analyzed from the prompt with an infinitive form and conjugations in the present tense.",
        properties: {
          infinitive: {
            type: "string",
            description: "The infinitive form of the verb in Spanish.",
          },
          translation: {
            type: "string",
            description:
              "The English translation of the verb with 'to' in front of it. Example: 'to be'",
          },
          conjugations: {
            type: "object",
            properties: {
              present: {
                type: "object",
                description:
                  "The conjugations of the verb in the present tense",
                properties: {
                  yo: { type: "string" },
                  tu: { type: "string" },
                  el: { type: "string" },
                  nosotros: { type: "string" },
                  vosotros: { type: "string" },
                  ellos: { type: "string" },
                },
                required: ["yo", "tu", "el", "nosotros", "vosotros", "ellos"],
              },
            },
            required: ["present"],
          },
        },
        required: ["infinitive", "translation", "conjugations"],
      },
    },
  },
  required: ["sentence", "translatedSentence", "nouns", "verbs"],
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We included four main properties in the schema. The &lt;code&gt;sentence&lt;/code&gt; and &lt;code&gt;translatedSentence&lt;/code&gt; properties contain the English and translated Spanish sentence prompted by the user to the model. The &lt;code&gt;nouns&lt;/code&gt; property contains an array of translated nouns identified in the English sentence by the AI and the &lt;code&gt;verbs&lt;/code&gt; property similarly contains translations of each verbs as well as the verb in its infinitive and present tense forms. Notice we provide description properties for each object. This is what notifies the AI about what content it should use to fill out the schema. &lt;/p&gt;

&lt;p&gt;Next, let's create Typescript types that describe the shape of the shape of the Javascript object we will obtain after parsing the JSON response from the model. Create a new types folder at &lt;code&gt;/src/types&lt;/code&gt; and add a &lt;code&gt;translationTypes.ts&lt;/code&gt; file to it with the following types,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//translationTypes.ts
export interface NounObject {
  noun: string;
  translation: string;
}
export interface ConjugationPronounsObject {
  yo: string;
  tu: string;
  el: string;
  nosotros: string;
  vosotros: string;
  ellos: string;
}

//feel free to change the language you are translating to or add more verb tenses
export interface VerbConjugationsObject {
  present: ConjugationPronounsObject;
}

export interface VerbObject {
  infinitive: string;
  translation: string;
  conjugations: VerbConjugationsObject;
}

export interface TranslationObject {
  sentence: string;
  translatedSentence: string;
  nouns: NounObject[];
  verbs: VerbObject[];
}

export interface TranslationResponseObject {
  success: boolean;
  translation: TranslationObject | null;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The TranslationObject mirrors the JSON schema we expect to receive from the call to the OpenAI API with our sentence to translate. The TranslationResponseObject includes a flag for whether the translation was a success. &lt;/p&gt;

&lt;p&gt;Let's go back to our &lt;code&gt;translator.ts&lt;/code&gt; endpoint and implement the types. First import them into the endpoint file,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//translator.ts

import type { NextApiRequest, NextApiResponse } from "next";
import type{
  TranslationResponseObject,
  TranslationObject,
} from "@/types/translationTypes";

import OpenAI from "openai";
import Ajv from "ajv";

const ajv = new Ajv();

...

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's create a validation function using &lt;code&gt;ajv&lt;/code&gt; using our JSON schema as the argument. We will also setup our API handler function, making the endpoint public to our client code,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//translator.ts

...

const validate = ajv.compile(translationSchema);

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const prompt = req.body.prompt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So far, our endpoint handler function just extracts the &lt;code&gt;prompt&lt;/code&gt; property from the client's request body JSON and stores it in a local variable. We want to use that prompt as input for our call to the OpenAI API so that the AI model can produce JSON following our translationSchema,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//translator.ts

...

const validate = ajv.compile(translationSchema);

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const prompt = req.body.prompt;

  const completion = await openai.chat.completions.create({
    model: "gpt-3.5-turbo-1106",
    response_format: { type: "json_object" },
    messages: [
      {
        role: "system",
        content: "You are a helpful assistant that returns JSON.",
      },
      {
        role: "user",
        content: prompt,
      },
    ],
    functions: [
      {
        name: "find_verbs",
        description:
          "Find all the verbs in a Spanish sentence and return their conjugations in the present, imperfect past, and preterite tenses according to the schema provided.",
        parameters: translationSchema,
      },
    ],
    function_call: { name: "find_verbs" },
    temperature: 0,
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we await a call to the OpenAI API. We are opening a request for a chat completion from the GPT3.5 turbo model and saving its response in a variable called &lt;code&gt;completion&lt;/code&gt;. The create function's arguments object accepts an array of messages that describe the "conversation" we are interested in having with the model. We supply a &lt;code&gt;system&lt;/code&gt; message to the model and we &lt;strong&gt;must&lt;/strong&gt; indicate to the model that it returns JSON. Our &lt;code&gt;user&lt;/code&gt; message simply includes our prompt which will be an English sentence we want to translate. Next we supply a &lt;code&gt;functions&lt;/code&gt; property to the argument object and define a new function &lt;code&gt;translate&lt;/code&gt;. We describe the function using the &lt;code&gt;description&lt;/code&gt; property and then supply our JSON schema as its &lt;code&gt;parameters&lt;/code&gt;. This will ensure the model provides JSON matching the schema when the function call is invoked. Notice we explicitly request the model to invoke our &lt;code&gt;translate&lt;/code&gt; function here: &lt;code&gt;function_call: { name: "translate" }&lt;/code&gt;. We set a &lt;code&gt;temperature&lt;/code&gt; of 0 to the API call which reduces randomness of returned content. &lt;/p&gt;

&lt;p&gt;Next, lets add some error handling to the API handler function and an actual response to the client when the endpoint is hit. Under the &lt;code&gt;completion&lt;/code&gt; variable, insert the following code,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//translator.ts

...

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {

   ...

   try {
    const functionCall = completion.choices[0].message.function_call;

    if (functionCall) {
      const argumentsJSON: TranslationObject = JSON.parse(
        functionCall?.arguments
      );

      // Validate the arguments
      const isValid = validate(argumentsJSON);

      if (!isValid) {
        return res.status(500).json({ success: false });
      }

      // Create a new translation object
      const translationObject: TranslationResponseObject = {
        success: true,
        translation: argumentsJSON,
      };

      return res.status(200).json(translationObject);
    }

    return res.status(500).json({ success: false });
  } catch (error) {
    throw error;
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First we extract the returned function call JSON text from the OpenAI API response, check it exists, and then parse the &lt;code&gt;arguments&lt;/code&gt; property from it into a Javascript object. Notice we coerce the object to be a &lt;code&gt;TranslationObject&lt;/code&gt; type, which we defined above. Next we validate the returned parsed JSON against our schema using &lt;code&gt;ajv&lt;/code&gt;. If the returned JSON is invalid we respond with an error code and no translation. If the JSON is valid we create a new &lt;code&gt;TranslationResponseObject&lt;/code&gt; and respond to our client with it, flagging the translation as a success.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step Three: Building Our Front End
&lt;/h2&gt;

&lt;p&gt;Our last step is to build out a front end application that consumes the the translation API we built. We will be using DaisyUI's TailwindCSS classes to manage our styles. Let's setup DaisyUI now.&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating a Layout Wrapper
&lt;/h4&gt;

&lt;p&gt;If you have been following along, you should already have DaisyUI installed a dependency in your project but if not, run &lt;code&gt;npm i -D daisyui@latest&lt;/code&gt; to install it as a dev dependency. Next we need to open our &lt;code&gt;tailwind.config.ts&lt;/code&gt; file in the root directory of the project and add DaisyUI as a plugin,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//tailwind.config.ts

import type { Config } from "tailwindcss";

const config: Config = {
  content: [
    "./src/pages/**/*.{js,ts,jsx,tsx,mdx}",
    "./src/components/**/*.{js,ts,jsx,tsx,mdx}",
    "./src/app/**/*.{js,ts,jsx,tsx,mdx}",
  ],
  plugins: [require("daisyui")],
};
export default config;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With DaisyUI installed, start by creating a &lt;code&gt;/components&lt;/code&gt; directory under &lt;code&gt;/src&lt;/code&gt;. We will store our React components for use in our NextJS frontend here. Let's create a simple layout to wrap our application using components from DaisyUI. Create a &lt;code&gt;/layout&lt;/code&gt; directory under &lt;code&gt;/src/components&lt;/code&gt; and add the following three files: &lt;code&gt;Header.tsx&lt;/code&gt;, &lt;code&gt;Footer.tsx&lt;/code&gt;, and &lt;code&gt;Layout.tsx&lt;/code&gt;,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Header.tsx

function Header() {
  return (
    &amp;lt;div className="navbar bg-base-100"&amp;gt;
      &amp;lt;a className="btn btn-ghost text-xl"&amp;gt;AI Spanish Translator&amp;lt;/a&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default Header;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Footer.tsx

function Footer() {
  return (
    &amp;lt;footer className="footer footer-center p-4 bg-base-300 text-base-content"&amp;gt;
      &amp;lt;aside&amp;gt;
        &amp;lt;p&amp;gt;Copyright © 2023 - All right reserved by ACME Industries Ltd&amp;lt;/p&amp;gt;
      &amp;lt;/aside&amp;gt;
    &amp;lt;/footer&amp;gt;
  );
}

export default Footer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Layout.tsx

import Header from "./Header";
import Footer from "./Footer";

//props typing
interface Props {
  children: React.ReactNode;
}

export default function Layout({ children }: Props) {
  return (
    &amp;lt;div className="h-screen flex flex-col"&amp;gt;
      &amp;lt;Header /&amp;gt;
      &amp;lt;main className="flex-grow"&amp;gt;{children}&amp;lt;/main&amp;gt;
      &amp;lt;Footer /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that &lt;code&gt;Layout&lt;/code&gt; accepts a &lt;code&gt;children&lt;/code&gt; prop which is typed as  a ReactNode. This means that any component we wrap with our &lt;code&gt;Layout&lt;/code&gt; component will be wrapped with our &lt;code&gt;Header&lt;/code&gt; component above it and our &lt;code&gt;Footer&lt;/code&gt; component below. Let's use our &lt;code&gt;Layout&lt;/code&gt; in the application next. Locate &lt;code&gt;_app.tsx&lt;/code&gt; under the &lt;code&gt;/src/pages&lt;/code&gt; directory. In a NextJS application, this component is responsible for rendering any component we mount at a given route. Let's modify it to use our &lt;code&gt;Layout&lt;/code&gt; component,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//_app.tsx

import "@/styles/globals.css";
import type { AppProps } from "next/app";

//import components
import Layout from "@/components/layout/Layout";

export default function App({ Component, pageProps }: AppProps) {
  return (
    &amp;lt;Layout&amp;gt;
      &amp;lt;Component {...pageProps} /&amp;gt;
    &amp;lt;/Layout&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's remove the boilerplate from when we scaffolded the project using &lt;code&gt;create-next-app&lt;/code&gt;. Navigate to &lt;code&gt;/src/pages/index.tsx&lt;/code&gt;. This file is our main entry point and the default component to mount when we launch our NextJS application's development server. Replace the code with the following,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//index.tsx

export default function Home() {
  return &amp;lt;div&amp;gt;hello world!&amp;lt;/div&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next navigate to &lt;code&gt;/src/styles/globals.css&lt;/code&gt; and remove everything except for the TailwindCSS declarations at the top,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//globals.css

@tailwind base;
@tailwind components;
@tailwind utilities;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's try running our web application using the development server. In a terminal, navigate to your project directory. Then run the following command, &lt;code&gt;npm run dev&lt;/code&gt;. This command should start the NextJS development server and if you navigate to &lt;code&gt;http://localhost:3000/&lt;/code&gt; in a web browser, you should see the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuvuz1m5dd6ce265kefwf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuvuz1m5dd6ce265kefwf.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating Our Global State Store
&lt;/h4&gt;

&lt;p&gt;We will use Zustand to create a global state store that can share the object representing our translated sentence and across our application. If you didn't install Zustand and axios above, do so now with &lt;code&gt;npm i zustand axios&lt;/code&gt;. Under &lt;code&gt;/src&lt;/code&gt; create a new directory called &lt;code&gt;/stores&lt;/code&gt;. In &lt;code&gt;/stores&lt;/code&gt; create a new file called &lt;code&gt;translationStore.tsx&lt;/code&gt;. We will put our Zustand store here. Let's import Zustand's &lt;code&gt;create&lt;/code&gt; function and define a Typescript type that describes what sort of state and functionality our store should have,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//translationStore.tsx

import { create } from "zustand";

import type { TranslationObject } from "@/types/translationTypes";

interface TranslationStore {
  translation: TranslationObject | null;
  setTranslation: (translation: TranslationObject | null) =&amp;gt; void;

  translationIsLoading: boolean;
  setTranslationIsLoading: (isLoading: boolean) =&amp;gt; void;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our &lt;code&gt;TranslationStore&lt;/code&gt; interface defines a Zustand store as an object. It has a getter and setter for a &lt;code&gt;translation&lt;/code&gt; property which will either be equal to a &lt;code&gt;TranslationObject&lt;/code&gt; (matching the schema we enforce on the JSON returned through the OpenAI API) or &lt;code&gt;null&lt;/code&gt; meaning no translation object exists. We also define as setter and getting for whether a translation is currently loading. &lt;/p&gt;

&lt;p&gt;Let's create the actual Zustand store next,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//translationStore.tsx

...

const useTranslationStore = create&amp;lt;TranslationStore&amp;gt;()((set) =&amp;gt; ({
  translation: null,
  setTranslation(translation) {
    set({ translation });
  },

  translationIsLoading: false,
  setTranslationIsLoading(isLoading) {
    set({ translationIsLoading: isLoading });
  },
}));

export default useTranslationStore;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Zustand supports a React hook based API. We export our useTranslationStore hook and can use it to access the state we have defined anywhere in our application.&lt;/p&gt;

&lt;h4&gt;
  
  
  Getting User Input
&lt;/h4&gt;

&lt;p&gt;Next, let's define a component that accepts an English sentence from a user to be submitted through translator endpoint to the OpenAI API. &lt;/p&gt;

&lt;p&gt;Under &lt;code&gt;/src/components/&lt;/code&gt; create a new directory called &lt;code&gt;prompt-box&lt;/code&gt; and add a &lt;code&gt;PromptBox.tsx&lt;/code&gt; file to it with the following code,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//PromptBox.tsx

function PromptBox() {
  return (
    &amp;lt;div className="w-full flex flex-col space-y-4"&amp;gt;
      &amp;lt;h3 className="text-xl font-semibold"&amp;gt;
        Enter English text to translate it to Spanish:
      &amp;lt;/h3&amp;gt;
      &amp;lt;div className="flex space-x-4"&amp;gt;
        &amp;lt;textarea
          className="textarea textarea-accent textarea-lg w-full"
          placeholder="type here"
        /&amp;gt;
        &amp;lt;button className="btn btn-success"&amp;gt;Translate&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default PromptBox;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file defines a functional component called &lt;code&gt;PromptBox&lt;/code&gt; that will allow a user to input an English sentence to be translated to Spanish. After the use types their English sentence into the textarea field, we want to submit it to our translator API when the user clicks the button. Let's set up that functionality. First let's import what we need above the functional component,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//PromptBox.tsx

import { useState } from "react";
import axios from "axios";

import type { TranslationResponseObject } from "@/types/translationTypes";

import useTranslationStore from "@/stores/translationStore";

...

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's define a &lt;code&gt;POST&lt;/code&gt; request to our translator API using &lt;code&gt;axios&lt;/code&gt;,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//PromptBox.tsx

import { useState } from "react";
import axios from "axios";

import type { TranslationResponseObject } from "@/types/translationTypes";

import useTranslationStore from "@/stores/translationStore";

// create an axios get request to the api
const getTranslation = async (prompt: string) =&amp;gt; {
  const response = await axios.post&amp;lt;TranslationResponseObject&amp;gt;(
    "/api/translator",
    { prompt: prompt }
  );
  return response.data;
};

...

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;getTranslation&lt;/code&gt; will create a &lt;code&gt;POST&lt;/code&gt; request to our translator API and return a &lt;code&gt;Promise&lt;/code&gt; that is typed as our &lt;code&gt;TranslationResponseObject&lt;/code&gt;. We can use this function to set state in our client's Zustand store,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//PromptBox.tsx

...

function PromptBox() {
  //store selector
  const [setTranslation, setTranslationIsLoading] = useTranslationStore(
    (state) =&amp;gt; [state.setTranslation, state.setTranslationIsLoading]
  );

  //component state
  const [prompt, setPrompt] = useState&amp;lt;string&amp;gt;("");

  const handleSubmit = async () =&amp;gt; {
    if (prompt === "") return;

    setTranslationIsLoading(true);
    const response = await getTranslation(prompt);
    setTranslationIsLoading(false);

    if (response.success) {
      setTranslation(response.translation);
    }
  };

  return (
    &amp;lt;div className="w-full flex flex-col space-y-4"&amp;gt;
      &amp;lt;h3 className="text-xl font-semibold"&amp;gt;
        Enter English text to translate it to Spanish:
      &amp;lt;/h3&amp;gt;
      &amp;lt;div className="flex space-x-4"&amp;gt;
        &amp;lt;textarea
          className="textarea textarea-accent textarea-lg w-full"
          placeholder="type here"
          value={prompt}
          onChange={(e) =&amp;gt; setPrompt(e.target.value)}
        /&amp;gt;
        &amp;lt;button onClick={handleSubmit} className="btn btn-success"&amp;gt;
          Translate
        &amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default PromptBox;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First we grab the &lt;code&gt;setTranslation&lt;/code&gt; and &lt;code&gt;setTranslationIsLoading&lt;/code&gt; function from our Zustand store. Next we define some state for the text input our user will write into the textarea in our component. Then we define a &lt;code&gt;handleSubmit&lt;/code&gt; function which will run when our button is clicked, set our &lt;code&gt;translationIsLoading&lt;/code&gt; global state to true, await a response with a TranslationResponseObject from our translator API and then set our &lt;code&gt;translation&lt;/code&gt; global state to the response if it's a success. &lt;/p&gt;

&lt;p&gt;Let's add &lt;code&gt;PromptBox&lt;/code&gt; to our application. Head back to &lt;code&gt;index.tsx&lt;/code&gt; and modify it as follows,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//import stores
import useTranslationStore from "@/stores/translationStore";

//import components
import PromptBox from "@/components/prompt-box/PromptBox";

export default function Home() {
  const [translation, translationIsLoading] = useTranslationStore((state) =&amp;gt; [
    state.translation,
    state.translationIsLoading,
  ]);

return (
    &amp;lt;div className="w-full h-full px-4 py-4 flex flex-col space-y-4 bg-neutral"&amp;gt;
      {!translationIsLoading &amp;amp;&amp;amp; !translation &amp;amp;&amp;amp; &amp;lt;PromptBox /&amp;gt;}

      {translationIsLoading &amp;amp;&amp;amp; (
        &amp;lt;span className="loading loading-dots loading-lg"&amp;gt;&amp;lt;/span&amp;gt;
      )}
    &amp;lt;/div&amp;gt;
  );

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we get the &lt;code&gt;translation&lt;/code&gt; and &lt;code&gt;translationIsLoading&lt;/code&gt; state from our Zustand store and conditionally render &lt;code&gt;PromptBox&lt;/code&gt; only if both are null. If a translation is loading, we show a loading spinner instead. Your application should now look like this: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqnul1pb2184mmyqi4nfx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqnul1pb2184mmyqi4nfx.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating Display Components For Our Translation
&lt;/h4&gt;

&lt;p&gt;Let's create the final components for the application that display the content from the &lt;code&gt;TranslationObject&lt;/code&gt; generated by the AI. &lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;translation-components&lt;/code&gt; directory under &lt;code&gt;/src/components&lt;/code&gt; and add a directory called &lt;code&gt;/sentence&lt;/code&gt; to it. Then create &lt;code&gt;Sentence.tsx&lt;/code&gt; under &lt;code&gt;src/components/translation-components/sentence/&lt;/code&gt; with the following,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Sentence.tsx

//import stores
import useTranslationStore from "@/stores/translationStore";

function Sentence() {
  // store selector
  const [translation, setTranslation] = useTranslationStore((state) =&amp;gt; [
    state.translation,
    state.setTranslation,
  ]);

  return (
    &amp;lt;div className="card w-full bg-base-300 shadow-xl"&amp;gt;
      &amp;lt;div className="card-body"&amp;gt;
        &amp;lt;div className="card-actions justify-end"&amp;gt;
          &amp;lt;button
            onClick={() =&amp;gt; setTranslation(null)}
            className="btn btn-primary btn-sm"
          &amp;gt;
            Translate something else
          &amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;p className="text-xl"&amp;gt;English: {translation?.sentence}&amp;lt;/p&amp;gt;
        &amp;lt;p className="text-xl"&amp;gt;
          Translation: {translation?.translatedSentence}
        &amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default Sentence;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This component grabs the &lt;code&gt;translation&lt;/code&gt; global state from our store and displays the original sentence and the translated sentence. It also has a button which sets &lt;code&gt;translation&lt;/code&gt; to null in our store which will allow us to enter a new sentence.&lt;/p&gt;

&lt;p&gt;Next create a directory, &lt;code&gt;/src/components/translation-components/nouns&lt;/code&gt;. We are going to use DaisyUI's "card" component class to create a display card for each noun identified by the AI in the translated sentence. Create &lt;code&gt;NounCard.tsx&lt;/code&gt; under the newly created &lt;code&gt;/nouns&lt;/code&gt; directory with,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//NounCard.tsx

import type { NounObject } from "@/types/translationTypes";

//props typing
interface Props {
  noun: NounObject;
}

function NounCard({ noun }: Props) {
  return (
    &amp;lt;div className="card w-full bg-base-100 shadow-xl"&amp;gt;
      &amp;lt;div className="card-body"&amp;gt;
        &amp;lt;h2 className="card-title"&amp;gt;{noun.noun}&amp;lt;/h2&amp;gt;
        &amp;lt;p&amp;gt;Translation: {noun.translation}&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default NounCard;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This components accepts a &lt;code&gt;NounObject&lt;/code&gt; which we defined earlier in our types file. It renders a card that contains the Spanish noun and its translation. We will need a parent component responsible for rendering all of the &lt;code&gt;NounCard&lt;/code&gt; components for each noun in the sentence. Create &lt;code&gt;Nouns.tsx&lt;/code&gt; in the same directory with the following code,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Nouns.tsx

//import stores
import useTranslationStore from "@/stores/translationStore";

//import components
import NounCard from "./NounCard";

function Nouns() {
  // store selector
  const [translation] = useTranslationStore((state) =&amp;gt; [state.translation]);

  if (!translation?.nouns) return null;

  return (
    &amp;lt;div className="flex flex-col space-y-4"&amp;gt;
      &amp;lt;h3 className="text-lg"&amp;gt;Nouns:&amp;lt;/h3&amp;gt;

        &amp;lt;div className="grid grid-cols-3 gap-4"&amp;gt;
          {translation.nouns.map((noun) =&amp;gt; (
            &amp;lt;NounCard noun={noun} key={noun.noun} /&amp;gt;
          ))}
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default Nouns;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Nouns&lt;/code&gt; checks if the &lt;code&gt;.nouns&lt;/code&gt; property exists on our &lt;code&gt;translation&lt;/code&gt; global state and then returns a grid of &lt;code&gt;NounCard&lt;/code&gt; components with for each noun identified by the AI. Let's set up something simila for verbs in the sentence. Create a &lt;code&gt;src/components/translation-components/verbs/&lt;/code&gt; directory with two components.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;VerbCard.tsx&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//VerbCard.tsx

//import types
import type { VerbObject } from "@/types/translationTypes";

//props typing
interface Props {
  verb: VerbObject;
}

function VerbCard({ verb }: Props) {
  return (
    &amp;lt;div className="card w-full bg-base-100 shadow-xl px-4 py-4"&amp;gt;
      &amp;lt;div className="card-body"&amp;gt;
        &amp;lt;h2 className="card-title"&amp;gt;
          {verb.infinitive} - {verb.translation}
        &amp;lt;/h2&amp;gt;
      &amp;lt;/div&amp;gt;

      {/* present conjugations */}
      &amp;lt;div className="overflow-x-auto"&amp;gt;
        &amp;lt;table className="table"&amp;gt;
          {/* head */}
          &amp;lt;thead&amp;gt;
            &amp;lt;tr&amp;gt;
              &amp;lt;th&amp;gt;Tense&amp;lt;/th&amp;gt;
              &amp;lt;th&amp;gt;Yo&amp;lt;/th&amp;gt;
              &amp;lt;th&amp;gt;Tú&amp;lt;/th&amp;gt;
              &amp;lt;th&amp;gt;Él/Ella&amp;lt;/th&amp;gt;
              &amp;lt;th&amp;gt;Nosotros&amp;lt;/th&amp;gt;
              &amp;lt;th&amp;gt;Vosotros&amp;lt;/th&amp;gt;
              &amp;lt;th&amp;gt;Ellos/Ellas&amp;lt;/th&amp;gt;
            &amp;lt;/tr&amp;gt;
          &amp;lt;/thead&amp;gt;
          &amp;lt;tbody&amp;gt;
            &amp;lt;tr&amp;gt;
              &amp;lt;th&amp;gt;Present&amp;lt;/th&amp;gt;
              &amp;lt;td&amp;gt;{verb.conjugations.present.yo}&amp;lt;/td&amp;gt;
              &amp;lt;td&amp;gt;{verb.conjugations.present.tu}&amp;lt;/td&amp;gt;
              &amp;lt;td&amp;gt;{verb.conjugations.present.el}&amp;lt;/td&amp;gt;
              &amp;lt;td&amp;gt;{verb.conjugations.present.nosotros}&amp;lt;/td&amp;gt;
              &amp;lt;td&amp;gt;{verb.conjugations.present.vosotros}&amp;lt;/td&amp;gt;
              &amp;lt;td&amp;gt;{verb.conjugations.present.ellos}&amp;lt;/td&amp;gt;
            &amp;lt;/tr&amp;gt;
          &amp;lt;/tbody&amp;gt;
        &amp;lt;/table&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default VerbCard;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;VerbCard&lt;/code&gt; component serves a similar function as our &lt;code&gt;NounCard&lt;/code&gt; component except it also produces a table containing the present tense conjugations of the Spanish verb identified by the AI. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Verbs.tsx&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Verbs.tsx

//import stores
import useTranslationStore from "@/stores/translationStore";

//import components
import VerbCard from "./VerbCard";

function Verbs() {
  // store selector
  const [translation] = useTranslationStore((state) =&amp;gt; [state.translation]);

  if (!translation?.verbs) return null;

  return (
    &amp;lt;div className="flex flex-col space-y-4"&amp;gt;

&amp;lt;h3 className="text-lg"&amp;gt;Verbs:&amp;lt;/h3&amp;gt;

        &amp;lt;div className="flex flex-col space-y-4"&amp;gt;
          {translation.verbs.map((verb) =&amp;gt; (
            &amp;lt;VerbCard verb={verb} key={verb.infinitive} /&amp;gt;
          ))}
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default Verbs;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Verbs&lt;/code&gt; component similarly renders a grid of &lt;code&gt;VerbCard&lt;/code&gt; components using our TranslationObject's &lt;code&gt;verbs&lt;/code&gt; property.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;We now have all the pieces in place to finish our simple application! Let's incorporate the &lt;code&gt;Sentence&lt;/code&gt;, &lt;code&gt;Nouns&lt;/code&gt; and &lt;code&gt;Verbs&lt;/code&gt; components.&lt;/p&gt;

&lt;p&gt;Let's import those components into our &lt;code&gt;index.tsx&lt;/code&gt; file and render them whenever a translation exists in our Zustand store. Here is what your final &lt;code&gt;index.tsx&lt;/code&gt; file should look like,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//index.tsx

//import stores
import useTranslationStore from "@/stores/translationStore";

//import components
import PromptBox from "@/components/prompt-box/PromptBox";
import Nouns from "@/components/translation-components/nouns/Nouns";
import Sentence from "@/components/translation-components/sentence/Sentence";
import Verbs from "@/components/translation-components/verbs/Verbs";

export default function Home() {
  const [translation, translationIsLoading] = useTranslationStore((state) =&amp;gt; [
    state.translation,
    state.translationIsLoading,
  ]);

  return (
    &amp;lt;div className="w-full h-full px-4 py-4 flex flex-col space-y-4 bg-neutral"&amp;gt;
      {!translationIsLoading &amp;amp;&amp;amp; !translation &amp;amp;&amp;amp; &amp;lt;PromptBox /&amp;gt;}

      {translationIsLoading &amp;amp;&amp;amp; (
        &amp;lt;span className="loading loading-dots loading-lg"&amp;gt;&amp;lt;/span&amp;gt;
      )}

      {translation &amp;amp;&amp;amp; (
        &amp;lt;&amp;gt;
          &amp;lt;Sentence /&amp;gt;
          &amp;lt;Nouns /&amp;gt;
          &amp;lt;Verbs /&amp;gt;
        &amp;lt;/&amp;gt;
      )}
    &amp;lt;/div&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is the result after inputting an English sentence into the translator app:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fes8op338461xux119iwm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fes8op338461xux119iwm.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing Thoughts
&lt;/h2&gt;

&lt;p&gt;OpenAI's function calling feature represents a breakthrough in how developers can integrate LLM functionality into their projects on the web. We have demonstrated a basic application that taps into this feature in order to translate plain language into strictly formatted JSON code. If you followed along with the tutorial the whole way through, congratulations! As mentioned above, I encourage you to experiment with either translating another language, extracting different elements of a language or creating something entirely different.  &lt;/p&gt;

&lt;p&gt;You can find the Github repo for the project &lt;a href="https://github.com/jamesonkunkel/lang-ai-next" rel="noopener noreferrer"&gt;here&lt;/a&gt; with some additional code added to support other verb tenses in the Spanish language. &lt;/p&gt;

</description>
      <category>openai</category>
      <category>nextjs</category>
      <category>ai</category>
      <category>zustand</category>
    </item>
  </channel>
</rss>
