avatarQendel AI

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

6174

Abstract

span> onion, finely chopped

  • <span class="hljs-number">2</span> cloves garlic, minced
  • <span class="hljs-number">1</span> can of black beans, drained <span class="hljs-keyword">and</span> rinsed
  • <span class="hljs-number">1</span> can of corn, drained
  • <span class="hljs-number">1</span> cup of salsa
  • <span class="hljs-number">1</span> cup of shredded cheddar cheese (optional)
  • Salt <span class="hljs-keyword">and</span> pepper, to taste

Instructions:

<span class="hljs-number">1.</span> Preheat oven to <span class="hljs-number">375</span>°F (<span class="hljs-number">190</span>°C). <span class="hljs-number">2.</span> Cut off the tops of the bell peppers <span class="hljs-keyword">and</span> remove the cores <span class="hljs-keyword">and</span> seeds. Place them <span class="hljs-keyword">in</span> a baking dish. <span class="hljs-number">3.</span> In a medium saucepan, combine quinoa <span class="hljs-keyword">and</span> vegetable broth. Bring to a boil, then reduce heat to low <span class="hljs-keyword">and</span> simmer <span class="hljs-keyword">for</span> <span class="hljs-number">15</span>-<span class="hljs-number">20</span> minutes, <span class="hljs-keyword">or</span> until quinoa <span class="hljs-keyword">is</span> cooked <span class="hljs-keyword">and</span> the liquid has been absorbed. <span class="hljs-number">4.</span> In a sauté pan, heat butter over medium-high heat. Add the chopped onion <span class="hljs-keyword">and</span> garlic <span class="hljs-keyword">and</span> sauté <span class="hljs-keyword">for</span> <span class="hljs-number">3</span>-<span class="hljs-number">5</span> minutes, <span class="hljs-keyword">or</span> until the onion <span class="hljs-keyword">is</span> soft <span class="hljs-keyword">and</span> translucent. <span class="hljs-number">5.</span> Add black beans, corn, salsa, <span class="hljs-keyword">and</span> shredded cheddar cheese (<span class="hljs-keyword">if</span> using) to the sauté pan <span class="hljs-keyword">and</span> stir to combine. <span class="hljs-number">6.</span> Add the cooked quinoa to the sauté pan <span class="hljs-keyword">and</span> stir to combine. <span class="hljs-number">7.</span> Spoon the quinoa mixture into the bell peppers. <span class="hljs-number">8.</span> Bake <span class="hljs-keyword">for</span> <span class="hljs-number">20</span>-<span class="hljs-number">25</span> minutes, <span class="hljs-keyword">or</span> until the bell peppers are tender <span class="hljs-keyword">and</span> the filling <span class="hljs-keyword">is</span> hot. <span class="hljs-number">9.</span> Garnish <span class="hljs-keyword">with</span> fresh herbs <span class="hljs-keyword">or</span> additional cheese (<span class="hljs-keyword">if</span> using). <span class="hljs-number">10.</span> Serve <span class="hljs-keyword">and</span> enjoy!

Option <span class="hljs-number">2</span>: Stuffed Portobello Mushrooms

Ingredients:

  • <span class="hljs-number">4</span> large Portobello mushrooms
  • <span class="hljs-number">1</span> cup of cooked brown rice
  • <span class="hljs-number">1</span> can of black beans, drained <span class="hljs-keyword">and</span> rinsed
  • <span class="hljs-number">1</span> can of corn, drained
  • <span class="hljs-number">1</span>/<span class="hljs-number">2</span> cup of chopped fresh cilantro
  • <span class="hljs-number">1</span>/<span class="hljs-number">4</span> cup of chopped fresh parsley
  • Juice of <span class="hljs-number">1</span> lime
  • <span class="hljs-number">2</span> tablespoons olive oil
  • Salt <span class="hljs-keyword">and</span> pepper, to taste
  • <span class="hljs-number">1</span> cup of grated Parmesan cheese (optional)

Directions:

