ruby_llm 0.1.0.pre19 → 0.1.0.pre20
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/LICENSE +21 -0
- data/README.md +9 -14
- data/lib/ruby_llm/providers/openai.rb +15 -3
- data/lib/ruby_llm/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c12d4699a9959fb454d21065bc5d666ef59873274f6bcaacb79c63cb7fdf9559
|
4
|
+
data.tar.gz: f391e7cb0970a1362238a5251bb7fc7316d5358af9542f059c112e84d0bed6cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c20e3a9addf60aaa9e475f433ac3ae707d5f07a89f7f4b95c785749f9ad051194d306d6f00ba9570c06cf9b0488ac711646c44251528899304ec2945883fb097
|
7
|
+
data.tar.gz: 9f9f1dca17a24b0c478d98755722721410706f3e5f2993d19290fc55c69da3d3cc3d058fe01826e92e828446e74a552a36b3ede40561d32a65ffd72237ce9a36
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Carmine Paolino
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -82,16 +82,13 @@ Need vector embeddings for your text? RubyLLM makes it simple:
|
|
82
82
|
|
83
83
|
```ruby
|
84
84
|
# Get embeddings with the default model
|
85
|
-
|
85
|
+
RubyLLM.embed "Hello, world!"
|
86
86
|
|
87
87
|
# Use a specific model
|
88
|
-
|
89
|
-
"Ruby is awesome!",
|
90
|
-
model: "text-embedding-3-large"
|
91
|
-
)
|
88
|
+
RubyLLM.embed "Ruby is awesome!", model: "text-embedding-3-large"
|
92
89
|
|
93
90
|
# Process multiple texts at once
|
94
|
-
|
91
|
+
RubyLLM.embed([
|
95
92
|
"First document",
|
96
93
|
"Second document",
|
97
94
|
"Third document"
|
@@ -242,14 +239,14 @@ That's it! Now you can use chats straight from your models:
|
|
242
239
|
|
243
240
|
```ruby
|
244
241
|
# Create a new chat
|
245
|
-
chat = Chat.create!
|
242
|
+
chat = Chat.create! model_id: "gpt-4o-mini"
|
246
243
|
|
247
244
|
# Ask questions - messages are automatically saved
|
248
245
|
chat.ask "What's the weather in Paris?"
|
249
246
|
|
250
247
|
# Stream responses in real-time
|
251
248
|
chat.ask "Tell me a story" do |chunk|
|
252
|
-
broadcast_chunk
|
249
|
+
broadcast_chunk chunk
|
253
250
|
end
|
254
251
|
|
255
252
|
# Everything is persisted automatically
|
@@ -307,7 +304,7 @@ The persistence works seamlessly with background jobs:
|
|
307
304
|
```ruby
|
308
305
|
class ChatJob < ApplicationJob
|
309
306
|
def perform(chat_id, message)
|
310
|
-
chat = Chat.find
|
307
|
+
chat = Chat.find chat_id
|
311
308
|
|
312
309
|
chat.ask(message) do |chunk|
|
313
310
|
# Optional: Broadcast chunks for real-time updates
|
@@ -341,8 +338,8 @@ class WeatherTool < RubyLLM::Tool
|
|
341
338
|
end
|
342
339
|
|
343
340
|
# Use tools with your persisted chats
|
344
|
-
chat = Chat.create!
|
345
|
-
chat.chat.with_tool
|
341
|
+
chat = Chat.create! model_id: "gpt-4"
|
342
|
+
chat.chat.with_tool WeatherTool.new
|
346
343
|
|
347
344
|
# Ask about weather - tool usage is automatically saved
|
348
345
|
chat.ask "What's the weather in Paris?"
|
@@ -352,8 +349,6 @@ pp chat.messages.map(&:role)
|
|
352
349
|
#=> [:user, :assistant, :tool, :assistant]
|
353
350
|
```
|
354
351
|
|
355
|
-
Looking for more examples? Check out the [example Rails app](https://github.com/example/ruby_llm_rails) showing these patterns in action!
|
356
|
-
|
357
352
|
## Development
|
358
353
|
|
359
354
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt.
|
@@ -364,4 +359,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/crmne/
|
|
364
359
|
|
365
360
|
## License
|
366
361
|
|
367
|
-
Released under the MIT License. See LICENSE
|
362
|
+
Released under the MIT License. See [LICENSE](LICENSE) for details.
|
@@ -43,13 +43,14 @@ module RubyLLM
|
|
43
43
|
payload[:tools] = tools.map { |_, tool| tool_for(tool) }
|
44
44
|
payload[:tool_choice] = 'auto'
|
45
45
|
end
|
46
|
+
payload[:stream_options] = { include_usage: true } if stream
|
46
47
|
end
|
47
48
|
end
|
48
49
|
|
49
50
|
def format_messages(messages)
|
50
51
|
messages.map do |msg|
|
51
52
|
{
|
52
|
-
role: msg.role
|
53
|
+
role: format_role(msg.role),
|
53
54
|
content: msg.content,
|
54
55
|
tool_calls: format_tool_calls(msg.tool_calls),
|
55
56
|
tool_call_id: msg.tool_call_id
|
@@ -57,6 +58,15 @@ module RubyLLM
|
|
57
58
|
end
|
58
59
|
end
|
59
60
|
|
61
|
+
def format_role(role)
|
62
|
+
case role
|
63
|
+
when :system
|
64
|
+
'developer'
|
65
|
+
else
|
66
|
+
role.to_s
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
60
70
|
def build_embedding_payload(text, model:)
|
61
71
|
{
|
62
72
|
model: model,
|
@@ -156,14 +166,16 @@ module RubyLLM
|
|
156
166
|
end.compact
|
157
167
|
end
|
158
168
|
|
159
|
-
def handle_stream(&block)
|
169
|
+
def handle_stream(&block) # rubocop:disable Metrics/MethodLength
|
160
170
|
to_json_stream do |data|
|
161
171
|
block.call(
|
162
172
|
Chunk.new(
|
163
173
|
role: :assistant,
|
164
174
|
model_id: data['model'],
|
165
175
|
content: data.dig('choices', 0, 'delta', 'content'),
|
166
|
-
tool_calls: parse_tool_calls(data.dig('choices', 0, 'delta', 'tool_calls'), parse_arguments: false)
|
176
|
+
tool_calls: parse_tool_calls(data.dig('choices', 0, 'delta', 'tool_calls'), parse_arguments: false),
|
177
|
+
input_tokens: data.dig('usage', 'prompt_tokens'),
|
178
|
+
output_tokens: data.dig('usage', 'completion_tokens')
|
167
179
|
)
|
168
180
|
)
|
169
181
|
end
|
data/lib/ruby_llm/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_llm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0.
|
4
|
+
version: 0.1.0.pre20
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carmine Paolino
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-02-
|
11
|
+
date: 2025-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: event_stream_parser
|
@@ -339,6 +339,7 @@ files:
|
|
339
339
|
- ".rspec"
|
340
340
|
- ".rubocop.yml"
|
341
341
|
- Gemfile
|
342
|
+
- LICENSE
|
342
343
|
- README.md
|
343
344
|
- Rakefile
|
344
345
|
- bin/console
|