lammy 0.1.1 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b017f8a1db36acfcb44ade375afb444be57a91950d2b8fdb9b4540c7a19d2a0
4
- data.tar.gz: 98702338c1b30893351da07ffb3cd25c3b91ab0411536a67a782ffcd52dea16b
3
+ metadata.gz: 9225fc482f95ed862d6e12bc4541731c8854127900e0bfe369075ef1092fae8e
4
+ data.tar.gz: 720ce8849858edb0832c34b0b10ef736c6353b12daa1e5cecf68c137de94a91d
5
5
  SHA512:
6
- metadata.gz: 21363a7704447fbfec6b7d6a38ab199d907b35a7512f0ede712ee26ac3f641ee159ad4f3379a91d303d7dcea029c8923bfde7ce66d50fbad738800053165c8dc
7
- data.tar.gz: a26916678096f4660d0abbb540655bd9b6d570c57bddfb01799b3595a5d1cf74ec8da51f8d15f837c33708fd3af48fd2212ce7218ffef05a1bceb46d2a833674
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(settings, user_message, @system_message)
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
@@ -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
- @chunk_by_size = size
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(settings, input)
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(settings, user_message, system_message = nil)
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(settings), messages: [
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 = response.dig('choices', 0, 'message', 'content')
30
- settings[:schema] ? ::Hashie::Mash.new(JSON.parse(content)) : content
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(settings, chunks)
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: :json_schema,
55
- json_schema: {
56
- name: :schema,
57
- schema: settings[: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.1.1
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-09-30 00:00:00.000000000 Z
11
+ date: 2024-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie