<?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: Jun Yamog</title>
    <description>The latest articles on DEV Community by Jun Yamog (@jkyamog).</description>
    <link>https://dev.to/jkyamog</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%2F1246160%2F20b3dcd3-610f-4bbd-be99-69c09447fab3.jpeg</url>
      <title>DEV Community: Jun Yamog</title>
      <link>https://dev.to/jkyamog</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jkyamog"/>
    <language>en</language>
    <item>
      <title>Trying to Impress My Better Half with ML</title>
      <dc:creator>Jun Yamog</dc:creator>
      <pubDate>Mon, 26 Aug 2024 07:05:28 +0000</pubDate>
      <link>https://dev.to/jkyamog/trying-to-impress-my-better-half-with-ml-7h6</link>
      <guid>https://dev.to/jkyamog/trying-to-impress-my-better-half-with-ml-7h6</guid>
      <description>&lt;p&gt;Learning new concepts like Machine Learning (ML) can often feel superficial. However, when you encounter these concepts in real life, it can be fun and memorable. I want to share my story of how a simple request from my better half, Tina, led me to apply these ML concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fine Tuning&lt;/li&gt;
&lt;li&gt;Data Preparation&lt;/li&gt;
&lt;li&gt;Hyper Parameters&lt;/li&gt;
&lt;li&gt;Over Fitting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;NOTE: Jump to "Fun Images" at the bottom if you want to skip the text.&lt;/p&gt;

&lt;h1&gt;
  
  
  Back story
&lt;/h1&gt;

&lt;p&gt;About 6 months ago, I got curious about ML again. The first thing I tried is Stable Diffusion (SD1.5 and SDXL) using in ComfyUI. I believe to better understand ML I have to be a user of ML-enabled apps. Another way for me to understand it is to explain ML to Tina especially why it can be useful.  While I was generating images, Tina casually remarked, "Well, let's see if you can make images like me".  I tried a few things, face swap and control nets.  It was ok but she was very unimpressed.  So I left it and moved on with other boring things like RAG, Text Classification, watching more AI YouTube content until a few weeks ago.&lt;/p&gt;

&lt;h1&gt;
  
  
  Fine Tuning
&lt;/h1&gt;

&lt;p&gt;When I started to hear about Flux.1, I tried it and I was very impressed. With Flux.1 I was able to prompt engineer to generate images without using complicated ComfyUI workflows.  I tried to mimic my photos by using text-to-image and image-to-image.  After watching Matt Wolfe's fine-tuning video of Flux, and also seeing Levelsio date night Flux X/Twitter posts I tried fine tuning it.  The ability to fine tune meant I could use Tina's photos to train the AI to generate images to her likeness.  I gathered 23 photos of Tina and started to fine tune using Low-rank adaptation (LoRA) with a Flux-dev model at replicate.com.  Most of the images were just fine, but there were a few images that were very impressive. Tina was starting to get impressed, progress! She posted it on FB, some of her close friends and even my mom didn't realize it wasn't her until they read my comments.&lt;/p&gt;

&lt;h1&gt;
  
  
  Data Preparation
&lt;/h1&gt;

&lt;p&gt;There were issues, specifically when I generated full-body images. Tina, usually nearby would see some of the images, and she would comment on images that didn’t resemble her. Some comments were hilariously blunt and not politically correct to the point that I wouldn't share them. I realized I needed better Data Preparation to generate images of Tina in varying compositions true to her likeness.  From my initial 23 photos, I increased it to 57 photos.  Not only did I increase the quantity, but I also got a better variety of poses and sizes.  So I gave it another try and started training a version 2.  Version 2 generated better images of her, including the likeness of her body, expression and poses.  Tina is a frustrated fashion model, she had aspired to be one but never went for it.  So one of the things she wanted to see was what she would look like as a fashion model.  Not all the images were great, but some were very impressive, and it was plausible it was her.  The model also picked up some inspiration from the clothes she wore and her poses, not just her facial features.&lt;/p&gt;

