llm_meta_client 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd284d6a68a9bbad852fbcb19054ce2aeda512f0ffe17c2ac7029eb30907a7e4
4
- data.tar.gz: 5fe0cd2beef4c9bbc7fb75ee49099558a07626e0affa6b9ba9b065eca940dc67
3
+ metadata.gz: 95bb1f421088cc0c287b4f79e961455a1a5275b860b372fb23523ddc8781d37a
4
+ data.tar.gz: d4f92a150de0d9996d3ea6c54e69fe24989763f4bd359ce46a6b7ff69d36ecb7
5
5
  SHA512:
6
- metadata.gz: 53f9fcccc2ffee1c08ba3df22bf95e08bc857d8da0cd748f520f26a7144b03dded4a3ceb4ca67b3108fda12d79f521ed88e5ce0b30cb8b232ac5185941d9832f
7
- data.tar.gz: e5529bc832a453030d348cd8d01100bb664896d03cee385f83ee43b945c316b0c1d03d4227c65b23d6b360943e91e1c9727e7d52d1311b107eacf2bf7f58799f
6
+ metadata.gz: 480919bad702190fec4166d8a2659121ff62b1880123e6db168d9a1c8a8b7f80f23ca175e2e6f4a31b96e6bde97fd0723d95caa71439845a5017c351c18e373e
7
+ data.tar.gz: c2600201eccae124c9929eabcd8b7d755b7dbac4092cd5441c55d1caf79c839af8f0b41b49d7bf1c2e6a324e33c6bd1957d29a1ac3e837a7ab6924d727e8ae91
@@ -56,6 +56,7 @@ module LlmMetaClient
56
56
  def add_migrations
57
57
  migration_template "db/migrate/create_chats.rb", "db/migrate/create_chats.rb"
58
58
  migration_template "db/migrate/create_messages.rb", "db/migrate/create_messages.rb"
59
+ migration_template "db/migrate/migrate_llm_uuid_to_prompt_executions.rb", "db/migrate/migrate_llm_uuid_to_prompt_executions.rb"
59
60
  end
60
61
 
61
62
  def configure_routes
@@ -61,9 +61,7 @@ class ChatsController < ApplicationController
61
61
  # Find or create chat
62
62
  @chat = Chat.find_or_switch_for_session(
63
63
  session,
64
- current_user,
65
- llm_uuid: params[:api_key_uuid],
66
- model: params[:model]
64
+ current_user
67
65
  )
68
66
  add_chat @chat
69
67
  @messages = @chat&.ordered_messages || []
@@ -86,6 +84,7 @@ class ChatsController < ApplicationController
86
84
 
87
85
  # Add user message (will be rendered via turbo stream)
88
86
  @prompt_execution, @user_message = @chat.add_user_message(params[:message],
87
+ params[:api_key_uuid],
89
88
  params[:model],
90
89
  params[:branch_from_uuid])
91
90
  # Push to history for rendering
@@ -144,9 +143,7 @@ class ChatsController < ApplicationController
144
143
 
145
144
  @chat = Chat.find_or_switch_for_session(
146
145
  session,
147
- current_user,
148
- llm_uuid: params[:api_key_uuid],
149
- model: params[:model]
146
+ current_user
150
147
  )
151
148
  @messages = @chat&.ordered_messages || []
152
149
  # initialize history for the chat
@@ -184,6 +181,7 @@ class ChatsController < ApplicationController
184
181
 
185
182
  # Add user message (will be rendered via turbo stream)
186
183
  @prompt_execution, @user_message = @chat.add_user_message(params[:message],
184
+ params[:api_key_uuid],
187
185
  params[:model],
188
186
  params[:branch_from_uuid])
189
187
  # Push to history for rendering
@@ -6,23 +6,14 @@ class Chat < ApplicationRecord
6
6
 
7
7
  before_create :set_uuid
8
8
 
9
- validates :llm_uuid, presence: true
10
- validates :model, presence: true
11
-
12
9
  # Find existing chat from session or create new one
13
10
  class << self
