pikuri-core 0.0.7 → 0.2.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 +17 -30
- data/lib/pikuri/agent/chat_transport.rb +168 -107
- data/lib/pikuri/agent/configurator.rb +194 -173
- data/lib/pikuri/agent/control/cancellable.rb +100 -70
- data/lib/pikuri/agent/control/handoff.rb +75 -0
- data/lib/pikuri/agent/control/interloper.rb +107 -118
- data/lib/pikuri/agent/control.rb +13 -35
- data/lib/pikuri/agent/event.rb +144 -197
- data/lib/pikuri/agent/extension.rb +83 -95
- data/lib/pikuri/agent/history.rb +247 -0
- data/lib/pikuri/agent/listener/in_memory_event_list.rb +1 -1
- data/lib/pikuri/agent/listener/terminal.rb +169 -136
- data/lib/pikuri/agent/listener/token_log.rb +84 -157
- data/lib/pikuri/agent/listener.rb +24 -44
- data/lib/pikuri/agent/listener_list.rb +32 -47
- data/lib/pikuri/agent/request_instrumenter.rb +102 -0
- data/lib/pikuri/agent.rb +1095 -577
- data/lib/pikuri/attachment.rb +65 -0
- data/lib/pikuri/bundler_env.rb +68 -0
- data/lib/pikuri/extractor/passthrough.rb +24 -31
- data/lib/pikuri/extractor.rb +125 -155
- data/lib/pikuri/file_type.rb +64 -136
- data/lib/pikuri/finalizers.rb +32 -47
- data/lib/pikuri/paths.rb +104 -13
- data/lib/pikuri/sanitizer.rb +46 -67
- data/lib/pikuri/subprocess.rb +86 -118
- data/lib/pikuri/testing.rb +369 -0
- data/lib/pikuri/tool/calculator.rb +168 -74
- data/lib/pikuri/tool/execute_context.rb +42 -0
- data/lib/pikuri/tool/parameters.rb +31 -29
- data/lib/pikuri/tool/result.rb +164 -0
- data/lib/pikuri/tool/trifecta_legs.rb +248 -0
- data/lib/pikuri/tool.rb +416 -67
- data/lib/pikuri/trifecta/contribution.rb +43 -0
- data/lib/pikuri/trifecta/node.rb +47 -0
- data/lib/pikuri/trifecta/report.rb +228 -0
- data/lib/pikuri/trifecta.rb +126 -0
- data/lib/pikuri/version.rb +1 -1
- data/lib/pikuri-core.rb +70 -92
- data/prompts/agent-loop.txt +5 -0
- data/prompts/pikuri-chat.txt +3 -12
- metadata +26 -72
- data/lib/pikuri/agent/context_window_detector.rb +0 -222
- data/lib/pikuri/agent/control/step_limit.rb +0 -111
- data/lib/pikuri/agent/extension_context.rb +0 -147
- data/lib/pikuri/agent/listener/rate_limited.rb +0 -172
- data/lib/pikuri/agent/synthesizer.rb +0 -171
- data/lib/pikuri/extractor/html.rb +0 -303
- data/lib/pikuri/tool/fetch.rb +0 -172
- data/lib/pikuri/tool/scraper.rb +0 -186
- data/lib/pikuri/tool/search/brave.rb +0 -198
- data/lib/pikuri/tool/search/duckduckgo.rb +0 -207
- data/lib/pikuri/tool/search/engines.rb +0 -186
- data/lib/pikuri/tool/search/exa.rb +0 -229
- data/lib/pikuri/tool/search/rate_limiter.rb +0 -92
- data/lib/pikuri/tool/search/result.rb +0 -29
- data/lib/pikuri/tool/web_scrape.rb +0 -121
- data/lib/pikuri/tool/web_search.rb +0 -57
- data/lib/pikuri/url_cache.rb +0 -112
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7a738319823ef2f5208a2a016af54ecb161082d378f44f5a745ad4fd9fc7044b
|
|
4
|
+
data.tar.gz: 2b2d0bd51009176acd6405b7a4c7e2b7e3127db618269b95f0cb80d597745c29
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cf9adcef0e8c7d148abe59e8f1b010ec75671802eede2fda6eed16d5dacb327af34d4402bedc1042df7c025d64814a5bb37cec9df55f70afceaa434119f4ce67
|
|
7
|
+
data.tar.gz: 23cbdbe94df3a7a74d9d06944718ec77604afe54c130b621e52a2f3fe218aacc8ab14cbf847bc5868c256460a6e51516b68024bb01318eb1b7e3775730d561bd
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# pikuri-core
|
|
2
2
|
|
|
3
|
-
The lean, audit-friendly foundation of the [pikuri](https://
|
|
3
|
+
The lean, audit-friendly foundation of the [pikuri](https://github.com/mvysny/pikuri)
|
|
4
4
|
AI-assistant toolkit:
|
|
5
5
|
|
|
6
6
|
- `Pikuri::Agent` — a thin wrapper around ruby_llm's chat loop with
|
|
@@ -10,23 +10,19 @@ AI-assistant toolkit:
|
|
|
10
10
|
(`Pikuri::Tool::Parameters`) and LLM-actionable error messages.
|
|
11
11
|
- Listener stream (`Pikuri::Agent::Listener::*`) for rendering,
|
|
12
12
|
token accounting, and structured capture.
|
|
13
|
-
- Controls (`
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
`Pikuri::Tool::WebSearch.build` (provider keys passed in, never
|
|
18
|
-
read from the environment).
|
|
13
|
+
- Controls (`Cancellable`, `Interloper`) for cancellation and
|
|
14
|
+
mid-loop injection. The loop itself is unbounded — see
|
|
15
|
+
`design/decisions.md` `D_no_step_limit`.
|
|
16
|
+
- One bundled example tool, `Pikuri::Tool::CALCULATOR`.
|
|
19
17
|
- A demo binary, `bin/pikuri-chat`.
|
|
20
18
|
|
|
21
|
-
Extensions (skills, MCP, workspace, coding stack,
|
|
22
|
-
personas) live in sibling gems and opt in à la carte — see
|
|
23
|
-
[`pikuri-
|
|
24
|
-
[`pikuri-
|
|
19
|
+
Extensions (web tools, skills, MCP, workspace, coding stack,
|
|
20
|
+
named-agent personas) live in sibling gems and opt in à la carte — see
|
|
21
|
+
[`pikuri-web`](../pikuri-web/README.md),
|
|
22
|
+
[`pikuri-skills`](https://github.com/mvysny/pikuri-workshop/blob/master/pikuri-skills/README.md),
|
|
23
|
+
[`pikuri-mcp`](https://github.com/mvysny/pikuri-workshop/blob/master/pikuri-mcp/README.md),
|
|
25
24
|
[`pikuri-workspace`](../pikuri-workspace/README.md),
|
|
26
|
-
[`pikuri-code`](
|
|
27
|
-
[`pikuri-assistant`](../pikuri-assistant/README.md). For the
|
|
28
|
-
convenience bundle that pulls in everything, see the
|
|
29
|
-
[`pikuri`](../pikuri/README.md) metagem.
|
|
25
|
+
[`pikuri-code`](https://github.com/mvysny/pikuri-workshop/blob/master/pikuri-code/README.md).
|
|
30
26
|
|
|
31
27
|
## Install
|
|
32
28
|
|
|
@@ -44,22 +40,14 @@ gem install pikuri-core
|
|
|
44
40
|
```ruby
|
|
45
41
|
require 'pikuri-core'
|
|
46
42
|
|
|
47
|
-
RubyLLM.configure do |c|
|
|
48
|
-
c.openai_api_base = 'http://localhost:8080/v1'
|
|
49
|
-
c.openai_api_key = 'not-needed'
|
|
50
|
-
end
|
|
51
|
-
|
|
52
43
|
agent = Pikuri::Agent.new(
|
|
53
|
-
transport: Pikuri::Agent::ChatTransport.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
assume_model_exists: true
|
|
44
|
+
transport: Pikuri::Agent::ChatTransport.from_openai_server(
|
|
45
|
+
server: 'http://localhost:8080',
|
|
46
|
+
model: 'ornith-ai/Ornith-1.5-35B-A3B-GGUF'
|
|
57
47
|
),
|
|
58
|
-
system_prompt: Pikuri.prompt(:'pikuri-chat')
|
|
59
|
-
step_limit: Pikuri::Agent::Control::StepLimit.new(max: 20)
|
|
48
|
+
system_prompt: Pikuri.prompt(:'pikuri-chat')
|
|
60
49
|
) do |c|
|
|
61
50
|
c.add_tool Pikuri::Tool::CALCULATOR
|
|
62
|
-
c.add_tool Pikuri::Tool::WebSearch.build
|
|
63
51
|
c.add_listener Pikuri::Agent::Listener::Terminal.new
|
|
64
52
|
end
|
|
65
53
|
agent.run_loop(user_message: 'What is 17 * 23?')
|
|
@@ -70,10 +58,9 @@ handling, and cancellation.
|
|
|
70
58
|
|
|
71
59
|
## Further reading
|
|
72
60
|
|
|
73
|
-
- **Narrative walkthrough:** [chapter
|
|
61
|
+
- **Narrative walkthrough:** [the first-conversation chapter of the pikuri guide](../book/chat.md)
|
|
74
62
|
— install llama.cpp, start the server, run `pikuri-chat`, the
|
|
75
|
-
agentic loop, the
|
|
76
|
-
privacy postures.
|
|
63
|
+
agentic loop, and the tools `pikuri-chat` wires.
|
|
77
64
|
- **API reference:** browse the YARD docs at
|
|
78
65
|
<https://rubydoc.info/gems/pikuri-core> (once published), or run
|
|
79
66
|
`bundle exec yard` in this directory for a local copy.
|
|
@@ -2,107 +2,123 @@
|
|
|
2
2
|
|
|
3
3
|
module Pikuri
|
|
4
4
|
class Agent
|
|
5
|
-
# Everything that
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
# class — every forwarding site (the synthesizer rescue in
|
|
13
|
-
# {Agent#run_loop}, the +agent+ tool from +pikuri-subagents+
|
|
14
|
-
# spawning a sub-agent, a mid-conversation model switch) used to
|
|
15
|
-
# pass the resolution fields individually, and dropping one routed
|
|
16
|
-
# the chat to a different server or raised
|
|
17
|
-
# +RubyLLM::ModelNotFoundError+ on the unknown model id. With a
|
|
18
|
-
# single value object the call site can't silently miss a field.
|
|
5
|
+
# Everything that must travel together for a chat to resolve to the same
|
|
6
|
+
# model *on the same server* on every construction: model id, provider
|
|
7
|
+
# hint, registry-bypass flag, and — when the model lives off the
|
|
8
|
+
# process-global +RubyLLM.config+ default — that server's base URL and API
|
|
9
|
+
# key. One value object so a forwarding site (the synthesizer, a sub-agent
|
|
10
|
+
# spawn, a mid-conversation switch) can't silently drop a field and route
|
|
11
|
+
# the chat to the wrong server or raise +RubyLLM::ModelNotFoundError+.
|
|
19
12
|
#
|
|
20
13
|
# == Why +api_base+ / +api_key+ live here
|
|
21
14
|
#
|
|
22
|
-
# +RubyLLM::Chat#with_model+ swaps only
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
29
|
-
#
|
|
30
|
-
# +
|
|
15
|
+
# +RubyLLM::Chat#with_model+ swaps only model/provider against the chat's
|
|
16
|
+
# *existing* connection, so switching to a model on a different server
|
|
17
|
+
# needs the connection to travel with the model — else the new id is sent
|
|
18
|
+
# to the old server's URL with the old key. {Agent} maps these two generic
|
|
19
|
+
# fields onto the provider's config slots (+#{provider}_api_base+ /
|
|
20
|
+
# +#{provider}_api_key+) via a per-chat +RubyLLM::Context+; both +nil+ for a
|
|
21
|
+
# transport riding the global config.
|
|
22
|
+
#
|
|
23
|
+
# No +RubyLLM+ references here, so the seam stays in {Agent},
|
|
24
|
+
# +bin/pikuri-chat+, and {Tool}.
|
|
25
|
+
#
|
|
26
|
+
# == Extra request parameters
|
|
27
|
+
#
|
|
28
|
+
# +params+ is an opaque passthrough for a request key pikuri names no field
|
|
29
|
+
# for. It is deep-merged into the chat-completion payload as written:
|
|
30
|
+
#
|
|
31
|
+
# transport.with(params: { chat_template_kwargs: { enable_thinking: false } })
|
|
32
|
+
# # on the wire: {"model":…,"messages":[…],
|
|
33
|
+
# # "chat_template_kwargs":{"enable_thinking":false}}
|
|
31
34
|
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
35
|
+
# It rides the transport rather than a call because the spelling is
|
|
36
|
+
# per-model-per-server, exactly like +context_window+; pikuri translates
|
|
37
|
+
# none of it — a parameter is sent as written or not at all. Why there is no
|
|
38
|
+
# typed +thinking:+ flag instead: +design/decisions.md+
|
|
39
|
+
# +D_opaque_request_params+.
|
|
40
|
+
#
|
|
41
|
+
# Two edges worth knowing. Keys are deep-symbolized on the way in (naming
|
|
42
|
+
# one key in both spellings raises), without which a table read from TOML
|
|
43
|
+
# would land a second +"temperature"+ beside the payload's +:temperature+
|
|
44
|
+
# instead of overriding it. And the merge is unguarded: a table naming
|
|
45
|
+
# +messages+, +tools+ or +stream+ breaks the loop rather than configuring
|
|
46
|
+
# it, and one naming +model+ sends a model this transport does not think it
|
|
47
|
+
# is using.
|
|
34
48
|
#
|
|
35
49
|
# @!attribute [r] model
|
|
36
|
-
# @return [String, nil] LLM
|
|
37
|
-
# +RubyLLM.config.default_model+ at {Agent} construction
|
|
50
|
+
# @return [String, nil] LLM id; +nil+ defers to
|
|
51
|
+
# +RubyLLM.config.default_model+ at {Agent} construction.
|
|
38
52
|
# @!attribute [r] provider
|
|
39
|
-
# @return [Symbol, nil] forwarded to +RubyLLM.chat+. Required
|
|
40
|
-
#
|
|
41
|
-
#
|
|
42
|
-
#
|
|
43
|
-
# +api_base+ / +api_key+ is set (it names the config slots).
|
|
53
|
+
# @return [Symbol, nil] forwarded to +RubyLLM.chat+. Required with
|
|
54
|
+
# +assume_model_exists+ for a local OpenAI-compatible server whose ids
|
|
55
|
+
# aren't in ruby_llm's registry, and whenever +api_base+/+api_key+ is
|
|
56
|
+
# set (it names the config slots).
|
|
44
57
|
# @!attribute [r] assume_model_exists
|
|
45
|
-
# @return [Boolean] forwarded to +RubyLLM.chat+; +true+ skips
|
|
46
|
-
#
|
|
47
|
-
# Requires +provider+.
|
|
58
|
+
# @return [Boolean] forwarded to +RubyLLM.chat+; +true+ skips the registry
|
|
59
|
+
# lookup and trusts the model id. Requires +provider+.
|
|
48
60
|
# @!attribute [r] api_base
|
|
49
|
-
# @return [String, nil]
|
|
50
|
-
#
|
|
51
|
-
#
|
|
52
|
-
# +#{provider}_api_base+ slot by {Agent}.
|
|
61
|
+
# @return [String, nil] this server's base URL (e.g.
|
|
62
|
+
# +http://localhost:8080/v1+); +nil+ rides the global config. Mapped to
|
|
63
|
+
# +#{provider}_api_base+ by {Agent}.
|
|
53
64
|
# @!attribute [r] api_key
|
|
54
|
-
# @return [String, nil] API key for this
|
|
55
|
-
#
|
|
56
|
-
#
|
|
57
|
-
# so it never leaks into a log line or backtrace.
|
|
65
|
+
# @return [String, nil] API key for this server; +nil+ rides the global
|
|
66
|
+
# config. Mapped to +#{provider}_api_key+ by {Agent}. Redacted in
|
|
67
|
+
# {#inspect}.
|
|
58
68
|
# @!attribute [r] context_window
|
|
59
|
-
# @return [Integer, nil]
|
|
60
|
-
#
|
|
61
|
-
#
|
|
62
|
-
#
|
|
63
|
-
#
|
|
64
|
-
# own
|
|
65
|
-
#
|
|
66
|
-
#
|
|
67
|
-
#
|
|
68
|
-
#
|
|
69
|
-
#
|
|
70
|
-
#
|
|
71
|
-
#
|
|
72
|
-
#
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
69
|
+
# @return [Integer, nil] this model-on-server's context-window cap, or
|
|
70
|
+
# +nil+ when unknown. Travels
|
|
71
|
+
# with the model because the cap *is* per-model-per-server. pikuri
|
|
72
|
+
# never looks it up: the host states it, or a probe the host runs fills
|
|
73
|
+
# it in. Never sent to ruby_llm, and read by nothing in pikuri-core: it
|
|
74
|
+
# is for the host's own display, and a sub-agent inherits it with the
|
|
75
|
+
# transport.
|
|
76
|
+
# @!attribute [r] modalities
|
|
77
|
+
# @return [Hash{Symbol => Boolean}, nil] which input kinds this
|
|
78
|
+
# model-on-server accepts, in llama.cpp's +/props+ vocabulary
|
|
79
|
+
# (+{vision: false, audio: false}+), or +nil+ when unknown. Only a
|
|
80
|
+
# +false+ entry refuses an attachment ({Agent::UnsupportedAttachmentError});
|
|
81
|
+
# a missing key, like +nil+, lets it go out and leaves any error to the
|
|
82
|
+
# server. Stated and inherited like +context_window+.
|
|
83
|
+
# @!attribute [r] faraday_adapter
|
|
84
|
+
# @return [Class, Symbol, nil] a custom Faraday adapter to install on this
|
|
85
|
+
# agent's per-chat +RubyLLM::Context+ (via +config.faraday_adapter=+),
|
|
86
|
+
# or +nil+ for ruby_llm's default (+:net_http+). A generic HTTP seam —
|
|
87
|
+
# inject a VCR / logging / *scripted-fake* adapter without touching the
|
|
88
|
+
# global +RubyLLM.config+. Its worked use is
|
|
89
|
+
# {Pikuri::Testing.fake_transport}, which bakes an anonymous scripted
|
|
90
|
+
# adapter here so the real ruby_llm loop runs against canned HTTP.
|
|
91
|
+
# @!attribute [r] params
|
|
92
|
+
# @return [Hash{Symbol => Object}] extra chat-completion payload keys,
|
|
93
|
+
# merged in verbatim; +{}+ for none. Keys symbolized, and every hash /
|
|
94
|
+
# array node frozen (a value object is *not* deep-frozen: a +String+
|
|
95
|
+
# value the caller still holds stays mutable).
|
|
96
|
+
# See +== Extra request parameters+.
|
|
97
|
+
class ChatTransport < Data.define(:model, :provider, :assume_model_exists, :api_base, :api_key,
|
|
98
|
+
:context_window, :modalities, :faraday_adapter, :params)
|
|
99
|
+
# Build an +:openai+-provider transport for an OpenAI-compatible server
|
|
100
|
+
# (local llama.cpp, cloud endpoint, ...), carrying its connection so the
|
|
101
|
+
# agent rides a per-chat +RubyLLM::Context+ instead of the global config.
|
|
102
|
+
# The +bin/pikuri-*+ host-boot factory in place of +RubyLLM.configure+ —
|
|
103
|
+
# one isolated connection per agent.
|
|
82
104
|
#
|
|
83
|
-
# +server+ is
|
|
84
|
-
#
|
|
85
|
-
#
|
|
86
|
-
#
|
|
87
|
-
# +https://api.x.ai/v1/+ all normalize to the same +.../v1+ base.
|
|
88
|
-
# Without this, a +server+ value that already ended in +/v1+ would
|
|
89
|
-
# double to +/v1/v1+ and every request would 404.
|
|
105
|
+
# A trailing +/v1+ on +server+ is stripped and re-appended exactly once,
|
|
106
|
+
# so +https://api.x.ai+, +.../v1+, and +.../v1/+ all normalize to the same
|
|
107
|
+
# +.../v1+ base — without which a +/v1+-terminated value would double to
|
|
108
|
+
# +/v1/v1+ and 404.
|
|
90
109
|
#
|
|
91
|
-
# @param server [String] server origin, with or without a trailing
|
|
92
|
-
# +/v1+, e.g. +"http://localhost:8080"+ or +"https://api.x.ai/v1"+
|
|
110
|
+
# @param server [String] server origin, with or without a trailing +/v1+
|
|
93
111
|
# @param model [String] model id served there, trusted verbatim
|
|
94
|
-
#
|
|
95
|
-
#
|
|
96
|
-
# @param
|
|
97
|
-
#
|
|
98
|
-
# @param
|
|
99
|
-
#
|
|
100
|
-
#
|
|
101
|
-
#
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
# normalized +.../v1+ URL and whose +api_key+ is +api_key+
|
|
105
|
-
def self.from_openai_server(server:, model:, api_key: 'not-needed', context_window: nil)
|
|
112
|
+
# @param api_key [String] API key; the +"not-needed"+ placeholder for a
|
|
113
|
+
# keyless local server
|
|
114
|
+
# @param context_window [Integer, nil] the model's cap, or +nil+ for
|
|
115
|
+
# unknown
|
|
116
|
+
# @param params [Hash{String, Symbol => Object}] extra payload keys for
|
|
117
|
+
# this connection, e.g.
|
|
118
|
+
# +{'chat_template_kwargs' => {'enable_thinking' => false}}+
|
|
119
|
+
# @return [ChatTransport]
|
|
120
|
+
def self.from_openai_server(server:, model:, api_key: 'not-needed', context_window: nil,
|
|
121
|
+
params: {})
|
|
106
122
|
base = server.to_s.strip.chomp('/').delete_suffix('/v1')
|
|
107
123
|
new(
|
|
108
124
|
model: model,
|
|
@@ -110,7 +126,8 @@ module Pikuri
|
|
|
110
126
|
assume_model_exists: true,
|
|
111
127
|
api_base: "#{base}/v1",
|
|
112
128
|
api_key: api_key,
|
|
113
|
-
context_window: context_window
|
|
129
|
+
context_window: context_window,
|
|
130
|
+
params: params
|
|
114
131
|
)
|
|
115
132
|
end
|
|
116
133
|
|
|
@@ -120,47 +137,91 @@ module Pikuri
|
|
|
120
137
|
# @param api_base [String, nil]
|
|
121
138
|
# @param api_key [String, nil]
|
|
122
139
|
# @param context_window [Integer, nil]
|
|
140
|
+
# @param modalities [Hash{Symbol => Boolean}, nil]
|
|
141
|
+
# @param faraday_adapter [Class, Symbol, nil]
|
|
142
|
+
# @param params [Hash{String, Symbol => Object}]
|
|
123
143
|
# @raise [ArgumentError] if +api_base+ or +api_key+ is set without
|
|
124
144
|
# a +provider+ (the provider names the config slots the
|
|
125
|
-
# connection overrides map onto)
|
|
126
|
-
|
|
127
|
-
|
|
145
|
+
# connection overrides map onto), or if +params+ is not a Hash, or
|
|
146
|
+
# names one key in both the +String+ and +Symbol+ spelling
|
|
147
|
+
def initialize(model:, provider: nil, assume_model_exists: false, api_base: nil,
|
|
148
|
+
api_key: nil, context_window: nil, modalities: nil, faraday_adapter: nil,
|
|
149
|
+
params: {})
|
|
128
150
|
if (api_base || api_key) && provider.nil?
|
|
129
151
|
raise ArgumentError, "api_base/api_key require a provider, got #{provider.inspect}"
|
|
130
152
|
end
|
|
153
|
+
raise ArgumentError, "params must be a Hash, got #{params.class}" unless params.is_a?(Hash)
|
|
131
154
|
|
|
132
|
-
super
|
|
155
|
+
super(model: model, provider: provider, assume_model_exists: assume_model_exists,
|
|
156
|
+
api_base: api_base, api_key: api_key, context_window: context_window,
|
|
157
|
+
modalities: modalities&.transform_keys(&:to_sym)&.freeze,
|
|
158
|
+
faraday_adapter: faraday_adapter, params: deep_symbolize(params))
|
|
133
159
|
end
|
|
134
160
|
|
|
135
|
-
#
|
|
136
|
-
# +RubyLLM::Context#chat+. Excludes the connection fields
|
|
137
|
-
#
|
|
138
|
-
# call
|
|
161
|
+
# Model-resolution kwargs to spread into +RubyLLM.chat+ /
|
|
162
|
+
# +RubyLLM::Context#chat+. Excludes the connection fields (those configure
|
|
163
|
+
# the +Context+, not the +chat+ call) and +params+ (a
|
|
164
|
+
# +with_provider_options+ call on the built chat).
|
|
139
165
|
#
|
|
140
166
|
# @return [Hash{Symbol => String, Symbol, Boolean, nil}]
|
|
141
167
|
def chat_kwargs
|
|
142
168
|
{ model: model, provider: provider, assume_model_exists: assume_model_exists }
|
|
143
169
|
end
|
|
144
170
|
|
|
145
|
-
#
|
|
146
|
-
#
|
|
171
|
+
# Default +Data#inspect+ would print +api_key+ verbatim into any log line
|
|
172
|
+
# or backtrace. Redact it.
|
|
147
173
|
#
|
|
148
|
-
#
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
# Default +Data#inspect+ would print +api_key+ verbatim, leaking
|
|
154
|
-
# the secret into any log line, +to_s+ interpolation, or backtrace
|
|
155
|
-
# that touches the transport. Redact it.
|
|
174
|
+
# +params+ is configuration rather than a credential channel, so it prints
|
|
175
|
+
# verbatim — anything sensitive a host puts there lands in every log line
|
|
176
|
+
# and backtrace that inspects a transport.
|
|
156
177
|
#
|
|
157
178
|
# @return [String]
|
|
158
179
|
def inspect
|
|
159
180
|
"#<#{self.class} model=#{model.inspect} provider=#{provider.inspect} " \
|
|
160
181
|
"assume_model_exists=#{assume_model_exists} api_base=#{api_base.inspect} " \
|
|
161
|
-
"api_key=#{api_key.nil? ? 'nil' : '[REDACTED]'} context_window=#{context_window.inspect}
|
|
182
|
+
"api_key=#{api_key.nil? ? 'nil' : '[REDACTED]'} context_window=#{context_window.inspect} " \
|
|
183
|
+
"modalities=#{modalities.inspect} faraday_adapter=#{faraday_adapter.inspect} params=#{params.inspect}>"
|
|
162
184
|
end
|
|
163
185
|
alias to_s inspect
|
|
186
|
+
|
|
187
|
+
private
|
|
188
|
+
|
|
189
|
+
# Recursively symbolize +value+'s hash keys, freezing what it builds.
|
|
190
|
+
# Scalars pass through as they are — only the containers are rebuilt.
|
|
191
|
+
#
|
|
192
|
+
# @param value [Hash, Array, Object] any +params+ subtree.
|
|
193
|
+
# @return [Hash, Array, Object] the same shape, symbol-keyed, with every
|
|
194
|
+
# hash / array node frozen.
|
|
195
|
+
# @raise [ArgumentError] if a hash names one key in both spellings —
|
|
196
|
+
# the merge can send only one of them, and picking silently is the
|
|
197
|
+
# failure mode this whole channel exists to avoid.
|
|
198
|
+
def deep_symbolize(value)
|
|
199
|
+
case value
|
|
200
|
+
when Hash
|
|
201
|
+
symbolized = value.to_h { |k, v| [symbolize_key(k), deep_symbolize(v)] }
|
|
202
|
+
reject_collisions(value) if symbolized.size < value.size
|
|
203
|
+
symbolized.freeze
|
|
204
|
+
when Array
|
|
205
|
+
value.map { |item| deep_symbolize(item) }.freeze
|
|
206
|
+
else
|
|
207
|
+
value
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# @param key [Object] a +params+ hash key.
|
|
212
|
+
# @return [Object] +key+ as a +Symbol+, or unchanged if it has no
|
|
213
|
+
# symbol form (an +Integer+ key in a provider's own table).
|
|
214
|
+
def symbolize_key(key)
|
|
215
|
+
key.respond_to?(:to_sym) ? key.to_sym : key
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# @param table [Hash] the pre-symbolize hash whose keys collided.
|
|
219
|
+
# @return [void]
|
|
220
|
+
# @raise [ArgumentError] always, naming the colliding spellings.
|
|
221
|
+
def reject_collisions(table)
|
|
222
|
+
collided = table.keys.group_by { |k| symbolize_key(k) }.select { |_, ks| ks.size > 1 }
|
|
223
|
+
raise ArgumentError, "params names a key twice: #{collided.values.flatten.inspect}"
|
|
224
|
+
end
|
|
164
225
|
end
|
|
165
226
|
end
|
|
166
227
|
end
|