<span class="hljs-number">1.</span> Preheat oven to <span class="hljs-number">375</span>°F (<span class="hljs-number">190</span>°C). <span class="hljs-number">2.</span> Remove the stems <span class="hljs-keyword">from</span> the Portobello mushrooms <span class="hljs-keyword">and</span> place them <span class="hljs-keyword">in</span> a baking dish. <span class="hljs-number">3.</span> In a small bowl, mix together cooked brown rice, black beans, corn, chopped cilantro, chopped parsley, lime juice, olive oil, salt, <span class="hljs-keyword">and</span> pepper. <span class="hljs-number">4.</span> Spoon the rice mixture into the center of each mushroom cap. <span class="hljs-number">5.</span> Cover the mushrooms <span class="hljs-keyword">with</span> foil <span class="hljs-keyword">and</span> bake <span class="hljs-keyword">for</span> <span class="hljs-number">20</span>-<span class="hljs-number">25</span> minutes, <span class="hljs-keyword">or</span> until the mushroom caps are tender <span class="hljs-keyword">and</span> the filling <span class="hljs-keyword">is</span> hot. <span class="hljs-number">6.</span> Remove foil <span class="hljs-keyword">and</span> sprinkle grated Parmesan cheese (<span class="hljs-keyword">if</span> using) over the mushroom caps. Return to oven <span class="hljs-keyword">and</span> bake <span class="hljs-keyword">for</span> an additional <span class="hljs-number">3</span>-<span class="hljs-number">5</span> minutes, <span class="hljs-keyword">or</span> until the cheese <span class="hljs-keyword">is</span> melted <span class="hljs-keyword">and</span> golden brown. <span class="hljs-number">7.</span> Serve <span class="hljs-keyword">and</span> enjoy!</s></pre></div><p id="4601">Looks like great vegetarian recipe suggestions, but I’ll leave it to the chefs to validate 😄 (let us know in the comments👇)</p><p id="5a91"><b>Prompt Test 2:</b></p><div id="f800"><pre>PROMPT= <span class="hljs-string">""" ### Instruction: Act as a currency expert.

Question:

Explain to me how Digital Currency works. Assume that I am a 5-year-old child.

Answer:

"""</span>

RESPONSE: <span class="hljs-string">""" ### Answer:\n Well, digital currency is like a special kind of money that can only be used on the internet. It's a little bit like having a toy store card that you can use to buy things. But instead of using it to buy toys, you can use it to buy things online like games, gadgets, or even pizza! Just like

Options

you need a real bank account to get money out of an ATM, you need a special digital wallet to use your digital currency. The cool thing about digital currency is that it doesn't just disappear after you use it. It keeps on your digital wallet so you can use it again and again! And the best part is, no one can take it away from you or steal it, because it's just a little number on a computer. So it's a great way to learn about Math and numbers as well!'</s>"""</span></pre></div><p id="5129">Those are pretty good results! You can experiment with more prompts later — now, let’s see how you can integrate the model with Langchain.</p><h1 id="9a9a">Integrating Mistral 7B Instruct model with Langchain</h1><p id="fe76"><b>Step 1:</b></p><p id="056f"><b>Create Text Generation Pipeline</b></p><p id="4f5a">Make sure that you have already installed and imported the above libraries, model, and tokenizer.</p><div id="47e2"><pre>text_generation_pipeline = transformers.pipeline( model=model, tokenizer=tokenizer, task=<span class="hljs-string">"text-generation"</span>, temperature=<span class="hljs-number">0.2</span>, repetition_penalty=<span class="hljs-number">1.1</span>, return_full_text=<span class="hljs-literal">True</span>, max_new_tokens=<span class="hljs-number">1000</span>,

)</pre></div><p id="a93c">You can tweak the parameters based on your system and use case.</p><p id="7579"><b>Step 2:</b></p><p id="4231"><b>Create an LLM Instance</b></p><div id="88c0"><pre>mistral_llm = HuggingFacePipeline(pipeline=text_generation_pipeline)</pre></div><p id="e276"><b>Step 3:</b></p><p id="b162"><b>Create Prompt Template</b></p><div id="9f21"><pre>prompt_template = <span class="hljs-string">"""

Instruction: Act as a LARGE Language Models Expert. I will ask you a QUESTION and give you an AUDIENCE PERSONA, and you will respond with an answer easily understandable by the AUDIENCE PERSONA.

AUDIENCE PERSONA:

{audience_persona}

QUESTION:

{question}

Answer:

"""</span></pre></div><p id="4d28"><b>Step 4:</b></p><p id="a8da"><b>Create Prompt and LLM Chain.</b></p><div id="3301"><pre><span class="hljs-comment"># Create prompt from prompt template </span> prompt = PromptTemplate( input_variables=[<span class="hljs-string">"audience_persona"</span>, <span class="hljs-string">"question"</span>], template=prompt_template, )

