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