From caf0552ede56ddc6942c4dec4925e436fd377829 Mon Sep 17 00:00:00 2001 From: Christoph Eicke Date: Mon, 23 Sep 2024 19:54:37 +0200 Subject: [PATCH] Fix autotitle for conversations (#511) --- app/jobs/autotitle_conversation_job.rb | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/app/jobs/autotitle_conversation_job.rb b/app/jobs/autotitle_conversation_job.rb index 0b1f61e3..8f80c011 100644 --- a/app/jobs/autotitle_conversation_job.rb +++ b/app/jobs/autotitle_conversation_job.rb @@ -21,13 +21,23 @@ def perform(conversation_id) private def generate_title_for(text) + ai_backend = @conversation.assistant.api_service.ai_backend.new(@conversation.user, @conversation.assistant) - response = ai_backend.get_oneoff_message( - system_message, - [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 - ) - response.scan(/(?<=:)"(.+?)"/)&.flatten&.first&.strip + + if ai_backend.class == AIBackend::OpenAI || ai_backend.class == AIBackend::Anthropic + response = ai_backend.get_oneoff_message( + system_message, + [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"] + else + response = ai_backend.get_oneoff_message( + system_message, + [text] + ) + return response.scan(/(?<=:)"(.+?)"/)&.flatten&.first&.strip + end end def system_message