From 08b2b655c1562974c76b047964c38738f2c46105 Mon Sep 17 00:00:00 2001 From: GuirriecP <123645000+GuirriecP@users.noreply.github.com> Date: Thu, 13 Jun 2024 09:51:48 +0200 Subject: [PATCH] fixes #1625 [Gen-Ai-Orchestrator]: fix generate sentence endpoint (#1626) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Correction du prompt de génération de phrases * fixes #1625 Gen-Ai-Orchestrator: Fix prompt and LCEL chain * Fix Jinja for loop --------- Co-authored-by: Pierre Guirriec --- .../models/engines-configuration.ts | 43 ++++++++++++------- .../services/completion/completion_service.py | 16 ++++--- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/bot/admin/web/src/app/configuration/sentence-generation-settings/models/engines-configuration.ts b/bot/admin/web/src/app/configuration/sentence-generation-settings/models/engines-configuration.ts index c59818df2d..f45a9d67bc 100644 --- a/bot/admin/web/src/app/configuration/sentence-generation-settings/models/engines-configuration.ts +++ b/bot/admin/web/src/app/configuration/sentence-generation-settings/models/engines-configuration.ts @@ -1,24 +1,35 @@ import { AzureOpenAiApiVersionsList, EnginesConfiguration, LLMProvider, OpenAIModelsList } from '../../../shared/model/ai-settings'; -export const DefaultPrompt: string = `Given the base sentences provided below, generate a list of {{nb_sentences}} unique sentences that convey the same meaning but vary in their presentation. +export const DefaultPrompt: string = `# Sentences generation instructions + +## Task description + +Given the base sentences provided below, generate a list of {{nb_sentences}} unique sentences that convey the same meaning but vary in their presentation. Ensure that all generated sentences remain intelligible and their meaning can be easily understood, despite the variations introduced. + +## Variations The variations should reflect a diverse range of alterations, including but not limited to: -{% if options.spelling_mistakes %} -- Spelling Mistakes: Introduce common and uncommon spelling errors that do not hinder the overall comprehension of the sentence. -{% endif %} -{% if options.sms_language %} -- Incorporation of Non-Standard Language Features: Where appropriate, use features like onomatopoeia, mimetic words, or linguistic innovations unique to digital communication. -{% endif %} -{% if options.abbreviated_language %} -- Abbreviations and DM (Direct Message) Language: Transform parts of the sentence using popular text messaging abbreviations, internet slang, and shorthand commonly found in online and informal communication. -{% endif %} -- Answer as a numbered list. -- Answer in {{locale}}. - -Base Sentences (remember: you must answer as a numbered list): + +{% if options.spelling_mistakes %}- Spelling Mistakes: Introduce common and uncommon spelling errors that do not hinder the overall comprehension of the sentence.{% endif %} +{% if options.sms_language %}- Incorporation of Non-Standard Language Features: Where appropriate, use features like onomatopoeia, mimetic words, or linguistic innovations unique to digital communication.{% endif %} +{% if options.abbreviated_language %}- Abbreviations and DM (Direct Message) Language: Transform parts of the sentence using popular text messaging abbreviations, internet slang, and shorthand commonly found in online and informal communication.{% endif %} + +(if nothing is listed in this 'Variations' entry, find some) + +## Generated sentences language + +Answer in '{{locale}}' (language locale). + +## Format + +{{format_instructions}} + +## Base sentences + {% for sentence in sentences %} -- {{ sentence }} -{% endfor %} +- {{sentence}}{% endfor %} + +## Generated sentences `; export const EngineConfigurations: EnginesConfiguration[] = [ diff --git a/gen-ai/orchestrator-server/src/main/python/server/src/gen_ai_orchestrator/services/completion/completion_service.py b/gen-ai/orchestrator-server/src/main/python/server/src/gen_ai_orchestrator/services/completion/completion_service.py index e7c4fb81ad..949244ab12 100644 --- a/gen-ai/orchestrator-server/src/main/python/server/src/gen_ai_orchestrator/services/completion/completion_service.py +++ b/gen-ai/orchestrator-server/src/main/python/server/src/gen_ai_orchestrator/services/completion/completion_service.py @@ -21,8 +21,12 @@ from langchain_core.output_parsers import NumberedListOutputParser from langchain_core.prompts import PromptTemplate as LangChainPromptTemplate -from gen_ai_orchestrator.errors.exceptions.exceptions import GenAIPromptTemplateException -from gen_ai_orchestrator.errors.handlers.openai.openai_exception_handler import openai_exception_handler +from gen_ai_orchestrator.errors.exceptions.exceptions import ( + GenAIPromptTemplateException, +) +from gen_ai_orchestrator.errors.handlers.openai.openai_exception_handler import ( + openai_exception_handler, +) from gen_ai_orchestrator.models.errors.errors_models import ErrorInfo from gen_ai_orchestrator.models.prompt.prompt_formatter import PromptFormatter from gen_ai_orchestrator.models.prompt.prompt_template import PromptTemplate @@ -41,7 +45,7 @@ @openai_exception_handler(provider='OpenAI or AzureOpenAIService') async def generate_and_split_sentences( - query: SentenceGenerationQuery, + query: SentenceGenerationQuery, ) -> SentenceGenerationResponse: """ Generate sentences using a language model based on the provided query, @@ -56,11 +60,13 @@ async def generate_and_split_sentences( logger.info('Prompt completion - template validation') validate_prompt_template(query.prompt) + parser = NumberedListOutputParser() prompt = LangChainPromptTemplate.from_template( - template=query.prompt.template, template_format=query.prompt.formatter.value + template=query.prompt.template, + template_format=query.prompt.formatter.value, + partial_variables={'format_instructions': parser.get_format_instructions()}, ) model = get_llm_factory(query.llm_setting).get_language_model() - parser = NumberedListOutputParser() chain = prompt | model | parser sentences = await chain.ainvoke(query.prompt.inputs)