Class: GlimResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/glim_response.rb

Overview

A response is always either available or a request has been sent

Direct Known Subclasses

AnthropicResponse, ChatResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(req) ⇒ GlimResponse

Returns a new instance of GlimResponse.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/glim_response.rb', line 15

def initialize(req)
  @req = req
  log_request_hash
  @time_request_sent = Time.now
  @cached_response = _look_for_cached_response
  if req.use_cached_response && @cached_response
    putt :cache, "Using cached response for key: #{req.cache_key}"
    @raw_response = cached_response.with_indifferent_access
    log_raw_response
    process_response_from_api
    log_summary_append
  else
    if @cached_reaponse 
      putt :cache, "Making API call because req.use_cached_response was #{req.use_cached_response}, even though cached response was found for key: #{req.cache_key}"
    else
      putt :cache, "Making API call because no cached response was found for key: #{req.cache_key}"
    end
    @cached_response = false
    putt :rpc, "RPC to #{req}"
    async_send_request_to_api
    # now the request has been send. the user's thread can do more stuff until the 
    # user eventually looks at this response, which will block if we haven't heard back.
  end
end

Instance Attribute Details

#cached_responseObject (readonly)

Returns the value of attribute cached_response.



86
87
88
# File 'lib/glim_response.rb', line 86

def cached_response
  @cached_response
end

#paramsObject (readonly)

Returns the value of attribute params.



13
14
15
# File 'lib/glim_response.rb', line 13

def params
  @params
end

#reqObject (readonly)

Returns the value of attribute req.



13
14
15
# File 'lib/glim_response.rb', line 13

def req
  @req
end

Instance Method Details

#[](key) ⇒ Object



144
145
146
147
# File 'lib/glim_response.rb', line 144

def [](key)
  wait_for_response
  @raw_response[key]
end

#_look_for_cached_responseObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/glim_response.rb', line 74

def _look_for_cached_response
  cache_file = File.join(CACHE_PATH, "#{req.cache_key}.json")
  me = self.class.name
  if File.exist?(cache_file)
    putt :cache, "Cached #{me} found for key: #{req.cache_key}"
    return (JSON.parse(File.read(cache_file)).with_indifferent_access)
  else
    putt :cache, "No cached #{me} found for key: #{req.cache_key}"
    return nil
  end
end

#completionObject



149
150
151
152
# File 'lib/glim_response.rb', line 149

def completion
  wait_for_response
  @completion
end

#contextObject



63
64
65
# File 'lib/glim_response.rb', line 63

def context
  req.context
end

#err(msg) ⇒ Object

Raises:



134
135
136
# File 'lib/glim_response.rb', line 134

def err(msg)
  raise GlimResponseError.new(raw_response, msg)
end

#log_completionObject



59
60
61
# File 'lib/glim_response.rb', line 59

def log_completion
  req.save_log_file("completion.txt", completion)
end

#log_raw_responseObject



55
56
57
# File 'lib/glim_response.rb', line 55

def log_raw_response
  req.save_log_file("raw_response.json", JSON.pretty_generate(raw_response))
end

#log_request_hashObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/glim_response.rb', line 40

def log_request_hash
  s = "# request_hash generated by #{req.class} with #{req.request_details.class}\n"
  s += JSON.pretty_generate(req.request_hash)
  req.save_log_file("request_hash.json", s)

  
  # template_text = File.read('lib/chat_request.erb')
  # template = ERB.new(template_text)
  #s = template.result_with_hash(request_hash: req.request_hash)
  #req.save_log_file("request_hash.html",s)



end

#log_summary_appendObject



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/glim_response.rb', line 104

def log_summary_append
  templ = req.template_name ? req.template_name+".erb" : "no_template"
  time_spent = Time.now - @time_request_sent
  tps = completion_tokens / time_spent
  s = "#{responding_llm_name}, #{templ}, #{prompt_tokens}, prompt_tokens + , #{completion_tokens}, completion_tokens = $"
  if cached_response
    s += ",0,0,cached, , , "
  else
    s += ",#{total_cost}, #{time_spent.round(3)}, seconds, #{tps}, tokens/s"
  end
  s += ", #{req.log_base_this_request}"
  context.log_line_to_summary(s)
end

#raw_responseObject

the raw response as received from OpenAI



139
140
141
142
# File 'lib/glim_response.rb', line 139

def raw_response
  wait_for_response
  @raw_response
end

#responding_llm_nameObject



92
93
94
# File 'lib/glim_response.rb', line 92

def responding_llm_name
  raise "implement in subclass!"
end

#response_available?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/glim_response.rb', line 88

def response_available?
  @thread.status == false
end

#save_raw_response_to_cacheObject

caching



68
69
70
71
72
# File 'lib/glim_response.rb', line 68

def save_raw_response_to_cache
  putt :cache, "Saving response to cache for key: #{req.cache_key}"
  cache_file = File.join(CACHE_PATH, "#{req.cache_key}.json")
  File.write(cache_file, raw_response.to_json)
end

#total_costObject



100
101
102
# File 'lib/glim_response.rb', line 100

def total_cost
  prompt_tokens * req.cost_per_prompt_token + completion_tokens * req.cost_per_completion_token
end

#total_tokensObject



96
97
98
# File 'lib/glim_response.rb', line 96

def total_tokens
  prompt_tokens + completion_tokens
end

#wait_for_responseObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/glim_response.rb', line 119

def wait_for_response
  return if @raw_response
  raise "No Thread! Call run() first!" unless @thread
  if response_available?
    putt :rpc, "Response already received from #{req.to_s}"
  else
    putt :rpc, "Will now block until we have a for #{req.to_s}"
  end
  @raw_response ||= (@thread.value.must_be_a Hash).with_indifferent_access
  save_raw_response_to_cache unless req.no_cache
  log_raw_response
  process_response_from_api
  log_summary_append
end