2023-10-12 23:13:10 +08:00
|
|
|
|
|
|
|
import copy
|
|
|
|
|
2024-01-12 12:34:01 +08:00
|
|
|
from core.prompt.advanced_prompt_templates import (BAICHUAN_CHAT_APP_CHAT_PROMPT_CONFIG,
|
|
|
|
BAICHUAN_CHAT_APP_COMPLETION_PROMPT_CONFIG,
|
|
|
|
BAICHUAN_COMPLETION_APP_CHAT_PROMPT_CONFIG,
|
|
|
|
BAICHUAN_COMPLETION_APP_COMPLETION_PROMPT_CONFIG, BAICHUAN_CONTEXT,
|
|
|
|
CHAT_APP_CHAT_PROMPT_CONFIG, CHAT_APP_COMPLETION_PROMPT_CONFIG,
|
|
|
|
COMPLETION_APP_CHAT_PROMPT_CONFIG,
|
|
|
|
COMPLETION_APP_COMPLETION_PROMPT_CONFIG, CONTEXT)
|
2023-10-18 20:02:52 +08:00
|
|
|
from core.prompt.prompt_transform import AppMode
|
2024-01-12 12:34:01 +08:00
|
|
|
|
2023-10-12 23:13:10 +08:00
|
|
|
|
|
|
|
class AdvancedPromptTemplateService:
|
|
|
|
|
2023-10-13 16:47:01 +08:00
|
|
|
@classmethod
|
|
|
|
def get_prompt(cls, args: dict) -> dict:
|
2023-10-12 23:13:10 +08:00
|
|
|
app_mode = args['app_mode']
|
|
|
|
model_mode = args['model_mode']
|
|
|
|
model_name = args['model_name']
|
|
|
|
has_context = args['has_context']
|
|
|
|
|
2023-10-18 20:02:52 +08:00
|
|
|
if 'baichuan' in model_name.lower():
|
2023-10-13 16:47:01 +08:00
|
|
|
return cls.get_baichuan_prompt(app_mode, model_mode, has_context)
|
2023-10-12 23:13:10 +08:00
|
|
|
else:
|
2023-10-13 16:47:01 +08:00
|
|
|
return cls.get_common_prompt(app_mode, model_mode, has_context)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_common_prompt(cls, app_mode: str, model_mode:str, has_context: str) -> dict:
|
|
|
|
context_prompt = copy.deepcopy(CONTEXT)
|
2023-10-12 23:13:10 +08:00
|
|
|
|
2023-10-18 20:02:52 +08:00
|
|
|
if app_mode == AppMode.CHAT.value:
|
2024-01-02 23:42:00 +08:00
|
|
|
if model_mode == "completion":
|
2023-10-13 16:47:01 +08:00
|
|
|
return cls.get_completion_prompt(copy.deepcopy(CHAT_APP_COMPLETION_PROMPT_CONFIG), has_context, context_prompt)
|
2024-01-02 23:42:00 +08:00
|
|
|
elif model_mode == "chat":
|
2023-10-13 16:47:01 +08:00
|
|
|
return cls.get_chat_prompt(copy.deepcopy(CHAT_APP_CHAT_PROMPT_CONFIG), has_context, context_prompt)
|
2023-10-18 20:02:52 +08:00
|
|
|
elif app_mode == AppMode.COMPLETION.value:
|
2024-01-02 23:42:00 +08:00
|
|
|
if model_mode == "completion":
|
2023-10-13 16:47:01 +08:00
|
|
|
return cls.get_completion_prompt(copy.deepcopy(COMPLETION_APP_COMPLETION_PROMPT_CONFIG), has_context, context_prompt)
|
2024-01-02 23:42:00 +08:00
|
|
|
elif model_mode == "chat":
|
2023-10-13 16:47:01 +08:00
|
|
|
return cls.get_chat_prompt(copy.deepcopy(COMPLETION_APP_CHAT_PROMPT_CONFIG), has_context, context_prompt)
|
2023-10-12 23:13:10 +08:00
|
|
|
|
2023-10-13 16:47:01 +08:00
|
|
|
@classmethod
|
|
|
|
def get_completion_prompt(cls, prompt_template: dict, has_context: str, context: str) -> dict:
|
2023-10-12 23:13:10 +08:00
|
|
|
if has_context == 'true':
|
|
|
|
prompt_template['completion_prompt_config']['prompt']['text'] = context + prompt_template['completion_prompt_config']['prompt']['text']
|
|
|
|
|
|
|
|
return prompt_template
|
|
|
|
|
2023-10-13 16:47:01 +08:00
|
|
|
@classmethod
|
|
|
|
def get_chat_prompt(cls, prompt_template: dict, has_context: str, context: str) -> dict:
|
2023-10-12 23:13:10 +08:00
|
|
|
if has_context == 'true':
|
|
|
|
prompt_template['chat_prompt_config']['prompt'][0]['text'] = context + prompt_template['chat_prompt_config']['prompt'][0]['text']
|
|
|
|
|
|
|
|
return prompt_template
|
|
|
|
|
2023-10-13 16:47:01 +08:00
|
|
|
@classmethod
|
|
|
|
def get_baichuan_prompt(cls, app_mode: str, model_mode:str, has_context: str) -> dict:
|
|
|
|
baichuan_context_prompt = copy.deepcopy(BAICHUAN_CONTEXT)
|
2023-10-12 23:13:10 +08:00
|
|
|
|
2023-10-18 20:02:52 +08:00
|
|
|
if app_mode == AppMode.CHAT.value:
|
2024-01-02 23:42:00 +08:00
|
|
|
if model_mode == "completion":
|
2023-10-13 16:47:01 +08:00
|
|
|
return cls.get_completion_prompt(copy.deepcopy(BAICHUAN_CHAT_APP_COMPLETION_PROMPT_CONFIG), has_context, baichuan_context_prompt)
|
2024-01-02 23:42:00 +08:00
|
|
|
elif model_mode == "chat":
|
2023-10-13 16:47:01 +08:00
|
|
|
return cls.get_chat_prompt(copy.deepcopy(BAICHUAN_CHAT_APP_CHAT_PROMPT_CONFIG), has_context, baichuan_context_prompt)
|
2023-10-18 20:02:52 +08:00
|
|
|
elif app_mode == AppMode.COMPLETION.value:
|
2024-01-02 23:42:00 +08:00
|
|
|
if model_mode == "completion":
|
2023-10-13 16:47:01 +08:00
|
|
|
return cls.get_completion_prompt(copy.deepcopy(BAICHUAN_COMPLETION_APP_COMPLETION_PROMPT_CONFIG), has_context, baichuan_context_prompt)
|
2024-01-02 23:42:00 +08:00
|
|
|
elif model_mode == "chat":
|
2023-10-13 16:47:01 +08:00
|
|
|
return cls.get_chat_prompt(copy.deepcopy(BAICHUAN_COMPLETION_APP_CHAT_PROMPT_CONFIG), has_context, baichuan_context_prompt)
|