-
Notifications
You must be signed in to change notification settings - Fork 4
Add LLM::Agent #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+317
−17
Merged
Add LLM::Agent #113
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module LLM | ||
| ## | ||
| # {LLM::Agent LLM::Agent} provides a class-level DSL for defining | ||
| # reusable, preconfigured assistants with defaults for model, | ||
| # tools, schema, and instructions. | ||
| # | ||
| # @note | ||
| # Unlike {LLM::Bot LLM::Bot}, this class will automatically run | ||
| # tool calls for you. | ||
| # | ||
| # @note | ||
| # Instructions are injected only on the first request. | ||
| # | ||
| # @note | ||
| # This idea originally came from RubyLLM and was adapted to llm.rb. | ||
| # | ||
| # @example | ||
| # class SystemAdmin < LLM::Agent | ||
| # model "gpt-4.1-nano" | ||
| # instructions "You are a Linux system admin" | ||
| # tools Shell | ||
| # schema Result | ||
| # end | ||
| # | ||
| # llm = LLM.openai(key: ENV["KEY"]) | ||
| # agent = SystemAdmin.new(llm) | ||
| # agent.chat("Run 'date'") | ||
| class Agent | ||
| ## | ||
| # Set or get the default model | ||
| # @param [String, nil] model | ||
| # The model identifier | ||
| # @return [String, nil] | ||
| # Returns the current model when no argument is provided | ||
| def self.model(model = nil) | ||
| return @model if model.nil? | ||
| @model = model | ||
| end | ||
|
|
||
| ## | ||
| # Set or get the default tools | ||
| # @param [Array<LLM::Function>, nil] tools | ||
| # One or more tools | ||
| # @return [Array<LLM::Function>] | ||
| # Returns the current tools when no argument is provided | ||
| def self.tools(*tools) | ||
| return @tools || [] if tools.empty? | ||
| @tools = tools.flatten | ||
| end | ||
|
|
||
| ## | ||
| # Set or get the default schema | ||
| # @param [#to_json, nil] schema | ||
| # The schema | ||
| # @return [#to_json, nil] | ||
| # Returns the current schema when no argument is provided | ||
| def self.schema(schema = nil) | ||
| return @schema if schema.nil? | ||
| @schema = schema | ||
| end | ||
|
|
||
| ## | ||
| # Set or get the default instructions | ||
| # @param [String, nil] instructions | ||
| # The system instructions | ||
| # @return [String, nil] | ||
| # Returns the current instructions when no argument is provided | ||
| def self.instructions(instructions = nil) | ||
| return @instructions if instructions.nil? | ||
| @instructions = instructions | ||
| end | ||
|
|
||
| ## | ||
| # @param [LLM::Provider] provider | ||
| # A provider | ||
| # @param [Hash] params | ||
| # The parameters to maintain throughout the conversation. | ||
| # Any parameter the provider supports can be included and | ||
| # not only those listed here. | ||
| # @option params [String] :model Defaults to the provider's default model | ||
| # @option params [Array<LLM::Function>, nil] :tools Defaults to nil | ||
| # @option params [#to_json, nil] :schema Defaults to nil | ||
| def initialize(provider, params = {}) | ||
| defaults = {model: self.class.model, tools: self.class.tools, schema: self.class.schema}.compact | ||
| @provider = provider | ||
| @bot = LLM::Bot.new(provider, defaults.merge(params)) | ||
| @instructions_applied = false | ||
| end | ||
|
|
||
| ## | ||
| # Maintain a conversation via the chat completions API. | ||
| # This method immediately sends a request to the LLM and returns the response. | ||
| # | ||
| # @param prompt (see LLM::Provider#complete) | ||
| # @param [Hash] params The params passed to the provider, including optional :stream, :tools, :schema etc. | ||
| # @option params [Integer] :max_tool_rounds The maxinum number of tool call iterations (default 10) | ||
| # @return [LLM::Response] Returns the LLM's response for this turn. | ||
| # @example | ||
| # llm = LLM.openai(key: ENV["KEY"]) | ||
| # agent = LLM::Agent.new(llm) | ||
| # response = agent.chat("Hello, what is your name?") | ||
| # puts response.choices[0].content | ||
| def chat(prompt, params = {}) | ||
| i, max = 0, Integer(params.delete(:max_tool_rounds) || 10) | ||
| res = @bot.chat(apply_instructions(prompt), params) | ||
| until @bot.functions.empty? | ||
| raise LLM::ToolLoopError, "pending tool calls remain" if i >= max | ||
| res = @bot.chat @bot.functions.map(&:call), params | ||
| i += 1 | ||
| end | ||
| @instructions_applied = true | ||
| res | ||
| end | ||
|
|
||
| ## | ||
| # Maintain a conversation via the responses API. | ||
| # This method immediately sends a request to the LLM and returns the response. | ||
| # | ||
| # @note Not all LLM providers support this API | ||
| # @param prompt (see LLM::Provider#complete) | ||
| # @param [Hash] params The params passed to the provider, including optional :stream, :tools, :schema etc. | ||
| # @option params [Integer] :max_tool_rounds The maxinum number of tool call iterations (default 10) | ||
| # @return [LLM::Response] Returns the LLM's response for this turn. | ||
| # @example | ||
| # llm = LLM.openai(key: ENV["KEY"]) | ||
| # agent = LLM::Agent.new(llm) | ||
| # res = agent.respond("What is the capital of France?") | ||
| # puts res.output_text | ||
| def respond(prompt, params = {}) | ||
| i, max = 0, Integer(params.delete(:max_tool_rounds) || 10) | ||
| res = @bot.respond(apply_instructions(prompt), params) | ||
| until @bot.functions.empty? | ||
| raise LLM::ToolLoopError, "pending tool calls remain" if i >= max | ||
| res = @bot.respond @bot.functions.map(&:call), params | ||
| i += 1 | ||
| end | ||
| @instructions_applied = true | ||
| res | ||
| end | ||
|
|
||
| ## | ||
| # @return [LLM::Buffer<LLM::Message>] | ||
| def messages | ||
| @bot.messages | ||
| end | ||
|
|
||
| ## | ||
| # @return [Array<LLM::Function>] | ||
| def functions | ||
| @bot.functions | ||
| end | ||
|
|
||
| ## | ||
| # @return [LLM::Object] | ||
| def usage | ||
| @bot.usage | ||
| end | ||
|
|
||
| ## | ||
| # @return [LLM::Builder] | ||
| def build_prompt(&) | ||
| @bot.build_prompt(&) | ||
| end | ||
|
|
||
| ## | ||
| # @param [String] url | ||
| # The URL | ||
| # @return [LLM::Object] | ||
| # Returns a tagged object | ||
| def image_url(url) | ||
| @bot.image_url(url) | ||
| end | ||
|
|
||
| ## | ||
| # @param [String] path | ||
| # The path | ||
| # @return [LLM::Object] | ||
| # Returns a tagged object | ||
| def local_file(path) | ||
| @bot.local_file(path) | ||
| end | ||
|
|
||
| ## | ||
| # @param [LLM::Response] res | ||
| # The response | ||
| # @return [LLM::Object] | ||
| # Returns a tagged object | ||
| def remote_file(res) | ||
| @bot.remote_file(res) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def apply_instructions(prompt) | ||
| instr = self.class.instructions | ||
| return prompt unless instr | ||
| if LLM::Builder === prompt | ||
| messages = prompt.to_a | ||
| builder = LLM::Builder.new(@provider) do |builder| | ||
| builder.system instr unless @instructions_applied | ||
| messages.each { |msg| builder.chat(msg.content, role: msg.role) } | ||
| end | ||
| builder.tap(&:call) | ||
| else | ||
| build_prompt do | ||
| _1.system instr unless @instructions_applied | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't work with Gemini.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed by 3291f3a |
||
| _1.user prompt | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't work with Gemini.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed by 3291f3a