<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Raja Osama</title>
    <description>The latest articles on DEV Community by Raja Osama (@rajaosama).</description>
    <link>https://dev.to/rajaosama</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F279272%2F6881a4d4-7d36-47df-9461-dbbeec0e2dad.png</url>
      <title>DEV Community: Raja Osama</title>
      <link>https://dev.to/rajaosama</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rajaosama"/>
    <language>en</language>
    <item>
      <title>GPT Models Can Do More Than Just Talk: How to Make Them Brew You Coffee ☕</title>
      <dc:creator>Raja Osama</dc:creator>
      <pubDate>Thu, 15 Jun 2023 08:43:54 +0000</pubDate>
      <link>https://dev.to/rajaosama/gpt-models-can-do-more-than-just-talk-how-to-make-them-brew-you-coffee-4pmm</link>
      <guid>https://dev.to/rajaosama/gpt-models-can-do-more-than-just-talk-how-to-make-them-brew-you-coffee-4pmm</guid>
      <description>&lt;h3&gt;
  
  
  Intent Classification 🎯
&lt;/h3&gt;

&lt;p&gt;Intent classification is the task of figuring out what the user wants to achieve or do from their natural language input. For example, if the user says "Can you make me a coffee?", the intent could be "Make a coffee". This way, we can map the user's input to a specific action or function that we want our GPT model to perform.&lt;/p&gt;

&lt;p&gt;To do this, I used Ada Models, &lt;a href="https://medium.com/javascript-in-plain-english/how-to-embed-search-with-text-embedding-ada-002-in-node-js-5605c8d08815"&gt;which I wrote about in a previous article.&lt;/a&gt; Ada Models are models that return the embedding of the text, which is a numerical representation of its meaning. The embedding captures the semantic and syntactic features of the text, such as the words, phrases, and context.&lt;/p&gt;

&lt;p&gt;By using a simple formula, we can find the most relatable of the embedding among a set of predefined categories, which indicates the most likely intent. The formula is based on the Euclidean distance, which measures how close two vectors are in a multidimensional space. The smaller the distance, the more similar the vectors are.&lt;/p&gt;

&lt;p&gt;&lt;a href="//images.ctfassets.net/k07f0awoib97/McW3ceYTm307MBmwtMKle/84a12dd06ec7bb215f1aa23a9ddcdaf9/1_d23UD_OMKKiv2qpsHDL1Rw__1_.png" class="article-body-image-wrapper"&gt;&lt;img src="//images.ctfassets.net/k07f0awoib97/McW3ceYTm307MBmwtMKle/84a12dd06ec7bb215f1aa23a9ddcdaf9/1_d23UD_OMKKiv2qpsHDL1Rw__1_.png" alt="1 d23UD OMKKiv2qpsHDL1Rw (1)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For example, if we have three categories: "Make a coffee", "Make a tea", and "Make a sandwich", and we have an embedding for each category, we can compare the embedding of the user's input with each category embedding and find the one with the smallest distance. If the user says "Can you brew me a coffee?", the embedding of their input will be closer to the embedding of "Make a coffee" than to the other two categories, so we can infer that their intent is "Make a coffee".&lt;/p&gt;

&lt;p&gt;A sample of how we can find the intent can be found by categorizing the intents into actions. So for example, if you have an action like &lt;code&gt;Make a coffee&lt;/code&gt;, you can define it as a category with a user-provided prompt and a function to execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Make a coffee =&amp;gt; (User Provided Prompt = "Can you make me a coffee?") =&amp;gt; MakeACoffee()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, to increase the accuracy and flexibility of the system, we don't just define one category for each action, but more different ways of expressing the same intent. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Make me a coffee =&amp;gt; (User Provided Prompt = "Can you make me a coffee?") =&amp;gt; MakeACoffee()\
Bring me a coffee =&amp;gt; (User Provided Prompt = "Can you bring me a coffee?") =&amp;gt; MakeACoffee()\
Buy me a coffee =&amp;gt; (User Provided Prompt = "Can you buy me a coffee?") =&amp;gt; MakeACoffee()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, all these categories will trigger the same function: MakeACoffee().&lt;/p&gt;

