Class: Hunyuan::ChatClient

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

Constant Summary collapse

HOST =
"hunyuan.cloud.tencent.com"
PATH =
"hyllm/v1/chat/completions"
URL =
"https://#{HOST}/#{PATH}"

Instance Method Summary collapse

Constructor Details

#initialize(app_id = ENV["HUNYUAN_APP_ID"], secret_id = ENV["HUNYUAN_SECRET_ID"], secret_key = ENV["HUNYUAN_SECRET_KEY"]) ⇒ ChatClient

Returns a new instance of ChatClient.



17
18
19
20
21
22
23
# File 'lib/hunyuan.rb', line 17

def initialize(app_id = ENV["HUNYUAN_APP_ID"],
               secret_id = ENV["HUNYUAN_SECRET_ID"],
               secret_key = ENV["HUNYUAN_SECRET_KEY"])
  @app_id = app_id
  @secret_id = secret_id
  @secret_key = secret_key
end

Instance Method Details

#chat(messages) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hunyuan.rb', line 25

def chat(messages)
  request = build_request(messages)
  signature = generate_signature(sign_params(request))

  headers = {
    "Content-Type" => "application/json",
    "Authorization" => signature
  }

  puts "Request: #{URL} | #{headers} | #{request}"

  response = Net::HTTP.post(URI(URL), request.to_json, headers)

  puts "Response:"
  result = JSON.parse(response.body)
  puts result

  result
end