Skip to content

Commit

Permalink
Rescue Faraday::BadRequestError and rewrite get_oneoff_message for au…
Browse files Browse the repository at this point in the history
…totitle
  • Loading branch information
papayalabs committed Nov 26, 2024
1 parent f4b198b commit 73c9ac3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 6 additions & 0 deletions app/jobs/autotitle_conversation_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def generate_title_for(text)
response_format: { type: "json_object" } # this causes problems for Groq even though it's supported: https://console.groq.com/docs/api-reference#chat-create
)
return JSON.parse(response)["topic"]
elsif ai_backend.class == AIBackend::Gemini
response = ai_backend.get_oneoff_message(
system_message,
[text]
)
return response.split(":")[1].split("}")[0].gsub(/"/, "").strip
else
response = ai_backend.get_oneoff_message(
system_message,
Expand Down
11 changes: 8 additions & 3 deletions app/services/ai_backend/gemini.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(user, assistant, conversation = nil, message = nil)
version: "v1beta"},
options: { model: assistant.language_model.api_name,
server_sent_events: true })
rescue ::Faraday::UnauthorizedError => e
rescue ::Faraday::UnauthorizedError, ::Faraday::BadRequestError => e
raise configuration_error
end
end
Expand All @@ -41,12 +41,17 @@ def configuration_error
def set_client_config(config)
super(config)

@client_config = {
@client_config = {
contents: config[:messages],
system_instruction: ( system_message(config[:instructions]) if @assistant.language_model.supports_system_message?)
}.compact
end

def get_oneoff_message(instructions, messages, params = {})
response = @client.generate_content( { system_instruction: { role: "user", parts: { text: instructions }}, contents: { role: "user", parts: { text: messages.first }}})
response.dig("candidates",0,"content","parts",0,"text")
end

def stream_next_conversation_message(&chunk_handler)
set_client_config(
messages: preceding_conversation_messages,
Expand All @@ -65,7 +70,7 @@ def stream_next_conversation_message(&chunk_handler)
yield content_chunk if content_chunk != nil
end
end
rescue ::Faraday::UnauthorizedError => e
rescue ::Faraday::UnauthorizedError, ::Faraday::BadRequestError => e
puts e.message
raise configuration_error
end
Expand Down

0 comments on commit 73c9ac3

Please sign in to comment.