Skip to content

Commit 3c93af8

Browse files
author
Nicholas Newberry
committed
add ApiCalls for streamed responses
1 parent caa2e1b commit 3c93af8

4 files changed

Lines changed: 50 additions & 5 deletions

File tree

app/lib/chromadb/client.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ def get(path)
108108
raise ResponseError, @last_response.body
109109
end
110110

111+
store_api_call("chromadb", @last_response)
112+
111113
@last_response.parsed_response
112114
end
113115

@@ -119,6 +121,8 @@ def post(path, body = {})
119121
raise ResponseError, @last_response.body
120122
end
121123

124+
store_api_call("chromadb", @last_response)
125+
122126
@last_response.parsed_response
123127
end
124128

@@ -130,7 +134,13 @@ def delete(path)
130134
raise ResponseError, @last_response.body
131135
end
132136

137+
store_api_call("chromadb", @last_response)
138+
133139
@last_response.parsed_response
134140
end
141+
142+
def store_api_call(service_name, response)
143+
ApiCall.from_httparty(service_name, response).save!
144+
end
135145
end
136146
end

app/lib/llm_clients/client.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ def with_retries
127127
raise exception
128128
end
129129

130-
def store_api_call(service_name, request, response)
130+
def store_api_call(service_name, request, response, response_body = nil)
131+
response.body = response_body if response_body
131132
ApiCall.from_net_http(service_name, request, response, @traceable).save!
132133
end
133134
end

app/lib/llm_clients/ollama/client.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ def helper
3131
def stream(request, &block)
3232
@stats = new_stats
3333

34+
response_contents = ""
35+
3436
Rails.logger.info("Sending request body:\n#{request.body}")
3537
# TODO: switch to HTTParty for this?
36-
Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: @uri.scheme == "https") do |http|
38+
# TODO: create ApiCall early and update response body after streaming is done
39+
full_response = Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: @uri.scheme == "https") do |http|
3740
stats[:start_time] = current_time
3841
http.request(request) do |response|
3942
raise response_error_for(response) unless response.is_a?(Net::HTTPSuccess)
@@ -57,6 +60,7 @@ def stream(request, &block)
5760

5861
if current_batch_size == @batch_size
5962
Rails.logger.debug "==> #{current_batch}"
63+
response_contents << current_batch
6064
yield current_batch
6165

6266
current_batch_size = 0
@@ -66,12 +70,17 @@ def stream(request, &block)
6670

6771
if current_batch_size > 0
6872
Rails.logger.debug "==> #{current_batch}"
73+
response_contents << current_batch
6974
yield current_batch
7075
end
7176

7277
calculate_stats
7378
end
7479
end
80+
81+
store_api_call("ollama", request, full_response, response_contents)
82+
83+
full_response
7584
end
7685

7786
def request(request)

app/models/api_call.rb

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,28 @@ class ApiCall < ApplicationRecord
44
enum http_method: { get: 0, post: 1, put: 2, delete: 3, patch: 4, head: 5 }, _prefix: :http_method
55

66
class << self
7-
# there's no way to resolve AbcSize that doesn't make the code ugly
87
# rubocop: disable Metrics/AbcSize
9-
def from_net_http(service_name, request, response, traceable)
8+
def from_httparty(service_name, response, traceable = nil)
9+
request = response.request
10+
11+
api_call = new(
12+
service_name:,
13+
http_method: http_method_from_httparty(request),
14+
url: request.uri.to_s,
15+
headers: request.options[:headers],
16+
body: json_body(request.raw_body),
17+
body_length: request.raw_body&.length,
18+
response_code: response.code,
19+
response_headers: response.headers,
20+
response_body: json_body(response.body),
21+
response_length: response.body.length
22+
)
23+
24+
api_call.traceable = traceable if traceable
25+
api_call
26+
end
27+
28+
def from_net_http(service_name, request, response, traceable = nil)
1029
api_call = new(
1130
service_name:,
1231
http_method: request.method.downcase,
@@ -28,9 +47,15 @@ def from_net_http(service_name, request, response, traceable)
2847
private
2948

3049
def json_body(body)
50+
return if body.nil?
51+
3152
JSON.parse(body)
32-
rescue JSON::ParserError
53+
rescue JSON::ParserError, TypeError
3354
body
3455
end
56+
57+
def http_method_from_httparty(request)
58+
request.http_method.name.split("::").last.downcase
59+
end
3560
end
3661
end

0 commit comments

Comments
 (0)