ai-engine 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/ai/engine/assistant_thread.rb +10 -2
- data/app/models/ai/engine/chat.rb +1 -1
- data/app/models/concerns/ai/engine/assistable.rb +9 -1
- data/app/models/concerns/ai/engine/chattable.rb +1 -1
- data/app/models/concerns/ai/engine/threadable.rb +1 -1
- data/app/services/ai/engine/openai/assistants/delete.rb +17 -0
- data/app/services/ai/engine/openai/threads/delete.rb +19 -0
- data/lib/ai/engine/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f3cd77d6cd96c5e07d9b1e2627abfe022072f69a83819a3191a5bc82c96db84
|
4
|
+
data.tar.gz: d4a2b9d2d26b3c0579e2f109c11801501b1e96d49d37cd789f347d680f6c4c7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b09245610c6a8ce82ba5e73dcee0b5bdc586b26c10ef5af6919894d5d8d58b3edf1b3e5c7340c2046a046bfcbc843fe35dfa447b6417ffc61c1a4b3991bdb0f
|
7
|
+
data.tar.gz: 4384746c755693decb0aef08674eed0222d4b17bb4cc63c8b0a468141242fd42b11d0a03fd719bc4378ff5df0da9dc2995d43b8f4c24d03f1abac3b90ed49120
|
@@ -3,10 +3,11 @@ module AI::Engine
|
|
3
3
|
include RemoteIdValidatable
|
4
4
|
|
5
5
|
belongs_to :threadable, polymorphic: true
|
6
|
-
has_many :runs, class_name: "AI::Engine::Run", foreign_key: "ai_engine_assistant_thread_id", dependent: :
|
7
|
-
has_many :messages, as: :messageable, class_name: "AI::Engine::Message", foreign_key: "messageable_id", dependent: :
|
6
|
+
has_many :runs, class_name: "AI::Engine::Run", foreign_key: "ai_engine_assistant_thread_id", dependent: :destroy
|
7
|
+
has_many :messages, as: :messageable, class_name: "AI::Engine::Message", foreign_key: "messageable_id", dependent: :destroy
|
8
8
|
|
9
9
|
before_create :create_openai_thread
|
10
|
+
before_destroy :delete_openai_thread
|
10
11
|
|
11
12
|
def run(assistant_id:, content:)
|
12
13
|
# Create the request Message, locally and remotely on OpenAI.
|
@@ -28,5 +29,12 @@ module AI::Engine
|
|
28
29
|
errors.add(:base, e.message)
|
29
30
|
throw(:abort)
|
30
31
|
end
|
32
|
+
|
33
|
+
def delete_openai_thread
|
34
|
+
AI::Engine::OpenAI::Threads::Delete.call(remote_id: remote_id)
|
35
|
+
rescue Faraday::Error => e
|
36
|
+
errors.add(:base, e.message)
|
37
|
+
throw(:abort)
|
38
|
+
end
|
31
39
|
end
|
32
40
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module AI::Engine
|
2
2
|
class Chat < ApplicationRecord
|
3
3
|
belongs_to :chattable, polymorphic: true
|
4
|
-
has_many :messages, as: :messageable, class_name: "AI::Engine::Message", foreign_key: "messageable_id", dependent: :
|
4
|
+
has_many :messages, as: :messageable, class_name: "AI::Engine::Message", foreign_key: "messageable_id", dependent: :destroy
|
5
5
|
|
6
6
|
def run(model:)
|
7
7
|
# Run the Chat, sending the complete message history to OpenAI.
|
@@ -4,10 +4,11 @@ module AI
|
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
6
|
included do
|
7
|
-
has_one :assistant, as: :assistable, class_name: "AI::Engine::Assistant", dependent: :
|
7
|
+
has_one :assistant, as: :assistable, class_name: "AI::Engine::Assistant", dependent: :destroy
|
8
8
|
|
9
9
|
before_create :create_openai_assistant
|
10
10
|
before_update :update_openai_assistant
|
11
|
+
before_destroy :delete_openai_assistant
|
11
12
|
|
12
13
|
# Default. Override in including model to customize.
|
13
14
|
def ai_engine_assistant
|
@@ -42,6 +43,13 @@ module AI
|
|
42
43
|
errors.add(:base, e.message)
|
43
44
|
throw(:abort)
|
44
45
|
end
|
46
|
+
|
47
|
+
def delete_openai_assistant
|
48
|
+
AI::Engine::OpenAI::Assistants::Delete.call(remote_id: assistant&.remote_id)
|
49
|
+
rescue Faraday::Error => e
|
50
|
+
errors.add(:base, e.message)
|
51
|
+
throw(:abort)
|
52
|
+
end
|
45
53
|
end
|
46
54
|
end
|
47
55
|
end
|
@@ -4,7 +4,7 @@ module AI
|
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
6
|
included do
|
7
|
-
has_many :chats, as: :chattable, class_name: "AI::Engine::Chat"
|
7
|
+
has_many :chats, as: :chattable, class_name: "AI::Engine::Chat", dependent: :destroy
|
8
8
|
|
9
9
|
def ai_engine_on_message_create(message:)
|
10
10
|
# This is a hook for the AI Engine to notify the Chattable that a Message has been updated.
|
@@ -4,7 +4,7 @@ module AI
|
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
6
|
included do
|
7
|
-
has_many :assistant_threads, as: :threadable, class_name: "AI::Engine::AssistantThread", dependent: :
|
7
|
+
has_many :assistant_threads, as: :threadable, class_name: "AI::Engine::AssistantThread", dependent: :destroy
|
8
8
|
|
9
9
|
def ai_engine_on_message_create(message:)
|
10
10
|
# This is a hook for the AI Engine to notify the AssistantThreadtable that a Message has been updated.
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class AI::Engine::OpenAI::Assistants::Delete
|
2
|
+
# Deletes an OpenAI Assistant by its ID.
|
3
|
+
def self.call(remote_id:)
|
4
|
+
client.assistants.delete(id: remote_id)
|
5
|
+
rescue Faraday::ResourceNotFound => e
|
6
|
+
Rails.logger.error("#{self} - #{e.response.dig(:body, "error", "message")}")
|
7
|
+
end
|
8
|
+
|
9
|
+
private_class_method def self.client
|
10
|
+
@client ||= OpenAI::Client.new(
|
11
|
+
access_token: AI::Engine::Engine.config.openai_access_token,
|
12
|
+
organization_id: AI::Engine::Engine.config.openai_organization_id,
|
13
|
+
log_errors: Rails.env.development? || Rails.env.test?,
|
14
|
+
request_timeout: 2.minutes.to_i
|
15
|
+
)
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class AI::Engine::OpenAI::Threads::Delete
|
2
|
+
# Deletes an OpenAI Thread by its ID.
|
3
|
+
def self.call(remote_id:)
|
4
|
+
response = client.threads.delete(id: remote_id)
|
5
|
+
|
6
|
+
response["id"]
|
7
|
+
rescue Faraday::ResourceNotFound => e
|
8
|
+
Rails.logger.error("#{self} - #{e.response.dig(:body, "error", "message")}")
|
9
|
+
end
|
10
|
+
|
11
|
+
private_class_method def self.client
|
12
|
+
@client ||= OpenAI::Client.new(
|
13
|
+
access_token: AI::Engine::Engine.config.openai_access_token,
|
14
|
+
organization_id: AI::Engine::Engine.config.openai_organization_id,
|
15
|
+
log_errors: Rails.env.development? || Rails.env.test?,
|
16
|
+
request_timeout: 2.minutes.to_i
|
17
|
+
)
|
18
|
+
end
|
19
|
+
end
|
data/lib/ai/engine/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ai-engine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Rudall
|
@@ -44,6 +44,7 @@ files:
|
|
44
44
|
- app/models/concerns/ai/engine/remote_id_validatable.rb
|
45
45
|
- app/models/concerns/ai/engine/threadable.rb
|
46
46
|
- app/services/ai/engine/openai/assistants/create.rb
|
47
|
+
- app/services/ai/engine/openai/assistants/delete.rb
|
47
48
|
- app/services/ai/engine/openai/assistants/retrieve.rb
|
48
49
|
- app/services/ai/engine/openai/assistants/update.rb
|
49
50
|
- app/services/ai/engine/openai/chats/stream.rb
|
@@ -51,6 +52,7 @@ files:
|
|
51
52
|
- app/services/ai/engine/openai/runs/create.rb
|
52
53
|
- app/services/ai/engine/openai/runs/retrieve.rb
|
53
54
|
- app/services/ai/engine/openai/threads/create.rb
|
55
|
+
- app/services/ai/engine/openai/threads/delete.rb
|
54
56
|
- config/initializers/inflections.rb
|
55
57
|
- config/initializers/openai.rb
|
56
58
|
- db/migrate/20240528153439_create_ai_engine_assistants.rb
|