<?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: irafrog</title>
    <description>The latest articles on DEV Community by irafrog (@irafrog).</description>
    <link>https://dev.to/irafrog</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%2F1127831%2F2c505e0f-8650-4f68-9746-4486a76f6172.png</url>
      <title>DEV Community: irafrog</title>
      <link>https://dev.to/irafrog</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/irafrog"/>
    <language>en</language>
    <item>
      <title>Vectorize your code repositories and PDFs with AI</title>
      <dc:creator>irafrog</dc:creator>
      <pubDate>Tue, 19 Sep 2023 12:25:36 +0000</pubDate>
      <link>https://dev.to/irafrog/vectorize-your-code-repositories-and-pdfs-with-ai-256m</link>
      <guid>https://dev.to/irafrog/vectorize-your-code-repositories-and-pdfs-with-ai-256m</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;




&lt;p&gt;In this tutorial, we'll walk you through the process of using the &lt;code&gt;@polyfact/vectorizer&lt;/code&gt; package to vectorize code repositories and PDFs using this AI package. This will help you convert your textual data into vector representations that can be used for various machine learning and data analysis tasks.&lt;/p&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Stack&lt;/li&gt;
&lt;li&gt;Step 1: Installation&lt;/li&gt;
&lt;li&gt;Step 2: Usage as a Library&lt;/li&gt;
&lt;li&gt;Step 2.1: Usage via Command Line Interface&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;li&gt;Other resources&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Stack
&lt;/h2&gt;

&lt;p&gt;To use the PolyFact vectorizer, you only need a terminal and your preferred work environment. Choose a document or a repository you want to vectorize.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1: Installation
&lt;/h2&gt;

&lt;p&gt;To get started, you need to install the &lt;code&gt;@polyfact/vectorizer&lt;/code&gt; package. You can do this using the Node Package Manager (npm):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @polyfact/vectorizer

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

&lt;/div&gt;



&lt;p&gt;If you want to use the CLI globally, you can install it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; @polyfact/vectorizer

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 2: Usage as a Library
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Importing the Library&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, let's import the &lt;code&gt;@polyfact/vectorizer&lt;/code&gt; library and set up the vectorizer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Vectorizer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;SourceType&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@polyfact/vectorizer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;your-api-token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;maxTokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Adjust as needed&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sourceType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;SourceType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DIRECTORY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;vectorizer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Vectorizer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;maxTokens&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sourceType&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Vectorizing Code Repositories&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, let's see how you can use the vectorizer to process code repositories:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filePaths&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path/to/your/repository&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;files&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;vectorizer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFiles&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filePaths&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;vectorizer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;vectorize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;files&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;progressCallback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;memoryId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;vectorizer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getMemoryId&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this code snippet, the &lt;code&gt;vectorizer&lt;/code&gt; tool is utilized to process and convert folders, PDFs, or audios from a specified path into vectorized format. Upon completion, a unique memory ID is returned. This memory ID acts as a distinct identifier, allowing you to pair it with the &lt;code&gt;generate&lt;/code&gt; function's &lt;code&gt;memoryId&lt;/code&gt; option. Consequently, when sending a task related to your files, PDF, or audio, the model will directly leverage your &lt;code&gt;embeddings&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It is also possible to use the PolyFact SDK to do the same thing, except for the PDFs. You can find out more &lt;a href="https://polyfact.readme.io/reference/polyfact"&gt;here&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2.1: Usage via Command Line Interface
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Vectorize a Code Repository&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To vectorize an entire code repository, use the following CLI command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;@polyfact/vectorizer repo path/to/your/repository &lt;span class="nt"&gt;--token&lt;/span&gt; your-api-token &lt;span class="nt"&gt;--max-token&lt;/span&gt; 1000

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Vectorize PDF Files&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To vectorize PDF files, use the following CLI command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;@polyfact/vectorizer pdf file1.pdf file2.pdf &lt;span class="nt"&gt;--token&lt;/span&gt; your-api-token

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congratulations! You've learned how to use the &lt;code&gt;@polyfact/vectorizer&lt;/code&gt; package to vectorize code repositories and PDFs using the PolyFact AI. These vector representations can be incredibly useful for various machine learning and data analysis tasks. Feel free to explore the PolyFact SDK documentation to learn more about how to use the generated memory ID in your projects.&lt;/p&gt;

