llm_client 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 777149fdf2ef683ec6195bde6e601df1f7cb5c63636118549e20e7d523b622ed
4
- data.tar.gz: 69fb1cf7477115727676d83cb1ff6e4ea17728a1cfb349a42c5e2deddf8b8d3b
3
+ metadata.gz: 8f72195e4042de9a5acd98101df86e73ce0749393436b54064a102c0b461e4c2
4
+ data.tar.gz: b1bf65a1e8f668824d1e0953697fe937001ce7345917974ca17ac0d3d9ac146c
5
5
  SHA512:
6
- metadata.gz: c15ac237dec34b0848f3d6024709d3eb1125e406ad3012fbf3d1ea772d623872dbc57de5cd2c3c63fbf40665c05e7d795c25dbb35c7c346454ea4063e56503aa
7
- data.tar.gz: d6698e4feb0dfbaf72bfd644186a5a7a90b4e8ac94fdd087392647f54d9997ceb15b25efba178f7b2a6fefa85c6fb4a62af8dfb8e1b9b16daabdd9d42fdfad70
6
+ metadata.gz: 4779414c25ddf22a382e0cd09c9185ebef4bcd846e9f65254a897ff33c75f5cc97612e2240cd2b339e643e46de0748a391b61c03c14caf46c41da623bd480370
7
+ data.tar.gz: de8db072e375ba200cdb3eccb62c0147224e9b48cfeedc05a459e1c2f49e8e01244ee0c63978b90b8c832a93241d3efc688e97b2f29058510b934d3f9f13c3e8
data/.standard.yml CHANGED
@@ -5,3 +5,4 @@ ruby_version: 3.2.2 # default: RUBY_VERSION
5
5
  default_ignores: false # default: true
6
6
 
7
7
  ignore: # default: []
8
+ - 'vendor/**/*'
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.2] - 2023-06-19
4
+
5
+ - Adds missing Ruby Next files to gem
6
+
7
+
8
+ ## [0.1.1] - 2023-06-19
9
+
10
+ - Fix to load properly RubyNext transpilated code for older Ruby versions
11
+
3
12
  ## [0.1.0] - 2023-06-19
4
13
 
5
14
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- llm_client (0.1.0)
4
+ llm_client (0.1.2)
5
5
  dry-monads (~> 1.6)
6
6
  http (~> 5.1, >= 5.1.1)
