Creativity as temperature in LLMs

The softmax function converts a numeric vector into a probabilty distribution
code
analysis
Author

David Harper, CFA, FRM

Published

May 11, 2024

I liked Logan Thorneloe’s swift explainer of LLM temperature in his What you need to understand about LLM creativity. Below I illustrate the softmax function with a dead-simple example.

Let’s assume our sentence begins “We live in Los Angeles, tomorrow we will travel to the …” where the possible location-destinations are found in the vector {beach, mountains, lake, mall, park, city}.

library(ggplot2)
library(tidyr)
library(reticulate)

softmax <- function(values, temps) {
  words <- names(values) 
  
  # Below is the softmax function
  exp_values <- exp(outer(values, 1 / temps))
  softmax_values <- sweep(exp_values, 2, colSums(exp_values), `/`)
  
  data.frame(
    temp = rep(temps, each = length(values)),
    word = rep(words, times = length(temps)),
    prob = c(softmax_values)
  )
}

# We live in Los Angeles, tomorrow we will travel to the ..
values <- c("beach"     = 9.1,
            "mountains" = 7.2, 
            "lake"      = 5.8,
            "mall"      = 3.4,
            "park"      = 2.3,
            "city"      = 1.5
           )

temps <- c(0.3, 1, 5, 10)
softmax_data <- softmax(values, temps)

# Plot
softmax_data |> ggplot(aes(y = reorder(word, -values[word]),
                           x = prob, fill = temp)) +
    geom_bar(stat = "identity") +
    facet_wrap(~temp, nrow = 2, ncol = 2, 
               labeller = labeller(temp = function(x) paste("Temp =", x))) +
    labs(title = "We live in Los Angeles, tomorrow we will travel to the ..",
         subtitle = "Horizontal axis is probability",
         x = NULL, y = NULL) +
    scale_fill_gradient(low = "darkseagreen4", high = "darkorange") + 
    theme_minimal() +
    theme(
        strip.text = element_text(face = "bold", size = 12), # facet labels
        axis.title.x = element_text(face = "bold", size = 12), # X-axis label
        axis.text.x = element_text(size = 11, face = "bold"),
        axis.text.y = element_text(size = 11, face = "bold")
    )

The above was my crafted example. Next I’ll conduct a tiny experiment to see how the temperature setting affects the output of the OpenAI language model (LLM) GPT-3.5. According to my co-pilot, “The temperature setting controls the randomness of the output generated by the model. A lower temperature setting results in more deterministic output, while a higher temperature setting leads to more creative and diverse responses.”

The first example is a financial advisor specializing in portfolio allocation. The second example will be a creative fiction writer using vivid imagery. Notice how I changed the system message to instruct the model to act as a creative writer.

# | output.wrap: true
# | output.frame.width: 300

from openai import OpenAI
import os

# Initialize the OpenAI client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

# Set the system message to instruct the model to act as a creative writer
system_message = {
    "role": "system",
    "content": "You are a financial advisor specializing in portfolio allocation. Your goal is to provide personalized investment recommendations based on an individual's risk tolerance, financial goals, and market conditions.",
}

# Define the user message
user_message = {
    "role": "user",
    "content": "Given an investor's risk tolerance of High and a target retirement age of 65, suggest an optimal portfolio allocation strategy.",
}


# Function to generate text with a specific temperature setting
def generate_text_with_temperature(prompt, temperature):
    completion = client.chat.completions.create(
        model="gpt-3.5-turbo-16k",
        messages=[system_message, user_message],
        temperature=temperature,
        max_tokens=200,  # Adjust max_tokens as needed
    )
    return completion.choices[0].message.content.strip()


# Generate text with different temperature settings
temperatures = [0.1, 0.8, 1.0, 1.5]
for temp in temperatures:
    generated_text = generate_text_with_temperature(user_message, temp)
    print(f"Temperature {temp}:\n{generated_text}\n")
Temperature 0.1:
Based on a high risk tolerance and a target retirement age of 65, an optimal portfolio allocation strategy may include a higher allocation to growth-oriented assets such as stocks and a smaller allocation to more conservative assets like bonds. Here's a suggested allocation:

1. Stocks: Allocate around 70% of the portfolio to a diversified mix of domestic and international stocks. This can be achieved through low-cost index funds or exchange-traded funds (ETFs) that track broad market indices.

2. Bonds: Allocate around 20% of the portfolio to bonds. Consider a mix of government bonds, corporate bonds, and/or bond funds. This allocation helps provide stability and income generation.

3. Cash and Cash Equivalents: Allocate around 10% of the portfolio to cash or cash equivalents. This can include high-yield savings accounts or short-term Treasury bills. Cash provides liquidity and acts as a buffer during market downturns.

It's important to note that this allocation is a general guideline and should be adjusted

Temperature 0.8:
Based on a high risk tolerance and a target retirement age of 65, here is an optimal portfolio allocation strategy:

1. Equities: Allocate a significant portion of the portfolio (around 70-80%) to diversified equity investments such as stocks or equity-based mutual funds. This allocation aims to maximize long-term growth potential, as equities tend to provide higher returns over the long run. However, keep in mind that this allocation comes with higher volatility and short-term fluctuations.

2. Bonds: Allocate a smaller portion of the portfolio (around 20-30%) to fixed-income investments such as bonds or bond-based mutual funds. Bonds provide stability and regular income, serving as a buffer against market downturns. They also help diversify the portfolio and reduce overall risk.

3. Real Estate: Consider diversifying the portfolio further by allocating a small portion (around 5-10%) to real estate investments such as Real Estate Investment Trusts (REITs) or real estate funds. Real estate

