<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Faiq Ahsan</title>
    <description>The latest articles on Forem by Faiq Ahsan (@faiqahsan).</description>
    <link>https://forem.com/faiqahsan</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%2F714133%2Ff2f59523-8524-4106-8a73-f4fd095b1cfe.png</url>
      <title>Forem: Faiq Ahsan</title>
      <link>https://forem.com/faiqahsan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/faiqahsan"/>
    <language>en</language>
    <item>
      <title>Build your own AI ChatBot on your machine</title>
      <dc:creator>Faiq Ahsan</dc:creator>
      <pubDate>Fri, 03 May 2024 11:23:17 +0000</pubDate>
      <link>https://forem.com/faiqahsan/build-your-own-ai-chatbot-on-your-machine-32f3</link>
      <guid>https://forem.com/faiqahsan/build-your-own-ai-chatbot-on-your-machine-32f3</guid>
      <description>&lt;p&gt;By now everyone knows and love ChatGPT and GenAI has taken the world by storm but do you know now you can build and run your own custom AI chatbot on your machine.&lt;/p&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%2Fmedia.giphy.com%2Fmedia%2FFkmgse8OMKn9C%2Fgiphy.gif%3Fcid%3Decf05e47w5wxug59dqaayf213tzdub14n6eojxs4cz1n5sg0%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" 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%2Fmedia.giphy.com%2Fmedia%2FFkmgse8OMKn9C%2Fgiphy.gif%3Fcid%3Decf05e47w5wxug59dqaayf213tzdub14n6eojxs4cz1n5sg0%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="really"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;YES! Let's take a look at the ingredients for this recipe.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://www.freecodecamp.org/news/learn-python-basics/" rel="noopener noreferrer"&gt;Python&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;If you are someone who is looking to dig deep into the AI / ML then you need to learn Python which is the go to programming language in this space. If you already know it then you are all set here otherwise i would suggest going through a python crash course or whatever suits you best and also make sure that you have python3 installed on your system.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://github.com/ollama/ollama?tab=readme-ov-file#ollama" rel="noopener noreferrer"&gt;Ollama&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Ollama is an awesome open source package which provides a really handy and easy way to run the large language models locally. We would be using this package to download and run the 8B version of Llama3.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://www.gradio.app/guides/quickstart" rel="noopener noreferrer"&gt;Gradio&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Gradio is the fastest way to demo your machine learning model with a friendly web interface so that anyone can use it.&lt;/p&gt;

&lt;p&gt;Ok so now lets start!!&lt;/p&gt;

&lt;h3&gt;
  
  
  Step1: Installing the Ollama
&lt;/h3&gt;

&lt;p&gt;Download and install the Ollama &lt;a href="https://github.com/ollama/ollama?tab=readme-ov-file#macos:~:text=language%20models%20locally.-,macOS,-Download" rel="noopener noreferrer"&gt;package&lt;/a&gt; on your machine. Once installed run the below command to pull the Llama3 8B version.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ollama pull llama3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default it downloads the 8B version if you want to run other version like 70B then simply append it after the name e.g llama3:70b. Check out the complete list &lt;a href="https://github.com/ollama/ollama?tab=readme-ov-file#macos:~:text=ollama%20run%20llama3-,Model%20library,-Ollama%20supports%20a" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Note: You should have at least 8 GB of RAM available to run the 7B models, 16 GB to run the 13B models, and 32 GB to run the 33B models.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step2: Creating custom model from Llama3
&lt;/h3&gt;

&lt;p&gt;Open up a code editor and create a file name &lt;code&gt;modelfile&lt;/code&gt; and paste the below content in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM llama3

## Set the Temperature

PARAMETER temperature 1

PARAMETER top_p 0.5

PARAMETER top_k 10

PARAMETER mirostat_tau 4.0

## Set the system prompt

SYSTEM """
You are a personal AI assistant named as Ultron created by Tony Stark. Answer and help around all the questions being asked.
"""

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Parameters
&lt;/h4&gt;

&lt;p&gt;Parameters dictates how your model responds and learn. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;temperature:&lt;/strong&gt; The temperature of the model. Increasing the temperature will make the model answer more creatively. (Default: 0.8)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;top_p:&lt;/strong&gt; Controls the balance between coherence and diversity of the output. A lower value will result in more focused and coherent text. (Default: 5.0)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;top_k:&lt;/strong&gt; Controls the balance between coherence and diversity of the output. A lower value will result in more focused and coherent text. (Default: 5.0)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;mirostat_tau:&lt;/strong&gt; Controls the balance between coherence and diversity of the output. A lower value will result in more focused and coherent text. (Default: 5.0)&lt;/p&gt;