7
7
  ruby-next (>= 0.15.0)
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LlmClient
4
+ # Request's response Data object.
5
+ #
6
+ # status - HTTP status code. It is zero when a request fails due to network error.
7
+ # body - Parsed JSON object or response body.
8
+ # headers - HTTP response headers.
9
+ # error - Exception if the response is not success
10
+ Response = Struct.new("Response", :status, :body, :headers, :error)
11
+
12
+ # Request class to perform HTTP requests.
13
+ class HttpClient
14
+ using RubyNext
15
+ include Dry::Monads[:result]
16
+
17
+ # Sends a heartbeat request to the LLM Server.
18
+ #
19
+ # This method is used to check the status of the LLM Server.
20
+ #
21
+ # Returns a dry-monad Result object, which can be a Success or Failure.
22
+ # If the server is running and responds successfully, a Success Result is returned.
23
+ # If there's an error or the server is not responding, a Failure Result is returned.
24
+ #
25
+ # Examples
26
+ #
27
+ # result = heartbeat
28
+ # if result.success?
29
+ # puts "Server is running"
30
+ # response = result.success
31
+ # puts "Status: #{response.status}"
32
+ # puts "Body: #{response.body}"
33
+ # puts "Headers: #{response.headers}"
34
+ # else
35
+ # puts "Server is not responding"
36
+ # error = result.failure
37
+ # puts "Error: #{error}"
38
+ # end
39
+ #
40
+ # Notes
41
+ #
42
+ # - Make sure the LLM Server is running before calling this method.
43
+ #
44
+ def heartbeat
45
+ url = "#{LlmClient.host}/heartbeat"
46
+ Util.log_debug("Sending a request", method: :GET, url: url)
47
+
48
+ response = HTTP.get(url)
49
+
50
+ if response.status.success?
51
+ Util.log_info("Successful response", status: response.status)
52
+ Success(build_response(response))
53
+ else
54
+ Util.log_info("Unsuccessful response", status: response.status)
55
+ Failure(build_response(response))
56
+ end
57
+ rescue => exception
58
+ Util.log_info("heartbeat error", error: exception.message)
59
+
60
+ error = LlmClient::ConnectionError.new("heartbeat error", exception)
61
+ Failure(Response.new(status: 0, body: error.message, headers: {}, error: error))
62
+ end
63
+
64
+ # Public: Sends a completion request to the LLM Server.
65
+ #
66
+ # This method is used to generate completions based on a given prompt using the LLM Server.
67
+ #
68
+ # Parameters:
69
+ # prompt - A String representing the prompt for which completions should be generated.
70
+ #
71
+ # Returns a dry-monad Result object, which can be a Success or Failure.
72
+ # If the completion request is successful, a Success Result is returned.
73
+ # If there's an error or the server fails to generate completions, a Failure Result is returned.
74
+ #
75
+ # Examples
76
+ #
77
+ # result = completion("Hello, world!")
78
+ # if result.success?
79
+ # puts "Completions generated successfully"
80
+ # response = result.success
81
+ # puts "Status: #{response.status}"
82
+ # puts "Body: #{response.body}"
83
+ # puts "Headers: #{response.headers}"
84
+ # calculated_response = response.body[:response]
85
+ # puts "Calculated Response: #{calculated_response}"
86
+ # else
87
+ # puts "Failed to generate completions"
88
+ # error = result.failure
89
+ # puts "Error: #{error}"
90
+ # end
91
+ #
92
+ # Notes
93
+ #
94
+ # - Make sure the LLM Server is running before calling this method.
95
+ #
96
+ def completion(prompt)
97
+ url = "#{LlmClient.host}/completion"
98
+ Util.log_debug("Sending a request", method: :POST, url: url)
99
+
100
+ response = HTTP.headers(
101
+ "Content-Type" => "application/json",
102
+ "Accept" => "application/json"
103
+ ).post(url, json: {prompt: prompt})
104
+
105
+ if response.status.success?
106
+ Util.log_info("Successful response", status: response.status)
107
+ Success(build_response(response, parse: true))
108
+ else
109
+ Util.log_info("Unsuccessful response", status: response.status)
110
+ Failure(build_response(response))
111
+ end
112
+ rescue => exception
113
+ Util.log_info("prompt error", error: exception.message)
114
+
115
+ error = LlmClient::ConnectionError.new("prompt error", exception)
116
+ Failure(Response.new(status: 0, body: error.message, headers: {}, error: error))
117
+ end
118
+
119
+ private
120
+
121
+ def build_response(response, parse: false)
122
+ Response.new(
123
+ status: response.status,
124
+ body: parse ? parse_json(response.body.to_s) : response.body.to_s,
125
+ headers: response.headers.to_h,
126
+ error: nil
127
+ )
128
+ end
129
+
130
+ def parse_json(content)
131
+ JSON.parse(content, symbolize_keys: true)
132
+ rescue JSON::ParserError, TypeError
133
+ content
134
+ end
135
+ end
136
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LlmClient
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/llm_client.rb CHANGED
@@ -10,11 +10,11 @@ require "ruby-next/language/setup"
10
10
 
11
11
  RubyNext::Language.setup_gem_load_path(transpile: true)
12
12
 
13
- require_relative "llm_client/util"
14
- require_relative "llm_client/configuration"
15
- require_relative "llm_client/errors"
16
- require_relative "llm_client/http_client"
17
- require_relative "llm_client/version"
13
+ require "llm_client/util"
14
+ require "llm_client/configuration"
15
+ require "llm_client/errors"
16
+ require "llm_client/http_client"
17
+ require "llm_client/version"
18
18
 
19
19
  # A module representing an LLM client.
20
20
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: llm_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Alberto Chávez
@@ -90,6 +90,7 @@ files:
90
90
  - LICENSE.txt
91
91
  - README.md
92
92
  - Rakefile
93
+ - lib/.rbnext/3.1/llm_client/http_client.rb
93
94
  - lib/llm_client.rb
94
95
  - lib/llm_client/configuration.rb
95
96
  - lib/llm_client/errors.rb