<?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: Ravi</title>
    <description>The latest articles on DEV Community by Ravi (@ragoli86).</description>
    <link>https://dev.to/ragoli86</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%2F1995591%2F2644b126-4894-4f88-aaf1-5a15ef5ff453.jpeg</url>
      <title>DEV Community: Ravi</title>
      <link>https://dev.to/ragoli86</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ragoli86"/>
    <language>en</language>
    <item>
      <title>DeepSeek vs ChatGPT: The Next-Generation AI Showdown</title>
      <dc:creator>Ravi</dc:creator>
      <pubDate>Mon, 27 Jan 2025 03:24:04 +0000</pubDate>
      <link>https://dev.to/ragoli86/deepseek-vs-chatgpt-the-next-generation-ai-showdown-3og6</link>
      <guid>https://dev.to/ragoli86/deepseek-vs-chatgpt-the-next-generation-ai-showdown-3og6</guid>
      <description>&lt;p&gt;There is a lot of hype on the DeepSeek model. Let’s get into the details of what DeepSeek is and the difference between ChatGPT and DeepSeek models.&lt;/p&gt;

&lt;p&gt;DeepSeek, the new artificial intelligence (AI) lab behind the innovation, unveiled its free large language model (LLM) DeepSeek-V3 in late December 2024 and claim it was built in two months for just $5.58 million — a fraction of the time and cost required by its Silicon Valley competitors.&lt;/p&gt;

&lt;p&gt;DeepSeek-R1, a new reasoning model made by new researchers from China, completes tasks with a comparable proficiency to OpenAI's o1 at a fraction of the cost.&lt;/p&gt;

&lt;p&gt;ChatGPT and DeepSeek are both large language models (LLMs), but they have key differences that have made DeepSeek a notable rival to OpenAI's ChatGPT:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Architecture: DeepSeek uses a Mixture-of-Experts (MoE) system, activating only 37 billion of its 671 billion parameters for any task, making it more computationally efficient.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open-source nature: DeepSeek is open-source, allowing developers to run it locally and integrate it into applications more easily, while ChatGPT is proprietary.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cost: DeepSeek is currently free or significantly cheaper to use compared to ChatGPT's subscription model.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Performance: In recent benchmarks, DeepSeek models have matched or surpassed ChatGPT in various tasks, including problem-solving, coding, and math.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Transparency: DeepSeek provides a more transparent "thinking" process, showing steps in its reasoning, which ChatGPT typically doesn't do.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;DeepSeek models are considered rivals to OpenAI because:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Rapid development: Chinese researchers built DeepSeek in just two months for $5.58 million, a fraction of the time and cost required by competitors.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Competitive performance: DeepSeek-R1 has surpassed ChatGPT's latest o1 model in many benchmark tests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Efficiency: DeepSeek's architecture allows for high performance at lower computational costs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accessibility: Its open-source nature and lower costs make it more accessible to developers and businesses.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These factors have led to excitement in the AI community.&lt;/p&gt;

&lt;p&gt;Citations:&lt;br&gt;
&lt;a href="https://www.livescience.com/technology/artificial-intelligence/china-releases-a-cheap-open-rival-to-chatgpt-thrilling-some-scientists-and-panicking-silicon-valley" rel="noopener noreferrer"&gt;https://www.livescience.com/technology/artificial-intelligence/china-releases-a-cheap-open-rival-to-chatgpt-thrilling-some-scientists-and-panicking-silicon-valley&lt;/a&gt;&lt;br&gt;
&lt;a href="https://daily.dev/blog/deepseek-everything-you-need-to-know-about-this-new-llm-in-one-place" rel="noopener noreferrer"&gt;https://daily.dev/blog/deepseek-everything-you-need-to-know-about-this-new-llm-in-one-place&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>chatgpt</category>
      <category>deepseek</category>
    </item>
    <item>
      <title>What is Time Series Forecasting and recent Trends in ML</title>
      <dc:creator>Ravi</dc:creator>
      <pubDate>Fri, 20 Sep 2024 16:34:28 +0000</pubDate>
      <link>https://dev.to/ragoli86/what-is-timeseries-forecasting-and-recent-trends-in-ml-4i18</link>
      <guid>https://dev.to/ragoli86/what-is-timeseries-forecasting-and-recent-trends-in-ml-4i18</guid>
      <description>&lt;h3&gt;
  
  
  What is Time Series Forecasting?
&lt;/h3&gt;

&lt;p&gt;Time series forecasting involves predicting future values based on previously observed values in a dataset indexed by time. Common applications include stock price prediction, weather forecasting, sales forecasting, and demand planning. The main characteristics of time series data include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Temporal Order&lt;/strong&gt;: Data points are collected sequentially over time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Trends&lt;/strong&gt;: Long-term movements in the data (e.g., increasing sales over several years).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Seasonality&lt;/strong&gt;: Patterns that repeat at regular intervals (e.g., higher ice cream sales in summer).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Noise&lt;/strong&gt;: Random variations that can obscure underlying patterns.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  TimeSeries Forecasting Process
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvldq0j460q1wgql8aiiu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvldq0j460q1wgql8aiiu.png" alt="timeseries forecasting description" width="800" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Project Goal Definition&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is the first step of the time series machine learning tutorial. this stage implies defining the project specifics through extensive research within the area of knowledge. This stage outcome would be a clear and well-defined project scope and objectives. It helps create a single document outlining the final goals, including forecasting needs, key metrics, and success criteria. The documents can be shared with all stakeholders and involved experts to avoid misunderstanding.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Gathering and Exploration&lt;/strong&gt;:&lt;br&gt;
Defining the basics leads to a clear view of the scope of data you need to collect to facilitate the further discovery of data insights.&lt;br&gt;
This stage outcome would be a set of data that will be utilized for machine learning model training and testing. The data may be collected from various sources. Moreover, datasets can be augmented by generating synthetic data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Preparation&lt;/strong&gt;:&lt;br&gt;
At this stage, the development team performs cleaning data for relevant insights and further subtracting the variables of importance. This stage outcome would be a clean and pre-processed dataset ready for modeling, comprising no errors or missing records. Inconsistencies and outliers are detected and corrected to achieve top-tier accuracy of the developed result.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Modeling&lt;/strong&gt;:&lt;br&gt;
On the basis of preliminary data preparation and exploratory analysis of a range of time series forecasting conducted at the previous stage, the team works with several models and chooses one based on the criteria of relevance and projected accuracy of the forecast. this stage outcome would be a set of trained machine learning models designed for analyzing datasets and delivering certain insights. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Evaluation&lt;/strong&gt;:&lt;br&gt;
This step covers the optimization of the forecasting model parameters and achievements of high performance. By applying a cross-validation tuning method implying the data split, data scientists train forecasting models with different sets of hyper-parameters. This stage outcome would be the implication of the testing datasets for evaluating the performance of ML models and the accuracy of delivered insights. Fine-tuned and optimized model parameters to achieve the best possible results.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Deployment&lt;/strong&gt;:&lt;br&gt;
This stage includes the forecasting model integration into production. At this particular stage, we highly recommend setting a pipeline to aggregate new data to use for the next AI features. It helps in data preparation work when performing your future projects. An iterative loop of continuous ML model utilization, testing, and improvement.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Recent Trends in Machine Learning for Time Series Forecasting
&lt;/h3&gt;

