<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Lars Grammel on Medium]]></title>
        <description><![CDATA[Stories by Lars Grammel on Medium]]></description>
        <link>https://medium.com/@lgrammel?source=rss-310ec22024e6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*pEImW29Ytx-dRSjXrtrfYQ.jpeg</url>
            <title>Stories by Lars Grammel on Medium</title>
            <link>https://medium.com/@lgrammel?source=rss-310ec22024e6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 02:22:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lgrammel/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Effortlessly Generate Structured Information with Ollama, Zod, and ModelFusion]]></title>
            <link>https://medium.com/codex/effortlessly-generate-structured-information-with-ollama-zod-and-modelfusion-9788471b0b5e?source=rss-310ec22024e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/9788471b0b5e</guid>
            <category><![CDATA[ollama]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Lars Grammel]]></dc:creator>
            <pubDate>Fri, 19 Jan 2024 13:19:16 GMT</pubDate>
            <atom:updated>2024-01-27T08:42:43.194Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O9-m1B5V7w1NmbNIB7dWbA.png" /></figure><p><strong>Have you ever wondered how to turn a simple text prompt into structured, typed information immediately usable in your JavaScript applications?</strong></p><p>In this blog post, we’ll create a JavaScript function that does just that. Our function will take a country as input and output a curated list of cities perfect for a weekend getaway in that country.</p><p>For this task, we’ll use several tools:</p><ul><li><a href="https://ollama.ai/">Ollama</a> is an application for running large language models (LLMs) on your local machine.</li><li><a href="https://huggingface.co/NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO">Nous-Hermes-2 Mixtral 8x7B</a>, a state-of-the-art open-source language model released in January 2024, provides the AI backbone for our task.</li><li><a href="https://github.com/colinhacks/zod">Zod</a>, a type validation library, ensures the structured data we generate is correctly formatted and typed.</li><li><a href="https://modelfusion.dev/">ModelFusion</a> is an open-source library I’m developing to integrate AI models seamlessly into TypeScript projects. It provides an <a href="https://modelfusion.dev/integration/model-provider/ollama">Ollama client</a> and a <a href="https://modelfusion.dev/guide/function/generate-structure">generateStructure function</a>.</li></ul><p>By the end of this post, you’ll see how these technologies work together to create a powerful solution for generating typed, structured data on your local machine. The example code is available in the <a href="https://github.com/lgrammel/modelfusion/blob/main/examples/basic/src/use-cases/generate-structured-information-city-destinations-ollama.ts">ModelFusion repository</a>.</p><h3>The Power and Limits of Generating Structured Information</h3><p>Imagine a large language model (LLM) as a librarian who has read the entire internet. Trained on a massive amount of text, these models encode information based on how often it appears in their training data. The more frequently something is mentioned, the better the model remembers it, just like a librarian with popular books.</p><p>These LLMs are fine-tuned to follow specific instructions, much like asking a librarian for a book recommendation. A general query, such as “What do I buy at the store?” might yield a broad list. In contrast, a more specific question like “Which apples do I buy at the store?” prompts a more targeted response. This precision in asking questions shapes the information the model provides.</p><p>LLMs can create structured output, e.g., matching a JSON schema, especially with engines like Ollama that ensure well-formed JSON output. This enables us to use the output directly in our applications.</p><p>However, it’s essential to understand LLMs’ limitations. When asked about less known or absent information, like the birthday of a personal relative, LLMs might ‘hallucinate’ or make up information. This is similar to a librarian guessing an answer when unsure. It’s essential to consider how well-represented the information you need is in the model.</p><h3>Installing Ollama and Nous-Hermes-2 Mixtral 8x7B</h3><p><a href="https://ollama.ai/">Ollama</a> is an application for running LLMs on your machine. You can use it to run LLMs locally without needing a cloud connection. To install Ollama, follow the instructions on the <a href="https://ollama.ai/">Ollama website</a>. Once Ollama is set up, you can download the model we will use for this blog post.</p><p>The <a href="https://ollama.ai/library/nous-hermes2-mixtral">Nous-Hermes-2 Mixtral 8x7B model</a> is a state-of-the-art LLM released in January 2024. It is a fine-tune of the <a href="https://mistral.ai/news/mixtral-of-experts/">Mixtral mixture of experts model</a>. Ollama provides the 4-bit quantized version of Nous-Hermes-2 Mixtral 8x7B, which is 26 GB big and requires at least 32 GB of RAM to run. Alternatively, you can use a smaller model, e.g., <a href="https://ollama.ai/library/openhermes">OpenHermes</a>, a fine-tuned Mistral 7B, which can run on machines with 16 GB of RAM.</p><p>You can download the model with the following command: ollama pull nous-hermes2-mixtral (or ollama pull openhermes2.5-mistral for the smaller model).</p><p>With Ollama and the Nous-Hermes-2 Mixtral 8x7B model installed, we can look into setting up our project.</p><h3>Setting up the Project</h3><p>To set up a Node.js repository with TypeScript, Zod, and ModelFusion, you can use the <a href="https://github.com/lgrammel/modelfusion-terminal-app-starter">ModelFusion Terminal App Starter</a>.</p><ol><li>Clone the starter repository:</li></ol><pre>git clone https://github.com/lgrammel/modelfusion-terminal-app-starter.git ollama-modelfusion-structured-information</pre><ol><li>Move into the starter directory:</li></ol><pre>cd ollama-modelfusion-structured-information</pre><ol><li>Install dependencies and add zod:</li></ol><pre>npm install zod</pre><p>The starter example uses OpenAI by default for simple text generation. We will replace src/main.ts with our own code. Please open src/main.ts in your favorite editor and delete the contents.</p><h3>Generating Structured Information</h3><p>Now, we can start writing our code. First, we need to add the imports:</p><pre>import {<br>  generateStructure,<br>  jsonStructurePrompt,<br>  ollama,<br>  zodSchema,<br>} from &quot;modelfusion&quot;;<br>import { z } from &quot;zod&quot;;</pre><p>Then, we add a listCityDestinations function that takes a country as input and returns a list of cities suitable for a weekend trip in that country.</p><pre>const listCityDestinations = (country: string) =&gt;<br>  generateStructure({<br>    model: // ...<br>    schema: // ...<br>    prompt: // ...<br>  });</pre><p>Let’s look at this code in more detail. We use the generateStructure function from ModelFusion to generate the list of cities. It has three main parts: the model, the schema, and the prompt.</p><h4>Defining the Model</h4><pre>generateStructure({<br>  model: ollama<br>    .ChatTextGenerator({<br>      model: &quot;nous-hermes2-mixtral&quot;,<br>      temperature: 0,<br>    })<br>    .asStructureGenerationModel(jsonStructurePrompt.text()),<br>  //...<br>});</pre><p>The model property specifies the LLM and the provider we want to use. We use the Ollama provider with their chat API by calling ollama.ChatTextGenerator, and specify the model that we want to use. We use the <a href="https://huggingface.co/NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO">Nous-Hermes-2 Mixtral 8x7B DPO model</a> by specifying model: &quot;nous-hermes2-mixtral&quot; (or openhermes2.5-mistral for the smaller model). We also set the temperature to 0 to reduce the randomness in the output.</p><p>The asStructureGenerationModel function adapts the text generator to a structure generator. jsonStructurePrompt is a text-to-JSON mapping for structure generation. It automatically enables JSON mode on Ollama API calls, injects the JSON schema into the prompt, and parses the JSON output.</p><p>jsonStructurePrompt.text() supports simple <a href="https://modelfusion.dev/guide/function/generate-text#text-prompt">text prompts</a>. You could also use jsonStructurePrompt.instruction() for more complex <a href="https://modelfusion.dev/guide/function/generate-text#instruction-prompt">instruction prompts</a>.</p><h4>Specifying the Schema</h4><pre>generateStructure({<br>  // ...<br>  schema: zodSchema(<br>    z.object({<br>      destinations: z.array(<br>        z.object({<br>          city: z.string(),<br>          region: z.string(),<br>          description: z.string(),<br>        })<br>      ),<br>    })<br>  ),<br>  // ...<br>});</pre><p>The schema property contains a <a href="https://github.com/colinhacks/zod">Zod</a>-schema that specifies the structure of the output. We use Zod to define a schema that has a destinationsarray. Each destination has a city, region, and description field, all strings.</p><p>The ModelFusion zodSchema function wraps the Zod object definition. This is necessary because ModelFusion supports Zod and unchecked JSON schemas and is adaptable to other schema validation libraries such as <a href="https://valibot.dev/">Valibot</a>.</p><p>The schema is converted to a JSON schema and injected into the prompt for the language model. It is also used to validate and type-check the parsed JSON output from the LLM.</p><h4>Defining the Prompt</h4><pre>generateStructure({<br>  // ...<br>  prompt:<br>    `List 5 city destinations in ${country} for weekend trips. ` +<br>    `Describe in 1 sentence what is unique and interesting about each destination.`,<br>});</pre><p>The prompt definition is quite simple. We use a template string to define the prompt. The ${country} part is replaced with the country that we pass into the function. The prompt is a simple string since we use jsonStructurePrompt.text().</p><h4>Calling the Function</h4><pre>async function main() {<br>  console.log(await listCityDestinations(&quot;France&quot;));<br>}<br><br>main().catch(console.error);</pre><p>Finally, we call the function with a country name. The function returns a promise, so we use await to wait for the result. We then print the result to the console.</p><h4>Putting it all Together</h4><p>This is the complete code for src/main.ts:</p><pre>import {<br>  generateStructure,<br>  jsonStructurePrompt,<br>  ollama,<br>  zodSchema,<br>} from &quot;modelfusion&quot;;<br>import { z } from &quot;zod&quot;;<br><br>const listCityDestinations = (country: string) =&gt;<br>  generateStructure({<br>    model: ollama<br>      .ChatTextGenerator({<br>        model: &quot;nous-hermes2-mixtral&quot;,<br>        temperature: 0,<br>      })<br>      .asStructureGenerationModel(jsonStructurePrompt.text()),<br>    schema: zodSchema(<br>      z.object({<br>        destinations: z.array(<br>          z.object({<br>            city: z.string(),<br>            region: z.string(),<br>            description: z.string(),<br>          })<br>        ),<br>      })<br>    ),<br>    prompt:<br>      `List 5 city destinations in ${country} for weekend trips. ` +<br>      `Describe in 1 sentence what is unique and interesting about each destination.`,<br>  });<br><br>async function main() {<br>  console.log(await listCityDestinations(&quot;France&quot;));<br>}<br><br>main().catch(console.error);</pre><p>You can run it with the following command:</p><pre>npm start</pre><p>It will take a while to run (expect 20–30 seconds, but it varies by machine) since the model needs to generate many tokens.</p><p>Here is some example output:</p><pre>❯ npm start<br><br>&gt; modelfusion-terminal-app-starter@1.0.0 start<br>&gt; npx tsx src/main.ts<br><br>{<br>  destinations: [<br>    {<br>      city: &#39;Paris&#39;,<br>      region: &#39;Île-de-France&#39;,<br>      description: &#39;The City of Lights, Paris offers iconic landmarks like the Eiffel Tower and Louvre Museum.&#39;<br>    },<br>    {<br>      city: &#39;Lyon&#39;,<br>      region: &#39;Auvergne-Rhône-Alpes&#39;,<br>      description: &#39;Known for its gastronomy, Lyon is a UNESCO World Heritage site with Roman ruins and historic architecture.&#39;<br>    },<br>    {<br>      city: &#39;Bordeaux&#39;,<br>      region: &#39;Nouvelle-Aquitaine&#39;,<br>      description: &#39;Famous for its wine production, Bordeaux boasts beautiful 18th-century architecture and a vibrant cultural scene.&#39;<br>    },<br>    {<br>      city: &#39;Marseille&#39;,<br>      region: &quot;Provence-Alpes-Côte d&#39;Azur&quot;,<br>      description: &#39;A bustling port city, Marseille offers stunning coastal views, historic sites like the Basilique Notre-Dame de la Garde, and a rich cultural heritage.&#39;<br>    },<br>    {<br>      city: &#39;Nice&#39;,<br>      region: &quot;Provence-Alpes-Côte d&#39;Azur&quot;,<br>      description: &#39;Known for its picturesque seaside promenade, Nice offers beautiful beaches, vibrant markets, and a rich artistic history.&#39;<br>    }<br>  ]<br>}</pre><h3>Conclusion</h3><p>In this post, we’ve explored how to use LLMs to generate structured information for JavaScript applications with Ollama, Zod, and ModelFusion. This approach illustrates one way to integrate AI into your projects.</p><p>You can experiment and adapt this example to your requirements. The possibilities are vast, and by tweaking the model, schema, and prompts, you can tailor the output to suit a wide range of applications. The example code is available in the <a href="https://github.com/lgrammel/modelfusion/blob/main/examples/basic/src/use-cases/generate-structured-information-city-destinations-ollama.ts">ModelFusion repository</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9788471b0b5e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/effortlessly-generate-structured-information-with-ollama-zod-and-modelfusion-9788471b0b5e">Effortlessly Generate Structured Information with Ollama, Zod, and ModelFusion</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create Your Own Local Chatbot with Next.js, Llama.cpp, and ModelFusion]]></title>
            <link>https://javascript.plainenglish.io/create-your-own-local-chatbot-with-next-js-llama-cpp-and-modelfusion-11ed4ea494c4?source=rss-310ec22024e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/11ed4ea494c4</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[llama-cpp]]></category>
            <category><![CDATA[nextjs]]></category>
            <dc:creator><![CDATA[Lars Grammel]]></dc:creator>
            <pubDate>Sat, 13 Jan 2024 13:48:59 GMT</pubDate>
            <atom:updated>2024-01-16T04:20:29.455Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*2U-FLHLDcKfc0RuHd7LjKQ.png" /></figure><p>In this blog post, we’ll build a <a href="https://nextjs.org/">Next.js</a> chatbot that runs on your computer. We’ll use <a href="https://github.com/ggerganov/llama.cpp">Llama.cpp</a> to serve the <a href="https://huggingface.co/teknium/OpenHermes-2.5-Mistral-7B">OpenHermes 2.5 Mistral</a> LLM (large language model) locally, the <a href="https://github.com/vercel/ai">Vercel AI SDK</a> to handle stream forwarding and rendering, and <a href="https://modelfusion.ai/">ModelFusion</a> to integrate Llama.cpp with the Vercel AI SDK. The chatbot will be able to generate responses to user messages in real-time.</p><p>The architecture looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*czeDTg5DxeLlH_15.png" /></figure><p>You can find a full Next.js, Vercel AI SDK, Llama.cpp &amp; ModelFusion starter with more examples here: <a href="https://github.com/lgrammel/modelfusion-llamacpp-nextjs-starter">github/com/lgrammel/modelfusion-Llamacpp-nextjs-starter</a></p><p>This blog post explains step by step how to build the chatbot. Let’s get started!</p><h3>Setup Llama.cpp</h3><p>The first step to getting started with our local chatbot is to setup <a href="https://github.com/ggerganov/llama.cpp">Llama.cpp</a>.</p><p>Llama.cpp is an LLM (large language model) inference engine implemented in C++ that allows us to run LLMs like <a href="https://huggingface.co/teknium/OpenHermes-2.5-Mistral-7B">OpenHermes 2.5 Mistral</a> on your machine. This is crucial for our chatbot as it forms the backbone of its AI capabilities.</p><h3>Step 1: Build Llama.cpp</h3><p>Llama.cpp requires you to clone the repository and build it on your machine. Please follow the instructions on the <a href="https://github.com/ggerganov/llama.cpp?tab=readme-ov-file#usage">Llama.cpp README</a>:</p><ol><li>Open your terminal or command prompt.</li><li>Clone the repository:</li></ol><pre>git clone https://github.com/ggerganov/llama.cpp<br>cd llama.cpp</pre><ol><li>Compile llama.cpp:</li><li>Linux/Mac: Run make</li><li>Windows or other setups: Please follow the instructions on the <a href="https://github.com/ggerganov/llama.cpp?tab=readme-ov-file#build">Llama.cpp README</a>.</li></ol><h3>Step 2: Downloading OpenHermes 2.5 Mistral GGUF</h3><p>Once Llama.cpp is ready, you’ll need to pull the specific LLM we will be using for this project, <a href="https://huggingface.co/TheBloke/OpenHermes-2.5-Mistral-7B-GGUF">OpenHermes 2.5 Mistral</a>.</p><ol><li><a href="https://huggingface.co/TheBloke/OpenHermes-2.5-Mistral-7B-GGUF/tree/main">Download the OpenHermes 2.5 Mistral model from HuggingFace</a>. I’ll use openhermes-2.5-mistral-7b.Q4_K_M.gguf in this tutorial.</li><li>Move the model file into the models/ directory of your local Llama.cpp repository.</li></ol><p>Llama.cpp runs LLMs in a format called GGUF (GPT-Generated Unified Format). You can find many <a href="https://huggingface.co/models?sort=trending&amp;search=gguf">GGUF models on HuggingFace</a>. 4-bit quantized models that fit in your machine&#39;s memory, e.g. 7B param models on a 8GB or 16GB machine, are usually the best models to run.</p><blockquote><em>Quantization involves reducing the precision of the numerical values representing the model’s weights, often from 32-bit floating points to lower precision formats like 4-bit. &gt; This decreases the model’s memory footprint and computational requirements.</em></blockquote><h3>Step 3: Start the Llama.cpp Server</h3><p>You can now start the Llama.cpp server by running the following command in your terminal (Mac/Linux):</p><pre>./server -m models/openhermes-2.5-mistral-7b.Q4_K_M.gguf</pre><p>After completing these steps, your system is running a Llama.cpp server with the OpenHermes 2.5 Mistral model, ready to be integrated into our Next.js chatbot.</p><h3>Creating the Next.js Project</h3><p>The next step is to create the foundational structure of our chatbot using <a href="https://nextjs.org/">Next.js</a>. Next.js will be used to build our chatbot application’s frontend and API routes.</p><p>Here are the steps to create the Next.js project:</p><ol><li>Execute the following command in your terminal to create a new Next.js project:</li></ol><pre>npx create-next-app@latest llamacpp-nextjs-chatbot</pre><ol><li>You will be prompted to configure various aspects of your Next.js application. Here are the settings for our chatbot project:</li></ol><pre>Would you like to use TypeScript? Yes<br>   Would you like to use ESLint? Yes<br>   Would you like to use Tailwind CSS? Yes<br>   Would you like to use `src/` directory? Yes<br>   Would you like to use App Router? (recommended) Yes<br>   Would you like to customize the default import alias? No</pre><p>These settings enable TypeScript for robust type-checking, ESLint for code quality, and Tailwind CSS for styling. Using the src/ directory and App Router enhances the project structure and routing capabilities.</p><ol><li>Once the project is initialized, navigate to the project directory:</li></ol><pre>cd llamacpp-nextjs-chatbot</pre><p>By following these steps, you have successfully created and configured your Next.js project. This forms the base of our chatbot application, where we will later integrate the AI functionalities using Llama.cpp and ModelFusion. The next part of the tutorial will guide you through installing additional libraries and setting up the backend logic for the chatbot.</p><blockquote><em>You can verify your setup by running </em><em>npm run dev in your terminal and navigating to </em><a href="http://localhost:3000/"><em>http://localhost:3000</em></a><em> in your browser. You should see the default Next.js page</em></blockquote><h3>Installing the Required Libraries</h3><p>We will use several libraries to build our chatbot. Here is an overview of the libraries we will use:</p><ul><li><strong>Vercel AI SDK</strong>: The <a href="https://github.com/vercel/ai">Vercel AI SDK</a> provides React hooks for creating chats (useChat) as well as streams that forward AI responses to the frontend (StreamingTextResponse).</li><li><strong>ModelFusion</strong>: <a href="https://github.com/lgrammel/modelfusion">ModelFusion</a> is a library for building multi-modal AI applications that I’ve been working on. It provides a streamText function that calls AI models and returns a streaming response. ModelFusion also contains a <a href="https://modelfusion.dev/integration/model-provider/llamacpp">Llama.cpp integration</a> that we will use to access the OpenHermes 2.5 Mistral model.</li><li><strong>ModelFusion Vercel AI SDK Integration</strong>: The <a href="https://github.com/lgrammel/modelfusion/tree/main/packages/%40modelfusion-vercel-ai">@modelfusion/vercel-ai</a> integration provides a ModelFusionTextStream that adapts ModelFusion&#39;s text streaming to the Vercel AI SDK&#39;s streaming response.</li></ul><p>You can run the following command in the chatbot project directory to install all libraries:</p><pre>npm install --save ai modelfusion @modelfusion/vercel-ai</pre><p>You have now installed all the libraries required for building the chatbot. The next section of the tutorial will guide you through creating an API route for handling chat interactions.</p><h3>Creating an API Route for the Chatbot</h3><p>Creating the API route for the <a href="https://nextjs.org/docs/app">Next.js app router</a> is the next step in building our chatbot. The API route will handle the chat interactions between the user and the AI.</p><p>Navigate to the src/app/api/chat/ directory in your project and create a new file named route.ts to serve as our API route file.</p><p>The API route requires several important imports from the ai, modelfusion, and @modelfusion/vercel-ai libraries. These imports bring in necessary classes and functions for streaming AI responses and processing chat messages.</p><pre>import { ModelFusionTextStream, asChatMessages } from &quot;@modelfusion/vercel-ai&quot;;<br>import { Message, StreamingTextResponse } from &quot;ai&quot;;<br>import { llamacpp, streamText } from &quot;modelfusion&quot;;</pre><p>We will use the <a href="https://edge-runtime.vercel.app/">edge runtime</a>:</p><pre>export const runtime = &quot;edge&quot;;</pre><p>The route itself is a POST request that takes a list of messages as input:</p><pre>export async function POST(req: Request) {<br>  // useChat will send a JSON with a messages property:<br>  const { messages }: { messages: Message[] } = await req.json();<br>  // ...<br>}t</pre><p>We initialize a ModelFusion text generation model for calling the Llama.cpp chat API with the OpenHermes 2.5 Mistral model. The .withChatPrompt() method creates an adapted model for chat prompts:</p><pre>const model = llamacpp<br>  .CompletionTextGenerator({<br>    promptTemplate: llamacpp.prompt.ChatML, // OpenHermes uses the ChatML prompt format<br>    temperature: 0,<br>    cachePrompt: true, // Cache previous processing for fast responses<br>    maxGenerationTokens: 1024, // Room for answer<br>  })<br>  .withChatPrompt();</pre><p>Next, we create a <a href="https://modelfusion.dev/guide/function/generate-text#chat-prompts">ModelFusion chat prompt</a> from the AI SDK messages:</p><pre>const prompt = {<br>  system: &quot;You are an AI chatbot. Follow the user&#39;s instructions carefully.&quot;,<br>  // map Vercel AI SDK Message to ModelFusion ChatMessage:<br>  messages: asChatMessages(messages),<br>};</pre><p>The asChatMessages helper converts the messages from the Vercel AI SDK to ModelFusion chat messages.</p><p>With the prompt and the model, you can then use ModelFusion to call Llama.cpp and generate a streaming response:</p><pre>const textStream = await streamText({ model, prompt });</pre><p>Finally you can return the streaming text response with the Vercel AI SDK. The ModelFusionTextStream adapts ModelFusion&#39;s streaming response to the Vercel AI SDK&#39;s streaming response:</p><pre>// Return the result using the Vercel AI SDK:<br>return new StreamingTextResponse(ModelFusionTextStream(textStream));</pre><h3>Adding the Chat Interface</h3><p>We need to create a dedicated chat page to bring our chatbot to life on the frontend. This page will be located at src/app/page.tsx and will leverage the useChat hook from the Vercel AI SDK. The useChat hook calls the /api/chat route and processes the streaming response as an array of messages, rendering each token as it arrives.</p><pre>// src/app/page.tsx<br>&quot;use client&quot;;<br>import { useChat } from &quot;ai/react&quot;;<br><br>export default function Chat() {<br>  const { messages, input, handleInputChange, handleSubmit } = useChat();<br>  return (<br>    &lt;div className=&quot;flex flex-col w-full max-w-md py-24 mx-auto stretch&quot;&gt;<br>      {messages.map((message) =&gt; (<br>        &lt;div<br>          key={message.id}<br>          className=&quot;whitespace-pre-wrap&quot;<br>          style={{ color: message.role === &quot;user&quot; ? &quot;black&quot; : &quot;green&quot; }}<br>        &gt;<br>          &lt;strong&gt;{`${message.role}: `}&lt;/strong&gt;<br>          {message.content}<br>          &lt;br /&gt;<br>          &lt;br /&gt;<br>        &lt;/div&gt;<br>      ))}<br>      &lt;form onSubmit={handleSubmit}&gt;<br>        &lt;input<br>          className=&quot;fixed bottom-0 w-full max-w-md p-2 mb-8 border border-gray-300 rounded shadow-xl&quot;<br>          value={input}<br>          placeholder=&quot;Say something...&quot;<br>          onChange={handleInputChange}<br>        /&gt;<br>      &lt;/form&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><p>It’s important to clean up the global styles for a more visually appealing chat interface. By default, the Next.js page is dark. We clean up src/app/globals.css to make it readable:</p><pre>@tailwind base;<br>@tailwind components;<br>@tailwind utilities;</pre><h3>Running the Chatbot Application</h3><p>With the chat page in place, it’s time to run our chatbot app and see the result of our hard work.</p><p>You can launch the development server by running the following command in your terminal:</p><pre>npm run dev</pre><p>You can now navigate to <a href="http://localhost:3000/">http://localhost:3000</a> in your browser to see the chat page. You can interact with the chatbot by typing messages into the input field. The chatbot will respond to your messages in real-time.</p><p>Below is a screenshot of what you can expect your chatbot interface to look like when you run the application:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*IyAI112USrd0DV8l.png" /></figure><h3>Conclusion</h3><p>And there you have it — a fully functional local chatbot built with Next.js, Llama.cpp, and ModelFusion at your fingertips. We’ve traversed the path from setting up our development environment, integrating a robust language model, and spinning up a user-friendly chat interface.</p><p>The code is intended as a starting point for your projects. Have fun exploring!</p><h3>In Plain English 🚀</h3><p><em>Thank you for being a part of the </em><a href="https://plainenglish.io/"><strong><em>In Plain English</em></strong></a><em> community! Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://twitter.com/inPlainEngHQ"><strong>X</strong></a><strong> | </strong><a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a><strong> | </strong><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>YouTube</strong></a><strong> | </strong><a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a><strong> | </strong><a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a></li><li>Visit our other platforms: <a href="https://stackademic.com/"><strong>Stackademic</strong></a><strong> | </strong><a href="https://cofeed.app/"><strong>CoFeed</strong></a><strong> | </strong><a href="https://venturemagazine.net/"><strong>Venture</strong></a></li><li>More content at <a href="https://plainenglish.io/"><strong>PlainEnglish.io</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=11ed4ea494c4" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/create-your-own-local-chatbot-with-next-js-llama-cpp-and-modelfusion-11ed4ea494c4">Create Your Own Local Chatbot with Next.js, Llama.cpp, and ModelFusion</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create Your Own Local Chatbot with Next.js, Ollama, and ModelFusion]]></title>
            <link>https://javascript.plainenglish.io/create-your-own-local-chatbot-with-next-js-ollama-and-modelfusion-dcc0bd63810f?source=rss-310ec22024e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/dcc0bd63810f</guid>
            <category><![CDATA[ollama]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Lars Grammel]]></dc:creator>
            <pubDate>Sat, 09 Dec 2023 17:57:24 GMT</pubDate>
            <atom:updated>2024-01-13T10:08:40.623Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*2U-FLHLDcKfc0RuHd7LjKQ.png" /></figure><p>In this blog post, we’ll build a <a href="https://nextjs.org/">Next.js</a> chatbot that runs on your computer. We’ll use <a href="https://ollama.ai/">Ollama</a> to serve the <a href="https://ollama.ai/library/openhermes2.5-mistral">OpenHermes 2.5 Mistral</a> LLM (large language model) locally, the <a href="https://github.com/vercel/ai">Vercel AI SDK</a> to handle stream forwarding and rendering, and <a href="https://modelfusion.ai/">ModelFusion</a> to integrate Ollama with the Vercel AI SDK. The chatbot will be able to generate responses to user messages in real-time.</p><p>The architecture looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*q97pa9ZwWgZH5vP_.png" /><figcaption>chatbot architecture</figcaption></figure><p>You can find a full Next.js, Vercel AI SDK, Ollama &amp; ModelFusion starter with more examples here: <a href="https://github.com/lgrammel/modelfusion-ollama-nextjs-starter">github/com/lgrammel/modelfusion-ollama-nextjs-starter</a></p><p>This blog post explains step by step how to build the chatbot. Let’s get started!</p><h3>Installing Ollama</h3><p>The first step to getting started with our local chatbot is installing <a href="https://ollama.ai/">Ollama</a>. Ollama is a versatile platform that allows us to run LLMs like <a href="https://ollama.ai/library/openhermes2.5-mistral">OpenHermes 2.5 Mistral</a> on your machine. This is crucial for our chatbot as it forms the backbone of its AI capabilities.</p><h3>Step 1: Download Ollama</h3><ol><li>Visit the official <a href="https://ollama.ai/">Ollama website</a>.</li><li>Follow the instructions provided on the site to download and install Ollama on your machine.</li></ol><h3>Step 2: Pulling OpenHermes 2.5 Mistral</h3><p>Once Ollama is installed, you’ll need to pull the specific LLM we will be using for this project, <a href="https://ollama.ai/library/openhermes2.5-mistral">OpenHermes 2.5 Mistral</a>. As of November 2023, it is one of the best open-source LLMs in the 7B parameter class. You need at least a MacBook M1 with 8GB of RAM or a similarly compatible computer to run it.</p><ol><li>Open your terminal or command prompt.</li><li>Run the following command:</li></ol><pre>ollama pull openhermes2.5-mistral</pre><p>This command will download the LLM and store it on your machine. You can now use it to generate text.</p><blockquote><em>💡 Tip<br>You can find the best-performing open-source LLMs on the </em><a href="https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard"><em>HuggingFace Open LLM Leaderboard</em></a><em>. They are ranked using a mix of benchmarks and grouped into different parameter classes so you can choose the best LLM for your machine. Many of the LLMs on the leaderboard are available on Ollama.</em></blockquote><p>After completing these steps, your system is equipped with Ollama and the OpenHermes 2.5 Mistral model, ready to be integrated into our Next.js chatbot.</p><h3>Creating the Next.js Project</h3><p>The next step is to create the foundational structure of our chatbot using <a href="https://nextjs.org/">Next.js</a>. Next.js will be used to build our chatbot application’s frontend and API routes.</p><p>Here are the steps to create the Next.js project:</p><ol><li>Execute the following command in your terminal to create a new Next.js project:</li></ol><pre>npx create-next-app@latest ollama-nextjs-chatbot</pre><ol><li>You will be prompted to configure various aspects of your Next.js application. Here are the settings for our chatbot project:</li></ol><pre>Would you like to use TypeScript? Yes<br>Would you like to use ESLint? Yes<br>Would you like to use Tailwind CSS? Yes<br>Would you like to use `src/` directory? Yes<br>Would you like to use App Router? (recommended) Yes<br>Would you like to customize the default import alias? No</pre><p>These settings enable TypeScript for robust type-checking, ESLint for code quality, and Tailwind CSS for styling. Using the src/ directory and App Router enhances the project structure and routing capabilities.</p><ol><li>Once the project is initialized, navigate to the project directory:</li></ol><pre>cd ollama-nextjs-chatbot</pre><p>By following these steps, you have successfully created and configured your Next.js project. This forms the base of our chatbot application, where we will later integrate the AI functionalities using Ollama and ModelFusion. The next part of the tutorial will guide you through installing additional libraries and setting up the backend logic for the chatbot.</p><blockquote><em>💡 Tip<br>You can verify your setup by running </em><em>npm run dev in your terminal and navigating to </em><a href="http://localhost:3000/"><em>http://localhost:3000</em></a><em> in your browser. You should see the default Next.js page.</em></blockquote><h3>Installing the Required Libraries</h3><p>We will use several libraries to build our chatbot. Here is an overview of the libraries we will use:</p><ul><li><strong>Vercel AI SDK</strong>: The <a href="https://github.com/vercel/ai">Vercel AI SDK</a> provides React hooks for creating chats (useChat) as well as streams that forward AI responses to the frontend (StreamingTextResponse).</li><li><strong>ModelFusion</strong>: <a href="https://github.com/lgrammel/modelfusion">ModelFusion</a> is a library for building multi-modal AI applications that I’ve been working on. It provides a streamText function that calls AI models and returns a streaming response. ModelFusion also contains an Ollama integration that we will use to access the OpenHermes 2.5 Mistral model.</li><li><strong>ModelFusion Vercel AI SDK Integration</strong>: The <a href="https://github.com/lgrammel/modelfusion/tree/main/extensions/vercel-ai">@modelfusion/vercel-ai</a> integration provides a ModelFusionTextStream that adapts ModelFusion&#39;s text streaming to the Vercel AI SDK&#39;s streaming response.</li></ul><p>You can run the following command in the chatbot project directory to install all libraries:</p><pre>npm install --save ai modelfusion @modelfusion/vercel-ai</pre><p>You have now installed all the libraries required for building the chatbot. The next section of the tutorial will guide you through creating an API route for handling chat interactions.</p><h3>Creating an API Route for the Chatbot</h3><p>Creating the API route for the <a href="https://nextjs.org/docs/app">Next.js app router</a> is the next step in building our chatbot. The API route will handle the chat interactions between the user and the AI.</p><p>Navigate to the src/app/api/chat/ directory in your project and create a new file named route.ts to serve as our API route file.</p><p>The API route requires several important imports from the ai, modelfusion, and @modelfusion/vercel-ai libraries. These imports bring in necessary classes and functions for streaming AI responses and processing chat messages.</p><pre>import { ModelFusionTextStream } from &quot;@modelfusion/vercel-ai&quot;;<br>import { Message, StreamingTextResponse } from &quot;ai&quot;;<br>import {<br>  ChatMLPrompt,<br>  TextChatMessage,<br>  ollama,<br>  streamText,<br>} from &quot;modelfusion&quot;</pre><p>We will use the <a href="https://edge-runtime.vercel.app/">edge runtime</a>:</p><pre>export const runtime = &quot;edge&quot;;</pre><p>The route itself is a POST request that takes a list of messages as input:</p><pre>export async function POST(req: Request) {<br>  // useChat will send a JSON with a messages property:<br>  const { messages }: { messages: Message[] } = await req.json();<br>  // ...<br>}</pre><p>We initialize a ModelFusion text generation model for calling Ollama and using the OpenHermes 2.5 Mistral model. We set the maxGenerationTokens to -1 to allow for infinite generation, and the temperature to 0 to disable randomness. The raw option is set to true, because we apply the ChatML prompt format (for the OpenHermes model) to the messages using ModelFusion:</p><pre>const model = ollama<br>  .CompletionTextGenerator({<br>    model: &quot;openhermes2.5-mistral&quot;,<br>    maxGenerationTokens: -1, // infinite generation<br>    temperature: 0,<br>    raw: true, // use raw inputs and map to prompt format below<br>  })<br>  .withPromptTemplate(ChatMLPrompt.chat());</pre><blockquote><em>💡 Tip<br>Different AI models use other prompt formats. The prompt format for a given model is often found in the model card. Using an incorrect prompt format can lead to unexpected results because models are specifically trained to work with a specific prompt format.</em></blockquote><p>Next, we create a <a href="https://modelfusion.dev/guide/function/generate-text#chat-prompts">ModelFusion chat prompt</a> from the AI SDK messages:</p><pre>const prompt = {<br>  system: <br>    &quot;You are an AI chatbot. &quot; +<br>    &quot;Follow the user&#39;s instructions carefully.&quot;,<br>  // map Vercel AI SDK Message to ModelFusion TextChatMessage:<br>  messages: messages.filter(<br>    // only user and assistant roles are supported:<br>    (message) =&gt; <br>      message.role === &quot;user&quot; || <br>      message.role === &quot;assistant&quot;<br>  ) as TextChatMessage[],<br>};</pre><p>With the prompt and the model, you can then use ModelFusion to call Ollama and generate a streaming response:</p><pre>const textStream = await streamText({ model, prompt });</pre><p>Finally you can return the streaming text response with the Vercel AI SDK. The ModelFusionTextStream adapts ModelFusion&#39;s streaming response to the Vercel AI SDK&#39;s streaming response:</p><pre>// Return the result using the Vercel AI SDK:<br>return new StreamingTextResponse(<br>  ModelFusionTextStream(textStream)<br>);</pre><h3>Adding the Chat Interface</h3><p>We need to create a dedicated chat page to bring our chatbot to life on the frontend. This page will be located at src/app/page.tsx and will leverage the useChat hook from the Vercel AI SDK. The useChat hook calls the /api/chat route and processes the streaming response as an array of messages, rendering each token as it arrives.</p><pre>// src/app/page.tsx<br>&quot;use client&quot;;import { useChat } from &quot;ai/react&quot;;<br><br>export default function Chat() {<br>  const { messages, input, handleInputChange, handleSubmit } = useChat();<br>  return (<br>    &lt;div className=&quot;flex flex-col w-full max-w-md py-24 mx-auto stretch&quot;&gt;<br>      {messages.map((message) =&gt; (<br>        &lt;div<br>          key={message.id}<br>          className=&quot;whitespace-pre-wrap&quot;<br>          style={{ color: message.role === &quot;user&quot; ? &quot;black&quot; : &quot;green&quot; }}<br>        &gt;<br>          &lt;strong&gt;{`${message.role}: `}&lt;/strong&gt;<br>          {message.content}<br>          &lt;br /&gt;<br>          &lt;br /&gt;<br>        &lt;/div&gt;<br>      ))}<br>      &lt;form onSubmit={handleSubmit}&gt;<br>        &lt;input<br>          className=&quot;fixed bottom-0 w-full max-w-md p-2 mb-8 border border-gray-300 rounded shadow-xl&quot;<br>          value={input}<br>          placeholder=&quot;Say something...&quot;<br>          onChange={handleInputChange}<br>        /&gt;<br>      &lt;/form&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><p>It’s important to clean up the global styles for a more visually appealing chat interface. By default, the Next.js page is dark. We clean up src/app/globals.css to make it readable:</p><pre>@tailwind base;<br>@tailwind components;<br>@tailwind utilities;</pre><h3>Running the Chatbot Application</h3><p>With the chat page in place, it’s time to run our chatbot app and see the result of our hard work.</p><p>You can launch the development server by running the following command in your terminal:</p><pre>npm run dev</pre><p>You can now navigate to <a href="http://localhost:3000/">http://localhost:3000</a> in your browser to see the chat page. You can interact with the chatbot by typing messages into the input field. The chatbot will respond to your messages in real-time.</p><p>Below is a screenshot of what you can expect your chatbot interface to look like when you run the application:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*LBjgqzmoZmIeo4Gb.png" /><figcaption>example chatbot interaction</figcaption></figure><h3>Conclusion</h3><p>And there you have it — a fully functional local chatbot built with Next.js, Ollama, and ModelFusion at your fingertips. We’ve traversed the path from setting up our development environment, integrating a robust language model, and spinning up a user-friendly chat interface.</p><p>The code is intended as a starting point for your projects. Have fun exploring!</p><p>For the complete source code and further examples to fuel your creativity, visit: <a href="https://github.com/lgrammel/modelfusion-ollama-nextjs-starter">github.com/lgrammel/modelfusion-ollama-nextjs-starter</a></p><h3><a href="https://plainenglish.io/">PlainEnglish.io</a> 🚀</h3><p><em>Thank you for being a part of the In Plain English community! Before you go:</em></p><ul><li><em>Be sure to </em><strong><em>clap</em></strong><em> and </em><strong><em>follow</em></strong><em> the writer</em><strong>️</strong></li><li><em>Learn how you can also </em><a href="https://plainenglish.io/blog/how-to-write-for-in-plain-english"><strong><em>write for In Plain English</em></strong></a><em>️</em></li><li><em>Follow us: </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>X</em></strong></a><strong><em> | </em></strong><a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><strong><em> | </em></strong><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a><strong><em> | </em></strong><a href="https://discord.gg/in-plain-english-709094664682340443"><strong><em>Discord</em></strong></a><strong><em> | </em></strong><a href="https://newsletter.plainenglish.io/"><strong><em>Newsletter</em></strong></a></li><li><em>Visit our other platforms: </em><a href="https://stackademic.com/"><strong><em>Stackademic</em></strong></a><strong><em> | </em></strong><a href="https://cofeed.app/"><strong><em>CoFeed</em></strong></a><strong><em> | </em></strong><a href="https://venturemagazine.net/"><strong><em>Venture</em></strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dcc0bd63810f" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/create-your-own-local-chatbot-with-next-js-ollama-and-modelfusion-dcc0bd63810f">Create Your Own Local Chatbot with Next.js, Ollama, and ModelFusion</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a PDF Chat with Node.js, OpenAI and ModelFusion]]></title>
            <link>https://javascript.plainenglish.io/building-a-pdf-chat-with-node-js-openai-and-modelfusion-9d111aaf4524?source=rss-310ec22024e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d111aaf4524</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Lars Grammel]]></dc:creator>
            <pubDate>Mon, 04 Sep 2023 09:49:13 GMT</pubDate>
            <atom:updated>2024-01-13T10:09:52.094Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*ehieJR1JxK8TbtzV8VS35g.png" /></figure><p>Have you ever wondered how a chatbot that can answer questions about a PDF works?</p><p>In this blog post, we’ll build a console app capable of searching and understanding PDF content to answer questions using Node.js, OpenAI, and ModelFusion. You’ll learn how to read and index PDFs for efficient search and deliver precise responses by retrieving relevant content from the PDFs.</p><p>You can find the complete code for the chatbot here: <a href="https://github.com/lgrammel/modelfusion/blob/main/examples/pdf-chat-terminal/src/main.ts">github/com/lgrammel/modelfusion/examples/pdf-chat-terminal</a></p><p>This blog post explains the essential parts in detail. Let’s get started!</p><h3>Loading Pages from PDFs<a href="https://modelfusion.dev/blog/pdf-chat-nodejs#loading-pages-from-pdfs">​</a></h3><p>We use Mozilla’s <a href="https://mozilla.github.io/pdf.js/">PDF.js</a> via the pdfjs-dist NPM module to load pages from a PDF file. The loadPdfPages function reads the PDF file and extracts its content. It returns an array where each object contains the page number and the text of that page.</p><pre>import fs from &quot;fs/promises&quot;;<br>import * as PdfJs from &quot;pdfjs-dist/legacy/build/pdf&quot;;<br><br>async function loadPdfPages(path: string) {<br>  const pdfData = await fs.readFile(path);<br>  const pdf = await PdfJs.getDocument({<br>    data: new Uint8Array(<br>      pdfData.buffer,<br>      pdfData.byteOffset,<br>      pdfData.byteLength<br>    ),<br>    useSystemFonts: true,<br>  }).promise;<br>  const pageTexts: Array&lt;{<br>    pageNumber: number;<br>    text: string;<br>  }&gt; = [];<br>  for (let i = 0; i &lt; pdf.numPages; i++) {<br>    const page = await pdf.getPage(i + 1);<br>    const pageContent = await page.getTextContent();<br>    pageTexts.push({<br>      pageNumber: i + 1,<br>      text: pageContent.items<br>        .filter((item) =&gt; (item as any).str != null)<br>        .map((item) =&gt; (item as any).str as string)<br>        .join(&quot; &quot;)<br>        .replace(/\s+/g, &quot; &quot;),<br>    });<br>  }<br>  return pageTexts;<br>}</pre><p>Let’s explore the primary tasks: “Load &amp; Parse PDF” and “Extract Page Numbers and Text.”</p><h3>Load &amp; parse the PDF<a href="https://modelfusion.dev/blog/pdf-chat-nodejs#load--parse-the-pdf">​</a></h3><p>Before working with the PDF content, we need to read the file from the disk and parse it into a format our code can understand.</p><pre>const pdfData = await fs.readFile(path);<br><br>const pdf = await PdfJs.getDocument({<br>  data: new Uint8Array(pdfData.buffer, pdfData.byteOffset, pdfData.byteLength),<br>  useSystemFonts: true,<br>}).promise;</pre><p>In this code snippet, the fs.readFile function reads the PDF file from the disk and stores the data in pdfData. We then use the PdfJs.getDocument function to parse this data. The flag useSystemFonts is set to true to avoid issues when system fonts are used in the PDF.</p><h3>Extract page numbers and text<a href="https://modelfusion.dev/blog/pdf-chat-nodejs#extract-page-numbers-and-text">​</a></h3><p>After successfully loading and parsing the PDF, the next step is to extract the text content from each page along with its page number.</p><pre>const pageTexts: Array&lt;{<br>  pageNumber: number;<br>  text: string;<br>}&gt; = [];<br><br>for (let i = 0; i &lt; pdf.numPages; i++) {<br>  const page = await pdf.getPage(i + 1);<br>  const pageContent = await page.getTextContent();<br>  pageTexts.push({<br>    pageNumber: i + 1,<br>    text: pageContent.items<br>      .filter((item) =&gt; (item as any).str != null)<br>      .map((item) =&gt; (item as any).str as string)<br>      .join(&quot; &quot;)<br>      .replace(/\s+/g, &quot; &quot;),<br>}</pre><p>The code defines an array named pageTexts to hold objects that contain the page number and the extracted text from each page. We then loop through each page of the PDF by using pdf.numPages to determine the total number of pages.</p><p>Within the loop, pdf.getPage(i + 1) fetches each page, starting from page number 1. We extract the text content with page.getTextContent().</p><p>Finally, the extracted text from each page is cleaned up by joining all text items and reducing multiple whitespaces to a single space. This cleaned-up text and the page number are stored in pageTexts.</p><h3>Indexing Pages<a href="https://modelfusion.dev/blog/pdf-chat-nodejs#indexing-pages">​</a></h3><p>Now that the PDF pages are available as text, we’ll delve into the mechanism for indexing the PDF text we’ve loaded. Indexing is crucial as it allows for quick and semantic-based retrieval of information later. Here’s how the magic happens:</p><pre>const pages = await loadPdfPages(file);<br><br>const embeddingModel = new OpenAITextEmbeddingModel({<br>  model: &quot;text-embedding-ada-002&quot;,<br>  throttle: throttleMaxConcurrency({ maxConcurrentCalls: 5 }),<br>});<br>const chunks = await splitTextChunks(<br>  splitAtToken({<br>    maxTokensPerChunk: 256,<br>    tokenizer: embeddingModel.tokenizer,<br>  }),<br>  pages<br>);<br>const vectorIndex = new MemoryVectorIndex&lt;{<br>  pageNumber: number;<br>  text: string;<br>}&gt;();<br>await upsertTextChunks({ vectorIndex, embeddingModel, chunks });</pre><p>Let’s look at each step:</p><h3>Initialize the text embedding model<a href="https://modelfusion.dev/blog/pdf-chat-nodejs#initialize-the-text-embedding-model">​</a></h3><p>The first step is to initialize a text embedding model. This model will be responsible for converting our text data into a format that can be compared for similarity.</p><pre>const embeddingModel = new OpenAITextEmbeddingModel({<br>  model: &quot;text-embedding-ada-002&quot;,<br>  throttle: throttleMaxConcurrency({ maxConcurrentCalls: 5 }),<br>});</pre><p>Text embedding models work by converting chunks of text into vectors in a multi-dimensional space such that text with similar meaning will have vectors that are close to each other. These vectors will be stored in a vector index.</p><h3>Tokenization and text chunking<a href="https://modelfusion.dev/blog/pdf-chat-nodejs#tokenization-and-text-chunking">​</a></h3><p>We need to prepare the text data before we convert our text into vectors. This preparation involves splitting the text into smaller pieces, known as “chunks,” that are manageable for the model.</p><pre>const chunks = await splitTextChunks(<br>  splitAtToken({<br>    maxTokensPerChunk: 256,<br>    tokenizer: embeddingModel.tokenizer,<br>  }),<br>  pages<br>);</pre><p>We limit each chunk to 256 tokens and use the tokenizer from our embedding model. The splitTextChunks function recursively splits the text until the chunks fit the specified maximum size.</p><p>You can play with the chunk size and see how it affects the results. When chunks are too small, they might contain only some of the necessary information to answer a question. When chunks are too large, their embedding vector may not be similar enough to the hypothetical answer we generate later.</p><blockquote><em>Token: A token is the smallest unit that a machine-learning model reads. In language models, a token can be as small as a character or as long as a word (e.g., ‘a’, ‘apple’).</em></blockquote><blockquote><em>Tokenizer: A tool that breaks down text into tokens. ModelFusion provides the tokenizer for most text generation and embedding models.</em></blockquote><h3>Creating a memory vector index<a href="https://modelfusion.dev/blog/pdf-chat-nodejs#creating-a-memory-vector-index">​</a></h3><p>The next step is to create an empty memory vector index to store our embedded text vectors.</p><pre>const vectorIndex = new MemoryVectorIndex&lt;{<br>  pageNumber: number;<br>  text: string;<br>}&gt;();</pre><p>A vector store is like a specialized database for vectors. It allows us to perform quick searches to find similar vectors to a given query vector.</p><p>In ModelFusion, a vector index is a searchable interface to access a vector store for a specific table or metadata. In our app, each vector in the index is associated with the page number and the text chunk it originated from.</p><p>The ModelFusion MemoryVectorIndex is a simple in-memory implementation of a vector index that uses cosine similarity to find similar vectors. It&#39;s a good choice for small datasets, such as a single PDF file loaded on-demand.</p><h3>Inserting text chunks into the vector index<a href="https://modelfusion.dev/blog/pdf-chat-nodejs#inserting-text-chunks-into-the-vector-index">​</a></h3><p>Finally, we populate our memory vector index with the text vectors generated from our chunks.</p><pre>await upsertTextChunks({ vectorIndex, embeddingModel, chunks });</pre><p>The function upsertTextChunks performs the following:</p><ul><li>It uses the embeddingModel to convert each text chunk into a vector.</li><li>It then inserts this vector into vectorIndex, along with the metadata (page number and text).</li></ul><p>At this point, our vector index is fully populated and ready for fast, semantic-based searches. This is essential for our chatbot to provide relevant and accurate answers.</p><p>In summary, indexing involves converting text chunks into a vectorized, searchable format. It the stage for semantic-based text retrieval, enabling our chatbot to understand and respond in a context-aware manner.</p><h3>The Chat Loop<a href="https://modelfusion.dev/blog/pdf-chat-nodejs#the-chat-loop">​</a></h3><p>The chat loop is the central part of our “Chat with PDF” application. It continuously awaits user questions, generates hypothetical answers, searches for similar text chunks from a pre-processed PDF, and responds to the user.</p><pre>const chat = readline.createInterface({<br>  input: process.stdin,<br>  output: process.stdout,<br>});<br><br>while (true) {<br>  const question = await chat.question(&quot;You: &quot;);<br><br>  const hypotheticalAnswer = await generateText({<br>    model: openai.ChatTextGenerator({ model: &quot;gpt-3.5-turbo&quot;, temperature: 0 }),<br>    prompt: [<br>      openai.ChatMessage.system(`Answer the user&#39;s question.`),<br>      openai.ChatMessage.user(question),<br>    ],<br>  });<br><br>  const information = await retrieve(<br>    new VectorIndexRetriever({<br>      vectorIndex,<br>      embeddingModel,<br>      maxResults: 5,<br>      similarityThreshold: 0.75,<br>    }),<br>    hypotheticalAnswer<br>  );<br><br>  const textStream = await streamText({<br>    model: openai.ChatTextGenerator({ model: &quot;gpt-4&quot;, temperature: 0 }),<br>    prompt: [<br>      openai.ChatMessage.system(<br>        `Answer the user&#39;s question using only the provided information.\n` +<br>          `Include the page number of the information that you are using.\n` +<br>          `If the user&#39;s question cannot be answered using the provided information, ` +<br>          `respond with &quot;I don&#39;t know&quot;.`<br>      ),<br>      openai.ChatMessage.user(question),<br>      openai.ChatMessage.fn({<br>        fnName: &quot;getInformation&quot;,<br>        content: JSON.stringify(information),<br>      }),<br>    ],<br>  });<br><br>  process.stdout.write(&quot;\nAI : &quot;);<br>  for await (const textFragment of textStream) {<br>    process.stdout.write(textFragment);<br>  }<br>  process.stdout.write(&quot;\n\n&quot;);<br>}</pre><p>Let’s break down the major components of the code within the chat loop.</p><h3>Looping and waiting for user input<a href="https://modelfusion.dev/blog/pdf-chat-nodejs#looping-and-waiting-for-user-input">​</a></h3><pre>const chat = readline.createInterface({<br>  input: process.stdin,<br>  output: process.stdout,<br>});<br><br>while (true) {<br>  const question = await chat.question(&quot;You: &quot;);<br>  // ...<br>}</pre><p>The chat loop runs indefinitely to keep the chat interaction alive. We use the Node.js readline package for collecting user input from the terminal on each iteration.</p><h3>Generate a hypothetical answer<a href="https://modelfusion.dev/blog/pdf-chat-nodejs#generate-a-hypothetical-answer">​</a></h3><pre>const hypotheticalAnswer = await generateText({<br>  model: openai.ChatTextGenerator({ model: &quot;gpt-3.5-turbo&quot;, temperature: 0 }),<br>  prompt: [<br>    openai.ChatMessage.system(`Answer the user&#39;s question.`),<br>    openai.ChatMessage.user(question),<br>  ],<br>});</pre><p>We use the gpt-3.5-turbo model from OpenAI to create a hypothetical answer first.</p><p>The idea (<a href="https://arxiv.org/abs/2212.10496">hypothetical document embeddings</a>) is that the hypothetical answer will be closer to the chunks we seek in the embedding vector space than the user’s question. This approach will help us to find better results when searching for similar text chunks later.</p><h3>Retrieve relevant text chunks<a href="https://modelfusion.dev/blog/pdf-chat-nodejs#retrieve-relevant-text-chunks">​</a></h3><pre>const { chunks: information } = await retrieveTextChunks(<br>  new SimilarTextChunksFromVectorIndexRetriever({<br>    vectorIndex,<br>    embeddingModel,<br>    maxResults: 5,<br>    similarityThreshold: 0.75,<br>  }),<br>  hypotheticalAnswer<br>);</pre><p>The retrieveTextChunks() function searches for text chunks similar to the hypothetical answer from the pre-processed PDF.</p><p>We limit the results to 5 and set a similarity threshold of 0.75. You can play with these parameters (in combination with the earlier chunk size setting) to see how they affect the results. When you e.g., make the chunks smaller, you might want to increase the number of results to get more information.</p><h3>Generate an answer using text chunks<a href="https://modelfusion.dev/blog/pdf-chat-nodejs#generate-an-answer-using-text-chunks">​</a></h3><pre>const textStream = await streamText({<br>  model: openai.ChatTextGenerator({ model: &quot;gpt-4&quot;, temperature: 0 }),<br>  prompt: [<br>    openai.ChatMessage.system(<br>      `Answer the user&#39;s question using only the provided information.\n` +<br>        `Include the page number of the information that you are using.\n` +<br>        `If the user&#39;s question cannot be answered using the provided information, ` +<br>        `respond with &quot;I don&#39;t know&quot;.`<br>    ),<br>    openai.ChatMessage.user(question),<br>    openai.ChatMessage.functionResult(<br>      &quot;getInformation&quot;,<br>      JSON.stringify(information)<br>    ),<br>  ],<br>});</pre><p>We use gpt-4 to generate a final answer based on the retrieved text chunks. The temperature is set to 0 to remove as much randomness as possible from the response.</p><p>In the system prompt, we specify that:</p><ul><li>The answer should be based solely on the retrieved text chunks.</li><li>The page number of the information should be included.</li><li>The answer should be “I don’t know” if the user’s question cannot be answered using the provided information. This instruction steers the LLM towards using this answer if it cannot find the answer in the text chunks.</li></ul><p>The chunks are inserted as fake function results (using the <a href="https://platform.openai.com/docs/guides/gpt/function-calling">OpenAI function calling API</a>) to indicate that they are separate from the user’s question.</p><p>The answer is streamed to show information to the user as soon as it is available.</p><h3>Stream the answer to the console<a href="https://modelfusion.dev/blog/pdf-chat-nodejs#stream-the-answer-to-the-console">​</a></h3><pre>process.stdout.write(&quot;\nAI : &quot;);<br>for await (const textFragment of textStream) {<br>  process.stdout.write(textFragment);<br>}<br>process.stdout.write(&quot;\n\n&quot;);</pre><p>Finally, we display the generated answer to the user using stdout.write() to print the text fragments collected from textStream.</p><h3>Conclusion<a href="https://modelfusion.dev/blog/pdf-chat-nodejs#conclusion">​</a></h3><p>That wraps up our journey into building a chatbot capable of answering questions based on PDF content. With the help of OpenAI and ModelFusion, you’ve seen how to read, index, and retrieve information from PDF files.</p><p>The code is intended as a starting point for your projects. Have fun exploring!</p><p>P.S.: You can find the complete code for the application here: <a href="https://github.com/lgrammel/modelfusion/tree/main/examples/pdf-chat-terminal">github.com/lgrammel/modelfusion/examples/pdf-chat-terminal</a></p><h3>In Plain English</h3><p><em>Thank you for being a part of our community! Before you go:</em></p><ul><li><em>Be sure to </em><strong><em>clap</em></strong><em> and </em><strong><em>follow</em></strong><em> the writer! 👏</em></li><li><em>You can find even more content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><strong><em> 🚀</em></strong></li><li><em>Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. 🗞️</em></li><li><em>Follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a><strong><em>(X</em></strong>), <a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a>, <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a>, and <a href="https://discord.gg/GtDtUAvyhW"><strong><em>Discord</em></strong></a><strong><em>.</em></strong></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d111aaf4524" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/building-a-pdf-chat-with-node-js-openai-and-modelfusion-9d111aaf4524">Building a PDF Chat with Node.js, OpenAI and ModelFusion</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing ModelFusion: Build AI apps with JavaScript and TypeScript]]></title>
            <link>https://lgrammel.medium.com/introducing-modelfusion-build-ai-apps-with-javascript-and-typescript-7b24a20ef756?source=rss-310ec22024e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b24a20ef756</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Lars Grammel]]></dc:creator>
            <pubDate>Tue, 08 Aug 2023 12:05:30 GMT</pubDate>
            <atom:updated>2024-01-13T10:10:24.919Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HtbeptK4zLiyARl8xX7fww.png" /></figure><p>If you’re a JavaScript or TypeScript developer, you’ve likely noticed the skyrocketing interest in building AI applications, especially since the launch of ChatGPT in November 2022. With AI on the rise, many are exploring new and creative ways to integrate models into applications, chatbots, and agents. But here’s a common roadblock: <strong>there are few libraries designed specifically for JS/TS developers that ease working with AI models, including LLMs (Large Language Models).</strong></p><p>Maybe you’ve experienced this yourself, as I did: You start with something like LangChain, build your first app, and feel a sense of accomplishment. But then, you hit a wall as you realize it’s not easily adjustable to your unique needs. You might then resort to using OpenAI or other APIs directly, only to end up rolling your own framework. It’s a path filled with complexity and unnecessary hassle.</p><p>That’s why I created <a href="https://github.com/lgrammel/modelfusion"><strong>ModelFusion</strong></a>. I went through this exact process and recognized the need for a solution that provides many reusable elements and unification across models without being too constraining. Unlike other tools that may feel like “black magic,” ModelFusion is designed as a library, not a framework. Think of it as a <strong>toolbox</strong> that empowers you with <strong>full power and control over the underlying models</strong>, adding support features with minimal overhead.</p><h3>Quick Example</h3><p>Here’s a simple example demonstrating how you might use ModelFusion to generate text with OpenAI:</p><pre>import { generateText, openai } from &quot;modelfusion&quot;;<br><br>const text = await generateText({<br>  model: openai.CompletionTextGenerator({ model: &quot;gpt-3.5-turbo-instruct&quot; }),<br>  prompt: &quot;Write a short story about a robot learning to love: &quot;,<br>});</pre><p>The response also contains additional information such as the metadata and the full response. The <a href="https://modelfusion.dev/">ModelFusion documentation</a> contains many <a href="https://modelfusion.dev/tutorial/">examples and demo apps</a>.</p><h3>Key Features</h3><p>Here are the features that make ModelFusion stand out:</p><ul><li><strong>Type Inference and Validation</strong>: Leveraging TypeScript and Zod, ModelFusion ensures that you get exactly what you expect from your models. No more guesswork, only clear and validated responses.</li><li><strong>Flexibility and Control</strong>: You’re in charge of your prompts, settings, and raw model responses. Adjust and adapt without feeling boxed in by the framework.</li><li><strong>No Chains and Predefined Prompts</strong>: Start from examples and build applications using familiar JavaScript concepts. Clear and explicit, without any confusing black magic.</li><li><strong>Support for different AI Models</strong>: Go beyond text with integrations like text-to-image and voice-to-text. ModelFusion supports a variety of models to suit your creative vision.</li><li><strong>Integrated Support Features</strong>: Focus on your application with essential features like logging, retries, throttling, tracing, and error handling built right in.</li></ul><p>ModelFusion integrates with a diverse range of model providers, vector indices, and observability tools, such as <strong>OpenAI</strong>, <strong>Llama.cpp</strong>, <strong>Pinecone</strong>, and <strong>Helicone</strong>. This growing list of integrations offers you, the JS/TS developer, a flexible and adaptable toolbox to suit your unique needs, without any unnecessary complexity.</p><h3>Getting Started</h3><p>Here’s what you need to kickstart your journey with ModelFusion:</p><ol><li><strong>Explore the Code</strong>: Visit the <a href="https://github.com/lgrammel/modelfusion">ModelFusion GitHub repository</a> to take a closer look at the source code, contribute, or even raise an issue if you encounter any challenges.</li><li><strong>Read the Docs</strong>: If you want to dive deep into the functionalities, the <a href="https://modelfusion.dev/">ModelFusion documentation</a> is packed with examples, demo apps, and tutorials that will guide you through various use cases and integrations.</li><li><strong>Join the Community</strong>: Have any questions or need help? Don’t hesitate to engage with other like-minded developers in the <a href="https://discord.gg/GqCwYZATem">ModelFusion Discord</a>. We’re here to support each other.</li></ol><p>If you’re working with AI models in JavaScript or TypeScript, explore ModelFusion; your feedback and participation can shape this toolkit to better serve the developer community.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b24a20ef756" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing Rubberduck: AI Chat in Visual Studio Code]]></title>
            <link>https://medium.com/codex/introducing-rubberduck-ai-chat-in-visual-studio-code-bd15966a1d4b?source=rss-310ec22024e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd15966a1d4b</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[visual-studio-code]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Lars Grammel]]></dc:creator>
            <pubDate>Thu, 02 Feb 2023 17:50:02 GMT</pubDate>
            <atom:updated>2023-02-08T13:05:01.840Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L1EFdY25dVcB98FzvDEP3Q.png" /></figure><p><a href="https://marketplace.visualstudio.com/items?itemName=Rubberduck.rubberduck-vscode">Rubberduck</a> is an open-source Visual Studio Code extension that adds a chat interface. You can edit code, get explanations, generate tests, and diagnose errors.</p><p>Rubberduck lets you use your own OpenAI API key and call the OpenAI API directly, so your code is only seen by you and OpenAI. Your API key is stored securely in the Visual Studio Code secret storage. Rubberduck is <a href="https://github.com/rubberduck-ai/rubberduck-vscode">open source and available on GitHub</a>.</p><h3>AI Chat</h3><p>Chat with Rubberduck about your code and software development topics. Rubberduck knows the editor selection at the time of conversation start.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/0*7nIsyxnEka6h3NH9.png" /></figure><h3>Edit Code</h3><p>Change the selected code by instructing Rubberduck to create an edit. You can refine the diff by providing instructions in the chat interface.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/0*MERsGOcz1fEjC3_5.png" /></figure><h3>Explain Code</h3><p>Ask Rubberduck to explain the selected code. Follow up with more questions in the chat interface.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/0*rQiNI5uopmtcnmpg.png" /></figure><h3>Generate Tests</h3><p>Writing test boilerplate is tedious. Generate test cases with Rubberduck, refine them through chat, and get them 90% ready in less than 1 minute.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/0*0I3fwBTj9_Re-W9G.gif" /></figure><h3>Diagnose Errors</h3><p>Let Rubberduck identify error causes and suggest fixes to fix compiler and linter errors faster.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/0*luAuQbz6VJ2Esi1M.png" /></figure><h3>Project</h3><p>Rubberduck is open source and being developed in public!</p><p>You can find out more here:</p><ul><li>Visual Studio Code Marketplace: <a href="https://marketplace.visualstudio.com/items?itemName=Rubberduck.rubberduck-vscode">Rubberduck.rubberduck-vscode</a></li><li>Github: <a href="https://github.com/rubberduck-ai/rubberduck-vscode">github.com/rubberduck-ai/rubberduck-vscode</a></li><li>Discord: <a href="https://discord.gg/8KN2HmyZmn">discord.gg/8KN2HmyZmn</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd15966a1d4b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/introducing-rubberduck-ai-chat-in-visual-studio-code-bd15966a1d4b">Introducing Rubberduck: AI Chat in Visual Studio Code</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AI-generated explanations and documentation for JavaScript code]]></title>
            <link>https://lgrammel.medium.com/ai-generated-explanations-and-documentation-for-javascript-code-33f79b9aa546?source=rss-310ec22024e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/33f79b9aa546</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Lars Grammel]]></dc:creator>
            <pubDate>Thu, 08 Dec 2022 19:37:36 GMT</pubDate>
            <atom:updated>2022-12-08T19:37:36.639Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*Doz1x-M1UTfB4EDTJM3wcA.png" /></figure><p>As a JavaScript developer, you know how challenging it can be to work with existing codebases. Often, the code is poorly documented, making it difficult to understand what it does and how it works. That’s why I’ve added two new AI-powered features to the <a href="https://p42.ai/?utm_medium=article&amp;utm_source=medium">JS Assistant</a> for Visual Studio Code: AI-generated JS-Doc comments and AI-generated explanations for code snippets.</p><h3>AI-generated JSDoc comments</h3><p>The JS Assistant can <a href="https://p42.ai/documentation/code-assist/document-function?utm_medium=article&amp;utm_source=medium">generate documentation for functions and methods</a>. This feature accelerates writing JSDoc comments by providing a pre-filled template you can refine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*A9yjTIfvWeLGPk7M.gif" /></figure><h3>AI-generated code explanations</h3><p>You can <a href="https://p42.ai/documentation/code-assist/explain-code?utm_medium=article&amp;utm_source=medium">generate explanations of JavaScript code snippets</a> to help you understand complex or challenging code passages. While such AI-generated descriptions can sometimes be inaccurate or misleading, they can help accelerate making sense of a legacy codebase. They are best used with your code understanding, e.g., to provide ideas that get you started or unstuck.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*UUfRwhzbpK3OFzYb.gif" /></figure><p>These AI-powered actions can help you work more effectively in complex legacy JavaScript code today.</p><p>Code explanations and comment generation are early, experimental features. I would love to hear how you think they would be helpful?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=33f79b9aa546" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[6 VS Code Extensions for Productive JavaScript and TypeScript Editing]]></title>
            <link>https://medium.com/better-programming/6-vs-code-extensions-for-productive-javascript-and-typescript-editing-d879674c5db0?source=rss-310ec22024e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/d879674c5db0</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vscode]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[visual-studio-code]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Lars Grammel]]></dc:creator>
            <pubDate>Tue, 01 Nov 2022 17:34:35 GMT</pubDate>
            <atom:updated>2022-11-02T05:02:53.800Z</atom:updated>
            <content:encoded><![CDATA[<h4>Enhance your code editing skills</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IMxila-qMoebV2Uxqf6R0A.jpeg" /></figure><p>Code editing is an essential part of the development workflow. It includes all your actions in the editor before re-running your tests or checking how your changes affect the app you are working on:</p><ul><li>Adding, changing, and removing code</li><li>Fixing syntax errors</li><li>Fixing typos, e.g., in variable and function names</li><li>Fixing linter errors and warnings</li><li>Formatting</li><li>Refactoring and restructuring code</li></ul><p>Having minor friction points in the code editing process can lead to wasted time and frustration. You might have to switch context and lose your train of thought when, for example, you don’t immediately see or understand a syntax error, you miss a potential bug, or your editor is suddenly full of formatting-related linter warnings.</p><p><a href="https://code.visualstudio.com/">Visual Studio Code</a> provides a solid editing experience and offers many extensions you can install to take it to the next level. These six Visual Studio Code extensions (<a href="https://marketplace.visualstudio.com/items?itemName=p42ai.vscode-javascript-editing-extension-pack">extension pack</a>) help me stay in the flow and be productive when editing JavaScript and TypeScript code:</p><h3>GitHub Copilot</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/0*rYwpIw_I9szVCVhI.png" /></figure><p><a href="https://marketplace.visualstudio.com/items?itemName=GitHub.copilot">GitHub Copilot</a> suggests code completions for your current editor position. Its AI-generated suggestions range from short statement completion to full functions and classes.</p><p>Copilot is, for example, valuable for automatically generating boilerplate code, discovering APIs, and implementing common patterns quickly. It is also helpful for getting unstuck when it is not immediately clear what code is needed to solve a problem.</p><p>The code suggestions are only sometimes perfect and need review and refinement, and often I discard them entirely. Still, overall, Copilot boosts my productivity and helps me stay in the flow.</p><h3>Prettier</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/0*PFAKXpHTjBxzzuU1.png" /></figure><p>Consistent formatting makes code easier to read and reduces accidental churn in pull requests. However, manually formatting your code is very tedious and error-prone. You can save time and keep your codebase consistently styled with an automated code formatter.</p><p><a href="https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode&amp;ssr=false">Prettier</a> is an opinionated code formatter that many JavaScript and TypeScript projects use. It can format JavaScript, TypeScript, JSON, HTML, JSX (React), CSS, and more. Because it only offers a handful of configuration options, using Prettier can help avoid bike-shedding discussions about code formatting.</p><p>You can configure Prettier to format your code automatically when you save a file. If you want to minimize modifications to existing files, you can limit the formatting to the modified code. These are the Visual Studio Code settings for configuring Prettier:</p><pre>&quot;editor.defaultFormatter&quot;: &quot;esbenp.prettier-vscode&quot;,<br>&quot;editor.formatOnSave&quot;: true,<br>&quot;editor.formatOnSaveMode&quot;: &quot;modificationsIfAvailable&quot;</pre><p>If you use a keyboard shortcut to save often, e.g., after each small code change, fix, or refactoring, you can even write poorly formatted code and rely on Prettier to fix it without much friction.</p><h3>quick-lint-js</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/683/0*9p79UJMc9FKRjAjl.png" /></figure><p><a href="https://marketplace.visualstudio.com/items?itemName=quick-lint.quick-lint-js&amp;ssr=false">quick-lint-js</a> is an excellent alternative to <a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint&amp;ssr=false">ESLint</a> when you primarily want to check for code correctness. It provides a fixed set of rules for TypeScript and JavaScript and is extremely fast.</p><p>While quick-lint-js does not have the extensibility and the configuration options of ESLint, it helps you see common mistakes quickly and fix them. And since it focuses on correctness, quick-lint-js does not contain opinionated style rules and works very well with Prettier.</p><h3>Code Spell Checker</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/0*aECSAWaF0XM6Otfu.png" /></figure><p>Not all mistakes result in syntax errors or potential bugs. It is easy to have a typo in a variable, function, or class name without noticing it. However, this can lead to future issues with searchability and readability. <a href="https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.code-spell-checker&amp;ssr=false">Code Spell Checker</a> helps you spot and correct typos early.</p><h3>P42 JS Assistant</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/0*40R0QBJMrPnmGYcP.png" /></figure><p>Small refactorings can help restructure and clean up your code while editing it. I have developed the <a href="https://marketplace.visualstudio.com/items?itemName=p42ai.refactor&amp;ssr=false">P42 JS Assistant</a>, which provides many automated code actions and suggests refactorings that make your code more readable and modern.</p><p>You can open a context menu with code actions in the editor by clicking on the lightbulb icon or using the quick-fix keyboard shortcut (Ctrl + . on Windows and Linux and ⌘ + . on Mac). You can use the code actions to make semantic changes to your code quickly and safely.</p><p>The refactoring suggestions show places where you can simplify or modernize your code. They can help you stay up-to-date with the latest JavaScript idioms and best practices. An underline with three dots indicates that a refactoring suggestion is available.</p><h3>Error Lens</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/0*EjQcPdKj3nwfduHN.png" /></figure><p>By default, the VS Code editor indicates the presence of errors, warnings, and other hints with various underlines. You need to hover over the underlined code section with the mouse cursor to be able to read the messages. This additional step can take time and requires you to use the mouse.</p><p><a href="https://marketplace.visualstudio.com/items?itemName=usernamehw.errorlens&amp;ssr=false">Error Lens</a> shows the diagnostic messages on the same line in the editor, saving you time and keeping you in the flow. You can distinguish different message levels using their color. Errors are displayed in red, warnings in yellow, information messages in blue, and hints in green.</p><p>In addition to showing TypeScript and JavaScript errors, the message coloring makes Error Lens an excellent companion to quick-lint-js (error, warnings), Code Spell Checker (information), and the P42 JS Assistant (hint). You can toggle the display of different message types with the Error Lens: Toggle commands in the Command Palette.</p><h3>Summary</h3><p>Editing code is central to software development. With the above Visual Studio Code extensions, you can enhance your editing experience and make it easier to write high-quality code. You can conveniently install the extensions using the <a href="https://marketplace.visualstudio.com/items?itemName=p42ai.vscode-javascript-editing-extension-pack&amp;ssr=false">JavaScript and TypeScript Editing extension pack</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d879674c5db0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/6-vs-code-extensions-for-productive-javascript-and-typescript-editing-d879674c5db0">6 VS Code Extensions for Productive JavaScript and TypeScript Editing</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Configure the Touch Bar in Visual Studio Code in Under 5 Minutes]]></title>
            <link>https://medium.com/codex/how-to-configure-the-touch-bar-in-visual-studio-code-in-under-5-minutes-9cdd0a9d3dc5?source=rss-310ec22024e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/9cdd0a9d3dc5</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vscode]]></category>
            <dc:creator><![CDATA[Lars Grammel]]></dc:creator>
            <pubDate>Thu, 20 Oct 2022 19:34:46 GMT</pubDate>
            <atom:updated>2022-10-21T01:23:21.879Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lrfNriH9H0gNYoUZaBa9vA.jpeg" /></figure><p>The touch bar on the MacBook has the potential to provide <strong>quick access to commonly used commands</strong> in Visual Studio Code.</p><p>In the default configuration, the ‘go forward’ and ‘go back’ buttons are very useful. However, other default touch bar actions are less valuable and take up space. You might also find that everyday operations such as ‘rename’ (F2) or &#39;go to definition&#39; (F12) are harder to access because you need to hold down Fn.</p><p>This post will show you how to <strong>configure Visual Studio Code to make the most of the touch bar</strong>.</p><h3>Nasc Touchbar extension</h3><p>The <a href="https://marketplace.visualstudio.com/items?itemName=felipe.nasc-touchbar&amp;ssr=false"><strong>Nasc Touchbar extension</strong></a> enables you to easily add many additional actions to the touch bar, for example, Run command, Toggle side bar, and Rename.</p><p>You can configure which buttons are displayed in the Visual Studio Code settings (Preferences &gt; Open Settings (UI), search for touch).</p><h3>JavaScript Assistant extension</h3><p>I have included touch bar buttons that are helpful for JavaScript and TypeScript users in the <a href="https://marketplace.visualstudio.com/items?itemName=p42ai.refactor&amp;ssr=false">JavaScript Assistant extension</a>. The extension adds the following actions to the touch bar:</p><ul><li>✏️ Rename: Triggers the rename command on the current symbol.</li><li>🪄 Quick-Fix: Opens the quick-fix context menu.</li><li>🔧 Refactor: Opens the refactor context menu.</li><li>📄 Source Action: Opens the source action context menu.</li></ul><p>The actions are only available when you edit JavaScript or TypeScript files. Similar to the Nasc Touchbar extension, you can configure which buttons are displayed in the Visual Studio Code settings (Preferences &gt; Open Settings (UI)).</p><h3>Disabling default debug touch bar actions</h3><p>The default touch bar buttons take precedence over the controls added by the Nasc Touchbar and JavaScript Assistant extensions. In particular, the debug actions can take up a lot of space and hide other buttons.<br>You can disable them by adding the following to your settings.json file:</p><pre>&quot;keyboard.touchbar.ignored&quot;: [<br>  &quot;workbench.action.debug.start&quot;,<br>  &quot;workbench.action.debug.run&quot;,<br>  &quot;workbench.action.debug.pause&quot;,<br>  &quot;workbench.action.debug.stepOut&quot;,<br>  &quot;workbench.action.debug.stepInto&quot;,<br>  &quot;workbench.action.debug.stepOver&quot;,<br>  &quot;workbench.action.debug.stop&quot;,<br>  &quot;workbench.action.debug.restart&quot;<br>]</pre><h3>Example configuration</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/0*5jtK-hwICg36Pg0_.png" /></figure><p>The above configuration shows the following buttons:</p><ul><li>Go back and Go forward (default)</li><li>Go to definition, Run command, Toggle side bar, and Toggle bottom panel (Nasc Touchbar)</li><li>Rename, Source Action, Refactor, and Quick-Fix (JavaScript Assistant)</li></ul><p>In addition to the disable configuration for the debug actions, the following settings are used:</p><pre>&quot;nasc-touchbar.addCursorBelow&quot;: false,<br>&quot;nasc-touchbar.rename&quot;: false,<br>&quot;nasc-touchbar.toggleSidebar&quot;: true</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9cdd0a9d3dc5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/how-to-configure-the-touch-bar-in-visual-studio-code-in-under-5-minutes-9cdd0a9d3dc5">How to Configure the Touch Bar in Visual Studio Code in Under 5 Minutes</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What’s new in P42? Summer 2022 Summary]]></title>
            <link>https://lgrammel.medium.com/whats-new-in-p42-summer-2022-summary-8a7b46c2dc52?source=rss-310ec22024e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a7b46c2dc52</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vscode]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Lars Grammel]]></dc:creator>
            <pubDate>Tue, 21 Jun 2022 08:34:45 GMT</pubDate>
            <atom:updated>2022-06-21T09:09:07.099Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SqqhSdJcVFQEqnTvIHl0RA.jpeg" /></figure><p>Since February, the <a href="https://marketplace.visualstudio.com/items?itemName=p42ai.refactor">P42 JavaScript Assistant for Visual Studio Code</a> has grown considerably and contains over 90 code assists and refactorings. The user interface has been improved based on feedback, and the JavaScript Assistant now supports Vue.js. Here are the details:</p><h3>Vue.js Support (P42 Pro)</h3><p>You can now use code assists, refactorings, and suggestions from the P42 JavaScript Assistant inside .vue files and mass refactorings updates .vue files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*ZBTzMdqifTMtuBpP.gif" /></figure><h3>User Interface</h3><h4>Keyboard Shortcuts</h4><p>The JavaScript Assistant has different default keyboard shortcuts for the code assist context menus and a “move” context menu shortcut. You can now <a href="https://p42.ai/documentation/p42-for-vscode/editor-integration#keyboard-shortcuts">add custom keyboard shortcuts for any P42 code assist</a>.</p><h4>Suggestion Display and Panel</h4><p>The P42 refactorings suggestions are shown as hints inside the editor (underline with three dots) by default, and the suggestion side panel highlights relevant code on mouseover. When you click on a selected item in the side panel, the editor scrolls to the appropriate location.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*4OgyCGPAM-R6EJ_R.gif" /></figure><h4>Mass Refactoring (P42 Pro)</h4><p>The <a href="https://p42.ai/documentation/p42-for-vscode/mass-refactoring">mass refactoring</a> dialog is now more usable. You can search the description and details, e.g., to find all ES2015 modernizations, and the refactorings are sorted alphabetically.</p><p>The mass refactoring output is easier to understand and contains more information. It includes the file exclusions patterns, a progress indicator for every 50 analyzed files, and the overall refactoring time in seconds.</p><h3>Configuration</h3><h4>ECMAScript Target Version</h4><p>You can <a href="https://p42.ai/documentation/p42-for-vscode/configuration#platform-version">specify the target ECMAScript version in the P42 configuration file</a>. Code modernizations that would require newer ECMAScript versions are hidden when you configure an older target version.</p><h4>Hide Overlapping Code Actions</h4><p>The P42 JavaScript Assistant provides code assists that mirror and extend the functionality of code assists that come with Visual Studio Code. You can now <a href="https://p42.ai/documentation/p42-for-vscode/configuration#overlapping-code-assist-visibility">configure the visibility of overlapping code assists</a>.</p><h3>Code Assists and Refactorings</h3><h4>Working with Variables</h4><ul><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/split-variable-declaration-and-initialization"><strong>Split Variable Declaration and Initialization</strong></a>: Separate the variable initialization from its declaration.</li><li><em>New</em> — <a href="https://p42.ai/code-assist/merge-variable-declaration-and-initialization"><strong>Merge Variable Declaration and Initialization</strong></a>: Convert the initial assignment of a variable into its declaration initializer.</li><li><em>New</em> — <a href="https://p42.ai/code-assist/replace-expression-with-existing-variable"><strong>Replace with existing variable</strong></a>: Replace an expression with an existing variable (P42 Pro).</li><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/push-into-initial-value-declaration"><strong>Push variable declaration into initial value</strong></a>: Inlines a variable that is initialized with another variable into the declaration of that variable (P42 Pro).</li><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/remove-unused-variable"><strong>Remove unused variable</strong></a>: Remove a variable that is not read or written.</li><li><em>Improved</em> — <a href="https://p42.ai/documentation/code-assist/extract-variable"><strong>Extract variable</strong></a> suggests better variable names when extracting identifiers and property access expressions and automatically creates blocks when needed.</li></ul><h4>Working with Destructuring Expressions</h4><ul><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/convert-to-destructuring-assignment"><strong>Convert to destructuring assignment</strong></a>: Convert a variable declaration that accesses an object property to a destructuring assignment.</li><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/merge-into-preceding-destructuring-assignment"><strong>Merge into preceding destructuring assignment</strong></a>: Combine an object destructuring assignment with its preceding sibling.</li><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/flatten-array-rest-property"><strong>Flatten array rest/spread property</strong></a>: Merge a ...[] expression into the outer array literal or destructuring expression (P42 Pro).</li></ul><h4>Working with Classes</h4><ul><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/move-field-initializer-into-constructor"><strong>Move field initialization into constructor</strong></a>: Moves the assignment of the initial field value into the class constructor (P42 Pro).</li><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/move-field-initializer-into-declaration"><strong>Move initialization into field declaration</strong></a>: Moves the assignment of the initial field value into the field declaration (P42 Pro).</li></ul><h4>Working with Functions</h4><ul><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/convert-named-function-to-function-expression"><strong>Convert named function to function expression</strong></a>: Converts a named function to a const declaration with a function expression.</li><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/introduce-early-return"><strong>Introduce early return / continue</strong></a>: Change an if-statement into an early return or continue statement (P42 Pro).</li></ul><h4>Working with Loops and Arrays</h4><ul><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/convert-loop-to-map"><strong>Convert loop to .map()</strong></a>: Convert a loop with .push() into a .map() call (P42 Pro).</li><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/replace-lodash-foreach"><strong>Replace <em>.each and </em>.forEach with array.forEach</strong></a>: Replace Lodash _.each and _.forEach with array.forEach (P42 Pro).</li><li><em>Improved</em> — <a href="https://p42.ai/documentation/code-assist/convert-array-index-of-to-array-includes"><strong>Convert array.indexOf() into array.includes()</strong></a> converts exclusion checks to !array.includes(..).</li><li><em>Improved</em> — <a href="https://p42.ai/documentation/code-assist/convert-loop-to-for-of"><strong>Convert loop to for…of</strong></a> supports converting anArray.forEach to a for…of loop.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*zKeo5rhhlSGfzOi6.gif" /></figure><h4>Working with Conditional Expressions</h4><ul><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/convert-and-and-guard-to-if-statement"><strong>Convert &amp;&amp; to if statement</strong></a>: Convert condition &amp;&amp; aFunction(); and similar expression statements into if statements.</li><li><em>Improved</em> — <a href="https://p42.ai/documentation/code-assist/split-if"><strong>Split if statement</strong></a> supports splitting on ||.</li><li><em>Improved</em> — <a href="https://p42.ai/documentation/code-assist/flip-operator"><strong>Flip operator</strong></a> supports +, *, and binary operators.</li></ul><h4>Working with String and Templates</h4><ul><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/convert-comparison-chain-to-array-includes"><strong>Convert string comparison chain to array.includes</strong></a>: Replace || value === &#39;aString&#39;`` and&amp;&amp; value !== &#39;aString&#39;chains with `array.includes().</li><li><em>Improved</em> — <a href="https://p42.ai/documentation/code-assist/extract-substring-to-variable"><strong>Extract selected text into variable</strong></a> supports extracting text and included expressions from template literals.</li><li><em>Improved</em> — <a href="https://p42.ai/documentation/code-assist/inline-into-template"><strong>Inline into template</strong></a> supports inlining template literals into template literals.</li></ul><h4>Other Code Actions</h4><ul><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/remove-console-log"><strong>Remove console.log</strong></a>: Remove console.log statement.</li><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/replace-void-0-with-undefined"><strong>Replace void 0 with undefined</strong></a>: Replace void 0 and other constant void expressions with undefined.</li><li><em>New</em> — <a href="https://p42.ai/documentation/code-assist/select-expression-occurrences"><strong>Select expression occurrences</strong></a>: Start a multi-cursor selection on several occurrences of the same expression (P42 Pro).</li><li><em>Improved</em> — Replaced the “move statement out of if-else” refactoring with new <a href="https://p42.ai/documentation/code-assist/move-first-statement-out-of-if-else"><strong>Move duplicated first statement out of if-else</strong></a> and <a href="https://p42.ai/documentation/code-assist/move-last-statement-out-of-if-else"><strong>Move duplicated last statement out of if-else</strong></a> refactorings.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*eu09w_CTOqf1A4Kp.gif" /></figure><p>You can follow <a href="https://twitter.com/p42ai">@p42ai on Twitter</a> to get notified about new features.</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a7b46c2dc52" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>