Skip to content

Commit daa0258

Browse files
authored
Add example about how to setup system messages in the Rails integration (#54)
This PR adds an example on how to set up a system message using the Rails integrations. The example in the docs only covered POROs.
1 parent d34592e commit daa0258

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

docs/guides/chat.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ chat.add_message role: :system, content: "Always format your code using proper R
6767
# - Creating specialized assistants
6868
```
6969

70+
If you want to set up system prompts with persistence, please refer to the [Rails integration guide]({% link guides/rails.md %}#using-system-messages)
71+
7072
## Multi-turn Conversations
7173

7274
Chats maintain conversation history automatically:
@@ -275,4 +277,4 @@ Now that you understand chat basics, you might want to explore:
275277

276278
- [Using Tools]({% link guides/tools.md %}) to let AI use your Ruby code
277279
- [Streaming Responses]({% link guides/streaming.md %}) for real-time interactions
278-
- [Rails Integration]({% link guides/rails.md %}) to persist conversations in your apps
280+
- [Rails Integration]({% link guides/rails.md %}) to persist conversations in your apps

docs/guides/rails.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ chat.messages.order(:created_at).each do |message|
122122
end
123123
```
124124

125+
## Using System Messages
126+
127+
If you wish to use systems messages, you can create a Message with the role `:system` directly in the chat:
128+
129+
```ruby
130+
# Create a new chat
131+
chat = Chat.create!(model_id: 'gpt-4o-mini')
132+
133+
# Create a message with the system role
134+
chat.messages.create!(role: :system, content: "You are a helpful Ruby programming assistant. Always include code examples in your responses and explain them line by line.")
135+
136+
# Now the AI will follow these instructions in all responses
137+
chat.ask "How do I handle file operations in Ruby?"
138+
139+
# The response is automatically persisted
140+
puts chat.messages.last.content
141+
```
142+
125143
## Streaming Responses
126144

127145
You can stream responses while still persisting the final result:
@@ -394,4 +412,4 @@ Now that you've integrated RubyLLM with Rails, you might want to explore:
394412

395413
- [Using Tools]({% link guides/tools.md %}) to add capabilities to your chats
396414
- [Streaming Responses]({% link guides/streaming.md %}) for a better user experience
397-
- [Error Handling]({% link guides/error-handling.md %}) to handle AI service issues gracefully
415+
- [Error Handling]({% link guides/error-handling.md %}) to handle AI service issues gracefully

0 commit comments

Comments
 (0)