多场景对话架构一期0525

This commit is contained in:
yhjun1026
2023-05-25 18:54:32 +08:00
parent 0e4955a62a
commit 623205d35f
8 changed files with 83 additions and 79 deletions

View File

@@ -27,12 +27,10 @@ class BaseOutputParser(ABC):
Output parsers help structure language model responses. Output parsers help structure language model responses.
""" """
def __init__(self, sep: str, is_stream_out: bool):
def __init__(self,sep:str, is_stream_out:bool):
self.sep = sep self.sep = sep
self.is_stream_out = is_stream_out self.is_stream_out = is_stream_out
# TODO 后续和模型绑定 # TODO 后续和模型绑定
def _parse_model_stream_resp(self, response, sep: str): def _parse_model_stream_resp(self, response, sep: str):
pass pass
@@ -40,6 +38,7 @@ class BaseOutputParser(ABC):
def _parse_model_nostream_resp(self, response, sep: str): def _parse_model_nostream_resp(self, response, sep: str):
text = response.text.strip() text = response.text.strip()
text = text.rstrip() text = text.rstrip()
text = text.lower()
respObj = json.loads(text) respObj = json.loads(text)
xx = respObj['response'] xx = respObj['response']
@@ -51,18 +50,21 @@ class BaseOutputParser(ABC):
tmpResp = all_text.split(sep) tmpResp = all_text.split(sep)
last_index = -1 last_index = -1
for i in range(len(tmpResp)): for i in range(len(tmpResp)):
if tmpResp[i].find('ASSISTANT:') != -1: if tmpResp[i].find('assistant:') != -1:
last_index = i last_index = i
ai_response = tmpResp[last_index] ai_response = tmpResp[last_index]
ai_response = ai_response.replace("ASSISTANT:", "") ai_response = ai_response.replace("assistant:", "")
ai_response = ai_response.replace("\n", "") ai_response = ai_response.replace("\n", "")
ai_response = ai_response.replace("\_", "_") ai_response = ai_response.replace("\_", "_")
ai_response = ai_response.replace("\*", "*")
print("un_stream clear response:{}", ai_response) print("un_stream clear response:{}", ai_response)
return ai_response return ai_response
else: else:
raise ValueError("Model server error!code=" + respObj_ex['error_code']); raise ValueError("Model server error!code=" + respObj_ex['error_code']);
def parse_model_server_out(self, response)->str:
def parse_model_server_out(self, response) -> str:
""" """
parse the model server http response parse the model server http response
Args: Args:
@@ -71,14 +73,12 @@ class BaseOutputParser(ABC):
Returns: Returns:
""" """
if self.is_stream_out: if not self.is_stream_out:
self._parse_model_nostream_resp(response, self.sep) return self._parse_model_nostream_resp(response, self.sep)
else: else:
### TODO return self._parse_model_stream_resp(response, self.sep)
self._parse_model_stream_resp(response, self.sep)
def parse_prompt_response(self, model_out_text) -> T:
def parse_prompt_response(self, model_out_text)->T:
""" """
parse model out text to prompt define response parse model out text to prompt define response
Args: Args:
@@ -89,8 +89,7 @@ class BaseOutputParser(ABC):
""" """
pass pass
def parse_view_response(self, ai_text) -> str:
def parse_view_response(self, ai_text)->str:
""" """
parse the ai response info to user view parse the ai response info to user view
Args: Args:

View File

@@ -31,6 +31,9 @@ class PromptTemplate(BaseModel, ABC):
input_variables: List[str] input_variables: List[str]
"""A list of the names of the variables the prompt template expects.""" """A list of the names of the variables the prompt template expects."""
template_scene: str template_scene: str
template_define:str
"""this template define"""
template: str template: str
"""The prompt template.""" """The prompt template."""
template_format: str = "f-string" template_format: str = "f-string"

View File

@@ -33,7 +33,7 @@ logger = build_logger("BaseChat", LOGDIR + "BaseChat.log")
headers = {"User-Agent": "dbgpt Client"} headers = {"User-Agent": "dbgpt Client"}
CFG = Config() CFG = Config()
class BaseChat( ABC): class BaseChat( ABC):
chat_scene: str = None chat_scene:str = None
llm_model: Any = None llm_model: Any = None
temperature: float = 0.6 temperature: float = 0.6
max_new_tokens: int = 1024 max_new_tokens: int = 1024

View File

