Skip to content

Commit

Permalink
Get model name from configuration and pass it to the parameters hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Cosmo committed May 19, 2024
1 parent 366c5b7 commit ab4f945
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ class LanguageModel
end

def self.all
gpt_4_turbo = LanguageModel.new(
name: "GPT-4 Turbo",
gpt_4o = LanguageModel.new(
name: "GPT-4o",
kind: "completion",
provider: "openai",
configuration: {
"api_key" => "your_key",
"model" => "gpt-4-turbo"
"model" => "gpt-4o"
}
)

Expand All @@ -48,7 +48,7 @@ class LanguageModel
}
)

[gpt_4_turbo, ada2]
[gpt_4o, ada2]
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/openai_api/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ class Completion # rubocop:disable Metrics/ClassLength
def initialize(model, stream: nil, raw: false)
@name = model.name
@api_key = model.configuration["api_key"]
@model_identifier = model.configuration["model"]
@api_url = "https://api.openai.com/v1/chat/completions"
@stream = stream
@raw = raw
end

def chat(parameters)
parameters[:model] = @model_identifier
# Rails.logger.info("Chatting with \"#{@name}\" model with URL: #{@api_url}.")
if @stream.nil?
single_request_chat(parameters)
Expand Down
2 changes: 2 additions & 0 deletions lib/openai_api/embedding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ class Embedding
def initialize(model)
@name = model.name
@api_key = model.configuration["api_key"]
@model_identifier = model.configuration["model"]
@api_url = "https://api.openai.com/v1/embeddings"
end

def embed(parameters)
parameters[:model] = @model_identifier
# Rails.logger.info("Embedding with \"#{@name}\" model with URL: #{@api_url}.")
response = connection.post do |request|
request.params = params
Expand Down
17 changes: 7 additions & 10 deletions test/test_openai_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ def self.all # rubocop:disable Metrics/MethodLength
}
)

gpt4turbo = LanguageModel.new(
name: "GPT-4-Turbo",
gpt4o = LanguageModel.new(
name: "GPT-4o",
kind: "completion",
provider: "openai",
configuration: {
"api_key" => ENV.fetch("OPENAI_API_KEY"),
"model" => "gpt-4-turbo"
"model" => "gpt-4o"
}
)

[ada2, gpt4turbo]
[ada2, gpt4o]
end
end

Expand Down Expand Up @@ -92,8 +92,7 @@ def test_chat # rubocop:disable Metrics/MethodLength
"role" => "system",
"content" => "Tell me a joke"
}
],
"model": "gpt-4o"
]
}

VCR.use_cassette("test_chat") do
Expand All @@ -117,8 +116,7 @@ def test_chat_content_filter # rubocop:disable Metrics/MethodLength
"role" => "user",
"content" => "FUCK SHIT PISS."
}
],
"model": "gpt-4o"
]
}

VCR.use_cassette("test_chat_content_filter") do
Expand All @@ -133,8 +131,7 @@ def test_embedding
first_embedding_model = LanguageModel.all.find { |model| model.kind == "embedding" }
client = OpenAIAPI::Embedding.new(first_embedding_model)
parameters = {
"input" => "Once upon a time",
"model": "text-embedding-ada-002",
"input" => "Once upon a time"
}

VCR.use_cassette("test_embedding") do
Expand Down

0 comments on commit ab4f945

Please sign in to comment.