<?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 Rajeev R Sharma on Medium]]></title>
        <description><![CDATA[Stories by Rajeev R Sharma on Medium]]></description>
        <link>https://medium.com/@rajeevs?source=rss-9352fec71fae------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*PJIdea4glVeZKVjGHn_tpw.jpeg</url>
            <title>Stories by Rajeev R Sharma on Medium</title>
            <link>https://medium.com/@rajeevs?source=rss-9352fec71fae------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 19:54:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rajeevs/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[Create Your Own Cloudflare Workers AI LLM Playground Using NuxtHub and NuxtUI]]></title>
            <link>https://medium.com/@rajeevs/create-your-own-cloudflare-workers-ai-llm-playground-using-nuxthub-and-nuxtui-d1961e77bbe2?source=rss-9352fec71fae------2</link>
            <guid isPermaLink="false">https://medium.com/p/d1961e77bbe2</guid>
            <category><![CDATA[nuxtui]]></category>
            <category><![CDATA[nuxthub]]></category>
            <category><![CDATA[cloudflare]]></category>
            <category><![CDATA[nuxt]]></category>
            <category><![CDATA[ai-worker]]></category>
            <dc:creator><![CDATA[Rajeev R Sharma]]></dc:creator>
            <pubDate>Wed, 04 Sep 2024 09:14:20 GMT</pubDate>
            <atom:updated>2024-09-04T09:24:49.167Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0GsUKAA5eyUhJLPJmLoqqQ.jpeg" /></figure><p>You might be wondering, ‘Another LLM (Large Language Model) playground? Aren’t there plenty of these already?’ Fair question. But here’s the thing: the world of AI is constantly evolving, and every day one new tool or another pops up. One such AI offering from <a href="https://hub.nuxt.com/">NuxtHub</a> was launched recently, and I couldn’t resist taking it for a spin. And let’s be honest — some of us just can’t resist adding a ‘dark mode’, something the Cloudflare Workers AI models playground hasn’t embraced yet.</p><p>But beyond these practical reasons, there’s something even more satisfying: the joy of creation and learning. So, if you’re ready to dive in, maybe you’ll pick up some new concepts like Server-Sent Events, response streaming, markdown parsing, and, of course, deploying your own chat playground without any of the usual fuss. So, get comfy — however you like — and let’s get started!</p><h3>Project Overview</h3><p>Alright, so what exactly are we going to create here? As you might have guessed already, we will be creating a chat interface to talk to different <a href="https://developers.cloudflare.com/workers-ai/models#text-generation">text generation models</a> supported by <a href="https://developers.cloudflare.com/workers-ai">Cloudflare Workers AI</a>. Below is a brief list of capabilities we will build along the way:</p><ul><li>Ability to set different LLM params, like temperature, max tokens, system prompt, top_p, top_k etc while keeping some of these optional</li><li>Ability to turn LLM response streaming on/off</li><li>Handle streaming/non-streaming LLM responses on both, the server and the client side</li><li>Parsing LLM responses for markdown and display it appropriately</li><li>Auto-scrolling the chat container as the response is streamed from the LLM endpoint</li><li>Adding the dark mode (this one is trivial but let’s add it here for completeness)</li></ul><p>This is how the interface will look when we are through this article:</p><figure><img alt="LLM playground chat interface" src="https://cdn-images-1.medium.com/max/1024/1*LydmPsqbZB7QxaG2jrL_Hg.png" /><figcaption>LLM playground chat interface</figcaption></figure><p>You can try it out live here: <a href="https://hub-chat.nuxt.dev/">https://hub-chat.nuxt.dev</a></p><p>We will cover each of the tasks in detail in the following sections.</p><h3>Project Setup</h3><p>Now that we’ve set the stage for our LLM playground project, let’s dive into the technologies we’ll be using and get our development environment ready.</p><h4>Technologies we’ll use</h4><ol><li><a href="https://nuxt.com/">Nuxt 3</a>: Nuxt 3 is a powerful Vue.js framework that will serve as the foundation of our application.</li><li><a href="https://ui.nuxt.com/">Nuxt UI</a>: A Nuxt module that will help us create a sleek and responsive interface.</li><li><a href="https://hub.nuxt.com/">NuxtHub</a>: <strong>NuxtHub</strong> is a deployment and administration platform for Nuxt, <strong>powered by Cloudflare</strong>. We can use NuxtHub to access different Cloudflare offerings like <strong>D1 database</strong>, <strong>Workers KV</strong>, <strong>R2 Storage</strong>, <strong>Workers AI</strong> etc. In this project, we’ll use it to access the LLMs as well as to deploy our project</li><li><a href="https://github.com/nuxt-modules/mdc">Nuxt MDC</a>: For parsing and displaying the chat messages</li></ol><h4>Prerequisites</h4><p>Apart from the basic prerequisites like Node/Npm, Code Editors, and some VueJs/Nuxt knowledge, you’ll need the following to follow along:</p><ol><li><strong>A Cloudflare account:</strong> To use the Workers AI models, as well as to deploy your project on Cloudflare Pages for free. If you don’t have it already, you can set it up <a href="https://www.cloudflare.com/">here</a>.</li><li><strong>A NuxtHub Admin Account</strong>: NuxtHub admin is a web based dashboard to manage NuxtHub apps. You can create your account <a href="https://admin.hub.nuxt.com/">here</a>.</li></ol><h4>Setting up the project</h4><p>We can either start with a Nuxt (or Nuxt UI) template and add NuxtHub on top of it, or we can use the NuxtHub starter template and add Nuxt UI to it. We’ll take the second approach:</p><p>1. Create a new NuxtHub project</p><pre># Init the project and install dependencies<br> npx nuxthub init cf-playground<br> <br> # Change into the created dir<br> cd cf-playground</pre><p>2. Add the Nuxt UI module. The below command will install the @nuxt/ui dependency as well as add it as a module in your nuxt config file</p><pre>npx nuxi module add ui</pre><p>3. Similarly, add the Nuxt MDC module</p><pre>npx nuxi module add mdc</pre><p>Now we have setup everything that we need for this project. You can try running the project with pnpm dev (or an equivalent command if you’re using a different package manager) and visit <a href="http://localhost:3000/">http://localhost:3000</a> in your browser. If you’ve dark mode enabled on your system then you’ll notice that dark mode is already up and running in the project (We just need a way to toggle it).</p><p>With our development environment set up, we’re ready to start building our LLM playground. In the next section, we’ll begin implementing our user interface using NuxtUI components.</p><h3>Creating the UI Components</h3><p>First, we will create a component that will let us set numerical values for LLM settings using either a range slider or an input box. Let’s call it RangeInput.</p><h4>The RangeInput Component</h4><p>We utilize a FormGroup component from NuxtUI to create this component. We put a Range slider in the default slot and an input in the hint slot to achieve the desired outcome. Here is the complete component code</p><pre>&lt;template&gt;<br>  &lt;UFormGroup :label=&quot;label&quot; :ui=&quot;{ container: &#39;mt-2&#39; }&quot;&gt;<br>    &lt;template #hint&gt;<br>      &lt;UInput<br>        v-model=&quot;model&quot;<br>        class=&quot;w-[72px]&quot;<br>        type=&quot;number&quot;<br>        :min=&quot;min&quot;<br>        :max=&quot;max&quot;<br>        :step=&quot;step&quot;<br>      /&gt;<br>    &lt;/template&gt;<br>    &lt;URange v-model=&quot;model&quot; :min=&quot;min&quot; :max=&quot;max&quot; :step=&quot;step&quot; size=&quot;sm&quot; /&gt;<br>  &lt;/UFormGroup&gt;<br>&lt;/template&gt;<br><br>&lt;script setup lang=&quot;ts&quot;&gt;<br>const model = defineModel({ type: Number, default: undefined });<br><br>defineProps({<br>  label: {<br>    type: String,<br>    required: true,<br>  },<br>  min: {<br>    type: Number,<br>    default: undefined,<br>  },<br>  max: {<br>    type: Number,<br>    default: undefined,<br>  },<br>  step: {<br>    type: Number,<br>    default: undefined,<br>  },<br>});<br>&lt;/script&gt;</pre><p>Instead of taking the model value as a prop and then emitting the changes manually, we use the defineModel macro to achieve two way binding. If the initial model value is undefined the input box will be empty. This will help in making some of the LLM params optional.</p><h4>The LLMSettings Component</h4><p>LLMSettings component utilizes many RangeInputs that we created above as most of the settings are numerical values. Apart from RangeInputs we use a textarea for the system prompt, a toggle for enabling/disabling response streaming, a select menu for choosing the LLM model, and an accordion for hiding the optional params.</p><p>Here are the relevant parts of the component</p><pre>&lt;template&gt;<br>  &lt;div class=&quot;h-full flex flex-col overflow-hidden&quot;&gt;<br>    &lt;!-- Settings Header Code --&gt;<br>    &lt;UDivider /&gt;<br>    &lt;div class=&quot;p-4 flex-1 space-y-6 overflow-y-auto&quot;&gt;<br>      &lt;UFormGroup label=&quot;Model&quot;&gt;<br>        &lt;USelectMenu<br>          v-model=&quot;llmParams.model&quot;<br>          size=&quot;md&quot;<br>          :options=&quot;models&quot;<br>          value-attribute=&quot;id&quot;<br>          option-attribute=&quot;name&quot;<br>        /&gt;<br>      &lt;/UFormGroup&gt;<br><br>      &lt;RangeInput<br>        v-model=&quot;llmParams.temperature&quot;<br>        label=&quot;Temperature&quot;<br>        :min=&quot;0&quot;<br>        :max=&quot;5&quot;<br>        :step=&quot;0.1&quot;<br>      /&gt;<br><br>      &lt;RangeInput<br>        v-model=&quot;llmParams.maxTokens&quot;<br>        label=&quot;Max Tokens&quot;<br>        :min=&quot;1&quot;<br>        :max=&quot;4096&quot;<br>      /&gt;<br><br>      &lt;UFormGroup label=&quot;System Prompt&quot;&gt;<br>        &lt;UTextarea<br>          v-model=&quot;llmParams.systemPrompt&quot;<br>          :rows=&quot;3&quot;<br>          :maxrows=&quot;8&quot;<br>          autoresize<br>        /&gt;<br>      &lt;/UFormGroup&gt;<br><br>      &lt;div class=&quot;flex items-center justify-between&quot;&gt;<br>        &lt;span&gt;Stream Response&lt;/span&gt;<br>        &lt;UToggle v-model=&quot;llmParams.stream&quot; /&gt;<br>      &lt;/div&gt;<br><br>      &lt;UAccordion<br>        :items=&quot;accordionItems&quot;<br>        color=&quot;white&quot;<br>        variant=&quot;solid&quot;<br>        size=&quot;md&quot;<br>      &gt;<br>        &lt;template #item&gt;<br>          &lt;UCard :ui=&quot;{ body: { base: &#39;space-y-6&#39;, padding: &#39;p-4 sm:p-4&#39; } }&quot;&gt;<br>            &lt;RangeInput<br>              v-model=&quot;llmParams.topP&quot;<br>              label=&quot;Top P&quot;<br>              :min=&quot;0&quot;<br>              :max=&quot;2&quot;<br>              :step=&quot;0.1&quot;<br>            /&gt;<br><br>            &lt;!-- Other optional params --&gt;<br>          &lt;/UCard&gt;<br>        &lt;/template&gt;<br>      &lt;/UAccordion&gt;<br>    &lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;<br><br>&lt;script setup lang=&quot;ts&quot;&gt;<br>type LlmParams = {<br>  model: string;<br>  temperature: number;<br>  maxTokens: number;<br>  topP?: number;<br>  topK?: number;<br>  frequencyPenalty?: number;<br>  presencePenalty?: number;<br>  systemPrompt: string;<br>  stream: boolean;<br>};<br><br>const llmParams = defineModel(&#39;llmParams&#39;, {<br>  type: Object as () =&gt; LlmParams,<br>  required: true,<br>});<br><br>defineEmits([&#39;hideDrawer&#39;, &#39;reset&#39;]);<br><br>const accordionItems = [<br>  {<br>    label: &#39;Advanced Settings&#39;,<br>    defaultOpen: false,<br>  },<br>];<br><br>const models = [<br>  {<br>    name: &#39;deepseek-coder-6.7b-base-awq&#39;,<br>    id: &#39;@hf/thebloke/deepseek-coder-6.7b-base-awq&#39;,<br>  },<br>  { <br>    name: &#39;llama-3-8b-instruct&#39;, <br>    id: &#39;@cf/meta/llama-3-8b-instruct&#39;, <br>  },<br>  // ...other models<br>]<br>&lt;/script&gt;</pre><h4>The ChatPanel Component</h4><p>This is the most important component of all as it handles the core chat functionality of the app. It consists of three parts:</p><p><strong>Chat Header</strong></p><p>It displays the app name/label apart from some global buttons for clearing chats, dark mode toggling and for showing the settings drawer on mobile devices</p><pre>&lt;template&gt;<br>  &lt;div class=&quot;flex items-center justify-between p-4&quot;&gt;<br>    &lt;div class=&quot;flex items-center gap-x-4&quot;&gt;<br>      &lt;h2 class=&quot;text-xl md:text-2xl text-primary font-bold&quot;&gt;Hub Chat&lt;/h2&gt;<br>      &lt;UTooltip text=&quot;Clear chat&quot;&gt;<br>        &lt;UButton<br>          color=&quot;gray&quot;<br>          icon=&quot;i-heroicons-trash&quot;<br>          size=&quot;xs&quot;<br>          :disabled=&quot;clearDisabled&quot;<br>          @click=&quot;$emit(&#39;clear&#39;)&quot;<br>        /&gt;<br>      &lt;/UTooltip&gt;<br>    &lt;/div&gt;<br>    &lt;div class=&quot;flex items-center gap-x-4&quot;&gt;<br>      &lt;ColorMode /&gt;<br>      &lt;UButton<br>        icon=&quot;i-heroicons-cog-6-tooth&quot;<br>        color=&quot;gray&quot;<br>        variant=&quot;ghost&quot;<br>        class=&quot;md:hidden&quot;<br>        @click=&quot;$emit(&#39;showDrawer&#39;)&quot;<br>      /&gt;<br>    &lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;<br><br>&lt;script setup lang=&quot;ts&quot;&gt;<br>defineEmits([&#39;clear&#39;, &#39;showDrawer&#39;]);<br><br>defineProps({<br>  clearDisabled: {<br>    type: Boolean,<br>    default: true,<br>  },<br>});<br>&lt;/script&gt;</pre><p><strong>Chats Container</strong></p><p>For parsing and displaying the chat messages. It also shows a message loading skeleton when streaming is off, as well as a NoChats placeholder when needed.</p><pre>&lt;div ref=&quot;chatContainer&quot; class=&quot;flex-1 overflow-y-auto p-4 space-y-5&quot;&gt;<br>  &lt;div<br>    v-for=&quot;(message, index) in chatHistory&quot;<br>    :key=&quot;`message-${index}`&quot;<br>    class=&quot;flex items-start gap-x-4&quot;<br>  &gt;<br>    &lt;div<br>      class=&quot;w-12 h-12 p-2 rounded-full&quot;<br>      :class=&quot;`${<br>        message.role === &#39;user&#39; ? &#39;bg-primary/20&#39; : &#39;bg-blue-500/20&#39;<br>      }`&quot;<br>    &gt;<br>      &lt;UIcon<br>        :name=&quot;`${<br>          message.role === &#39;user&#39;<br>            ? &#39;i-mdi-user&#39;<br>            : &#39;i-heroicons-sparkles-solid&#39;<br>        }`&quot;<br>        class=&quot;w-8 h-8&quot;<br>        :class=&quot;`${<br>          message.role === &#39;user&#39; ? &#39;text-primary-400&#39; : &#39;text-blue-400&#39;<br>        }`&quot;<br>      /&gt;<br>    &lt;/div&gt;<br>    &lt;div v-if=&quot;message.role === &#39;user&#39;&quot;&gt;<br>      {{ message.content }}<br>    &lt;/div&gt;<br>    &lt;AssistantMessage v-else :content=&quot;message.content&quot; /&gt;<br>  &lt;/div&gt;<br>  &lt;ChatLoadingSkeleton v-if=&quot;loading === &#39;message&#39;&quot; /&gt;<br>  &lt;NoChats v-if=&quot;chatHistory.length === 0&quot; class=&quot;h-full&quot; /&gt;<br>&lt;/div&gt;</pre><p>To keep the user informed of the progress, we show a message loading skeleton when streaming is off. To do this we utilize a loading variable which can have three states: `idle`, `message` &amp; `stream`. So when a non-streaming request is made we set the ref to message and the loading skeleton is shown.</p><p><strong>User Message Textbox</strong></p><p>For entering user message. It shows up as a single line textarea that resizes automatically when needed.</p><pre>&lt;div class=&quot;flex items-start p-3.5 relative&quot;&gt;<br>  &lt;UTextarea<br>    v-model=&quot;userMessage&quot;<br>    placeholder=&quot;How can I help you today?&quot;<br>    class=&quot;w-full&quot;<br>    :ui=&quot;{ padding: { xl: &#39;pr-11&#39; } }&quot;<br>    :rows=&quot;1&quot;<br>    :maxrows=&quot;5&quot;<br>    :disabled=&quot;loading !== &#39;idle&#39;&quot;<br>    autoresize<br>    size=&quot;xl&quot;<br>    @keydown.enter.exact.prevent=&quot;sendMessage&quot;<br>    @keydown.enter.shift.exact.prevent=&quot;userMessage += &#39;\n&#39;&quot;<br>  /&gt;<br><br>  &lt;UButton<br>    icon=&quot;i-heroicons-arrow-up-20-solid&quot;<br>    class=&quot;absolute top-5 right-5&quot;<br>    :disabled=&quot;loading !== &#39;idle&#39;&quot;<br>    @click=&quot;sendMessage&quot;<br>  /&gt;<br>&lt;/div&gt;</pre><p>For allowing the user to send message on hitting enter, we use the keydown event listener with the needed modifiers (@keydown.enter.exact.prevent). Similarly, for adding a newline we use enter + shift keys.</p><p>Now that we have our UI components in place, let’s shift our focus to the backend. We’ll set up the AI integration and create an API endpoint to handle our chat requests. This will bridge the gap between our frontend interface and the AI model.</p><h3>Setting up AI and API Endpoint</h3><p>Integrating AI into our project is very simple thanks to NuxtHub. Let’s look at our nuxt.config.ts file in the root dir of the project.</p><pre>// https://nuxt.com/docs/api/configuration/nuxt-config<br>export default defineNuxtConfig({<br>  compatibilityDate: &#39;2024-07-30&#39;,<br>  // https://nuxt.com/docs/getting-started/upgrade#testing-nuxt-4<br>  future: { compatibilityVersion: 4 },<br><br>  // https://nuxt.com/modules<br>  modules: [&#39;@nuxthub/core&#39;, &#39;@nuxt/eslint&#39;, &#39;@nuxtjs/mdc&#39;, &quot;@nuxt/ui&quot;],<br><br>  // https://hub.nuxt.com/docs/getting-started/installation#options<br>  hub: {},<br><br>  // Env variables - https://nuxt.com/docs/getting-started/configuration#environment-variables-and-private-tokens<br>  runtimeConfig: {<br>    public: {<br>      // Can be overridden by NUXT_PUBLIC_HELLO_TEXT environment variable<br>      helloText: &#39;Hello from the Edge 👋&#39;,<br>    },<br>  },<br><br>  // https://eslint.nuxt.com<br>  eslint: {<br>    config: {<br>      stylistic: {<br>        quotes: &#39;single&#39;,<br>      },<br>    },<br>  },<br><br>  // https://devtools.nuxt.com<br>  devtools: { enabled: true },<br>});</pre><p>To enable AI, we just need to add the ai:true flag under hub config options above. If needed, we can enable other NuxtHub Cloudflare integrations like D1 database (database:true), Workers KV ( kv:true) etc. While we are here, we can remove runtimeConfig as we don’t need it. (You can also remove/modify the eslint config as per your code editor settings).</p><p>If we want to use AI in dev mode (which we definitely do), we need to link this project to a Cloudflare project. This is needed as we will be talking directly to the Cloudflare APIs as no workers are running yet (and also because the AI usage will be linked to your Cloudflare account). This is a straight forward task, just run the following command</p><pre>npx nuxthub link</pre><p>Running the link command will make sure that we are logged into to our NuxtHub account, and will allow us to create a new NuxtHub project (backed by Cloudflare) or choose an existing one.</p><h4>Creating Chat API Endpoint</h4><p>Let’s create a chat api endpoint. Create a new file chat.post.ts (&quot;post” in the file name signifies that this endpoint will only accept HHTP POST requests) in the server/api directory and add the following code to it</p><pre>export default defineEventHandler(async (event) =&gt; {<br>  const { messages, params } = await readBody(event);<br>  if (!messages || messages.length === 0 || !params) {<br>    throw createError({<br>      statusCode: 400,<br>      statusMessage: &#39;Missing messages or LLM params&#39;,<br>    });<br>  }<br><br>  const config = {<br>    max_tokens: params.maxTokens,<br>    temperature: params.temperature,<br>    top_p: params.topP,<br>    top_k: params.topK,<br>    frequency_penalty: params.frequencyPenalty,<br>    presence_penalty: params.presencePenalty,<br>    stream: params.stream,<br>  };<br><br>  const ai = hubAI();<br><br>  try {<br>    const result = await ai.run(params.model, {<br>      messages: params.systemPrompt<br>        ? [{ role: &#39;system&#39;, content: params.systemPrompt }, ...messages]<br>        : messages,<br>      ...config,<br>    });<br><br>    return params.stream<br>      ? sendStream(event, result as ReadableStream)<br>      : (<br>          result as {<br>            response: string;<br>          }<br>        ).response;<br>  } catch (error) {<br>    console.error(error);<br>    throw createError({<br>      statusCode: 500,<br>      statusMessage: &#39;Error processing request&#39;,<br>    });<br>  }<br>});</pre><p>hubAI is a server composable that returns a Workers AI client for interacting with the LLMs. We pass the messages as well as the LLM params that we received from the frontend and run the requested model.</p><p>If you look at the above code carefully you’ll notice that this endpoint supports both: streaming and non-streaming responses. To return a stream from the endpoint we just need to call the sendStream utility function.</p><p>We are through with our server side code. Let’s look at how to handle the stream response on the client side in the next section.</p><h3>Consuming Server Sent Events</h3><p>Before diving into how to handle stream responses, let’s understand why streaming is crucial for LLM interactions and how Server Sent Events (SSE) facilitate this process.</p><h4>Why Stream LLM Responses with Server Sent Events?</h4><p>Large Language Models (LLMs) can take considerable time to generate complete responses, especially for complex queries. Traditionally, web applications wait for the entire response before displaying anything to the user. However, this approach can lead to long waiting times and a less engaging user experience.</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events">Server Sent Events (SSE)</a> offer a solution to this problem.</p><blockquote>💡 SSE is a technology that allows a server to push data to a web page at any time, enabling real-time updates without the need for the client to constantly request new information.</blockquote><p>Here’s how SSE benefits LLM responses:</p><ol><li>Immediate Feedback: As soon as the LLM starts generating content, it can be sent to the client and displayed, providing immediate feedback to the user.</li><li>Improved Perceived Performance: Users see content appearing progressively, giving the impression of a faster, more responsive system.</li><li>Real-time Interaction: The gradual appearance of text mimics human typing, creating a more natural and engaging conversational experience.</li></ol><blockquote>💡 Think of it like ordering at a restaurant: Instead of waiting for all dishes to be prepared before serving, SSE allows the “kitchen” (server) to send out each “dish” (piece of the response) as soon as it’s ready. This way, you start “eating” (reading and processing) sooner, and the overall experience feels faster and more satisfying.</blockquote><p>Technically, SSE works by establishing a unidirectional channel from the server to the client. The server sends text data encoded in UTF-8, separated by newline characters. This simple yet effective mechanism allows for real-time updates in web applications, making it ideal for streaming LLM responses.</p><p>Now that we understand the importance of streaming for LLM responses and how SSE enables this, let’s look at how to implement this in our Nuxt 3 application.</p><h4>Handling Server Sent Events with Nuxt 3 POST Requests</h4><p>Since ours is a POST request, we need handle it differently. <a href="https://nuxt.com/docs/getting-started/data-fetching#consuming-sse-server-sent-events-via-post-request">Nuxt 3 docs</a> gives you an excellent starting point to do this. Reproducing the code from the docs</p><pre>// Make a POST request to the SSE endpoint<br>const response = await $fetch&lt;ReadableStream&gt;(&#39;/chats/ask-ai&#39;, {<br>  method: &#39;POST&#39;,<br>  body: {<br>    query: &quot;Hello AI, how are you?&quot;,<br>  },<br>  responseType: &#39;stream&#39;,<br>})<br><br>// Create a new ReadableStream from the response with TextDecoderStream to get the data as text<br>const reader = response.pipeThrough(new TextDecoderStream()).getReader()<br><br>// Read the chunk of data as we get it<br>while (true) {<br>  const { value, done } = await reader.read()<br><br>  if (done)<br>    break<br><br>  console.log(&#39;Received:&#39;, value)<br>}</pre><p>We need to set the request responseType flag as stream, and set the type of $fetch response as ReadableStream. Then we create a stream reader while decoding the received chunks by piping the events through a TextDecoder.</p><p>Below is a glimpse of the events data received from our chat endpoint (sent by the LLM)</p><pre>data: {“response”:”Hello”,”p”:”abcdefghijklmnopqrstuvwxyz0123456789abcdefghij”}<br><br>data: {&quot;response&quot;:&quot;!&quot;,&quot;p&quot;:&quot;abcdefgh&quot;}<br><br>data: [DONE]</pre><p>These events may contain more than one data line per event, and some events may not contain a complete JSON object (rest of the object will arrive with the next event). To handle this stream we can create a composable with a streamResponse <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*">generator function</a> as shown below</p><pre>export function useChat() {<br>  async function* streamResponse(<br>    url: string,<br>    messages: ChatMessage[],<br>    llmParams: LlmParams<br>  ) {<br>    let buffer = &#39;&#39;;<br><br>    try {<br>      const response = await $fetch&lt;ReadableStream&gt;(url, {<br>        method: &#39;POST&#39;,<br>        body: {<br>          messages,<br>          params: llmParams,<br>        },<br>        responseType: &#39;stream&#39;,<br>      });<br><br>      const reader = response.pipeThrough(new TextDecoderStream()).getReader();<br><br>      while (true) {<br>        const { value, done } = await reader.read();<br><br>        if (done) {<br>          if (buffer.trim()) {<br>            console.warn(&#39;Stream ended with unparsed data:&#39;, buffer);<br>          }<br><br>          return;<br>        }<br><br>        buffer += value;<br>        const lines = buffer.split(&#39;\n&#39;);<br>        buffer = lines.pop() || &#39;&#39;;<br><br>        for (const line of lines) {<br>          if (line.startsWith(&#39;data: &#39;)) {<br>            const data = line.slice(&#39;data: &#39;.length).trim();<br>            if (data === &#39;[DONE]&#39;) {<br>              return;<br>            }<br><br>            try {<br>              const jsonData = JSON.parse(data);<br>              if (jsonData.response) {<br>                yield jsonData.response;<br>              }<br>            } catch (parseError) {<br>              console.warn(&#39;Error parsing JSON:&#39;, parseError);<br>            }<br>          }<br>        }<br>      }<br>    } catch (error) {<br>      console.error(&#39;Error sending message:&#39;, error);<br><br>      throw error;<br>    }<br>  }<br><br>  // For handling non-streaming responses<br>  async function getResponse() {}<br><br>  return {<br>    getResponse,<br>    streamResponse,<br>  };<br>}</pre><p>To handle non streaming responses we can also create a simple $fetch call wrapper function in the same composable (check out the code in the linked Github Repo).</p><p>Now that we understand how to consume Server Sent Events and handle streaming responses, let’s put all the pieces together in our main chat interface. We’ll integrate the components we’ve built, set up the chat API call, and use our `useChat` composable to manage the LLM responses.</p><h4>Final Chat Interface Page</h4><p>The below is the final code of the index page (our app has only one page). Here we use all the components that we created before, and call the chat api endpoint. Then we use the useChat composable to handle the LLM responses.</p><pre>&lt;template&gt;<br>  &lt;div class=&quot;h-screen flex flex-col md:flex-row&quot;&gt;<br>    &lt;USlideover<br>      v-model=&quot;isDrawerOpen&quot;<br>      class=&quot;md:hidden&quot;<br>      :ui=&quot;{ width: &#39;max-w-xs&#39; }&quot;<br>    &gt;<br>      &lt;LlmSettings<br>        v-model:llmParams=&quot;llmParams&quot;<br>        @hide-drawer=&quot;isDrawerOpen = false&quot;<br>        @reset=&quot;resetSettings&quot;<br>      /&gt;<br>    &lt;/USlideover&gt;<br><br>    &lt;div class=&quot;hidden md:block md:w-1/3 lg:w-1/4&quot;&gt;<br>      &lt;LlmSettings v-model:llmParams=&quot;llmParams&quot; @reset=&quot;resetSettings&quot; /&gt;<br>    &lt;/div&gt;<br><br>    &lt;UDivider orientation=&quot;vertical&quot; class=&quot;hidden md:block&quot; /&gt;<br><br>    &lt;div class=&quot;flex-grow md:w-2/3 lg:w-3/4&quot;&gt;<br>      &lt;ChatPanel<br>        :chat-history=&quot;chatHistory&quot;<br>        :loading=&quot;loading&quot;<br>        @clear=&quot;chatHistory = []&quot;<br>        @message=&quot;sendMessage&quot;<br>        @show-drawer=&quot;isDrawerOpen = true&quot;<br>      /&gt;<br>    &lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;<br><br>&lt;script setup lang=&quot;ts&quot;&gt;<br>import type { ChatMessage, LlmParams, LoadingType } from &#39;~~/types&#39;;<br><br>const isDrawerOpen = ref(false);<br><br>const defaultSettings: LlmParams = {<br>  model: &#39;@cf/meta/llama-3.1-8b-instruct&#39;,<br>  temperature: 0.6,<br>  maxTokens: 512,<br>  systemPrompt: &#39;You are a helpful assistant.&#39;,<br>  stream: true,<br>};<br><br>const llmParams = reactive&lt;LlmParams&gt;({ ...defaultSettings });<br>const resetSettings = () =&gt; {<br>  Object.assign(llmParams, defaultSettings);<br>};<br><br>const { getResponse, streamResponse } = useChat();<br>const chatHistory = ref&lt;ChatMessage[]&gt;([]);<br>const loading = ref&lt;LoadingType&gt;(&#39;idle&#39;);<br>async function sendMessage(message: string) {<br>  chatHistory.value.push({ role: &#39;user&#39;, content: message });<br><br>  try {<br>    if (llmParams.stream) {<br>      loading.value = &#39;stream&#39;;<br>      const messageGenerator = streamResponse(<br>        &#39;/api/chat&#39;,<br>        chatHistory.value,<br>        llmParams<br>      );<br><br>      let responseAdded = false;<br>      for await (const chunk of messageGenerator) {<br>        if (responseAdded) {<br>          // add the response to the current message<br>          chatHistory.value[chatHistory.value.length - 1]!.content += chunk;<br>        } else {<br>          // add a new message to the chat history<br>          chatHistory.value.push({<br>            role: &#39;assistant&#39;,<br>            content: chunk,<br>          });<br><br>          responseAdded = true;<br>        }<br>      }<br>    } else {<br>      loading.value = &#39;message&#39;;<br>      const response = await getResponse(<br>        &#39;/api/chat&#39;,<br>        chatHistory.value,<br>        llmParams<br>      );<br><br>      chatHistory.value.push({ role: &#39;assistant&#39;, content: response });<br>    }<br>  } catch (error) {<br>    console.error(&#39;Error sending message:&#39;, error);<br>  } finally {<br>    loading.value = &#39;idle&#39;;<br>  }<br>}<br>&lt;/script&gt;</pre><p>This completes bulk of the coding. The only things remaining are:</p><ol><li>Response parsing for markdown and display</li><li>Auto scrolling the chat container</li></ol><p>Let’s tackle these in the next section.</p><h3>Polishing the Chat Interface</h3><p>We will finish the remaining items in our task list now and call it a day. Stay with me, it won’t take long now.</p><h4>Using Nuxt MDC to Parse &amp; Display Messages</h4><p>If you look at the “Chats Container” code in one of the previous sections you’ll notice a component AssistantMessage for displaying the response. Reproducing the relevant code here</p><pre>&lt;div v-if=&quot;message.role === &#39;user&#39;&quot;&gt;<br>  {{ message.content }}<br>&lt;/div&gt;<br>&lt;AssistantMessage v-else :content=&quot;message.content&quot; /&gt;</pre><p>We use the parseMarkdown utility function from the MDC Module that we included earlier to parse the content, and then use the MDCRenderer component from the same module for displaying it. To take care of streaming response we add a watch for the message content and redo the parsing with useAsyncData composable.</p><pre>&lt;template&gt;<br>  &lt;MDCRenderer class=&quot;flex-1 prose dark:prose-invert&quot; :body=&quot;ast?.body&quot; /&gt;<br>&lt;/template&gt;<br><br>&lt;script setup lang=&quot;ts&quot;&gt;<br>import { parseMarkdown } from &#39;#imports&#39;;<br><br>const props = defineProps&lt;{<br>  content: string;<br>}&gt;();<br><br>const { data: ast, refresh } = await useAsyncData(useId(), () =&gt;<br>  parseMarkdown(props.content)<br>);<br><br>watch(<br>  () =&gt; props.content,<br>  () =&gt; {<br>    refresh();<br>  }<br>);<br>&lt;/script&gt;</pre><blockquote>💡 We could have used the &lt;MDC&gt; component instead of using parseMarkdown + MDCRenderer combination. &lt;MDC&gt; handles the parsing internally using the same parseMarkdown function, then why we didn’t use it?</blockquote><p>The <em>&lt;MDC&gt;</em> component was causing overwriting of previous messages that were similar (the start of the message) to the latest message from the API endpoint. This happens because we don’t have a unique key in our messages. Here is the relevant code from the <em>&lt;MDC&gt;</em> component.</p><pre>const key = computed(() =&gt; hash(props.value))<br><br>const { data, refresh, error } = await useAsyncData(key.value, async () =&gt; {<br>  if (typeof props.value !== &#39;string&#39;) {<br>    return props.value<br>  }<br>  return await parseMarkdown(props.value, props.parserOptions)<br>})</pre><p>As you can see, the key for useAsyncData is generated based on the content props value. So if the server streaming responses start with the same letters (e.g., Here’s a joke…, Here’s another…), the key will be same, and all similar previous responses from the server gets overwritten in the UI.</p><p>In the AssistantMessage component we are using useId composable to generate a unique id for each useAsyncData call, so the issue doesn’t occur.</p><h4>Using MutationObserver to Auto Scroll the Chats Container</h4><p>In the ChatPanel component, the only scrolling part is the chats container. There can be other ways to observe the changes in the container but the simplest one I found is a <a href="https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver">MutationObserver</a>.</p><blockquote>💡 The MutationObserver interface provides the ability to watch for changes being made to the DOM tree.</blockquote><p>In the case of streaming, we keep appending new content to the latest message that is being displayed. So to handle this effectively, we create a MutationObserver, give it a target to watch (the ChatsContainer), and some criteria to watch for (childList, subtree &amp; characterData).</p><p>Here is the relevant code to do this</p><pre>const chatContainer = ref&lt;HTMLElement | null&gt;(null);<br>let observer: MutationObserver | null = null;<br><br>onMounted(() =&gt; {<br>  if (chatContainer.value) {<br>    observer = new MutationObserver(() =&gt; {<br>      if (chatContainer.value) {<br>        chatContainer.value.scrollTop = chatContainer.value.scrollHeight;<br>      }<br>    });<br><br>    observer.observe(chatContainer.value, {<br>      childList: true,<br>      subtree: true,<br>      characterData: true,<br>    });<br>  }<br>});<br><br>onUnmounted(() =&gt; {<br>  if (observer) {<br>    observer.disconnect();<br>  }<br>});</pre><p>When the <strong>ChatsPanel</strong> gets mounted we create a new <strong>MutationObserver</strong> which when based on the given criteria, we make the chats container scroll to its fullest.</p><h4>Bonus: Handling the Dark Mode</h4><p>As previously mentioned, the dark mode is already working; we just need a way to toggle it. We can also change the flavor of gray we want in the app. This can be done by adding an app.config.ts file in the app directory.</p><pre>// app.config.ts<br>export default defineAppConfig({<br>  ui: {<br>    primary: &#39;orange&#39;,<br>    gray: &#39;slate&#39;,<br>  },<br>});</pre><p>Add the following in your app.vue file for setting the required background colors.</p><pre>&lt;script setup lang=&quot;ts&quot;&gt;<br>useHead({<br>  bodyAttrs: {<br>    class: &#39;bg-white dark:bg-gray-900&#39;,<br>  },<br>});<br>&lt;/script&gt;</pre><p>And add a new ColorMode component in your app/components directory.</p><pre>&lt;template&gt;<br>  &lt;ClientOnly&gt;<br>    &lt;UButton<br>      :icon=&quot;isDark ? &#39;i-heroicons-moon-20-solid&#39; : &#39;i-heroicons-sun-20-solid&#39;&quot;<br>      color=&quot;gray&quot;<br>      variant=&quot;ghost&quot;<br>      aria-label=&quot;Theme&quot;<br>      @click=&quot;isDark = !isDark&quot;<br>    /&gt;<br><br>    &lt;template #fallback&gt;<br>      &lt;div class=&quot;w-8 h-8&quot; /&gt;<br>    &lt;/template&gt;<br>  &lt;/ClientOnly&gt;<br>&lt;/template&gt;<br><br>&lt;script setup lang=&quot;ts&quot;&gt;<br>const colorMode = useColorMode();<br><br>const isDark = computed({<br>  get() {<br>    return colorMode.value === &#39;dark&#39;;<br>  },<br>  set() {<br>    colorMode.preference = colorMode.value === &#39;dark&#39; ? &#39;light&#39; : &#39;dark&#39;;<br>  },<br>});<br>&lt;/script&gt;</pre><p>Now you can use this color mode toggle button anywhere you like. In our app we’ve added it in the ChatHeader component.</p><p>Phew! And we have completed all the tasks we had set out to complete at the beginning of this article.</p><h3>Deploying the App</h3><p>You can deploy the project in multiple ways. I would recommend to do it through the NuxtHub Admin console. Push your code to a Github repository, link this repository with NuxtHub and then deploy from the Admin console.</p><p>But if you want to see it live right away then you can use the below command</p><pre>npx nuxthub deploy</pre><blockquote>💡 If you do your first deployment with the NuxtHub CLI, you won’t be able to attach your GitHub/GitLab repository later on due to a Cloudflare limitation.</blockquote><p>For more details on deployment you can visit the <a href="https://hub.nuxt.com/docs/getting-started/deploy">NuxtHub Docs</a>.</p><h3>Source Code</h3><p>I only covered the most important parts of the source code here. You can visit the linked GIthub Repo for the complete source code. It should be self explanatory, but if you have question then please feel free to drop a comment below.</p><p><a href="https://github.com/ra-jeev/hub-chat">GitHub - ra-jeev/hub-chat: Cloudflare Workers AI LLM Playground with Nuxt</a></p><h3>Conclusion</h3><p>Congratulations! You’ve successfully built a feature-rich LLM playground using Nuxt 3, NuxtUI, and Cloudflare Workers AI through NuxtHub. We’ve covered a wide range of topics, including:</p><ul><li>Handling streaming responses using Server Sent Events</li><li>Parsing and displaying markdown content in chat messages</li><li>Implementing auto-scrolling for a better user experience etc.</li></ul><p>You can take this project as the starting point and improve it further by:</p><ul><li>Adding the ability to talks to other types of LLMs, e.g. text-to-image, image-to-text, speech recognition etc.</li><li>Implementing user authentication and session management</li><li>Adding support for multiple conversation threads etc.</li></ul><p>Thank you for staying till the end. I hope you learned some new concepts from this article. Do let me know your learnings in the comments section. Your feedback and experiences are valuable not just to me, but to the entire community of developers exploring this fascinating field.</p><p>Until next time!</p><blockquote>Keep adding the bits and soon you’ll have a lot of bytes to share with the world.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d1961e77bbe2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Beyond Gut Feeling: Leveraging AI for Smarter Domain Name Decisions]]></title>
            <link>https://medium.com/@rajeevs/beyond-gut-feeling-leveraging-ai-for-smarter-domain-name-decisions-1fe3947e2abc?source=rss-9352fec71fae------2</link>
            <guid isPermaLink="false">https://medium.com/p/1fe3947e2abc</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[anthropic-claude]]></category>
            <category><![CDATA[nuxtjs]]></category>
            <category><![CDATA[nuxt]]></category>
            <dc:creator><![CDATA[Rajeev R Sharma]]></dc:creator>
            <pubDate>Thu, 01 Aug 2024 07:15:13 GMT</pubDate>
            <atom:updated>2024-08-01T07:15:13.668Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NrYfcRVGRXS1cFqltzrGqw.png" /></figure><p>If you work in tech-related fields, sooner or later there will come a time when you have to pick a domain name for the idea you’ve been working on. Whether it’s an app or a service, it still needs a name. And if you’re anything like me, this is one of the trickiest parts of building that idea. The perfect domain name can make your project more memorable, boost SEO, and even influence user trust. But how do you know if you’re making the right choice?</p><p>Traditionally, selecting a domain name has been a mix of creativity, gut feeling, and perhaps a quick poll among people in your circle of influence. The post-GPT era might have added another ingredient to the mix: consulting your trusted AI/LLM. But if you have talked to this advisor, it generally throws some names at you without revealing why it chose these names. And this is where <strong>”Name Insights”</strong> steps in, offering not just suggestions, but insights into the ‘why’ behind those domain name choices.</p><h3>Introduction</h3><p>I have been thinking about domain names (hoarding to be frank) for quite some time now. Maybe because I have bought more domains in the recent past than my usual appetite allows me to. And the first step to this whole process is: knowing the name that you want to buy.</p><p>After the usual availability checks, you wonder whether that weird-looking name is right for the purpose you’re buying it for. Let’s be honest, all the good ones are already taken (why does it feel like I’m not writing a tech article?) or the price is simply beyond your reach. Sometimes you zero down on more than one name, so which one should you go ahead with? These were the guiding questions behind building Name Insights.</p><p>Name Insights aims to address these challenges by providing detailed, contextual answers to help you make informed decisions about domain names.</p><h3>App Features</h3><p>Name Insights offers three powerful services to assist in your domain name decision-making process. Each service utilizes an AI-driven scoring system that evaluates domains based on six key aspects of what makes a good domain name. Let’s explore each feature:</p><h4>Name Insights &amp; Scoring</h4><p>This feature provides an unbiased view of what kind of app or service is best suited for a given domain name. It:</p><ul><li>Evaluates the domain on 6 different parameters, namely:<br>1. Brand Impact: Memorability, brandability, uniqueness, emotional appeal. <br>2. Usability: Length, spelling simplicity, pronunciation clarity, absence of numbers/hyphens.<br>3. Relevance and SEO: Relevance to purpose, keyword inclusion, extension potential.<br>4. Technical Considerations: TLD appropriateness, potential social media availability.<br>5. Legal and Cultural Factors: Potential trademark risks, cultural/linguistic considerations.<br>6. Market Potential: Ability to target desired audience, scalability for business growth.</li><li>Provides an overall score based on weighted parameters.</li><li>Offers a brief explanation for each score.</li><li>Highlights pros and cons of the domain name.</li></ul><h4>Domain Names Comparison</h4><p>This feature helps you choose between different options you might have. It:</p><ul><li>Compares two different domain names</li><li>Uses the same scoring strategy as the insights feature</li><li>Determines a “winning” name</li><li>Provides a brief summary explaining the choice</li></ul><h4>Domain Name Ideas</h4><p>This feature is most useful when you’re starting from scratch. It:</p><ul><li>Builds upon the same domain name scoring strategy, and offers five distinct domain name suggestions based on your app/service idea</li><li>Provides an overall score for each suggestion</li><li>Breaks down different category scores</li><li>Offers a brief summary of strengths and weaknesses for each name</li></ul><h3>App Results Screenshots</h3><p>Here are some screenshots of the app in action.</p><h4>Name Insights Page (for dev.to domain)</h4><figure><img alt="Domain name insights for the dev.to domain" src="https://cdn-images-1.medium.com/max/1024/1*aV8gnQEZ4DIn259gnfaRvg.png" /><figcaption>Domain name insights for the dev.to domain</figcaption></figure><h4>Name Comparison results for similar sounding domains</h4><figure><img alt="Comparison between two domains" src="https://cdn-images-1.medium.com/max/1024/1*yH2Jlnpb4fC5f7c-1tbIaQ.png" /><figcaption>Comparison between two domains</figcaption></figure><h4>Name ideas results for a generic code snippets vault app</h4><figure><img alt="Domain name ideas for a code snippets service" src="https://cdn-images-1.medium.com/max/1024/1*A3-Pgz5YjO5INqQVKmF-eg.png" /><figcaption>Domain name ideas for a code snippets service</figcaption></figure><h3>Technical Details</h3><p>Here is a brief overview of the app stack and approach</p><ol><li><strong>Nuxt3:</strong> Name Insights uses the full-stack capabilities of Nuxt3 as its backbone for a faster turnaround time. You can achieve the same results using another framework of your choice, but for me it made more sense because of where it is hosted (see point 2).</li><li><strong>NuxtHub:</strong> The app is hosted on Cloudflare Pages using <a href="https://hub.nuxt.com/">NuxtHub</a>. NuxtHub offers a great DX for hosting serverless Nuxt apps, and for talking to various Cloudflare services. At the time of writing this article it supports Cloudflare features such as KV, D1, and R2.</li><li><strong>NuxtUI:</strong> The app UI is built using <a href="https://ui.nuxt.com/">NuxtUI</a>, a collection of prebuilt components based on HeadlessUI &amp; TailwindCSS.</li><li><strong>Claude 3.5 Sonnet / Anthropic AI:</strong> The core features of the app are thanks to the Anthropic AI SDK, using the Claude 3.5 Sonnet model. The best in class reasoning capabilities of the model allows for a good overall experience of the app features.</li></ol><p>At the heart of the app features is the domain scoring strategy. At present, the app relies on the advanced reasoning capabilities of the <strong>Claude 3.5 Sonnet</strong> model to grade a domain name. The key to the implementation lies in carefully crafted system prompts (which you call Prompt Engineering). These prompts instruct the AI on how to analyze domain names, what factors to consider, and how to present the results. E.g. the below is the core part of the system prompt for scoring a domain and generating insights:</p><pre>const nameScorePrompt = `You are an AI assistant specialized in <br>evaluating and scoring domain names. Analyze the given domain name <br>as if you&#39;re seeing it for the very first time, without any prior <br>knowledge of its actual use or purpose.<br><br>Evaluate the domain based on these categories and weights:<br>1. Brand Impact (30%)<br>2. Usability (20%)<br>3. Relevance and SEO (20%)<br>4. Technical Considerations (15%)<br>5. Legal and Cultural Factors (10%)<br>6. Market Potential (5%)<br><br>Provide a score out of 100 for each category, along with a brief, <br>unbiased explanation. Calculate the weighted overall score out of 100 <br>(rounded to the nearest integer). Include concise lists of strengths <br>and weaknesses of the domain name based solely on its characteristics, <br>not its known use.`;<br><br>const response = await this.anthropic.messages.create({<br>  model: &quot;claude-3-5-sonnet-20240620&quot;,<br>  max_tokens: 1000,<br>  temperature: 0.4,<br>  system: systemPrompt,<br>  messages: [<br>    {<br>      role: &quot;user&quot;,<br>      content: `Analyze the domain name: ${domainName}`,<br>    },<br>  ],<br>});</pre><p>The prompt is intentionally detailed and specific to get the desired results from the LLM. We incorporate the required JSON structure into the prompt to receive the output in JSON format. This output is then parsed using the following function:</p><pre>export function extractAndParseJson&lt;T&gt;(text: string): T {<br>  try {<br>    return JSON.parse(text) as T;<br>  } catch (error) {<br>    console.error(&quot;Error parsing JSON:&quot;, error);<br>  }<br><br>  const backtickPattern = /```(?:json)?\s*([\s\S]*?)\s*```/g;<br>  const matches = text.match(backtickPattern);<br><br>  if (matches) {<br>    for (const match of matches) {<br>      const content = match.replace(backtickPattern, &quot;&quot;).trim();<br>      try {<br>        return JSON.parse(content) as T;<br>      } catch (error) {<br>        continue;<br>      }<br>    }<br>  }<br><br>  throw new Error(&quot;No valid JSON found in the text&quot;);<br>}</pre><p>To try out different LLMs from other AI services, the backend implementation is kept generic using interfaces which every AI service needs to implement and extend.</p><p>Here is the <strong>AIService</strong> interface:</p><pre>export interface AIService {<br>  getDomainScore(domainName: string): Promise&lt;string&gt;;<br>  compareDomains(firstDomain: string, secondDomain: string): Promise&lt;string&gt;;<br>  getDomainSuggestions(purpose: string): Promise&lt;string&gt;;<br>}</pre><p>And the <strong>BaseAIService</strong></p><pre>export abstract class BaseAIService implements AIService {<br>  protected getSystemPrompt(promptType: SystemPromptType): string {<br>    return getSystemPrompt(promptType);<br>  }<br><br>  abstract getDomainScore(domainName: string): Promise&lt;string&gt;;<br><br>  abstract compareDomains(<br>    firstDomain: string,<br>    secondDomain: string<br>  ): Promise&lt;string&gt;;<br><br>  abstract getDomainSuggestions(purpose: string): Promise&lt;string&gt;;<br>}</pre><p>This approach provides a common system prompt for all AI services while allowing individual services the flexibility to override and define their own custom system prompts if needed.</p><h3>App &amp; Repo Links</h3><p>You can try out the app live at: <a href="https://name-insights.nuxt.dev/](https://name-insights.nuxt.dev/)">https://name-insights.nuxt.dev/</a></p><p>The complete app code can be found here:</p><p><a href="https://github.com/ra-jeev/domain-name-insights">GitHub - ra-jeev/domain-name-insights</a></p><h3>Limitations</h3><p>As the app relies entirely on an LLM for its core functionality, the output is not deterministic. This means that if you analyze the same domain name multiple times, you may receive slightly different scores. This variability is inherent to large language models, which can produce different outputs based on subtle differences in how they process the input each time.</p><p>However, in my testing, I’ve found that this limitation doesn’t significantly impact the app’s utility. The variance in scores tends to be low, and the overall insights remain consistent. Name Insights is an excellent sounding board for giving you a head start in your domain name search.</p><h3>Further Enhancements</h3><p>This is just the beginning. Below are some of the actions items that can further enhance the app outcome and usefulness:</p><ol><li>Integrating other LLMs and utilize multiple models simultaneously for arriving at a score and generating insights</li><li>Adding real world data to the mix. This will make the scoring more trustworthy and accurate.</li><li>Adding real time domain name availability checks, and possibly the social media handles.</li></ol><h3>Conclusion</h3><p>Selecting the perfect domain name is a critical step in establishing your online presence. With its three distinct features Name Insights aims to make the process easier at various stages of the domain name search journey. It helps you to:</p><ul><li>Gain objective insights into the strengths and weaknesses of potential domain names</li><li>Make informed comparisons between different options</li><li>Generate fresh ideas tailored to your specific needs</li></ul><p>Irrespective of the slight variability in results, the overall consistency and depth of analysis provided by Name Insights make it an invaluable resource for smarter domain name decisions.</p><p>I hope you liked reading the article. Do try out the app and it would mean the world to me if you can share your feedback with me.</p><p>Until next time…!</p><blockquote>Keep adding the bits and soon you’ll have a lot of bytes to share with the world.</blockquote><p>Follow me on Twitter/X: <a href="https://x.com/ra_jeeves">https://x.com/ra_jeeves</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1fe3947e2abc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>