&lt;h3&gt;
  
  
  Rule-Based System 📜
&lt;/h3&gt;

&lt;p&gt;The rule-based system is like any other chatbot system, but ours will be very dynamic and powerful and use AI from start to end. The concept is to make a list of categories as keys and objects as values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;intents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;categories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Make a coffee&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MakeACoffee&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Make me a coffee&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MakeACoffee&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bring me a coffee&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MakeACoffee&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Buy me a coffee&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MakeACoffee&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each object has an action, which can be either an API call or something else, a URL to call if it's an API call and a response function to handle the result of the call. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MakeACoffee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="na"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;LOCAL_ACTION&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="na"&gt;response&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Your coffee is here!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A function would make a fetch request and return the response. You can customize the response as much as you like.&lt;/p&gt;

&lt;p&gt;Now whenever you ask for, &lt;code&gt;Make me a coffee&lt;/code&gt; Your result would be your coffee is here. &lt;/p&gt;

&lt;p&gt;But what if you have follow-up questions or options? How would you handle that?&lt;/p&gt;

&lt;p&gt;For example, when you ask the system to makeACoffee, you might want to handle what kind of coffee use case. To do that, we will have a recursive approach.&lt;/p&gt;

&lt;p&gt;Each object can also have an ask function to generate a follow-up question and a categories object to define the possible options for the user. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MakeACoffee = {
  action: "API_CALL",
  url: "http://localhost:3000/coffees",
  ask: [
    (coffees) =&amp;gt; {
      return `What kind of coffee would you like ${coffees.map(
        (coffee) =&amp;gt; coffee
      )}?`;
    },
  ],
  categories: () =&amp;gt; ({
    Mocha: MochaCoffee,
    Karak: KarakCoffee,
    Espresso: EspressoCoffee,
  }),
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now with this approach, we will have a list of coffees in the response with a follow-up question and another object to handle the user's choice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MochaCoffee = {
  action: "LOCAL_ACTION",
  response: (response) =&amp;gt; "Your mocha coffee is here!",
};

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

&lt;/div&gt;



&lt;p&gt;And that's it! You have successfully integrated your GPT model with your own data sources and actions. You can now enjoy your coffee while chatting with your AI friend. ☕&lt;/p&gt;

&lt;h3&gt;
  
  
  Behind the Scenes 🕵️‍♂️
&lt;/h3&gt;

&lt;p&gt;Now let's look at the behind the scene working of this application. I showed you the configuration, but let's look at the internals.&lt;/p&gt;

&lt;p&gt;The second part of this article is here : &lt;a href="https://rajaosama.me/blogs/gpt-models-can-do-more-than-just-talk-how-to-make-them-brew-you-coffee"&gt;GPT Models Can Do More Than Just Talk: How to Make Them Brew You Coffee&lt;/a&gt; ☕&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Blazing Fast Search with text-embedding-ada-002 in Node.js 🚀</title>
      <dc:creator>Raja Osama</dc:creator>
      <pubDate>Wed, 14 Jun 2023 10:57:30 +0000</pubDate>
      <link>https://dev.to/rajaosama/blazing-fast-search-with-text-embedding-ada-002-in-nodejs-4abc</link>
      <guid>https://dev.to/rajaosama/blazing-fast-search-with-text-embedding-ada-002-in-nodejs-4abc</guid>
      <description>&lt;p&gt;Do you want to create amazing applications that can search, cluster, recommend, and classify text and code? Do you want to use a powerful and cost-effective embedding model that can handle long documents and diverse tasks? Do you want to learn how to use text-embedding-ada-002 in Node.js with ease and simplicity? If you answered yes to any of these questions, then this article is for you! 😍&lt;/p&gt;

&lt;p&gt;In this article, we will show you how to use text-embedding-ada-002, a new and improved embedding model from OpenAI, in Node.js. We will explain what embeddings are, why they are useful, and how text-embedding-ada-002 outperforms previous models. We will also show you how to install the OpenAI library, and make requests to the /createEmbedding endpoint. Finally, we will give you one examples of how to use text-embedding-ada-002 for different use cases, such as search.&lt;/p&gt;