<span class="hljs-comment"># Create llm chain </span> llm_chain = LLMChain(llm=mistral_llm, prompt=prompt)</pre></div><p id="d947"><b>Step 5:</b></p><p id="acf0"><b>Run LLM Chain</b></p><p id="90f8">It is time to drive everything home — run the llm chain and get results.</p><div id="554f"><pre><span class="hljs-comment"># Input query to LLM</span> input_text = { <span class="hljs-string">"audience_persona"</span>: <span class="hljs-string">"A 5 year-old child"</span>, <span class="hljs-string">"question"</span>: <span class="hljs-string">"Explain how Large Language Models work."</span> }

<span class="hljs-comment"># Generate and print LLM response </span> response = llm_chain(input_text)

<span class="hljs-built_in">print</span>(resoponse_text[<span class="hljs-string">'text'</span>])</pre></div><p id="ab73">Response:</p><div id="60c9"><pre><span class="hljs-string">"""Large language models are like super smart computers that can understand and talk about anything! They're made up of lots of tiny computers working together, and they learn from all the things people write online. So when you ask them a question, they know what to say because they've seen so many different ways people have asked that question before. It's kind of like having a friend who knows everything and can help you with anything you need!"""</span></pre></div><p id="c25a">Amazing results!</p><p id="11d0">You have just seen how to get started with Mistral 7B Instruct Model and integrate it with the Langchain framework.</p><h1 id="a4f9">What’s Next</h1><p id="d309">Of course, you want to run some more tests for your use cases, but you have only scratched the surface.</p><p id="7151">My articles below show how to utilize Mistral 7B Instruct Model for your specific use case’s data and instructions.</p><div id="b30d" class="link-block"> <a href="https://readmedium.com/rag-pipeline-with-mistral-7b-instruct-model-a-step-by-step-guide-138df378a0c2"> <div> <div> <h2>RAG Pipeline With Mistral 7B Instruct Model: A step-by-step guide</h2> <div><h3>Building RAG pipeline with Mistral 7B Instruct Model: A beginner’s guide</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*h7jh61vz0fw-3hEm3Z8_CQ.png)"></div> </div> </div> </a> </div><div id="f77c" class="link-block"> <a href="https://readmedium.com/fine-tuning-mistral-7b-instruct-model-in-colab-a-beginners-guide-0f7bebccf11c"> <div> <div> <h2>Fine-tuning Mistral 7B Instruct Model in Colab: A Beginner’s Guide</h2> <div><h3>9 Easy Beginner Steps to Fine-tune Mistral 7B Instruct Model on Your Data</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*VZwNuaPWqs30K2wRl-nB0Q.png)"></div> </div> </div> </a> </div><p id="6fad" type="7">🎖️Thanks For Reading🎖️</p><p id="b085">⚡️LIGHT UP⚡️ this article with a C-L-A-P👏</p><p id="d2c7">🚀 F-O-L-L-O-W <b><i>Qendel AI</i></b> for<b> upcoming content 👇</b></p><p id="71cb"><i>💡 <a href="https://readmedium.com/rag-pipeline-with-mistral-7b-instruct-model-a-step-by-step-guide-138df378a0c2">Retrieval Augmented Generation with Mistral 7B Instruct Model</a></i></p><p id="ef38"><i>💡 <a href="https://readmedium.com/fine-tuning-mistral-7b-instruct-model-in-colab-a-beginners-guide-0f7bebccf11c">Finetuning Mistral 7B Instruct Model</a></i></p></article></body>

Photo created using Dalle-3.

Getting Started With Mistral 7B Instruct Model & Langchain Integration

“No LLM has been most popular > 2 months.”

Llama 2 has already been taking the Open Source LLM space by storm, but not anymore. Mistral 7B is a new open-source model that beats Llama models.

The Mistral 7B model

  • Outperforms Llama 2 13B on all benchmarks
  • Outperforms Llama 1 34B on many benchmarks
  • Approaches CodeLlama 7B performance on code while remaining good at English tasks
  • Uses Grouped-query attention (GQA) for faster inference
  • Uses Sliding Window Attention (SWA) to handle longer sequences at a smaller cost