&lt;p&gt;Machine learning has greatly enhanced the accuracy and flexibility of time series forecasting. Here are some recent trends and techniques:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. &lt;strong&gt;Deep Learning Models&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;LSTM (Long Short-Term Memory): A type of recurrent neural network (RNN) designed to remember information for long periods, making it suitable for sequential data.&lt;/li&gt;
&lt;li&gt;GRU (Gated Recurrent Units): A variant of LSTM that simplifies the architecture while maintaining performance.&lt;/li&gt;
&lt;li&gt;Transformers: Originally designed for natural language processing, they have been adapted for time series tasks, allowing for parallel processing of data sequences.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. &lt;strong&gt;Ensemble Learning&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Combining multiple models to improve forecasting accuracy. Techniques like stacking, bagging, and boosting can reduce overfitting and enhance predictions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. &lt;strong&gt;AutoML and Feature Engineering&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Automated machine learning (AutoML) platforms streamline model selection and hyperparameter tuning.&lt;/li&gt;
&lt;li&gt;Advanced feature engineering techniques, such as extracting time-based features (e.g., day of the week, month) and external variables (e.g., economic indicators), improve model performance.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  4. &lt;strong&gt;Probabilistic Forecasting&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Instead of providing a single point estimate, models predict a distribution of possible future values, which can be useful for understanding uncertainty.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  5. &lt;strong&gt;Transfer Learning&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Applying knowledge gained from one time series task to another, especially when data is scarce, can enhance forecasting accuracy.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Achieving Accurate Forecasting
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Preparation&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cleaning and preprocessing the data to handle missing values, outliers, and ensuring proper formatting.&lt;/li&gt;
&lt;li&gt;Normalizing or scaling features can improve model convergence.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Model Selection&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choosing the right model based on the characteristics of the data. Some datasets may perform better with traditional models (like ARIMA), while others may benefit from machine learning or deep learning approaches.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Hyperparameter Tuning&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using techniques like grid search or randomized search to find optimal model parameters can significantly improve forecasting accuracy.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Evaluation Metrics&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using appropriate metrics like Mean Absolute Error (MAE), Mean Squared Error (MSE), and others tailored for time series helps in assessing model performance accurately.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Cross-Validation Techniques&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Time series cross-validation (e.g., walk-forward validation) ensures that the model is evaluated in a way that respects the temporal order of the data.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Machine learning forecasting proved to be the most effective in capturing the patterns in the sequence of both structured and unstructured data and its further time series analysis forecasting.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj781t4au3ktsks63yz2l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj781t4au3ktsks63yz2l.png" alt="TS data component description" width="800" height="577"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Talking about the suitable model for deep learning for time series forecasting, it is important to understand the components of the time series data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Trends&lt;/strong&gt;: to describe increasing or decreasing behavior of the time series frequently presented in linear modes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Seasonality&lt;/strong&gt;: to hight the repeating pattern of cycles of behavior over time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Irregularity/Noise&lt;/strong&gt;: to regard the non-systematic aspect of time series deviating from the common model values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cyclicity&lt;/strong&gt;: to identify the repetitive changes in the time series and define their placement in the cycle.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Time series forecasting has evolved significantly with the advent of machine learning techniques. By leveraging deep learning, ensemble methods, and advanced feature engineering, organizations can achieve more accurate and robust forecasts. The key to success lies in proper data preparation, model selection, and rigorous evaluation methods. &lt;/p&gt;

&lt;p&gt;I will cover more details of Modeling time series methods in the upcoming blogs.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>ai</category>
    </item>
    <item>
      <title>Sample python script: using Text-Bison model via Azure OpenAI</title>
      <dc:creator>Ravi</dc:creator>
      <pubDate>Fri, 20 Sep 2024 07:03:24 +0000</pubDate>
      <link>https://dev.to/ragoli86/sample-python-script-using-text-bison-model-in-azure-openai-16b</link>
      <guid>https://dev.to/ragoli86/sample-python-script-using-text-bison-model-in-azure-openai-16b</guid>
      <description>&lt;p&gt;To use the Text-Bison model via Azure OpenAI, you need to set up your Azure account and configure the necessary resources. Below is a sample Python script that demonstrates how to interact with the Text-Bison model using Azure OpenAI.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Azure Account&lt;/strong&gt;: Create an Azure account if you don’t have one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Create an OpenAI Resource&lt;/strong&gt;: Set up an OpenAI resource in the Azure portal and obtain your endpoint and API key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Install Required Libraries&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   pip &lt;span class="nb"&gt;install &lt;/span&gt;requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Sample Python Script
&lt;/h3&gt;

&lt;p&gt;Here's a basic script to interact with the Text-Bison model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;

&lt;span class="c1"&gt;# Azure OpenAI configuration
&lt;/span&gt;&lt;span class="n"&gt;endpoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;https://&amp;lt;your-endpoint&amp;gt;.openai.azure.com/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;api_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;your-api-key&amp;gt;&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;deployment_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;text-bison&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;# Your deployment name
&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;endpoint&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;openai/deployments/&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;deployment_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/completions?api-version=2023-05-15&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Content-Type&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;application/json&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;api-key&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;api_key&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;# Define the request body
&lt;/span&gt;    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;prompt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;max_tokens&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;temperature&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.7&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;# Make the request to the Azure OpenAI API
&lt;/span&gt;    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;choices&lt;/span&gt;&lt;span class="sh"&gt;'&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;text&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;strip&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Error: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; - &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What are the benefits of using AI in healthcare?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="n"&gt;generated_text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generate_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;generated_text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Generated Text:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;generated_text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Configuration&lt;/strong&gt;: Replace &lt;code&gt;&amp;lt;your-endpoint&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;your-api-key&amp;gt;&lt;/code&gt; with your Azure OpenAI endpoint and API key. The &lt;code&gt;deployment_name&lt;/code&gt; should match the name of your Text-Bison model deployment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Function to Generate Text&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;generate_text&lt;/code&gt; function constructs the API request.&lt;/li&gt;
&lt;li&gt;It sets the necessary headers for authentication and specifies the request body, including the prompt and parameters like &lt;code&gt;max_tokens&lt;/code&gt; and &lt;code&gt;temperature&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Making the Request&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The script uses the &lt;code&gt;requests&lt;/code&gt; library to send a POST request to the Azure OpenAI API.&lt;/li&gt;
&lt;li&gt;If the request is successful, it returns the generated text; otherwise, it prints an error message.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Execution&lt;/strong&gt;: The script runs a prompt and prints the generated text.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Notes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Ensure you have set the proper permissions and configurations in your Azure portal.&lt;/li&gt;
&lt;li&gt;Adjust parameters such as &lt;code&gt;max_tokens&lt;/code&gt; and &lt;code&gt;temperature&lt;/code&gt; based on your requirements.&lt;/li&gt;
&lt;li&gt;Make sure you handle any API limits or quotas as specified by Azure.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>azure</category>
      <category>llm</category>
      <category>textbison</category>
    </item>
    <item>
      <title>Sample python script: using Text-Bison model via GCP</title>
      <dc:creator>Ravi</dc:creator>
      <pubDate>Fri, 20 Sep 2024 06:58:06 +0000</pubDate>
      <link>https://dev.to/ragoli86/sample-python-script-using-text-bison-using-gcp-12k</link>
      <guid>https://dev.to/ragoli86/sample-python-script-using-text-bison-using-gcp-12k</guid>
      <description>&lt;p&gt;To use &lt;strong&gt;Text-Bison&lt;/strong&gt; from Google Cloud's Generative AI services, you'll typically need to set up a Google Cloud project, enable the necessary APIs, and install the relevant libraries. Below is a sample Python script demonstrating how to interact with the Text-Bison API to generate text.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Google Cloud Account&lt;/strong&gt;: Create a Google Cloud account if you don’t have one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Create a Project&lt;/strong&gt;: Set up a new project in the Google Cloud Console.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enable APIs&lt;/strong&gt;: Enable the Generative AI API for your project.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Install the Google Cloud Client Library&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   pip &lt;span class="nb"&gt;install &lt;/span&gt;google-cloud-aiplatform
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Set Up Authentication&lt;/strong&gt;: Make sure you have set up authentication using a service account key.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Sample Python Script
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;google.cloud&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;aiplatform&lt;/span&gt;

&lt;span class="c1"&gt;# Initialize the AI Platform with your project and location
&lt;/span&gt;&lt;span class="n"&gt;project_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;your-project-id&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;us-central1&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;  &lt;span class="c1"&gt;# or your specific region
&lt;/span&gt;
&lt;span class="n"&gt;aiplatform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;project&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;project_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Create a Text-Bison model instance
&lt;/span&gt;    &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;aiplatform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;text-bison&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Call the model to generate text
&lt;/span&gt;    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;instances&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;prompt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;}],&lt;/span&gt;
        &lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;temperature&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;max_output_tokens&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;predictions&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;text&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What are the benefits of using AI in healthcare?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="n"&gt;generated_text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generate_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Generated Text:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;generated_text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Initialization&lt;/strong&gt;: The script initializes the AI Platform with your project ID and location.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Text Generation Function&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;It defines a function &lt;code&gt;generate_text&lt;/code&gt; that takes a prompt as input.&lt;/li&gt;
