raix 0.4.4 → 0.4.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/Gemfile.lock +1 -1
- data/README.md +115 -0
- data/lib/raix/chat_completion.rb +1 -0
- data/lib/raix/predicate.rb +2 -2
- data/lib/raix/prompt_declarations.rb +1 -0
- data/lib/raix/response_format.rb +1 -0
- data/lib/raix/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2ae6f2ed473737e2d208868c2bdd458e12d391ebaae3d0930439267badc4d63
|
4
|
+
data.tar.gz: a93f7985ba809218e2fb7883ab31f0b0d246a769bbc7071fd6d60412bae5fd8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ae8374160c7ffe8f26db849a357b4c3403c58222cee941c90921e1961f1185d191111486b040b07af36c0a8aa54dd0721b11c6128cc2326c19be900a2a2154d
|
7
|
+
data.tar.gz: fa398a36ce224f3f8805f57b95378a5a7c589a45a4bf73f016ace860f7aa24be4680e168f4b51c6cc4dfedf3f80851073350a476779e1a7aeef0a2fd5e75283a
|
data/CHANGELOG.md
CHANGED
@@ -21,3 +21,13 @@
|
|
21
21
|
|
22
22
|
## [0.4.3] - 2024-11-11
|
23
23
|
- adds support for `Predicate` module
|
24
|
+
|
25
|
+
## [0.4.4] - 2024-11-11
|
26
|
+
- adds support for multiple tool calls in a single response
|
27
|
+
|
28
|
+
## [0.4.5] - 2024-11-11
|
29
|
+
- adds support for `ResponseFormat`
|
30
|
+
- added some missing requires to support String#squish
|
31
|
+
|
32
|
+
## [0.4.6] - 2024-11-12
|
33
|
+
- adds missing openai support for `Predicate`
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -101,6 +101,30 @@ RSpec.describe WhatIsTheWeather do
|
|
101
101
|
end
|
102
102
|
```
|
103
103
|
|
104
|
+
#### Multiple Tool Calls
|
105
|
+
|
106
|
+
Some AI models (like GPT-4) can make multiple tool calls in a single response. When this happens, Raix will automatically handle all the function calls sequentially and return an array of their results. Here's an example:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
class MultipleToolExample
|
110
|
+
include Raix::ChatCompletion
|
111
|
+
include Raix::FunctionDispatch
|
112
|
+
|
113
|
+
function :first_tool do |arguments|
|
114
|
+
"Result from first tool"
|
115
|
+
end
|
116
|
+
|
117
|
+
function :second_tool do |arguments|
|
118
|
+
"Result from second tool"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
example = MultipleToolExample.new
|
123
|
+
example.transcript << { user: "Please use both tools" }
|
124
|
+
results = example.chat_completion(openai: "gpt-4o")
|
125
|
+
# => ["Result from first tool", "Result from second tool"]
|
126
|
+
```
|
127
|
+
|
104
128
|
#### Manually Stopping a Loop
|
105
129
|
|
106
130
|
To loop AI components that don't interact with end users, at least one function block should invoke `stop_looping!` whenever you're ready to stop processing.
|
@@ -309,6 +333,97 @@ question.ask("Any question")
|
|
309
333
|
# => RuntimeError: Please define a yes and/or no block
|
310
334
|
```
|
311
335
|
|
336
|
+
## Response Format (Experimental)
|
337
|
+
|
338
|
+
The `ResponseFormat` class provides a way to declare a JSON schema for the response format of an AI chat completion. It's particularly useful when you need structured responses from AI models, ensuring the output conforms to your application's requirements.
|
339
|
+
|
340
|
+
### Features
|
341
|
+
|
342
|
+
- Converts Ruby hashes and arrays into JSON schema format
|
343
|
+
- Supports nested structures and arrays
|
344
|
+
- Enforces strict validation with `additionalProperties: false`
|
345
|
+
- Automatically marks all top-level properties as required
|
346
|
+
- Handles both simple type definitions and complex nested schemas
|
347
|
+
|
348
|
+
### Basic Usage
|
349
|
+
|
350
|
+
```ruby
|
351
|
+
# Simple schema with basic types
|
352
|
+
format = Raix::ResponseFormat.new("PersonInfo", {
|
353
|
+
name: { type: "string" },
|
354
|
+
age: { type: "integer" }
|
355
|
+
})
|
356
|
+
|
357
|
+
# Use in chat completion
|
358
|
+
my_ai.chat_completion(response_format: format)
|
359
|
+
```
|
360
|
+
|
361
|
+
### Complex Structures
|
362
|
+
|
363
|
+
```ruby
|
364
|
+
# Nested structure with arrays
|
365
|
+
format = Raix::ResponseFormat.new("CompanyInfo", {
|
366
|
+
company: {
|
367
|
+
name: { type: "string" },
|
368
|
+
employees: [
|
369
|
+
{
|
370
|
+
name: { type: "string" },
|
371
|
+
role: { type: "string" },
|
372
|
+
skills: ["string"]
|
373
|
+
}
|
374
|
+
],
|
375
|
+
locations: ["string"]
|
376
|
+
}
|
377
|
+
})
|
378
|
+
```
|
379
|
+
|
380
|
+
### Generated Schema
|
381
|
+
|
382
|
+
The ResponseFormat class generates a schema that follows this structure:
|
383
|
+
|
384
|
+
```json
|
385
|
+
{
|
386
|
+
"type": "json_schema",
|
387
|
+
"json_schema": {
|
388
|
+
"name": "SchemaName",
|
389
|
+
"schema": {
|
390
|
+
"type": "object",
|
391
|
+
"properties": {
|
392
|
+
"property1": { "type": "string" },
|
393
|
+
"property2": { "type": "integer" }
|
394
|
+
},
|
395
|
+
"required": ["property1", "property2"],
|
396
|
+
"additionalProperties": false
|
397
|
+
},
|
398
|
+
"strict": true
|
399
|
+
}
|
400
|
+
}
|
401
|
+
```
|
402
|
+
|
403
|
+
### Using with Chat Completion
|
404
|
+
|
405
|
+
When used with chat completion, the AI model will format its response according to your schema:
|
406
|
+
|
407
|
+
```ruby
|
408
|
+
class StructuredResponse
|
409
|
+
include Raix::ChatCompletion
|
410
|
+
|
411
|
+
def analyze_person(name)
|
412
|
+
format = Raix::ResponseFormat.new("PersonAnalysis", {
|
413
|
+
full_name: { type: "string" },
|
414
|
+
age_estimate: { type: "integer" },
|
415
|
+
personality_traits: ["string"]
|
416
|
+
})
|
417
|
+
|
418
|
+
transcript << { user: "Analyze the person named #{name}" }
|
419
|
+
chat_completion(response_format: format)
|
420
|
+
end
|
421
|
+
end
|
422
|
+
|
423
|
+
response = StructuredResponse.new.analyze_person("Alice")
|
424
|
+
# Returns a hash matching the defined schema
|
425
|
+
```
|
426
|
+
|
312
427
|
## Installation
|
313
428
|
|
314
429
|
Install the gem and add to the application's Gemfile by executing:
|
data/lib/raix/chat_completion.rb
CHANGED
data/lib/raix/predicate.rb
CHANGED
@@ -27,13 +27,13 @@ module Raix
|
|
27
27
|
base.extend(ClassMethods)
|
28
28
|
end
|
29
29
|
|
30
|
-
def ask(question)
|
30
|
+
def ask(question, openai: false)
|
31
31
|
raise "Please define a yes and/or no block" if self.class.yes_block.nil? && self.class.no_block.nil?
|
32
32
|
|
33
33
|
transcript << { system: "Always answer 'Yes, ', 'No, ', or 'Maybe, ' followed by a concise explanation!" }
|
34
34
|
transcript << { user: question }
|
35
35
|
|
36
|
-
chat_completion.tap do |response|
|
36
|
+
chat_completion(openai:).tap do |response|
|
37
37
|
if response.downcase.start_with?("yes,")
|
38
38
|
instance_exec(response, &self.class.yes_block) if self.class.yes_block
|
39
39
|
elsif response.downcase.start_with?("no,")
|
data/lib/raix/response_format.rb
CHANGED
data/lib/raix/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: raix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Obie Fernandez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|