@@ -2,6 +2,7 @@ import requests
import datetime import datetime
import threading import threading
import json import json
import traceback
from urllib.parse import urljoin from urllib.parse import urljoin
from sqlalchemy import ( from sqlalchemy import (
MetaData, MetaData,
@@ -24,17 +25,17 @@ from pilot.utils import (
build_logger, build_logger,
server_error_msg, server_error_msg,
) )
from pilot.common.markdown_text import generate_markdown_table,generate_htm_table,datas_to_table_html from pilot.common.markdown_text import generate_markdown_table, generate_htm_table, datas_to_table_html
from pilot.scene.chat_db.prompt import chat_db_prompt from pilot.scene.chat_db.prompt import chat_db_prompt
from pilot.out_parser.base import BaseOutputParser from pilot.out_parser.base import BaseOutputParser
from pilot.scene.chat_db.out_parser import DbChatOutputParser from pilot.scene.chat_db.out_parser import DbChatOutputParser
CFG = Config() CFG = Config()
class ChatWithDb(BaseChat): class ChatWithDb(BaseChat):
chat_scene: str = ChatScene.ChatWithDb.value chat_scene: str = ChatScene.ChatWithDb.value
"""Number of results to return from the query""" """Number of results to return from the query"""
def __init__(self, chat_session_id, db_name, user_input): def __init__(self, chat_session_id, db_name, user_input):
@@ -86,39 +87,49 @@ class ChatWithDb(BaseChat):
try: try:
### 走非流式的模型服务接口 ### 走非流式的模型服务接口
response = requests.post(urljoin(CFG.MODEL_SERVER, "generate"), headers=headers, json=payload, timeout=120)
ai_response_text = self.prompt_template.output_parser.parse_model_server_out(response)
self.current_message.add_ai_message(ai_response_text)
prompt_define_response = self.prompt_template.output_parser.parse_prompt_response(ai_response_text)
result = self.database.run(self.db_connect, prompt_define_response.sql)
# # TODO - TEST # # TODO - TEST
# response = requests.post(urljoin(CFG.MODEL_SERVER, "generate"), headers=headers, json=payload, timeout=120) # resp_test = {
# ai_response_text = self.prompt_template.output_parser.parse_model_server_out(response) # "SQL": "select * from users",
# "thoughts": {
# "text": "thought",
# "reasoning": "reasoning",
# "plan": "- short bulleted\n- list that conveys\n- long-term plan",
# "criticism": "constructive self-criticism",
# "speak": "thoughts summary to say to user"
# }
# }
# #
# prompt_define_response = self.prompt_template.output_parser.parse_prompt_response(ai_response_text) # sql_action = SqlAction(**resp_test)
# self.current_message.add_ai_message(json.dumps(prompt_define_response._asdict())) # self.current_message.add_ai_message(json.dumps(sql_action._asdict()))
# result = self.database.run(self.db_connect, prompt_define_response.SQL) # result = self.database.run(self.db_connect, sql_action.SQL)
if hasattr(prompt_define_response, 'thoughts'):
if prompt_define_response.thoughts.get("speak"):
resp_test = { self.current_message.add_view_message(
"SQL": "select * from users", self.prompt_template.output_parser.parse_view_response(prompt_define_response.thoughts.get("speak"),result))
"thoughts": { elif prompt_define_response.thoughts.get("reasoning"):
"text": "thought", self.current_message.add_view_message(
"reasoning": "reasoning", self.prompt_template.output_parser.parse_view_response(prompt_define_response.thoughts.get("reasoning"), result))
"plan": "- short bulleted\n- list that conveys\n- long-term plan", else:
"criticism": "constructive self-criticism", self.current_message.add_view_message(
"speak": "thoughts summary to say to user" self.prompt_template.output_parser.parse_view_response(prompt_define_response.thoughts, result))
} else:
} self.current_message.add_view_message(
self.prompt_template.output_parser.parse_view_response(prompt_define_response, result))
sql_action = SqlAction(**resp_test)
self.current_message.add_ai_message(json.dumps(sql_action._asdict()))
result = self.database.run(self.db_connect, sql_action.SQL)
self.current_message.add_view_message(self.prompt_template.output_parser.parse_view_response(result))
except Exception as e: except Exception as e:
print(traceback.format_exc())
logger.error("model response parase faild" + str(e)) logger.error("model response parase faild" + str(e))
self.current_message.add_ai_message(str(e)) self.current_message.add_view_message(f"ERROR:{str(e)}!{ai_response_text}")
### 对话记录存储 ### 对话记录存储
self.memory.append(self.current_message) self.memory.append(self.current_message)
def chat_show(self): def chat_show(self):
ret = [] ret = []
# 单论对话只能有一次User 记录 和一次 AI 记录 # 单论对话只能有一次User 记录 和一次 AI 记录
@@ -133,15 +144,14 @@ class ChatWithDb(BaseChat):
return ret return ret
# 暂时为了兼容前端 # 暂时为了兼容前端
def current_ai_response(self)->str: def current_ai_response(self) -> str:
for message in self.current_message.messages: for message in self.current_message.messages:
if message.type == 'view': if message.type == 'view':
return message.content return message.content
return None return None
def generate_llm_text(self) -> str: def generate_llm_text(self) -> str:
text = "" text = self.prompt_template.template_define + self.prompt_template.sep
### 线处理历史信息 ### 线处理历史信息
if (len(self.history_message) > self.chat_retention_rounds): if (len(self.history_message) > self.chat_retention_rounds):
### 使用历史信息的第一轮和最后一轮数据合并成历史对话记录, 做上下文提示时,用户展示消息需要过滤掉 ### 使用历史信息的第一轮和最后一轮数据合并成历史对话记录, 做上下文提示时,用户展示消息需要过滤掉
@@ -167,10 +177,9 @@ class ChatWithDb(BaseChat):
return text return text
@property
@classmethod
def chat_type(self) -> str: def chat_type(self) -> str:
return ChatScene.ChatWithDb.value return ChatScene.ChatExecution.value
if __name__ == "__main__": if __name__ == "__main__":
@@ -204,7 +213,6 @@ if __name__ == "__main__":
data = db.run(db_connect, "select * from users") data = db.run(db_connect, "select * from users")
print(generate_htm_table(data)) print(generate_htm_table(data))
# #
# print(db.run(db_connect, "select * from users")) # print(db.run(db_connect, "select * from users"))
# #

View File

@@ -17,7 +17,7 @@ from pilot.out_parser.base import BaseOutputParser, T
class SqlAction(NamedTuple): class SqlAction(NamedTuple):
SQL: str sql: str
thoughts: Dict thoughts: Dict
@@ -44,19 +44,20 @@ class DbChatOutputParser(BaseOutputParser):
cleaned_output = cleaned_output[: -len("```")] cleaned_output = cleaned_output[: -len("```")]
cleaned_output = cleaned_output.strip() cleaned_output = cleaned_output.strip()
response = json.loads(cleaned_output) response = json.loads(cleaned_output)
sql, thoughts = response["SQL"], response["thoughts"] sql, thoughts = response["sql"], response["thoughts"]
return SqlAction(sql, thoughts) return SqlAction(sql, thoughts)
def parse_view_response(self, data) -> str: def parse_view_response(self, speak, data) -> str:
### tool out data to table view ### tool out data to table view
df = pd.DataFrame(data[1:], columns=data[0]) df = pd.DataFrame(data[1:], columns=data[0])
table_style = """<style> table_style = """<style>
table{border-collapse:collapse;width:60%;height:80%;margin:0 auto;float:right;border: 1px solid #007bff; background-color:#CFE299}th,td{border:1px solid #ddd;padding:3px;text-align:center}th{background-color:#C9C3C7;color: #fff;font-weight: bold;}tr:nth-child(even){background-color:#7C9F4A}tr:hover{background-color:#333} table{border-collapse:collapse;width:100%;height:80%;margin:0 auto;float:center;border: 1px solid #007bff; background-color:#333; color:#fff}th,td{border:1px solid #ddd;padding:3px;text-align:center}th{background-color:#C9C3C7;color: #fff;font-weight: bold;}tr:nth-child(even){background-color:#444}tr:hover{background-color:#444}
</style>""" </style>"""
html_table = df.to_html(index=False, escape=False) html_table = df.to_html(index=False, escape=False)
html = f"<html><head>{table_style}</head><body>{html_table}</body></html>" html = f"<html><head>{table_style}</head><body>{html_table}</body></html>"
return html.replace("\n", " ") view_text = f"##### {str(speak)}" + "\n" + html.replace("\n", " ")
return view_text
@property @property
def _type(self) -> str: def _type(self) -> str:

View File

@@ -2,12 +2,12 @@ import json
from pilot.prompts.prompt_new import PromptTemplate from pilot.prompts.prompt_new import PromptTemplate
from pilot.configs.config import Config from pilot.configs.config import Config
from pilot.scene.base import ChatScene from pilot.scene.base import ChatScene
from pilot.scene.chat_db.out_parser import DbChatOutputParser,SqlAction from pilot.scene.chat_db.out_parser import DbChatOutputParser, SqlAction
from pilot.common.schema import SeparatorStyle from pilot.common.schema import SeparatorStyle
CFG = Config() CFG = Config()
PROMPT_SCENE_DEFINE = """""" PROMPT_SCENE_DEFINE = """You are an AI designed to answer human questions, please follow the prompts and conventions of the system's input for your answers"""
PROMPT_SUFFIX = """Only use the following tables: PROMPT_SUFFIX = """Only use the following tables:
{table_info} {table_info}
@@ -16,7 +16,8 @@ Question: {input}
""" """
_DEFAULT_TEMPLATE = """Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer. _DEFAULT_TEMPLATE = """
You are a SQL expert. Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer.
Unless the user specifies in his question a specific number of examples he wishes to obtain, always limit your query to at most {top_k} results. Unless the user specifies in his question a specific number of examples he wishes to obtain, always limit your query to at most {top_k} results.
You can order the results by a relevant column to return the most interesting examples in the database. You can order the results by a relevant column to return the most interesting examples in the database.
Never query for all the columns from a specific table, only ask for a the few relevant columns given the question. Never query for all the columns from a specific table, only ask for a the few relevant columns given the question.
@@ -24,7 +25,7 @@ Pay attention to use only the column names that you can see in the schema descri
""" """
_mysql_prompt = """You are a MySQL expert. Given an input question, first create a syntactically correct MySQL query to run, then look at the results of the query and return the answer to the input question. _mysql_prompt = """You are a MySQL expert. Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer to the input question.
Unless the user specifies in the question a specific number of examples to obtain, query for at most {top_k} results using the LIMIT clause as per MySQL. You can order the results to return the most informative data in the database. Unless the user specifies in the question a specific number of examples to obtain, query for at most {top_k} results using the LIMIT clause as per MySQL. You can order the results to return the most informative data in the database.
Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in backticks (`) to denote them as delimited identifiers. Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in backticks (`) to denote them as delimited identifiers.
Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table. Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.
@@ -33,10 +34,10 @@ Pay attention to use CURDATE() function to get the current date, if the question
""" """
PROMPT_RESPONSE = """You should only respond in JSON format as following format: PROMPT_RESPONSE = """You must respond in JSON format as following format:
{response} {response}
Ensure the response can be parsed by Python json.loads Ensure the response is correct json and can be parsed by Python json.loads
""" """
RESPONSE_FORMAT = { RESPONSE_FORMAT = {
@@ -44,7 +45,7 @@ RESPONSE_FORMAT = {
"reasoning": "reasoning", "reasoning": "reasoning",
"speak": "thoughts summary to say to user", "speak": "thoughts summary to say to user",
}, },
"SQL": "SQL Query to run" "sql": "SQL Query to run"
} }
PROMPT_SEP = SeparatorStyle.SINGLE.value PROMPT_SEP = SeparatorStyle.SINGLE.value
@@ -55,10 +56,10 @@ chat_db_prompt = PromptTemplate(
template_scene=ChatScene.ChatWithDb.value, template_scene=ChatScene.ChatWithDb.value,
input_variables=["input", "table_info", "dialect", "top_k", "response"], input_variables=["input", "table_info", "dialect", "top_k", "response"],
response_format=json.dumps(RESPONSE_FORMAT, indent=4), response_format=json.dumps(RESPONSE_FORMAT, indent=4),
template=_DEFAULT_TEMPLATE + PROMPT_RESPONSE + PROMPT_SUFFIX, template_define=PROMPT_SCENE_DEFINE,
template=_DEFAULT_TEMPLATE + PROMPT_SUFFIX + PROMPT_RESPONSE,
stream_out=PROMPT_NEED_NEED_STREAM_OUT,
output_parser=DbChatOutputParser(sep=PROMPT_SEP, is_stream_out=PROMPT_NEED_NEED_STREAM_OUT), output_parser=DbChatOutputParser(sep=PROMPT_SEP, is_stream_out=PROMPT_NEED_NEED_STREAM_OUT),
) )
CFG.prompt_templates.update({chat_db_prompt.template_scene: chat_db_prompt}) CFG.prompt_templates.update({chat_db_prompt.template_scene: chat_db_prompt})

