raix-openai-eight 1.0.1
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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +53 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +168 -0
- data/CLAUDE.md +13 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +24 -0
- data/Gemfile.lock +240 -0
- data/Guardfile +72 -0
- data/LICENSE.txt +21 -0
- data/README.llm +106 -0
- data/README.md +775 -0
- data/Rakefile +18 -0
- data/lib/mcp/sse_client.rb +297 -0
- data/lib/mcp/stdio_client.rb +80 -0
- data/lib/mcp/tool.rb +67 -0
- data/lib/raix/chat_completion.rb +346 -0
- data/lib/raix/configuration.rb +71 -0
- data/lib/raix/function_dispatch.rb +132 -0
- data/lib/raix/mcp.rb +255 -0
- data/lib/raix/message_adapters/base.rb +50 -0
- data/lib/raix/predicate.rb +68 -0
- data/lib/raix/prompt_declarations.rb +166 -0
- data/lib/raix/response_format.rb +81 -0
- data/lib/raix/version.rb +5 -0
- data/lib/raix.rb +27 -0
- data/raix-openai-eight.gemspec +36 -0
- data/sig/raix.rbs +4 -0
- metadata +140 -0
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Obie Fernandez
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.llm
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# Raix (Ruby AI eXtensions)
|
2
|
+
Raix adds LLM-based AI functionality to Ruby classes. It supports OpenAI or OpenRouter as providers and can work in non-Rails apps if you include ActiveSupport.
|
3
|
+
|
4
|
+
## Chat Completion
|
5
|
+
You must include `Raix::ChatCompletion`. It gives you a `transcript` array for messages and a `chat_completion` method that sends them to the AI.
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
class MeaningOfLife
|
9
|
+
include Raix::ChatCompletion
|
10
|
+
end
|
11
|
+
|
12
|
+
ai = MeaningOfLife.new
|
13
|
+
ai.transcript << { user: "What is the meaning of life?" }
|
14
|
+
puts ai.chat_completion
|
15
|
+
```
|
16
|
+
|
17
|
+
You can add messages using either `{ user: "..." }` or `{ role: "user", content: "..." }`.
|
18
|
+
|
19
|
+
### Predicted Outputs
|
20
|
+
Pass `prediction` to support [Predicted Outputs](https://platform.openai.com/docs/guides/latency-optimization#use-predicted-outputs):
|
21
|
+
```ruby
|
22
|
+
ai.chat_completion(openai: "gpt-4o", params: { prediction: "..." })
|
23
|
+
```
|
24
|
+
|
25
|
+
### Prompt Caching
|
26
|
+
When using Anthropic models, you can specify `cache_at`. Messages above that size get sent as ephemeral multipart segments.
|
27
|
+
```ruby
|
28
|
+
ai.chat_completion(params: { cache_at: 1000 })
|
29
|
+
```
|
30
|
+
|
31
|
+
## Function Dispatch
|
32
|
+
Include `Raix::FunctionDispatch` to declare functions AI can call in a chat loop. Use `chat_completion(loop: true)` so the AI can call functions and generate more messages until it outputs a final text response.
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class WhatIsTheWeather
|
36
|
+
include Raix::ChatCompletion
|
37
|
+
include Raix::FunctionDispatch
|
38
|
+
|
39
|
+
function :check_weather, "Check the weather for a location", location: { type: "string" } do |args|
|
40
|
+
"The weather in #{args[:location]} is hot and sunny"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
If the AI calls multiple functions at once, Raix handles them in sequence and returns an array of results. Call `stop_tool_calls_and_respond!` inside a function to end the loop.
|
46
|
+
|
47
|
+
## Prompt Declarations
|
48
|
+
Include `Raix::PromptDeclarations` to define a chain of prompts in order. Each prompt can be inline text or a callable class that also includes `ChatCompletion`.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
class PromptSubscriber
|
52
|
+
include Raix::ChatCompletion
|
53
|
+
include Raix::PromptDeclarations
|
54
|
+
|
55
|
+
prompt call: FetchUrlCheck
|
56
|
+
prompt call: MemoryScan
|
57
|
+
prompt text: -> { user_message.content }
|
58
|
+
|
59
|
+
def message_created(user_message)
|
60
|
+
chat_completion(loop: true, openai: "gpt-4o")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
## Predicate Module
|
66
|
+
Include `Raix::Predicate` to handle yes/no/maybe questions. Define blocks with the `yes?`, `no?`, and `maybe?` methods.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
class Question
|
70
|
+
include Raix::Predicate
|
71
|
+
|
72
|
+
yes? { |explanation| puts "Affirmative: #{explanation}" }
|
73
|
+
no? { |explanation| puts "Negative: #{explanation}" }
|
74
|
+
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
## ResponseFormat (Experimental)
|
79
|
+
Use `Raix::ResponseFormat` to enforce JSON schemas for structured responses.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
format = Raix::ResponseFormat.new("PersonInfo", {
|
83
|
+
name: { type: "string" },
|
84
|
+
age: { type: "integer" }
|
85
|
+
})
|
86
|
+
|
87
|
+
class StructuredResponse
|
88
|
+
include Raix::ChatCompletion
|
89
|
+
|
90
|
+
def analyze_person(name)
|
91
|
+
chat_completion(response_format: format)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
## Installation
|
97
|
+
Add `gem "raix"` to your Gemfile or run `gem install raix`. Configure an OpenRouter or OpenAI client in an initializer:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
# config/initializers/raix.rb
|
101
|
+
Raix.configure do |config|
|
102
|
+
config.openrouter_client = OpenRouter::Client.new
|
103
|
+
end
|
104
|
+
```
|
105
|
+
Make sure you have valid API tokens for your chosen provider.
|
106
|
+
```
|