Class: RubyLLM::Context

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

Overview

A Context is an isolated configuration scope. It offers the same entry points as the top-level RubyLLM module but reads from its own Configuration copy instead of the global one, which suits multi-tenant applications and per-request overrides.

Contexts are created with RubyLLM.context:

ctx = RubyLLM.context do |config|
config.openai_api_key = ENV.fetch('TENANT_OPENAI_API_KEY')
config.request_timeout = 180
end

ctx.chat.ask "Explain Ruby blocks."
ctx.paint("A paper boat in the rain").save("boat.png")
ctx.transcribe("meeting.wav").text

The global configuration is left untouched.

Batch submission uses each chat's own configuration (a chat carries the config it was built with), so there is no context entry point for it. To look up an existing batch, where no chats carry the config, pass a context to Batch.find:

RubyLLM::Batch.find(id, provider: :anthropic, context: ctx)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Context

:nodoc:



32
33
34
# File 'lib/ruby_llm/context.rb', line 32

def initialize(config) # :nodoc:
  @config = config
end

Instance Attribute Details

#configObject (readonly)

:nodoc:



30
31
32
# File 'lib/ruby_llm/context.rb', line 30

def config
  @config
end

Instance Method Details

#animate(*args, **kwargs) ⇒ Object

Generates a video using this context's configuration, blocking until it is ready. Accepts the same arguments as RubyLLM.animate.



80
81
82
# File 'lib/ruby_llm/context.rb', line 80

def animate(*args, **kwargs, &)
  Video.animate(*args, **kwargs, context: self, &)
end

#animate_later(*args, **kwargs) ⇒ Object

Submits a video generation job using this context's configuration. Accepts the same arguments as RubyLLM.animate_later.



86
87
88
# File 'lib/ruby_llm/context.rb', line 86

def animate_later(*args, **kwargs, &)
  VideoJob.animate_later(*args, **kwargs, context: self, &)
end

#cache(*args, **kwargs) ⇒ Object

Creates a provider-side prompt cache using this context's configuration. Accepts the same arguments as RubyLLM.cache.



147
148
149
# File 'lib/ruby_llm/context.rb', line 147

def cache(*args, **kwargs, &)
  CachedContent.create(*args, **kwargs, context: self, &)
end

#chat(*args, **kwargs) ⇒ Object

Creates a new Chat that uses this context's configuration. Accepts the same arguments as RubyLLM.chat.



38
39
40
# File 'lib/ruby_llm/context.rb', line 38

def chat(*args, **kwargs, &)
  Chat.new(*args, **kwargs, context: self, &)
end

#count_tokens(text, model: nil, provider: nil) ⇒ Object

Counts tokens using this context's configuration. Accepts the same arguments as RubyLLM.count_tokens.



44
45
46
# File 'lib/ruby_llm/context.rb', line 44

def count_tokens(text, model: nil, provider: nil)
  chat(model:, provider:).count_tokens(text)
end

#download(*args, **kwargs) ⇒ Object

Downloads a provider-hosted file using this context's configuration. Accepts the same arguments as RubyLLM.download.



141
142
143
# File 'lib/ruby_llm/context.rb', line 141

def download(*args, **kwargs, &)
  UploadedFile.download(*args, **kwargs, context: self, &)
end

#embed(*args, **kwargs) ⇒ Object

Generates embeddings using this context's configuration. Accepts the same arguments as RubyLLM.embed.



62
63
64
# File 'lib/ruby_llm/context.rb', line 62

def embed(*args, **kwargs, &)
  Embedding.embed(*args, **kwargs, context: self, &)
end

#embed_later(text, model: nil, provider: nil, dimensions: nil) ⇒ Object

Stages an embedding request using this context's configuration. Accepts the same arguments as RubyLLM.embed_later.



68
69
70
# File 'lib/ruby_llm/context.rb', line 68

def embed_later(text, model: nil, provider: nil, dimensions: nil)
  EmbeddingRequest.new(text, model:, provider:, dimensions:, context: self)
end

#moderate(*args, **kwargs) ⇒ Object

Runs content moderation using this context's configuration. Accepts the same arguments as RubyLLM.moderate.



92
93
94
# File 'lib/ruby_llm/context.rb', line 92

def moderate(*args, **kwargs, &)
  Moderation.moderate(*args, **kwargs, context: self, &)
end

#ocr(*args, **kwargs) ⇒ Object

Extracts document text using this context's configuration. Accepts the same arguments as RubyLLM.ocr.



123
124
125
# File 'lib/ruby_llm/context.rb', line 123

def ocr(*args, **kwargs, &)
  OCR.ocr(*args, **kwargs, context: self, &)
end

#paint(*args, **kwargs) ⇒ Object

Generates an image using this context's configuration. Accepts the same arguments as RubyLLM.paint.



74
75
76
# File 'lib/ruby_llm/context.rb', line 74

def paint(*args, **kwargs, &)
  Image.paint(*args, **kwargs, context: self, &)
end

#rerank(*args, **kwargs) ⇒ Object

Ranks documents using this context's configuration. Accepts the same arguments as RubyLLM.rerank.



129
130
131
# File 'lib/ruby_llm/context.rb', line 129

def rerank(*args, **kwargs, &)
  Rerank.rerank(*args, **kwargs, context: self, &)
end

#research(*args, **kwargs) ⇒ Object

Runs hosted research using this context's configuration. Accepts the same arguments as RubyLLM.research.



98
99
100
# File 'lib/ruby_llm/context.rb', line 98

def research(*args, **kwargs)
  ResearchJob.research(*args, **kwargs, context: self)
end

#research_later(*args, **kwargs) ⇒ Object

Submits hosted research using this context's configuration. Accepts the same arguments as RubyLLM.research_later.



104
105
106
# File 'lib/ruby_llm/context.rb', line 104

def research_later(*args, **kwargs)
  ResearchJob.research_later(*args, **kwargs, context: self)
end

#speak(*args, **kwargs) ⇒ Object

Generates speech audio using this context's configuration. Given a block, yields SpeechChunk objects and returns the complete Speech. Accepts the same arguments as RubyLLM.speak.



111
112
113
# File 'lib/ruby_llm/context.rb', line 111

def speak(*args, **kwargs, &)
  Speech.speak(*args, **kwargs, context: self, &)
end

#tokenize(*args, **kwargs) ⇒ Object

Tokenizes plain text using this context's configuration. Accepts the same arguments as RubyLLM.tokenize.



50
51
52
# File 'lib/ruby_llm/context.rb', line 50

def tokenize(*args, **kwargs)
  Tokenization.tokenize(*args, **kwargs, context: self)
end

#transcribe(*args, **kwargs) ⇒ Object

Transcribes audio using this context's configuration. Accepts the same arguments as RubyLLM.transcribe.



117
118
119
# File 'lib/ruby_llm/context.rb', line 117

def transcribe(*args, **kwargs, &)
  Transcription.transcribe(*args, **kwargs, context: self, &)
end

#upload(*args, **kwargs) ⇒ Object

Uploads a file to a provider using this context's configuration. Accepts the same arguments as RubyLLM.upload.



135
136
137
# File 'lib/ruby_llm/context.rb', line 135

def upload(*args, **kwargs, &)
  UploadedFile.upload(*args, **kwargs, context: self, &)
end

#workflow(name, id: nil, metadata: nil) ⇒ Object

Runs a named workflow using this context's instrumenter. Accepts the same arguments as RubyLLM.workflow.



56
57
58
# File 'lib/ruby_llm/context.rb', line 56

def workflow(name, id: nil, metadata: nil, &)
  Workflow.new(name, id:, metadata:, config: config).run(&)
end