Method: Boxcars::Cohere#chat

Defined in:
lib/boxcars/engine/cohere.rb

#chat(params, cohere_api_key) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/boxcars/engine/cohere.rb', line 42

def chat(params, cohere_api_key)
  raise Boxcars::ConfigurationError('Cohere API key not set') if cohere_api_key.blank?

  # Define the API endpoint and parameters
  api_endpoint = 'https://api.cohere.ai/v1/chat'

  connection = Faraday.new(api_endpoint) do |faraday|
    faraday.request :url_encoded
    faraday.headers['Authorization'] = "Bearer #{cohere_api_key}"
    faraday.headers['Content-Type'] = 'application/json'
  end

  # Make the API call
  response = connection.post { |req| req.body = params.to_json }
  JSON.parse(response.body, symbolize_names: true)
end