-
Notifications
You must be signed in to change notification settings - Fork 239
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
1 parent
a892e8b
commit 2a6c046
Showing
9 changed files
with
132 additions
and
2 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
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
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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require "test_helper" | ||
|
||
class GetNextAIMessageJobGeminiTest < ActiveJob::TestCase | ||
setup do | ||
@conversation = conversations(:gemini_conversation) | ||
@user = @conversation.user | ||
@assistant = @conversation.assistant | ||
@conversation.messages.create! role: :user, content_text: "Still there?", assistant: @assistant | ||
@assistant.language_model.update!(supports_tools: false) # this will change the TestClient response so we want to be selective about this | ||
@message = @conversation.latest_message_for_version(:latest) | ||
@test_client = TestClient::Gemini.new(access_token: "abc") | ||
end | ||
|
||
test "populates the latest message from the assistant" do | ||
assert_no_difference "@conversation.messages.reload.length" do | ||
TestClient::Gemini.stub :text, "Hello" do | ||
assert GetNextAIMessageJob.perform_now(@user.id, @message.id, @assistant.id) | ||
end | ||
end | ||
|
||
assert_equal "Hello", @conversation.latest_message_for_version(:latest).content_text | ||
end | ||
|
||
test "returns early if the message id was invalid" do | ||
refute GetNextAIMessageJob.perform_now(@user.id, 0, @assistant.id) | ||
end | ||
|
||
test "returns early if the assistant id was invalid" do | ||
refute GetNextAIMessageJob.perform_now(@user.id, @message.id, 0) | ||
end | ||
|
||
test "returns early if the message was already generated" do | ||
@message.update!(content_text: "Hello") | ||
refute GetNextAIMessageJob.perform_now(@user.id, @message.id, @assistant.id) | ||
end | ||
|
||
test "returns early if the user has replied after this" do | ||
@conversation.messages.create! role: :user, content_text: "Ignore that, new question:", assistant: @assistant | ||
refute GetNextAIMessageJob.perform_now(@user.id, @message.id, @assistant.id) | ||
end | ||
|
||
test "when Gemini key is blank, a nice error message is displayed" do | ||
api_service = @assistant.language_model.api_service | ||
api_service.update!(token: "") | ||
|
||
assert GetNextAIMessageJob.perform_now(@user.id, @message.id, @assistant.id) | ||
assert_includes @conversation.latest_message_for_version(:latest).content_text, "need to enter a valid API key for Gemini" | ||
end | ||
|
||
test "when API response key is missing, a nice error message is displayed" do | ||
TestClient::Gemini.stub :text, "" do | ||
assert GetNextAIMessageJob.perform_now(@user.id, @message.id, @assistant.id) | ||
assert_includes @conversation.latest_message_for_version(:latest).content_text, "a blank response" | ||
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,31 @@ | ||
module TestClient | ||
class Gemini | ||
def initialize(args) | ||
end | ||
|
||
def self.text | ||
nil | ||
end | ||
|
||
# This response is a valid example response from the API. | ||
# | ||
# Stub this method to respond with something more specific if needed. | ||
def stream_generate_content(args) | ||
contents = args.dig(:contents) | ||
system_message = args.dig(:system_instruction) | ||
return [{"candidates"=> | ||
[{"content"=> | ||
{"role"=>"model", | ||
"parts"=> | ||
[{"text"=> self.class.text || "Hello this is a model with instruction #{system_message.to_s.inspect}! How can I assist you today?"}]}, | ||
"safetyRatings"=> | ||
[{"category"=>"HARM_CATEGORY_HARASSMENT", "probability"=>"NEGLIGIBLE"}, | ||
{"category"=>"HARM_CATEGORY_HATE_SPEECH", "probability"=>"NEGLIGIBLE"}, | ||
{"category"=>"HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability"=>"NEGLIGIBLE"}, | ||
{"category"=>"HARM_CATEGORY_DANGEROUS_CONTENT", "probability"=>"NEGLIGIBLE"}]}], | ||
"usageMetadata"=>{"promptTokenCount"=>1037, "candidatesTokenCount"=>31, "totalTokenCount"=>1068} | ||
}] | ||
|
||
end | ||
end | ||
end |