Here’s Mistral 7B model’s performance comparison with the Llama-2 family:

Mistral 7B model performance comparison with the Llama-2 family. Source.

Mistral 7B also has an instruct fine-tuned (chat) version, and it outperforms all 7B models on MT-Bench — as shown below:

Mistral 7B Instruct Model comparison on MT Bench. Source.

Now, let’s dive into the steps to start with the Mistral 7B Instruct Model on Google Colab.

Getting Started With Mistral 7B Instruct Model

Step 1:

Install and Import Libraries

!pip install git+https://github.com/huggingface/transformers torch accelerate bitsandbytes langchain

If the libraries above aren’t working for you, please try the following:

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
python -m pip install bitsandbytes --prefer-binary --extra-index-url=https://jllllll.github.io/bitsandbytes-windows-webui
pip install transformers==4.34.0
pip install trl==0.7.1
pip install datasets==2.14.5Import libraries and set variables
# Import libraries 
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
from langchain.llms import HuggingFacePipeline
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate

device = 'cuda' if torch.cuda.is_available() else 'cpu'

Step 2:

Download the Mistral 7B Instruct Model and Tokenizer

In my case, I am downloading a 4-bit version of the model to fit on my limited Colab GPU, but you can download an 8-bit version or the full model as long as your machine can handle it.

model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1", load_in_4bit=True, device_map='auto')
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")

Step 3:

Time to SHINE — Prompting Mistral 7B Instruct Model

I used prompts from my previous articles, Persona Prompt Pattern and Audience Persona Pattern, to compare results.

PROMPT Test 1:

# Test prompt 1 
vegeterian_recipe_prompt = """### Instruction: Act as a gourmet chef. 
I have a friend coming over who is a vegetarian.
I want to impress my friend with a special vegetarian dish. 
What do you recommend?

Give me two options, along with the whole recipe for each.

 ### Answer:
 """

encoded_instruction = tokenizer(vegeterian_recipe_prompt,  return_tensors="pt", add_special_tokens=True)

model_inputs = encoded_instruction.to(device)

generated_ids = model.generate(**model_inputs, max_new_tokens=1000, do_sample=True, pad_token_id=tokenizer.eos_token_id)
decoded = tokenizer.batch_decode(generated_ids)
print(decoded[0])

Here’s the response:

Great! I recommend trying one of these two vegetarian dishes. 

Option 1: Quinoa Stuffed Peppers

Ingredients:
 
 * 4 fresh bell peppers (any color)
* 1 cup quinoa, rinsed
* 2 cups vegetable broth
* 1 onion, finely chopped
* 2 cloves garlic, minced
* 1 can of black beans, drained and rinsed
* 1 can of corn, drained
* 1 cup of salsa
* 1 cup of shredded cheddar cheese (optional)
* Salt and pepper, to taste

Instructions:

1. Preheat oven to 375°F (190°C).
2. Cut off the tops of the bell peppers and remove the cores and seeds. Place them in a baking dish.
3. In a medium saucepan, combine quinoa and vegetable broth. Bring to a boil, then reduce heat to low and simmer for 15-20 minutes, or until quinoa is cooked and the liquid has been absorbed.
4. In a sauté pan, heat butter over medium-high heat. Add the chopped onion and garlic and sauté for 3-5 minutes, or until the onion is soft and translucent.
5. Add black beans, corn, salsa, and shredded cheddar cheese (if using) to the sauté pan and stir to combine.
6. Add the cooked quinoa to the sauté pan and stir to combine.
7. Spoon the quinoa mixture into the bell peppers.
8. Bake for 20-25 minutes, or until the bell peppers are tender and the filling is hot.
9. Garnish with fresh herbs or additional cheese (if using).
10. Serve and enjoy!

Option 2: Stuffed Portobello Mushrooms

Ingredients:

* 4 large Portobello mushrooms
* 1 cup of cooked brown rice
* 1 can of black beans, drained and rinsed
* 1 can of corn, drained
* 1/2 cup of chopped fresh cilantro
* 1/4 cup of chopped fresh parsley
* Juice of 1 lime
* 2 tablespoons olive oil
* Salt and pepper, to taste
* 1 cup of grated Parmesan cheese (optional)

Directions:

1. Preheat oven to 375°F (190°C).
2. Remove the stems from the Portobello mushrooms and place them in a baking dish.
3. In a small bowl, mix together cooked brown rice, black beans, corn, chopped cilantro, chopped parsley, lime juice, olive oil, salt, and pepper.
4. Spoon the rice mixture into the center of each mushroom cap.
5. Cover the mushrooms with foil and bake for 20-25 minutes, or until the mushroom caps are tender and the filling is hot.
6. Remove foil and sprinkle grated Parmesan cheese (if using) over the mushroom caps. Return to oven and bake for an additional 3-5 minutes, or until the cheese is melted and golden brown.
7. Serve and enjoy!</s>

Looks like great vegetarian recipe suggestions, but I’ll leave it to the chefs to validate 😄 (let us know in the comments👇)

Prompt Test 2:

PROMPT= """ ### Instruction: Act as a currency expert.

### Question:
Explain to me how Digital Currency works. Assume that I am a 5-year-old child.

 ### Answer:
 """

RESPONSE:
""" ### Answer:\n
Well, digital currency is like a special kind of money that can only be used on the internet. 
It's a little bit like having a toy store card that you can use to buy things. 
But instead of using it to buy toys, you can use it to buy things online like games, gadgets, or even pizza! 
Just like you need a real bank account to get money out of an ATM, you need a special digital wallet to use your digital currency. 
The cool thing about digital currency is that it doesn't just disappear after you use it. 
It keeps on your digital wallet so you can use it again and again! 
And the best part is, no one can take it away from you or steal it, because it's just a little number on a computer. 
So it's a great way to learn about Math and numbers as well!'</s>"""

Those are pretty good results! You can experiment with more prompts later — now, let’s see how you can integrate the model with Langchain.

Integrating Mistral 7B Instruct model with Langchain

Step 1:

Create Text Generation Pipeline

Make sure that you have already installed and imported the above libraries, model, and tokenizer.

text_generation_pipeline = transformers.pipeline(
    model=model,
    tokenizer=tokenizer,
    task="text-generation",
    temperature=0.2,
    repetition_penalty=1.1,
    return_full_text=True,
    max_new_tokens=1000,

)

You can tweak the parameters based on your system and use case.

Step 2:

Create an LLM Instance

mistral_llm = HuggingFacePipeline(pipeline=text_generation_pipeline)

Step 3:

Create Prompt Template

prompt_template = """
### Instruction: Act as a LARGE Language Models Expert. I will ask you a QUESTION and give you an AUDIENCE PERSONA, and you will respond with an answer easily understandable by the AUDIENCE PERSONA.

### AUDIENCE PERSONA:
{audience_persona}

### QUESTION:
{question}

 ### Answer:
 """

Step 4:

Create Prompt and LLM Chain.

# Create prompt from prompt template 
prompt = PromptTemplate(
    input_variables=["audience_persona", "question"],
    template=prompt_template,
)

# Create llm chain 
llm_chain = LLMChain(llm=mistral_llm, prompt=prompt)

Step 5:

Run LLM Chain

It is time to drive everything home — run the llm chain and get results.

# Input query to LLM
input_text = {
    "audience_persona": "A 5 year-old child",
    "question": "Explain how Large Language Models work."
}

# Generate and print LLM response 
response = llm_chain(input_text)


print(resoponse_text['text'])

Response:

"""Large language models are like super smart computers that can understand and talk about anything! 
They're made up of lots of tiny computers working together, and they learn from all the things people write online. 
So when you ask them a question, they know what to say because they've seen so many different ways people have asked that question before. 
It's kind of like having a friend who knows everything and can help you with anything you need!"""

Amazing results!

You have just seen how to get started with Mistral 7B Instruct Model and integrate it with the Langchain framework.

What’s Next

Of course, you want to run some more tests for your use cases, but you have only scratched the surface.

My articles below show how to utilize Mistral 7B Instruct Model for your specific use case’s data and instructions.

🎖️Thanks For Reading🎖️

⚡️LIGHT UP⚡️ this article with a C-L-A-P👏

🚀 F-O-L-L-O-W Qendel AI for upcoming content 👇

💡 Retrieval Augmented Generation with Mistral 7B Instruct Model

💡 Finetuning Mistral 7B Instruct Model

Large Language Models
Data Science
Llm
Machine Learning
Naturallanguageprocessing
Recommended from ReadMedium