&lt;h1&gt;
  
  
  Hyper Parameters
&lt;/h1&gt;

&lt;p&gt;Once Tina and I found an image with a composition we liked, I experimented on the hyper parameters.  To get the image closer to what Tina would look like, I took note of the random seed of a particular image, and used the seed to give it a consistent image generation.  I played around with: inference steps, guidance scale and lora scale to tweak the image to our liking.  I found that increasing inference steps made usually the face closer to Tina's features.  Changing guidance scale makes it closer to the prompts, while the Lora scale makes it closer to Tina's general looks.  I don't think you should just max out the parameters, as that usually creates worse images. I usually put back the default or slightly higher settings and unset the random seed when generating the next set of images. I would also lower them sometimes especially the guidance scale if I accept some flexibility.  &lt;/p&gt;

&lt;p&gt;NOTE: Hyper parameters are more complicated than what I currently understand them to be.  Please comment below your experience or deeper explanation of hyper parameters.  &lt;/p&gt;

&lt;h1&gt;
  
  
  Over Fitting
&lt;/h1&gt;

&lt;p&gt;One of the things in ML that can be easily overlooked is over fitting.  Fine tuning the model also had the effect of skewing the images towards everyone looking like Tina.  In practical terms, the model generates a world filled with Tinas, so everyone will look like her.  Over fitting for image generation can be fun, but models that have bigger implication or impact overfitting can be a serious issue. &lt;/p&gt;

&lt;p&gt;NOTE: Does anybody have a solution for this overfitting issue? I’d like to train using LoRA with more than one subject/style.&lt;/p&gt;

&lt;h1&gt;
  
  
  Closing Remarks
&lt;/h1&gt;

&lt;p&gt;Tina is slightly impressed, but I have more work to do. What she really wants is to clone herself. I need Artificial General Intelligence (AGI) to be release, when is it coming? I need it now, so I can fine-tune Tina on it. Although, I think the safety team will likely not allow this fine-tuning to happen - for the sake of the world! :)&lt;/p&gt;

&lt;h1&gt;
  
  
  Fun Images
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Control Nets in ComfyUI
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fudy287fo5yqhvzovrpqt.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fudy287fo5yqhvzovrpqt.JPG" alt="Side by side comparison"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I searched for the simplest images I had of Tina for the control net to have the best success. Here is Tina standing on the Te Paki sand dunes, wearing a plain dress with not much detail in the background. Meh results. &lt;/p&gt;

&lt;h2&gt;
  
  
  Fine Tuning using initial dataset
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fd199xpxhod9jrpp219fs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fd199xpxhod9jrpp219fs.png" alt="Collage v1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Very impressive half-body images, as I had several on my initial training photos.  I also made it look as if I took them with my camera in low aperture and low light scenario.  The low light also helped set the mood and hide some telltale signs of AI image generation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fine Tuning with better prepared data and tweaking hyper parameters
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fnqbntwx7zkw2kye57u5t.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fnqbntwx7zkw2kye57u5t.JPG" alt="Collage v2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Super impressive; it even gets Tina's body shape and poses.  With some tweaking of the parameters we made some images closer to her likeness.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overfitting
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fme9nt4jentr8uteutfzo.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fme9nt4jentr8uteutfzo.JPG" alt="Everyone is Tina"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each version of Tina (member of the band) had her pose.  The peace/V fingers.  The hand on the hips. The side pose. Every suspect in the fine tuned world will look like Tina.  Can the real Tina please step forward?&lt;/p&gt;

