autoflux-openai 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9f6a06620fc9aaad94651a79c50f43e0e6620c0170195e3f6e0e7b37e7e1a5bc
4
- data.tar.gz: cf620fa918dcbb690a78c09ac53b9f77d2e4a76db0c9703d0563743e2ad6fc01
3
+ metadata.gz: b9c7f1d47432e8221ec33239f799ebeb0f4ed37026d01b4479b0bda40bfd207a
4
+ data.tar.gz: 5a16cb407af4dfc3fe49b76a526343bc2497520bb079e1ae46a2a5bda7158ae9
5
5
  SHA512:
6
- metadata.gz: b9873032436f3cbb76471d7de41fe03b25580e7b5a850bbc11660c51ab30c9be69ed65379deb1937a8e38bbf0cc4230daed0ef092430ffbdf8c288e2d09b52c1
7
- data.tar.gz: 5fd593e5c8cf1c4ae12b14fdd2e8bafb1d853f06195d4ca00d9bf800de6e4fbadf471133fa7e58ef1e5b6aeb9aafbac5bd09ca46b47956a7b0617a70aa892d83
6
+ metadata.gz: 28e472dc22a1ae7406ee6d614770dd3089db0ac3aec1a3e91caa75f83e7e34646b41f2334416ceefefac1901b828ab0073115451fa9ad98c2e9fe4011ae51cb3
7
+ data.tar.gz: 7145feb76a8d631ebfca05481ace81046d23776048bda5fe5d0f4460fa703a29dd29812983f6e4d5bd19c8b997d2ad89f3f2702829f95b713daafd9789cb5ace
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  Autoflux::OpenAI
2
2
  ===
3
3
 
