minds_sdk 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c7f832efbb02c672f51644db5065474eb0795f5a34da8f97b1dcd345d43cd02
4
- data.tar.gz: 6f0518f9fb72275fe165e1ef5ce47433f38eadfcf755c7dc0716d85cb488505f
3
+ metadata.gz: e7d338a8c7a60751a9ff07a8e0a80b88d5a41c8d725913d20b9a48e8101ac862
4
+ data.tar.gz: 5f6df32b49f25319e67d1ac7b2992bd6dd89922176f46ad5819b08aad119e698
5
5
  SHA512:
6
- metadata.gz: 1c0b304a9f92c73c61e9d96d9e9ef33e726dffd39740e92185ae6543eec606e2a3ea8b4dcdfcb3eb1e775f20ffbff445a1fb26e44e175c347d78d4432da41c93
7
- data.tar.gz: 822c1f3baa51a8f9385787c0f984f9241b68e5b13df67c1d281cdf626c6937c68c0c49bbae493948f8a6e811c140378e23e092a923a69a0bae7e322c89e963d2
6
+ metadata.gz: f481f5d03353c3199685763970b6c0b78c2787819d8222bb9ad8b18e71812c9de90736400ae8ace9c21ca94b529c5267845250b595e4199735e7493af358c935
7
+ data.tar.gz: 93b0f55f6be94511d2172324804f6ecc7a8c2f457beb0fc17804970596305f16e1be3afdb1622996f4149938a9db5b7d38caeb401ff284a9b310c590d6cb627d
data/README.md CHANGED
@@ -1,7 +1,10 @@
1
1
  # Minds Ruby SDK