&lt;h2&gt;
  
  
  Dataset for training
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.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%2F534bhfz92hformihefj2.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F534bhfz92hformihefj2.jpeg" alt="Images Trained"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The images that were used to train.  This is where the model got Tina's facial features, poses and expression.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>ai</category>
      <category>learning</category>
      <category>datascience</category>
    </item>
    <item>
      <title>Fine-tuning LLAMA 3 for Text Classification with Limited Resources</title>
      <dc:creator>Jun Yamog</dc:creator>
      <pubDate>Sat, 20 Jul 2024 07:36:05 +0000</pubDate>
      <link>https://dev.to/jkyamog/fine-tuning-llama-3-for-text-classification-with-limited-resources-4i06</link>
      <guid>https://dev.to/jkyamog/fine-tuning-llama-3-for-text-classification-with-limited-resources-4i06</guid>
      <description>&lt;p&gt;I recently needed to classify sentences for a particular use case at work. Remembering Jeremy Howard's &lt;a href="https://youtu.be/toUgBQv1BT8?si=xZR_y7nE1rBJzsqp" rel="noopener noreferrer"&gt;Lesson 4: Getting started with NLP for absolute beginners&lt;/a&gt;, I first adapted his notebook to fine-tune DEBERTA.&lt;/p&gt;

&lt;p&gt;It worked, but not to my satisfaction, so I was curious what would happen if I used a LLM like LLAMA 3. The problem? Limited GPU resources. I only had access to a Tesla/Nvidia T4 instance.&lt;/p&gt;

&lt;p&gt;Research led me to QLORA. This tutorial on &lt;a href="https://youtu.be/YJNbgusTSF0?si=jR0aB8QaSv7Wn3my" rel="noopener noreferrer"&gt;Fine tuning LLama 3 LLM for Text Classification of Stock Sentiment using QLoRA&lt;/a&gt; was particularly useful. To better understand the tutorial, I adapted Lesson 4 into the QLORA tutorial notebook.&lt;/p&gt;

&lt;p&gt;QLORA uses two main techniques:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Quantization: Reduces model precision, making it smaller.&lt;/li&gt;
&lt;li&gt;LORA (Low-Rank Adaptation): Adds small, trainable layers instead of fine-tuning the whole model.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This allowed me to train LLAMA 3 8B on a 16GB VRAM T4, using about 12GB of VRAM. The results were surprisingly good, with prediction accuracy over 90%.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Confusion Matrix:
[[83  4]
[ 4  9]]
Classification Report:
              precision    recall  f1-score   support
         0.0       0.95      0.95      0.95        87
         1.0       0.69      0.69      0.69        13
    accuracy                           0.92       100
   macro avg       0.82      0.82      0.82       100
weighted avg       0.92      0.92      0.92       100
Balanced Accuracy Score: 0.8231653404067196
Accuracy Score: 0.92
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's the &lt;a href="https://github.com/jkyamog/ml-experiments/blob/main/fine-tuning-qlora/LLAMA_3_Fine_Tuning_for_Sequence_Classification.ipynb" rel="noopener noreferrer"&gt;iPython notebook&lt;/a&gt; detailing the process.&lt;/p&gt;

&lt;p&gt;This approach shows it's possible to work with large language models on limited hardware. Working with constraints often leads to creative problem-solving and learning opportunities. In this case, the limitations pushed me to explore and implement more efficient fine-tuning techniques.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>python</category>
      <category>tutorial</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Using LangChain Expression Language (LCEL) for prompts and retrieval</title>
      <dc:creator>Jun Yamog</dc:creator>
      <pubDate>Sun, 03 Mar 2024 03:16:32 +0000</pubDate>
      <link>https://dev.to/jkyamog/using-langchain-expression-language-lcel-for-prompts-and-retrieval-47fn</link>
      <guid>https://dev.to/jkyamog/using-langchain-expression-language-lcel-for-prompts-and-retrieval-47fn</guid>
      <description>&lt;p&gt;In my previous post &lt;a href="https://dev.to/jkyamog/use-case-for-rag-and-llm-4pih"&gt;Use case for RAG and LLM&lt;/a&gt; my sample code only used basic string manipulation of the prompt.  On this post I will show how to use &lt;a href="https://python.langchain.com/docs/expression_language/get_started"&gt;LangChain Expression Language (LCEL)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Instead of string manipulation, LCEL offers a more effective alternative.  Here are the step by step conversion: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Instead of using python string interpolation:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;prompt&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="s"&gt;I need help on &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;use the same string without interpolation and a chat prompt template&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="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ChatPromptTemplate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_template&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;I need help on {context}&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;We can directly use the vector store as a retriever within a sub-chain, simplifying the search and integration process.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;retriever&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vector_store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;as_retriever&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;search_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;similarity&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;context_subchain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;itemgetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;user_query&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;retriever&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Finally combine the prompts, retriever and output processing in a chain.  RunnablePassthrough is used for the user_query  is supplied when the chain is invoked.  itemgetter is use for llm_personality which will be substituted from a disctionary passed on the chain's invocation.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;chain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&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;context&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;context_subchain&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;user_query&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RunnablePassthrough&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; 
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;llm_personality&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;itemgetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;llm_personality&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="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;prompt&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;StrOutputParser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is as sample code is written using LCEL&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="n"&gt;template_system&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;

