<?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>Forem: Abhishek Biswal</title>
    <description>The latest articles on Forem by Abhishek Biswal (@abhishek_biswal).</description>
    <link>https://forem.com/abhishek_biswal</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%2F603846%2Fa361312b-c333-421d-b508-d9f0423ecbfb.jpg</url>
      <title>Forem: Abhishek Biswal</title>
      <link>https://forem.com/abhishek_biswal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/abhishek_biswal"/>
    <language>en</language>
    <item>
      <title>Classification of Phrases/Quotes using Chat GPT API and Firebase Cloud Functions</title>
      <dc:creator>Abhishek Biswal</dc:creator>
      <pubDate>Mon, 13 Mar 2023 15:55:20 +0000</pubDate>
      <link>https://forem.com/abhishek_biswal/generating-tagscategories-for-phrasesquotes-using-chat-gpt-api-and-firebase-cloud-functions-2njl</link>
      <guid>https://forem.com/abhishek_biswal/generating-tagscategories-for-phrasesquotes-using-chat-gpt-api-and-firebase-cloud-functions-2njl</guid>
      <description>&lt;p&gt;Generating topics for a given quote can be challenging, but with advancements in AI and natural language processing, now it's possible to generate topics with high accuracy. In this article, we'll go through how to use the Chat GPT API to generate topics for a given quote and host the method in the Firebase cloud function HTTP method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Chat-GPT-API?&lt;/strong&gt;&lt;br&gt;
Chat-GPT-API is an API that provides access to OpenAI's GPT (Generative Pre-trained Transformer) language model. GPT is a machine learning model that is trained to generate human-like text based on a given prompt. The Chat-GPT-API allows developers to integrate the GPT model into their applications and generate text based on user input.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are Firebase Cloud Functions?&lt;/strong&gt;&lt;br&gt;
Firebase Cloud Functions is a serverless compute service that allows developers to run code in response to events and automatically scale based on traffic. Firebase Cloud Functions supports Node.js, Python, Java, Go, and .NET. In this article, we will use Firebase Cloud Functions with Node.js to host our code.&lt;/p&gt;

&lt;p&gt;Now that we have an understanding of the technologies we will be using, let's take a look at the process.&lt;/p&gt;

