Class: Minds::Mind

Inherits:
Object
  • Object
show all
Defined in:
lib/minds/minds.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_atObject (readonly)

Returns the value of attribute created_at.



10
11
12
# File 'lib/minds/minds.rb', line 10

def created_at
  @created_at
end

#datasourcesObject (readonly)

Returns the value of attribute datasources.



10
11
12
# File 'lib/minds/minds.rb', line 10

def datasources
  @datasources
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



10
11
12
# File 'lib/minds/minds.rb', line 10

def model_name
  @model_name
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/minds/minds.rb', line 10

def name
  @name
end

#parametersObject (readonly)

Returns the value of attribute parameters.



10
11
12
# File 'lib/minds/minds.rb', line 10

def parameters
  @parameters
end

#prompt_templateObject (readonly)

Returns the value of attribute prompt_template.



10
11
12
# File 'lib/minds/minds.rb', line 10

def prompt_template
  @prompt_template
end

#providerObject (readonly)

Returns the value of attribute provider.



10
11
12
# File 'lib/minds/minds.rb', line 10

def provider
  @provider
end

#updated_atObject (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

Parameters:

  • datasource (String, Datasource, DatabaseConfig)

    Datasource to add Can be passed as:

    • String: name of the datasource

    • Datasource object (Minds::Datasource)

    • DatabaseConfig object (Minds::DatabaseConfig), in this case datasource will be created



77
78
79
80
81
82
83
84
85
86
# File 'lib/minds/minds.rb', line 77

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

Parameters:

  • message (String)

    The input question or prompt

  • stream (Boolean) (defaults to: false)

    Whether to enable stream mode (default: false)

Returns:

  • (String, Enumerator)

    If stream mode is off, returns a String. If stream mode is on, returns an Enumerator of ChoiceDelta objects (as defined by OpenAI)



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/minds/minds.rb', line 115

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: message } ],
    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

Parameters:

  • datasource (String, Datasource)

    Datasource to remove Can be passed as:

    • String: name of the datasource

    • Datasource object (Minds::Datasource)

Raises:

  • (ArgumentError)

    If datasource type is invalid



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/minds/minds.rb', line 96

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

Parameters:

  • name (String, nil) (defaults to: nil)

    New name of the mind (optional)

  • model_name (String, nil) (defaults to: nil)

    New LLM model name (optional)

  • provider (String, nil) (defaults to: nil)

    New LLM provider (optional)

  • prompt_template (String, nil) (defaults to: nil)

    New prompt template (optional)

  • datasources (Array<String, Datasource, DatabaseConfig>, nil) (defaults to: nil)

    Alter list of datasources used by mind (optional) Datasource can be passed as:

    • String: name of the datasource

    • Datasource object (Minds::Datasource)

    • DatabaseConfig object (Minds::DatabaseConfig), in this case datasource will be created

  • parameters (Hash, nil) (defaults to: nil)

    Alter other parameters of the mind (optional)



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
# File 'lib/minds/minds.rb', line 38

def update(name: nil, model_name: nil, provider: nil, prompt_template: nil, datasources: nil, parameters: 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