llmclt 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e66ad7b12be7dca2e764807c310f9eaae3b73034c39484d9e271237d1407d9e8
4
+ data.tar.gz: 0c48358585d5143392dac374d15d19c30a73fd2b6b32043b32f0cffce661c7fd
5
+ SHA512:
6
+ metadata.gz: c58507035a6516fa009c3bd46cc9ffb4cbe986a4f15226e3583b18b8377b049b53443d20b8c41fd2dedffd080c57aff5dd0385c3a260475d72854db4983745b8
7
+ data.tar.gz: dab595d5bfacb0982260700eb2f000685f8eae5da59c5382f87ec0861f343b1bb29780bb434b3c9169317c91d31591a435e890ec5670838a7dfb4d349055524d
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,44 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+ - rubocop-performance
5
+
6
+ inherit_from: .rubocop_todo.yml
7
+
8
+ AllCops:
9
+ TargetRubyVersion: 2.7
10
+ NewCops: enable
11
+ Exclude:
12
+ - 'log/**/*'
13
+ - 'tmp/**/*'
14
+ - 'vendor/**/*'
15
+
16
+ Gemspec/RequireMFA:
17
+ Enabled: false
18
+
19
+ Style/AccessorGrouping:
20
+ Enabled: false
21
+
22
+ Style/ClassAndModuleChildren:
23
+ Enabled: false
24
+
25
+ Style/Documentation:
26
+ Enabled: false
27
+
28
+ Style/StringLiterals:
29
+ EnforcedStyle: single_quotes
30
+
31
+ Style/StringLiteralsInInterpolation:
32
+ EnforcedStyle: single_quotes
33
+
34
+ Metrics/BlockLength:
35
+ Enabled: false
36
+
37
+ Metrics/ClassLength:
38
+ Max: 150
39
+
40
+ Metrics/MethodLength:
41
+ Max: 30
42
+
43
+ RSpec/MultipleMemoizedHelpers:
44
+ Enabled: false
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,7 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2025-01-14 02:35:52 UTC using RuboCop version 1.70.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # CHANGELOG
2
+
3
+ ## 1.0.0
4
+
5
+ - First release.
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at mirasi1h@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 MasatoMiyoshi
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,124 @@
1
+ # Llmclt
2
+
3
+ A simple client for [Gemini API](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/inference).
4
+
5
+ For an authentication, this gem uses a service account json file.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'llmclt'
13
+ ```
14
+
15
+ And then run:
16
+
17
+ ```
18
+ $ bundle install
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```shell
24
+ $ gem install llmclt
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ Use as follows:
30
+
31
+ ```ruby
32
+ client = Llmclt::Client.new(
33
+ project_id: project_id,
34
+ location_id: location_id,
35
+ model: model,
36
+ service_account_json: service_account_json
37
+ )
38
+
39
+ prompt = 'Hello. This is Tom!. How are you?'
40
+
41
+ response = client.request(prompt)
42
+ response.text
43
+
44
+ client.shutdown
45
+ ```
46
+
47
+ ### System instruction
48
+
49
+ ```ruby
50
+ client = Llmclt::Client.new(
51
+ project_id: project_id,
52
+ location_id: location_id,
53
+ model: model,
54
+ service_account_json: service_account_json,
55
+ system_instruction_prompt: 'You are a helpful assistant.'
56
+ )
57
+
58
+ prompt = 'Hello. This is Tom!. How are you?'
59
+
60
+ response = client.request(prompt)
61
+ response.text
62
+
63
+ client.shutdown
64
+ ```
65
+
66
+ ### Stream mode
67
+
68
+ ```ruby
69
+ response = client.request(prompt, stream: true)
70
+ response.text
71
+ ```
72
+
73
+ ### Multi turn
74
+
75
+ ```ruby
76
+ histories = [
77
+ { role: 'user', text: 'Hello! This is Tom.' },
78
+ { role: 'model', text: 'Hello, Tom. Can I help you?' }
79
+ ]
80
+ prompt = 'Do you know my name?'
81
+ response = client.request(prompt, histories: histories)
82
+ response.text
83
+ ```
84
+
85
+ ### Configuration
86
+
87
+ ```ruby
88
+ client = Llmclt::Client.new(
89
+ project_id: project_id,
90
+ location_id: location_id,
91
+ model: model,
92
+ service_account_json: service_account_json,
93
+ safety_settings: [
94
+ {
95
+ category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
96
+ threshold: 'BLOCK_LOW_AND_ABOVE'
97
+ }
98
+ ],
99
+ generation_config: {
100
+ temperature: 1.0,
101
+ top_p: 0.95
102
+ }
103
+ )
104
+ ```
105
+
106
+ Refer [the API references](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/inference) for configuration.
107
+
108
+ ## Development
109
+
110
+ 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.
111
+
112
+ 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).
113
+
114
+ ## Contributing
115
+
116
+ Bug reports and pull requests are welcome on GitHub at https://github.com/MasatoMiyoshi/llmclt. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/MasatoMiyoshi/llmclt/blob/main/CODE_OF_CONDUCT.md).
117
+
118
+ ## License
119
+
120
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
121
+
122
+ ## Code of Conduct
123
+
124
+ Everyone interacting in the Llmclt project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/MasatoMiyoshi/llmclt/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Llmclt
4
+ class Client
5
+ def initialize(**kwargs)
6
+ @config = Llmclt::Config.new(**kwargs)
7
+ @request = Llmclt::Request.new(@config)
8
+ end
9
+
10
+ def request(prompt, stream: false, histories: [])
11
+ @request.run(prompt, stream: stream, histories: histories)
12
+ end
13
+
14
+ def shutdown
15
+ @request.shutdown
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Llmclt
4
+ class Config
5
+ class GenerationConfig
6
+ # Generation configuration parameters
7
+ # temperature: number
8
+ # topP: number
9
+ # topK: number
10
+ # candidateCount: integer
11
+ # maxOutputTokens: integer
12
+ # presencePenalty: float
13
+ # frequencyPenalty: float
14
+ # stopSequences: [
15
+ # string
16
+ # ]
17
+ # responseMimeType: string
18
+ # responseSchema: schema
19
+ # seed: integer
20
+ # responseLogprobs: boolean
21
+ # logprobs: integer
22
+ # audioTimestamp: boolean
23
+
24
+ PARAMETERS = %i[
25
+ temperature
26
+ top_p
27
+ top_k
28
+ candidate_count
29
+ max_output_tokens
30
+ presence_penalty
31
+ frequency_penalty
32
+ stop_sequences
33
+ response_mime_type
34
+ response_schema
35
+ seed
36
+ response_logprobs
37
+ logprobs
38
+ audio_timestamp
39
+ ].freeze
40
+
41
+ def initialize(setting = {})
42
+ @setting = setting
43
+ end
44
+
45
+ def build_request_content
46
+ return {} if @setting.nil? || @setting.empty?
47
+
48
+ config = PARAMETERS.each_with_object({}) do |param, cfg|
49
+ p = param.to_s.camelize(:lower).to_sym
50
+ cfg[p] = @setting[param] unless @setting[param].nil?
51
+ end
52
+
53
+ { generationConfig: config }
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Llmclt
4
+ class Config
5
+ class SafetyConfig
6
+ # SafetySettings parameters
7
+ # category: enum
8
+ # HARM_CATEGORY_SEXUALLY_EXPLICIT
9
+ # HARM_CATEGORY_HATE_SPEECH
10
+ # HARM_CATEGORY_HARASSMENT
11
+ # HARM_CATEGORY_DANGEROUS_CONTENT
12
+ # threshold: enum
13
+ # OFF
14
+ # BLOCK_NONE
15
+ # BLOCK_LOW_AND_ABOVE
16
+ # BLOCK_MEDIUM_AND_ABOVE
17
+ # BLOCK_ONLY_HIGH
18
+
19
+ PARAMETERS = %i[category threshold].freeze
20
+
21
+ def initialize(settings = [])
22
+ @settings = settings
23
+ end
24
+
25
+ def build_request_content
26
+ return {} if @settings.nil? || @settings.empty?
27
+
28
+ {
29
+ safetySettings: @settings.map do |setting|
30
+ PARAMETERS.each_with_object({}) do |param, cfg|
31
+ p = param.to_s.camelize(:lower).to_sym
32
+ cfg[p] = setting[param] unless setting[param].nil?
33
+ end
34
+ end
35
+ }
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Llmclt
4
+ class Config
5
+ DEFAULT_CONFIGS = {
6
+ project_id: nil,
7
+ location_id: 'asia-northeast1',
8
+ model: 'gemini-1.5-pro-001',
9
+ service_account_json: nil,
10
+ system_instruction_prompt: nil,
11
+ open_timeout: 60,
12
+ read_timeout: 60,
13
+ keep_alive_timeout: 15
14
+ }.freeze
15
+
16
+ attr_accessor(*DEFAULT_CONFIGS.keys)
17
+ attr_accessor :safety_config
18
+ attr_accessor :generation_config
19
+
20
+ def initialize(**kwargs)
21
+ DEFAULT_CONFIGS.each do |key, value|
22
+ send(:"#{key}=", kwargs[key] || value)
23
+ end
24
+
25
+ self.safety_config = Llmclt::Config::SafetyConfig.new(kwargs[:safety_settings])
26
+ self.generation_config = Llmclt::Config::GenerationConfig.new(kwargs[:generation_config])
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Llmclt
4
+ class Http
5
+ class Connection
6
+ attr_accessor :name, :http, :last_use
7
+
8
+ def initialize(**kwargs)
9
+ @name = kwargs[:name]
10
+ @http = kwargs[:http]
11
+ @last_use = kwargs[:last_use]
12
+ end
13
+
14
+ def finish
15
+ http.finish if http&.started?
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Llmclt
4
+ class Http
5
+ class ConnectionPool
6
+ def initialize(max_size = 10)
7
+ @max_size = max_size
8
+ end
9
+
10
+ def checkout(name)
11
+ pool[name] ||= Connection.new(name: name, http: yield)
12
+
13
+ conn = pool[name]
14
+ conn.last_use = Time.now
15
+
16
+ reduce if pool.size > @max_size
17
+
18
+ conn.http
19
+ end
20
+
21
+ def shutdown
22
+ pool.each_value(&:finish)
23
+ Thread.current[:llmclt_connection] = nil
24
+ end
25
+
26
+ private
27
+
28
+ def reduce
29
+ conns = pool.values.sort_by(&:last_use)
30
+ conns.first(pool.size - @max_size).each do |conn|
31
+ conn.finish
32
+ pool.delete(conn.name)
33
+ end
34
+ end
35
+
36
+ def pool
37
+ Thread.current[:llmclt_connection] ||= {}
38
+ Thread.current[:llmclt_connection]
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Llmclt
4
+ class Request
5
+ def initialize(config)
6
+ @config = config
7
+ @pool = Llmclt::Http::ConnectionPool.new
8
+ end
9
+
10
+ def run(prompt, stream: false, histories: [])
11
+ uri = request_uri(stream)
12
+ request = build_request(uri, prompt, histories)
13
+ http = checkout_http(uri)
14
+ response = http.start { |h| h.request(request) }
15
+ Llmclt::Response.new(response.body)
16
+ end
17
+
18
+ def shutdown
19
+ @pool.shutdown
20
+ end
21
+
22
+ private
23
+
24
+ def request_uri(stream)
25
+ if stream
26
+ URI.parse("#{endpoint_url}:streamGenerateContent")
27
+ else
28
+ URI.parse("#{endpoint_url}:generateContent")
29
+ end
30
+ end
31
+
32
+ def endpoint_url
33
+ "https://#{@config.location_id}-aiplatform.googleapis.com/v1/projects/#{@config.project_id}/locations/#{@config.location_id}/publishers/google/models/#{@config.model}"
34
+ end
35
+
36
+ def request_headers
37
+ authorizer.apply({ 'Content-Type': 'application/json' })
38
+ end
39
+
40
+ def authorizer
41
+ if @authorizer.nil? || @authorizer.expires_within?(60)
42
+ @authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
43
+ json_key_io: StringIO.new(@config.service_account_json),
44
+ scope: 'https://www.googleapis.com/auth/cloud-platform'
45
+ )
46
+ @authorizer.fetch_access_token!
47
+ end
48
+ @authorizer
49
+ end
50
+
51
+ def checkout_http(uri)
52
+ @pool.checkout(pool_name(uri)) { build_http(uri) }
53
+ end
54
+
55
+ def pool_name(uri)
56
+ "#{uri.scheme}_#{uri.host}_#{uri.port || uri.default_port}"
57
+ end
58
+
59
+ def build_http(uri)
60
+ http = Net::HTTP.new(uri.host, uri.port)
61
+ http.open_timeout = @config.open_timeout
62
+ http.read_timeout = @config.read_timeout
63
+ http.keep_alive_timeout = @config.keep_alive_timeout
64
+ http.use_ssl = true
65
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
66
+ http
67
+ end
68
+
69
+ def build_request(uri, prompt, histories)
70
+ request = Net::HTTP::Post.new(uri.request_uri)
71
+ request_headers.each do |key, value|
72
+ request[key] = value
73
+ end
74
+ request.body = build_request_json(prompt, histories)
75
+ request
76
+ end
77
+
78
+ def build_request_json(prompt, histories)
79
+ req = {}
80
+ req.merge!(build_request_contents(prompt, histories))
81
+ req.merge!(build_request_system_instruction)
82
+ req.to_json
83
+ end
84
+
85
+ def build_request_contents(prompt, histories)
86
+ content = { contents: [] }
87
+ content.merge!(@config.safety_config.build_request_content)
88
+ content.merge!(@config.generation_config.build_request_content)
89
+ content[:contents].concat(build_request_history_contents(histories))
90
+ content[:contents] << build_request_user_prompt(prompt)
91
+ content
92
+ end
93
+
94
+ def build_request_history_contents(histories)
95
+ histories.map do |history|
96
+ {
97
+ role: history[:role],
98
+ parts: [{ text: history[:text] }]
99
+ }
100
+ end
101
+ end
102
+
103
+ def build_request_user_prompt(prompt)
104
+ {
105
+ role: 'user',
106
+ parts: [
107
+ {
108
+ text: prompt
109
+ }
110
+ ]
111
+ }
112
+ end
113
+
114
+ def build_request_system_instruction
115
+ return {} if @config.system_instruction_prompt.nil? || @config.system_instruction_prompt.empty?
116
+
117
+ {
118
+ systemInstruction: {
119
+ parts: [
120
+ {
121
+ text: @config.system_instruction_prompt
122
+ }
123
+ ]
124
+ }
125
+ }
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Llmclt
4
+ class Response
5
+ attr_reader :response
6
+
7
+ def initialize(response_json)
8
+ @response = JSON.parse(response_json)
9
+ end
10
+
11
+ def text
12
+ texts.join
13
+ end
14
+
15
+ def texts
16
+ candidates = responses.map { |res| res['candidates'] }.flatten
17
+ parts = candidates.map { |candidate| candidate['content']['parts'] }.flatten
18
+ parts.map { |part| part['text'] }
19
+ end
20
+
21
+ def stream?
22
+ response.is_a?(Array)
23
+ end
24
+
25
+ private
26
+
27
+ def responses
28
+ stream? ? response : [response]
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Llmclt
4
+ VERSION = '1.0.0'
5
+ end
data/lib/llmclt.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/inflections'
4
+ require 'googleauth'
5
+
6
+ require_relative 'llmclt/config'
7
+ require_relative 'llmclt/config/safety_config'
8
+ require_relative 'llmclt/config/generation_config'
9
+ require_relative 'llmclt/client'
10
+ require_relative 'llmclt/request'
11
+ require_relative 'llmclt/http/connection'
12
+ require_relative 'llmclt/http/connection_pool'
13
+ require_relative 'llmclt/response'
14
+ require_relative 'llmclt/version'
15
+
16
+ module Llmclt
17
+ class Error < StandardError; end
18
+ # Your code goes here...
19
+ end
data/sig/llmclt.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Llmclt
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: llmclt
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - MasatoMiyoshi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: googleauth
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A simple client for Gemini API
42
+ email:
43
+ - miyoshi@sitebridge.co.jp
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".rubocop.yml"
50
+ - ".rubocop_todo.yml"
51
+ - CHANGELOG.md
52
+ - CODE_OF_CONDUCT.md
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - lib/llmclt.rb
57
+ - lib/llmclt/client.rb
58
+ - lib/llmclt/config.rb
59
+ - lib/llmclt/config/generation_config.rb
60
+ - lib/llmclt/config/safety_config.rb
61
+ - lib/llmclt/http/connection.rb
62
+ - lib/llmclt/http/connection_pool.rb
63
+ - lib/llmclt/request.rb
64
+ - lib/llmclt/response.rb
65
+ - lib/llmclt/version.rb
66
+ - sig/llmclt.rbs
67
+ homepage: https://github.com/MasatoMiyoshi/llmclt
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ homepage_uri: https://github.com/MasatoMiyoshi/llmclt
72
+ source_code_uri: https://github.com/MasatoMiyoshi/llmclt
73
+ changelog_uri: https://github.com/MasatoMiyoshi/llmclt/CHANGELOG.md
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 2.7.0
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.5.11
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: A simple client for Gemini API
93
+ test_files: []