14
- def find_or_switch_for_session(session, current_user, llm_uuid: nil, model: nil)
11
+ def find_or_switch_for_session(session, current_user)
15
12
  chat = find_by_session_chat_id(session, current_user)
16
- return chat if llm_uuid.nil? || model.nil?
17
-
18
- if chat.present?
19
- # Update LLM/model on existing chat if changed
20
- chat.update!(llm_uuid: llm_uuid, model: model) if chat.needs_reset?(llm_uuid, model)
21
- else
22
- chat = create!(user: current_user, llm_uuid: llm_uuid, model: model)
23
- session[:chat_id] = chat.id
24
- end
13
+ return chat if chat.present?
25
14
 
15
+ chat = create!(user: current_user)
16
+ session[:chat_id] = chat.id
26
17
  chat
27
18
  end
28
19
 
@@ -39,15 +30,8 @@ class Chat < ApplicationRecord
39
30
  end
40
31
  end
41
32
 
42
- # Get the LLM type for this chat
43
- def llm_type(jwt_token)
44
- llm_options = LlmMetaClient::ServerResource.available_llm_options(jwt_token)
45
- selected_llm = llm_options.find { |opt| opt[:uuid] == llm_uuid }
46
- selected_llm&.dig(:llm_type) || "unknown"
47
- end
48
-
49
33
  # Add a user message to the chat
50
- def add_user_message(message, model, branch_from_execution_id = nil)
34
+ def add_user_message(message, llm_uuid, model, branch_from_execution_id = nil)
51
35
  previous_id = if branch_from_execution_id.present?
52
36
  PromptNavigator::PromptExecution.find_by(execution_id: branch_from_execution_id)&.id
53
37
  else
@@ -55,6 +39,7 @@ class Chat < ApplicationRecord
55
39
  end