&lt;p&gt;Check out all the available parameters and their purpose &lt;a href="https://github.com/ollama/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values:~:text=PARAMETER%20%3Cparameter%3E%20%3Cparametervalue%3E-,Valid%20Parameters%20and%20Values,-Parameter" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  System prompt
&lt;/h4&gt;

&lt;p&gt;Here you can play around and give any name and personality to your chatbot.&lt;/p&gt;

&lt;p&gt;Now let's create the custom model from the modelfile by running the below command. Provide a name of your choice e.g ultron.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ollama create ultron -f ./Modelfile
ollama run ultron
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You would see ultron running and ready to accept input prompt. Ollama has also REST API for running and managing the model so when you run your model it's also available for use on the below endpoint&lt;/p&gt;

&lt;p&gt;&lt;code&gt;http://localhost:11434/api/generate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We will be using this api to integrate with our Gradio chatbot UI.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step3: Create the UI for chatbot
&lt;/h3&gt;

&lt;p&gt;Initialize a python virtual environment by running the below commands.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 -m venv env
source env/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now install the required packages&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install requests gradio
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now create a python file app.py and paste the below code in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import requests
import json
import gradio as gr

model_api = "http://localhost:11434/api/generate"

headers = {"Content-Type": "application/json"}

history = []


def generate_response(prompt):
    history.append(prompt)
    final_prompt = "\n".join(history)  # append history
    data = {
        "model": "ultron",
        "prompt": final_prompt,
        "stream": False,
    }
    response = requests.post(model_api, headers=headers, data=json.dumps(data))
    if response.status_code == 200:  # successful
        response = response.text
        data = json.loads(response)
        actual_response = data["response"]
        return actual_response
    else:
        print("error:", response.text)


interface = gr.Interface(
    title="Ultron: Your personal assistant",
    fn=generate_response,
    inputs=gr.Textbox(lines=4, placeholder="How can i help you today?"),
    outputs="text",
)
interface.launch(share=True)

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

&lt;/div&gt;



&lt;p&gt;Now let's launch the app, run your python file &lt;code&gt;python3 app.py&lt;/code&gt; and your chatbot would be live on the below endpoint or similar. Please note that the response time may vary according to the your system's computing power.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;http://127.0.0.1:7860/&lt;/code&gt;&lt;/p&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%2Fmedia.giphy.com%2Fmedia%2FxjUlxQHkMBqSvLeSTy%2Fgiphy.gif%3Fcid%3Decf05e475wrst0zekqlny7114oguylcg67q92g5vzvrk20oe%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" 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%2Fmedia.giphy.com%2Fmedia%2FxjUlxQHkMBqSvLeSTy%2Fgiphy.gif%3Fcid%3Decf05e475wrst0zekqlny7114oguylcg67q92g5vzvrk20oe%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="amazing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There you have it! Your own chatbot running locally on your machine, you can even turn off the internet it would still work. Please share in the comments what other cools apps you are making with AI models.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>chatgpt</category>
      <category>machinelearning</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Beginner's guide to code with Generative AI and LLM</title>
      <dc:creator>Faiq Ahsan</dc:creator>
      <pubDate>Thu, 02 May 2024 14:16:47 +0000</pubDate>
      <link>https://forem.com/faiqahsan/beginners-guide-to-generative-ai-and-llm-56ci</link>
      <guid>https://forem.com/faiqahsan/beginners-guide-to-generative-ai-and-llm-56ci</guid>
      <description>&lt;p&gt;Ever since the launch of ChatGPT, every tech company is investing and integrating AI into their products and no surprise that everyone is jumping on the AI bandwagon but majority of the people who are using ChatGPT are not machine learning or AI experts so if you are just an average software engineer like me who is eager to learn more about this hype then by the end of this post you can also become that cool kid in your friend's group.&lt;/p&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%2Fmedia.giphy.com%2Fmedia%2F62PP2yEIAZF6g%2Fgiphy.gif%3Fcid%3D790b7611193gaf5a2lmnieixg6ao6bqucg05evbj3hqn56ft%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" 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%2Fmedia.giphy.com%2Fmedia%2F62PP2yEIAZF6g%2Fgiphy.gif%3Fcid%3D790b7611193gaf5a2lmnieixg6ao6bqucg05evbj3hqn56ft%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="Cool"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What are LLMs:
&lt;/h2&gt;

&lt;p&gt;LLMs stands for "Large Language Models." These are advanced artificial intelligence models designed to understand and generate human-like text. Large Language Models are trained on vast amounts of text data, allowing them to learn patterns, syntax, semantics, and context of language. They have the capability to generate coherent and contextually relevant responses to input text or prompts.&lt;/p&gt;