&lt;li&gt;Inside the function, it creates a model instance for Text-Bison.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;predict&lt;/code&gt; method is called on the model with the prompt and optional parameters like &lt;code&gt;temperature&lt;/code&gt; (which controls the randomness of the output) and &lt;code&gt;max_output_tokens&lt;/code&gt; (the maximum length of the generated output).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execution&lt;/strong&gt;: The script generates text based on a predefined prompt and prints the result.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Notes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Replace &lt;code&gt;"your-project-id"&lt;/code&gt; with your actual Google Cloud project ID.&lt;/li&gt;
&lt;li&gt;Adjust the parameters based on your needs (e.g., change &lt;code&gt;temperature&lt;/code&gt; and &lt;code&gt;max_output_tokens&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Make sure your environment is properly authenticated with Google Cloud to allow access to the API.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Word-embedding-with-Python: doc2vec</title>
      <dc:creator>Ravi</dc:creator>
      <pubDate>Fri, 20 Sep 2024 06:35:00 +0000</pubDate>
      <link>https://dev.to/ragoli86/word-embedding-with-python-doc2vec-4ok</link>
      <guid>https://dev.to/ragoli86/word-embedding-with-python-doc2vec-4ok</guid>
      <description>&lt;h2&gt;
  
  
  doc2vec implementation with Python (&amp;amp; Gensim)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Note: This code is written in Python 3.6.1 (+Gensim 2.3.0)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python implementation and application of doc2vec with Gensim&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import re
import numpy as np

from gensim.models import doc2Vec
from gensim.models.doc2vec import TaggedDocument
from nltk.corpus import gutenberg
from multiprocessing import Pool
from scipy import spatial
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Import training dataset&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Import Shakespeare's Hamlet corpus from nltk library
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sentences = list(gutenberg.sents('shakespeare-hamlet.txt'))   # import the corpus and convert into a list

print('Type of corpus: ', type(sentences))
print('Length of corpus: ', len(sentences))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Type of corpus:  class 'list'&lt;br&gt;
Length of corpus:  3106&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(sentences[0])    # title, author, and year
print(sentences[1])
print(sentences[10])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;['[', 'The', 'Tragedie', 'of', 'Hamlet', 'by', 'William', 'Shakespeare', '1599', ']']&lt;br&gt;
['Actus', 'Primus', '.']&lt;br&gt;
['Fran', '.']&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Preprocess data&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use re module to preprocess data&lt;/li&gt;
&lt;li&gt;Convert all letters into lowercase&lt;/li&gt;
&lt;li&gt;Remove punctuations, numbers, etc.&lt;/li&gt;
&lt;li&gt;For the doc2vec model, input data should be in format of iterable TaggedDocuments"

&lt;ul&gt;
&lt;li&gt;Each TaggedDocument instance comprises words and tags&lt;/li&gt;
&lt;li&gt;Hence, each document (i.e., a sentence or paragraph) should have a unique tag which is identifiable
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(len(sentences)):
    sentences[i] = [word.lower() for word in sentences[i] if re.match('^[a-zA-Z]+', word)]  
print(sentences[0])    # title, author, and year
print(sentences[1])
print(sentences[10])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;['the', 'tragedie', 'of', 'hamlet', 'by', 'william', 'shakespeare']&lt;br&gt;
['actus', 'primus']&lt;br&gt;
['fran']&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(len(sentences)):
    sentences[i] = TaggedDocument(words = sentences[i], tags = ['sent{}'.format(i)])    # converting each sentence into a TaggedDocument
sentences[0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TaggedDocument(words=['the', 'tragedie', 'of', 'hamlet', 'by', 'william', 'shakespeare'], tags=['sent0'])&lt;/p&gt;

&lt;h3&gt;
  
  
  Create and train model
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a doc2vec model and train it with Hamlet corpus&lt;/li&gt;
&lt;li&gt;Key parameter description (&lt;a href="https://radimrehurek.com/gensim/models/doc2vec.html" rel="noopener noreferrer"&gt;https://radimrehurek.com/gensim/models/doc2vec.html&lt;/a&gt;)

&lt;ul&gt;
&lt;li&gt;sentences: training data (has to be a list with tokenized sentences)&lt;/li&gt;
&lt;li&gt;size: dimension of embedding space&lt;/li&gt;
&lt;li&gt;sg: CBOW if 0, skip-gram if 1&lt;/li&gt;
&lt;li&gt;window: number of words accounted for each context (if the window &lt;/li&gt;
&lt;li&gt;size is 3, 3 word in the left neighorhood and 3 word in the right neighborhood are considered)&lt;/li&gt;
&lt;li&gt;min_count: minimum count of words to be included in the vocabulary&lt;/li&gt;
&lt;li&gt;iter: number of training iterations&lt;/li&gt;
&lt;li&gt;workers: number of worker threads to train
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;model = Doc2Vec(documents = sentences,dm = 1, size = 100, min_count = 1, iter = 10, workers = Pool()._processes)

model.init_sims(replace = True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Save and load model
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;doc2vec model can be saved and loaded locally&lt;/li&gt;
&lt;li&gt;Doing so can reduce time to train model again
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;model.save('doc2vec_model')
model = doc2Vec.load('doc2vec_model')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Similarity calculation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Similarity between embedded words (i.e., vectors) can be computed using metrics such as cosine similarity
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;model.most_similar('hamlet')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;[('horatio', 0.9978846311569214),&lt;br&gt;
 ('queene', 0.9971947073936462),&lt;br&gt;
 ('laertes', 0.9971820116043091),&lt;br&gt;
 ('king', 0.9968599081039429),&lt;br&gt;
 ('mother', 0.9966716170310974),&lt;br&gt;
 ('where', 0.9966292381286621),&lt;br&gt;
 ('deere', 0.9965540170669556),&lt;br&gt;
 ('ophelia', 0.9964221715927124),&lt;br&gt;
 ('very', 0.9963752627372742),&lt;br&gt;
 ('oh', 0.9963476657867432)]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;v1 = model['king']
v2 = model['queen']

# define a function that computes cosine similarity between two words
def cosine_similarity(v1, v2):
    return 1 - spatial.distance.cosine(v1, v2)

cosine_similarity(v1, v2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;0.99437165260314941&lt;/p&gt;

</description>
      <category>genai</category>
      <category>vectordatabase</category>
      <category>python</category>
    </item>
    <item>
      <title>Word-embedding-with-Python: Word2Vec</title>
      <dc:creator>Ravi</dc:creator>
      <pubDate>Fri, 20 Sep 2024 06:10:19 +0000</pubDate>
      <link>https://dev.to/ragoli86/word-embedding-with-python-word2vec-540c</link>
      <guid>https://dev.to/ragoli86/word-embedding-with-python-word2vec-540c</guid>
      <description>&lt;h2&gt;
  
  
  word2vec implementation with Python (&amp;amp; Gensim)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Note: This code is written in Python 3.6.1 (+Gensim 2.3.0)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python implementation and application of word2vec with Gensim&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import re
import numpy as np

from gensim.models import Word2Vec
from nltk.corpus import gutenberg
from multiprocessing import Pool
from scipy import spatial
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Import training dataset&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Import Shakespeare's Hamlet corpus from nltk library
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sentences = list(gutenberg.sents('shakespeare-hamlet.txt'))   # import the corpus and convert into a list

print('Type of corpus: ', type(sentences))
print('Length of corpus: ', len(sentences))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Type of corpus:  class 'list'&lt;br&gt;
Length of corpus:  3106&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(sentences[0])    # title, author, and year
print(sentences[1])
print(sentences[10])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;['[', 'The', 'Tragedie', 'of', 'Hamlet', 'by', 'William', 'Shakespeare', '1599', ']']&lt;br&gt;
['Actus', 'Primus', '.']&lt;br&gt;
['Fran', '.']&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Preprocess data&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use re module to preprocess data&lt;/li&gt;
&lt;li&gt;Convert all letters into lowercase&lt;/li&gt;
&lt;li&gt;Remove punctuations, numbers, etc.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(len(sentences)):
    sentences[i] = [word.lower() for word in sentences[i] if re.match('^[a-zA-Z]+', word)]  
print(sentences[0])    # title, author, and year
print(sentences[1])
print(sentences[10])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;['the', 'tragedie', 'of', 'hamlet', 'by', 'william', 'shakespeare']&lt;br&gt;
['actus', 'primus']&lt;br&gt;
['fran']&lt;/p&gt;
&lt;h3&gt;
  
  
  Create and train model
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a word2vec model and train it with Hamlet corpus&lt;/li&gt;
&lt;li&gt;Key parameter description (&lt;a href="https://radimrehurek.com/gensim/models/word2vec.html" rel="noopener noreferrer"&gt;https://radimrehurek.com/gensim/models/word2vec.html&lt;/a&gt;)

&lt;ul&gt;
&lt;li&gt;sentences: training data (has to be a list with tokenized sentences)&lt;/li&gt;
&lt;li&gt;size: dimension of embedding space&lt;/li&gt;
&lt;li&gt;sg: CBOW if 0, skip-gram if 1&lt;/li&gt;
&lt;li&gt;window: number of words accounted for each context (if the window &lt;/li&gt;
&lt;li&gt;size is 3, 3 word in the left neighorhood and 3 word in the right neighborhood are considered)&lt;/li&gt;
&lt;li&gt;min_count: minimum count of words to be included in the vocabulary&lt;/li&gt;
&lt;li&gt;iter: number of training iterations&lt;/li&gt;
&lt;li&gt;workers: number of worker threads to train
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;model = Word2Vec(sentences = sentences, size = 100, sg = 1, window = 3, min_count = 1, iter = 10, workers = Pool()._processes)

model.init_sims(replace = True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Save and load model
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;word2vec model can be saved and loaded locally&lt;/li&gt;
&lt;li&gt;Doing so can reduce time to train model again
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;model.save('word2vec_model')
model = Word2Vec.load('word2vec_model')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Similarity calculation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Similarity between embedded words (i.e., vectors) can be computed using metrics such as cosine similarity
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;model.most_similar('hamlet')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;[('horatio', 0.9978846311569214),&lt;br&gt;
 ('queene', 0.9971947073936462),&lt;br&gt;
 ('laertes', 0.9971820116043091),&lt;br&gt;
 ('king', 0.9968599081039429),&lt;br&gt;
 ('mother', 0.9966716170310974),&lt;br&gt;
 ('where', 0.9966292381286621),&lt;br&gt;
 ('deere', 0.9965540170669556),&lt;br&gt;
 ('ophelia', 0.9964221715927124),&lt;br&gt;
 ('very', 0.9963752627372742),&lt;br&gt;
 ('oh', 0.9963476657867432)]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;v1 = model['king']
v2 = model['queen']

# define a function that computes cosine similarity between two words
def cosine_similarity(v1, v2):
    return 1 - spatial.distance.cosine(v1, v2)

cosine_similarity(v1, v2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;0.99437165260314941&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Original paper: Mikolov, T., Chen, K., Corrado, G., &amp;amp; Dean, J. (2013). Efficient estimation of word representations in vector space. arXiv preprint arXiv:1301.3781.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>genai</category>
    </item>
    <item>
      <title>Exploring Word Embeddings: python implementation of Word2Vec and GloVe in Vector Databases</title>
      <dc:creator>Ravi</dc:creator>
      <pubDate>Fri, 20 Sep 2024 05:49:33 +0000</pubDate>
      <link>https://dev.to/ragoli86/exploring-word-embeddings-python-implementation-of-word2vec-and-glove-in-vector-databases-2jcf</link>
      <guid>https://dev.to/ragoli86/exploring-word-embeddings-python-implementation-of-word2vec-and-glove-in-vector-databases-2jcf</guid>
      <description>&lt;p&gt;Word embeddings like Word2Vec and GloVe are powerful techniques to convert words into continuous vector representations. These vectors capture semantic relationships between words, making them useful for various applications, including vector databases.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example of Using Word Embeddings with Python
&lt;/h3&gt;

&lt;p&gt;We'll cover how to generate word embeddings using Word2Vec and GloVe, and then store these embeddings in a vector database (like FAISS or Annoy) for efficient similarity searches.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Install Required Libraries
&lt;/h3&gt;

&lt;p&gt;First, make sure you have the required libraries installed. You can install them via pip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;gensim faiss-cpu
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Generate Word Embeddings
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Using Word2Vec
&lt;/h4&gt;

&lt;p&gt;Here's how to generate word embeddings using Word2Vec:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;gensim&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;gensim.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Word2Vec&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;nltk.tokenize&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;word_tokenize&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;nltk&lt;/span&gt;

&lt;span class="c1"&gt;# Download NLTK resources
&lt;/span&gt;&lt;span class="n"&gt;nltk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;download&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;punkt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Sample text data
&lt;/span&gt;&lt;span class="n"&gt;sentences&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Natural language processing is a fascinating field.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Word embeddings are useful for semantic search.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Gensim is a popular library for topic modeling and embeddings.&lt;/span&gt;&lt;span class="sh"&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;# Tokenize the sentences
&lt;/span&gt;&lt;span class="n"&gt;tokenized_sentences&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;word_tokenize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sentence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;sentence&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sentences&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Train Word2Vec model
&lt;/span&gt;&lt;span class="n"&gt;word2vec_model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Word2Vec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sentences&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;tokenized_sentences&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vector_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;min_count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;workers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Save the model
&lt;/span&gt;&lt;span class="n"&gt;word2vec_model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;word2vec.model&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break down the provided code step by step to understand its purpose and functionality:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Importing Libraries&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;gensim&lt;/code&gt; is a library for topic modeling and document similarity analysis.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Word2Vec&lt;/code&gt; is a specific model within Gensim for creating word embeddings.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;word_tokenize&lt;/code&gt; from the NLTK (Natural Language Toolkit) library is used for breaking sentences into individual words (tokens).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nltk&lt;/code&gt; is the library that provides various tools for natural language processing.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Downloading NLTK Resources&lt;/strong&gt;: This line downloads the necessary tokenizer resources from NLTK, which is needed for the &lt;code&gt;word_tokenize&lt;/code&gt; function to work.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sample Text Data&lt;/strong&gt;: Here, a list of sentences is defined to serve as the training data for the Word2Vec model. This data contains different aspects of natural language processing and the Gensim library.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Tokenizing Sentences&lt;/strong&gt;: This line processes each sentence in the &lt;code&gt;sentences&lt;/code&gt; list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It converts the sentence to lowercase to ensure uniformity.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;word_tokenize&lt;/code&gt; breaks the sentence into individual words, resulting in a list of tokenized sentences.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Training the Word2Vec Model&lt;/strong&gt;: This line creates and trains a Word2Vec model using the tokenized sentences.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;vector_size=100&lt;/code&gt;: Sets the dimensionality of the word vectors to 100.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;window=5&lt;/code&gt;: Defines the context window size, meaning the model will consider 5 words before and after a target word to learn its context.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;min_count=1&lt;/code&gt;: Ensures that words appearing at least once are included in the model. (In practice, a higher value is often used to filter out rare words.)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;workers=4&lt;/code&gt;: Specifies the number of CPU threads to use during training, allowing for faster processing.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Saving the Model&lt;/strong&gt;: This line saves the trained Word2Vec model to a file named "word2vec.model", allowing you to load and use it later without retraining.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Using GloVe
&lt;/h4&gt;

&lt;p&gt;To use GloVe, you'll need to install the &lt;code&gt;glove-python-binary&lt;/code&gt; package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;glove-python-binary
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's how to generate GloVe embeddings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;glove&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Corpus&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Glove&lt;/span&gt;

&lt;span class="c1"&gt;# Create a corpus from the tokenized sentences
&lt;/span&gt;&lt;span class="n"&gt;corpus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Corpus&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;corpus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tokenized_sentences&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="o"&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;# Train GloVe model
&lt;/span&gt;&lt;span class="n"&gt;glove_model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Glove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no_components&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;learning_rate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.05&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;glove_model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;corpus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;epochs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;no_threads&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;verbose&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Save the model
&lt;/span&gt;&lt;span class="n"&gt;glove_model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;glove.model&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Importing Libraries: This line imports the Corpus and Glove classes from the glove library, which is used for generating GloVe (Global Vectors for Word Representation) embeddings.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creating a Corpus: This line creates an instance of the Corpus class. A corpus is a collection of text that will be used to train the GloVe model.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fitting the Corpus: This line trains the Corpus object using the tokenized_sentences, which is a list of tokenized words from your text data. The window parameter specifies the size of the context window (the number of words to consider before and after a target word). A larger window means more context is taken into account.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creating a GloVe Model: This line creates an instance of the Glove class. The no_components parameter specifies the dimensionality of the word vectors (in this case, 100 dimensions), and learning_rate sets the initial learning rate for the model training.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Training the GloVe Model: This line fits the GloVe model to the matrix created from the corpus.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;corpus.matrix provides the co-occurrence matrix of words, which is used to train the embeddings.&lt;/li&gt;
&lt;li&gt;epochs specifies the number of training iterations (30 in this case).&lt;/li&gt;
&lt;li&gt;no_threads indicates the number of CPU threads to use for training (4 threads).&lt;/li&gt;
&lt;li&gt;verbose=True means that the training process will output progress messages.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Saving the Model: This line saves the trained GloVe model to a file named "glove.model". This allows you to load the model later for generating embeddings or performing other tasks without retraining.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 3: Store and Query Word Embeddings in a Vector Database
&lt;/h3&gt;

&lt;p&gt;For this example, we will use FAISS to create a simple vector database and perform similarity searches.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using FAISS
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;faiss&lt;/span&gt;

&lt;span class="c1"&gt;# Get word vectors from the Word2Vec model
&lt;/span&gt;&lt;span class="n"&gt;word_vectors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;word2vec_model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wv&lt;/span&gt;
&lt;span class="n"&gt;word_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word_vectors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index_to_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;word_embeddings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;array&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;word_vectors&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;word_list&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;astype&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;float32&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Create FAISS index
&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;faiss&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IndexFlatL2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word_embeddings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# L2 distance
&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word_embeddings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Function to find the top n similar words
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_similar_words&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;word_vectors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;word_vector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;word_vectors&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;reshape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;astype&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;float32&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;distances&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indices&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word_vector&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="n"&gt;word_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;distances&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="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;indices&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="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="c1"&gt;# Example query
&lt;/span&gt;&lt;span class="n"&gt;similar_words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;find_similar_words&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;language&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Similar words to &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;language&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;similar_words&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break down the provided code step by step to understand its purpose and functionality:&lt;/p&gt;

&lt;p&gt;Let's break down the provided code step by step to understand its purpose and functionality:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Importing Libraries&lt;/strong&gt;: This line imports NumPy (for numerical operations) and FAISS (Facebook AI Similarity Search), a library optimized for efficient similarity search and clustering of dense vectors.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Accessing Word Vectors&lt;/strong&gt;: This code retrieves the word vectors from a previously trained Word2Vec model. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;word_vectors&lt;/code&gt; contains the actual embeddings for each word.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;word_list&lt;/code&gt; creates a list of words (the vocabulary) based on their indices.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Creating a NumPy Array of Embeddings&lt;/strong&gt;: This line constructs a NumPy array (&lt;code&gt;word_embeddings&lt;/code&gt;) containing the word vectors for all the words in the vocabulary. The vectors are converted to the &lt;code&gt;float32&lt;/code&gt; data type for compatibility with FAISS.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Creating a FAISS Index&lt;/strong&gt;: This line initializes a FAISS index for performing similarity searches. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;IndexFlatL2&lt;/code&gt; creates a flat (non-hierarchical) index that uses L2 distance (Euclidean distance) to measure similarity between vectors.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;word_embeddings.shape[1]&lt;/code&gt; specifies the dimensionality of the vectors.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Adding Embeddings to the Index&lt;/strong&gt;: This line adds all the word embeddings to the FAISS index, allowing for efficient similarity search operations.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Defining a Similarity Search Function&lt;/strong&gt;: This function, &lt;code&gt;find_similar_words&lt;/code&gt;, takes a word and the number of similar words to return (&lt;code&gt;n&lt;/code&gt;).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It first checks if the word is in the word vectors.&lt;/li&gt;
&lt;li&gt;If the word exists, it retrieves its corresponding vector, reshapes it to a 2D array, and converts it to &lt;code&gt;float32&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;index.search&lt;/code&gt; method is used to find the &lt;code&gt;n&lt;/code&gt; most similar words based on L2 distance, returning both the distances and indices of the closest words.&lt;/li&gt;
&lt;li&gt;The function then constructs a list of tuples containing the similar words and their distances.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Executing a Query&lt;/strong&gt;: This code calls the &lt;code&gt;find_similar_words&lt;/code&gt; function with the word "language" and prints out the similar words along with their distances.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;To conclude, This code demonstrates how to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Generate word embeddings using Word2Vec and GloVe.&lt;/li&gt;
&lt;li&gt;Store these embeddings in a FAISS vector database.&lt;/li&gt;
&lt;li&gt;Perform similarity searches to find words that are semantically similar.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can adjust the sample text and query words to see how the embeddings capture different relationships.&lt;/p&gt;

</description>
      <category>genai</category>
      <category>machinelearning</category>
      <category>vectordatabase</category>
    </item>
    <item>
      <title>Semantic Search and Algorithms</title>
      <dc:creator>Ravi</dc:creator>
      <pubDate>Fri, 20 Sep 2024 04:36:43 +0000</pubDate>
      <link>https://dev.to/ragoli86/semantic-search-and-algorithms-4d0b</link>
      <guid>https://dev.to/ragoli86/semantic-search-and-algorithms-4d0b</guid>
      <description>&lt;p&gt;&lt;strong&gt;Semantic Search&lt;/strong&gt; refers to a search technique that seeks to improve search accuracy by understanding the intent and contextual meaning of search queries, rather than relying solely on keyword matching. It employs natural language processing (NLP) and machine learning techniques to comprehend the relationships between words, phrases, and concepts, allowing it to deliver more relevant and context-aware search results.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Features of Semantic Search:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Understanding Context&lt;/strong&gt;: Semantic search analyzes the context of a query, considering synonyms, related terms, and user intent, which helps to deliver more precise results.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Entity Recognition&lt;/strong&gt;: It identifies entities (people, places, organizations) within the search queries, allowing for more relevant connections and results.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;User Intent&lt;/strong&gt;: By grasping the user’s underlying intention (informational, transactional, navigational), it provides results that better match what users are truly looking for.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Natural Language Processing&lt;/strong&gt;: It utilizes NLP techniques to process and understand human language, making it easier for users to interact with search systems using everyday language.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Popularity in Generative AI:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Enhanced User Experience&lt;/strong&gt;: As users expect more intuitive and conversational interactions, semantic search allows for a more engaging experience, making it easier to find information.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Improved Relevance&lt;/strong&gt;: With the rise of vast data sources, semantic search helps in filtering and retrieving relevant information quickly, which is critical for applications in content generation, chatbots, and virtual assistants.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Integration with AI Models&lt;/strong&gt;: Generative AI models (like GPT-3) benefit from semantic search as they can retrieve and utilize contextually relevant information, leading to richer and more coherent outputs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Personalization&lt;/strong&gt;: By understanding user preferences and past behavior, semantic search can offer personalized content and recommendations, a key feature in many AI-driven applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scalability&lt;/strong&gt;: In an era where information is constantly growing, semantic search provides scalable solutions that adapt to diverse datasets, enhancing the capability of AI systems.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Semantic search is increasingly popular in the Generative AI space because it aligns closely with the goal of creating more human-like interactions and understanding within AI systems. Its ability to provide relevant, context-aware results makes it a valuable tool in applications ranging from content generation to customer support, ultimately improving how users interact with technology.&lt;/p&gt;

&lt;p&gt;Semantic search algorithms leverage various techniques and models to enhance the understanding of user queries and the context of content. Here are some of the key algorithms and approaches used in semantic search:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Vector Space Models&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;TF-IDF (Term Frequency-Inverse Document Frequency)&lt;/strong&gt;: A statistical measure that evaluates the importance of a word in a document relative to a collection of documents (corpus). It helps in weighting terms to identify relevant documents.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Word Embeddings&lt;/strong&gt;: Techniques like Word2Vec and GloVe convert words into continuous vector representations, capturing semantic relationships between words based on their context.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Latent Semantic Analysis (LSA)&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;This technique reduces the dimensionality of the term-document matrix to uncover latent relationships between terms and documents, helping to identify synonyms and related terms.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;Latent Dirichlet Allocation (LDA)&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A generative statistical model that identifies topics in a collection of documents, helping to understand the underlying themes and improve content retrieval based on topic relevance.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. &lt;strong&gt;Deep Learning Models&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Recurrent Neural Networks (RNNs)&lt;/strong&gt; and &lt;strong&gt;Long Short-Term Memory (LSTM)&lt;/strong&gt; networks can process sequences of words, understanding context and relationships better than traditional models.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transformers&lt;/strong&gt;: Models like BERT (Bidirectional Encoder Representations from Transformers) and GPT (Generative Pre-trained Transformer) are particularly powerful for semantic search. They understand the context of words in relation to all other words in a sentence, making them effective at grasping nuanced meanings.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. &lt;strong&gt;Knowledge Graphs&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;These are structured representations of knowledge that include entities and their relationships. They enhance search by providing context and connections, allowing for more relevant results based on user intent.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. &lt;strong&gt;Natural Language Processing (NLP) Techniques&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Named Entity Recognition (NER)&lt;/strong&gt;: Identifies and classifies entities in text (like people, places, organizations), helping the system understand the key components of a query.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Semantic Role Labeling&lt;/strong&gt;: Assigns roles to words in a sentence to understand the actions and relationships better.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  7. &lt;strong&gt;Query Expansion Techniques&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;This involves expanding the original query with synonyms, related terms, or variations to improve the retrieval of relevant documents.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  8. &lt;strong&gt;Hybrid Approaches&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Combining multiple techniques (e.g., traditional keyword-based methods with deep learning models) to leverage the strengths of each and improve search results.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The landscape of semantic search algorithms is continually evolving, driven by advancements in machine learning and natural language processing. By using these algorithms, systems can better understand user intent and context, leading to more relevant and accurate search results.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>genai</category>
      <category>vectordatabase</category>
    </item>
    <item>
      <title>AI-Powered Bot using Vectorized knowledge Architecture</title>
      <dc:creator>Ravi</dc:creator>
      <pubDate>Wed, 18 Sep 2024 00:01:26 +0000</pubDate>
      <link>https://dev.to/ragoli86/ai-powered-bot-using-vectorized-knowledge-architecture-4f76</link>
      <guid>https://dev.to/ragoli86/ai-powered-bot-using-vectorized-knowledge-architecture-4f76</guid>
      <description>&lt;p&gt;&lt;strong&gt;"By connecting chatbots to internal knowledge bases, businesses can significantly enhance the contextual relevance of their interactions.&lt;/strong&gt; This integration allows chatbots to tailor responses to individual users' needs and preferences, providing personalized recommendations, explanations, and support. For instance, a chatbot could suggest products based on a shopper's past purchases, explain technical details in a language tailored to their expertise, or access customer records to provide accurate account support.&lt;/p&gt;

&lt;p&gt;This capability not only improves customer satisfaction but also drives tangible business value. By understanding natural language, incorporating relevant information, and delivering customized replies, chatbots can streamline processes, reduce costs, and foster stronger customer relationships. While integrating knowledge bases can present challenges, the benefits often outweigh the complexities involved."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"Retrieval Augmented Generation (RAG)&lt;/strong&gt; is a powerful technique for natural language generation that combines information retrieval with text generation. This approach enhances the quality and relevance of generated text by incorporating relevant information from external sources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RAG architecture typically involves two main workflows:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data Preprocessing:&lt;/strong&gt; This stage involves ingesting and organizing large amounts of data into a structured format that can be easily accessed and searched.&lt;br&gt;
&lt;strong&gt;Text Generation with Enhanced Context:&lt;/strong&gt; Once the data is prepared, the LLM generates text while leveraging the retrieved information to provide more accurate, informative, and contextually relevant responses.&lt;/p&gt;

&lt;p&gt;By integrating information retrieval into the generation process, RAG models can produce more comprehensive and informative text, making them valuable for a wide range of applications, such as question answering, summarization, and creative writing."&lt;/p&gt;

&lt;p&gt;Here is the high-level RAG architecture.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj89n5f5w84fah6uy2n3x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj89n5f5w84fah6uy2n3x.png" alt="RAG architecture description" width="782" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This diagram illustrates the workflow of a text generation system that leverages a large language model (LLM) and a vector store for enhanced context retrieval. The system takes a user input, processes it through embeddings, searches for relevant context from a vector store, and uses the LLM to generate a response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Points:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The embeddings model plays a crucial role in understanding the semantic meaning of the text.&lt;/li&gt;
&lt;li&gt;The vector store enables efficient retrieval of relevant context based on similarity.&lt;/li&gt;
&lt;li&gt;Prompt augmentation enhances the quality and relevance of the LLM's response by providing additional context.&lt;/li&gt;
&lt;li&gt;The LLM generates the final text output based on the augmented prompt.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This workflow can be adapted for various text generation tasks, such as question answering, summarization, and creative writing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Additional considerations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Vector Databases:&lt;/strong&gt; RAG often relies on vector databases to efficiently store and retrieve relevant information.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prompt Engineering:&lt;/strong&gt; Crafting effective prompts is crucial for guiding the LLM to generate high-quality text.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Evaluation Metrics:&lt;/strong&gt; Evaluating RAG models requires specialized metrics that assess both the quality of the generated text and the relevance of the retrieved information.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this design, I am using the Amazon Bedrock is a serverless option to build powerful conversational AI systems using RAG. It's a fully managed data ingestion and text generation workflows.&lt;/p&gt;

&lt;p&gt;For data ingestion, Amazon Bedrock provides the &lt;strong&gt;StartIngestionJob&lt;/strong&gt; API to start an ingestion job. It handles creating, storing, managing, and updating text embeddings of document data in the vector database automatically. It splits the documents into manageable chunks for efficient retrieval. The chunks are then converted to embeddings and written to a vector index, while allowing you to see the source documents when answering a question.&lt;/p&gt;

&lt;p&gt;For text generation, Amazon Bedrock provides the &lt;strong&gt;RetrieveAndGenerate&lt;/strong&gt; API to create embeddings of user queries, and retrieves relevant chunks from the vector database to generate accurate responses. It also supports source attribution and short-term memory needed for RAG applications.&lt;/p&gt;

&lt;p&gt;Here is the solution overview of the chatbot application using the following solution architecture:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwto4lqgxst0tz1qc16ow.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwto4lqgxst0tz1qc16ow.png" alt="chatbot app" width="653" height="682"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This architecture workflow includes the following steps:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data Ingestion and Preparation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Data Upload: A user uploads content (files, documents etc.,) to an Amazon S3 bucket.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; Data Synchronization: An AWS Lambda function is triggered to synchronize the data source with the knowledge base.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; Data Ingestion: The Lambda function starts the data ingestion process using StartIngestionJob.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.&lt;/strong&gt; Data Chunking: The knowledge base splits the documents into manageable chunks for efficient retrieval.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.&lt;/strong&gt; &lt;strong&gt;Vector Store and Embedding Creation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Vector Store Setup: The knowledge base uses Amazon OpenSearch Serverless as its vector store.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Embedding Creation: Amazon Titan is used to create embeddings for the document chunks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Vector Index Creation: The embeddings are written to a vector index in the OpenSearch vector store, maintaining a mapping to the original document.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;6.&lt;/strong&gt; A user interacts with the chatbot interface and submit a query in natural language. The chatbot frontend application is a single page application built using the React or Angular or any other UI framework.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7.&lt;/strong&gt; &lt;strong&gt;API Invocation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The chatbot frontend application invokes a REST API created using Amazon API Gateway.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lambda Function Trigger: A Lambda function integrated with the API invokes the RetrieveAndGenerate API.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;8.&lt;/strong&gt; Load embeddings&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Query Embedding: Amazon Bedrock Knowledge Bases converts the user query to a vector.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Semantic Similarity Search: The knowledge base finds chunks that are semantically similar to the user query.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Prompt Augmentation: The user prompt is augmented with the retrieved chunks.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;9.&lt;/strong&gt; LLM Response Generation: The augmented prompt is sent to an LLM (Anthropic Claude Instant 1.2) to generate a response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10.&lt;/strong&gt; &lt;strong&gt;Response Delivery:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Response Return: The Lambda function returns the answer and citation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;User Interface Display: The user sees the answer and citation on the chatbot user interface.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this we have seen the value of contextual bots,  RAG systems, Amazon Bedrock Knowledge Bases, Amazon opensearch vector store, this post aimed to showcase how Amazon managed services enables you to build sophisticated conversational AI applications.&lt;/p&gt;

</description>
      <category>genai</category>
      <category>rag</category>
      <category>vectordatabase</category>
    </item>
    <item>
      <title>RNN - Recurrent Neural Network</title>
      <dc:creator>Ravi</dc:creator>
      <pubDate>Sun, 15 Sep 2024 18:46:28 +0000</pubDate>
      <link>https://dev.to/ragoli86/rnn-recurrent-neural-network-2mml</link>
      <guid>https://dev.to/ragoli86/rnn-recurrent-neural-network-2mml</guid>
      <description>&lt;p&gt;&lt;strong&gt;RNN (Recurrent Neural Network)&lt;/strong&gt; is a type of artificial neural network (ANN) designed to process sequential data. Unlike traditional feedforward neural networks, which process each input independently,&lt;br&gt;
RNNs can maintain information about previous inputs, allowing them to learn and remember patterns over time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does a RNN work?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following image shows a diagram of an RNN.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6qvmycnfd604pyyt7nl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6qvmycnfd604pyyt7nl.png" alt="RNN works description" width="538" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;RNNs are made of neurons: data-processing nodes that work together to perform complex tasks. The neurons are organized as input, output, and hidden layers. The input layer receives the information to process, and the output layer provides the result. Data processing, analysis, and prediction take place in the hidden layer. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hidden Layer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;RNNs work by passing the sequential data that they receive to the hidden layers one step at a time. However, they also have a self-looping or recurrent workflow: the hidden layer can remember and use previous inputs for future predictions in a short-term memory component. It uses the current input and the stored memory to predict the next sequence. &lt;/p&gt;

&lt;p&gt;For example, consider the sequence: Apple is red. You want the RNN to predict red when it receives the input sequence Apple is. When the hidden layer processes the word Apple, it stores a copy in its memory. Next, when it sees the word is, it recalls Apple from its memory and understands the full sequence: Apple is for context. It can then predict red for improved accuracy. This makes RNNs useful in speech recognition, machine translation, and other language modeling tasks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of RNNs:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Simple RNN:&lt;/strong&gt; The most basic type of RNN.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Bidirectional recurrent neural networks&lt;/strong&gt;&lt;br&gt;
A bidirectional recurrent neural network (BRNN) processes data sequences with forward and backward layers of hidden nodes. The forward layer works similarly to the RNN, which stores the previous input in the hidden state and uses it to predict the subsequent output. Meanwhile, the backward layer works in the opposite direction by taking both the current input and the future hidden state to update the present hidden state. Combining both layers enables the BRNN to improve prediction accuracy by considering past and future contexts. For example, you can use the BRNN to predict the word trees in the sentence Apple trees are tall. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;LSTM (Long Short-Term Memory):&lt;/strong&gt; A more complex type of RNN that uses gates to control the flow of information, making it better suited for learning long-term dependencies.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consider the following sentences: &lt;em&gt;Tom is a cat.&lt;/em&gt; Tom’s favorite food is fish. When you’re using an RNN, the model can’t remember that Tom is a cat. It might generate various foods when it predicts the last word. LSTM networks add a special memory block called cells in the hidden layer. Each cell is controlled by an input gate, output gate, and forget gate, which enables the layer to remember helpful information. For example, the cell remembers the words Tom and cat, enabling the model to predict the word fish. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GRU (Gated Recurrent Unit):&lt;/strong&gt; A simpler variant of LSTM that uses fewer gates, making it computationally efficient. The RNN enables selective memory retention. The model adds an update and forgets the gate to its hidden layer, which can store or remove information in the memory. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How do RNN compare to other deep learning networks?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Recurrent neural network vs. feed-forward neural network&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Like RNNs, feed-forward neural networks are artificial neural networks that pass information from one end to the other end of the architecture. A feed-forward neural network can perform simple classification, regression, or recognition tasks, but it can’t remember the previous input that it has processed. For example, it forgets Apple by the time its neuron processes the word is. The RNN overcomes this memory limitation by including a hidden memory state in the neuron.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Recurrent neural network vs. convolutional neural networks&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Convolutional neural networks are artificial neural networks that are designed to process spatial data. You can use convolutional neural networks to extract spatial information from videos and images by passing them through a series of convolutional and pooling layers in the neural network. RNNs are designed to capture long-term dependencies in sequential data&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do transformers overcome the limitations of recurrent neural networks?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Transformers are deep learning models that use self-attention mechanisms in an encoder-decoder feed-forward neural network. They can process sequential data the same way that RNNs do. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Self-attention&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Transformers don’t use hidden states to capture the interdependencies of data sequences. Instead, they use a self-attention head to process data sequences in parallel. This enables transformers to train and process longer sequences in less time than an RNN does. With the self-attention mechanism, transformers overcome the memory limitations and sequence interdependencies that RNNs face. Transformers can process data sequences in parallel and use positional encoding to remember how each input relates to others. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Parallelism&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Transformers solve the gradient issues that RNNs face by enabling parallelism during training. By processing all input sequences simultaneously, a transformer isn’t subjected to backpropagation restrictions because gradients can flow freely to all weights. They are also optimized for parallel computing, which graphic processing units (GPUs) offer for generative AI developments. Parallelism enables transformers to scale massively and handle complex NLP tasks by building larger models. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How can AWS support your RNN requirements?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Amazon SageMaker:&lt;/strong&gt; is a fully managed service to prepare data and build, train, and deploy ML models for any use case. It has fully managed infrastructure, tools, and workflows.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Amazon Bedrock:&lt;/strong&gt; simplifies generative AI development by enabling the customization and deployment of industry-leading foundation models securely and efficiently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;AWS Trainium:&lt;/strong&gt; is an ML accelerator that you can use to train and scale deep learning models affordably in the cloud. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;RNNs are a powerful tool for processing sequential data, and they have found widespread applications in various fields. Their ability to learn and remember patterns over time makes them well-suited for tasks that involve sequential data.&lt;/p&gt;

</description>
      <category>nlp</category>
      <category>genai</category>
    </item>
    <item>
      <title>Key differences in GPT3.5 VS GPT4.0</title>
      <dc:creator>Ravi</dc:creator>
      <pubDate>Wed, 11 Sep 2024 19:02:35 +0000</pubDate>
      <link>https://dev.to/ragoli86/key-differences-in-gpt35-vs-gpt40-ci9</link>
      <guid>https://dev.to/ragoli86/key-differences-in-gpt35-vs-gpt40-ci9</guid>
      <description>&lt;p&gt;GPT-3.5 and GPT-4 are both large language models developed by OpenAI, but they differ in several key areas:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Size and Architecture:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;GPT-3.5:&lt;/strong&gt; Has 175 billion parameters and a transformer architecture.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;GPT-4:&lt;/strong&gt; Is significantly larger and more complex, with an estimated 1 trillion parameters and a more advanced transformer architecture.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Capabilities:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Text Generation:&lt;/strong&gt; Both models are excellent at generating human-quality text, but GPT-4 is generally more creative and able to produce more diverse and nuanced responses.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;According to OpenAI, ChatGPT-4 is “82 percent less likely to respond to requests for disallowed content and 40 percent more likely to produce factual responses than GPT-3.5 on our internal evaluations”.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Problem-Solving:&lt;/strong&gt; GPT-4 is better at solving complex problems and understanding nuanced instructions.  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ChatGPT-4 also has a longer context window, or the amount of text it can process simultaneously. For example, you could ask ChatGPT-4 to analyze a document for you, and it can now process about 25,000 words at a time. Another version of the technology called ChatGPT-4 Turbo can process up to 128,000 words. With this feature, you could include a website link in your prompt and ask ChatGPT to consider that source when giving its answer. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Multimodality:&lt;/strong&gt; GPT-4 can process and generate text, code, and images, while GPT-3.5 is primarily text-based.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the previous version, you needed to write a prompt using text to generate an output from ChatGPT. With version 4, you can still use text, but you can also offer an image or even a voice command to make a request from the application. OpenAI’s example of this new feature is that you could put in a picture of the inside of your refrigerator, and ChatGPT-4 could suggest recipes you could make with the ingredients in the image. You can also speak to ChatGPT-4, and the AI will generate a voice to speak to you. Multimodality also allows ChatGPT to handle more functions, like captioning or translating videos. &lt;br&gt;
  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Knowledge Base:&lt;/strong&gt; GPT-4 has access to a larger knowledge base, allowing it to provide more informative and accurate responses.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other news regarding ChatGPT upgrades, the program can now access the internet in real-time and provide summaries or respond to links. Previously, you could not access the internet in real-time using ChatGPT, and the program was limited to a data set containing information available before 2021. OpenAI released this update on September 27, 2023, allowing ChatGPT users the ability to discuss current events, ask ChatGPT questions about websites, or suggest links to find more information. Currently, this new feature is only available to paid ChatGPT subscribers, but it is not exclusive to GPT-4. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Benchmarks:&lt;/strong&gt; GPT-4 has consistently outperformed GPT-3.5 on various benchmarks, demonstrating its superior capabilities.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Human-Level Performance:&lt;/strong&gt; In certain tests, GPT-4 has achieved human-level performance, surpassing previous AI models.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What is GPT-4 used for?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some of the things you can use ChatGPT for, such as solving math problems, writing essays, translating languages, or writing computer code. However, as the Microsoft research team pointed out, this technology has the potential for even greater applications. Here are some of the ways companies are using ChatGPT: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Be My Eyes: Bridging the Gap&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Purpose:&lt;/strong&gt; Assisting visually impaired individuals with everyday tasks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GPT-4's Role:&lt;/strong&gt; Powering a virtual volunteer service to provide assistance when human volunteers are unavailable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Benefit:&lt;/strong&gt; Enables real-time help for tasks like checking expiration dates, reading labels, or describing objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Morgan Stanley: Financial Expertise at Your Fingertips&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Purpose:&lt;/strong&gt; Providing expert financial advice and information.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GPT-4's Role:&lt;/strong&gt; Serving as a virtual financial advisor, accessing and processing vast amounts of financial data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Benefit:&lt;/strong&gt; Offers instant access to personalized financial advice and information, streamlining the process for clients.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Duolingo: Language Learning Made More Immersive&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Purpose:&lt;/strong&gt; Enhancing language learning through conversational practice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GPT-4's Role:&lt;/strong&gt; Simulating natural conversations with students, providing a more engaging and realistic learning experience.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Benefit:&lt;/strong&gt; Offers students the opportunity to practice speaking and listening skills in a more authentic setting.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Summary:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;GPT-4 represents a significant advancement over GPT-3.5 in terms of its size, capabilities, and performance. It is a more powerful and versatile tool for a wide range of applications, from content creation to complex problem-solving.&lt;/p&gt;

</description>
      <category>genai</category>
      <category>chatgpt</category>
      <category>llm</category>
    </item>
    <item>
      <title>Data Visualization Techniques for Text Data</title>
      <dc:creator>Ravi</dc:creator>
      <pubDate>Tue, 10 Sep 2024 01:00:45 +0000</pubDate>
      <link>https://dev.to/ragoli86/data-visualization-techniques-for-text-data-bh</link>
      <guid>https://dev.to/ragoli86/data-visualization-techniques-for-text-data-bh</guid>
      <description>&lt;p&gt;Python offers a variety of powerful libraries for creating visualizations, including word clouds, bar charts and histograms. These visualizations can be particularly useful for analyzing text data and gaining insights into word frequency, sentiment, and other characteristics.&lt;/p&gt;

&lt;p&gt;Let's perform the visualization of the text data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps to perform:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Load the Text Data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Preprocess the Text Data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create Word Cloud&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create Bar Chart&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create Histogram Chart&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;install nltk&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgp7i0t6et9dpv4ti9cs2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgp7i0t6et9dpv4ti9cs2.png" alt="install nltk" width="800" height="141"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will use the NLTK (Natural Language Toolkit) provides tools for text processing and analysis.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;importing nltk and download punkt&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftoy9s0evwcwv5tao49qp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftoy9s0evwcwv5tao49qp.png" alt="download punkt" width="800" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;import other required packages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We use the Seaborn package which a high-level data visualization library built on top of Matplotlib.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuy6s8jz537dwdn4ymn5t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuy6s8jz537dwdn4ymn5t.png" alt="import other packages" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;load the sample text data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9sv3y4nrs0eakwsze99t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9sv3y4nrs0eakwsze99t.png" alt="Sample text data" width="800" height="184"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Word Clouds&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Word clouds visually represent the frequency of words in a text by varying the size and position of words based on their importance.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;downloading package stopwords&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8l8yh0y84ou3cnecl0gz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8l8yh0y84ou3cnecl0gz.png" alt="download stopwords pkg" width="800" height="126"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;remove the stopwords from the text and create wordcloud and show&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F457ithq65umsqvzc4bhl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F457ithq65umsqvzc4bhl.png" alt="wordcloud" width="800" height="233"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxbaw1u683b5bvcdx61ey.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxbaw1u683b5bvcdx61ey.png" alt="wordcloud image" width="800" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See, this is how the visualization of this word cloud would look like. this has created the word cloud depending on the frequency of the word appearing the one that is in bigger size compared.&lt;/p&gt;

&lt;p&gt;Now, Let's see how can we create the bar chart.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bar Chart&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Bar charts are effective for visualizing the frequency of words or phrases in a text corpus.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fokdwfwvjy72ntqyomxn4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fokdwfwvjy72ntqyomxn4.png" alt="Barchart code" width="800" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bar chart&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Full5mg988uvxiyf0hv1d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Full5mg988uvxiyf0hv1d.png" alt="Bar Chart" width="800" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'll access the 10 most common words. We will create a plot using the above. Let's see how can we create histogram chart.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Histograms Chart&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Histograms can be used to visualize the distribution of word lengths or other numerical characteristics of text data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu7fwb1csaggcaz00td44.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu7fwb1csaggcaz00td44.png" alt="Histogram code" width="800" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx1z6als903u70velkfum.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx1z6als903u70velkfum.png" alt="histogram" width="581" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Additional Libraries:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Gensim: A library for topic modeling and document similarity.&lt;br&gt;
Seaborn: A high-level data visualization library built on top of Matplotlib.&lt;/p&gt;

&lt;p&gt;By combining these libraries and techniques, you can create informative and visually appealing visualizations to explore and understand the text data.&lt;/p&gt;

</description>
      <category>python</category>
      <category>seaborn</category>
      <category>nltk</category>
      <category>genai</category>
    </item>
  </channel>
</rss>