View File

@@ -10,7 +10,6 @@ class ChatFactory(metaclass=Singleton):
def get_implementation(chat_mode, **kwargs): def get_implementation(chat_mode, **kwargs):
chat_classes = BaseChat.__subclasses__() chat_classes = BaseChat.__subclasses__()
implementation = None implementation = None
for cls in chat_classes: for cls in chat_classes:
if(cls.chat_scene == chat_mode): if(cls.chat_scene == chat_mode):

View File

@@ -18,17 +18,10 @@ from langchain import PromptTemplate
ROOT_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) ROOT_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(ROOT_PATH) sys.path.append(ROOT_PATH)
from pilot.configs.model_config import LOGDIR, DATASETS_DIR
from pilot.plugins import scan_plugins
from pilot.configs.config import Config
from pilot.commands.command import execute_ai_response_json
from pilot.commands.command_mange import CommandRegistry from pilot.commands.command_mange import CommandRegistry
from pilot.prompts.auto_mode_prompt import AutoModePrompt
from pilot.prompts.generator import PromptGenerator
from pilot.scene.base_chat import BaseChat from pilot.scene.base_chat import BaseChat
from pilot.commands.exception_not_commands import NotCommands
from pilot.configs.config import Config from pilot.configs.config import Config
from pilot.configs.model_config import ( from pilot.configs.model_config import (
DATASETS_DIR, DATASETS_DIR,