lammy 0.1.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lammy/chat.rb +2 -2
- data/lib/lammy/embeddings.rb +8 -8
- data/lib/lammy/openai.rb +23 -10
- data/lib/lammy/schema.rb +1 -1
- data/lib/lammy.rb +0 -37
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9225fc482f95ed862d6e12bc4541731c8854127900e0bfe369075ef1092fae8e
|
4
|
+
data.tar.gz: 720ce8849858edb0832c34b0b10ef736c6353b12daa1e5cecf68c137de94a91d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83be8412698ad307dba650aaa54e230cf9b47ee9e140e9dac1d26c2927f28b43649ac9b2e6196154a09b8b608a04b8fb72e7c0792e2a6bd799a2ff1edd339494
|
7
|
+
data.tar.gz: 9add86016f31a13416848ff64a7b9eeea03808b9220b239b029049c1ffc04796a9253f71dfac623928b8eddacbd8ffa182a243fd1a7c50c5a5ade6bdc8a68b50
|
data/lib/lammy/chat.rb
CHANGED
@@ -28,8 +28,8 @@ module L
|
|
28
28
|
|
29
29
|
case settings[:model]
|
30
30
|
when *OpenAI::MODELS
|
31
|
-
client = OpenAI.new
|
32
|
-
client.chat(
|
31
|
+
client = OpenAI.new(settings)
|
32
|
+
client.chat(user_message, @system_message)
|
33
33
|
else
|
34
34
|
raise "Unsupported model: #{settings[:model]}"
|
35
35
|
end
|
data/lib/lammy/embeddings.rb
CHANGED
@@ -15,13 +15,13 @@ module L
|
|
15
15
|
|
16
16
|
# Redefine the method
|
17
17
|
define_method(method_name) do |*args, &block|
|
18
|
-
# Initialize chunking settings
|
19
|
-
@chunk_by_size = nil
|
18
|
+
# # Initialize chunking settings
|
19
|
+
# @chunk_by_size = nil
|
20
20
|
|
21
|
-
# Make `chunk_by_size` method available within the instance
|
22
|
-
define_singleton_method(:chunk_by_size) do |size|
|
23
|
-
|
24
|
-
end
|
21
|
+
# # Make `chunk_by_size` method available within the instance
|
22
|
+
# define_singleton_method(:chunk_by_size) do |size|
|
23
|
+
# @chunk_by_size = size
|
24
|
+
# end
|
25
25
|
|
26
26
|
# Call the original method to get the input
|
27
27
|
input = original_method.bind(self).call(*args, &block)
|
@@ -37,8 +37,8 @@ module L
|
|
37
37
|
|
38
38
|
case settings[:model]
|
39
39
|
when *OpenAI::EMBEDDINGS
|
40
|
-
client = OpenAI.new
|
41
|
-
client.embeddings(
|
40
|
+
client = OpenAI.new(settings)
|
41
|
+
client.embeddings(input)
|
42
42
|
else
|
43
43
|
raise "Unsupported model: #{settings[:model]}"
|
44
44
|
end
|
data/lib/lammy/openai.rb
CHANGED
@@ -15,25 +15,32 @@ module L
|
|
15
15
|
text-embedding-3-small text-embedding-3-large text-embedding-ada-002
|
16
16
|
].freeze
|
17
17
|
|
18
|
+
attr_reader :settings
|
19
|
+
|
20
|
+
def initialize(settings)
|
21
|
+
@settings = settings
|
22
|
+
end
|
23
|
+
|
18
24
|
# Generate a response with support for structured output
|
19
|
-
def chat(
|
25
|
+
def chat(user_message, system_message = nil)
|
26
|
+
schema = schema(settings)
|
20
27
|
response = client.chat(
|
21
28
|
parameters: {
|
22
|
-
model: settings[:model], response_format: schema
|
29
|
+
model: settings[:model], response_format: schema, messages: [
|
23
30
|
system_message ? { role: :system, content: system_message } : nil,
|
24
31
|
{ role: :user, content: user_message }
|
25
32
|
].compact
|
26
33
|
}.compact
|
27
|
-
)
|
34
|
+
).dig('choices', 0, 'message', 'content')
|
28
35
|
|
29
|
-
content =
|
30
|
-
|
36
|
+
content = schema ? ::Hashie::Mash.new(JSON.parse(response)) : response
|
37
|
+
array?(schema) ? content.items : content
|
31
38
|
end
|
32
39
|
|
33
40
|
# OpenAI’s text embeddings measure the relatedness of text strings. An embedding is a vector of floating point
|
34
41
|
# numbers. The distance between two vectors measures their relatedness. Small distances suggest high relatedness
|
35
42
|
# and large distances suggest low relatedness.
|
36
|
-
def embeddings(
|
43
|
+
def embeddings(chunks)
|
37
44
|
responses = chunks.map do |chunk|
|
38
45
|
response = client.embeddings(
|
39
46
|
parameters: { model: settings[:model], dimensions: settings[:dimensions], input: chunk }
|
@@ -51,15 +58,21 @@ module L
|
|
51
58
|
return unless settings[:schema]
|
52
59
|
|
53
60
|
{
|
54
|
-
type
|
55
|
-
json_schema
|
56
|
-
name
|
57
|
-
schema
|
61
|
+
'type' => 'json_schema',
|
62
|
+
'json_schema' => {
|
63
|
+
'name' => 'schema',
|
64
|
+
'schema' => settings[:schema]
|
58
65
|
}
|
59
66
|
}
|
60
67
|
end
|
61
68
|
|
69
|
+
def array?(schema)
|
70
|
+
schema.is_a?(Hash) && schema.dig('json_schema', 'schema', 'properties', 'items', 'type') == 'array'
|
71
|
+
end
|
72
|
+
|
62
73
|
def client
|
74
|
+
return settings[:client] if settings[:client]
|
75
|
+
|
63
76
|
@client ||= ::OpenAI::Client.new(
|
64
77
|
access_token: ENV.fetch('OPENAI_ACCESS_TOKEN')
|
65
78
|
)
|
data/lib/lammy/schema.rb
CHANGED
@@ -22,7 +22,7 @@ module L
|
|
22
22
|
def to_h(object)
|
23
23
|
{
|
24
24
|
'type' => 'object',
|
25
|
-
"properties": object.inject({}) { |h, (k, v)| h.merge(k => { 'type' => v }) },
|
25
|
+
"properties": object.inject({}) { |h, (k, v)| h.merge(k.to_s => { 'type' => v.to_s }) },
|
26
26
|
"required": object.keys,
|
27
27
|
"additionalProperties": false
|
28
28
|
}
|
data/lib/lammy.rb
CHANGED
@@ -5,43 +5,6 @@ require 'lammy/openai'
|
|
5
5
|
require 'lammy/schema'
|
6
6
|
require 'lammy/chat'
|
7
7
|
|
8
|
-
# Example:
|
9
|
-
#
|
10
|
-
# ```ruby
|
11
|
-
# class User < ApplicationRecord
|
12
|
-
# include L
|
13
|
-
#
|
14
|
-
# llm(model: 'gpt-4o')
|
15
|
-
# def welcome
|
16
|
-
# context "You are a helpful assistant that writes in lower case."
|
17
|
-
# "Say hello to #{name.reverse} with a poem."
|
18
|
-
# end
|
19
|
-
#
|
20
|
-
# v(model: 'text-embedding-3-large')
|
21
|
-
# def embeddings
|
22
|
-
# chunk_by_size 256
|
23
|
-
# welcome
|
24
|
-
# end
|
25
|
-
# end
|
26
|
-
#
|
27
|
-
# user = User.new(name: 'John Doe')
|
28
|
-
# user.welcome
|
29
|
-
#
|
30
|
-
# # => "hello eoD nhoJ, let's make a cheer,\n
|
31
|
-
# # with a whimsical poem to bring you near.\n
|
32
|
-
# # though your name's in reverse, it’s clear and bright,\n
|
33
|
-
# # let's dance in verse on this delightful night.\n\n
|
34
|
-
# #
|
35
|
-
# # to a friend unique, in every single way,\n
|
36
|
-
# # we flip the letters but the bond will stay.\n
|
37
|
-
# # the sun may set and rise again,\n
|
38
|
-
# # with you, the fun will never wane.\n\n
|
39
|
-
# #
|
40
|
-
# # through twists and turns, in backwards flow,\n
|
41
|
-
# # we celebrate you in this poetic show.\n
|
42
|
-
# # eoD nhoJ, here's a cheer to you,\n
|
43
|
-
# # in every form, our friendship's true!"
|
44
|
-
# ```
|
45
8
|
module L
|
46
9
|
extend Schema
|
47
10
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lammy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kamil Nicieja
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-10-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|