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 +4 -4
- data/README.md +63 -36
- data/lib/autoflux/openai/client.rb +10 -1
- data/lib/autoflux/openai/version.rb +1 -1
- data/lib/autoflux/openai.rb +3 -0
- data/sig/autoflux/openai/client.rbs +1 -0
- data/sig/autoflux/openai.rbs +9 -0
- metadata +2 -6
- data/.husky/commit-msg +0 -1
- data/.husky/pre-commit +0 -1
- data/package-lock.json +0 -1259
- data/package.json +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9c7f1d47432e8221ec33239f799ebeb0f4ed37026d01b4479b0bda40bfd207a
|
4
|
+
data.tar.gz: 5a16cb407af4dfc3fe49b76a526343bc2497520bb079e1ae46a2a5bda7158ae9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
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
|
-
|
73
|
+
{ text: params[:text].upcase }
|
72
74
|
end
|
73
75
|
|
74
76
|
agent = Autoflux::OpenAI::Agent.new(
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
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
|
-
|
93
|
-
|
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
|
-
|
103
|
-
|
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
|
-
|
107
|
-
|
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
|
-
|
118
|
-
|
119
|
-
|
144
|
+
def initialize
|
145
|
+
@store = []
|
146
|
+
end
|
120
147
|
|
121
|
-
|
122
|
-
|
123
|
-
|
148
|
+
def push(message)
|
149
|
+
@store.push(message)
|
150
|
+
end
|
124
151
|
|
125
|
-
|
126
|
-
|
127
|
-
|
152
|
+
def <<(message)
|
153
|
+
push(message)
|
154
|
+
end
|
128
155
|
|
129
|
-
|
130
|
-
|
131
|
-
|
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
|
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"
|
data/lib/autoflux/openai.rb
CHANGED
@@ -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"
|
data/sig/autoflux/openai.rbs
CHANGED
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.
|
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-
|
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
|