ruby_llm 0.1.0.pre16 → 0.1.0.pre18
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 +27 -0
- data/lib/ruby_llm/chat.rb +3 -1
- data/lib/ruby_llm/configuration.rb +6 -1
- data/lib/ruby_llm/embedding.rb +17 -0
- data/lib/ruby_llm/provider.rb +10 -4
- data/lib/ruby_llm/providers/openai.rb +16 -0
- data/lib/ruby_llm/version.rb +1 -1
- data/lib/ruby_llm.rb +4 -0
- 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: cb34b8e16d243dfdc95d7fc6a70a67ecb2afac895da5a646e57dbc915da0a8e9
|
4
|
+
data.tar.gz: 6e95fb39b3670a38422b0dd79ec97263c34e6143751fef2272a6401ccac79c95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af33908a0ff937c4a7d0b074b4406c4211456dbb92c39d5c2237ca6463f3bd8129141a4b2c23270da85fbf90d26c0e8c6b295aaedb8574a23e0ab3eb107abd39
|
7
|
+
data.tar.gz: c6ce63d8d361fffcfaf6bfa2fbdea1df42ddf75e2969d68c5f789fbb14fccb61284c9a6b46fb0c0472c0e18cb31da150544b3cf2e9af4a1e53039a164b4d3e9f
|
data/README.md
CHANGED
@@ -76,6 +76,33 @@ last_message = chat.messages.last
|
|
76
76
|
puts "Conversation used #{last_message.input_tokens} input tokens and #{last_message.output_tokens} output tokens"
|
77
77
|
```
|
78
78
|
|
79
|
+
## Text Embeddings
|
80
|
+
|
81
|
+
Need vector embeddings for your text? RubyLLM makes it simple:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
# Get embeddings with the default model
|
85
|
+
vector = RubyLLM.embed "Hello, world!"
|
86
|
+
|
87
|
+
# Use a specific model
|
88
|
+
vector = RubyLLM.embed(
|
89
|
+
"Ruby is awesome!",
|
90
|
+
model: "text-embedding-3-large"
|
91
|
+
)
|
92
|
+
|
93
|
+
# Process multiple texts at once
|
94
|
+
vectors = RubyLLM.embed([
|
95
|
+
"First document",
|
96
|
+
"Second document",
|
97
|
+
"Third document"
|
98
|
+
])
|
99
|
+
|
100
|
+
# Configure the default model
|
101
|
+
RubyLLM.configure do |config|
|
102
|
+
config.default_embedding_model = 'text-embedding-3-large'
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
79
106
|
## Using Tools
|
80
107
|
|
81
108
|
Give your AI assistants access to your Ruby code by creating tool classes that do one thing well:
|
data/lib/ruby_llm/chat.rb
CHANGED
@@ -97,8 +97,10 @@ module RubyLLM
|
|
97
97
|
|
98
98
|
def handle_tool_calls(response, &block)
|
99
99
|
response.tool_calls.each_value do |tool_call|
|
100
|
+
@on[:new_message]&.call
|
100
101
|
result = execute_tool tool_call
|
101
|
-
add_tool_result tool_call.id, result
|
102
|
+
message = add_tool_result tool_call.id, result
|
103
|
+
@on[:end_message]&.call(message)
|
102
104
|
end
|
103
105
|
|
104
106
|
complete(&block)
|
@@ -10,11 +10,16 @@ module RubyLLM
|
|
10
10
|
# config.anthropic_api_key = ENV['ANTHROPIC_API_KEY']
|
11
11
|
# end
|
12
12
|
class Configuration
|
13
|
-
attr_accessor :openai_api_key,
|
13
|
+
attr_accessor :openai_api_key,
|
14
|
+
:anthropic_api_key,
|
15
|
+
:default_model,
|
16
|
+
:default_embedding_model,
|
17
|
+
:request_timeout
|
14
18
|
|
15
19
|
def initialize
|
16
20
|
@request_timeout = 30
|
17
21
|
@default_model = 'gpt-4o-mini'
|
22
|
+
@default_embedding_model = 'text-embedding-3-small'
|
18
23
|
end
|
19
24
|
end
|
20
25
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RubyLLM
|
4
|
+
# Core embedding interface. Provides a clean way to generate embeddings
|
5
|
+
# from text using various provider models.
|
6
|
+
module Embedding
|
7
|
+
module_function
|
8
|
+
|
9
|
+
def embed(text, model: nil)
|
10
|
+
model_id = model || RubyLLM.config.default_embedding_model
|
11
|
+
Models.find(model_id)
|
12
|
+
|
13
|
+
provider = Provider.for(model_id)
|
14
|
+
provider.embed(text, model: model_id)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/ruby_llm/provider.rb
CHANGED
@@ -30,17 +30,23 @@ module RubyLLM
|
|
30
30
|
parse_list_models_response response
|
31
31
|
end
|
32
32
|
|
33
|
+
def embed(text, model:)
|
34
|
+
payload = build_embedding_payload text, model: model
|
35
|
+
response = post embedding_url, payload
|
36
|
+
parse_embedding_response response
|
37
|
+
end
|
38
|
+
|
33
39
|
private
|
34
40
|
|
35
41
|
def sync_response(payload)
|
36
|
-
response = post payload
|
42
|
+
response = post completion_url, payload
|
37
43
|
parse_completion_response response
|
38
44
|
end
|
39
45
|
|
40
46
|
def stream_response(payload, &block)
|
41
47
|
accumulator = StreamAccumulator.new
|
42
48
|
|
43
|
-
post payload do |req|
|
49
|
+
post completion_url, payload do |req|
|
44
50
|
req.options.on_data = handle_stream do |chunk|
|
45
51
|
accumulator.add chunk
|
46
52
|
block.call chunk
|
@@ -50,8 +56,8 @@ module RubyLLM
|
|
50
56
|
accumulator.to_message
|
51
57
|
end
|
52
58
|
|
53
|
-
def post(payload)
|
54
|
-
connection.post
|
59
|
+
def post(url, payload)
|
60
|
+
connection.post url, payload do |req|
|
55
61
|
req.headers.merge! headers
|
56
62
|
yield req if block_given?
|
57
63
|
end
|
@@ -28,6 +28,10 @@ module RubyLLM
|
|
28
28
|
'/v1/models'
|
29
29
|
end
|
30
30
|
|
31
|
+
def embedding_url
|
32
|
+
'/v1/embeddings'
|
33
|
+
end
|
34
|
+
|
31
35
|
def build_payload(messages, tools:, temperature:, model:, stream: false) # rubocop:disable Metrics/MethodLength
|
32
36
|
{
|
33
37
|
model: model,
|
@@ -53,6 +57,18 @@ module RubyLLM
|
|
53
57
|
end
|
54
58
|
end
|
55
59
|
|
60
|
+
def build_embedding_payload(text, model:)
|
61
|
+
{
|
62
|
+
model: model,
|
63
|
+
input: text
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
def parse_embedding_response(response)
|
68
|
+
embeddings = response.body['data'].map { |d| d['embedding'] }
|
69
|
+
embeddings.size == 1 ? embeddings.first : embeddings
|
70
|
+
end
|
71
|
+
|
56
72
|
def format_tool_calls(tool_calls) # rubocop:disable Metrics/MethodLength
|
57
73
|
return nil unless tool_calls&.any?
|
58
74
|
|
data/lib/ruby_llm/version.rb
CHANGED
data/lib/ruby_llm.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.pre18
|
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-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: event_stream_parser
|
@@ -348,6 +348,7 @@ files:
|
|
348
348
|
- lib/ruby_llm/chat.rb
|
349
349
|
- lib/ruby_llm/chunk.rb
|
350
350
|
- lib/ruby_llm/configuration.rb
|
351
|
+
- lib/ruby_llm/embedding.rb
|
351
352
|
- lib/ruby_llm/message.rb
|
352
353
|
- lib/ruby_llm/model_capabilities/anthropic.rb
|
353
354
|
- lib/ruby_llm/model_capabilities/openai.rb
|