4
+ [![Ruby](https://github.com/elct9620/autoflux-openai/actions/workflows/main.yml/badge.svg)](https://github.com/elct9620/autoflux-openai/actions/workflows/main.yml) [![codecov](https://codecov.io/gh/elct9620/autoflux-openai/graph/badge.svg?token=VW9V0FU009)](https://codecov.io/gh/elct9620/autoflux-openai)
5
+
4
6
  This gem implements [autoflux](https://github.com/elct9620/autoflux) agent to use OpenAI as the backend.
5
7
 
6
8
  ## Installation
@@ -41,9 +43,9 @@ The agent use same parameters to call the OpenAI API. You can customize it when
41
43
 
42
44
  ```ruby
43
45
  agent = Autoflux::OpenAI::Agent.new(
44
- model: "gpt-4o-mini", # Required
45
- # Extra parameters
46
- temperature: 0.5,
46
+ model: "gpt-4o-mini", # Required
47
+ # Extra parameters
48
+ temperature: 0.5,
47
49
  )
48
50
  res = agent.call("Hello, world!")
49
51
  # => "Hello, world!" from OpenAI
@@ -57,31 +59,56 @@ You can attach tool to the agent to give it more capabilities.
57
59
 
58
60
  ```ruby
59
61
  uppercase = Autoflux::OpenAI::Tool.new(
60
- name: "uppercase",
61
- description: "Convert the content to uppercase",
62
- parameters: {
63
- type: "object",
64
- properties: {
65
- text: {
66
- type: "string"
67
- }
68
- }
62
+ name: "uppercase",
63
+ description: "Convert the content to uppercase",
64
+ parameters: {
65
+ type: "object",
66
+ properties: {
67
+ text: {
68
+ type: "string"
69
+ }
69
70
  }
71
+ }
70
72
  ) do |params|
71
- { text: params[:text].upcase }
73
+ { text: params[:text].upcase }
72
74
  end
73
75
 
74
76
  agent = Autoflux::OpenAI::Agent.new(
75
- model: "gpt-4o-mini",
76
- tools: [uppercase],
77
- memory: [
78
- { role: "system", content: "Always transform the user input and don't do anything else." }
79
- ]
77
+ model: "gpt-4o-mini",
78
+ tools: [uppercase],
79
+ memory: [
80
+ { role: "system", content: "Always transform the user input and don't do anything else." }
81
+ ]
80
82
  )
81
83
  res = agent.call("Hello, world!")
82
84
  # => "HELLO, WORLD!" from OpenAI
83
85
  ```
84
86
 
87
+ The tool is an object has a `name`, `description`, `parameters`, and a `#call` method. The `#call` method will be called with the parameters from the agent.
88
+
89
+ ```ruby
90
+ class MyTool
91
+ attr_reader :name, :description, :parameters
92
+
93
+ def initialize
94
+ @name = "my-tool" # Must be string which can be lookup by the agent
95
+ @description = "My tool"
96
+ @parameters = {
97
+ type: "object",
98
+ properties: {
99
+ text: {
100
+ type: "string"
101
+ }
102
+ }
103
+ }
104
+ end
105
+
106
+ def call(params, **context)
107
+ { text: params[:text].upcase }
108
+ end
109
+ end
110
+ ```
111
+
85
112
  ### Client
86
113
 
87
114
  This gem embeds a lightweight client to interact with OpenAI API. The client can be used to interact with OpenAI API directly.
@@ -89,8 +116,8 @@ This gem embeds a lightweight client to interact with OpenAI API. The client can
89
116
  ```ruby
90
117
  client = Autoflux::OpenAI::Client.new(api_key: "your-api-key")
91
118
  res = client.call(
92
- model: "gpt-4o-mini",
93
- messages: [{ role: "user", content: "Hello, world!" }]
119
+ model: "gpt-4o-mini",
120
+ messages: [{ role: "user", content: "Hello, world!" }]
94
121
  )
95
122
  # => { choices: [{ message: { role: "assistant", content: "Hello World!" }}] }
96
123
  ```
@@ -99,12 +126,12 @@ If your api key or endpoint is not default, you can specify them in the client.
99
126
 
100
127
  ```ruby
101
128
  client = Autoflux::OpenAI::Client.new(
102
- api_key: "your-api-key",
103
- endpoint: URI("https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai")
129
+ api_key: "your-api-key",
130
+ endpoint: URI("https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai")
104
131
  )
105
132
  client.call(
106
- model: "gpt-4o-mini",
107
- messages: [{ role: "user", content: "Hello, world!" }]
133
+ model: "gpt-4o-mini",
134
+ messages: [{ role: "user", content: "Hello, world!" }]
108
135
  )
109
136
  ```
110
137
 
@@ -114,21 +141,21 @@ The agent default use a Ruby array to store the conversation history. If you wan
114
141
 
115
142
  ```ruby
116
143
  class MyMemory
117
- def initialize
118
- @store = []
119
- end
144
+ def initialize
145
+ @store = []
146
+ end
120
147
 
121
- def push(message)
122
- @store.push(message)
123
- end
148
+ def push(message)
149
+ @store.push(message)
150
+ end
124
151
 
125
- def <<(message)
126
- push(message)
127
- end
152
+ def <<(message)
153
+ push(message)
154
+ end
128
155
 
129
- def to_a
130
- @store.last(100)
131
- end
156
+ def to_a
157
+ @store.last(100)
158
+ end
132
159
  end
133
160
  ```
134
161
 
@@ -16,7 +16,7 @@ module Autoflux
16
16
 
17
17
  def call(payload)
18
18
  res = http.post(@endpoint.path || "", payload.to_json, headers)
19
- raise Error, res.body unless res.is_a?(Net::HTTPSuccess)
19
+ raise error_of(res), res.body unless res.is_a?(Net::HTTPSuccess)
20
20
 
21
21
  JSON.parse(res.body, symbolize_names: true)
22
22
  rescue JSON::ParserError
@@ -32,6 +32,15 @@ module Autoflux
32
32
  }
33
33
  end
34
34
 
35
+ def error_of(res)
36
+ case res
37
+ when Net::HTTPUnauthorized then AuthoriztionError
38
+ when Net::HTTPBadRequest then BadRequestError
39
+ when Net::HTTPTooManyRequests then RateLimitError
40
+ else Error
41
+ end
42
+ end
43
+
35
44
  def http
36
45
  @http ||= Net::HTTP.new(@endpoint.host || "", @endpoint.port).tap do |http|
37
46
  http.use_ssl = @endpoint.scheme == "https"
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Autoflux
4
4
  module OpenAI
5
- VERSION = "0.3.0"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
@@ -6,6 +6,9 @@ module Autoflux
6
6
  # The lightweight OpenAI agent for Autoflux
7
7
  module OpenAI
8
8
  class Error < StandardError; end
9
+ class AuthoriztionError < Error; end
10
+ class BadRequestError < Error; end
11
+ class RateLimitError < Error; end
9
12
 
10
13
  require_relative "openai/client"
11
14
  require_relative "openai/agent"
@@ -13,6 +13,7 @@ module Autoflux
13
13
  def call: (::_ToJson payload) -> Hash[Symbol, untyped]
14
14
  def headers: () -> Hash[String, String]
15
15
  def http: () -> Net::HTTP
16
+ def error_of: (Net::HTTPResponse res) -> _Exception
16
17
  end
17
18
  end
18
19
  end
@@ -4,5 +4,14 @@ module Autoflux
4
4
 
5
5
  class Error < StandardError
6
6
  end
7
+
8
+ class AuthoriztionError < Error
9
+ end
10
+
11
+ class BadRequestError < Error
12
+ end
13
+
14
+ class RateLimitError < Error
15
+ end
7
16
  end
8
17
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autoflux-openai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aotokitsuruya
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-01-12 00:00:00.000000000 Z
11
+ date: 2025-01-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: The OpenAI agent for Autoflux
14
14
  email:
@@ -17,8 +17,6 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - ".husky/commit-msg"
21
- - ".husky/pre-commit"
22
20
  - ".rspec"
23
21
  - ".rubocop.yml"
24
22
  - LICENSE.txt
@@ -31,8 +29,6 @@ files:
31
29
  - lib/autoflux/openai/client.rb
32
30
  - lib/autoflux/openai/tool.rb
33
31
  - lib/autoflux/openai/version.rb
34
- - package-lock.json
35
- - package.json
36
32
  - sig/autoflux/openai.rbs
37
33
  - sig/autoflux/openai/agent.rbs
38
34
  - sig/autoflux/openai/client.rbs
data/.husky/commit-msg DELETED
@@ -1 +0,0 @@
1
- npx --no -- commitlint --verbose --edit $1
data/.husky/pre-commit DELETED
@@ -1 +0,0 @@
1
- bundle exec rake