&lt;p&gt;For more information and more packages, refer to the &lt;a href="https://polyfact.readme.io/reference/polyfact"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Other resources:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/polyfact/polyfact-node/wiki"&gt;Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://discord.com/invite/v3Wxu4APaZ"&gt;Discord&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/polyfact"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/PolyFactAI"&gt;Twitter&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>chatbot</category>
      <category>ai</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Creating a Simple ChatGPT-like Chatbot: Tutorial</title>
      <dc:creator>irafrog</dc:creator>
      <pubDate>Tue, 12 Sep 2023 16:32:57 +0000</pubDate>
      <link>https://dev.to/irafrog/creating-a-simple-chatgpt-like-chatbot-tutorial-13ee</link>
      <guid>https://dev.to/irafrog/creating-a-simple-chatgpt-like-chatbot-tutorial-13ee</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;With this tutorial, you can learn to create a simple ChatGPT‐like chatbot using React and PolyFact. It can be followed by beginner developers as well. Hope you find it useful!&lt;/p&gt;




&lt;h3&gt;
  
  
  Stack
&lt;/h3&gt;

&lt;p&gt;The stack we will be using in this tutorial is React for the user interface, Tailwind CSS for easy styling, and PolyFact SDK for chatbot functionality. A fundamental grasp of programming concepts and React is recommended to successfully follow this tutorial.&lt;/p&gt;




&lt;h3&gt;
  
  
  Full Documentation
&lt;/h3&gt;

&lt;p&gt;You can consult the libraries for every stack if you have any problems or need more information.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/polyfact/polyfact-node/wiki"&gt;PolyFact Documentation&lt;/a&gt; &lt;br&gt;
&lt;a href="https://legacy.reactjs.org/docs/getting-started.html"&gt;React Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://v2.tailwindcss.com/docs"&gt;Tailwind Documentation&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  Step 0: Get a PolyFact API key for LLM calls on PolyFact
&lt;/h3&gt;

&lt;p&gt;First, you will need to already have created an account on &lt;a href="https://app.polyfact.com/"&gt;https://app.polyfact.com/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When your account is created you can get a project_id by creating a project here: &lt;a href="https://app.polyfact.com/project/new"&gt;https://app.polyfact.com/project/new&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will need it for later!&lt;/p&gt;


&lt;h3&gt;
  
  
  Step 1: Setting up the React App
&lt;/h3&gt;

&lt;p&gt;Open your terminal and initialize a new React app using TypeScript. Any terminal will suffice.&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-react-app chat-using-polyfact --template typescript
cd chat-using-polyfact
npm install polyfact
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, in a separate terminal, you can run the development server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 2: Integrating PolyFact SDK
&lt;/h3&gt;

&lt;p&gt;We will start by adding a login feature. We'll use the &lt;code&gt;usePolyfact&lt;/code&gt; hook to access the login functionality.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To make PolyFact calls directly from the frontend, you always need an Authentification session. If you put our global API token in the Client, your PolyFact project might get hacked.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;src/App.tsx&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import { usePolyfact } from 'polyfact';

function App() {
  const { login, polyfact } = usePolyfact({ project: "&amp;lt;your_project_id&amp;gt;" });
  return (
    &amp;lt;div&amp;gt;{
        login ? (&amp;lt;button onClick={() =&amp;gt; login({ provider: "github" })}&amp;gt;Login with Github&amp;lt;/button&amp;gt;) : polyfact ? "We are logged!" : "Loading..."
    }&amp;lt;/div&amp;gt;
  )
}

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

&lt;/div&gt;



&lt;p&gt;This code defines a React component that uses the &lt;code&gt;usePolyfact&lt;/code&gt; hook to handle GitHub authentication. It renders different content based on the user's login status and whether the authentication process is still loading.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 3: Generating Data
&lt;/h3&gt;

&lt;p&gt;Remove the &lt;code&gt;&amp;lt;StrictMode&amp;gt;&lt;/code&gt; tags in your index.tsx.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;PolyFact works with strict mode, but here we want to avoid duplicated useEffect requests.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;src/index.tsx&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement
);
root.render(
  &amp;lt;App /&amp;gt;
);

reportWebVitals();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's make our component generate and display a basic text once the &lt;code&gt;polyfact&lt;/code&gt; object is ready:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;src/App.tsx&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';
import { usePolyfact } from 'polyfact';

