net-llm 0.1.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 +7 -0
- data/CHANGELOG.md +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +95 -0
- data/Rakefile +8 -0
- data/lib/net/llm/version.rb +7 -0
- data/lib/net/llm.rb +38 -0
- data/sig/net/llm.rbs +6 -0
- metadata +51 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 28baf2a76420a22be944c1a46aab05d8dfceda575bbb9f341260e592a9e7711f
|
4
|
+
data.tar.gz: f746ecda882d6f08336753fd45319aa28d133c36f43fea9334e83564735d11cd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fb6e7aebda0fbb74e06892f2eacc6812c428922743e5130ef5fde3e5ef235bbb9156a73234fdd57be04afba3ef9d62d438345dfd044a8db03798ef17b41344e4
|
7
|
+
data.tar.gz: 61d7e73ea24484b1c7115ae19f192579370f66e925fd80c1337a6e94ef54af16b05d465d3a83a8d5a6fcff45eaac01d38d73ceaed8ff0973db6ccaa3787b264d
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 mo khan
|
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.md
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# Net::Llm
|
2
|
+
|
3
|
+
A minimal Ruby gem providing interfaces to connect to OpenAI, Ollama, and Anthropic (Claude) LLM APIs.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
```bash
|
10
|
+
bundle add net-llm
|
11
|
+
```
|
12
|
+
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
gem install net-llm
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### OpenAI
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'net/llm'
|
25
|
+
|
26
|
+
client = Net::Llm::OpenAI.new(
|
27
|
+
api_key: ENV['OPENAI_API_KEY'],
|
28
|
+
model: 'gpt-4o-mini'
|
29
|
+
)
|
30
|
+
|
31
|
+
messages = [
|
32
|
+
{ role: 'user', content: 'Hello!' }
|
33
|
+
]
|
34
|
+
|
35
|
+
response = client.chat(messages, [])
|
36
|
+
puts response.dig('choices', 0, 'message', 'content')
|
37
|
+
```
|
38
|
+
|
39
|
+
#### Custom Base URL
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
client = Net::Llm::OpenAI.new(
|
43
|
+
api_key: ENV['OPENAI_API_KEY'],
|
44
|
+
base_url: 'https://custom-openai-compatible-api.com/v1',
|
45
|
+
model: 'gpt-4o-mini'
|
46
|
+
)
|
47
|
+
```
|
48
|
+
|
49
|
+
#### With Tools
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
tools = [
|
53
|
+
{
|
54
|
+
type: 'function',
|
55
|
+
function: {
|
56
|
+
name: 'get_weather',
|
57
|
+
description: 'Get current weather',
|
58
|
+
parameters: {
|
59
|
+
type: 'object',
|
60
|
+
properties: {
|
61
|
+
location: { type: 'string' }
|
62
|
+
},
|
63
|
+
required: ['location']
|
64
|
+
}
|
65
|
+
}
|
66
|
+
}
|
67
|
+
]
|
68
|
+
|
69
|
+
response = client.chat(messages, tools)
|
70
|
+
```
|
71
|
+
|
72
|
+
## API Coverage
|
73
|
+
|
74
|
+
### OpenAI
|
75
|
+
- `/v1/chat/completions` (with tools support)
|
76
|
+
|
77
|
+
### Ollama
|
78
|
+
Coming soon
|
79
|
+
|
80
|
+
### Anthropic (Claude)
|
81
|
+
Coming soon
|
82
|
+
|
83
|
+
## Development
|
84
|
+
|
85
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
86
|
+
|
87
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
88
|
+
|
89
|
+
## Contributing
|
90
|
+
|
91
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/xlgmokha/net-llm.
|
92
|
+
|
93
|
+
## License
|
94
|
+
|
95
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/net/llm.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "llm/version"
|
4
|
+
|
5
|
+
module Net
|
6
|
+
module Llm
|
7
|
+
class Error < StandardError; end
|
8
|
+
DEFAULT_TIMEOUT = 60 * 2
|
9
|
+
|
10
|
+
class OpenAI
|
11
|
+
attr_reader :api_key, :base_url, :model
|
12
|
+
|
13
|
+
def initialize(api_key:, base_url: "https://api.openai.com/v1", model: "gpt-4o-mini")
|
14
|
+
@api_key = api_key
|
15
|
+
@base_url = base_url
|
16
|
+
@model = model
|
17
|
+
end
|
18
|
+
|
19
|
+
def chat(messages, tools, timeout: DEFAULT_TIMEOUT)
|
20
|
+
uri = URI("#{base_url}/chat/completions")
|
21
|
+
request = Net::HTTP::Post.new(uri)
|
22
|
+
request["Authorization"] = "Bearer #{api_key}"
|
23
|
+
request["Content-Type"] = "application/json"
|
24
|
+
request.body = { model: model, messages: messages, tools: tools, tool_choice: "auto" }.to_json
|
25
|
+
|
26
|
+
http = Net::HTTP.new(uri.hostname, uri.port)
|
27
|
+
http.use_ssl = true
|
28
|
+
http.open_timeout = timeout
|
29
|
+
http.read_timeout = timeout
|
30
|
+
http.write_timeout = timeout if http.respond_to?(:write_timeout=)
|
31
|
+
|
32
|
+
response = http.start { |h| h.request(request) }
|
33
|
+
raise "HTTP #{response.code}: #{response.body}" unless response.is_a?(Net::HTTPSuccess)
|
34
|
+
JSON.parse(response.body)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/sig/net/llm.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: net-llm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- mo khan
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies: []
|
12
|
+
description: A minimal Ruby gem providing interfaces to connect to OpenAI, Ollama,
|
13
|
+
and Anthropic (Claude) LLM APIs
|
14
|
+
email:
|
15
|
+
- mo@mokhan.ca
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- CHANGELOG.md
|
21
|
+
- LICENSE.txt
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
24
|
+
- lib/net/llm.rb
|
25
|
+
- lib/net/llm/version.rb
|
26
|
+
- sig/net/llm.rbs
|
27
|
+
homepage: https://github.com/xlgmokha/net-llm
|
28
|
+
licenses:
|
29
|
+
- MIT
|
30
|
+
metadata:
|
31
|
+
homepage_uri: https://github.com/xlgmokha/net-llm
|
32
|
+
source_code_uri: https://github.com/xlgmokha/net-llm
|
33
|
+
changelog_uri: https://github.com/xlgmokha/net-llm/blob/main/CHANGELOG.md
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 3.2.0
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubygems_version: 3.7.2
|
49
|
+
specification_version: 4
|
50
|
+
summary: Ruby client for OpenAI, Ollama, and Anthropic LLM APIs
|
51
|
+
test_files: []
|