Avi Chawla Profile picture
Apr 22 9 tweets 3 min read Read on X
Temperature in LLMs, clearly explained (with code):
Let's prompt OpenAI GPT-3.5 with a low temperature value twice.

It produces identical responses from the LLM.

Check the response below👇 Image
Now, let's prompt it with a high temperature value.

This time, it produces a gibberish output. Check the output below👇

What is going on here? Let's dive in! Image
Text-generating LLMs are like classification models whose output layer spans the entire vocabulary.

However, instead of selecting the best token, they "sample" the prediction.

So even if “Token 1” has the highest softmax score, it may not be chosen due to sampling👇 Image
The impact of sampling is controlled using the Temperature parameter.

Temperature introduces the following tweak in the softmax function 👇 Image
If the temperature is low, the probabilities look like a max value instead of a “soft-max” value.

This means the sampling process will almost certainly choose the token with the highest probability. This makes the generation process (nearly) greedy.

Check this👇 Image
If the temperature is high, the probabilities start to look like a uniform distribution:

This means the sampling process may select any token. This makes the generation process random and heavily stochastic, like we saw earlier.

Check this👇 Image
Some best practices for using temperature (T):

- Set a low T value to generate predictable responses.
- Set a high T value to generate more random and creative responses.
- An extremely high T value rarely has any real utility, as shown below👇 Image
That's a wrap!

If you enjoyed this tutorial:

Find me → @_avichawla

Every day, I share tutorials and insights on DS, ML, LLMs, and RAGs.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Avi Chawla

Avi Chawla Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @_avichawla

Apr 20
Function calling & MCP for LLMs, clearly explained (with visuals):
Before MCPs became popular, AI workflows relied on traditional Function Calling for tool access. Now, MCP is standardizing it for Agents/LLMs.

The visual below explains how Function Calling and MCP work under the hood.

Let's learn more!
In Function Calling:

- The LLM receives a prompt.
- The LLM decides the tool.
- The programmer implements a procedure to accept a tool call request and prepare a function call.
- A backend service executes the tool.

Let's implement this to understand better!
Read 10 tweets
Apr 19
10 AWESOME (and must-use) libraries for Python programmers:
1️⃣ CleanLab

You're missing out on a lot if you haven't started using Cleanlab yet!

Cleanlab helps you clean data and labels by automatically detecting issues in any dataset.

Check this👇 Image
2️⃣ Drawdata

Drawdata is a Python library that allows you to draw a 2-D dataset of any shape in a Jupyter Notebook.

Check this out👇
Read 12 tweets
Apr 14
5 open-source MCP servers that will give superpowers to your AI Agents:
1️⃣ Stagehand MCP Server

This MCP server (by @browserbasehq) provides web automation capabilities to AI Agents.

Combining Stagehand with Claude delivers an OpenAI Operator alternative that’s more controlled and reliable.

Fully open-source! Image
2️⃣ Jupyter MCP server

This MCP server lets you control Jupyter notebooks to:
- Create code cells
- Execute code cells
- Create markdown cells

Use this for complex use cases where you can analyze full datasets by just telling Claude Desktop a file path.
Read 7 tweets
Apr 13
10 MCP, AI Agents, and RAG projects for AI Engineers (with code):
1️⃣ MCP-powered Agentic RAG

In this project, you'll learn how to create an MCP-powered Agentic RAG that searches a vector database and falls back to web search if needed.

Check the full breakdown (with code) below👇
2️⃣ A multi-agent book writer

In this project, you'll build an Agentic workflow that can write a 20k word book from a 3-5 word book title.

Read the walk-through thread below👇
Read 12 tweets
Apr 8
Let's build an MCP-powered Agentic RAG (100% local):
Below, we have an MCP-driven Agentic RAG that searches a vector database and falls back to web search if needed.

To build this, we'll use:
- Bright Data to scrape web at scale.
- @qdrant_engine as the vector DB.
- @cursor_ai as the MCP client.

Let's build it!
Here's how it works:

1) The user inputs a query through the MCP client (Cursor).
2-3) The client contacts the MCP server to select a relevant tool.
4-6) The tool output is returned to the client to generate a response.

Let's implement this!
Read 12 tweets
Apr 7
Let's build a RAG app with Meta's latest Llama 4:
Meta just released multilingual and multimodal open-source LLMs.

Today, we're building a RAG app powered by @Meta's Llama4.

Tech stack:
- @Llama_Index for orchestration.
- @CerebrasSystems for blazing-fast Llama4 inference.
- @Streamlit for the UI.

Let's build it!
Before we dive into the code, here's a diagram that illustrates the key components & how they interact with each other!

It will be followed by detailed descriptions & code for each component:
Read 11 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(