lammy 0.1.0 → 0.2.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/embeddings.rb +6 -6
- data/lib/lammy/openai.rb +14 -8
- 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: '0992b774797e59eeae152cacb65110b83cec70002e6afb7a40153ae05f942490'
|
4
|
+
data.tar.gz: d2680e0acfc73a28f89e5468a9d4292cd2a4bc9573a5570b9c40b86e3ffa8a14
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee429f696a2dfcf9f79582db94bdf696c6b4900f903880daddc48d32d9ea13c60a76138b2e47dc47d9c9efb9ed94196af1221827fbcd3c0b9e5b2384d74b2611
|
7
|
+
data.tar.gz: d44d1b6161a805fce9f5da44ac481b909c1edfccd50eb74a61c3498e55642da5782637b1105c07408594de46b1162f5ac7004bdca6bc03caccac4ac48f190f28
|
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)
|
data/lib/lammy/openai.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'openai'
|
4
|
+
require 'hashie'
|
4
5
|
|
5
6
|
module L
|
6
7
|
# Use the OpenAI API's official Ruby library
|
@@ -16,17 +17,18 @@ module L
|
|
16
17
|
|
17
18
|
# Generate a response with support for structured output
|
18
19
|
def chat(settings, user_message, system_message = nil)
|
20
|
+
schema = schema(settings)
|
19
21
|
response = client.chat(
|
20
22
|
parameters: {
|
21
|
-
model: settings[:model], response_format: schema
|
23
|
+
model: settings[:model], response_format: schema, messages: [
|
22
24
|
system_message ? { role: :system, content: system_message } : nil,
|
23
25
|
{ role: :user, content: user_message }
|
24
26
|
].compact
|
25
27
|
}.compact
|
26
|
-
)
|
28
|
+
).dig('choices', 0, 'message', 'content')
|
27
29
|
|
28
|
-
content =
|
29
|
-
|
30
|
+
content = schema ? ::Hashie::Mash.new(JSON.parse(response)) : response
|
31
|
+
array?(schema) ? content.items : content
|
30
32
|
end
|
31
33
|
|
32
34
|
# OpenAI’s text embeddings measure the relatedness of text strings. An embedding is a vector of floating point
|
@@ -50,14 +52,18 @@ module L
|
|
50
52
|
return unless settings[:schema]
|
51
53
|
|
52
54
|
{
|
53
|
-
type
|
54
|
-
json_schema
|
55
|
-
name
|
56
|
-
schema
|
55
|
+
'type' => 'json_schema',
|
56
|
+
'json_schema' => {
|
57
|
+
'name' => 'schema',
|
58
|
+
'schema' => settings[:schema]
|
57
59
|
}
|
58
60
|
}
|
59
61
|
end
|
60
62
|
|
63
|
+
def array?(schema)
|
64
|
+
schema.is_a?(Hash) && schema.dig('json_schema', 'schema', 'properties', 'items', 'type') == 'array'
|
65
|
+
end
|
66
|
+
|
61
67
|
def client
|
62
68
|
@client ||= ::OpenAI::Client.new(
|
63
69
|
access_token: ENV.fetch('OPENAI_ACCESS_TOKEN')
|
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.2.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
|