&lt;p&gt;Complete Article can be found here &lt;a href="https://rajaosama.me/blogs/blazing-fast-search-with-embedding-model-ada"&gt;https://rajaosama.me/blogs/blazing-fast-search-with-embedding-model-ada&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What are embeddings? 🤔
&lt;/h2&gt;

&lt;p&gt;Embeddings are numerical representations of concepts converted to number sequences, which make it easy for computers to understand the relationships between those concepts. For example, the word "cat" can be represented by a vector of numbers, such as [0.2, -0.5, 0.7 ...]. The distance between two vectors measures their relatedness. Small distances suggest high relatedness and large distances suggest low relatedness.&lt;/p&gt;

&lt;p&gt;Embeddings are commonly used for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Search&lt;/strong&gt; (where results are ranked by relevance to a query string)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clustering&lt;/strong&gt; (where text strings are grouped by similarity)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recommendations&lt;/strong&gt; (where items with related text strings are recommended)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Anomaly detection&lt;/strong&gt; (where outliers with little relatedness are identified)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Diversity measurement&lt;/strong&gt; (where similarity distributions are analyzed)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Classification&lt;/strong&gt; (where text strings are classified by their most similar label)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why text-embedding-ada-002? 🙌
&lt;/h2&gt;

&lt;p&gt;text-embedding-ada-002 is a new embedding model from OpenAI that replaces five separate models for text search, text similarity, and code search. It outperforms the previous most capable model, Davinci, at most tasks, while being priced 99.8% lower¹. Here are some of the advantages of text-embedding-ada-002:&lt;/p&gt;

&lt;p&gt;&lt;a href="//images.ctfassets.net/k07f0awoib97/2n4uIQh2bAX7fRmx4AGzyY/a1bc6fa1e2d14ff247716b5f589a2099/Screen_Recording_2023-06-03_at_4.52.54_PM.gif" class="article-body-image-wrapper"&gt;&lt;img src="//images.ctfassets.net/k07f0awoib97/2n4uIQh2bAX7fRmx4AGzyY/a1bc6fa1e2d14ff247716b5f589a2099/Screen_Recording_2023-06-03_at_4.52.54_PM.gif" alt="Embedding"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Stronger performance&lt;/strong&gt;. text-embedding-ada-002 outperforms all the old embedding models on text search, code search, and sentence similarity tasks and gets comparable performance on text classification¹.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unification of capabilities&lt;/strong&gt;. text-embedding-ada-002 simplifies the interface of the /createEmbedding endpoint by merging the five separate models into a single new model. This single representation performs better than the previous embedding models across a diverse set of text search, sentence similarity, and code search benchmarks¹.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Longer context&lt;/strong&gt;. The context length of text-embedding-ada-002 is increased by a factor of four, from 2048 to 8192 tokens¹, making it more convenient to work with long documents.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Smaller embedding size&lt;/strong&gt;. The new embeddings have only 1536 dimensions¹, one-eighth the size of davinci embeddings², making them more cost effective in working with vector databases.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reduced price&lt;/strong&gt;. The price of text-embedding-ada-002 is reduced by 90% compared to old models of the same size². The new model achieves better or similar performance as Davinci at a 99.8% lower price¹.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall, text-embedding-ada-002 is a much more powerful tool for natural language processing and code tasks. It can help you create even more capable applications in your respective fields.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to set up Node.js? 💻
&lt;/h2&gt;

&lt;p&gt;Node.js is an open-source and cross-platform JavaScript runtime environment⁵ that allows you to run JavaScript code outside the browser. To use text-embedding-ada-002 in Node.js, you need to set up your Node.js environment first.&lt;/p&gt;

&lt;p&gt;To install Node.js on your machine, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to the official Node.js website⁶ and download the installer for your operating system.&lt;/li&gt;
&lt;li&gt;Run the installer and follow the instructions on the screen.&lt;/li&gt;
&lt;li&gt;To verify that Node.js is installed correctly, open a terminal or command prompt window and type &lt;code&gt;node -v&lt;/code&gt;. You should see something like &lt;code&gt;v18.16.0&lt;/code&gt; printed on the screen.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How to install OpenAI API library? 📚
&lt;/h2&gt;

