Skip to content

Commit

Permalink
Extract configuration class
Browse files Browse the repository at this point in the history
  • Loading branch information
bodyyyya committed Mar 2, 2024
1 parent 7c50907 commit eb5d4f2
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 74 deletions.
1 change: 1 addition & 0 deletions lib/rubyai.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative "rubyai/client"
require_relative "rubyai/configuration"

module RubyAI
class Error < StandardError; end
Expand Down
66 changes: 34 additions & 32 deletions lib/rubyai/client.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
require 'faraday'
require 'json'
require_relative 'configuration'
require_relative 'http'

module RubyAI
class Client
def initialize(api_key, messages, temperature: 0.7, model: RubyAI::Configuration::DEFAULT_MODEL)
@api_key = api_key
@messages = messages
@temperature = temperature
@model = model
end

def call
response = connection.post do |req|
req.url RubyAI::Configuration::BASE_URL
req.headers.merge!(RubyAI::HTTP.build_headers(@api_key))
req.body = RubyAI::HTTP.build_body(@messages, @model, @temperature).to_json
end

JSON.parse(response.body)
end

private

def connection
@connection ||= Faraday.new do |faraday|
faraday.adapter Faraday.default_adapter
end
require 'faraday'
require 'json'
require_relative 'configuration'
require_relative 'http'

module RubyAI
class Client

def initialize(client_or_configuration, messages = nil, **options)
if client_or_configuration.is_a?(Configuration)
@configuration = client_or_configuration
else
@configuration = Configuration.new(client_or_configuration, messages, **options)
end
end
end
end

def call
response = connection.post do |req|
req.url RubyAI::Configuration::BASE_URL
req.headers.merge!(RubyAI::HTTP.build_headers(@configuration.api_key))
req.body = RubyAI::HTTP.build_body(@configuration.messages, @configuration.model, @configuration.temperature).to_json
end

JSON.parse(response.body)
end

private

def connection
@connection ||= Faraday.new do |faraday|
faraday.adapter Faraday.default_adapter
end
end
end
end
43 changes: 25 additions & 18 deletions lib/rubyai/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
module RubyAI
class Configuration
BASE_URL = "https://api.openai.com/v1/chat/completions"

MODELS = {
"gpt-4" => "gpt-4",
"gpt-4-0314" => "gpt-4-0314",
"gpt-4-32k" => "gpt-4-32k",
"gpt-3.5-turbo" => "gpt-3.5-turbo",
"gpt-3.5-turbo-0301" => "gpt-3.5-turbo-0301",
"text-davinci-003" => "text-davinci-003"
}

DEFAULT_MODEL = "gpt-3.5-turbo"

attr_accessor :api_key, :model, :messages, :temperature
end
end
module RubyAI
class Configuration
BASE_URL = "https://api.openai.com/v1/chat/completions"

MODELS = {
"gpt-4" => "gpt-4",
"gpt-4-0314" => "gpt-4-0314",
"gpt-4-32k" => "gpt-4-32k",
"gpt-3.5-turbo" => "gpt-3.5-turbo",
"gpt-3.5-turbo-0301" => "gpt-3.5-turbo-0301",
"text-davinci-003" => "text-davinci-003"
}

DEFAULT_MODEL = "gpt-3.5-turbo"

attr_accessor :api_key, :model, :messages, :temperature

def initialize(api_key, messages, temperature: 0.7, model: DEFAULT_MODEL)
@api_key = api_key
@messages = messages
@temperature = temperature
@model = model
end
end
end
48 changes: 24 additions & 24 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
require 'webmock/rspec'
require_relative '../lib/rubyai'

RSpec.describe RubyAI::Client do
let(:api_key) { 'your_api_key' }
let(:messages) { 'Hello, how are you?' }
let(:temperature) { 0.7 }
let(:model) { 'gpt-3.5-turbo' }
let(:client) { described_class.new(api_key, messages, temperature: temperature, model: model) }

describe '#call' do
let(:response_body) { { 'completion' => 'This is a response from the model.' } }
let(:status) { 200 }

before do
stub_request(:post, RubyAI::Configuration::BASE_URL)
.to_return(status: status, body: response_body.to_json, headers: { 'Content-Type' => 'application/json' })
end

it 'returns parsed JSON response' do
expect(client.call).to eq(response_body)
end
end
end
require 'webmock/rspec'
require_relative '../lib/rubyai/client.rb'
RSpec.describe RubyAI::Client do
let(:api_key) { 'your_api_key' }
let(:messages) { 'Hello, how are you?' }
let(:temperature) { 0.7 }
let(:model) { 'gpt-3.5-turbo' }
let(:client) { described_class.new(api_key, messages, temperature: temperature, model: model) }
describe '#call' do
let(:response_body) { { 'completion' => 'This is a response from the model.' } }
let(:status) { 200 }
before do
stub_request(:post, RubyAI::Configuration::BASE_URL)
.to_return(status: status, body: response_body.to_json, headers: { 'Content-Type' => 'application/json' })
end
it 'returns parsed JSON response when passing through client directly' do
expect(client.call).to eq(response_body)
end
end
end
24 changes: 24 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'webmock/rspec'
require_relative '../lib/rubyai/client.rb'

RSpec.describe RubyAI::Client do
let(:api_key) { 'your_api_key' }
let(:messages) { 'Hello, how are you?' }
let(:temperature) { 0.7 }
let(:model) { 'gpt-3.5-turbo' }
let(:client) { described_class.new(api_key, messages, temperature: temperature, model: model) }

describe '#call' do
let(:response_body) { { 'completion' => 'This is a response from the model.' } }
let(:status) { 200 }

before do
stub_request(:post, RubyAI::Configuration::BASE_URL)
.to_return(status: status, body: response_body.to_json, headers: { 'Content-Type' => 'application/json' })
end

it 'returns parsed JSON response when passing through client directly' do
expect(client.call).to eq(response_body)
end
end
end

0 comments on commit eb5d4f2

Please sign in to comment.