ruby_conversations 1.1.1 → 1.1.2
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 +4 -4
- data/README.md +5 -28
- data/app/models/ruby_conversations/concerns/conversation_chat.rb +8 -5
- data/app/models/ruby_conversations/concerns/conversation_messages.rb +7 -2
- data/app/models/ruby_conversations/conversation.rb +7 -55
- data/app/models/ruby_conversations/message_builder.rb +7 -1
- data/lib/ruby_conversations/configuration.rb +1 -1
- data/lib/ruby_conversations/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ebe08b35dd3c009e6bd81c5fb6bbb8e9d109d51b76d4469a8af13deeadaebfc5
|
4
|
+
data.tar.gz: db51099d58654198bcbb1d0c873896f729170937798b0f8cee7928cc44dc5e3c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78866ece10dffbce9e4daaa3b6fff1dd8b019663b5c0f665f4043b984b8de17a4c004ae23e62a490eae3626540be14f0811a3b9fef7b00c18f7593789dced64a
|
7
|
+
data.tar.gz: 84cd8cfe96fad9343323ecd5080586647ab2bb762290954b2aa8887c875f628d96c12d6b61a4473489ea584c2b2cb5749b35cb5632f129e71e5b903a5c890666
|
data/README.md
CHANGED
@@ -7,9 +7,7 @@ A Rails engine for managing AI conversations and storing them in prompt studio.
|
|
7
7
|
- **Built-in Prompt Management**: Version-controlled prompts with placeholder validation
|
8
8
|
- **Conversation History**: Track and manage conversation threads
|
9
9
|
- **Input/Output Storage**: Structured storage for message inputs and responses
|
10
|
-
- **Real-time Updates**: Built-in broadcasting support for real-time applications
|
11
10
|
- **JWT Authentication**: Secure remote mode with JWT authentication
|
12
|
-
- **Easy Integration**: Simple setup with Rails generators
|
13
11
|
|
14
12
|
## Installation
|
15
13
|
|
@@ -36,7 +34,7 @@ gem install ruby_conversations
|
|
36
34
|
Configure the engine in `config/initializers/ai_conversation_engine.rb`:
|
37
35
|
|
38
36
|
```ruby
|
39
|
-
|
37
|
+
RubyConversations.configure do |config|
|
40
38
|
# API settings
|
41
39
|
config.api_url = ENV['AI_CONVERSATION_API_URL']
|
42
40
|
config.jwt_secret = ENV['AI_CONVERSATION_JWT_SECRET']
|
@@ -51,34 +49,13 @@ end
|
|
51
49
|
|
52
50
|
```ruby
|
53
51
|
# Create a new conversation
|
54
|
-
conversation =
|
52
|
+
conversation = RubyConversations::Conversation.new
|
55
53
|
|
56
54
|
# Ask a question using a predefined prompt
|
57
|
-
conversation.
|
58
|
-
|
59
|
-
|
60
|
-
### With Associated Objects
|
61
|
-
|
62
|
-
```ruby
|
63
|
-
# Create a conversation linked to another object
|
64
|
-
class Project < ApplicationRecord
|
65
|
-
has_many :conversations, as: :conversationable,
|
66
|
-
class_name: 'AiConversationEngine::AiConversation'
|
67
|
-
end
|
68
|
-
|
69
|
-
project = Project.find(1)
|
70
|
-
conversation = project.conversations.create!
|
71
|
-
conversation.ask("analyze_project", inputs: { name: project.name })
|
72
|
-
```
|
73
|
-
|
74
|
-
|
75
|
-
### Real-time Updates
|
76
|
-
|
77
|
-
```ruby
|
78
|
-
# In your view
|
79
|
-
<%= turbo_stream_from "chat_#{@conversation.id}" %>
|
55
|
+
conversation.with_prompt("explain_code", inputs: { code: "def hello; end" })
|
56
|
+
result = conversation.call_llm
|
80
57
|
|
81
|
-
|
58
|
+
puts result[:content]
|
82
59
|
```
|
83
60
|
|
84
61
|
## Development
|
@@ -6,15 +6,17 @@ module RubyConversations
|
|
6
6
|
module ConversationChat
|
7
7
|
extend ActiveSupport::Concern
|
8
8
|
|
9
|
-
def
|
9
|
+
def call_llm(system_message: nil, &block)
|
10
10
|
validate_conversation_state
|
11
|
-
chat_messages = generate_chat_response(system_message)
|
11
|
+
chat_messages = generate_chat_response(system_message, &block)
|
12
12
|
|
13
13
|
validate_tool_calls(chat_messages)
|
14
14
|
|
15
15
|
store_and_update_conversation(chat_messages)
|
16
16
|
end
|
17
17
|
|
18
|
+
alias execute call_llm
|
19
|
+
|
18
20
|
def validate_tool_calls(chat_messages)
|
19
21
|
return unless tools_configured?
|
20
22
|
return if tool_calls?(chat_messages)
|
@@ -50,9 +52,9 @@ module RubyConversations
|
|
50
52
|
raise ArgumentError, 'Conversation must have at least one message to execute' unless message
|
51
53
|
end
|
52
54
|
|
53
|
-
def generate_chat_response(system_message)
|
55
|
+
def generate_chat_response(system_message, &)
|
54
56
|
setup_llm_chat(system_message: system_message)
|
55
|
-
chat.ask(messages.last.request)
|
57
|
+
chat.ask(messages.last.request, &)
|
56
58
|
chat.messages
|
57
59
|
end
|
58
60
|
|
@@ -62,7 +64,8 @@ module RubyConversations
|
|
62
64
|
|
63
65
|
{
|
64
66
|
response: chat_messages,
|
65
|
-
conversation: self
|
67
|
+
conversation: self,
|
68
|
+
content: chat_messages.last.content
|
66
69
|
}
|
67
70
|
end
|
68
71
|
|
@@ -17,15 +17,20 @@ module RubyConversations
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
20
|
+
def with_prompts(prompt_inputs, description: nil)
|
21
21
|
MessageBuilder.new(self).build_from_multiple_prompts(prompt_inputs, description: description)
|
22
22
|
self
|
23
23
|
end
|
24
24
|
|
25
|
-
def
|
25
|
+
def with_prompt(name, description: nil, inputs: {})
|
26
26
|
MessageBuilder.new(self).build_from_single_prompt(name, description: description, inputs: inputs)
|
27
27
|
self
|
28
28
|
end
|
29
|
+
|
30
|
+
def with_user_message(message, description: nil)
|
31
|
+
MessageBuilder.new(self).build_from_user_message(message, description: description)
|
32
|
+
self
|
33
|
+
end
|
29
34
|
end
|
30
35
|
end
|
31
36
|
end
|
@@ -13,46 +13,22 @@ module RubyConversations
|
|
13
13
|
include RubyConversations::Concerns::MessageProcessing
|
14
14
|
|
15
15
|
# Define attributes needed for API interaction & local state
|
16
|
-
attr_accessor :
|
17
|
-
:messages, :tool, :tools, :created_at, :updated_at
|
16
|
+
attr_accessor :id, :conversationable_type, :conversationable_id, :conversationable,
|
17
|
+
:messages, :tool, :tools, :created_at, :updated_at
|
18
18
|
|
19
19
|
# Validations
|
20
20
|
validates :messages, presence: { message: 'At least one message is required' }, on: :update
|
21
21
|
validate :validate_messages
|
22
22
|
|
23
23
|
# Initialization
|
24
|
-
def initialize(attributes =
|
25
|
-
|
24
|
+
def initialize(attributes = {})
|
25
|
+
@chat = attributes.delete(:chat)
|
26
26
|
@messages = []
|
27
|
-
|
28
|
-
@persist = extract_persist_flag(attributes)
|
29
|
-
|
30
|
-
super # Initialize with remaining attributes using ActiveModel::Model
|
31
|
-
initialize_messages(messages_attrs)
|
32
|
-
|
33
|
-
build_chat
|
34
|
-
end
|
35
|
-
|
36
|
-
def extract_message_attributes(attributes)
|
37
|
-
attributes.delete(:messages) ||
|
38
|
-
attributes.delete('messages') ||
|
39
|
-
attributes.delete(:messages_attributes) ||
|
40
|
-
attributes.delete('messages_attributes') ||
|
41
|
-
[]
|
27
|
+
super
|
42
28
|
end
|
43
29
|
|
44
|
-
def
|
45
|
-
|
46
|
-
attributes.delete('persist') ||
|
47
|
-
false
|
48
|
-
end
|
49
|
-
|
50
|
-
def build_chat
|
51
|
-
@chat = if @persist && RubyConversations.configuration.persistence_model
|
52
|
-
resolve_persistence_model.create!(model_id: llm, provider: provider)
|
53
|
-
else
|
54
|
-
RubyLLM.chat(model: llm, provider: provider).with_temperature(0.0)
|
55
|
-
end
|
30
|
+
def chat
|
31
|
+
@chat ||= RubyLLM.chat(model: llm, provider: provider).with_temperature(0.0)
|
56
32
|
end
|
57
33
|
|
58
34
|
def model_identifier
|
@@ -63,36 +39,12 @@ module RubyConversations
|
|
63
39
|
@provider ||= RubyConversations.configuration.default_llm_provider
|
64
40
|
end
|
65
41
|
|
66
|
-
def initialize_messages(message_attributes)
|
67
|
-
(message_attributes || []).each do |msg_data|
|
68
|
-
processed_prompts = process_message_prompts(msg_data)
|
69
|
-
|
70
|
-
message_instance = RubyConversations::Message.new(msg_data.except('id', 'conversation_id', 'created_at',
|
71
|
-
'updated_at'))
|
72
|
-
message_instance.message_prompts = processed_prompts
|
73
|
-
@messages << message_instance # Add to the messages array
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
42
|
def conversation_attributes_for_storage
|
78
43
|
{ ai_conversation: base_attributes.merge(relationship_attributes).merge(messages_attributes).compact }
|
79
44
|
end
|
80
45
|
|
81
46
|
private
|
82
47
|
|
83
|
-
def resolve_persistence_model
|
84
|
-
model_config = RubyConversations.configuration.persistence_model
|
85
|
-
unless model_config.is_a?(String)
|
86
|
-
raise ConfigurationError, "Invalid persistence_model configured: Must be a String, got #{model_config.class}"
|
87
|
-
end
|
88
|
-
|
89
|
-
model_config.safe_constantize ||
|
90
|
-
raise(ConfigurationError, "Invalid persistence_model configured: '#{model_config}' could not be resolved.")
|
91
|
-
rescue NameError => e
|
92
|
-
# Catch potential NameError from constantize if the class name is invalid
|
93
|
-
raise ConfigurationError, "Invalid persistence_model configured: #{e.message}"
|
94
|
-
end
|
95
|
-
|
96
48
|
def base_attributes
|
97
49
|
{
|
98
50
|
id: id
|
@@ -15,7 +15,13 @@ module RubyConversations
|
|
15
15
|
message = build_message(interpolated_message, description)
|
16
16
|
build_prompt_association(message, prompt, inputs)
|
17
17
|
|
18
|
-
message
|
18
|
+
message
|
19
|
+
end
|
20
|
+
|
21
|
+
def build_from_user_message(raw_message, description: nil)
|
22
|
+
message = build_message(raw_message, description)
|
23
|
+
build_message_prompt(message, Prompt.new(name: 'user_message', role: 'user', message: raw_message))
|
24
|
+
message
|
19
25
|
end
|
20
26
|
|
21
27
|
def build_from_multiple_prompts(prompt_inputs, description: nil)
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RubyConversations
|
4
4
|
# Configuration options for RubyConversations
|
5
5
|
class Configuration
|
6
|
-
attr_accessor :api_url, :jwt_secret, :default_llm_model, :default_llm_provider
|
6
|
+
attr_accessor :api_url, :jwt_secret, :default_llm_model, :default_llm_provider
|
7
7
|
|
8
8
|
def initialize
|
9
9
|
@default_llm_model = 'claude-3-7-sonnet'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_conversations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Shippy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
11
|
+
date: 2025-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|