Skip to content

Commit a508707

Browse files
WIP: Adding rough views+controller to be used by the assistant generator
1 parent ea1a7ce commit a508707

File tree

8 files changed

+107
-3
lines changed

8 files changed

+107
-3
lines changed

lib/langchainrb_overrides/assistant.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ def load(id)
5858
llm: ar_assistant.llm,
5959
tools: tools,
6060
instructions: ar_assistant.instructions,
61-
tool_choice: ar_assistant.tool_choice
61+
# Default to auto to match the behavior of the original Langchain::Assistant
62+
tool_choice: ar_assistant.tool_choice || "auto"
6263
)
6364

6465
ar_assistant.messages.each do |ar_message|

lib/langchainrb_rails/generators/langchainrb_rails/assistant_generator.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ def migration_version
5151
"[#{::ActiveRecord::VERSION::MAJOR}.#{::ActiveRecord::VERSION::MINOR}]"
5252
end
5353

54+
def create_controller_file
55+
template "assistant/controllers/assistants_controller.rb", "app/controllers/assistants_controller.rb"
56+
end
57+
58+
def create_view_files
59+
template "assistant/views/index.html.erb", "app/views/assistants/index.html.erb"
60+
template "assistant/views/new.html.erb", "app/views/assistants/new.html.erb"
61+
template "assistant/views/show.html.erb", "app/views/assistants/show.html.erb"
62+
end
63+
64+
def add_routes
65+
route <<~EOS
66+
resources :assistants do
67+
member do
68+
post 'chat'
69+
end
70+
end
71+
EOS
72+
end
73+
5474
# TODO: Depending on the LLM provider, we may need to add additional gems
5575
# def add_to_gemfile
5676
# end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
class AssistantsController < ApplicationController
4+
def index
5+
@assistants = Assistant.all
6+
end
7+
8+
def new
9+
@assistant = Assistant.new
10+
end
11+
12+
def create
13+
@assistant = Assistant.new(assistant_params)
14+
if @assistant.save
15+
redirect_to @assistant, notice: 'Assistant was successfully created.'
16+
else
17+
render :new
18+
end
19+
end
20+
21+
def show
22+
@assistant = Assistant.find(params[:id])
23+
@messages = @assistant.messages
24+
end
25+
26+
def chat
27+
@assistant = Assistant.find(params[:id])
28+
message = @assistant.messages.create(role: 'user', content: params[:content])
29+
30+
langchain_assistant = Langchain::Assistant.load(@assistant.id)
31+
response = langchain_assistant.chat(params[:content])
32+
33+
@assistant.messages.create(role: 'assistant', content: response.content)
34+
35+
render json: { message: message, response: response.content }
36+
end
37+
38+
private
39+
40+
def assistant_params
41+
params.require(:assistant).permit(:instructions)
42+
end
43+
end

lib/langchainrb_rails/generators/langchainrb_rails/templates/assistant/migrations/create_assistants.rb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version
33
create_table :assistants do |t|
44
t.string :instructions
55
t.string :tool_choice
6-
t.json :tools
6+
t.json :tools, default: []
77
t.timestamps
88
end
99
end

lib/langchainrb_rails/generators/langchainrb_rails/templates/assistant/migrations/create_messages.rb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version
44
t.references :assistant, foreign_key: true
55
t.string :role
66
t.text :content
7-
t.json :tool_calls
7+
t.json :tool_calls, default: []
88
t.string :tool_call_id
99
t.timestamps
1010
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h1>Assistants</h1>
2+
3+
<%%= link_to 'New Assistant', new_assistant_path %>
4+
5+
<ul>
6+
<%% @assistants.each do |assistant| %>
7+
<li><%%= link_to assistant.instructions, assistant_path(assistant) %></li>
8+
<%% end %>
9+
</ul>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<h1>New Assistant</h1>
2+
3+
<%%= form_with(model: @assistant, local: true) do |form| %>
4+
<div>
5+
<%%= form.label :instructions %>
6+
<%%= form.text_area :instructions %>
7+
</div>
8+
9+
<%%= form.submit %>
10+
<%% end %>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<h1>Assistant: <%%= @assistant.instructions %></h1>
2+
3+
<div id="chat-messages">
4+
<%% @messages.each do |message| %>
5+
<p><strong><%%= message.role %>:</strong> <%%= message.content %></p>
6+
<%% end %>
7+
</div>
8+
9+
<%%= form_with(url: chat_assistant_path(@assistant), method: :post, remote: true) do |form| %>
10+
<%%= form.text_area :content %>
11+
<%%= form.submit 'Send' %>
12+
<%% end %>
13+
14+
<script>
15+
document.addEventListener('ajax:success', function(event) {
16+
const [data, _status, _xhr] = event.detail;
17+
const chatMessages = document.getElementById('chat-messages');
18+
chatMessages.innerHTML += `<p><strong>user:</strong> ${data.message.content}</p>`;
19+
chatMessages.innerHTML += `<p><strong>assistant:</strong> ${data.response}</p>`;
20+
});
21+
</script>

0 commit comments

Comments
 (0)