&lt;p&gt;The OpenAI library is a wrapper around the OpenAI API that makes it easy to use in different programming languages. To use text-embedding-ada-002 in Node.js, you need to install the OpenAI API library for Node.js.&lt;/p&gt;

&lt;p&gt;To install the OpenAI API library for Node.js using npm (Node Package Manager), follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open a terminal or command prompt window and navigate to your project directory.&lt;/li&gt;
&lt;li&gt;Type &lt;code&gt;npm install openai&lt;/code&gt; and press enter.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How to make requests to /createEmbedding endpoint? 📨
&lt;/h2&gt;

&lt;p&gt;To get an embedding from text-embedding-ada-002 in Node.js using the OpenAI API library,&lt;br&gt;
follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Import the OpenAI API library by typing &lt;code&gt;const openai = require('openai');&lt;/code&gt; at the top of your JavaScript file.&lt;/li&gt;
&lt;li&gt;Create an instance of the OpenAI class by typing &lt;code&gt;const OPENAI_API_KEY = 'sk-your-api-key';&lt;/code&gt; and 
&lt;code&gt;const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const configuration = new Configuration({
apiKey: OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);&lt;/code&gt; 
where you replace &lt;code&gt;your-api-key&lt;/code&gt; with your actual API key that you can get from your account page⁷.&lt;/li&gt;
&lt;li&gt;Create an input object that contains your input string and the model ID by typing &lt;code&gt;const input = {input: 'Your input string goes here', model: 'text-embedding-ada-002'};&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Call the &lt;code&gt;createEmbedding&lt;/code&gt; method of the OpenAI class by typing &lt;code&gt;openai.createEmbedding(input).then(response =&amp;gt; { // do something with response });&lt;/code&gt;. The response object will contain an embedding array that you can extract,
save,
and use.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  How to use text-embedding-ada-002 for different use cases? 🚀
&lt;/h2&gt;

&lt;p&gt;Now that you know how to get an embedding from text-&lt;br&gt;
embedding-&lt;br&gt;
ada-&lt;br&gt;
002 in Node.js,&lt;br&gt;
let's see some examples of how to use it for different use cases.&lt;/p&gt;

&lt;p&gt;Complete Article can be found here &lt;a href="https://rajaosama.me/blogs/blazing-fast-search-with-embedding-model-ada"&gt;https://rajaosama.me/blogs/blazing-fast-search-with-embedding-model-ada&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Search 🔎
&lt;/h3&gt;

&lt;p&gt;One common use case for embeddings is search,&lt;br&gt;
where you want to rank results by relevance to a query string.&lt;br&gt;
For example,&lt;br&gt;
suppose you have a collection of documents about animals,&lt;br&gt;
and you want to find the most relevant ones for a given query.&lt;/p&gt;

&lt;p&gt;To do this,&lt;br&gt;
you can follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Get embeddings for all your documents using text-
embedding-
ada-
002
and store them in a vector database,
such as Elasticsearch⁸.&lt;/li&gt;
&lt;li&gt;Get an embedding for your query string using text-
embedding-
ada-
002
and send it to your vector database.&lt;/li&gt;
&lt;li&gt;Retrieve the documents with the smallest distances (or highest similarities) to your query embedding
and display them as your search results.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is some pseudocode that illustrates this process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Import libraries&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Configuration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OpenAIApi&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;openai&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@elastic/elasticsearch&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;node&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://localhost:9200&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;elastic&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ELASTIC_SEARCH&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Test the connection&lt;/span&gt;
&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ping&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Connection failed:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Connection successful:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Create instances&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;OPENAI_API_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;OPENAI_API_KEY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;configuration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Configuration&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;OPENAI_API_KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;openai&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;OpenAIApi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;configuration&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createIndices&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pets&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;mappings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;properties&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="c1"&gt;// The document field stores the text of the animal&lt;/span&gt;
          &lt;span class="na"&gt;document&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt;
          &lt;span class="c1"&gt;// The embedding field stores the vector representation of the animal&lt;/span&gt;
          &lt;span class="na"&gt;embedding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dense_vector&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;dims&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1536&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;similarity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cosine&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createIndexs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// // Get embeddings for documents&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;documents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A cat is a domesticated animal that likes to sleep.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A dog is a loyal companion that likes to play.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A bird is a feathered creature that likes to fly.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A fish is an aquatic animal that likes to swim.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A lion is a wild animal that likes to hunt.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// sample documents&lt;/span&gt;
  &lt;span class="nx"&gt;documents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text-embedding-ada-002&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nx"&gt;openai&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createEmbedding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;embedding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;embedding&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// extract embedding array&lt;/span&gt;
        &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
          &lt;span class="c1"&gt;// store document and embedding in Elasticsearch&lt;/span&gt;
          &lt;span class="na"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pets&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="c1"&gt;//   type: 'document',&lt;/span&gt;
          &lt;span class="c1"&gt;//   id: index,&lt;/span&gt;
          &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;document&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;embedding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;embedding&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Failed once, dont try again&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Get embedding for query&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What animal likes water?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// sample query&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text-embedding-ada-002&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="nx"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createEmbedding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;query_embedding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;embedding&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// extract query embedding array&lt;/span&gt;

    &lt;span class="c1"&gt;// Search documents by query embedding&lt;/span&gt;
    &lt;span class="nx"&gt;client&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pets&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;knn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;field&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;embedding&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;query_vector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;query_embedding&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Return the top 3 nearest neighbors&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;num_candidates&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hits&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// get the matched documents&lt;/span&gt;
        &lt;span class="nx"&gt;hits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;hit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// print the document text&lt;/span&gt;
          &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_score&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// print the similarity score&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;main&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;createIndices&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;createIndexs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;main&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of this code might look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A fish is an aquatic animal that likes to swim.
1.9999999
A bird is a feathered creature that likes to fly.
1.0000001
A dog is a loyal companion that likes to play.
0.99999994
A cat is a domesticated animal that likes to sleep.
0.9999999
A lion is a wild animal that likes to hunt.
0.9999998
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the fish document is the most relevant one for the query, followed by the bird document. The other documents have lower similarity scores and are less relevant.&lt;/p&gt;

&lt;p&gt;More Examples and usecases of using text-embedding-ada-002 can be found here &lt;a href="https://rajaosama.me/blogs/blazing-fast-search-with-embedding-model-ada"&gt;https://rajaosama.me/blogs/blazing-fast-search-with-embedding-model-ada&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Interview Topics for a JS Full Stack Engineer in 2022 💻</title>
      <dc:creator>Raja Osama</dc:creator>
      <pubDate>Fri, 04 Mar 2022 20:18:58 +0000</pubDate>
      <link>https://dev.to/rajaosama/interview-topics-for-a-js-full-stack-engineer-in-2022-4b7l</link>
      <guid>https://dev.to/rajaosama/interview-topics-for-a-js-full-stack-engineer-in-2022-4b7l</guid>
      <description>&lt;p&gt;Hello 👋 Rockstars 🎸, so lately i have been job hunting 👨🏻‍🎓 and i am mostly these days preparing for the job interviews and therefore i created a list of interview subjects that are mostly likely to be in an interview for the role of Full Stack developer (JS). These questions are what i have came across mostly in my interviews so therefore i decided to write about it. &lt;/p&gt;

&lt;p&gt;When I talk about Full-Stack engineer (JS), I am most likely to be talking about the stack that includes: ✅&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Javascript &lt;/li&gt;
&lt;li&gt;React&lt;/li&gt;
&lt;li&gt;Node Js&lt;/li&gt;
&lt;li&gt;CI/CD&lt;/li&gt;
&lt;li&gt;Software engineering concepts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So Let's get into it. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Micro Services &lt;/li&gt;
&lt;li&gt;Monolithic&lt;/li&gt;
&lt;li&gt;Server-less&lt;/li&gt;
&lt;li&gt;Event-Driven architecture&lt;/li&gt;
&lt;li&gt;Message Driven architecture&lt;/li&gt;
&lt;li&gt;Procedural Programming &lt;/li&gt;
&lt;li&gt;Functional Programming&lt;/li&gt;
&lt;li&gt;Object-Oriented Programing&lt;/li&gt;
&lt;li&gt;OOPS&lt;/li&gt;
&lt;li&gt;Inheritance&lt;/li&gt;
&lt;li&gt;Polymorphism&lt;/li&gt;
&lt;li&gt;Abstraction&lt;/li&gt;
&lt;li&gt;Encapsulation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Node JS&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What type of frameworks are commonly used&lt;/li&gt;
&lt;li&gt;What is Rest API&lt;/li&gt;
&lt;li&gt;What is GraphQL&lt;/li&gt;
&lt;li&gt;How is GraphQL and Rest API is different&lt;/li&gt;
&lt;li&gt;What is Node Js&lt;/li&gt;
&lt;li&gt;How is Node Js different from the javascript that is being used in browsers.&lt;/li&gt;
&lt;li&gt;Can we make node Js multi-threaded &lt;/li&gt;
&lt;li&gt;How do you scale a node Js application &lt;/li&gt;
&lt;li&gt;What is clustering in Node Js &lt;/li&gt;
&lt;li&gt;What is HTTPS servers&lt;/li&gt;
&lt;li&gt;What is express&lt;/li&gt;
&lt;li&gt;What are the middlewares in the express application&lt;/li&gt;
&lt;li&gt;A Use case for middleware in the express application &lt;/li&gt;
&lt;li&gt;response Status codes we commonly used group 100, group 200, group 300, group 400, group 500&lt;/li&gt;
&lt;li&gt;Testing with Jest&lt;/li&gt;
&lt;li&gt;Unit Testing&lt;/li&gt;
&lt;li&gt;What are containers&lt;/li&gt;
&lt;li&gt;What is Docker&lt;/li&gt;
&lt;li&gt;what is K8S&lt;/li&gt;
&lt;li&gt;How do you handle authentication&lt;/li&gt;
&lt;li&gt;How you handle OAuth2&lt;/li&gt;
&lt;li&gt;What is JWT&lt;/li&gt;
&lt;li&gt;How do you validate JWT&lt;/li&gt;
&lt;li&gt;Where do you usually validate JWT&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;React Js&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is React&lt;/li&gt;
&lt;li&gt;What is Hooks&lt;/li&gt;
&lt;li&gt;Name commonly-used Hooks&lt;/li&gt;
&lt;li&gt;What is State&lt;/li&gt;
&lt;li&gt;What is Props&lt;/li&gt;
&lt;li&gt;Difference Between States and props&lt;/li&gt;
&lt;li&gt;Can we change states directly&lt;/li&gt;
&lt;li&gt;What is server-side rendering&lt;/li&gt;
&lt;li&gt;What is client-side rendering&lt;/li&gt;
&lt;li&gt;How is client-side and server-side rendering different&lt;/li&gt;
&lt;li&gt;What are HOC&lt;/li&gt;
&lt;li&gt;What is Redux&lt;/li&gt;
&lt;li&gt;What is a store in redux&lt;/li&gt;
&lt;li&gt;What are actions in redux&lt;/li&gt;
&lt;li&gt;What are reducers in redux&lt;/li&gt;
&lt;li&gt;What are the effects in SAGA&lt;/li&gt;
&lt;li&gt;What are generator function&lt;/li&gt;
&lt;li&gt;How is a generator function different then the normal function&lt;/li&gt;
&lt;li&gt;Commonly used reserved keyword of a generator function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Javascript&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define Event Loop&lt;/li&gt;
&lt;li&gt;What is javascript&lt;/li&gt;
&lt;li&gt;How does Javascript handle concurrency &lt;/li&gt;
&lt;li&gt;What are callbacks&lt;/li&gt;
&lt;li&gt;What are Closures&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is debouncing and throttling&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is Babel &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is Webpack&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is Integration Testing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is acceptance testing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is system testing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is MongoDB&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is Redis&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is micro front-end&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pros and cons of micro front-end&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is CI &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is CD&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is a pipeline&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Above are some of the topics that i feel like are very likely to be in an interview question. I will keep on updating the list. If you want to show some support or stay upto date with what i am doing you can follow me on my linkedIn. &lt;/p&gt;

&lt;p&gt;Anyways thank you for being here. hope you have a wonderful day.&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://rajaosama.me/"&gt;Raja Osama&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>react</category>
      <category>software</category>
    </item>
  </channel>
</rss>