56
40
  prompt_execution = PromptNavigator::PromptExecution.create!(
57
41
  prompt: message,
42
+ llm_uuid: llm_uuid,
58
43
  model: model,
59
44
  configuration: "",
60
45
  previous_id: previous_id
@@ -70,9 +55,9 @@ class Chat < ApplicationRecord
70
55
 
71
56
  # Add assistant response by sending to LLM
72
57
  def add_assistant_response(prompt_execution, jwt_token, tool_ids: [], generation_settings: {})
73
- response_content = send_to_llm(jwt_token, tool_ids: tool_ids, generation_settings: generation_settings)
58
+ response_content = send_to_llm(prompt_execution, jwt_token, tool_ids: tool_ids, generation_settings: generation_settings)
74
59
  prompt_execution.update!(
75
- llm_platform: llm_type(jwt_token),
60
+ llm_platform: resolve_llm_type(prompt_execution.llm_uuid, jwt_token),
76
61
  response: response_content
77
62
  )
78
63
  new_message = messages.create!(
@@ -100,19 +85,24 @@ class Chat < ApplicationRecord
100
85
  .map(&:prompt_navigator_prompt_execution)
101
86
  end
102
87
 
103
- # Check if chat needs to be reset due to LLM or model change
104
- def needs_reset?(new_llm_uuid, new_model)
105
- llm_uuid != new_llm_uuid || model != new_model
106
- end
107
-
108
88
  private
109
89
 
90
+ # Resolve the LLM type (e.g. "openai", "google") from a given llm_uuid
91
+ def resolve_llm_type(llm_uuid, jwt_token)
92
+ llm_options = LlmMetaClient::ServerResource.available_llm_options(jwt_token)
93
+ selected_llm = llm_options.find { |opt| opt[:uuid] == llm_uuid }
94
+ selected_llm&.dig(:llm_type) || "unknown"
95
+ end
96
+
110
97
  # Summarize the user's prompt into a short title via LLM (required by ChatManager::TitleGeneratable)
111
98
  def summarize_for_title(prompt_text, jwt_token)
99
+ latest_pe = ordered_by_descending_prompt_executions.first
100
+ return nil unless latest_pe&.llm_uuid && latest_pe&.model
101
+
112
102
  LlmMetaClient::ServerQuery.new.call(
113
103
  jwt_token,
114
- llm_uuid,
115
- model,
104
+ latest_pe.llm_uuid,
105
+ latest_pe.model,
116
106
  "No context available.",
117
107
  { role: "user", prompt: "Please summarize the following text into a short title (max 50 characters). Respond with only the title, nothing else: #{prompt_text}" }
118
108
  )
@@ -124,7 +114,10 @@ class Chat < ApplicationRecord
124
114
  end
125
115
 
126
116
  # Send messages to LLM and get response
127
- def send_to_llm(jwt_token, tool_ids: [], generation_settings: {})
117
+ def send_to_llm(prompt_execution, jwt_token, tool_ids: [], generation_settings: {})
118
+ llm_uuid = prompt_execution.llm_uuid
119
+ model = prompt_execution.model
120
+
128
121
  # Get LLM options
129
122
  llm_options = LlmMetaClient::ServerResource.available_llm_options(jwt_token)
130
123
 
@@ -75,6 +75,12 @@
75
75
  messageInput.dispatchEvent(event);
76
76
  }
77
77
 
78
+ // Update branch_from_uuid to the latest prompt execution
79
+ const branchField = document.getElementById('branch_from_uuid');
80
+ if (branchField) {
81
+ branchField.value = '<%%= @prompt_execution&.execution_id %>';
82
+ }
83
+
78
84
  // Scroll to bottom
79
85
  const chatMessages = document.getElementById('chat-messages');
80
86
  if (chatMessages) {
@@ -68,6 +68,12 @@
68
68
  messageInput.dispatchEvent(event);
69
69
  }
70
70
 
71
+ // Update branch_from_uuid to the latest prompt execution
72
+ const branchField = document.getElementById('branch_from_uuid');
73
+ if (branchField) {
74
+ branchField.value = '<%%= @prompt_execution&.execution_id %>';
75
+ }
76
+
71
77
  // Scroll to bottom
72
78
  const chatMessages = document.getElementById('chat-messages');
73
79
  if (chatMessages) {
@@ -4,8 +4,6 @@ class CreateChats < ActiveRecord::Migration[8.1]
4
4
  t.references :user, null: true, foreign_key: true
5
5
  t.string :uuid, null: false
6
6
  t.string :title
7
- t.string :llm_uuid
8
- t.string :model
9
7
 
10
8
  t.timestamps
11
9
  end
@@ -0,0 +1,7 @@
1
+ class MigrateLlmUuidToPromptExecutions < ActiveRecord::Migration[8.1]
2
+ def change
3
+ add_column :prompt_navigator_prompt_executions, :llm_uuid, :string unless column_exists?(:prompt_navigator_prompt_executions, :llm_uuid)
4
+ remove_column :chats, :llm_uuid, :string if column_exists?(:chats, :llm_uuid)
5
+ remove_column :chats, :model, :string if column_exists?(:chats, :model)
6
+ end
7
+ end
@@ -117,7 +117,7 @@ module LlmMetaClient
117
117
  def ollama_options
118
118
  ollama_list = llms.filter { it["family"] == "ollama" }
119
119
  raise LlmMetaClient::Exceptions::OllamaUnavailableError if ollama_list.empty?
120
- ollama_list
120
+ ollama_list.each { it["llm_type"] ||= "ollama" }
121
121
  end
122
122
 
123
123
  # Builds normalized option hashes from an array of prompts by slicing common keys
@@ -1,3 +1,3 @@
1
1
  module LlmMetaClient
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: llm_meta_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - dhq_boiler
@@ -130,6 +130,7 @@ files:
130
130
  - lib/generators/llm_meta_client/scaffold/templates/config/initializers/llm_service.rb
131
131
  - lib/generators/llm_meta_client/scaffold/templates/db/migrate/create_chats.rb
132
132
  - lib/generators/llm_meta_client/scaffold/templates/db/migrate/create_messages.rb
133
+ - lib/generators/llm_meta_client/scaffold/templates/db/migrate/migrate_llm_uuid_to_prompt_executions.rb
133
134
  - lib/llm_meta_client.rb
134
135
  - lib/llm_meta_client/chat_manageable.rb
135
136
  - lib/llm_meta_client/engine.rb