|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Chat |
| 4 | +parent: Guides |
| 5 | +nav_order: 2 |
| 6 | +permalink: /guides/chat |
| 7 | +--- |
| 8 | + |
| 9 | +# Chatting with AI Models |
| 10 | + |
| 11 | +RubyLLM's chat interface provides a natural way to interact with various AI models. This guide covers everything from basic chatting to advanced features like multimodal inputs and streaming responses. |
| 12 | + |
| 13 | +## Basic Chat |
| 14 | + |
| 15 | +Creating a chat and asking questions is straightforward: |
| 16 | + |
| 17 | +```ruby |
| 18 | +# Create a chat with the default model |
| 19 | +chat = RubyLLM.chat |
| 20 | + |
| 21 | +# Ask a question |
| 22 | +response = chat.ask "What's the best way to learn Ruby?" |
| 23 | + |
| 24 | +# The response is a Message object |
| 25 | +puts response.content |
| 26 | +puts "Role: #{response.role}" |
| 27 | +puts "Model: #{response.model_id}" |
| 28 | +puts "Tokens: #{response.input_tokens} input, #{response.output_tokens} output" |
| 29 | +``` |
| 30 | + |
| 31 | +## Choosing Models |
| 32 | + |
| 33 | +You can specify which model to use when creating a chat: |
| 34 | + |
| 35 | +```ruby |
| 36 | +# Create a chat with a specific model |
| 37 | +chat = RubyLLM.chat(model: 'gpt-4o-mini') |
| 38 | + |
| 39 | +# Use Claude instead |
| 40 | +claude_chat = RubyLLM.chat(model: 'claude-3-5-sonnet-20241022') |
| 41 | + |
| 42 | +# Or change the model for an existing chat |
| 43 | +chat.with_model('gemini-2.0-flash') |
| 44 | +``` |
| 45 | + |
| 46 | +## Multi-turn Conversations |
| 47 | + |
| 48 | +Chats maintain conversation history automatically: |
| 49 | + |
| 50 | +```ruby |
| 51 | +chat = RubyLLM.chat |
| 52 | + |
| 53 | +# Start a conversation |
| 54 | +chat.ask "What's your favorite programming language?" |
| 55 | + |
| 56 | +# Follow up |
| 57 | +chat.ask "Why do you like that language?" |
| 58 | + |
| 59 | +# Continue the conversation |
| 60 | +chat.ask "What are its weaknesses?" |
| 61 | + |
| 62 | +# Access the conversation history |
| 63 | +chat.messages.each do |message| |
| 64 | + puts "#{message.role}: #{message.content[0..50]}..." |
| 65 | +end |
| 66 | +``` |
| 67 | + |
| 68 | +## Working with Images |
| 69 | + |
| 70 | +Vision-capable models can understand images: |
| 71 | + |
| 72 | +```ruby |
| 73 | +chat = RubyLLM.chat |
| 74 | + |
| 75 | +# Ask about an image (local file) |
| 76 | +chat.ask "What's in this image?", with: { image: "path/to/image.jpg" } |
| 77 | + |
| 78 | +# Or use an image URL |
| 79 | +chat.ask "Describe this picture", with: { image: "https://example.com/image.jpg" } |
| 80 | + |
| 81 | +# Include multiple images |
| 82 | +chat.ask "Compare these two charts", with: { |
| 83 | + image: ["chart1.png", "chart2.png"] |
| 84 | +} |
| 85 | + |
| 86 | +# Combine text and image |
| 87 | +chat.ask "Is this the Ruby logo?", with: { image: "logo.png" } |
| 88 | +``` |
| 89 | + |
| 90 | +## Working with Audio |
| 91 | + |
| 92 | +Models with audio capabilities can process spoken content: |
| 93 | + |
| 94 | +```ruby |
| 95 | +chat = RubyLLM.chat(model: 'gpt-4o-audio-preview') |
| 96 | + |
| 97 | +# Analyze audio content |
| 98 | +chat.ask "What's being said in this recording?", with: { |
| 99 | + audio: "meeting.wav" |
| 100 | +} |
| 101 | + |
| 102 | +# Ask follow-up questions about the audio |
| 103 | +chat.ask "Summarize the key points mentioned" |
| 104 | +``` |
| 105 | + |
| 106 | +## Streaming Responses |
| 107 | + |
| 108 | +For a more interactive experience, you can stream responses as they're generated: |
| 109 | + |
| 110 | +```ruby |
| 111 | +chat = RubyLLM.chat |
| 112 | + |
| 113 | +# Stream the response with a block |
| 114 | +chat.ask "Tell me a story about a Ruby programmer" do |chunk| |
| 115 | + # Each chunk is a partial response |
| 116 | + print chunk.content |
| 117 | + $stdout.flush # Ensure output is displayed immediately |
| 118 | +end |
| 119 | + |
| 120 | +# Useful for long responses or real-time displays |
| 121 | +chat.ask "Write a detailed essay about programming paradigms" do |chunk| |
| 122 | + add_to_ui(chunk.content) # Your method to update UI |
| 123 | +end |
| 124 | +``` |
| 125 | + |
| 126 | +## Temperature Control |
| 127 | + |
| 128 | +Control the creativity and randomness of AI responses: |
| 129 | + |
| 130 | +```ruby |
| 131 | +# Higher temperature (more creative) |
| 132 | +creative_chat = RubyLLM.chat.with_temperature(0.9) |
| 133 | +creative_chat.ask "Write a poem about Ruby programming" |
| 134 | + |
| 135 | +# Lower temperature (more deterministic) |
| 136 | +precise_chat = RubyLLM.chat.with_temperature(0.1) |
| 137 | +precise_chat.ask "Explain how Ruby's garbage collector works" |
| 138 | +``` |
| 139 | + |
| 140 | +## Access Token Usage |
| 141 | + |
| 142 | +RubyLLM automatically tracks token usage for billing and quota management: |
| 143 | + |
| 144 | +```ruby |
| 145 | +chat = RubyLLM.chat |
| 146 | +response = chat.ask "Explain quantum computing" |
| 147 | + |
| 148 | +# Check token usage |
| 149 | +puts "Input tokens: #{response.input_tokens}" |
| 150 | +puts "Output tokens: #{response.output_tokens}" |
| 151 | +puts "Total tokens: #{response.input_tokens + response.output_tokens}" |
| 152 | + |
| 153 | +# Estimate cost (varies by model) |
| 154 | +model = RubyLLM.models.find(response.model_id) |
| 155 | +input_cost = response.input_tokens * model.input_price_per_million / 1_000_000 |
| 156 | +output_cost = response.output_tokens * model.output_price_per_million / 1_000_000 |
| 157 | +puts "Estimated cost: $#{(input_cost + output_cost).round(6)}" |
| 158 | +``` |
| 159 | + |
| 160 | +## Registering Event Handlers |
| 161 | + |
| 162 | +You can register callbacks for chat events: |
| 163 | + |
| 164 | +```ruby |
| 165 | +chat = RubyLLM.chat |
| 166 | + |
| 167 | +# Called when a new assistant message starts |
| 168 | +chat.on_new_message do |
| 169 | + puts "Assistant is typing..." |
| 170 | +end |
| 171 | + |
| 172 | +# Called when a message is complete |
| 173 | +chat.on_end_message do |message| |
| 174 | + puts "Response complete!" |
| 175 | + puts "Used #{message.input_tokens + message.output_tokens} tokens" |
| 176 | +end |
| 177 | + |
| 178 | +# These callbacks work with both streaming and non-streaming responses |
| 179 | +chat.ask "Tell me about Ruby's history" |
| 180 | +``` |
| 181 | + |
| 182 | +## Multiple Parallel Chats |
| 183 | + |
| 184 | +You can maintain multiple separate chat instances: |
| 185 | + |
| 186 | +```ruby |
| 187 | +# Create multiple chat instances |
| 188 | +ruby_chat = RubyLLM.chat |
| 189 | +python_chat = RubyLLM.chat |
| 190 | + |
| 191 | +# Each has its own conversation history |
| 192 | +ruby_chat.ask "What's great about Ruby?" |
| 193 | +python_chat.ask "What's great about Python?" |
| 194 | + |
| 195 | +# Continue separate conversations |
| 196 | +ruby_chat.ask "How does Ruby handle metaprogramming?" |
| 197 | +python_chat.ask "How does Python handle decorators?" |
| 198 | +``` |
| 199 | + |
| 200 | +## Next Steps |
| 201 | + |
| 202 | +Now that you understand chat basics, you might want to explore: |
| 203 | + |
| 204 | +- [Using Tools]({% link guides/tools.md %}) to let AI use your Ruby code |
| 205 | +- [Streaming Responses]({% link guides/streaming.md %}) for real-time interactions |
| 206 | +- [Rails Integration]({% link guides/rails.md %}) to persist conversations in your apps |
0 commit comments