function App() {
  const { login, polyfact } = usePolyfact({ project: "&amp;lt;your_project_id&amp;gt;" });
  const [helloWorld, setHelloWorld] = useState&amp;lt;string&amp;gt;();

  useEffect(() =&amp;gt; {
    if (polyfact) {
      (async () =&amp;gt; {
        const haiku = await polyfact.generate("Write a hello world haiku");
        if (typeof haiku === "string") setHelloWorld(haiku);
      })()
    }
  }, [polyfact])

  return (
    &amp;lt;div className="text-2xl font-bold p-4"&amp;gt;
      {login ? (
        &amp;lt;button onClick={() =&amp;gt; login({ provider: "github" })}&amp;gt;
          Login with Github
        &amp;lt;/button&amp;gt;
      ) : helloWorld ? (
        &amp;lt;p style={{ whiteSpace: "pre-line" }}&amp;gt;{helloWorld}&amp;lt;/p&amp;gt;
      ) : (
        "Loading..."
      )}
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;This code defines a React component that uses the hook to handle GitHub authentication and generate a "hello world" haiku. The haiku generation is triggered when the &lt;code&gt;polyfact&lt;/code&gt; value changes. The component conditionally renders content based on the user's login status and the availability of the generated haiku.&lt;/p&gt;

&lt;p&gt;Your AI generated Haiku should look like something similar to this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5H4tZeoa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4g1a6gm8g9bu86n34akb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5H4tZeoa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4g1a6gm8g9bu86n34akb.png" alt="Screenshot of a haiku that was generated by the AI" width="800" height="520"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  Step 4: Styling with TailwindCSS
&lt;/h3&gt;

&lt;p&gt;For styling our chat interface, we'll be using TailwindCSS. Install it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D tailwindcss
npx tailwindcss init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, set up your styles and configure Tailwind:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;src/index.css&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@tailwind base;
@tailwind components;
@tailwind utilities;

html, body, #root {
    width: 100%;
    height: 100%;
    margin: 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;tailwind.config.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./src/**/*.{js,jsx,ts,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to restart your server to apply the Tailwind styles.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 5: Designing the Chat Interface
&lt;/h3&gt;

&lt;p&gt;Time to design our chat interface: &lt;strong&gt;ChatBox Component.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a new ChatBox.tsx file and paste that code in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ChatBox() {
  return (
    &amp;lt;div className="flex flex-col items-center h-full py-2"&amp;gt;
        &amp;lt;div className="border-black border border-solid p-4 mb-2 w-[800px] grow"&amp;gt;
            &amp;lt;pre&amp;gt;
              &amp;lt;p&amp;gt;&amp;lt;b&amp;gt;AI:&amp;lt;/b&amp;gt; Hello World!&amp;lt;/p&amp;gt;
            &amp;lt;/pre&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="flex w-[800px]"&amp;gt;
            &amp;lt;div className="flex grow items-center border-black border border-solid"&amp;gt;
                &amp;lt;div className="font-bold ml-4"&amp;gt;Human:&amp;lt;/div&amp;gt;
                &amp;lt;input className="p-1 my-2 mx-4 h-12 font-mono grow" placeholder="Type your message here !" /&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;button className="bg-black text-white ml-2 p-2 px-5 font-mono font-bold"&amp;gt;Send &amp;amp;gt;&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Now, we want to import that ChatBot interface into our main App. Change your App.tsx so that it looks like the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  const { login, polyfact } = usePolyfact({
    project: "23104477-b687-45c7-9980-de306ec16df3",
  });

  useEffect(() =&amp;gt; {
    if (polyfact) {
      (async () =&amp;gt; {})();
    }
  }, [polyfact]);

  return (
    &amp;lt;div className="text-2xl font-bold p-2"&amp;gt;
      {login ? (
        &amp;lt;button onClick={() =&amp;gt; login({ provider: "github" })}&amp;gt;
          Login with Github
        &amp;lt;/button&amp;gt;
      ) : polyfact ? (
        &amp;lt;ChatBox /&amp;gt;
      ) : (
        "Loading..."
      )}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this code, the &lt;code&gt;ChatBox&lt;/code&gt; component renders a chat interface where messages are displayed, and the user can input new messages. The messages are displayed with labels ("Human" or "AI") and the content. The input field and button allow users to interact with the chat interface.&lt;/p&gt;

&lt;p&gt;Your interface should look like this:&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oh5MWoCu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/phc8ny2rj0fhb33b32uv.png" alt='Screenshot of a chat interface, where messages are displayed, and the user can input new messages.The messages are displayed with labels ("Human" or "AI") and the content. ' width="800" height="520"&gt;
&lt;/h2&gt;

&lt;p&gt;But we can't yet tap messages; we need to add the JavaScript logic in the ChatBox component to do that. This is done below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ChatBox&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;messages&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;is_user_message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}[]&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"flex flex-col items-center h-full py-2"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"border-black border border-solid p-4 mb-2 w-[800px] grow"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elem&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;b&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;is_user_message&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Human:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;AI:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;b&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"flex w-[800px]"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"flex grow items-center border-black border border-solid"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"font-bold ml-4"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Human:&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"p-1 my-2 mx-4 h-12 font-mono grow"&lt;/span&gt; &lt;span class="na"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Type your message here !"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"bg-black text-white ml-2 p-2 px-5 font-mono font-bold"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Send &amp;gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ChatBox&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should give you an error; don't worry about it, and go directly to the next step.&lt;/p&gt;




&lt;p&gt;Next, we enhance our &lt;code&gt;ChatBox&lt;/code&gt; by introducing the callback property &lt;code&gt;onMessage&lt;/code&gt; to handle user message submissions. For this, we need to import the &lt;code&gt;FormEvent&lt;/code&gt; type from React. Update your ChatBox.tsx:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, FormEvent } from "react";

function ChatBox({ messages, onMessage }: { messages: { is_user_message: boolean, content: string }[], onMessage: (message: string) =&amp;gt; Promise&amp;lt;void&amp;gt; | void }) {
  const [loading, setLoading] = useState(false);
  async function handleSubmit(e: FormEvent&amp;lt;HTMLFormElement&amp;gt;) {
      e.preventDefault();

      setLoading(true);
      const message = (e.target as any).message.value;
      if (message) {
        (e.target as any).message.value = "";
        await onMessage(message);
      }
      setLoading(false);
  }

  return (
    &amp;lt;div className="flex flex-col items-center h-full py-2"&amp;gt;
        &amp;lt;div className="border-black border border-solid p-4 mb-2 w-[800px] grow overflow-y-scroll"&amp;gt;
            &amp;lt;pre&amp;gt;
                {messages.map(elem =&amp;gt; (&amp;lt;p className="whitespace-pre-wrap"&amp;gt;&amp;lt;b&amp;gt;{elem.is_user_message ? "Human:" : "AI:"}&amp;lt;/b&amp;gt; {elem.content}&amp;lt;/p&amp;gt;))}
            &amp;lt;/pre&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;form onSubmit={handleSubmit} className="flex w-[800px]"&amp;gt;
            &amp;lt;div className="flex grow items-center border-black border border-solid"&amp;gt;
                &amp;lt;div className="font-bold ml-4"&amp;gt;Human:&amp;lt;/div&amp;gt;
                &amp;lt;input className="p-1 my-2 mx-4 h-12 font-mono grow" placeholder="Type your message here !" name="message" /&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;input className="cursor-pointer bg-black text-white ml-2 p-2 px-5 font-mono font-bold" value={loading ? "Loading..." : "Send &amp;gt;"} type="submit" disabled={loading} /&amp;gt;
        &amp;lt;/form&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;The component now handles form submissions, loading states, and updates to the user interface for a better user experience.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 6: Implementing the Chat Logic
&lt;/h3&gt;

&lt;p&gt;To make that preceding Chat Logic work, we need to update our App.tsx, and work with the PolyFact API to generate chat messages. We will make a chat and display a chat history of the conversation. Let's update our App component so it looks something like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;App Component&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  const { login, polyfact } = usePolyfact({ project: "&amp;lt;your_project_id&amp;gt;" });
  const [chat, setChat] = useState&amp;lt;Chat&amp;gt;();
  const [messages, setMessages] = useState&amp;lt;{ is_user_message: boolean, content: string }[]&amp;gt;([]);

  useEffect(() =&amp;gt; {
      if (polyfact) {
          setChat(new polyfact.Chat());
      }
  }, [polyfact]);

  return (
    &amp;lt;&amp;gt;{
      login ? (&amp;lt;button onClick={() =&amp;gt; login({ provider: "github" })}&amp;gt;Login with Github&amp;lt;/button&amp;gt;)
      : chat ? &amp;lt;ChatBox messages={messages} onMessage={async (message) =&amp;gt; {
          await chat.sendMessage(message);
          setMessages((await chat.getMessages()).reverse());
      }} /&amp;gt;
      : "Loading..."
    }&amp;lt;/&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try typing in a question! You should get an answer from GPT-3.5, the default PolyFact AI model. Your interface should look something like the one below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_fLVSUBk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0eq9jse38e3g9uuxo0y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_fLVSUBk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0eq9jse38e3g9uuxo0y.png" alt="Screenshot of the finished chat interface, with messages exchanged between the user and the chatbot." width="800" height="520"&gt;&lt;/a&gt;&lt;br&gt;
And there you have it! You've successfully created a simple chat application similar to ChatGPT using React and Polyfact. Enjoy chatting!&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 7: Polishing the Chatbot
&lt;/h3&gt;

&lt;p&gt;You could stop after the last step if you are satisfied with a simple app, but you could also work on it further and iron out any details. You can change the interface how you like and fine-tune the model to be more precise!&lt;/p&gt;

&lt;p&gt;If you have any more questions, you can ask for help on &lt;a href="https://discord.com/invite/v3Wxu4APaZ"&gt;our discord&lt;/a&gt;, or in another dev-populated space, like dedicated subreddits or discord servers.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

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