llm_client 0.1.0 → 0.1.2
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 +4 -4
- data/.standard.yml +1 -0
- data/CHANGELOG.md +9 -0
- data/Gemfile.lock +1 -1
- data/lib/.rbnext/3.1/llm_client/http_client.rb +136 -0
- data/lib/llm_client/version.rb +1 -1
- data/lib/llm_client.rb +5 -5
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f72195e4042de9a5acd98101df86e73ce0749393436b54064a102c0b461e4c2
|
4
|
+
data.tar.gz: b1bf65a1e8f668824d1e0953697fe937001ce7345917974ca17ac0d3d9ac146c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4779414c25ddf22a382e0cd09c9185ebef4bcd846e9f65254a897ff33c75f5cc97612e2240cd2b339e643e46de0748a391b61c03c14caf46c41da623bd480370
|
7
|
+
data.tar.gz: de8db072e375ba200cdb3eccb62c0147224e9b48cfeedc05a459e1c2f49e8e01244ee0c63978b90b8c832a93241d3efc688e97b2f29058510b934d3f9f13c3e8
|
data/.standard.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -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
|
data/lib/llm_client/version.rb
CHANGED
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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.
|
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
|