Class: Rasti::AI::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/rasti/ai/provider.rb

Constant Summary collapse

MODULES =
{
  open_ai: 'OpenAI',
  gemini: 'Gemini',
  anthropic: 'Anthropic',
  open_router: 'OpenRouter',
  huawei_maas: 'HuaweiMaaS'
}.freeze
ALIASES =
{
  openai: :open_ai,
  openrouter: :open_router,
  huawei: :huawei_maas
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model: nil, api_key: nil, usage_tracker: nil, logger: nil, http_connect_timeout: nil, http_read_timeout: nil, http_max_retries: nil) ⇒ Provider

Returns a new instance of Provider.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rasti/ai/provider.rb', line 49

def initialize(model:nil, api_key:nil, usage_tracker:nil, logger:nil,
               http_connect_timeout:nil, http_read_timeout:nil, http_max_retries:nil)

  @model = model || default_model
  @api_key = api_key || default_api_key
  @usage_tracker = usage_tracker
  @logger = logger
  @http_connect_timeout = http_connect_timeout
  @http_read_timeout = http_read_timeout
  @http_max_retries = http_max_retries
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



47
48
49
# File 'lib/rasti/ai/provider.rb', line 47

def model
  @model
end

Class Method Details

.build(name, **options) ⇒ Object



21
22
23
24
# File 'lib/rasti/ai/provider.rb', line 21

def build(name, **options)
  return name if name.is_a? Provider
  provider_class(name).new(**options)
end

.namesObject



26
27
28
# File 'lib/rasti/ai/provider.rb', line 26

def names
  MODULES.keys
end

Instance Method Details

#base_urlObject

Raises:

  • (NotImplementedError)


73
74
75
# File 'lib/rasti/ai/provider.rb', line 73

def base_url
  raise NotImplementedError
end

#build_clientObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rasti/ai/provider.rb', line 81

def build_client
  client_class.new(
    provider: self,
    api_key: api_key,
    logger: logger,
    usage_tracker: usage_tracker,
    http_connect_timeout: http_connect_timeout,
    http_read_timeout: http_read_timeout,
    http_max_retries: http_max_retries
  )
end

#create_assistant(state: nil, system: nil, model: nil, tools: [], mcp_servers: {}, json_schema: nil, thinking: nil, client: nil) ⇒ Object

Raises:

  • (ArgumentError)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rasti/ai/provider.rb', line 93

def create_assistant(state:nil, system:nil, model:nil, tools:[], mcp_servers:{},
                     json_schema:nil, thinking:nil, client:nil)

  raise ArgumentError, 'Use state or system, not both' if state && system

  state = AssistantState.new(context: system) if state.nil? && system

  assistant_class.new(
    provider: self,
    client: client,
    model: model || self.model,
    state: state,
    tools: tools,
    mcp_servers: mcp_servers,
    json_schema: json_schema,
    thinking: thinking,
    logger: logger
  )
end

#default_api_keyObject

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/rasti/ai/provider.rb', line 69

def default_api_key
  raise NotImplementedError
end

#default_modelObject

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/rasti/ai/provider.rb', line 65

def default_model
  raise NotImplementedError
end

#generate_text(prompt: nil, messages: nil, system: nil, model: nil, json_schema: nil, thinking: nil, client: nil) ⇒ Object

Raises:

  • (ArgumentError)


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

def generate_text(prompt:nil, messages:nil, system:nil, model:nil,
                  json_schema:nil, thinking:nil, client:nil)

  raise ArgumentError, 'Use prompt or messages, not both' if prompt && messages
  raise ArgumentError, 'Undefined prompt or messages' if prompt.nil? && messages.nil?

  system_prompt, conversation = split_system(messages || [{role: Roles::USER, content: prompt}])

  client ||= build_client

  response = request(
    client: client,
    messages: conversation.map { |message| encode_message message },
    system: system || system_prompt,
    model: model || self.model,
    json_schema: json_schema,
    thinking: thinking_config(thinking)
  )

  Result.new content: parse_content(response),
             usage: parse_usage(response),
             raw: response
end

#nameObject

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/rasti/ai/provider.rb', line 61

def name
  raise NotImplementedError
end

#parse_usage(response) ⇒ Object

Raises:

  • (NotImplementedError)


77
78
79
# File 'lib/rasti/ai/provider.rb', line 77

def parse_usage(response)
  raise NotImplementedError
end