mirror of
https://github.com/csunny/DB-GPT.git
synced 2025-08-30 15:01:31 +00:00
多场景对话架构一期0525
This commit is contained in:
parent
0e4955a62a
commit
623205d35f
@ -27,12 +27,10 @@ class BaseOutputParser(ABC):
|
||||
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.is_stream_out = is_stream_out
|
||||
|
||||
|
||||
# TODO 后续和模型绑定
|
||||
def _parse_model_stream_resp(self, response, sep: str):
|
||||
pass
|
||||
@ -40,6 +38,7 @@ class BaseOutputParser(ABC):
|
||||
def _parse_model_nostream_resp(self, response, sep: str):
|
||||
text = response.text.strip()
|
||||
text = text.rstrip()
|
||||
text = text.lower()
|
||||
respObj = json.loads(text)
|
||||
|
||||
xx = respObj['response']
|
||||
@ -51,18 +50,21 @@ class BaseOutputParser(ABC):
|
||||
tmpResp = all_text.split(sep)
|
||||
last_index = -1
|
||||
for i in range(len(tmpResp)):
|
||||
if tmpResp[i].find('ASSISTANT:') != -1:
|
||||
if tmpResp[i].find('assistant:') != -1:
|
||||
last_index = i
|
||||
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("\_", "_")
|
||||
ai_response = ai_response.replace("\*", "*")
|
||||
print("un_stream clear response:{}", ai_response)
|
||||
return ai_response
|
||||
else:
|
||||
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
|
||||
Args:
|
||||
@ -71,14 +73,12 @@ class BaseOutputParser(ABC):
|
||||
Returns:
|
||||
|
||||
"""
|
||||
if self.is_stream_out:
|
||||
self._parse_model_nostream_resp(response, self.sep)
|
||||
if not self.is_stream_out:
|
||||
return self._parse_model_nostream_resp(response, self.sep)
|
||||
else:
|
||||
### TODO
|
||||
self._parse_model_stream_resp(response, self.sep)
|
||||
return 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
|
||||
Args:
|
||||
@ -89,8 +89,7 @@ class BaseOutputParser(ABC):
|
||||
"""
|
||||
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
|
||||
Args:
|
||||
|
@ -31,6 +31,9 @@ class PromptTemplate(BaseModel, ABC):
|
||||
input_variables: List[str]
|
||||
"""A list of the names of the variables the prompt template expects."""
|
||||
template_scene: str
|
||||
|
||||
template_define:str
|
||||
"""this template define"""
|
||||
template: str
|
||||
"""The prompt template."""
|
||||
template_format: str = "f-string"
|
||||
|
@ -33,7 +33,7 @@ logger = build_logger("BaseChat", LOGDIR + "BaseChat.log")
|
||||
headers = {"User-Agent": "dbgpt Client"}
|
||||
CFG = Config()
|
||||
class BaseChat( ABC):
|
||||
chat_scene: str = None
|
||||
chat_scene:str = None
|
||||
llm_model: Any = None
|
||||
temperature: float = 0.6
|
||||
max_new_tokens: int = 1024
|
||||
|
@ -2,6 +2,7 @@ import requests
|
||||
import datetime
|
||||
import threading
|
||||
import json
|
||||
import traceback
|
||||
from urllib.parse import urljoin
|
||||
from sqlalchemy import (
|
||||
MetaData,
|
||||
@ -24,17 +25,17 @@ from pilot.utils import (
|
||||
build_logger,
|
||||
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.out_parser.base import BaseOutputParser
|
||||
from pilot.scene.chat_db.out_parser import DbChatOutputParser
|
||||
|
||||
CFG = Config()
|
||||
|
||||
|
||||
class ChatWithDb(BaseChat):
|
||||
chat_scene: str = ChatScene.ChatWithDb.value
|
||||
|
||||
|
||||
"""Number of results to return from the query"""
|
||||
|
||||
def __init__(self, chat_session_id, db_name, user_input):
|
||||
@ -86,39 +87,49 @@ class ChatWithDb(BaseChat):
|
||||
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
|
||||
# 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)
|
||||
# resp_test = {
|
||||
# "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)
|
||||
# self.current_message.add_ai_message(json.dumps(prompt_define_response._asdict()))
|
||||
# result = self.database.run(self.db_connect, prompt_define_response.SQL)
|
||||
|
||||
|
||||
resp_test = {
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
# 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)
|
||||
if hasattr(prompt_define_response, 'thoughts'):
|
||||
if prompt_define_response.thoughts.get("speak"):
|
||||
self.current_message.add_view_message(
|
||||
self.prompt_template.output_parser.parse_view_response(prompt_define_response.thoughts.get("speak"),result))
|
||||
elif prompt_define_response.thoughts.get("reasoning"):
|
||||
self.current_message.add_view_message(
|
||||
self.prompt_template.output_parser.parse_view_response(prompt_define_response.thoughts.get("reasoning"), result))
|
||||
else:
|
||||
self.current_message.add_view_message(
|
||||
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))
|
||||
|
||||
except Exception as e:
|
||||
print(traceback.format_exc())
|
||||
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)
|
||||
|
||||
|
||||
def chat_show(self):
|
||||
ret = []
|
||||
# 单论对话只能有一次User 记录 和一次 AI 记录
|
||||
@ -133,44 +144,42 @@ class ChatWithDb(BaseChat):
|
||||
return ret
|
||||
|
||||
# 暂时为了兼容前端
|
||||
def current_ai_response(self)->str:
|
||||
def current_ai_response(self) -> str:
|
||||
for message in self.current_message.messages:
|
||||
if message.type == 'view':
|
||||
return message.content
|
||||
return message.content
|
||||
return None
|
||||
|
||||
|
||||
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):
|
||||
### 使用历史信息的第一轮和最后一轮数据合并成历史对话记录, 做上下文提示时,用户展示消息需要过滤掉
|
||||
for first_message in self.history_message[0].messages:
|
||||
if not isinstance(first_message, ViewMessage):
|
||||
if not isinstance(first_message, ViewMessage):
|
||||
text += first_message.type + ":" + first_message.content + self.prompt_template.sep
|
||||
|
||||
index = self.chat_retention_rounds - 1
|
||||
for last_message in self.history_message[-index:].messages:
|
||||
if not isinstance(last_message, ViewMessage):
|
||||
text += last_message.type + ":" + last_message.content + self.prompt_template.sep
|
||||
text += last_message.type + ":" + last_message.content + self.prompt_template.sep
|
||||
|
||||
else:
|
||||
### 直接历史记录拼接
|
||||
for conversation in self.history_message:
|
||||
for message in conversation.messages:
|
||||
if not isinstance(message, ViewMessage):
|
||||
text += message.type + ":" + message.content + self.prompt_template.sep
|
||||
text += message.type + ":" + message.content + self.prompt_template.sep
|
||||
|
||||
### current conversation
|
||||
for now_message in self.current_message.messages:
|
||||
text += now_message.type + ":" + now_message.content + self.prompt_template.sep
|
||||
text += now_message.type + ":" + now_message.content + self.prompt_template.sep
|
||||
|
||||
return text
|
||||
|
||||
|
||||
@classmethod
|
||||
@property
|
||||
def chat_type(self) -> str:
|
||||
return ChatScene.ChatWithDb.value
|
||||
return ChatScene.ChatExecution.value
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@ -204,7 +213,6 @@ if __name__ == "__main__":
|
||||
data = db.run(db_connect, "select * from users")
|
||||
print(generate_htm_table(data))
|
||||
|
||||
|
||||
#
|
||||
# print(db.run(db_connect, "select * from users"))
|
||||
#
|
||||
|
@ -17,7 +17,7 @@ from pilot.out_parser.base import BaseOutputParser, T
|
||||
|
||||
|
||||
class SqlAction(NamedTuple):
|
||||
SQL: str
|
||||
sql: str
|
||||
thoughts: Dict
|
||||
|
||||
|
||||
@ -44,19 +44,20 @@ class DbChatOutputParser(BaseOutputParser):
|
||||
cleaned_output = cleaned_output[: -len("```")]
|
||||
cleaned_output = cleaned_output.strip()
|
||||
response = json.loads(cleaned_output)
|
||||
sql, thoughts = response["SQL"], response["thoughts"]
|
||||
sql, thoughts = response["sql"], response["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
|
||||
df = pd.DataFrame(data[1:], columns=data[0])
|
||||
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>"""
|
||||
html_table = df.to_html(index=False, escape=False)
|
||||
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
|
||||
def _type(self) -> str:
|
||||
|
@ -2,12 +2,12 @@ import json
|
||||
from pilot.prompts.prompt_new import PromptTemplate
|
||||
from pilot.configs.config import Config
|
||||
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
|
||||
|
||||
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:
|
||||
{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.
|
||||
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.
|
||||
@ -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.
|
||||
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.
|
||||
@ -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}
|
||||
|
||||
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 = {
|
||||
@ -44,21 +45,21 @@ RESPONSE_FORMAT = {
|
||||
"reasoning": "reasoning",
|
||||
"speak": "thoughts summary to say to user",
|
||||
},
|
||||
"SQL": "SQL Query to run"
|
||||
"sql": "SQL Query to run"
|
||||
}
|
||||
|
||||
PROMPT_SEP = SeparatorStyle.SINGLE.value
|
||||
|
||||
PROMPT_NEED_NEED_STREAM_OUT = False
|
||||
PROMPT_NEED_NEED_STREAM_OUT = False
|
||||
|
||||
chat_db_prompt = PromptTemplate(
|
||||
template_scene=ChatScene.ChatWithDb.value,
|
||||
input_variables=["input", "table_info", "dialect", "top_k", "response"],
|
||||
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),
|
||||
)
|
||||
|
||||
CFG.prompt_templates.update({chat_db_prompt.template_scene: chat_db_prompt})
|
||||
|
||||
|
||||
|
@ -10,7 +10,6 @@ class ChatFactory(metaclass=Singleton):
|
||||
def get_implementation(chat_mode, **kwargs):
|
||||
|
||||
chat_classes = BaseChat.__subclasses__()
|
||||
|
||||
implementation = None
|
||||
for cls in chat_classes:
|
||||
if(cls.chat_scene == chat_mode):
|
||||
|
@ -18,17 +18,10 @@ from langchain import PromptTemplate
|
||||
ROOT_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
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.prompts.auto_mode_prompt import AutoModePrompt
|
||||
from pilot.prompts.generator import PromptGenerator
|
||||
|
||||
from pilot.scene.base_chat import BaseChat
|
||||
|
||||
from pilot.commands.exception_not_commands import NotCommands
|
||||
from pilot.configs.config import Config
|
||||
from pilot.configs.model_config import (
|
||||
DATASETS_DIR,
|
||||
|
Loading…
Reference in New Issue
Block a user