Sub topic 7
Adding a system prompt and temperature control
This chapter assumes you can write a gr.ChatInterface app, understand the (message, history) contract, and have an OpenAI-compatible client working with streaming. Here you let the user change the system prompt and temperature from the page.
What additional inputs do
A plain gr.ChatInterface passes only message and history to your function. The additional_inputs parameter adds more components to the page. Gradio reads their current values on every submit and passes them to your function as extra positional arguments.
The order matters. The first extra component maps to the third parameter of your function, the second to the fourth, and so on.
The full example
import gradio as gr
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY from the environment
def respond(message, history, system_prompt, temperature):
messages = [{"role": "system", "content": system_prompt}]
messages += [{"role": h["role"], "content": h["content"]} for h in history]
messages.append({"role": "user", "content": message})
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
temperature=temperature,
stream=True,
)
reply = ""
for chunk in stream:
delta = chunk.choices[0].delta.content or ""
reply += delta
yield reply
demo = gr.ChatInterface(
fn=respond,
type="messages",
additional_inputs=[
gr.Textbox("You are a helpful assistant.", label="System prompt"),
gr.Slider(0, 1, value=0.7, label="Temperature"),
],
)
if __name__ == "__main__":
demo.launch()Run it and open the page. The two controls appear in a collapsed "Additional Inputs" section below the chat box. Open it, change the values, and send a message.
How the pieces work
- System prompt. The system message is built fresh on every call and placed first in the list. It is not stored in
history, so editing the textbox affects the next reply immediately, including in a conversation that is already under way. - Temperature. This setting controls how random the model's word choices are. Values near 0 give focused, repeatable answers. Higher values give more varied ones. The slider range of 0 to 1 is a safe common subset. Some providers accept values up to 2.
- Component values. The first argument of
gr.Textboxand thevalue=argument ofgr.Sliderset the starting values. Alabelmakes each control readable.
You can also tune the slider with step and minimum/maximum arguments. For example, gr.Slider(0, 2, value=0.7, step=0.1, label="Temperature") allows finer control over a wider range.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `TypeError: respond() missing 2 required positional arguments` | The function signature has no parameters for the extra inputs | Add one parameter per component, in the same order |
| The system prompt seems to be ignored | The prompt was added to `history` or placed after the user message | Put it first in the list you send to the model |
| Temperature has no visible effect | It is set to 0, or the model ignores it | Compare replies at 0 and 1 using the same message |
| Wrong values in the wrong variables | The component order differs from the parameter order | Match the order of `additional_inputs` to the parameters |
Exercise
Exercise
You add a third component, a gr.Number for max_tokens, to the end of additional_inputs. What change must you make to respond, and where do you use the value?
Show answer
Add a fourth extra parameter after temperature, for example def respond(message, history, system_prompt, temperature, max_tokens). Pass it to the request as max_tokens=int(max_tokens), because gr.Number can return a float.
Exercise
A user edits the system prompt halfway through a conversation. Does the earlier part of the chat change? Why or why not?
Show answer
No. Earlier replies are already stored in the history. The new prompt is applied only to later requests, because the function builds the system message from the textbox value on every call.