Temperature 1.0:
Based on your risk tolerance of High, it is reasonable to assume that you are comfortable with a higher level of volatility and have a longer time horizon for your investments. With a target retirement age of 65, you have a significant investment horizon, which means you can potentially earn higher returns over time by taking on more risk.

An optimal portfolio allocation strategy for you might include a higher allocation to equities and a smaller allocation to fixed income. The specific allocation percentages will depend on your individual circumstances, but here is a suggested allocation as a starting point:

Equities: 75%
Fixed Income: 25%

Within the equities allocation, it is advisable to diversify your investments across different asset classes and geographic regions to reduce risk. This may include allocating a portion of your equities to domestic and international stocks, as well as exposure to different sectors such as technology, healthcare, finance, etc.

For the fixed income allocation, consider investing in a mix of government bonds, corporate bonds, and other

Temperature 1.5:
Based on your risk tolerance of High and target retirement age of 65, an optimal portfolio allocation strategy could be:

1. Aggressive growth stocks: Allocate around 60-70% of your portfolio to aggressive growth stocks. These stocks have higher growth potential but also come with higher volatility.
2. Diversified equity mutual funds: Allocate around 15-25% of your portfolio to diversified equity mutual funds. These funds offer exposure to a broad range of stocks, which can help mitigate risk while still providing potential for growth.
3. Bonds: Allocate around 10-20% of your portfolio to bonds. This can include a mix of corporate bonds, treasury bonds, and municipal bonds. Bonds can provide more stability and act as a hedge against stock market volatility.
4. Real Estate Investment Trusts (REITs): Allocate around 5-10% of your portfolio to REITs. These investments can provide steady income from rental properties, while also offering potential for capital appreciation.
5
    
system_message = {
    "role": "system",
    "content": "You are a creative fiction writer who uses vivid imagery",
}
user_message = {
    "role": "user",
    "content": "Finish this sentence: She opened her cryptocurrency wallet and discovered ...",
}

temperatures = [0.1, 0.8, 1.0, 1.5]
for temp in temperatures:
    generated_text = generate_text_with_temperature(user_message, temp)
    print(f"Temperature {temp}:\n{generated_text}\n")
Temperature 0.1:
She opened her cryptocurrency wallet and discovered a kaleidoscope of digital currencies swirling in a mesmerizing dance of colors and shapes. Each coin, like a radiant gemstone, emitted a unique glow that illuminated the screen before her. Bitcoin, with its golden hue, stood tall and proud, exuding an air of dominance. Ethereum, adorned in a shimmering shade of azure, seemed to pulsate with untapped potential. Ripple, with its liquid silver sheen, appeared to ripple and flow like a tranquil river. Litecoin, a luminous silver orb, emitted a soft, ethereal glow, reminiscent of a moonlit night. And amidst this dazzling array, a myriad of other cryptocurrencies, each with its own distinct personality, formed a celestial tapestry of digital wealth.

As she delved deeper into her wallet, she discovered the intricate web of transactions, a labyrinthine network of connections that spanned the globe. Each transaction, like a shooting star, left a trail of shimmering stardust

Temperature 0.8:
She opened her cryptocurrency wallet and discovered a mesmerizing digital landscape, pulsating with vibrant colors and shimmering with a kaleidoscope of ever-changing patterns. The wallet, a portal into this virtual realm, seemed to breathe with a life of its own, as if it held the secrets of an enchanting universe.

Floating in the depths of this ethereal expanse were countless digital coins, each one a miniature constellation of light. Their luminescence danced and swirled, casting ethereal hues that reflected the essence of their respective blockchain networks. Bitcoin glowed with a golden radiance, representing the pioneering spirit that birthed the decentralized revolution. Ethereum shimmered with a soothing blue, evoking the innovative energy of its smart contracts and decentralized applications.

As she navigated through this digital cosmos, she uncovered a multitude of tokenized wonders. Tokens representing art, music, and literature floated gracefully, their beauty transcending the physical realm. NFTs, like celestial gems, sparkled with the

Temperature 1.0:
that it was a mesmerizing labyrinth of shimmering codes and colors. Each coin and token seemed to have a life of its own, pulsating with an ethereal glow that reflected the digital essence within. As she delved deeper into the depths of her wallet, the myriad of currencies unfolded like an enchanting kaleidoscope, beckoning her to explore the infinite possibilities of this alternate world.

With a gentle touch, she selected her prized Bitcoin. Like a radiant sun, it radiated a warm golden hue, exuding an air of stability and dominance among the others. As she moved her virtual fingers, she marveled at the intricate pattern of intricate lock and key combinations dancing across the surface. It was as if the essence of virtual value had materialized into an electric tapestry.

Beside the Bitcoin, a graceful Ethereum token rested, vibrant with a heavenly shade of blue. Evoking a sense of infinite potential, it seemed to ripple and shimmer like a mystical ocean captured within its digital boundaries

Temperature 1.5:
a dazzling galaxy of currencies swirling within it, each one emanating its own luminous pulse. Each digital coin was a shimmering orb of unique colors and patterns, casting a hypnotic spell upon her eager eyes. Like gems plucked from worlds yet explored, they sparkled with untapped potential. Silkily floating in the ether, they beckoned her to dive deeper into their mysterious enclave.

Amidst the electrifying atmosphere, Bitcoin stood poised in its timeless dominance - a massive ethereal monolith radiating golden warmth and historical significance. Ethereum followed closely, resembling a nebula painted with all the colors of introspection and newly ignited futures. Ripple, reminiscent of undulating helicoids in an outpouring tide resonating with liveliness joined silently. Stoke of Tezos within delivering cryptic codes coding freedom not coded. Ada a mathematical frontier us called victory length furereal Tiëbeurtsjong puzzle store assisted fortune sliding thought abstract radical Chaın soft uncträd Disk "you