lammy 0.4.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: bab0a835c9ce12e44b4c5f0d4252eb3040e0d5cb3336aeefe24856f59a74a2a9
4
- data.tar.gz: a627a3d18b19a86785a1c70d95304b5bc8ab820766faf15f7893a79d563aeb44
3
+ metadata.gz: 656f2074be0687f9996946697d7bc3ea5286b384749113521012bb061f88f914
4
+ data.tar.gz: de4f1f7c8ec35448807230777ee4a5e75a3eacb7d66c88978296d5f30bf38efd
5
5
  SHA512:
6
- metadata.gz: 0cd66977d719fb4bf752b96c9d5fcaeb1d776fda9d3f929366b4ac41a9913f2e16de6cb1454f0116214f1a7b404b815506f3e0375ac781f5ef2304aa01e54b6c
7
- data.tar.gz: 8c39fea57e23ce2c39a926b039c01e132482237a5c9e6af4fd1bd9de20b884845f05930be747a202f5a3982fe22bbf2f3e308f9f605d934c9897b57b095c72c0
6
+ metadata.gz: 1dc35a49b4139d03baf530930d6157023a8b6e4eed36fdf26ad9c042a4e8c8d87ee812330aef2c047e9f6f45d4240a6c5c74abd1fc223ca011a6cc32fe1fdfff
7
+ data.tar.gz: 9066b3c808e63abc01822ec355bcf49dd19aa5632c5b68640ddba5d33fd951c05f12da14ea600049d9bdbe66f3a8ad803f3e6194ea6a114080887ee4178a2a4d
data/lib/lammy/chat.rb CHANGED
@@ -18,7 +18,7 @@ module L
18
18
  # Initialize context
19
19
  @system_message = nil
20
20
 
21
- # Make `context` method available within the instance
21
+ # `context` sets the system message and is available within the instance
22
22
  define_singleton_method(:context) do |message|
23
23
  @system_message = message
24
24
  end
data/lib/lammy/openai.rb CHANGED
@@ -24,12 +24,13 @@ module L
24
24
  # Generate a response with support for structured output
25
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.4.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