Use the following information to answer the user&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s query:

{context}
&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

&lt;span class="n"&gt;template_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
User&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s query:

{user_query}
&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

&lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ChatPromptTemplate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_messages&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
        &lt;span class="n"&gt;SystemMessagePromptTemplate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_template&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;template_system&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;HumanMessagePromptTemplate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_template&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;template_user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="n"&gt;retriever&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vector_store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;as_retriever&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;search_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;similarity&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;context_subchain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;itemgetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;user_query&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;retriever&lt;/span&gt;

&lt;span class="n"&gt;chain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&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;context&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;context_subchain&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;user_query&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RunnablePassthrough&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; 
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;llm_personality&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;itemgetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;llm_personality&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="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;prompt&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;StrOutputParser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;)&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;chain&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&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;user_query&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;user_query&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;prompt_placeholders&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see a more complete &lt;a href="https://github.com/jkyamog/ml-experiments/commit/3d5494e5364b3c58d4cacdcd214fe797df93b4ad?diff=split&amp;amp;w=1"&gt;commit diff&lt;/a&gt; from old string manipulation to LCEL&lt;/p&gt;

</description>
      <category>ai</category>
      <category>python</category>
      <category>tutorial</category>
      <category>learning</category>
    </item>
    <item>
      <title>Use case for RAG and LLM</title>
      <dc:creator>Jun Yamog</dc:creator>
      <pubDate>Thu, 01 Feb 2024 08:16:21 +0000</pubDate>
      <link>https://dev.to/jkyamog/use-case-for-rag-and-llm-4pih</link>
      <guid>https://dev.to/jkyamog/use-case-for-rag-and-llm-4pih</guid>
      <description>&lt;h1&gt;
  
  
  The Challenge
&lt;/h1&gt;

&lt;p&gt;Tackling an interesting problem: given a user query, search through a PDF document and provide feedback on how well the query aligns with the document's content.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Solution
&lt;/h1&gt;

&lt;p&gt;The approach is a three-step process: Load &amp;amp; Index, Search &amp;amp; RAG, and Feedback Generation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Load &amp;amp; Index
&lt;/h2&gt;

&lt;p&gt;First, I need to understand the PDF document. I do this by creating a "semantic index". It's like creating a map of the document, but instead of landmarks, we have vectors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Search &amp;amp; RAG
&lt;/h2&gt;

&lt;p&gt;Next, I take your query and find the most related parts in the PDF document. This is where RAG (Retrieval Augmentation Generation) comes in. It's like giving the system a cheat sheet before the big test.&lt;/p&gt;

&lt;h2&gt;
  
  
  Feedback Generation
&lt;/h2&gt;

&lt;p&gt;Finally, I generate feedback for you. This isn't just a simple "yes" or "no". I provide detailed feedback with references from the PDF document. It's like having footnotes for your query.&lt;/p&gt;

&lt;h1&gt;
  
  
  Dive Deeper
&lt;/h1&gt;

