Conversations & Context
A single chat.ask is useful, but the real power is multi-turn conversations. The chat object automatically manages history — every message you send and every response you receive is remembered.
How Context Works
chat = RubyLLM.chat
chat.ask "My name is Alice"# => "Nice to meet you, Alice!"
chat.ask "What's my name?"# => "Your name is Alice!"Each ask sends the entire conversation history to the model. The AI sees every previous exchange naturally.
System Instructions
Set the AI’s behavior with with_instructions:
chat = RubyLLM.chatchat.with_instructions "You are a pirate. Always respond in pirate speak."chat.ask "What is Ruby?"# => "Arrr! Ruby be a fine programming language, matey! ..."You can also append to existing instructions:
chat.with_instructions "Keep responses under 2 sentences.", append: trueYour Task
Open conversations.rb and build a multi-turn conversation:
- Create a chat and set a system instruction with
with_instructions - Ask multiple follow-up questions — the AI should reference previous answers
- Print the conversation history with
chat.messages
chat = RubyLLM.chatchat.with_instructions("You are a friendly Ruby expert. Keep answers concise — under 3 sentences.")chat.with_temperature(0.7)
response = chat.ask("What are blocks in Ruby?")puts "A: #{response.content}"
response = chat.ask("Can you show me a simple example?")puts "A: #{response.content}"
response = chat.ask("How is that different from a Proc?")puts "A: #{response.content}"
# Print full historychat.messages.each do |msg| puts "[#{msg.role}] #{msg.content.to_s.lines.first&.strip}"endRun it:
$ ruby conversations.rbAccessing History
chat.messages returns every message in the conversation:
chat.messages.each do |msg| puts "[#{msg.role}] #{msg.content}"end# [system] You are a friendly Ruby expert...# [user] What are blocks?# [assistant] Blocks are anonymous functions...Each message has a role (:system, :user, or :assistant) and content.
Temperature Control
# Low — factual, consistent answerschat = RubyLLM.chat.with_temperature(0.2)
# High — creative, varied responseschat = RubyLLM.chat.with_temperature(0.9)Temperature ranges from 0 (deterministic) to 1+ (creative). Default is usually around 0.7.
Files
Preparing Environment
- Preparing Ruby runtime