lammy 0.5.0 → 0.6.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: dddd7ebf694f48f5d75ff062990bdec4f4e2c1188af7f958fe12a6856928c103
4
- data.tar.gz: ea361dc698c42d8c8c37318e0e98300d7fb416e31de53d2b4f3ab05882bf480d
3
+ metadata.gz: 656f2074be0687f9996946697d7bc3ea5286b384749113521012bb061f88f914
4
+ data.tar.gz: de4f1f7c8ec35448807230777ee4a5e75a3eacb7d66c88978296d5f30bf38efd
5
5
  SHA512:
6
- metadata.gz: c925d41bfa4a2b66867e43e081187f76f0143775d1e2ac0044b6a243fc3aadcaec13499b9678cbc891b076ae50c9b8612999d183c987b77ae99838658f4f222d
7
- data.tar.gz: dcfb1071b3eb78ad66e1151b2d04d4b58581d576ff211f48d2126d9d52af991abbb387bb5cdc36c925efca9ca990a8be1bf1d19767ac5b4dd5aa9d904c997077
6
+ metadata.gz: 1dc35a49b4139d03baf530930d6157023a8b6e4eed36fdf26ad9c042a4e8c8d87ee812330aef2c047e9f6f45d4240a6c5c74abd1fc223ca011a6cc32fe1fdfff
7
+ data.tar.gz: 9066b3c808e63abc01822ec355bcf49dd19aa5632c5b68640ddba5d33fd951c05f12da14ea600049d9bdbe66f3a8ad803f3e6194ea6a114080887ee4178a2a4d
data/lib/lammy/chat.rb CHANGED
@@ -17,18 +17,12 @@ module L
17
17
  define_method(method_name) do |*args, &block|
18
18
  # Initialize context
19
19
  @system_message = nil
20
- @prefilled_message = nil
21
20
 
22
21
  # `context` sets the system message and is available within the instance
23
22
  define_singleton_method(:context) do |message|
24
23
  @system_message = message
25
24
  end
26
25
 
27
- # `prefill` sets the prefilled message and is available within the instance
28
- define_singleton_method(:prefill) do |message|
29
- @prefilled_message = message
30
- end
31
-
32
26
  # Call the original method to get the user message
33
27
  user_message = original_method.bind(self).call(*args, &block)
34
28
 
@@ -41,7 +35,7 @@ module L
41
35
  raise "Unsupported model: #{settings[:model]}"
42
36
  end
43
37
 
44
- client.chat(user_message, @system_message, @prefilled_message)
38
+ client.chat(user_message, @system_message)
45
39
  end
46
40
  end
47
41
  end
data/lib/lammy/openai.rb CHANGED
@@ -22,14 +22,15 @@ module L
22
22
  end
23
23
 
24
24
  # Generate a response with support for structured output
25
- def chat(user_message, system_message = nil, _ = nil)
25
+ def chat(user_message, system_message = nil)
26
26
  schema = schema(settings)
27
+ messages = messages(user_message, system_message)
28
+
27
29
  response = client.chat(
28
30
  parameters: {
29
- model: settings[:model], response_format: schema, messages: [
30
- system_message ? { role: :system, content: system_message } : nil,
31
- { role: :user, content: user_message }
32
- ].compact
31
+ model: settings[:model],
32
+ response_format: schema,
33
+ messages: messages
33
34
  }.compact
34
35
  ).dig('choices', 0, 'message', 'content')
35
36
 
@@ -66,6 +67,15 @@ module L
66
67
  }
67
68
  end
68
69
 
70
+ def messages(user_message, system_message)
71
+ return user_message if user_message.is_a?(Array)
72
+
73
+ [
74
+ system_message ? L.system(system_message) : nil,
75
+ L.user(user_message)
76
+ ].compact
77
+ end
78
+
69
79
  def array?(schema)
70
80
  schema.is_a?(Hash) && schema.dig('json_schema', 'schema', 'properties', 'items', 'type') == 'array'
71
81
  end
data/lib/lammy/schema.rb CHANGED
@@ -1,11 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module L
4
- # Structured Outputs is a feature that ensures the model will always generate responses
5
- # that adhere to your supplied JSON Schema, so you don't need to worry about the model
6
- # omitting a required key, or hallucinating an invalid enum value. This is a set of
7
- # helper methods to help you define your JSON Schema easily.
8
4
  module Schema
5
+ def system(content)
6
+ { role: :system, content: content }
7
+ end
8
+
9
+ def user(content)
10
+ { role: :user, content: content }
11
+ end
12
+
13
+ def assistant(content)
14
+ { role: :assistant, content: content }
15
+ end
16
+
17
+ # Structured Outputs is a feature that ensures the model will always generate responses
18
+ # that adhere to your supplied JSON Schema, so you don't need to worry about the model
19
+ # omitting a required key, or hallucinating an invalid enum value. This is a set of
20
+ # helper methods to help you define your JSON Schema easily.
9
21
  def to_a(object)
10
22
  {
11
23
  'type' => 'object',
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.5.0
4
+ version: 0.6.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-10-03 00:00:00.000000000 Z
11
+ date: 2024-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: anthropic