&lt;p&gt;The code is open for you to explore. Feel free to fork it, and see how it fits your use case. I'll break down each section and highlight key points.&lt;/p&gt;

&lt;p&gt;If you have any suggestions or improvements, don't hesitate to share. For more technical details, continue reading along this &lt;a href="https://github.com/jkyamog/ml-experiments/blob/main/document-feedback/document-feedback.ipynb"&gt;Jupyter notebook&lt;/a&gt;.  &lt;/p&gt;

</description>
      <category>ai</category>
      <category>learning</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Reviving an old miner for machine learning</title>
      <dc:creator>Jun Yamog</dc:creator>
      <pubDate>Wed, 03 Jan 2024 04:44:03 +0000</pubDate>
      <link>https://dev.to/jkyamog/reviving-an-old-miner-for-machine-learning-1326</link>
      <guid>https://dev.to/jkyamog/reviving-an-old-miner-for-machine-learning-1326</guid>
      <description>&lt;h2&gt;
  
  
  Short/TLDR version
&lt;/h2&gt;

&lt;p&gt;Last year, I got an old miner (a PC with multiple GPUs) for free, and I have been learning some Machine Learning recently. I decided to revive the old miner and use it for ML. It was fun and rewarding, and it reminded me of my interest for PCs. However, I think renting a cloud computer with GPU is a better option, unless you have a free PC and want to experiment. Some cloud services, like Google’s &lt;a href="https://colab.research.google.com/"&gt;Colab&lt;/a&gt;, are even free.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;I have been learning Machine Learning (ML) from Jeremy Howard’s &lt;a href="https://course.fast.ai/"&gt;FastAI's course&lt;/a&gt;. The course recommends using cloud computers for learning ML, as they are more convenient and powerful. However, I wanted to run my Jupyter notebooks locally, by using an old miner that I got for free. My work mate left me this miner (equipped with 4 GPUs), it was too big bring it along when he moved to another city.&lt;/p&gt;

&lt;h2&gt;
  
  
  1st challenge: No hard drive and OS.
&lt;/h2&gt;

&lt;p&gt;The old miner did not have a hard drive and an operating system (OS). My old Linux HDD that I had as a backup failed to boot, so I bought a new cheap 2TB HDD to start with. I also wanted to learn how to boot over the network using PXE boot.&lt;/p&gt;

&lt;p&gt;I chose Iventoy as my PXE boot server, because it seemed simple and easy to use. I had an old Thinkpad with Ubuntu installed, which I used as the host machine. I connected the Thinkpad and the miner with a cross cable. The general steps I followed were:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Download Iventoy&lt;/li&gt;
&lt;li&gt;Download ISO and copy to ISO directory&lt;/li&gt;
&lt;li&gt;Start Iventoy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This guide helped me to understand the basic steps &lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://www.youtube.com/watch?si=eeZ7VVCK5ZpFgcqk&amp;amp;v=jZYbFcQQ3x8&amp;amp;feature=youtu.be" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--vj7aXn2m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ytimg.com/vi/jZYbFcQQ3x8/maxresdefault.jpg" height="450" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://www.youtube.com/watch?si=eeZ7VVCK5ZpFgcqk&amp;amp;v=jZYbFcQQ3x8&amp;amp;feature=youtu.be" rel="noopener noreferrer" class="c-link"&gt;
          Create a PXE Boot Server for your computer network! - YouTube
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          I show you how to use iVentoy to setup your own PXE Network Boot server rather than having to use a USB Stick to boot your PC and install your operating syst...
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://res.cloudinary.com/practicaldev/image/fetch/s--DOQSfsUf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://www.youtube.com/s/desktop/bcd251ee/img/favicon.ico" width="16" height="16"&gt;
        youtube.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;I encountered some minor issues, such as the miner having only 4GB RAM (I later upgraded it to 16GB). The PXE boot process would copy the ISO image to the RAM, which meant that larger ISOs like Ubuntu 22.04 would not fit. I decided to try Proxmox, which had a fairly small ISO.&lt;/p&gt;

