Class: Minds::Mind
- Inherits:
-
Object
- Object
- Minds::Mind
- Defined in:
- lib/minds/minds.rb
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#datasources ⇒ Object
readonly
Returns the value of attribute datasources.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#prompt_template ⇒ Object
readonly
Returns the value of attribute prompt_template.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
Instance Method Summary collapse
-
#add_datasources(datasource) ⇒ void
Add datasource to mind.
-
#completion(message:, stream: false) ⇒ String, Enumerator
Call mind completion.
-
#destroy_datasources(datasource) ⇒ void
Remove datasource from mind.
-
#initialize(client, attributes = {}) ⇒ Mind
constructor
A new instance of Mind.
-
#update(name: nil, model_name: nil, provider: nil, prompt_template: nil, datasources: nil, parameters: nil) ⇒ void
Update mind.
Constructor Details
#initialize(client, attributes = {}) ⇒ Mind
Returns a new instance of Mind.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/minds/minds.rb', line 12 def initialize(client, attributes = {}) @client = client @project = "mindsdb" @name = attributes["name"] @model_name = attributes["model_name"] @provider = attributes["provider"] @parameters = attributes["parameters"] || {} @prompt_template = @parameters.delete("prompt_template") @created_at = attributes["created_at"] @updated_at = attributes["updated_at"] @datasources = attributes["datasources"] end |
Instance Attribute Details
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
10 11 12 |
# File 'lib/minds/minds.rb', line 10 def created_at @created_at end |
#datasources ⇒ Object (readonly)
Returns the value of attribute datasources.
10 11 12 |
# File 'lib/minds/minds.rb', line 10 def datasources @datasources end |
#model_name ⇒ Object (readonly)
Returns the value of attribute model_name.
10 11 12 |
# File 'lib/minds/minds.rb', line 10 def model_name @model_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/minds/minds.rb', line 10 def name @name end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
10 11 12 |
# File 'lib/minds/minds.rb', line 10 def parameters @parameters end |
#prompt_template ⇒ Object (readonly)
Returns the value of attribute prompt_template.
10 11 12 |
# File 'lib/minds/minds.rb', line 10 def prompt_template @prompt_template end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
10 11 12 |
# File 'lib/minds/minds.rb', line 10 def provider @provider end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
10 11 12 |
# File 'lib/minds/minds.rb', line 10 def updated_at @updated_at end |
Instance Method Details
#add_datasources(datasource) ⇒ void
This method returns an undefined value.
Add datasource to mind
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/minds/minds.rb', line 78 def add_datasources(datasource) ds_name = @client.minds.check_datasource(datasource) data = { name: ds_name } @client.post(path: "projects/#{@project}/minds/#{@name}/datasources", parameters: data) mind = @client.minds.find(@name) @datasources = mind.datasources true end |
#completion(message:, stream: false) ⇒ String, Enumerator
Call mind completion
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/minds/minds.rb', line 116 def completion(message:, stream: false) openai_client = OpenAI::Client.new(access_token: @client.api_key, uri_base: @client.base_url) params = { model: @name, messages: [ { role: "user", content: } ], temperature: 0 } if stream openai_client.chat( parameters: params.merge( stream: proc do |chunk, _bytesize| yield chunk if block_given? end ) ) else response = openai_client.chat(parameters: params) response.dig("choices", 0, "message", "content") end end |
#destroy_datasources(datasource) ⇒ void
This method returns an undefined value.
Remove datasource from mind
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/minds/minds.rb', line 97 def destroy_datasources(datasource) if datasource.is_a?(Datasource) datasource = datasource.name elsif !datasource.is_a?(String) raise ArgumentError, "Unknown type of datasource: #{datasource}" end @client.delete(path: "projects/#{@project}/minds/#{@name}/datasources/#{datasource}") mind = @client.minds.find(@name) @datasources = mind.datasources true end |
#update(name: nil, model_name: nil, provider: nil, prompt_template: nil, datasources: nil, parameters: nil) ⇒ void
This method returns an undefined value.
Update mind
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/minds/minds.rb', line 38 def update(name: nil, model_name: nil, provider: nil, prompt_template: nil, datasources: nil, parameters: nil) Validators.validate_mind_name!(name) if !name.nil? data = {} ds_names = [] datasources.each do |ds| ds_name = @client.minds.check_datasource(ds) ds_names << ds_name end if datasources data["datasources"] = ds_names if !ds_names.empty? data["name"] = name if name data["model_name"] = model_name if model_name data["provider"] = provider if provider data["parameters"] = parameters.nil? ? {} : parameters data["parameters"]["prompt_template"] = prompt_template if prompt_template @client.patch(path: "projects/#{@project}/minds/#{@name}", parameters: data) @name = name if name && name != @name updated_mind = @client.minds.find(@name) @model_name = updated_mind.model_name @datasources = updated_mind.datasources @parameters = updated_mind.parameters @prompt_template = updated_mind.prompt_template @provider = updated_mind.provider @created_at = updated_mind.created_at @updated_at = updated_mind.updated_at true end |