minds_sdk 0.1.1 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +31 -3
- data/lib/minds/client.rb +26 -16
- data/lib/minds/config/base.rb +9 -3
- data/lib/minds/datasources.rb +121 -0
- data/lib/minds/errors.rb +6 -5
- data/lib/minds/minds.rb +267 -0
- data/lib/minds/rest_client.rb +52 -0
- data/lib/minds/version.rb +1 -1
- data/lib/minds.rb +36 -2
- metadata +17 -11
- data/lib/minds/resources/base.rb +0 -26
- data/lib/minds/resources/datasources.rb +0 -96
- data/lib/minds/resources/minds.rb +0 -237
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c87aafd23aa3c79beffb7e71f2e8c5559cdeacd4d9ea313a918959d3c467f7c6
|
4
|
+
data.tar.gz: 50968756d64771da955cbbcdfe0a4f56222c01b6a1a5d4228b0916d0e2e34cf3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0cb27972f74e235847215349ae2ff1d43c3dd157918954d7987e3ee1d1327d4239e13180213fd7c8fcf95abedb47b3c1a059dde0e22c614b24dabd86868e8385
|
7
|
+
data.tar.gz: 5a9e256939116119fe9b2e5252dadcdc15cc5fc8083f9434bd296005e47ccff265ade9922db54a6c9554dcf895501acd15b173a9fafb84419fb41e2cd5e8d6e6
|
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# Minds Ruby SDK
|
2
|
-
[![Gem Version](https://
|
2
|
+
[![Gem Version](https://img.shields.io/gem/v/minds_sdk)](https://rubygems.org/gems/minds_sdk)
|
3
|
+
[![Ruby](https://img.shields.io/badge/ruby->=3.0.0-ruby.svg)](https://www.ruby-lang.org/en/)
|
4
|
+
[![Documentation](https://img.shields.io/badge/docs-rubydoc.info-blue.svg)](https://www.rubydoc.info/gems/minds_sdk)
|
5
|
+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
|
6
|
+
|
3
7
|
|
4
8
|
Minds Ruby SDK provides an interface to interact with the Minds AI system API. It allows you to create and manage "minds" (artificial intelligences), create chat completions, and manage data sources.
|
5
9
|
|
@@ -24,6 +28,11 @@ Or install it yourself as:
|
|
24
28
|
```bash
|
25
29
|
$ gem install minds_sdk
|
26
30
|
```
|
31
|
+
and require with:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require 'minds'
|
35
|
+
```
|
27
36
|
|
28
37
|
## Getting Started
|
29
38
|
|
@@ -66,6 +75,20 @@ client = Minds::Client.new(api_key: "YOUR_API_KEY", base_url: "https://<custom_c
|
|
66
75
|
```
|
67
76
|
> Get your minds api key [here](https://mdb.ai/apiKeys)
|
68
77
|
|
78
|
+
### Logging
|
79
|
+
|
80
|
+
By default, the Minds SDK does not log any Faraday::Errors encountered during network requests to prevent potential data leaks. To enable error logging, you can set `log_errors` to true when configuring the client:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
# Global configuration
|
84
|
+
Minds::Client.configure do |config|
|
85
|
+
config.log_errors = true
|
86
|
+
end
|
87
|
+
|
88
|
+
# Or instance configuration
|
89
|
+
client = Minds::Client.new(log_errors: true)
|
90
|
+
```
|
91
|
+
|
69
92
|
## Resources
|
70
93
|
|
71
94
|
### Creating a Data Source
|
@@ -73,7 +96,7 @@ client = Minds::Client.new(api_key: "YOUR_API_KEY", base_url: "https://<custom_c
|
|
73
96
|
You can connect to various databases, such as PostgreSQL, by configuring your data source. Use the DatabaseConfig to define the connection details for your data source.
|
74
97
|
|
75
98
|
```ruby
|
76
|
-
postgres_config = Minds::
|
99
|
+
postgres_config = Minds::DatabaseConfig.new(
|
77
100
|
name: 'my_datasource',
|
78
101
|
description: '<DESCRIPTION-OF-YOUR-DATA>',
|
79
102
|
engine: 'postgres',
|
@@ -192,7 +215,12 @@ response = mind.completion(message: "Hello, how are you?")
|
|
192
215
|
puts response
|
193
216
|
|
194
217
|
# For streaming responses
|
195
|
-
mind.completion(message: "Tell me a story", stream: true)
|
218
|
+
mind.completion(message: "Tell me a story", stream: true) do |chunk|
|
219
|
+
puts chunk
|
220
|
+
end
|
221
|
+
|
222
|
+
# => {"id"=>"ad2592865b844aadbb070b3fb5090869", "choices"=>[{"delta"=>{"content"=>"I understand your request. I'm working on a detailed response for you.", "function_call"=>nil, "role"=>"assistant", "tool_calls"=>nil}, "finish_reason"=>nil, "index"=>0, "logprobs"=>nil}], "created"=>1729085931, "model"=>"mind_house_sale", "object"=>"chat.completion.chunk", "system_fingerprint"=>nil, "usage"=>nil}
|
223
|
+
# => ...
|
196
224
|
```
|
197
225
|
|
198
226
|
## Development
|
data/lib/minds/client.rb
CHANGED
@@ -1,12 +1,20 @@
|
|
1
|
-
|
2
|
-
require "json"
|
3
|
-
require_relative "config/base"
|
4
|
-
require_relative "resources/base"
|
5
|
-
require_relative "resources/minds"
|
6
|
-
require_relative "resources/datasources"
|
1
|
+
# frozen_string_literal: true
|
7
2
|
|
8
3
|
module Minds
|
9
4
|
class Client
|
5
|
+
include Minds::RestClient
|
6
|
+
|
7
|
+
SENSITIVE_ATTRIBUTES = %i[@base_url @api_key].freeze
|
8
|
+
CONFIG_KEYS = %i[base_url api_key log_errors api_version].freeze
|
9
|
+
attr_reader(*CONFIG_KEYS)
|
10
|
+
|
11
|
+
def initialize(options = {})
|
12
|
+
# if key not present. Fall back to global config
|
13
|
+
CONFIG_KEYS.each do |key|
|
14
|
+
instance_variable_set "@#{key}", options[key] || Client.config.send(key)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
10
18
|
class << self
|
11
19
|
def config
|
12
20
|
@config ||= Config::Base.new
|
@@ -17,20 +25,22 @@ module Minds
|
|
17
25
|
end
|
18
26
|
end
|
19
27
|
|
20
|
-
attr_accessor :base_url, :api_key
|
21
|
-
|
22
|
-
def initialize(api_key: nil, base_url: nil)
|
23
|
-
# if api_key & base_url not present. Fall back to global config
|
24
|
-
@base_url = base_url.nil? ? Minds::Client.config.send(:base_url) : base_url
|
25
|
-
@api_key = api_key.nil? ? Minds::Client.config.send(:api_key) : api_key
|
26
|
-
end
|
27
|
-
|
28
28
|
def datasources
|
29
|
-
@datasources ||=
|
29
|
+
@datasources ||= Datasources.new(client: self)
|
30
30
|
end
|
31
31
|
|
32
32
|
def minds
|
33
|
-
@minds ||= Minds
|
33
|
+
@minds ||= Minds.new(client: self)
|
34
|
+
end
|
35
|
+
|
36
|
+
def inspect
|
37
|
+
vars = instance_variables.map do |var|
|
38
|
+
value = instance_variable_get(var)
|
39
|
+
|
40
|
+
SENSITIVE_ATTRIBUTES.include?(var) ? "#{var}=[FILTERED]" : "#{var}=#{value.inspect}"
|
41
|
+
end
|
42
|
+
|
43
|
+
"#<#{self.class}:0x#{object_id.to_s(16)} #{vars.join(', ')}>"
|
34
44
|
end
|
35
45
|
end
|
36
46
|
end
|
data/lib/minds/config/base.rb
CHANGED
@@ -1,12 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Minds
|
2
4
|
module Config
|
3
5
|
class Base
|
4
|
-
attr_accessor :base_url, :api_key
|
5
|
-
DEFAULT_BASE_URL = "https://mdb.ai"
|
6
|
+
attr_accessor :base_url, :api_key, :log_errors, :api_version
|
7
|
+
DEFAULT_BASE_URL = "https://mdb.ai".freeze
|
8
|
+
DEFAULT_LOG_ERRORS = false
|
9
|
+
DEFAULT_API_VERSION = "api".freeze
|
6
10
|
|
7
11
|
def initialize
|
8
|
-
@base_url = DEFAULT_BASE_URL
|
9
12
|
@api_key = nil
|
13
|
+
@base_url = DEFAULT_BASE_URL
|
14
|
+
@log_errors = DEFAULT_LOG_ERRORS
|
15
|
+
@api_version = DEFAULT_API_VERSION
|
10
16
|
end
|
11
17
|
end
|
12
18
|
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
module Minds
|
2
|
+
class DatabaseConfig
|
3
|
+
attr_accessor :name, :engine, :description, :connection_data, :tables, :created_at
|
4
|
+
|
5
|
+
def initialize(name:, engine:, description:, connection_data: {}, tables: [], created_at: nil)
|
6
|
+
@name = name
|
7
|
+
@engine = engine
|
8
|
+
@description = description
|
9
|
+
@connection_data = connection_data
|
10
|
+
@tables = tables
|
11
|
+
@created_at = created_at
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_h
|
15
|
+
{
|
16
|
+
name: @name,
|
17
|
+
engine: @engine,
|
18
|
+
description: @description,
|
19
|
+
connection_data: @connection_data,
|
20
|
+
tables: @tables
|
21
|
+
}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Datasource < DatabaseConfig; end
|
26
|
+
|
27
|
+
class Datasources
|
28
|
+
def initialize(client:)
|
29
|
+
@client = client
|
30
|
+
end
|
31
|
+
|
32
|
+
# Create a new datasource and return it
|
33
|
+
#
|
34
|
+
# @param ds_config [DatabaseConfig] datasource configuration
|
35
|
+
# @option ds_config [String] :name Name of the datasource
|
36
|
+
# @option ds_config [String] :engine Type of database handler (e.g., 'postgres', 'mysql')
|
37
|
+
# @option ds_config [String] :description Description of the database. Used by mind to understand what data can be retrieved from it.
|
38
|
+
# @option ds_config [Hash] :connection_data (optional) Credentials to connect to the database
|
39
|
+
# @option ds_config [Array<String>] :tables (optional) List of allowed tables
|
40
|
+
# @param update [Boolean] If true, to update datasourse if exists, default is false
|
41
|
+
# @return [Datasource] The created datasource object
|
42
|
+
# @raise [ObjectNotSupported] If datasource type is not supported
|
43
|
+
#
|
44
|
+
# @example
|
45
|
+
# config = DatabaseConfig.new(
|
46
|
+
# name: 'sales_db',
|
47
|
+
# engine: 'postgres',
|
48
|
+
# connection_data: {
|
49
|
+
# host: 'localhost',
|
50
|
+
# port: 5432,
|
51
|
+
# user_name: "test"
|
52
|
+
# password: "test"
|
53
|
+
# }
|
54
|
+
# )
|
55
|
+
# datasource = datasources.create(config)
|
56
|
+
#
|
57
|
+
def create(ds_config, update = false)
|
58
|
+
name = ds_config.name
|
59
|
+
|
60
|
+
path = "datasources"
|
61
|
+
path += "/#{name}" if update
|
62
|
+
|
63
|
+
@client.send(update ? :put : :post, path: path, parameters: ds_config.to_h)
|
64
|
+
find(name)
|
65
|
+
end
|
66
|
+
|
67
|
+
# Return a list of datasources
|
68
|
+
#
|
69
|
+
# @return [Array<Datasource>] An array of Datasource objects
|
70
|
+
#
|
71
|
+
# @example
|
72
|
+
# datasources = datasources.all
|
73
|
+
# datasources.each { |ds| puts ds.name }
|
74
|
+
#
|
75
|
+
def all
|
76
|
+
data = @client.get(path: "datasources")
|
77
|
+
return [] if data.empty?
|
78
|
+
|
79
|
+
data.each_with_object([]) do |item, ds_list|
|
80
|
+
next if item["engine"].nil?
|
81
|
+
|
82
|
+
ds_list << Datasource.new(**item.transform_keys(&:to_sym))
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Find a datasource by name
|
87
|
+
#
|
88
|
+
# @param name [String] The name of the datasource to find
|
89
|
+
# @return [Datasource] The found datasource object
|
90
|
+
# @raise [ObjectNotSupported] If the datasource type is not supported
|
91
|
+
#
|
92
|
+
# @example
|
93
|
+
# datasource = datasources.find('sales_db')
|
94
|
+
# puts datasource.engine
|
95
|
+
#
|
96
|
+
def find(name)
|
97
|
+
data = @client.get(path: "datasources/#{name}")
|
98
|
+
if data["engine"].nil?
|
99
|
+
raise ObjectNotSupported, "Wrong type of datasource: #{name}"
|
100
|
+
end
|
101
|
+
Datasource.new(**data.transform_keys(&:to_sym))
|
102
|
+
end
|
103
|
+
|
104
|
+
# Delete a datasource
|
105
|
+
#
|
106
|
+
# @param name [String] Datasource name to delete
|
107
|
+
# @param force [Boolean] Whether to force delete from all minds
|
108
|
+
#
|
109
|
+
# @example
|
110
|
+
# # Simple delete
|
111
|
+
# datasources.destroy('old_db')
|
112
|
+
#
|
113
|
+
# # Force delete
|
114
|
+
# datasources.destroy('old_db', force: true)
|
115
|
+
#
|
116
|
+
def destroy(name, force: false)
|
117
|
+
data = force ? { cascade: true } : nil
|
118
|
+
@client.delete(path: "datasources/#{name}", parameters: data)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/lib/minds/errors.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
class ValidationError < Error; end
|
4
|
-
class ObjectNotFound < Error; end
|
5
|
-
class ObjectNotSupported < Error; end
|
1
|
+
module Minds
|
2
|
+
class Error < StandardError; end
|
3
|
+
class ValidationError < Error; end
|
4
|
+
class ObjectNotFound < Error; end
|
5
|
+
class ObjectNotSupported < Error; end
|
6
|
+
end
|
data/lib/minds/minds.rb
ADDED
@@ -0,0 +1,267 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "openai"
|
4
|
+
require "uri"
|
5
|
+
|
6
|
+
module Minds
|
7
|
+
DEFAULT_PROMPT_TEMPLATE = "Use your database tools to answer the user's question: {{question}}"
|
8
|
+
|
9
|
+
class Mind
|
10
|
+
attr_reader :name, :model_name, :provider, :parameters, :created_at, :updated_at, :datasources, :prompt_template
|
11
|
+
|
12
|
+
def initialize(client, attributes = {})
|
13
|
+
@client = client
|
14
|
+
@project = "mindsdb"
|
15
|
+
@name = attributes["name"]
|
16
|
+
@model_name = attributes["model_name"]
|
17
|
+
@provider = attributes["provider"]
|
18
|
+
@parameters = attributes["parameters"] || {}
|
19
|
+
@prompt_template = @parameters.delete("prompt_template")
|
20
|
+
@created_at = attributes["created_at"]
|
21
|
+
@updated_at = attributes["updated_at"]
|
22
|
+
@datasources = attributes["datasources"]
|
23
|
+
end
|
24
|
+
|
25
|
+
# Update mind
|
26
|
+
#
|
27
|
+
# @param name [String, nil] New name of the mind (optional)
|
28
|
+
# @param model_name [String, nil] New LLM model name (optional)
|
29
|
+
# @param provider [String, nil] New LLM provider (optional)
|
30
|
+
# @param prompt_template [String, nil] New prompt template (optional)
|
31
|
+
# @param datasources [Array<String, Datasource, DatabaseConfig>, nil] Alter list of datasources used by mind (optional)
|
32
|
+
# Datasource can be passed as:
|
33
|
+
# - String: name of the datasource
|
34
|
+
# - Datasource object (Minds::Datasource)
|
35
|
+
# - DatabaseConfig object (Minds::DatabaseConfig), in this case datasource will be created
|
36
|
+
# @param parameters [Hash, nil] Alter other parameters of the mind (optional)
|
37
|
+
# @return [void]
|
38
|
+
def update(name: nil, model_name: nil, provider: nil, prompt_template: nil, datasources: nil, parameters: nil)
|
39
|
+
data = {}
|
40
|
+
ds_names = []
|
41
|
+
datasources.each do |ds|
|
42
|
+
ds_name = @client.minds.check_datasource(ds)
|
43
|
+
ds_names << ds_name
|
44
|
+
end if datasources
|
45
|
+
|
46
|
+
data["datasources"] = ds_names if !ds_names.empty?
|
47
|
+
data["name"] = name if name
|
48
|
+
data["model_name"] = model_name if model_name
|
49
|
+
data["provider"] = provider if provider
|
50
|
+
data["parameters"] = parameters.nil? ? {} : parameters
|
51
|
+
data["parameters"]["prompt_template"] = prompt_template if prompt_template
|
52
|
+
|
53
|
+
@client.patch(path: "projects/#{@project}/minds/#{@name}", parameters: data)
|
54
|
+
|
55
|
+
@name = name if name && name != @name
|
56
|
+
|
57
|
+
updated_mind = @client.minds.find(@name)
|
58
|
+
|
59
|
+
@model_name = updated_mind.model_name
|
60
|
+
@datasources = updated_mind.datasources
|
61
|
+
@parameters = updated_mind.parameters
|
62
|
+
@prompt_template = updated_mind.prompt_template
|
63
|
+
@provider = updated_mind.provider
|
64
|
+
@created_at = updated_mind.created_at
|
65
|
+
@updated_at = updated_mind.updated_at
|
66
|
+
true
|
67
|
+
end
|
68
|
+
|
69
|
+
# Add datasource to mind
|
70
|
+
#
|
71
|
+
# @param datasource [String, Datasource, DatabaseConfig] Datasource to add
|
72
|
+
# Can be passed as:
|
73
|
+
# - String: name of the datasource
|
74
|
+
# - Datasource object (Minds::Datasource)
|
75
|
+
# - DatabaseConfig object (Minds::DatabaseConfig), in this case datasource will be created
|
76
|
+
# @return [void]
|
77
|
+
def add_datasources(datasource)
|
78
|
+
ds_name = @client.minds.check_datasource(datasource)
|
79
|
+
data = { name: ds_name }
|
80
|
+
@client.post(path: "projects/#{@project}/minds/#{@name}/datasources", parameters: data)
|
81
|
+
|
82
|
+
mind = @client.minds.find(@name)
|
83
|
+
@datasources = mind.datasources
|
84
|
+
|
85
|
+
true
|
86
|
+
end
|
87
|
+
|
88
|
+
# Remove datasource from mind
|
89
|
+
#
|
90
|
+
# @param datasource [String, Datasource] Datasource to remove
|
91
|
+
# Can be passed as:
|
92
|
+
# - String: name of the datasource
|
93
|
+
# - Datasource object (Minds::Datasource)
|
94
|
+
# @return [void]
|
95
|
+
# @raise [ArgumentError] If datasource type is invalid
|
96
|
+
def destroy_datasources(datasource)
|
97
|
+
if datasource.is_a?(Datasource)
|
98
|
+
datasource = datasource.name
|
99
|
+
elsif !datasource.is_a?(String)
|
100
|
+
raise ArgumentError, "Unknown type of datasource: #{datasource}"
|
101
|
+
end
|
102
|
+
@client.delete(path: "projects/#{@project}/minds/#{@name}/datasources/#{datasource}")
|
103
|
+
|
104
|
+
mind = @client.minds.find(@name)
|
105
|
+
@datasources = mind.datasources
|
106
|
+
true
|
107
|
+
end
|
108
|
+
|
109
|
+
# Call mind completion
|
110
|
+
#
|
111
|
+
# @param message [String] The input question or prompt
|
112
|
+
# @param stream [Boolean] Whether to enable stream mode (default: false)
|
113
|
+
# @return [String, Enumerator] If stream mode is off, returns a String.
|
114
|
+
# If stream mode is on, returns an Enumerator of ChoiceDelta objects (as defined by OpenAI)
|
115
|
+
def completion(message:, stream: false)
|
116
|
+
openai_client = OpenAI::Client.new(access_token: @client.api_key, uri_base: @client.base_url)
|
117
|
+
params = {
|
118
|
+
model: @name,
|
119
|
+
messages: [ { role: "user", content: message } ],
|
120
|
+
temperature: 0
|
121
|
+
}
|
122
|
+
|
123
|
+
if stream
|
124
|
+
openai_client.chat(
|
125
|
+
parameters: params.merge(
|
126
|
+
stream: proc do |chunk, _bytesize|
|
127
|
+
yield chunk if block_given?
|
128
|
+
end
|
129
|
+
)
|
130
|
+
)
|
131
|
+
else
|
132
|
+
response = openai_client.chat(parameters: params)
|
133
|
+
response.dig("choices", 0, "message", "content")
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
class Minds
|
139
|
+
def initialize(client:)
|
140
|
+
@client = client
|
141
|
+
@project = "mindsdb"
|
142
|
+
end
|
143
|
+
|
144
|
+
# Lists minds
|
145
|
+
#
|
146
|
+
# @return [Array<Mind>] List of minds
|
147
|
+
#
|
148
|
+
# @example
|
149
|
+
# minds = minds.all
|
150
|
+
# minds.each { |mind| puts mind.name }
|
151
|
+
#
|
152
|
+
def all
|
153
|
+
data = @client.get(path: "projects/#{@project}/minds")
|
154
|
+
return [] if data.empty?
|
155
|
+
|
156
|
+
data.map { |item| Mind.new(@client, item) }
|
157
|
+
end
|
158
|
+
|
159
|
+
# Find a mind by name
|
160
|
+
#
|
161
|
+
# @param name [String] The name of the mind to find
|
162
|
+
# @return [Mind] The found mind object
|
163
|
+
#
|
164
|
+
# @example
|
165
|
+
# mind = minds.find('sales_assistant')
|
166
|
+
# puts mind.model_name
|
167
|
+
#
|
168
|
+
def find(name)
|
169
|
+
data = @client.get(path: "projects/#{@project}/minds/#{name}")
|
170
|
+
Mind.new(@client, data)
|
171
|
+
end
|
172
|
+
|
173
|
+
# Delete a mind
|
174
|
+
#
|
175
|
+
# @param name [String] The name of the mind to delete
|
176
|
+
# @return [void]
|
177
|
+
#
|
178
|
+
# @example
|
179
|
+
# minds.destroy('old_assistant')
|
180
|
+
#
|
181
|
+
def destroy(name)
|
182
|
+
@client.delete(path: "projects/#{@project}/minds/#{name}")
|
183
|
+
end
|
184
|
+
|
185
|
+
# Create a new mind and return it
|
186
|
+
#
|
187
|
+
# @param name [String] The name of the mind
|
188
|
+
# @param model_name [String, nil] The LLM model name (optional)
|
189
|
+
# @param provider [String, nil] The LLM provider (optional)
|
190
|
+
# @param prompt_template [String, nil] Instructions to LLM (optional)
|
191
|
+
# @param datasources [Array<String, Datasource, DatabaseConfig>, nil] List of datasources used by mind (optional)
|
192
|
+
# Datasource can be passed as:
|
193
|
+
# - String: name of the datasource
|
194
|
+
# - Datasource object (Minds::Datasource)
|
195
|
+
# - DatabaseConfig object (Minds::DatabaseConfig), in this case datasource will be created
|
196
|
+
# @param parameters [Hash, nil] Other parameters of the mind (optional)
|
197
|
+
# @param replace [Boolean] If true, remove existing mind with the same name (default: false)
|
198
|
+
# @param update [Boolean] If true, to update mind if exists(default: false)
|
199
|
+
# @return [Mind] The created mind object
|
200
|
+
#
|
201
|
+
# @example Simple creation
|
202
|
+
# mind = minds.create(name: 'sales_assistant', model_name: 'gpt-4')
|
203
|
+
#
|
204
|
+
# @example Creation with datasources
|
205
|
+
# mind = minds.create(
|
206
|
+
# name: 'sales_assistant',
|
207
|
+
# model_name: 'gpt-4',
|
208
|
+
# datasources: ['sales_db'],
|
209
|
+
# prompt_template: 'Analyze sales data: {{question}}'
|
210
|
+
# )
|
211
|
+
#
|
212
|
+
def create(name:, model_name: nil, provider: nil, prompt_template: nil, datasources: nil, parameters: nil, replace: false, update: false)
|
213
|
+
if replace
|
214
|
+
find(name)
|
215
|
+
destroy(name)
|
216
|
+
end
|
217
|
+
|
218
|
+
ds_names = []
|
219
|
+
datasources.each do |ds|
|
220
|
+
ds_name = check_datasource(ds)
|
221
|
+
ds_names << ds_name
|
222
|
+
end if datasources
|
223
|
+
|
224
|
+
parameters = {} if parameters.nil?
|
225
|
+
|
226
|
+
parameters["prompt_template"] = prompt_template if prompt_template
|
227
|
+
parameters["prompt_template"] ||= DEFAULT_PROMPT_TEMPLATE
|
228
|
+
data = {
|
229
|
+
name: name,
|
230
|
+
model_name: model_name,
|
231
|
+
provider: provider,
|
232
|
+
parameters: parameters,
|
233
|
+
datasources: ds_names
|
234
|
+
}
|
235
|
+
|
236
|
+
path = "projects/#{@project}/minds"
|
237
|
+
path += "/#{name}" if update
|
238
|
+
@client.send(update ? :put : :post, path: path, parameters: data)
|
239
|
+
find(name)
|
240
|
+
end
|
241
|
+
|
242
|
+
def check_datasource(ds)
|
243
|
+
ds_name = extract_datasource_name(ds)
|
244
|
+
create_datasource_if_needed(ds)
|
245
|
+
ds_name
|
246
|
+
end
|
247
|
+
|
248
|
+
private
|
249
|
+
|
250
|
+
def extract_datasource_name(ds)
|
251
|
+
case ds
|
252
|
+
when Datasource, DatabaseConfig, String
|
253
|
+
ds.respond_to?(:name) ? ds.name : ds
|
254
|
+
else
|
255
|
+
raise ArgumentError, "Unknown type of datasource: #{ds.class}"
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
def create_datasource_if_needed(ds)
|
260
|
+
return unless ds.is_a?(DatabaseConfig)
|
261
|
+
|
262
|
+
@client.datasources.find(ds.name)
|
263
|
+
rescue Faraday::ResourceNotFound
|
264
|
+
@client.datasources.create(ds)
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Minds
|
4
|
+
module RestClient
|
5
|
+
def get(path:)
|
6
|
+
conn.get(uri(path: path))&.body
|
7
|
+
end
|
8
|
+
|
9
|
+
def post(path:, parameters:)
|
10
|
+
conn.post(uri(path: path)) do |req|
|
11
|
+
req.body = parameters.to_json
|
12
|
+
end&.body
|
13
|
+
end
|
14
|
+
|
15
|
+
def patch(path:, parameters:)
|
16
|
+
conn.patch(uri(path: path)) do |req|
|
17
|
+
req.body = parameters.to_json
|
18
|
+
end&.body
|
19
|
+
end
|
20
|
+
|
21
|
+
def put(path:, parameters:)
|
22
|
+
conn.put(uri(path: path)) do |req|
|
23
|
+
req.body = parameters.to_json
|
24
|
+
end&.body
|
25
|
+
end
|
26
|
+
|
27
|
+
def delete(path:, parameters: nil)
|
28
|
+
conn.delete(uri(path: path)) do |req|
|
29
|
+
req.body = parameters.to_json
|
30
|
+
end&.body
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def uri(path:)
|
36
|
+
return path if @base_url.include?(@api_version)
|
37
|
+
|
38
|
+
"/#{@api_version}/#{path}"
|
39
|
+
end
|
40
|
+
|
41
|
+
def conn
|
42
|
+
Faraday.new(url: @base_url) do |builder|
|
43
|
+
builder.use MiddlewareErrors if @log_errors
|
44
|
+
builder.headers["Authorization"] = "Bearer #{@api_key}"
|
45
|
+
builder.headers["Content-Type"] = "application/json"
|
46
|
+
builder.request :json
|
47
|
+
builder.response :json
|
48
|
+
builder.response :raise_error
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/minds/version.rb
CHANGED
data/lib/minds.rb
CHANGED
@@ -1,9 +1,43 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "faraday"
|
4
|
+
require "json"
|
3
5
|
require_relative "minds/version"
|
6
|
+
require_relative "minds/rest_client"
|
4
7
|
require_relative "minds/client"
|
5
8
|
require_relative "minds/errors"
|
6
9
|
require_relative "minds/config/base"
|
7
|
-
require_relative "minds/
|
10
|
+
require_relative "minds/datasources"
|
11
|
+
require_relative "minds/minds"
|
8
12
|
|
9
|
-
module Minds
|
13
|
+
module Minds
|
14
|
+
class MiddlewareErrors < Faraday::Middleware
|
15
|
+
##
|
16
|
+
# Handles API error responses and provides detailed logging
|
17
|
+
#
|
18
|
+
# @param env [Hash] The Faraday environment hash
|
19
|
+
# @raise [Faraday::Error] Re-raises the original error after logging
|
20
|
+
def call(env)
|
21
|
+
@app.call(env)
|
22
|
+
rescue Faraday::Error => e
|
23
|
+
raise e unless e.response.is_a?(Hash)
|
24
|
+
|
25
|
+
logger = Logger.new($stdout)
|
26
|
+
logger.formatter = proc do |_severity, datetime, _progname, msg|
|
27
|
+
timestamp = datetime.strftime("%Y-%m-%d %H:%M:%S.%L")
|
28
|
+
|
29
|
+
error_prefix = "\033[31m[Minds #{VERSION}] #{timestamp} ERROR"
|
30
|
+
error_suffix = "\033[0m"
|
31
|
+
|
32
|
+
formatted_message = msg.split("\n").map do |line|
|
33
|
+
"#{' ' * 15}#{line}"
|
34
|
+
end.join("\n")
|
35
|
+
|
36
|
+
"#{error_prefix} Rest Client Error\n#{formatted_message}#{error_suffix}\n"
|
37
|
+
end
|
38
|
+
|
39
|
+
logger.error(e.response[:body])
|
40
|
+
raise e
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
metadata
CHANGED
@@ -1,43 +1,49 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minds_sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- tungnt
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-10
|
11
|
+
date: 2024-11-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.12'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2.12'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: ruby-openai
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '7.3'
|
31
34
|
- - ">="
|
32
35
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
36
|
+
version: 7.3.1
|
34
37
|
type: :runtime
|
35
38
|
prerelease: false
|
36
39
|
version_requirements: !ruby/object:Gem::Requirement
|
37
40
|
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '7.3'
|
38
44
|
- - ">="
|
39
45
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
46
|
+
version: 7.3.1
|
41
47
|
description: Official Ruby SDK for Minds
|
42
48
|
email:
|
43
49
|
- tungnguyen120301@gmail.com
|
@@ -55,10 +61,10 @@ files:
|
|
55
61
|
- lib/minds.rb
|
56
62
|
- lib/minds/client.rb
|
57
63
|
- lib/minds/config/base.rb
|
64
|
+
- lib/minds/datasources.rb
|
58
65
|
- lib/minds/errors.rb
|
59
|
-
- lib/minds/
|
60
|
-
- lib/minds/
|
61
|
-
- lib/minds/resources/minds.rb
|
66
|
+
- lib/minds/minds.rb
|
67
|
+
- lib/minds/rest_client.rb
|
62
68
|
- lib/minds/version.rb
|
63
69
|
- sig/minds.rbs
|
64
70
|
homepage: https://github.com/tungnt1203/minds_ruby_sdk
|
data/lib/minds/resources/base.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
module Minds
|
2
|
-
module Resources
|
3
|
-
class Base
|
4
|
-
attr_accessor :api_key, :base_url, :api
|
5
|
-
|
6
|
-
def initialize(client)
|
7
|
-
@base_url = client.base_url
|
8
|
-
@api_key = client.api_key
|
9
|
-
@api = conn
|
10
|
-
end
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def conn
|
15
|
-
Faraday.new(url: @base_url) do |builder|
|
16
|
-
builder.request :json
|
17
|
-
builder.response :json
|
18
|
-
builder.response :raise_error
|
19
|
-
builder.adapter Faraday.default_adapter
|
20
|
-
builder.headers["Authorization"] = "Bearer #{@api_key}"
|
21
|
-
builder.headers["Content-Type"] = "application/json"
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,96 +0,0 @@
|
|
1
|
-
module Minds
|
2
|
-
module Resources
|
3
|
-
class DatabaseConfig
|
4
|
-
attr_reader :name, :engine, :description, :connection_data, :tables, :created_at
|
5
|
-
|
6
|
-
def initialize(name:, engine:, description:, connection_data: {}, tables: [], created_at: nil)
|
7
|
-
@name = name
|
8
|
-
@engine = engine
|
9
|
-
@description = description
|
10
|
-
@connection_data = connection_data
|
11
|
-
@tables = tables
|
12
|
-
@created_at = created_at
|
13
|
-
end
|
14
|
-
|
15
|
-
def to_h
|
16
|
-
{
|
17
|
-
name: @name,
|
18
|
-
engine: @engine,
|
19
|
-
description: @description,
|
20
|
-
connection_data: @connection_data,
|
21
|
-
tables: @tables
|
22
|
-
}
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
class Datasource < DatabaseConfig; end
|
27
|
-
|
28
|
-
class Datasources < Base
|
29
|
-
# Create a new datasource and return it
|
30
|
-
#
|
31
|
-
# @param ds_config [DatabaseConfig] datasource configuration
|
32
|
-
# @option ds_config [String] :name Name of the datasource
|
33
|
-
# @option ds_config [String] :engine Type of database handler (e.g., 'postgres', 'mysql')
|
34
|
-
# @option ds_config [String] :description Description of the database. Used by mind to understand what data can be retrieved from it.
|
35
|
-
# @option ds_config [Hash] :connection_data (optional) Credentials to connect to the database
|
36
|
-
# @option ds_config [Array<String>] :tables (optional) List of allowed tables
|
37
|
-
# @param replace [Boolean] If true, replaces an existing datasource with the same name
|
38
|
-
# @return [Datasource] The created datasource object
|
39
|
-
# @raise [ObjectNotFound] If the datasource to be replaced doesn't exist
|
40
|
-
def create(ds_config, replace = false)
|
41
|
-
name = ds_config.name
|
42
|
-
|
43
|
-
if replace
|
44
|
-
begin
|
45
|
-
find(name)
|
46
|
-
destroy(name)
|
47
|
-
rescue ObjectNotFound
|
48
|
-
# Do nothing
|
49
|
-
end
|
50
|
-
end
|
51
|
-
self.api.post("/api/datasources") do |req|
|
52
|
-
req.body = ds_config.to_h.to_json
|
53
|
-
end
|
54
|
-
find(name)
|
55
|
-
end
|
56
|
-
|
57
|
-
# Returns a list of all datasources
|
58
|
-
#
|
59
|
-
# @return [Array<Datasource>] An array of Datasource objects
|
60
|
-
def all
|
61
|
-
data = self.api.get("/api/datasources").body
|
62
|
-
data.each_with_object([]) do |item, ds_list|
|
63
|
-
next if item["engine"].nil?
|
64
|
-
|
65
|
-
ds_list << Datasource.new(**item.transform_keys(&:to_sym))
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
# Get a datasource by name
|
70
|
-
#
|
71
|
-
# @param name [String] The name of the datasource to find
|
72
|
-
# @return [Datasource] The found datasource object
|
73
|
-
# @raise [ObjectNotSupported] If the datasource type is not supported
|
74
|
-
def find(name)
|
75
|
-
data = self.api.get("api/datasources/#{name}").body
|
76
|
-
|
77
|
-
if data["engine"].nil?
|
78
|
-
raise ObjectNotSupported, "Wrong type of datasource: #{name}"
|
79
|
-
end
|
80
|
-
|
81
|
-
Datasource.new(**data.transform_keys(&:to_sym))
|
82
|
-
end
|
83
|
-
|
84
|
-
# Drop (delete) a datasource by name
|
85
|
-
#
|
86
|
-
# @param name [String] The name of the datasource to delete
|
87
|
-
# @return [void]
|
88
|
-
# @raise [Faraday::ResourceNotFound] If the datasource doesn't exist
|
89
|
-
# @raise [Faraday::ClientError] If there's a client-side error
|
90
|
-
# @raise [Faraday::ServerError] If there's a server-side error
|
91
|
-
def destroy(name)
|
92
|
-
self.api.delete("api/datasources/#{name}")
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
@@ -1,237 +0,0 @@
|
|
1
|
-
require "openai"
|
2
|
-
require "uri"
|
3
|
-
module Minds
|
4
|
-
module Resources
|
5
|
-
DEFAULT_PROMPT_TEMPLATE = "Use your database tools to answer the user's question: {{question}}"
|
6
|
-
|
7
|
-
class Mind < Base
|
8
|
-
attr_accessor :name, :model_name, :provider, :parameters, :created_at, :updated_at, :datasources
|
9
|
-
|
10
|
-
def initialize(client, attributes = {})
|
11
|
-
super(client)
|
12
|
-
@project = "mindsdb"
|
13
|
-
@name = attributes["name"]
|
14
|
-
@model_name = attributes["model_name"]
|
15
|
-
@provider = attributes["provider"]
|
16
|
-
@parameters = attributes["parameters"] || {}
|
17
|
-
@prompt_template = @parameters.delete("prompt_template")
|
18
|
-
@created_at = attributes["created_at"]
|
19
|
-
@updated_at = attributes["updated_at"]
|
20
|
-
@datasources = attributes["datasources"]
|
21
|
-
end
|
22
|
-
|
23
|
-
# Update the mind with new parameters
|
24
|
-
#
|
25
|
-
# @param name [String, nil] New name of the mind (optional)
|
26
|
-
# @param model_name [String, nil] New LLM model name (optional)
|
27
|
-
# @param provider [String, nil] New LLM provider (optional)
|
28
|
-
# @param prompt_template [String, nil] New prompt template (optional)
|
29
|
-
# @param datasources [Array<String, Datasource, DatabaseConfig>, nil] Alter list of datasources used by mind (optional)
|
30
|
-
# Datasource can be passed as:
|
31
|
-
# - String: name of the datasource
|
32
|
-
# - Datasource object (Minds::Resources::Datasource)
|
33
|
-
# - DatabaseConfig object (Minds::Resources::DatabaseConfig), in this case datasource will be created
|
34
|
-
# @param parameters [Hash, nil] Alter other parameters of the mind (optional)
|
35
|
-
# @return [void]
|
36
|
-
def update(name: nil, model_name: nil, provider: nil, prompt_template: nil, datasources: nil, parameters: nil)
|
37
|
-
data = {}
|
38
|
-
|
39
|
-
ds_names = []
|
40
|
-
datasources.each do |ds|
|
41
|
-
minds = Minds.new(self)
|
42
|
-
ds_name = minds.check_datasource(ds)
|
43
|
-
ds_names << ds_name
|
44
|
-
data["datasources"] = ds_names
|
45
|
-
end if datasources
|
46
|
-
|
47
|
-
data["name"] = name if name
|
48
|
-
data["model_name"] = model_name if model_name
|
49
|
-
data["provider"] = provider if provider
|
50
|
-
data["parameters"] = parameters.nil? ? {} : parameters
|
51
|
-
data["parameters"]["prompt_template"] = prompt_template if prompt_template
|
52
|
-
|
53
|
-
self.api.patch("/api/projects/#{@project}/minds/#{@name}") do |req|
|
54
|
-
req.body = data.to_json
|
55
|
-
end
|
56
|
-
|
57
|
-
@name = name if name && name != @name
|
58
|
-
end
|
59
|
-
|
60
|
-
# Add a datasource to the mind
|
61
|
-
#
|
62
|
-
# @param datasource [String, Datasource, DatabaseConfig] The datasource to add
|
63
|
-
# Can be passed as:
|
64
|
-
# - String: name of the datasource
|
65
|
-
# - Datasource object (Minds::Resources::Datasource)
|
66
|
-
# - DatabaseConfig object (Minds::Resources::DatabaseConfig), in this case datasource will be created
|
67
|
-
# @return [void]
|
68
|
-
def add_datasources(datasource)
|
69
|
-
minds = Minds.new(self)
|
70
|
-
ds_name = minds.check_datasource(datasource)
|
71
|
-
self.api.post("/api/projects/#{project}/minds/#{@name}/datasources") do |req|
|
72
|
-
req.body = { name: ds_name }.to_json
|
73
|
-
end
|
74
|
-
|
75
|
-
mind = minds.find(@name)
|
76
|
-
|
77
|
-
@datasources = mind.datasources
|
78
|
-
end
|
79
|
-
|
80
|
-
# Delete a datasource from the mind
|
81
|
-
#
|
82
|
-
# @param datasource [String, Datasource] The datasource to delete
|
83
|
-
# Can be passed as:
|
84
|
-
# - String: name of the datasource
|
85
|
-
# - Datasource object (Minds::Resources::Datasource)
|
86
|
-
# @return [void]
|
87
|
-
def destroy_datasources(datasource)
|
88
|
-
if datasource.is_a?(Datasource)
|
89
|
-
datasource = datasource.name
|
90
|
-
elsif !datasource.is_a?(String)
|
91
|
-
raise ArgumentError, "Unknown type of datasource: #{datasource}"
|
92
|
-
end
|
93
|
-
self.api.delete("/api/projects/#{@project}/minds/#{@name}/datasources/#{datasource}")
|
94
|
-
|
95
|
-
minds = Minds.new(self)
|
96
|
-
mind = minds.find(@name)
|
97
|
-
@datasources = mind.datasources
|
98
|
-
end
|
99
|
-
|
100
|
-
# Call mind completion
|
101
|
-
#
|
102
|
-
# @param message [String] The input question or prompt
|
103
|
-
# @param stream [Boolean] Whether to enable stream mode (default: false)
|
104
|
-
# @return [String, Enumerator] If stream mode is off, returns a String.
|
105
|
-
# If stream mode is on, returns an Enumerator of ChoiceDelta objects (as defined by OpenAI)
|
106
|
-
def completion(message:, stream: false)
|
107
|
-
openai_client = OpenAI::Client.new(access_token: self.api_key, uri_base: self.base_url)
|
108
|
-
if stream
|
109
|
-
openai_client.chat(
|
110
|
-
parameters: {
|
111
|
-
model: @name,
|
112
|
-
messages: [ { role: "user", content: message } ], # Required.
|
113
|
-
temperature: 0,
|
114
|
-
stream: proc do |chunk, _bytesize|
|
115
|
-
print chunk.dig("choices", 0, "delta")
|
116
|
-
end
|
117
|
-
}
|
118
|
-
)
|
119
|
-
else
|
120
|
-
response = openai_client.chat(
|
121
|
-
parameters: {
|
122
|
-
model: @name,
|
123
|
-
messages: [ { role: "user", content: message } ],
|
124
|
-
temperature: 0
|
125
|
-
}
|
126
|
-
)
|
127
|
-
response.dig("choices", 0, "message", "content")
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
class Minds < Base
|
133
|
-
def initialize(client)
|
134
|
-
super
|
135
|
-
@project = "mindsdb"
|
136
|
-
end
|
137
|
-
|
138
|
-
# Returns a list of all minds
|
139
|
-
#
|
140
|
-
# @return [Array<Mind>] An array of Mind objects
|
141
|
-
def all
|
142
|
-
data = self.api.get("/api/projects/#{@project}/minds").body
|
143
|
-
return [] if data.empty?
|
144
|
-
|
145
|
-
data.map { |item| Mind.new(self, item) }
|
146
|
-
end
|
147
|
-
|
148
|
-
# Get a mind by name
|
149
|
-
#
|
150
|
-
# @param name [String] The name of the mind to find
|
151
|
-
# @return [Mind] The found mind object
|
152
|
-
def find(name)
|
153
|
-
resp = self.api.get("/api/projects/#{@project}/minds/#{name}")
|
154
|
-
Mind.new(self, resp.body)
|
155
|
-
end
|
156
|
-
|
157
|
-
# Drop (delete) a mind by name
|
158
|
-
#
|
159
|
-
# @param name [String] The name of the mind to delete
|
160
|
-
# @return [void]
|
161
|
-
def destroy(name)
|
162
|
-
self.api.delete("/api/projects/#{@project}/minds/#{name}")
|
163
|
-
end
|
164
|
-
|
165
|
-
# Create a new mind and return it
|
166
|
-
#
|
167
|
-
# @param name [String] The name of the mind
|
168
|
-
# @param model_name [String, nil] The LLM model name (optional)
|
169
|
-
# @param provider [String, nil] The LLM provider (optional)
|
170
|
-
# @param prompt_template [String, nil] Instructions to LLM (optional)
|
171
|
-
# @param datasources [Array<String, Datasource, DatabaseConfig>, nil] List of datasources used by mind (optional)
|
172
|
-
# Datasource can be passed as:
|
173
|
-
# - String: name of the datasource
|
174
|
-
# - Datasource object (Minds::Resources::Datasource)
|
175
|
-
# - DatabaseConfig object (Minds::Resources::DatabaseConfig), in this case datasource will be created
|
176
|
-
# @param parameters [Hash, nil] Other parameters of the mind (optional)
|
177
|
-
# @param replace [Boolean] If true, remove existing mind with the same name (default: false)
|
178
|
-
# @return [Mind] The created mind object
|
179
|
-
def create(name:, model_name: nil, provider: nil, prompt_template: nil, datasources: nil, parameters: nil, replace: false)
|
180
|
-
if replace
|
181
|
-
begin
|
182
|
-
find(name)
|
183
|
-
destroy(name)
|
184
|
-
rescue Faraday::ResourceNotFound
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
ds_names = []
|
189
|
-
datasources.each do |ds|
|
190
|
-
ds_name = check_datasource(ds)
|
191
|
-
ds_names << ds_name
|
192
|
-
end if datasources
|
193
|
-
|
194
|
-
parameters = {} if parameters.nil?
|
195
|
-
|
196
|
-
parameters["prompt_template"] = prompt_template if prompt_template
|
197
|
-
parameters["prompt_template"] ||= DEFAULT_PROMPT_TEMPLATE
|
198
|
-
self.api.post("api/projects/#{@project}/minds") do |req|
|
199
|
-
req.body = {
|
200
|
-
name: name,
|
201
|
-
model_name: model_name,
|
202
|
-
provider: provider,
|
203
|
-
parameters: parameters,
|
204
|
-
datasources: ds_names
|
205
|
-
}.to_json
|
206
|
-
end
|
207
|
-
find(name)
|
208
|
-
end
|
209
|
-
|
210
|
-
def check_datasource(ds)
|
211
|
-
ds_name = extract_datasource_name(ds)
|
212
|
-
create_datasource_if_needed(ds)
|
213
|
-
ds_name
|
214
|
-
end
|
215
|
-
|
216
|
-
private
|
217
|
-
|
218
|
-
def extract_datasource_name(ds)
|
219
|
-
case ds
|
220
|
-
when Datasource, DatabaseConfig, String
|
221
|
-
ds.respond_to?(:name) ? ds.name : ds
|
222
|
-
else
|
223
|
-
raise ArgumentError, "Unknown type of datasource: #{ds.class}"
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
|
-
def create_datasource_if_needed(ds)
|
228
|
-
return unless ds.is_a?(DatabaseConfig)
|
229
|
-
|
230
|
-
datasources = Datasources.new(self)
|
231
|
-
datasources.find(ds.name)
|
232
|
-
rescue Faraday::ResourceNotFound
|
233
|
-
datasources.create(ds)
|
234
|
-
end
|
235
|
-
end
|
236
|
-
end
|
237
|
-
end
|