Writing a Simple Static Prompt App with Streamlit using langchain.

Bhaskar RBhaskar R
3 min read

Hello readers in this article I am going to explain you How to Write a Simple Static Prompt App with Streamlit using langchain. Before going to learn let first understand what is mean by Static Prompt.

A static prompt in Large Language Models (LLMs) is a pre-defined and fixed input to trigger a conversation with the model. It has only plain text with no templating or external information. User have to write fine details during input as to get desired reults.

A static prompt is where the prompt itself doesn’t change based on user input. As opposed to a dynamic prompt that will change based on user decisions, a static prompt is hardcoded and sent to the model without any changes.

import os
import streamlit as st
from dotenv import load_dotenv
from langchain.prompts import PromptTemplate
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint

# Load environment variables
load_dotenv()
hf_token = os.getenv("HUGGINGFACEHUB_ACCESS_TOKEN")

# Initialize Hugging Face LLM
llm = HuggingFaceEndpoint(
    repo_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
    task="text-generation",
    huggingfacehub_api_token=hf_token,
    max_new_tokens=50
)

model = ChatHuggingFace(llm=llm)

# Streamlit UI
st.title("Summarizer Research Tool")

# User input
user_input = st.text_area("Enter your prompt:")

# Define static prompt using PromptTemplate
template = PromptTemplate(
    template="""You are an AI assistant. Please respond in a clear and concise manner.

    User request: {user_input}

    AI Response:
    """.strip(),
    input_variables=["user_input"]
)

# Summarization Button
if st.button('Generate Response'):
    if user_input:
        prompt = template.format(user_input=user_input)  # Fill template with user input
        result = model.invoke(prompt)
        st.subheader("Generated Response:")
        st.write(result.content)
    else:
        st.warning("Please enter a prompt.")

Now, let’s explore the Model step-by-step :

ChatHuggingFace, HuggingFaceEndpoint

Both ChatHuggingFace and HuggingFaceEndpoint are used to integrate Hugging Face models into LangChain, but they serve slightly different purposes.

  1. ChatHuggingFace :

    Specifically designed for chat models that follow conversational interfaces.

    Used with models like mistralai/Mistral-7B-Instruct or meta-llama/Llama-2-Chat.

    Supports automatic message history formatting (for chat-based interactions).

  2. HuggingFaceEndpoint :

    More general and can be used for both chat and non-chat models (e.g., text generation, summarization).

    Works with any Hugging Face text-generation API endpoint.

    Suitable for deploying custom models on Hugging Face Inference API.

load_dotenv

  1. load_dotenv() :

    This loads enviorment variables from a .env file into your python enviorment.

    The .env file typically contains sensitive information like API Keys.

  2. os.getenv() :

    retrives the API token stored in the .env file.

Creating LLM Model

  1. Creates an LLM (Language Model) instance using the HuggingFaceEndpoint class.

  2. Specifies the model to use: "TinyLlama/TinyLlama-1.1B-Chat-v1.0".

  3. Defines the task as "text-generation" (used for generating text-based responses).

  4. Authenticates using the API token (hf_token) retrieved from the .env file.

  5. Limits response length by setting max_new_tokens=50 (restricts output to 50 tokens).

Defining the static prompt

  1. Create a text input box in Streamlit
user_input = st.text_area("Enter your prompt:")
  • Allow user to enter prompt in multiline text-area and stores it in user_input variable.

  • eg, user_input = Write 5 lines on Apple (prompt stored in user_input variable)

  • Define a static prompt template using `PromptTemplate()

    Helps to structure the user Input(or prompt) in a pre-defined format, before sending it to the model (like TinyLlama).

  1. Set the template format
    template="""You are an AI assistant. Please respond in a clear and concise manner.

    User request: {user_input}

    AI Response:
    """.strip(),
  • template = (PromptStructure) # Defines the Static Structure of the prompt.

  • {user_input} is a placeholder that will be replaced dynamically.

  • strip() removes any unnecessary leading/trailing whitespace.

for user_input = “Write 5 lines on Apple“, the final prompt will be :

You are an AI assistant. Please respond in a clear and concise manner.

User request: Write 5 lines on the Apple

AI Response:
  1. Defining Variables
input_variables=["user_input"]
  • Specifies which variables will be dynamically inserted into the prompt.

  • user_input corresponds to {user_input} in the template.

  • When calling .format(user_input="your text"), it replaces {user_input}.

The formatted prompt is sent to the TinyLlama.

The model processes the request and generates a response.

1
Subscribe to my newsletter

Read articles from Bhaskar R directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Bhaskar R
Bhaskar R