-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |