Class: Rasti::AI::Provider
- Inherits:
-
Object
- Object
- Rasti::AI::Provider
- Defined in:
- lib/rasti/ai/provider.rb
Direct Known Subclasses
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
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Class Method Summary collapse
Instance Method Summary collapse
- #base_url ⇒ Object
- #build_client ⇒ Object
- #create_assistant(state: nil, system: nil, model: nil, tools: [], mcp_servers: {}, json_schema: nil, thinking: nil, client: nil) ⇒ Object
- #default_api_key ⇒ Object
- #default_model ⇒ Object
- #generate_text(prompt: nil, messages: nil, system: nil, model: nil, json_schema: nil, thinking: nil, client: nil) ⇒ Object
-
#initialize(model: nil, api_key: nil, usage_tracker: nil, logger: nil, http_connect_timeout: nil, http_read_timeout: nil, http_max_retries: nil) ⇒ Provider
constructor
A new instance of Provider.
- #name ⇒ Object
- #parse_usage(response) ⇒ Object
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
#model ⇒ Object (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, **) return name if name.is_a? Provider provider_class(name).new(**) end |
.names ⇒ Object
26 27 28 |
# File 'lib/rasti/ai/provider.rb', line 26 def names MODULES.keys end |
Instance Method Details
#base_url ⇒ Object
73 74 75 |
# File 'lib/rasti/ai/provider.rb', line 73 def base_url raise NotImplementedError end |
#build_client ⇒ Object
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
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_key ⇒ Object
69 70 71 |
# File 'lib/rasti/ai/provider.rb', line 69 def default_api_key raise NotImplementedError end |
#default_model ⇒ Object
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
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 && raise ArgumentError, 'Undefined prompt or messages' if prompt.nil? && .nil? system_prompt, conversation = split_system( || [{role: Roles::USER, content: prompt}]) client ||= build_client response = request( client: client, messages: conversation.map { || }, 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 |
#name ⇒ Object
61 62 63 |
# File 'lib/rasti/ai/provider.rb', line 61 def name raise NotImplementedError end |
#parse_usage(response) ⇒ Object
77 78 79 |
# File 'lib/rasti/ai/provider.rb', line 77 def parse_usage(response) raise NotImplementedError end |