&lt;h2&gt;
  
  
  2nd challenge: Accessing the GPUs in virtualization.
&lt;/h2&gt;

&lt;p&gt;I chose Proxmox as my OS, because it had a small ISO and it worked with PXE boot. I also wanted to have the flexibility to experiment with different virtual machines on the miner. However, this option also posed a new challenge: how to access the GPUs from the guest OS.&lt;/p&gt;

&lt;p&gt;At first, Proxmox would not install, because it said that the hardware did not support virtualization. I thought the miner was too old for that. But after checking the Proxmox requirements and the Intel ARK specs, I realized that it should work. I just had to enable some settings in the BIOS:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Intel VT - required for hypervisors like Proxmox&lt;/li&gt;
&lt;li&gt;VT-d - required interrupt remapping for PCI passthrough&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After Proxmox was installed, I had to make the guest OS use the four GPUs exclusively. This required a PCI passthrough, which allows the guest OS to directly access the hardware devices. The following guides were very helpful:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pve.proxmox.com/wiki/PCI_Passthrough"&gt;PCI Passthrough - Proxmox VE&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pve.proxmox.com/pve-docs/pve-admin-guide.html#qm_pci_passthrough"&gt;Proxmox VE Administration Guide&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This video also helped a lot &lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://www.youtube.com/watch?si=vl2oWRa2C562iYE5&amp;amp;v=i_2ZGcm4E_4&amp;amp;feature=youtu.be" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--1tphGG-Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ytimg.com/vi/i_2ZGcm4E_4/maxresdefault.jpg" height="450" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://www.youtube.com/watch?si=vl2oWRa2C562iYE5&amp;amp;v=i_2ZGcm4E_4&amp;amp;feature=youtu.be" rel="noopener noreferrer" class="c-link"&gt;
          Double GPU Passthrough in Proxmox 8!? Play Baldur's Gate 3 and Minecraft On The Same Machine? - YouTube
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Get the most out of your hardware, why not create a gaming VM, or use a GPU to accelerate tasks such as video rendering and Plex transcoding!?In this video I...
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://res.cloudinary.com/practicaldev/image/fetch/s--DOQSfsUf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://www.youtube.com/s/desktop/bcd251ee/img/favicon.ico" width="16" height="16"&gt;
        youtube.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;To verify that I got it working, I used nvidia-smi and pytorch:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E4N8zgAg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuv4k2quc4eksfq8esd1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E4N8zgAg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuv4k2quc4eksfq8esd1.png" alt="Nvidia SMI" width="800" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gfkEYnFq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dy8xduqaz43n9kz3kjsp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gfkEYnFq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dy8xduqaz43n9kz3kjsp.png" alt="Pytorch CUDA" width="212" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3rd problem: Finding a place for the big machine.
&lt;/h2&gt;

&lt;p&gt;I was so absorbed in getting the miner to work that I didn't think about where to put it. It was a big and noisy machine, and it needed a lot of power and cooling. I learned how to make all four GPUs run together, and I forked this &lt;a href="https://github.com/jkyamog/mac-ml-speed-test/commit/63963b13ae2b9f7cb916673495478f1a8abf9fc9"&gt;benchmark&lt;/a&gt; to test them with PyTorch DDP. I was able to train a model using all four GPUs, which was very satisfying. Comment below, if you want me to write about this in detail.&lt;/p&gt;

&lt;p&gt;My wife noticed that I was very happy with my project. She said, "You always enjoy fixing and solving things." She was very supportive of me and suggested that we find a place for the miner. We decided to get a glass coffee table and put the miner underneath it for now. It looked nice, as my daughter said, "It looks legit dad!".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6ejdz929--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhgiv0mg11yy10te4eys.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6ejdz929--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhgiv0mg11yy10te4eys.jpg" alt="Miner under a coffee table" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>linux</category>
      <category>proxmox</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