2
+ [![Gem Version](https://badge.fury.io/rb/minds_sdk.svg)](https://badge.fury.io/rb/minds_sdk)
2
3
 
3
4
  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.
4
5
 
6
+ The best starting point is the [Mind Website](https://docs.mdb.ai/docs/data-mind), with its introduction and explanation.
7
+
5
8
  ## Installation
6
9
 
7
10
  Add this line to your application's Gemfile:
@@ -21,6 +24,11 @@ Or install it yourself as:
21
24
  ```bash
22
25
  $ gem install minds_sdk
23
26
  ```
27
+ and require with:
28
+
29
+ ```ruby
30
+ require 'minds'
31
+ ```
24
32
 
25
33
  ## Getting Started
26
34
 
@@ -44,7 +52,7 @@ end
44
52
  Alternatively, you can configure each client instance individually:
45
53
 
46
54
  ```ruby
47
- client = Minds::Client.new("YOUR_API_KEY", "https://mdb.ai")
55
+ client = Minds::Client.new(api_key: "YOUR_API_KEY", base_url: "https://mdb.ai")
48
56
  ```
49
57
 
50
58
  ### Initialize the Client
@@ -56,19 +64,35 @@ After configuration, you can initialize the client:
56
64
  client = Minds::Client.new
57
65
 
58
66
  # Or with instance-specific configuration
59
- client = Minds::Client.new("YOUR_API_KEY", "https://mdb.ai")
67
+ client = Minds::Client.new(api_key: "YOUR_API_KEY", base_url: "https://mdb.ai")
60
68
 
61
69
  # For a self-hosted Minds Cloud instance
62
- client = Minds::Client.new("YOUR_API_KEY", "https://<custom_cloud>.mdb.ai")
70
+ client = Minds::Client.new(api_key: "YOUR_API_KEY", base_url: "https://<custom_cloud>.mdb.ai")
71
+ ```
72
+ > Get your minds api key [here](https://mdb.ai/apiKeys)
73
+
74
+ ### Logging
75
+
76
+ 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:
77
+
78
+ ```ruby
79
+ # Global configuration
80
+ Minds::Client.configure do |config|
81
+ config.log_errors = true
82
+ end
83
+
84
+ # Or instance configuration
85
+ client = Minds::Client.new(log_errors: true)
63
86
  ```
64
87
 
65
88
  ## Resources
89
+
66
90
  ### Creating a Data Source
67
91
 
68
92
  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.
69
93
 
70
94
  ```ruby
71
- postgres_config = Minds::Resources::DatabaseConfig.new(
95
+ postgres_config = Minds::DatabaseConfig.new(
72
96
  name: 'my_datasource',
73
97
  description: '<DESCRIPTION-OF-YOUR-DATA>',
74
98
  engine: 'postgres',
@@ -83,9 +107,10 @@ postgres_config = Minds::Resources::DatabaseConfig.new(
83
107
  tables: ['<TABLE-1>', '<TABLE-2>']
84
108
  )
85
109
 
86
- See supported [Data Sources](https://docs.mdb.ai/docs/data_sources)
87
110
  ```
88
111
 
112
+ > See supported [Data Sources](https://docs.mdb.ai/docs/data_sources)
113
+
89
114
  ### Creating a Mind
90
115
 
91
116
  You can create a mind and associate it with a data source.
@@ -132,7 +157,7 @@ mind.update(
132
157
  You can list all the minds you've created:
133
158
 
134
159
  ```ruby
135
- puts client.minds.all
160
+ client.minds.all
136
161
  ```
137
162
 
138
163
  ### Get a Mind by Name
@@ -156,7 +181,7 @@ client.minds.destroy('mind_name')
156
181
  To view all data sources:
157
182
 
158
183
  ```ruby
159
- puts client.datasources.all
184
+ client.datasources.all
160
185
  ```
161
186
 
162
187
  ### Get a Data Source by Name
@@ -186,7 +211,12 @@ response = mind.completion(message: "Hello, how are you?")
186
211
  puts response
187
212
 
188
213
  # For streaming responses
189
- mind.completion(message: "Tell me a story", stream: true)
214
+ mind.completion(message: "Tell me a story", stream: true) do |chunk|
215
+ puts chunk
216
+ end
217
+
218
+ # => {"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}
219
+ # => ...
190
220
  ```
191
221
 
192
222
  ## Development
@@ -195,11 +225,13 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
195
225
 
196
226
  ## Contributing
197
227
 
198
- Bug reports and pull requests are welcome on GitHub at [here](https://github.com/your_github_username/minds_ruby_sdk).
228
+ Bug reports and pull requests are welcome on GitHub at [here](https://github.com/tungnt1203/minds_ruby_sdk).
199
229
 
200
230
  ## Acknowledgments
201
231
 
202
- This SDK is built for integration with Minds, AI layer for existing databases. We would like to express our gratitude to the MindsDB team for their innovative work in making AI more accessible.
232
+ This SDK is built for integration with Minds, AI layer for existing databases. See more docs [here](https://docs.mdb.ai/docs/data-mind)
233
+
234
+ We would like to express our gratitude to the MindsDB team for their innovative work in making AI more accessible.
203
235
  For more information about MindsDB, please visit their official website: [https://mindsdb.com/](https://mindsdb.com/)
204
236
  ## License
205
237
 
data/lib/minds/client.rb CHANGED
@@ -1,12 +1,18 @@
1
- require "faraday"
2
- require "json"
3
- require_relative "config/base"
4
- require_relative "resources/base"
5
- require_relative "resources/minds"
6
- require_relative "resources/datasources"
7
-
8
1
  module Minds
9
2
  class Client
3
+ include Minds::RestClient
4
+
5
+ SENSITIVE_ATTRIBUTES = %i[@base_url @api_key].freeze
6
+ CONFIG_KEYS = %i[base_url api_key log_errors api_version].freeze
7
+ attr_reader(*CONFIG_KEYS)
8
+
9
+ def initialize(options = {})
10
+ # if key not present. Fall back to global config
11
+ CONFIG_KEYS.each do |key|
12
+ instance_variable_set "@#{key}", options[key] || Client.config.send(key)
13
+ end
14
+ end
15
+
10
16
  class << self
11
17
  def config
12
18
  @config ||= Config::Base.new
@@ -17,26 +23,22 @@ module Minds
17
23
  end
18
24
  end
19
25
 
20
- attr_accessor :base_url, :api_key
21
- DEFAULT_HOST = "https://mdb.ai"
22
-
23
- def initialize(api_key = nil, base_url = nil)
24
- # if api_key & base_url not present. Fall back to global config
25
- @base_url =
26
- if base_url.nil?
27
- Minds::Client.config.send(:base_url).nil? ? DEFAULT_HOST : Minds::Client.config.send(:base_url)
28
- else
29
- base_url
30
- end
31
- @api_key = api_key.nil? ? Minds::Client.config.send(:api_key) : api_key
32
- end
33
-
34
26
  def datasources
35
- @datasources ||= Minds::Resources::Datasources.new(self)
27
+ @datasources ||= Datasources.new(client: self)
36
28
  end
37
29
 
38
30
  def minds
39
- @minds ||= Minds::Resources::Minds.new(self)
31
+ @minds ||= Minds.new(client: self)
32
+ end
33
+
34
+ def inspect
35
+ vars = instance_variables.map do |var|
36
+ value = instance_variable_get(var)
37
+
38
+ SENSITIVE_ATTRIBUTES.include?(var) ? "#{var}=[FILTERED]" : "#{var}=#{value.inspect}"
39
+ end
40
+
41
+ "#<#{self.class}:0x#{object_id.to_s(16)} #{vars.join(', ')}>"
40
42
  end
41
43
  end
42
44
  end
@@ -1,11 +1,16 @@
1
1
  module Minds
2
2
  module Config
3
3
  class Base
4
- attr_accessor :base_url, :api_key
4
+ attr_accessor :base_url, :api_key, :log_errors, :api_version
5
+ DEFAULT_BASE_URL = "https://mdb.ai".freeze
6
+ DEFAULT_LOG_ERRORS = false
7
+ DEFAULT_API_VERSION = "api".freeze
5
8
 
6
9
  def initialize
7
- @base_url = nil
8
10
  @api_key = nil
11
+ @base_url = DEFAULT_BASE_URL
12
+ @log_errors = DEFAULT_LOG_ERRORS
13
+ @api_version = DEFAULT_API_VERSION
9
14
  end
10
15
  end
11
16
  end
@@ -0,0 +1,89 @@
1
+ module Minds
2
+ class DatabaseConfig
3
+ attr_reader :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
+ # Create a new datasource and return it
32
+ #
33
+ # @param ds_config [DatabaseConfig] datasource configuration
34
+ # @option ds_config [String] :name Name of the datasource
35
+ # @option ds_config [String] :engine Type of database handler (e.g., 'postgres', 'mysql')
36
+ # @option ds_config [String] :description Description of the database. Used by mind to understand what data can be retrieved from it.
37
+ # @option ds_config [Hash] :connection_data (optional) Credentials to connect to the database
38
+ # @option ds_config [Array<String>] :tables (optional) List of allowed tables
39
+ # @param replace [Boolean] If true, replaces an existing datasource with the same name
40
+ # @return [Datasource] The created datasource object
41
+ def create(ds_config, replace = false)
42
+ name = ds_config.name
43
+ if replace
44
+ begin
45
+ find(name)
46
+ destroy(name, force: true)
47
+ end
48
+ end
49
+ @client.post(path: "datasources", parameters: ds_config.to_h)
50
+ find(name)
51
+ end
52
+
53
+ # Returns a list of all datasources
54
+ #
55
+ # @return [Array<Datasource>] An array of Datasource objects
56
+ def all
57
+ data = @client.get(path: "datasources")
58
+ return [] if data.empty?
59
+
60
+ data.each_with_object([]) do |item, ds_list|
61
+ next if item["engine"].nil?
62
+
63
+ ds_list << Datasource.new(**item.transform_keys(&:to_sym))
64
+ end
65
+ end
66
+
67
+ # Get a datasource by name
68
+ #
69
+ # @param name [String] The name of the datasource to find
70
+ # @return [Datasource] The found datasource object
71
+ # @raise [ObjectNotSupported] If the datasource type is not supported
72
+ def find(name)
73
+ data = @client.get(path: "datasources/#{name}")
74
+ if data["engine"].nil?
75
+ raise ObjectNotSupported, "Wrong type of datasource: #{name}"
76
+ end
77
+ Datasource.new(**data.transform_keys(&:to_sym))
78
+ end
79
+
80
+ # Destroy (delete) a datasource by name
81
+ #
82
+ # @param name [String] The name of the datasource to delete
83
+ # @param force: if true -remove from all minds, default: false
84
+ def destroy(name, force: false)
85
+ data = force ? { cascade: true } : nil
86
+ @client.delete(path: "datasources/#{name}", parameters: data)
87
+ end
88
+ end
89
+ end
data/lib/minds/errors.rb CHANGED
@@ -1,5 +1,6 @@
1
- class Error < StandardError; end
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
@@ -0,0 +1,222 @@
1
+ require "openai"
2
+ require "uri"
3
+
4
+ module Minds
5
+ DEFAULT_PROMPT_TEMPLATE = "Use your database tools to answer the user's question: {{question}}"
6
+
7
+ class Mind
8
+ attr_accessor :name, :model_name, :provider, :parameters, :created_at, :updated_at, :datasources
9
+
10
+ def initialize(client, attributes = {})
11
+ @client = 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
+ ds_name = @client.minds.check_datasource(ds)
42
+ ds_names << ds_name
43
+ data["datasources"] = ds_names
44
+ end if datasources
45
+
46
+ data["name"] = name if name
47
+ data["model_name"] = model_name if model_name
48
+ data["provider"] = provider if provider
49
+ data["parameters"] = parameters.nil? ? {} : parameters
50
+ data["parameters"]["prompt_template"] = prompt_template if prompt_template
51
+
52
+ @client.patch(path: "projects/#{@project}/minds/#{@name}", parameters: data)
53
+
54
+ @name = name if name && name != @name
55
+ end
56
+
57
+ # Add a datasource to the mind
58
+ #
59
+ # @param datasource [String, Datasource, DatabaseConfig] The datasource to add
60
+ # Can be passed as:
61
+ # - String: name of the datasource
62
+ # - Datasource object (Minds::Resources::Datasource)
63
+ # - DatabaseConfig object (Minds::Resources::DatabaseConfig), in this case datasource will be created
64
+ # @return [void]
65
+ def add_datasources(datasource)
66
+ ds_name = @client.minds.check_datasource(datasource)
67
+ data = { name: ds_name }
68
+ @client.post(path: "projects/#{@project}/minds/#{@name}/datasources", parameters: data)
69
+
70
+ mind = @client.minds.find(@name)
71
+ @datasources = mind.datasources
72
+ end
73
+
74
+ # Delete a datasource from the mind
75
+ #
76
+ # @param datasource [String, Datasource] The datasource to delete
77
+ # Can be passed as:
78
+ # - String: name of the datasource
79
+ # - Datasource object (Minds::Resources::Datasource)
80
+ # @return [void]
81
+ def destroy_datasources(datasource)
82
+ if datasource.is_a?(Datasource)
83
+ datasource = datasource.name
84
+ elsif !datasource.is_a?(String)
85
+ raise ArgumentError, "Unknown type of datasource: #{datasource}"
86
+ end
87
+ @client.delete(path: "projects/#{@project}/minds/#{@name}/datasources/#{datasource}")
88
+
89
+ mind = @client.minds.find(@name)
90
+ @datasources = mind.datasources
91
+ end
92
+
93
+ # Call mind completion
94
+ #
95
+ # @param message [String] The input question or prompt
96
+ # @param stream [Boolean] Whether to enable stream mode (default: false)
97
+ # @return [String, Enumerator] If stream mode is off, returns a String.
98
+ # If stream mode is on, returns an Enumerator of ChoiceDelta objects (as defined by OpenAI)
99
+ def completion(message:, stream: false)
100
+ openai_client = OpenAI::Client.new(access_token: @client.api_key, uri_base: @client.base_url)
101
+ params = {
102
+ model: @name,
103
+ messages: [ { role: "user", content: message } ],
104
+ temperature: 0
105
+ }
106
+
107
+ if stream
108
+ openai_client.chat(
109
+ parameters: params.merge(
110
+ stream: proc do |chunk, _bytesize|
111
+ yield chunk if block_given?
112
+ end
113
+ )
114
+ )
115
+ else
116
+ response = openai_client.chat(parameters: params)
117
+ response.dig("choices", 0, "message", "content")
118
+ end
119
+ end
120
+ end
121
+
122
+ class Minds
123
+ def initialize(client:)
124
+ @client = client
125
+ @project = "mindsdb"
126
+ end
127
+
128
+ # Returns a list of all minds
129
+ #
130
+ # @return [Array<Mind>] An array of Mind objects
131
+ def all
132
+ data = @client.get(path: "projects/#{@project}/minds")
133
+ return [] if data.empty?
134
+
135
+ data.map { |item| Mind.new(@client, item) }
136
+ end
137
+
138
+ # Get a mind by name
139
+ #
140
+ # @param name [String] The name of the mind to find
141
+ # @return [Mind] The found mind object
142
+ def find(name)
143
+ data = @client.get(path: "projects/#{@project}/minds/#{name}")
144
+ Mind.new(@client, data)
145
+ end
146
+
147
+ # Drop (delete) a mind by name
148
+ #
149
+ # @param name [String] The name of the mind to delete
150
+ # @return [void]
151
+ def destroy(name)
152
+ @client.delete(path: "projects/#{@project}/minds/#{name}")
153
+ end
154
+
155
+ # Create a new mind and return it
156
+ #
157
+ # @param name [String] The name of the mind
158
+ # @param model_name [String, nil] The LLM model name (optional)
159
+ # @param provider [String, nil] The LLM provider (optional)
160
+ # @param prompt_template [String, nil] Instructions to LLM (optional)
161
+ # @param datasources [Array<String, Datasource, DatabaseConfig>, nil] List of datasources used by mind (optional)
162
+ # Datasource can be passed as:
163
+ # - String: name of the datasource
164
+ # - Datasource object (Minds::Datasource)
165
+ # - DatabaseConfig object (Minds::DatabaseConfig), in this case datasource will be created
166
+ # @param parameters [Hash, nil] Other parameters of the mind (optional)
167
+ # @param replace [Boolean] If true, remove existing mind with the same name (default: false)
168
+ # @return [Mind] The created mind object
169
+ def create(name:, model_name: nil, provider: nil, prompt_template: nil, datasources: nil, parameters: nil, replace: false)
170
+ if replace
171
+ begin
172
+ find(name)
173
+ destroy(name)
174
+ end
175
+ end
176
+
177
+ ds_names = []
178
+ datasources.each do |ds|
179
+ ds_name = check_datasource(ds)
180
+ ds_names << ds_name
181
+ end if datasources
182
+
183
+ parameters = {} if parameters.nil?
184
+
185
+ parameters["prompt_template"] = prompt_template if prompt_template
186
+ parameters["prompt_template"] ||= DEFAULT_PROMPT_TEMPLATE
187
+ data = {
188
+ name: name,
189
+ model_name: model_name,
190
+ provider: provider,
191
+ parameters: parameters,
192
+ datasources: ds_names
193
+ }
194
+
195
+ @client.post(path: "projects/#{@project}/minds", parameters: data)
196
+ find(name)
197
+ end
198
+
199
+ def check_datasource(ds)
200
+ ds_name = extract_datasource_name(ds)
201
+ create_datasource_if_needed(ds)
202
+ ds_name
203
+ end
204
+
205
+ private
206
+
207
+ def extract_datasource_name(ds)
208
+ case ds
209
+ when Datasource, DatabaseConfig, String
210
+ ds.respond_to?(:name) ? ds.name : ds
211
+ else
212
+ raise ArgumentError, "Unknown type of datasource: #{ds.class}"
213
+ end
214
+ end
215
+
216
+ def create_datasource_if_needed(ds)
217
+ return unless ds.is_a?(DatabaseConfig)
218
+
219
+ @client.datasources.find(ds.name)
220
+ end
221
+ end
222
+ end
@@ -0,0 +1,44 @@
1
+ module Minds
2
+ module RestClient
3
+ def get(path:)
4
+ conn.get(uri(path: path))&.body
5
+ end
6
+
7
+ def post(path:, parameters:)
8
+ conn.post(uri(path: path)) do |req|
9
+ req.body = parameters.to_json
10
+ end&.body
11
+ end
12
+
13
+ def patch(path:, parameters:)
14
+ conn.patch(uri(path: path)) do |req|
15
+ req.body = parameters.to_json
16
+ end&.body
17
+ end
18
+
19
+ def delete(path:, parameters: nil)
20
+ conn.delete(uri(path: path)) do |req|
21
+ req.body = parameters.to_json
22
+ end&.body
23
+ end
24
+
25
+ private
26
+
27
+ def uri(path:)
28
+ return path if @base_url.include?(@api_version)
29
+
30
+ "/#{@api_version}/#{path}"
31
+ end
32
+
33
+ def conn
34
+ Faraday.new(url: @base_url) do |builder|
35
+ builder.use MiddlewareErrors if @log_errors
36
+ builder.headers["Authorization"] = "Bearer #{@api_key}"
37
+ builder.headers["Content-Type"] = "application/json"
38
+ builder.request :json
39
+ builder.response :json
40
+ builder.response :raise_error
41
+ end
42
+ end
43
+ end
44
+ end
data/lib/minds/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Minds
4
- VERSION = "0.1.0"
4
+ VERSION = "1.0.0"
5
5
  end
data/lib/minds.rb CHANGED
@@ -1,9 +1,38 @@
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/resources/datasources"
10
+ require_relative "minds/datasources"
11
+ require_relative "minds/minds"
8
12
 
9
- module Minds; end
13
+ module Minds
14
+ class MiddlewareErrors < Faraday::Middleware
15
+ def call(env)
16
+ @app.call(env)
17
+ rescue Faraday::Error => e
18
+ raise e unless e.response.is_a?(Hash)
19
+
20
+ logger = Logger.new($stdout)
21
+ logger.formatter = proc do |_severity, datetime, _progname, msg|
22
+ timestamp = datetime.strftime("%Y-%m-%d %H:%M:%S.%L")
23
+
24
+ error_prefix = "\033[31m[Minds #{VERSION}] #{timestamp} ERROR"
25
+ error_suffix = "\033[0m"
26
+
27
+ formatted_message = msg.split("\n").map do |line|
28
+ "#{' ' * 15}#{line}"
29
+ end.join("\n")
30
+
31
+ "#{error_prefix} Rest Client Error\n#{formatted_message}#{error_suffix}\n"
32
+ end
33
+
34
+ logger.error(e.response[:body])
35
+ raise e
36
+ end
37
+ end
38
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minds_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
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-13 00:00:00.000000000 Z
11
+ date: 2024-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -55,10 +55,10 @@ files:
55
55
  - lib/minds.rb
56
56
  - lib/minds/client.rb
57
57
  - lib/minds/config/base.rb
58
+ - lib/minds/datasources.rb
58
59
  - lib/minds/errors.rb
59
- - lib/minds/resources/base.rb
60
- - lib/minds/resources/datasources.rb
61
- - lib/minds/resources/minds.rb
60
+ - lib/minds/minds.rb
61
+ - lib/minds/rest_client.rb
62
62
  - lib/minds/version.rb
63
63
  - sig/minds.rbs
64
64
  homepage: https://github.com/tungnt1203/minds_ruby_sdk
@@ -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,95 +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
- ds_list << Datasource.new(**item.transform_keys(&:to_sym))
65
- end
66
- end
67
-
68
- # Get a datasource by name
69
- #
70
- # @param name [String] The name of the datasource to find
71
- # @return [Datasource] The found datasource object
72
- # @raise [ObjectNotSupported] If the datasource type is not supported
73
- def find(name)
74
- data =self.api.get("api/datasources/#{name}").body
75
-
76
- if data["engine"].nil?
77
- raise ObjectNotSupported, "Wrong type of datasource: #{name}"
78
- end
79
-
80
- Datasource.new(**data.transform_keys(&:to_sym))
81
- end
82
-
83
- # Drop (delete) a datasource by name
84
- #
85
- # @param name [String] The name of the datasource to delete
86
- # @return [void]
87
- # @raise [Faraday::ResourceNotFound] If the datasource doesn't exist
88
- # @raise [Faraday::ClientError] If there's a client-side error
89
- # @raise [Faraday::ServerError] If there's a server-side error
90
- def destroy(name)
91
- self.api.delete("api/datasources/#{name}")
92
- end
93
- end
94
- end
95
- end
@@ -1,247 +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
- parsed = URI.parse(self.api.base_url)
108
- host = parsed.host
109
- if host == "mdb.ai"
110
- llm_host = "llm.mdb.ai"
111
- else
112
- llm_host = "ai.#{host}"
113
- end
114
- parsed.host = llm_host
115
- parsed.path = ""
116
- parsed.query = nil
117
- uri_base = parsed
118
- openai_client = OpenAI::Client.new(access_token: self.api_key, uri_base: uri_base)
119
- if stream
120
- openai_client.chat(
121
- parameters: {
122
- model: @model_name,
123
- messages: [ { role: "user", content: message } ], # Required.
124
- temperature: 0,
125
- stream: proc do |chunk, _bytesize|
126
- print chunk.dig("choices", 0, "delta")
127
- end
128
- }
129
- )
130
- else
131
- response = openai_client.chat(
132
- parameters: {
133
- model: @model_name,
134
- messages: [ { role: "user", content: message } ],
135
- temperature: 0
136
- }
137
- )
138
- response.dig("choices", 0, "message", "content")
139
- end
140
- end
141
- end
142
-
143
- class Minds < Base
144
- def initialize(client)
145
- super
146
- @project = "mindsdb"
147
- end
148
-
149
- # Returns a list of all minds
150
- #
151
- # @return [Array<Mind>] An array of Mind objects
152
- def all
153
- resp = self.api.get("/api/projects/#{@project}/minds")
154
- resp.body
155
- resp.body.map { |item| Mind.new(self, item) }
156
- end
157
-
158
- # Get a mind by name
159
- #
160
- # @param name [String] The name of the mind to find
161
- # @return [Mind] The found mind object
162
- def find(name)
163
- resp = self.api.get("/api/projects/#{@project}/minds/#{name}")
164
- Mind.new(self, resp.body)
165
- end
166
-
167
- # Drop (delete) a mind by name
168
- #
169
- # @param name [String] The name of the mind to delete
170
- # @return [void]
171
- def destroy(name)
172
- self.api.delete("/api/projects/#{@project}/minds/#{name}")
173
- end
174
-
175
- # Create a new mind and return it
176
- #
177
- # @param name [String] The name of the mind
178
- # @param model_name [String, nil] The LLM model name (optional)
179
- # @param provider [String, nil] The LLM provider (optional)
180
- # @param prompt_template [String, nil] Instructions to LLM (optional)
181
- # @param datasources [Array<String, Datasource, DatabaseConfig>, nil] List of datasources used by mind (optional)
182
- # Datasource can be passed as:
183
- # - String: name of the datasource
184
- # - Datasource object (Minds::Resources::Datasource)
185
- # - DatabaseConfig object (Minds::Resources::DatabaseConfig), in this case datasource will be created
186
- # @param parameters [Hash, nil] Other parameters of the mind (optional)
187
- # @param replace [Boolean] If true, remove existing mind with the same name (default: false)
188
- # @return [Mind] The created mind object
189
- def create(name:, model_name: nil, provider: nil, prompt_template: nil, datasources: nil, parameters: nil, replace: false)
190
- if replace
191
- begin
192
- find(name)
193
- destroy(name)
194
- rescue Faraday::ResourceNotFound
195
- end
196
- end
197
-
198
- ds_names = []
199
- datasources.each do |ds|
200
- ds_name = check_datasource(ds)
201
- ds_names << ds_name
202
- end if datasources
203
-
204
- parameters = {} if parameters.nil?
205
-
206
- parameters["prompt_template"] = prompt_template if prompt_template
207
- parameters["prompt_template"] ||= DEFAULT_PROMPT_TEMPLATE
208
- self.api.post("api/projects/#{@project}/minds") do |req|
209
- req.body = {
210
- name: name,
211
- model_name: model_name,
212
- provider: provider,
213
- parameters: parameters,
214
- datasources: ds_names
215
- }.to_json
216
-
217
- find(name)
218
- end
219
- end
220
-
221
- def check_datasource(ds)
222
- ds_name = extract_datasource_name(ds)
223
- create_datasource_if_needed(ds)
224
- ds_name
225
- end
226
-
227
- private
228
-
229
- def extract_datasource_name(ds)
230
- case ds
231
- when Datasource, DatabaseConfig, String
232
- ds.respond_to?(:name) ? ds.name : ds
233
- else
234
- raise ArgumentError, "Unknown type of datasource: #{ds.class}"
235
- end
236
- end
237
-
238
- def create_datasource_if_needed(ds)
239
- return unless ds.is_a?(DatabaseConfig)
240
- datasources = Datasources.new(self)
241
- datasources.find(ds.name)
242
- rescue Faraday::ResourceNotFound
243
- datasources.create(ds)
244
- end
245
- end
246
- end
247
- end