From a0b16e541cdc84fa404afb6be67c8540ce63e1e6 Mon Sep 17 00:00:00 2001 From: jyong Date: Mon, 18 Mar 2024 22:38:12 +0800 Subject: [PATCH] question classifier --- .../nodes/question_classifier/entities.py | 6 +- .../question_classifier_node.py | 2 +- .../question_classifier/template_prompts.py | 66 +++++++++---------- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/api/core/workflow/nodes/question_classifier/entities.py b/api/core/workflow/nodes/question_classifier/entities.py index 5371862ea8..f9a72f562b 100644 --- a/api/core/workflow/nodes/question_classifier/entities.py +++ b/api/core/workflow/nodes/question_classifier/entities.py @@ -1,4 +1,4 @@ -from typing import Any +from typing import Any, Optional from pydantic import BaseModel @@ -46,5 +46,5 @@ class QuestionClassifierNodeData(BaseNodeData): type: str = 'question-classifier' model: ModelConfig classes: list[ClassConfig] - instruction: str - memory: MemoryConfig + instruction: Optional[str] + memory: Optional[MemoryConfig] diff --git a/api/core/workflow/nodes/question_classifier/question_classifier_node.py b/api/core/workflow/nodes/question_classifier/question_classifier_node.py index 42bd141faf..a4696845ea 100644 --- a/api/core/workflow/nodes/question_classifier/question_classifier_node.py +++ b/api/core/workflow/nodes/question_classifier/question_classifier_node.py @@ -68,7 +68,7 @@ class QuestionClassifierNode(BaseNode): stop=stop ) try: - result_text_json = json.loads(result_text) + result_text_json = json.loads(result_text.strip('```JSON\n')) categories = result_text_json.get('categories', []) process_data = { 'model_mode': model_config.mode, diff --git a/api/core/workflow/nodes/question_classifier/template_prompts.py b/api/core/workflow/nodes/question_classifier/template_prompts.py index 871fc8d3e9..4b55093968 100644 --- a/api/core/workflow/nodes/question_classifier/template_prompts.py +++ b/api/core/workflow/nodes/question_classifier/template_prompts.py @@ -1,43 +1,43 @@ -QUESTION_CLASSIFIER_SYSTEM_PROMPT = ( - '### Job Description', - 'You are a text classification engine that analyzes text data and assigns categories based on user input or automatically determined categories.', - '### Task', - 'Your task is to assign one categories ONLY to the input text and only one category may be assigned returned in the output.Additionally, you need to extract the key words from the text that are related to the classification.', - '### Format', - 'The input text is in the variable text_field.Categories are specified as a comma-separated list in the variable categories or left empty for automatic determination.Classification instructions may be included to improve the classification accuracy.', - '### Constraint', - 'DO NOT include anything other than the JSON array in your response.' -) +QUESTION_CLASSIFIER_SYSTEM_PROMPT = """ + ### Job Description', + You are a text classification engine that analyzes text data and assigns categories based on user input or automatically determined categories. + ### Task + Your task is to assign one categories ONLY to the input text and only one category may be assigned returned in the output.Additionally, you need to extract the key words from the text that are related to the classification. + ### Format + The input text is in the variable text_field.Categories are specified as a comma-separated list in the variable categories or left empty for automatic determination.Classification instructions may be included to improve the classification accuracy. + ### Constraint + DO NOT include anything other than the JSON array in your response. +""" -QUESTION_CLASSIFIER_USER_PROMPT_1 = ( - '{ "input_text": ["I recently had a great experience with your company. The service was prompt and the staff was very friendly."],', - '"categories": ["Customer Service, Satisfaction, Sales, Product"],', - '"classification_instructions": ["classify the text based on the feedback provided by customer"]}```JSON' -) +QUESTION_CLASSIFIER_USER_PROMPT_1 = """ + { "input_text": ["I recently had a great experience with your company. The service was prompt and the staff was very friendly."], + "categories": ["Customer Service, Satisfaction, Sales, Product"], + "classification_instructions": ["classify the text based on the feedback provided by customer"]}```JSON +""" -QUESTION_CLASSIFIER_ASSISTANT_PROMPT_1 = ( - '{"keywords": ["recently", "great experience", "company", "service", "prompt", "staff", "friendly"],', - '"categories": ["Customer Service"]}```' -) +QUESTION_CLASSIFIER_ASSISTANT_PROMPT_1 = """ + {"keywords": ["recently", "great experience", "company", "service", "prompt", "staff", "friendly"], + "categories": ["Customer Service"]}``` +""" -QUESTION_CLASSIFIER_USER_PROMPT_2 = ( - '{"input_text": ["bad service, slow to bring the food"],', - '"categories": ["Food Quality, Experience, Price" ], ', - '"classification_instructions": []}```JSON' -) +QUESTION_CLASSIFIER_USER_PROMPT_2 = """ + {"input_text": ["bad service, slow to bring the food"], + "categories": ["Food Quality, Experience, Price" ], + "classification_instructions": []}```JSON +""" -QUESTION_CLASSIFIER_ASSISTANT_PROMPT_2 = ( - '{"keywords": ["bad service", "slow", "food", "tip", "terrible", "waitresses"],', - '"categories": ["Experience""]}```' -) +QUESTION_CLASSIFIER_ASSISTANT_PROMPT_2 = """ + {"keywords": ["bad service", "slow", "food", "tip", "terrible", "waitresses"], + "categories": ["Experience""]}``` +""" -QUESTION_CLASSIFIER_USER_PROMPT_3 = ( - '{"input_text": ["{input_text}"],', +QUESTION_CLASSIFIER_USER_PROMPT_3 = """ + '{{"input_text": ["{input_text}"],', '"categories": ["{categories}" ], ', - '"classification_instructions": ["{classification_instructions}"]}```JSON' -) + '"classification_instructions": ["{classification_instructions}"]}}```JSON' +""" QUESTION_CLASSIFIER_COMPLETION_PROMPT = """ ### Job Description @@ -58,5 +58,5 @@ Output: ### Memory Here is the chat histories between human and assistant, inside XML tags. ### User Input -{"input_text" : [{{input_text}}], "class" : [{{class}}],"classification_instruction" : [{{classification_instructions}}]} +{{"input_text" : ["{input_text}"], "class" : ["{class}"],"classification_instruction" : ["{classification_instructions}"]}} """ \ No newline at end of file