&lt;p&gt;ChatGPT is also built upon LLM (GPT 3.5, GPT4 etc) by OpenAI but GPT models are not open source and in order to integrate them in your applications you would have to use paid OpenAI API keys and also you can't further fine tune the GPT models but its not just OpenAI that is building LLMs other companies like Facebook and Google are also building their own models  in this AI arms race.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do LLMs operate?
&lt;/h2&gt;

&lt;p&gt;The foundational task underpinning the training of most cutting-edge LLMs revolves around word prediction, predicting the probability distribution of the next word given a sequence.&lt;/p&gt;

&lt;p&gt;For instance, when presented with the sequence "Listen to your ____," potential next words might include: heart, gut, body, parents, grandma, and so forth. This is typically represented as a probability distribution.&lt;/p&gt;

&lt;p&gt;Some prominent open source LLMs are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://llama.meta.com/llama3/" rel="noopener noreferrer"&gt;Llama&lt;/a&gt; by Facebook&lt;br&gt;
Gemma by Google&lt;br&gt;
Mistral by Mistral AI&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Few key point about LLMs
&lt;/h2&gt;

&lt;p&gt;When coming across LLMs you would often see something like Llama3 70B, Llama3 8b etc. That digits at the end is actually the number of parameters this model has so in the given case its 40 billion and 8 billions, yes BILLIONS!! Thats why they are so efficient with natural language processing.&lt;/p&gt;
&lt;h3&gt;
  
  
  Model size
&lt;/h3&gt;

&lt;p&gt;The model size is the number of parameters in the LLM. The more parameters a model has, the more complex it is and the more data it can process. So for example Llama3 70B is 40GB while Llama3 8b is 4.5GB. Larger models are also more computationally expensive to train and deploy and you would need high performing GPUs in order to run them.&lt;/p&gt;
&lt;h3&gt;
  
  
  Training data
&lt;/h3&gt;

&lt;p&gt;The training data is the dataset that the LLM is trained on. The quality and quantity of the training data has a significant impact on the performance of the model.&lt;/p&gt;
&lt;h3&gt;
  
  
  Hyperparameters
&lt;/h3&gt;

&lt;p&gt;Hyperparameters are settings that control how the LLM is trained. These settings can be fine-tuned to improve the performance of the model according to your specific needs.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;a href="https://huggingface.co/" rel="noopener noreferrer"&gt;HuggingFace&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;HuggingFace has an AI community and it's a very cool platform where you can find tons of open source models for different categories. On huggingFace you would find:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Models:&lt;/strong&gt; Open source LLMs&lt;br&gt;
&lt;strong&gt;Datasets:&lt;/strong&gt; Publicly available datasets to custom train your LLM)&lt;br&gt;
&lt;strong&gt;Spaces:&lt;/strong&gt; AI Apps developed by the community and deployed on HuggingFace&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;a href="https://huggingface.co/docs/transformers/index" rel="noopener noreferrer"&gt;Transformers Library&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The Transformers library developed by Hugging Face is a powerful and versatile open-source library. Transformers provides APIs and tools to easily download and train pretrained models. This is a very extensive package and offers a ton of functionality out of the box so definitely check it out.&lt;/p&gt;

&lt;p&gt;To give you a glimpse of it, below is the python code snippet which will use &lt;strong&gt;"microsoft/codereviewer"&lt;/strong&gt; model from huggingFace and review a javascript code file and share the suggestions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline

model_id = "microsoft/codereviewer"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)


def review_javascript_code(file_path):

    pipe = pipeline("text2text-generation", model="microsoft/codereviewer")

    with open(file_path, "r") as file:
        code = file.read()

    result = pipe(code, max_length=512, num_return_sequences=1)[0]["generated_text"]
    print(result)


javascript_file_path = "test.js"
review_javascript_code(javascript_file_path)

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

&lt;/div&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%2Fmedia.giphy.com%2Fmedia%2FjsxpFvPXEU2ByvfBkM%2Fgiphy.gif%3Fcid%3Decf05e47c9f3a14bgij3lylynsu2ynzcxrpnrrygrqglicre%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" 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%2Fmedia.giphy.com%2Fmedia%2FjsxpFvPXEU2ByvfBkM%2Fgiphy.gif%3Fcid%3Decf05e47c9f3a14bgij3lylynsu2ynzcxrpnrrygrqglicre%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="wao"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What's Next?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the next articles we will see how we can configure and run a LLM locally on our machine and how do we custom train them for our own specific tasks. Please share what other cool tools you came across in this space.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Related:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://dev.to/faiqahsan/build-your-own-ai-chatbot-on-your-machine-32f3"&gt;Build and run your own AI chatbot&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>chatgpt</category>
      <category>huggingface</category>
    </item>
  </channel>
</rss>
