llm.rb 16.0.0.pre.2 → 16.0.0.pre.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/llm/stream.rb CHANGED
@@ -37,10 +37,12 @@ module LLM
37
37
  #
38
38
  # @see LLM::Agent Where streams are typically attached
39
39
  # @see LLM::Context Where streams are bound per-turn
40
+ # @see LLM::Step An extension that saves the conversation at each step
40
41
  class Stream
41
42
  require_relative "stream/queue"
42
43
  require_relative "stream/io"
43
44
  require_relative "stream/disabled"
45
+ require_relative "step"
44
46
 
45
47
  ##
46
48
  # This method will try to convert its argument into
@@ -51,18 +53,22 @@ module LLM
51
53
  # objects, IO objects who implement `#<<`, true, false,
52
54
  # and nil. Anything else raises a TypeError.
53
55
  #
56
+ # The stream it answers with is ready to be told about a step:
57
+ # {LLM::Step LLM::Step} is prepended onto it here, once, rather than by
58
+ # every caller that means to save a conversation.
59
+ #
54
60
  # @raise [TypeError]
55
61
  # @param [LLM::Stream, #<<, Boolean, NilClass] obj
56
62
  # @return [LLM::Stream]
57
63
  def self.try(obj, extra: {})
58
64
  if LLM::Stream === obj
59
- obj.tap { _1.extra.merge!(extra) }
65
+ obj.tap { _1.extra.merge!(extra) }.tap { _1.singleton_class.prepend(LLM::Step) }
60
66
  elsif obj.respond_to?(:<<)
61
- LLM::Stream::IO.new(obj).tap { _1.extra.merge!(extra) }
67
+ LLM::Stream::IO.new(obj).tap { _1.extra.merge!(extra) }.tap { _1.singleton_class.prepend(LLM::Step) }
62
68
  elsif obj == true
63
- LLM::Stream.new.tap { _1.extra.merge!(extra) }
69
+ LLM::Stream.new.tap { _1.extra.merge!(extra) }.tap { _1.singleton_class.prepend(LLM::Step) }
64
70
  elsif obj.nil? || obj == false
65
- LLM::Stream::Disabled.new.tap { _1.extra.merge!(extra) }
71
+ LLM::Stream::Disabled.new.tap { _1.extra.merge!(extra) }.tap { _1.singleton_class.prepend(LLM::Step) }
66
72
  else
67
73
  raise TypeError, "invalid stream object"
68
74
  end
@@ -152,6 +158,32 @@ module LLM
152
158
  nil
153
159
  end
154
160
 
161
+ ##
162
+ # Called when a request in a turn has completed.
163
+ #
164
+ # This is the boundary between one request and the next: the response
165
+ # is in the context's message history, and a turn that asked for tools
166
+ # runs them after this while a turn that is finished ends here.
167
+ #
168
+ # It is emitted once per successful request, so a request that is
169
+ # retried after a rate limit or a timeout calls this when it lands and
170
+ # not once per attempt. It is also emitted when the stream is disabled:
171
+ # a disabled stream has no output to report, but it is still told that
172
+ # a step ended.
173
+ #
174
+ # The context is passed with the response so that a stream does not have
175
+ # to reach for it: {LLM::Step LLM::Step}, which saves the conversation the
176
+ # context holds, is written against this callback alone.
177
+ #
178
+ # @param [LLM::Context] ctx
179
+ # The context the request belongs to
180
+ # @param [LLM::Response] res
181
+ # The response for the request that completed
182
+ # @return [nil]
183
+ def on_step(ctx, res)
184
+ nil
185
+ end
186
+
155
187
  ##
156
188
  # Called before a context transformer rewrites a prompt.
157
189
  # @param [LLM::Transformer] transformer
data/lib/llm/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LLM
4
- VERSION = "16.0.0.pre.2"
4
+ VERSION = "16.0.0.pre.4"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: llm.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 16.0.0.pre.2
4
+ version: 16.0.0.pre.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Gleeson
@@ -400,6 +400,9 @@ files:
400
400
  - docs/deepdive/features/console.md
401
401
  - docs/deepdive/features/database.md
402
402
  - docs/deepdive/features/embeddings.md
403
+ - docs/deepdive/features/files.md
404
+ - docs/deepdive/features/moderations.md
405
+ - docs/deepdive/features/responses.md
403
406
  - docs/deepdive/fundamentals/agents.md
404
407
  - docs/deepdive/fundamentals/providers.md
405
408
  - docs/deepdive/fundamentals/schema.md
@@ -414,6 +417,7 @@ files:
414
417
  - docs/deepdive/reference/cost.md
415
418
  - docs/deepdive/reference/model_registry.md
416
419
  - docs/deepdive/reference/object.md
420
+ - docs/deepdive/reference/response.md
417
421
  - docs/deepdive/reference/tracer.md
418
422
  - lib/llm.rb
419
423
  - lib/llm/a2a.rb
@@ -642,6 +646,7 @@ files:
642
646
  - lib/llm/server_tool.rb
643
647
  - lib/llm/session.rb
644
648
  - lib/llm/skill.rb
649
+ - lib/llm/step.rb
645
650
  - lib/llm/stream.rb
646
651
  - lib/llm/stream/disabled.rb
647
652
  - lib/llm/stream/io.rb