llm-shell 0.9.2 → 0.10.0
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.
- checksums.yaml +4 -4
- data/README.md +61 -66
- data/lib/llm/shell/command.rb +40 -40
- data/lib/llm/shell/commands/clear_screen.rb +4 -18
- data/lib/llm/shell/commands/debug_mode.rb +12 -0
- data/lib/llm/shell/commands/dir_import.rb +4 -20
- data/lib/llm/shell/commands/disable_tool.rb +33 -0
- data/lib/llm/shell/commands/enable_tool.rb +33 -0
- data/lib/llm/shell/commands/file_import.rb +4 -20
- data/lib/llm/shell/commands/help.rb +23 -36
- data/lib/llm/shell/commands/show_chat.rb +4 -19
- data/lib/llm/shell/commands/show_version.rb +4 -20
- data/lib/llm/shell/commands/system_prompt.rb +4 -18
- data/lib/llm/shell/completion.rb +5 -5
- data/lib/llm/shell/config.rb +4 -5
- data/lib/llm/shell/formatter.rb +1 -2
- data/lib/llm/shell/internal/coderay/lib/coderay/duo.rb +81 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/_map.rb +17 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/comment_filter.rb +25 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/count.rb +39 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/debug.rb +49 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/debug_lint.rb +63 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/div.rb +23 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/encoder.rb +190 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/filter.rb +58 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html/css.rb +65 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html/numbering.rb +108 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html/output.rb +164 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html.rb +333 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/json.rb +83 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/lines_of_code.rb +45 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/lint.rb +59 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/null.rb +18 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/page.rb +24 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/span.rb +23 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/statistic.rb +95 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/terminal.rb +195 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/text.rb +46 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/token_kind_filter.rb +111 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/xml.rb +72 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/yaml.rb +50 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders.rb +18 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/for_redcloth.rb +95 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/file_type.rb +151 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/plugin.rb +55 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/plugin_host.rb +221 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/word_list.rb +72 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/_map.rb +24 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/c.rb +189 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/clojure.rb +217 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/cpp.rb +217 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/css.rb +196 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/debug.rb +75 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/delphi.rb +144 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/diff.rb +221 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/erb.rb +81 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/go.rb +208 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/groovy.rb +268 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/haml.rb +168 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/html.rb +275 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/java/builtin_types.rb +421 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/java.rb +174 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/java_script.rb +236 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/json.rb +98 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/lua.rb +280 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/php.rb +527 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/python.rb +287 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/raydebug.rb +75 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/ruby/patterns.rb +178 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/ruby/string_state.rb +79 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/ruby.rb +477 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/sass.rb +232 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/scanner.rb +337 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/sql.rb +169 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/taskpaper.rb +36 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/text.rb +26 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/xml.rb +17 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/yaml.rb +140 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners.rb +27 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles/_map.rb +7 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles/alpha.rb +153 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles/style.rb +18 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles.rb +15 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/token_kinds.rb +85 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/tokens.rb +164 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/tokens_proxy.rb +55 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/version.rb +3 -0
- data/lib/llm/shell/internal/coderay/lib/coderay.rb +284 -0
- data/lib/llm/shell/internal/io-line/lib/io/line/multiple.rb +19 -0
- data/lib/{io → llm/shell/internal/io-line/lib/io}/line.rb +2 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/builder.rb +31 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/conversable.rb +37 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/prompt/completion.rb +49 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/prompt/respond.rb +49 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot.rb +150 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/buffer.rb +162 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/client.rb +36 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/error.rb +49 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventhandler.rb +44 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventstream/event.rb +69 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventstream/parser.rb +88 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventstream.rb +8 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/file.rb +91 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/function.rb +177 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/message.rb +178 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/mime.rb +140 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/multipart.rb +101 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/object/builder.rb +38 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/object/kernel.rb +53 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/object.rb +89 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/provider.rb +352 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/error_handler.rb +36 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/files.rb +155 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/format/completion_format.rb +88 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/format.rb +29 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/models.rb +54 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/completion.rb +39 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/enumerable.rb +11 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/file.rb +23 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/web_search.rb +21 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/stream_parser.rb +66 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic.rb +138 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/deepseek/format/completion_format.rb +68 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/deepseek/format.rb +27 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/deepseek.rb +75 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/audio.rb +73 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/error_handler.rb +47 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/files.rb +146 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/format/completion_format.rb +69 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/format.rb +39 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/images.rb +133 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/models.rb +60 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/completion.rb +35 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/embedding.rb +8 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/file.rb +11 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/files.rb +15 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/image.rb +31 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/models.rb +15 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/web_search.rb +22 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/stream_parser.rb +86 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini.rb +173 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/llamacpp.rb +74 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/error_handler.rb +36 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/format/completion_format.rb +77 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/format.rb +29 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/models.rb +56 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/response/completion.rb +28 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/response/embedding.rb +9 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/stream_parser.rb +44 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama.rb +116 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/audio.rb +91 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/error_handler.rb +46 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/files.rb +134 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format/completion_format.rb +90 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format/moderation_format.rb +35 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format/respond_format.rb +72 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format.rb +54 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/images.rb +109 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/models.rb +55 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/moderations.rb +65 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/audio.rb +7 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/completion.rb +40 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/embedding.rb +9 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/enumerable.rb +23 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/file.rb +7 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/image.rb +16 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/moderations.rb +34 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/responds.rb +48 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/web_search.rb +21 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/responses/stream_parser.rb +76 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/responses.rb +99 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/stream_parser.rb +86 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/vector_stores.rb +228 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai.rb +206 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/xai/images.rb +58 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/xai.rb +72 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/zai.rb +74 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/response.rb +67 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/array.rb +26 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/boolean.rb +13 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/integer.rb +43 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/leaf.rb +78 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/null.rb +13 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/number.rb +43 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/object.rb +41 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/string.rb +34 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/version.rb +8 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema.rb +81 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/server_tool.rb +32 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/tool/param.rb +75 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/tool.rb +78 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/utils.rb +19 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/version.rb +5 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm.rb +121 -0
- data/lib/llm/shell/internal/optparse/lib/optionparser.rb +2 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/ac.rb +70 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/date.rb +18 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/kwargs.rb +27 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/shellwords.rb +7 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/time.rb +11 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/uri.rb +7 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/version.rb +80 -0
- data/lib/llm/shell/internal/optparse/lib/optparse.rb +2469 -0
- data/lib/llm/shell/internal/paint/lib/paint/constants.rb +104 -0
- data/lib/llm/shell/internal/paint/lib/paint/pa.rb +13 -0
- data/lib/llm/shell/internal/paint/lib/paint/rgb_colors.rb +14 -0
- data/lib/llm/shell/internal/paint/lib/paint/shortcuts.rb +100 -0
- data/lib/llm/shell/internal/paint/lib/paint/shortcuts_version.rb +5 -0
- data/lib/llm/shell/internal/paint/lib/paint/util.rb +16 -0
- data/lib/llm/shell/internal/paint/lib/paint/version.rb +5 -0
- data/lib/llm/shell/internal/paint/lib/paint.rb +261 -0
- data/lib/llm/shell/internal/reline/lib/reline/config.rb +378 -0
- data/lib/llm/shell/internal/reline/lib/reline/face.rb +199 -0
- data/lib/llm/shell/internal/reline/lib/reline/history.rb +76 -0
- data/lib/llm/shell/internal/reline/lib/reline/io/ansi.rb +322 -0
- data/lib/llm/shell/internal/reline/lib/reline/io/dumb.rb +120 -0
- data/lib/llm/shell/internal/reline/lib/reline/io/windows.rb +530 -0
- data/lib/llm/shell/internal/reline/lib/reline/io.rb +55 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/base.rb +37 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/composite.rb +17 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/emacs.rb +517 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/vi_command.rb +518 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/vi_insert.rb +517 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor.rb +8 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_stroke.rb +119 -0
- data/lib/llm/shell/internal/reline/lib/reline/kill_ring.rb +125 -0
- data/lib/llm/shell/internal/reline/lib/reline/line_editor.rb +2356 -0
- data/lib/llm/shell/internal/reline/lib/reline/unicode/east_asian_width.rb +1292 -0
- data/lib/llm/shell/internal/reline/lib/reline/unicode.rb +421 -0
- data/lib/llm/shell/internal/reline/lib/reline/version.rb +3 -0
- data/lib/llm/shell/internal/reline/lib/reline.rb +527 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/generated_parser.rb +712 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/handler.rb +268 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/local_date.rb +35 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/local_date_time.rb +42 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/local_time.rb +40 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/parser.rb +21 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/scanner.rb +92 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/string_utils.rb +40 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/version.rb +5 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb.rb +49 -0
- data/lib/llm/shell/options.rb +1 -1
- data/lib/llm/shell/renderer.rb +2 -3
- data/lib/llm/shell/repl.rb +21 -16
- data/lib/llm/shell/tool.rb +42 -0
- data/lib/llm/shell/tools/read_file.rb +15 -0
- data/lib/llm/shell/tools/system.rb +17 -0
- data/lib/llm/shell/tools/write_file.rb +16 -0
- data/lib/llm/shell/version.rb +1 -1
- data/lib/llm/shell.rb +83 -39
- data/libexec/llm-shell/shell +4 -6
- data/llm-shell.gemspec +0 -4
- metadata +233 -63
- data/lib/llm/function.rb +0 -17
- data/lib/llm/shell/command/extension.rb +0 -42
- data/lib/llm/shell/commands/utils.rb +0 -21
- data/lib/llm/shell/functions/read_file.rb +0 -22
- data/lib/llm/shell/functions/write_file.rb +0 -22
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "openai" unless defined?(LLM::OpenAI)
|
|
4
|
+
|
|
5
|
+
module LLM
|
|
6
|
+
##
|
|
7
|
+
# The ZAI class implements a provider for [zAI](https://docs.z.ai/guides/overview/quick-start).
|
|
8
|
+
#
|
|
9
|
+
# @example
|
|
10
|
+
# #!/usr/bin/env ruby
|
|
11
|
+
# require "llm"
|
|
12
|
+
#
|
|
13
|
+
# llm = LLM.zai(key: ENV["KEY"])
|
|
14
|
+
# bot = LLM::Bot.new(llm, stream: $stdout)
|
|
15
|
+
# bot.chat("Greetings Robot", role: :user).flush
|
|
16
|
+
class ZAI < OpenAI
|
|
17
|
+
##
|
|
18
|
+
# @param [String] host A regional host or the default ("api.z.ai")
|
|
19
|
+
# @param key (see LLM::Provider#initialize)
|
|
20
|
+
def initialize(host: "api.z.ai", **)
|
|
21
|
+
super
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
##
|
|
25
|
+
# @raise [NotImplementedError]
|
|
26
|
+
def files
|
|
27
|
+
raise NotImplementedError
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
##
|
|
31
|
+
# @return [LLM::XAI::Images]
|
|
32
|
+
def images
|
|
33
|
+
raise NotImplementedError
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
##
|
|
37
|
+
# @raise [NotImplementedError]
|
|
38
|
+
def audio
|
|
39
|
+
raise NotImplementedError
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
##
|
|
43
|
+
# @raise [NotImplementedError]
|
|
44
|
+
def moderations
|
|
45
|
+
raise NotImplementedError
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
##
|
|
49
|
+
# @raise [NotImplementedError]
|
|
50
|
+
def responses
|
|
51
|
+
raise NotImplementedError
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
##
|
|
55
|
+
# @raise [NotImplementedError]
|
|
56
|
+
def vector_stores
|
|
57
|
+
raise NotImplementedError
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
##
|
|
61
|
+
# Returns the default model for chat completions
|
|
62
|
+
# #see https://docs.z.ai/guides/llm/glm-4.5#glm-4-5-flash glm-4.5-flash
|
|
63
|
+
# @return [String]
|
|
64
|
+
def default_model
|
|
65
|
+
"glm-4.5-flash"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
def completions_path
|
|
71
|
+
"/api/paas/v4/chat/completions"
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LLM
|
|
4
|
+
##
|
|
5
|
+
# {LLM::Response LLM::Response} encapsulates a response
|
|
6
|
+
# from an LLM provider. It is returned by all methods
|
|
7
|
+
# that make requests to a provider, and sometimes extended
|
|
8
|
+
# with provider-specific functionality.
|
|
9
|
+
class Response
|
|
10
|
+
require "json"
|
|
11
|
+
|
|
12
|
+
##
|
|
13
|
+
# Returns the HTTP response
|
|
14
|
+
# @return [Net::HTTPResponse]
|
|
15
|
+
attr_reader :res
|
|
16
|
+
|
|
17
|
+
##
|
|
18
|
+
# @param [Net::HTTPResponse] res
|
|
19
|
+
# HTTP response
|
|
20
|
+
# @return [LLM::Response]
|
|
21
|
+
# Returns an instance of LLM::Response
|
|
22
|
+
def initialize(res)
|
|
23
|
+
@res = res
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
##
|
|
27
|
+
# Returns the response body
|
|
28
|
+
# @return [Hash, String]
|
|
29
|
+
def body
|
|
30
|
+
@body ||= case @res["content-type"]
|
|
31
|
+
when %r|\Aapplication/json\s*| then LLM::Object.from_hash(JSON.parse(@res.body))
|
|
32
|
+
else @res.body
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
##
|
|
37
|
+
# Returns an inspection of the response object
|
|
38
|
+
# @return [String]
|
|
39
|
+
def inspect
|
|
40
|
+
"#<#{self.class.name}:0x#{object_id.to_s(16)} @body=#{body.inspect} @res=#{@res.inspect}>"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
##
|
|
44
|
+
# Returns true if the response is successful
|
|
45
|
+
# @return [Boolean]
|
|
46
|
+
def ok?
|
|
47
|
+
Net::HTTPSuccess === @res
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
##
|
|
51
|
+
# Returns true if the response is from the Files API
|
|
52
|
+
# @return [Boolean]
|
|
53
|
+
def file?
|
|
54
|
+
false
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def method_missing(m, *args, **kwargs, &b)
|
|
60
|
+
body.respond_to?(m) ? body[m.to_s] : super
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def respond_to_missing?(m, include_private = false)
|
|
64
|
+
body.respond_to?(m) || super
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class LLM::Schema
|
|
4
|
+
##
|
|
5
|
+
# The {LLM::Schema::Array LLM::Schema::Array} class represents an
|
|
6
|
+
# array value in a JSON schema. It is a subclass of
|
|
7
|
+
# {LLM::Schema::Leaf LLM::Schema::Leaf} and provides methods that
|
|
8
|
+
# can act as constraints.
|
|
9
|
+
class Array < Leaf
|
|
10
|
+
def initialize(items)
|
|
11
|
+
@items = items
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def to_h
|
|
15
|
+
super.merge!({type: "array", items:})
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def to_json(options = {})
|
|
19
|
+
to_h.to_json(options)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
attr_reader :items
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class LLM::Schema
|
|
4
|
+
##
|
|
5
|
+
# The {LLM::Schema::Boolean LLM::Schema::Boolean} class represents a
|
|
6
|
+
# boolean value in a JSON schema. It is a subclass of
|
|
7
|
+
# {LLM::Schema::Leaf LLM::Schema::Leaf}.
|
|
8
|
+
class Boolean < Leaf
|
|
9
|
+
def to_h
|
|
10
|
+
super.merge!({type: "boolean"})
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class LLM::Schema
|
|
4
|
+
##
|
|
5
|
+
# The {LLM::Schema::Integer LLM::Schema::Integer} class represents a
|
|
6
|
+
# whole number value in a JSON schema. It is a subclass of
|
|
7
|
+
# {LLM::Schema::Leaf LLM::Schema::Leaf} and provides methods that
|
|
8
|
+
# can act as constraints.
|
|
9
|
+
class Integer < Leaf
|
|
10
|
+
##
|
|
11
|
+
# Constrain the number to a minimum value
|
|
12
|
+
# @param [Integer] i The minimum value
|
|
13
|
+
# @return [LLM::Schema::Number] Returns self
|
|
14
|
+
def min(i)
|
|
15
|
+
tap { @minimum = i }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
##
|
|
19
|
+
# Constrain the number to a maximum value
|
|
20
|
+
# @param [Integer] i The maximum value
|
|
21
|
+
# @return [LLM::Schema::Number] Returns self
|
|
22
|
+
def max(i)
|
|
23
|
+
tap { @maximum = i }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
##
|
|
27
|
+
# Constrain the number to a multiple of a given value
|
|
28
|
+
# @param [Integer] i The multiple
|
|
29
|
+
# @return [LLM::Schema::Number] Returns self
|
|
30
|
+
def multiple_of(i)
|
|
31
|
+
tap { @multiple_of = i }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def to_h
|
|
35
|
+
super.merge!({
|
|
36
|
+
type: "integer",
|
|
37
|
+
minimum: @minimum,
|
|
38
|
+
maximum: @maximum,
|
|
39
|
+
multipleOf: @multiple_of
|
|
40
|
+
}).compact
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class LLM::Schema
|
|
4
|
+
##
|
|
5
|
+
# The {LLM::Schema::Leaf LLM::Schema::Leaf} class is the
|
|
6
|
+
# superclass of all values that can appear in a JSON schema.
|
|
7
|
+
# See the instance methods of {LLM::Schema LLM::Schema} for
|
|
8
|
+
# an example of how to create instances of {LLM::Schema::Leaf LLM::Schema::Leaf}
|
|
9
|
+
# through its subclasses.
|
|
10
|
+
class Leaf
|
|
11
|
+
def initialize
|
|
12
|
+
@description = nil
|
|
13
|
+
@default = nil
|
|
14
|
+
@enum = nil
|
|
15
|
+
@required = nil
|
|
16
|
+
@const = nil
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
##
|
|
20
|
+
# Set the description of a leaf
|
|
21
|
+
# @param [String] str The description
|
|
22
|
+
# @return [LLM::Schema::Leaf]
|
|
23
|
+
def description(str)
|
|
24
|
+
tap { @description = str }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
##
|
|
28
|
+
# Set the default value of a leaf
|
|
29
|
+
# @param [Object] value The default value
|
|
30
|
+
# @return [LLM::Schema::Leaf]
|
|
31
|
+
def default(value)
|
|
32
|
+
tap { @default = value }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
##
|
|
36
|
+
# Set the allowed values of a leaf
|
|
37
|
+
# @see https://tour.json-schema.org/content/02-Primitive-Types/07-Enumerated-Values-II Enumerated Values
|
|
38
|
+
# @param [Array] values The allowed values
|
|
39
|
+
# @return [LLM::Schema::Leaf]
|
|
40
|
+
def enum(*values)
|
|
41
|
+
tap { @enum = values }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
##
|
|
45
|
+
# Set the value of a leaf to be a constant value
|
|
46
|
+
# @see https://tour.json-schema.org/content/02-Primitive-Types/08-Defining-Constant-Values Constant Values
|
|
47
|
+
# @param [Object] value The constant value
|
|
48
|
+
# @return [LLM::Schema::Leaf]
|
|
49
|
+
def const(value)
|
|
50
|
+
tap { @const = value }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
##
|
|
54
|
+
# Denote a leaf as required
|
|
55
|
+
# @return [LLM::Schema::Leaf]
|
|
56
|
+
def required
|
|
57
|
+
tap { @required = true }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
##
|
|
61
|
+
# @return [Hash]
|
|
62
|
+
def to_h
|
|
63
|
+
{description: @description, default: @default, enum: @enum}.compact
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
##
|
|
67
|
+
# @return [String]
|
|
68
|
+
def to_json(options = {})
|
|
69
|
+
to_h.to_json(options)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
##
|
|
73
|
+
# @return [Boolean]
|
|
74
|
+
def required?
|
|
75
|
+
@required
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class LLM::Schema
|
|
4
|
+
##
|
|
5
|
+
# The {LLM::Schema::Null LLM::Schema::Null} class represents a
|
|
6
|
+
# null value in a JSON schema. It is a subclass of
|
|
7
|
+
# {LLM::Schema::Leaf LLM::Schema::Leaf}.
|
|
8
|
+
class Null < Leaf
|
|
9
|
+
def to_h
|
|
10
|
+
super.merge!({type: "null"})
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class LLM::Schema
|
|
4
|
+
##
|
|
5
|
+
# The {LLM::Schema::Number LLM::Schema::Number} class represents a
|
|
6
|
+
# a number (either whole or decimal) value in a JSON schema. It is a
|
|
7
|
+
# subclass of {LLM::Schema::Leaf LLM::Schema::Leaf} and provides
|
|
8
|
+
# methods that can act as constraints.
|
|
9
|
+
class Number < Leaf
|
|
10
|
+
##
|
|
11
|
+
# Constrain the number to a minimum value
|
|
12
|
+
# @param [Integer, Float] i The minimum value
|
|
13
|
+
# @return [LLM::Schema::Number] Returns self
|
|
14
|
+
def min(i)
|
|
15
|
+
tap { @minimum = i }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
##
|
|
19
|
+
# Constrain the number to a maximum value
|
|
20
|
+
# @param [Integer, Float] i The maximum value
|
|
21
|
+
# @return [LLM::Schema::Number] Returns self
|
|
22
|
+
def max(i)
|
|
23
|
+
tap { @maximum = i }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
##
|
|
27
|
+
# Constrain the number to a multiple of a given value
|
|
28
|
+
# @param [Integer, Float] i The multiple
|
|
29
|
+
# @return [LLM::Schema::Number] Returns self
|
|
30
|
+
def multiple_of(i)
|
|
31
|
+
tap { @multiple_of = i }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def to_h
|
|
35
|
+
super.merge!({
|
|
36
|
+
type: "number",
|
|
37
|
+
minimum: @minimum,
|
|
38
|
+
maximum: @maximum,
|
|
39
|
+
multipleOf: @multiple_of
|
|
40
|
+
}).compact
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class LLM::Schema
|
|
4
|
+
##
|
|
5
|
+
# The {LLM::Schema::Object LLM::Schema::Object} class represents an
|
|
6
|
+
# object value in a JSON schema. It is a subclass of
|
|
7
|
+
# {LLM::Schema::Leaf LLM::Schema::Leaf} and provides methods that
|
|
8
|
+
# can act as constraints.
|
|
9
|
+
class Object < Leaf
|
|
10
|
+
attr_reader :properties
|
|
11
|
+
|
|
12
|
+
def initialize(properties)
|
|
13
|
+
@properties = properties
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def to_h
|
|
17
|
+
super.merge!({type: "object", properties:, required:})
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
##
|
|
21
|
+
# @raise [TypeError]
|
|
22
|
+
# When given an object other than Object
|
|
23
|
+
# @return [LLM::Schema::Object]
|
|
24
|
+
# Returns self
|
|
25
|
+
def merge!(other)
|
|
26
|
+
raise TypeError unless self.class === self
|
|
27
|
+
@properties.merge!(other.properties)
|
|
28
|
+
self
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def to_json(options = {})
|
|
32
|
+
to_h.to_json(options)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def required
|
|
38
|
+
@properties.filter_map { _2.required? ? _1 : nil }
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class LLM::Schema
|
|
4
|
+
##
|
|
5
|
+
# The {LLM::Schema::String LLM::Schema::String} class represents a
|
|
6
|
+
# string value in a JSON schema. It is a subclass of
|
|
7
|
+
# {LLM::Schema::Leaf LLM::Schema::Leaf} and provides methods that
|
|
8
|
+
# can act as constraints.
|
|
9
|
+
class String < Leaf
|
|
10
|
+
##
|
|
11
|
+
# Constrain the string to a minimum length
|
|
12
|
+
# @param [Integer] i The minimum length
|
|
13
|
+
# @return [LLM::Schema::String] Returns self
|
|
14
|
+
def min(i)
|
|
15
|
+
tap { @minimum = i }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
##
|
|
19
|
+
# Constrain the string to a maximum length
|
|
20
|
+
# @param [Integer] i The maximum length
|
|
21
|
+
# @return [LLM::Schema::String] Returns self
|
|
22
|
+
def max(i)
|
|
23
|
+
tap { @maximum = i }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def to_h
|
|
27
|
+
super.merge!({
|
|
28
|
+
type: "string",
|
|
29
|
+
minLength: @minimum,
|
|
30
|
+
maxLength: @maximum
|
|
31
|
+
}).compact
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
##
|
|
4
|
+
# The {LLM::Schema LLM::Schema} class represents a JSON schema,
|
|
5
|
+
# and provides methods that let you describe and produce a schema
|
|
6
|
+
# that can be used in various contexts that include the validation
|
|
7
|
+
# and generation of JSON data.
|
|
8
|
+
#
|
|
9
|
+
# @see https://json-schema.org/ JSON Schema Specification
|
|
10
|
+
# @see https://tour.json-schema.org/ JSON Schema Tour
|
|
11
|
+
#
|
|
12
|
+
# @example
|
|
13
|
+
# schema = LLM::Schema.new
|
|
14
|
+
# schema.object({
|
|
15
|
+
# name: schema.string.enum("John", "Jane").required,
|
|
16
|
+
# age: schema.integer.required,
|
|
17
|
+
# hobbies: schema.array(schema.string, schema.null).required,
|
|
18
|
+
# address: schema.object({street: schema.string}).required,
|
|
19
|
+
# })
|
|
20
|
+
class LLM::Schema
|
|
21
|
+
require_relative "schema/version"
|
|
22
|
+
require_relative "schema/leaf"
|
|
23
|
+
require_relative "schema/object"
|
|
24
|
+
require_relative "schema/array"
|
|
25
|
+
require_relative "schema/string"
|
|
26
|
+
require_relative "schema/number"
|
|
27
|
+
require_relative "schema/integer"
|
|
28
|
+
require_relative "schema/boolean"
|
|
29
|
+
require_relative "schema/null"
|
|
30
|
+
|
|
31
|
+
##
|
|
32
|
+
# Returns an object
|
|
33
|
+
# @param [Hash] properties A hash of properties
|
|
34
|
+
# @return [LLM::Schema::Object]
|
|
35
|
+
def object(properties)
|
|
36
|
+
Object.new(properties)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
##
|
|
40
|
+
# Returns an array
|
|
41
|
+
# @param [Array] items An array of items
|
|
42
|
+
# @return [LLM::Schema::Array]
|
|
43
|
+
def array(*items)
|
|
44
|
+
Array.new(*items)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
##
|
|
48
|
+
# Returns a string
|
|
49
|
+
# @return [LLM::Schema::String]
|
|
50
|
+
def string
|
|
51
|
+
String.new
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
##
|
|
55
|
+
# Returns a number
|
|
56
|
+
# @return [LLM::Schema::Number] a number
|
|
57
|
+
def number
|
|
58
|
+
Number.new
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
##
|
|
62
|
+
# Returns an integer
|
|
63
|
+
# @return [LLM::Schema::Integer]
|
|
64
|
+
def integer
|
|
65
|
+
Integer.new
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
##
|
|
69
|
+
# Returns a boolean
|
|
70
|
+
# @return [LLM::Schema::Boolean]
|
|
71
|
+
def boolean
|
|
72
|
+
Boolean.new
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
##
|
|
76
|
+
# Returns null
|
|
77
|
+
# @return [LLM::Schema::Null]
|
|
78
|
+
def null
|
|
79
|
+
Null.new
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
##
|
|
4
|
+
# The {LLM::ServerTool LLM::ServerTool} class represents a platform-native tool
|
|
5
|
+
# that can be activated by an LLM provider. Unlike {LLM::Function LLM::Function},
|
|
6
|
+
# these tools are pre-defined by the provider and their capabilities
|
|
7
|
+
# are already known to the underlying LLM.
|
|
8
|
+
#
|
|
9
|
+
# @example
|
|
10
|
+
# #!/usr/bin/env ruby
|
|
11
|
+
# llm = LLM.gemini ENV["KEY"]
|
|
12
|
+
# bot = LLM::Bot.new(llm, tools: [LLM::ServerTool.new(:google_search)])
|
|
13
|
+
# bot.chat("Summarize today's news", role: :user)
|
|
14
|
+
# print bot.messages.find(&:assistant?).content, "\n"
|
|
15
|
+
class LLM::ServerTool < Struct.new(:name, :options, :provider)
|
|
16
|
+
##
|
|
17
|
+
# @return [String]
|
|
18
|
+
def to_json(...)
|
|
19
|
+
to_h.to_json(...)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
##
|
|
23
|
+
# @return [Hash]
|
|
24
|
+
def to_h
|
|
25
|
+
case provider.class.to_s
|
|
26
|
+
when "LLM::Anthropic" then options.merge("name" => name.to_s)
|
|
27
|
+
when "LLM::Gemini" then {name => options}
|
|
28
|
+
else options.merge("type" => name.to_s)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
alias_method :to_hash, :to_h
|
|
32
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class LLM::Tool
|
|
4
|
+
##
|
|
5
|
+
# The {LLM::Tool::Param LLM::Tool::Param} module extends the
|
|
6
|
+
# {LLM::Tool LLM::Tool} class with a "param" method that can
|
|
7
|
+
# define a parameter for simple types. For complex types, use
|
|
8
|
+
# {LLM::Tool.params LLM::Tool.params} instead.
|
|
9
|
+
#
|
|
10
|
+
# @example
|
|
11
|
+
# class Greeter < LLM::Tool
|
|
12
|
+
# name "greeter"
|
|
13
|
+
# description "Greets the user"
|
|
14
|
+
# param :name, String, "The user's name", required: true
|
|
15
|
+
#
|
|
16
|
+
# def call(name:)
|
|
17
|
+
# puts "Hello, #{name}!"
|
|
18
|
+
# end
|
|
19
|
+
# end
|
|
20
|
+
module Param
|
|
21
|
+
##
|
|
22
|
+
# @param name [Symbol]
|
|
23
|
+
# The name of a parameter
|
|
24
|
+
# @param type [Class, Symbol]
|
|
25
|
+
# The parameter type (eg String)
|
|
26
|
+
# @param description [String]
|
|
27
|
+
# The description of a property
|
|
28
|
+
# @param options [Hash]
|
|
29
|
+
# A hash of options for the parameter
|
|
30
|
+
# @option options [Boolean] :required
|
|
31
|
+
# Whether or not the parameter is required
|
|
32
|
+
# @option options [Object] :default
|
|
33
|
+
# The default value for a given property
|
|
34
|
+
# @option options [Array<String>] :enum
|
|
35
|
+
# One or more possible values for a param
|
|
36
|
+
def param(name, type, description, options = {})
|
|
37
|
+
lock do
|
|
38
|
+
function.params do |schema|
|
|
39
|
+
leaf = schema.public_send(Utils.resolve(type))
|
|
40
|
+
leaf = Utils.setup(leaf, description, options)
|
|
41
|
+
schema.object(name => leaf)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
##
|
|
47
|
+
# @api private
|
|
48
|
+
module Utils
|
|
49
|
+
extend self
|
|
50
|
+
|
|
51
|
+
def resolve(type)
|
|
52
|
+
if type == String
|
|
53
|
+
:string
|
|
54
|
+
elsif type == Integer
|
|
55
|
+
:integer
|
|
56
|
+
elsif type == Float
|
|
57
|
+
:number
|
|
58
|
+
else
|
|
59
|
+
type
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def setup(leaf, description, options)
|
|
64
|
+
required = options.fetch(:required, false)
|
|
65
|
+
default = options.fetch(:default, nil)
|
|
66
|
+
enum = options.fetch(:enum, nil)
|
|
67
|
+
leaf.required if required
|
|
68
|
+
leaf.description(description) if description
|
|
69
|
+
leaf.default(default) if default
|
|
70
|
+
leaf.enum(enum) if enum
|
|
71
|
+
leaf
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|