&lt;p&gt;The first step in creating a tag/category is to create a list of topics from which to choose. In our example, we defined a list of default topics as an array of objects with an id and name field. Below is an 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 defaultTags = [
    { id: "1", name: "a" },
    { id: "2", name: "b" },
    { id: "3", name: "c" },
    { id: "4", name: "d" },
    { id: "5", name: "x" },
    { id: "6", name: "z" }
  ];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we need to set up the configuration for the OpenAI API using our API key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const configuration = new Configuration({
    apiKey: process.env.OPENAI_API_KEY,
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function that creates the topics can now be defined. We will host this function in Firebase cloud function HTTP way so that we may access it via an API endpoint.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function generateTopics(req, res){
    var data = req.body
    var tagNames = defaultTags.map(tag =&amp;gt; tag.name);

    #initialize openaiapi
    const openai = new OpenAIApi(configuration);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next create a prompt:&lt;br&gt;
&lt;code&gt;var userPrompt =&lt;br&gt;
        "Assign multiple topics as an array from the topic list given below to the following quote:\nQuote - " +&lt;br&gt;
        data.content +&lt;br&gt;
        "\n\ntopics = [" +&lt;br&gt;
        tagNames.join(", ") +&lt;br&gt;
        "]\n\n Output should be in the format `topics = ['']`";&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;then create a openAIApi response using the prompt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const response = await openai.createChatCompletion({
        model: "gpt-3.5-turbo",
        messages: [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role":"user", "content": userPrompt}],
        temperature: 0.8,
        max_tokens: 150,
        top_p: 1,
        frequency_penalty: 0,
        presence_penalty: 0.6
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are using &lt;code&gt;gpt-3.5-turbo&lt;/code&gt; model to get the response.&lt;/p&gt;

&lt;p&gt;Then we have to parse the response to get the message. The structure of the response is like the below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "choices": [{
    "index": 0,
    "message": {
      "role": "user",
      "content": "topics = ['a', 'b', 'c']",
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have to get the &lt;code&gt;message&lt;/code&gt; which is required further to get the topics.&lt;br&gt;
&lt;code&gt;const message = response.data.choices[0]&lt;br&gt;
 const json = message.message&lt;br&gt;
 const content = JSON.stringify(json.content)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then we can use &lt;code&gt;regular expression&lt;/code&gt; to parse the topics from the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const topicsArr = str.match(/\[([^[\]]*)\]/)[1].match(/'[^']*'/g).map(topic =&amp;gt; topic.slice(1, -1));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this we can use &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;filter&lt;/code&gt; to assign respective ids from the default topic list&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const selectedTagObjects = updatedTags.filter(tag =&amp;gt; generatedTopics.includes(tag.name))
        .map(({ id, name }) =&amp;gt; ({ id, name }));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;after all this we can return the result as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var result = {
        "tags": selectedTagObjects
    }
    cors(req,res, ()=&amp;gt;{
        res.status(200).json({result})
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In conclusion, using the Chat GPT API for generating topics from a given quote can be a useful tool in various applications, such as content tagging and topic identification. The code snippet above demonstrates how we can utilize this API in a Firebase Cloud Function and provide an endpoint to generate topics from a given quote. By simply sending a post request with the quote in the request body, we can get back a list of relevant topics generated by the API. This can be further improved and integrated with other systems to create a more comprehensive solution for content analysis and classification.&lt;/p&gt;

</description>
      <category>chatgpt</category>
      <category>webdev</category>
      <category>serverless</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to prevent your files from being indexed while your application uses S3 for storage and CloudFront as distribution ?</title>
      <dc:creator>Abhishek Biswal</dc:creator>
      <pubDate>Fri, 26 Mar 2021 05:53:39 +0000</pubDate>
      <link>https://forem.com/abhishek_biswal/how-to-prevent-your-files-from-being-indexed-while-your-application-uses-s3-for-storage-and-cloudfront-as-distribution-2pfd</link>
      <guid>https://forem.com/abhishek_biswal/how-to-prevent-your-files-from-being-indexed-while-your-application-uses-s3-for-storage-and-cloudfront-as-distribution-2pfd</guid>
      <description>&lt;h3&gt;
  
  
  When would you face such a scenario ?
&lt;/h3&gt;

&lt;p&gt;While hosting your web application in an S3 and CloudFront distribution in AWS, sometimes we need to prevent some files(direct download links) from being indexed in the search engine results. Files like PDFs, Docs, Mp3s, Videos, Spreadsheets, PPts, etc., get indexed with the direct download link in the SERP. This isn't desirable as users directly get the file from the SERP without actually visiting your application/site which decreases site visits. To stop these files from ever being indexed in a SERP or to deindex the files which have already been indexed, we need to add an HTTP response header to these files that is &lt;strong&gt;X-Robots-Tag: noindex&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Common mistakes-
&lt;/h3&gt;

&lt;p&gt;While there are tons of articles &amp;amp; guides available which claim one should block the search engine bots from accessing those files or folders and they simply won't be indexed.&lt;/p&gt;

&lt;p&gt;Well, that's not true at all. The blocked bots will not crawl those folders but if they find those file links from other sources [ e.g., pages with internal links to those files ], they will still crawl and will index those files. We have faced this issue with some of our clients before and thus I decided to make a comprehensive guide on this topic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complications-
&lt;/h3&gt;

&lt;p&gt;There is no direct method to add HTTP response headers to the files present inside the S3 bucket. There is an option to add custom user-defined meta headers to the files inside the S3 bucket with the prefix &lt;code&gt;x-amz-meta-header-&lt;/code&gt;. CloudFront will serve them with those user-defined headers. So if we check those files for response headers with any HTTP headers testing method available [e.g., checking with &lt;a href="https://securityheaders.com/"&gt;https://securityheaders.com/&lt;/a&gt; ], we can see these custom user-defined headers present on those files. Suppose we want to add the header &lt;code&gt;X-Robots-Tag: noindex&lt;/code&gt; to those files in the S3 bucket. We have to add it like &lt;code&gt;x-amz-meta-header-X-Robots-Tag: noindex&lt;/code&gt;. The crawlers won't recognise it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Proposed solution-
&lt;/h3&gt;

&lt;p&gt;We have to use the &lt;strong&gt;Lambda@Edge&lt;/strong&gt; function to edit those origin response headers while accessing those files via CloudFront URL. [ custom domain you've connected to your CloudFront distribution ]. From the response HTTP header, we have to remove &lt;code&gt;x-amz-meta-header-&lt;/code&gt;from the 'keyname' of the user-defined header, So the crawlers will find &lt;code&gt;X-Robots-Tag: noindex&lt;/code&gt; as HTTP header while accessing those files and follow it's protocols.&lt;/p&gt;

&lt;h3&gt;
  
  
  Execution mechanism-
&lt;/h3&gt;

&lt;p&gt;CloudFront dispatches four events &lt;code&gt;[Viewer Request, Viewer Response, Origin Request, Origin Response]&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;These four events can be worked with AWS Lambda@Edge, Part of an AWS-lambda to execute methods to customize the contents CloudFront delivers. Lamba@Edge scales automatically and runs in the CloudFront location closer to the viewer.&lt;/p&gt;

&lt;p&gt;In our case, we have to use the CloudFront event &lt;code&gt;[Origin-Response]&lt;/code&gt; with lambda@edge to modify the header.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(Origin Response - Dispatched right after CloudFront gets a response from the Origin and before the object is cached in the response.)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps at a glance-
&lt;/h3&gt;

&lt;p&gt;Create a lambda function with any language environment you're acquainted with [ here in the example; we've used &lt;code&gt;Python 3.7&lt;/code&gt;]&lt;br&gt;
Select the default service role, select the CloudFront distribution&lt;br&gt;
Add the custom user-defined header &lt;code&gt;x-amz-meta-X-Robots-Tag: noindex&lt;/code&gt; to those files in the S3 bucket.&lt;br&gt;
Write the necessary code to remove &lt;code&gt;x-amz-meta-&lt;/code&gt; from &lt;code&gt;x-amz-meta-X-Robots-Tag&lt;/code&gt; &lt;br&gt;
In your CloudFront distribution create an invalidation.&lt;br&gt;
Then check your files with any method to check HTTP headers, and you'll find the &lt;code&gt;X-Robots-Tag: noindex&lt;/code&gt; header in those files.&lt;br&gt;
So when the crawlers get this response header, they will know that these files shouldn't be indexed. Eventually, indexed files will be removed from the SERP.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is one of the few definitive ways to prevent your files from being indexed.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>aws</category>
      <category>cloudfront</category>
      <category>python</category>
    </item>
  </channel>
</rss>
