Skip to content

Commit

Permalink
fixes #1625 [Gen-Ai-Orchestrator]: fix generate sentence endpoint (#1626
Browse files Browse the repository at this point in the history
)

* 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 <[email protected]>
  • Loading branch information
GuirriecP and Pierre Guirriec authored Jun 13, 2024
1 parent 6175325 commit 08b2b65
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 08b2b65

Please sign in to comment.