Class: Roseflow::OpenAI::Model
- Inherits:
-
Object
- Object
- Roseflow::OpenAI::Model
- Defined in:
- lib/roseflow/openai/model.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #blocking? ⇒ Boolean
-
#call(operation, options) {|chunk| ... } ⇒ Faraday::Response
Calls the model.
-
#chat(messages, options = {}) {|chunk| ... } ⇒ OpenAI::ChatResponse
Convenience method for chat completions.
-
#chattable? ⇒ Boolean
Indicates if the model is chattable.
-
#completionable? ⇒ Boolean
Indicates if the model can do completions.
-
#embeddable? ⇒ Boolean
Indicates if the model can do embeddings.
-
#finetuneable? ⇒ Boolean
Indicates if the model is fine-tunable.
-
#imageable? ⇒ Boolean
Indicates if the model can do image completions.
-
#initialize(model, provider) ⇒ Model
constructor
Initializes a new model instance.
-
#max_tokens ⇒ Object
Returns the maximum number of tokens for the model.
-
#operations ⇒ Object
Returns a list of operations for the model.
-
#sampleable? ⇒ Boolean
Indicates if the model can be sampled.
-
#searchable_indices? ⇒ Boolean
Indicates if the model has searchable indices.
-
#tokenizer ⇒ Object
Tokenizer instance for the model.
Constructor Details
#initialize(model, provider) ⇒ Model
Initializes a new model instance.
20 21 22 23 24 |
# File 'lib/roseflow/openai/model.rb', line 20 def initialize(model, provider) @model_ = model @provider_ = provider assign_attributes end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/roseflow/openai/model.rb', line 14 def name @name end |
Instance Method Details
#blocking? ⇒ Boolean
98 99 100 |
# File 'lib/roseflow/openai/model.rb', line 98 def blocking? @permissions_.fetch("is_blocking") end |
#call(operation, options) {|chunk| ... } ⇒ Faraday::Response
Calls the model.
50 51 52 53 |
# File 'lib/roseflow/openai/model.rb', line 50 def call(operation, , &block) operation = OperationHandler.new(operation, ).call client.post(operation, &block) end |
#chat(messages, options = {}) {|chunk| ... } ⇒ OpenAI::ChatResponse
Convenience method for chat completions.
37 38 39 40 41 42 |
# File 'lib/roseflow/openai/model.rb', line 37 def chat(, = {}, &block) token_count = tokenizer.count_tokens((.fetch(:messages, []))) raise TokenLimitExceededError, "Token limit for model #{name} exceeded: #{token_count} is more than #{max_tokens}" if token_count > max_tokens response = call(:chat, .merge({ messages: , model: name }), &block) ChatResponse.new(response) unless block_given? end |
#chattable? ⇒ Boolean
Indicates if the model is chattable.
64 65 66 |
# File 'lib/roseflow/openai/model.rb', line 64 def chattable? OpenAI::Config::CHAT_MODELS.include?(name) end |
#completionable? ⇒ Boolean
Indicates if the model can do completions.
69 70 71 |
# File 'lib/roseflow/openai/model.rb', line 69 def completionable? OpenAI::Config::COMPLETION_MODELS.include?(name) end |
#embeddable? ⇒ Boolean
Indicates if the model can do embeddings.
79 80 81 |
# File 'lib/roseflow/openai/model.rb', line 79 def OpenAI::Config::EMBEDDING_MODELS.include?(name) end |
#finetuneable? ⇒ Boolean
Indicates if the model is fine-tunable.
84 85 86 |
# File 'lib/roseflow/openai/model.rb', line 84 def finetuneable? @permissions_.fetch("allow_fine_tuning") end |
#imageable? ⇒ Boolean
Indicates if the model can do image completions.
74 75 76 |
# File 'lib/roseflow/openai/model.rb', line 74 def imageable? OpenAI::Config::IMAGE_MODELS.include?(name) end |
#max_tokens ⇒ Object
Returns the maximum number of tokens for the model.
103 104 105 |
# File 'lib/roseflow/openai/model.rb', line 103 def max_tokens OpenAI::Config::MAX_TOKENS.fetch(name, 2049) end |
#operations ⇒ Object
Returns a list of operations for the model.
TODO: OpenAI does not actually provide this information per model. Figure out a way to do this in a proper way if feasible.
59 60 61 |
# File 'lib/roseflow/openai/model.rb', line 59 def operations OperationHandler::OPERATION_CLASSES.keys end |
#sampleable? ⇒ Boolean
Indicates if the model can be sampled.
94 95 96 |
# File 'lib/roseflow/openai/model.rb', line 94 def sampleable? @permissions_.fetch("allow_sampling") end |
#searchable_indices? ⇒ Boolean
Indicates if the model has searchable indices.
89 90 91 |
# File 'lib/roseflow/openai/model.rb', line 89 def searchable_indices? @permissions_.fetch("allow_search_indices") end |
#tokenizer ⇒ Object
Tokenizer instance for the model.
27 28 29 |
# File 'lib/roseflow/openai/model.rb', line 27 def tokenizer @tokenizer_ ||= Roseflow::Tiktoken::Tokenizer.new(model: name) end |