From 7208dd6c882574955b1250589d9aec968439d1f6 Mon Sep 17 00:00:00 2001 From: "tuyang.yhj" Date: Thu, 29 Jun 2023 15:33:14 +0800 Subject: [PATCH 1/3] WEB API independent --- .../{test_duckdb.py => study_duckdb.py} | 6 +- pilot/memory/chat_history/base.py | 5 ++ pilot/memory/chat_history/duckdb_history.py | 25 ++++-- pilot/openapi/api_v1/api_v1.py | 80 +++++++++++-------- pilot/out_parser/base.py | 2 +- pilot/prompts/example_base.py | 6 +- pilot/scene/base_chat.py | 8 +- pilot/scene/chat_db/auto_execute/example.py | 39 +++++++++ pilot/scene/chat_db/auto_execute/prompt.py | 29 ++----- pilot/scene/chat_execution/example.py | 34 +++++++- 10 files changed, 161 insertions(+), 73 deletions(-) rename pilot/connections/rdbms/py_study/{test_duckdb.py => study_duckdb.py} (56%) create mode 100644 pilot/scene/chat_db/auto_execute/example.py diff --git a/pilot/connections/rdbms/py_study/test_duckdb.py b/pilot/connections/rdbms/py_study/study_duckdb.py similarity index 56% rename from pilot/connections/rdbms/py_study/test_duckdb.py rename to pilot/connections/rdbms/py_study/study_duckdb.py index dc3f926ac..6e41aa480 100644 --- a/pilot/connections/rdbms/py_study/test_duckdb.py +++ b/pilot/connections/rdbms/py_study/study_duckdb.py @@ -6,9 +6,9 @@ default_db_path = os.path.join(os.getcwd(), "message") duckdb_path = os.getenv("DB_DUCKDB_PATH", default_db_path + "/chat_history.db") if __name__ == "__main__": - if os.path.isfile(duckdb_path): - cursor = duckdb.connect(duckdb_path).cursor() + if os.path.isfile("../../../message/chat_history.db"): + cursor = duckdb.connect("../../../message/chat_history.db").cursor() # cursor.execute("SELECT * FROM chat_history limit 20") - cursor.execute("SELECT * FROM chat_history limit 20") + cursor.execute("SELECT * FROM chat_history where conv_uid ='b54ae5fe-1624-11ee-a271-b26789cc3e58'") data = cursor.fetchall() print(data) diff --git a/pilot/memory/chat_history/base.py b/pilot/memory/chat_history/base.py index 830968504..ec4af1a14 100644 --- a/pilot/memory/chat_history/base.py +++ b/pilot/memory/chat_history/base.py @@ -25,6 +25,11 @@ class BaseChatHistoryMemory(ABC): def messages(self) -> List[OnceConversation]: # type: ignore """Retrieve the messages from the local file""" + @abstractmethod + def create(self, user_name:str) -> None: + """Append the message to the record in the local file""" + + @abstractmethod def append(self, message: OnceConversation) -> None: """Append the message to the record in the local file""" diff --git a/pilot/memory/chat_history/duckdb_history.py b/pilot/memory/chat_history/duckdb_history.py index de80a5bc2..6aa7c344c 100644 --- a/pilot/memory/chat_history/duckdb_history.py +++ b/pilot/memory/chat_history/duckdb_history.py @@ -37,7 +37,7 @@ class DuckdbHistoryMemory(BaseChatHistoryMemory): if not result: # 如果表不存在,则创建新表 self.connect.execute( - "CREATE TABLE chat_history (conv_uid VARCHAR(100) PRIMARY KEY, user_name VARCHAR(100), messages TEXT)") + "CREATE TABLE chat_history (conv_uid VARCHAR(100) PRIMARY KEY, chat_mode VARCHAR(50), summary VARCHAR(255), user_name VARCHAR(100), messages TEXT)") def __get_messages_by_conv_uid(self, conv_uid: str): cursor = self.connect.cursor() @@ -47,6 +47,7 @@ class DuckdbHistoryMemory(BaseChatHistoryMemory): return content[0] else: return None + def messages(self) -> List[OnceConversation]: context = self.__get_messages_by_conv_uid(self.chat_seesion_id) if context: @@ -54,6 +55,17 @@ class DuckdbHistoryMemory(BaseChatHistoryMemory): return conversations return [] + def create(self, chat_mode, summary: str, user_name: str) -> None: + try: + cursor = self.connect.cursor() + cursor.execute( + "INSERT INTO chat_history(conv_uid, chat_mode summary, user_name, messages)VALUES(?,?,?,?,?)", + [self.chat_seesion_id, chat_mode, summary, user_name, ""]) + cursor.commit() + self.connect.commit() + except Exception as e: + print("init create conversation log error!" + str(e)) + def append(self, once_message: OnceConversation) -> None: context = self.__get_messages_by_conv_uid(self.chat_seesion_id) conversations: List[OnceConversation] = [] @@ -65,8 +77,9 @@ class DuckdbHistoryMemory(BaseChatHistoryMemory): cursor.execute("UPDATE chat_history set messages=? where conv_uid=?", [json.dumps(conversations, ensure_ascii=False), self.chat_seesion_id]) else: - cursor.execute("INSERT INTO chat_history(conv_uid, user_name, messages)VALUES(?,?,?)", - [self.chat_seesion_id, "", json.dumps(conversations, ensure_ascii=False)]) + cursor.execute( + "INSERT INTO chat_history(conv_uid, chat_mode, summary, user_name, messages)VALUES(?,?,?,?,?)", + [self.chat_seesion_id, once_message.chat_mode, once_message.get_user_conv().content, "",json.dumps(conversations, ensure_ascii=False)]) cursor.commit() self.connect.commit() @@ -103,11 +116,11 @@ class DuckdbHistoryMemory(BaseChatHistoryMemory): return [] - - def get_messages(self)-> List[OnceConversation]: + def get_messages(self) -> List[OnceConversation]: cursor = self.connect.cursor() cursor.execute("SELECT messages FROM chat_history where conv_uid=?", [self.chat_seesion_id]) context = cursor.fetchone() if context: - return json.loads(context[0]) + if context[0]: + return json.loads(context[0]) return None diff --git a/pilot/openapi/api_v1/api_v1.py b/pilot/openapi/api_v1/api_v1.py index 4f2e23946..740af4f05 100644 --- a/pilot/openapi/api_v1/api_v1.py +++ b/pilot/openapi/api_v1/api_v1.py @@ -51,6 +51,39 @@ def __get_conv_user_message(conversations: dict): return "" +def __new_conversation(chat_mode, user_id) -> ConversationVo: + unique_id = uuid.uuid1() + history_mem = DuckdbHistoryMemory(str(unique_id)) + return ConversationVo(conv_uid=str(unique_id), chat_mode=chat_mode) + + +def get_db_list(): + db = CFG.local_db + dbs = db.get_database_list() + params: dict = {} + for name in dbs: + params.update({name: name}) + return params + + +def plugins_select_info(): + plugins_infos: dict = {} + for plugin in CFG.plugins: + plugins_infos.update({f"【{plugin._name}】=>{plugin._description}": plugin._name}) + return plugins_infos + + +def knowledge_list(): + """return knowledge space list""" + params: dict = {} + request = KnowledgeSpaceRequest() + spaces = knowledge_service.get_knowledge_space(request) + for space in spaces: + params.update({space.name: space.name}) + return params + + + @router.get("/v1/chat/dialogue/list", response_model=Result[ConversationVo]) async def dialogue_list(response: Response, user_id: str = None): # 设置CORS头部信息 @@ -63,14 +96,15 @@ async def dialogue_list(response: Response, user_id: str = None): for item in datas: conv_uid = item.get("conv_uid") - messages = item.get("messages") - conversations = json.loads(messages) + summary = item.get("summary") + chat_mode = item.get("chat_mode") + + - first_conv: OnceConversation = conversations[0] conv_vo: ConversationVo = ConversationVo( conv_uid=conv_uid, - user_input=__get_conv_user_message(first_conv), - chat_mode=first_conv["chat_mode"], + user_input=summary, + chat_mode=chat_mode, ) dialogues.append(conv_vo) @@ -101,39 +135,13 @@ async def dialogue_scenes(): return Result.succ(scene_vos) + @router.post("/v1/chat/dialogue/new", response_model=Result[ConversationVo]) async def dialogue_new( chat_mode: str = ChatScene.ChatNormal.value, user_id: str = None ): - unique_id = uuid.uuid1() - return Result.succ(ConversationVo(conv_uid=str(unique_id), chat_mode=chat_mode)) - - -def get_db_list(): - db = CFG.local_db - dbs = db.get_database_list() - params: dict = {} - for name in dbs: - params.update({name: name}) - return params - - -def plugins_select_info(): - plugins_infos: dict = {} - for plugin in CFG.plugins: - plugins_infos.update({f"【{plugin._name}】=>{plugin._description}": plugin._name}) - return plugins_infos - - -def knowledge_list(): - """return knowledge space list""" - params: dict = {} - request = KnowledgeSpaceRequest() - spaces = knowledge_service.get_knowledge_space(request) - for space in spaces: - params.update({space.name: space.name}) - return params - + conv_vo = __new_conversation(chat_mode, user_id) + return Result.succ(conv_vo) @router.post("/v1/chat/mode/params/list", response_model=Result[dict]) async def params_list(chat_mode: str = ChatScene.ChatNormal.value): @@ -180,7 +188,8 @@ async def chat_completions(dialogue: ConversationVo = Body()): if not dialogue.chat_mode: dialogue.chat_mode = ChatScene.ChatNormal.value if not dialogue.conv_uid: - dialogue.conv_uid = str(uuid.uuid1()) + conv_vo = __new_conversation(dialogue.chat_mode, dialogue.user_name) + dialogue.conv_uid = conv_vo.conv_uid global model_semaphore, global_counter global_counter += 1 @@ -236,6 +245,7 @@ async def no_stream_generator(chat): msg = msg.replace("\n", "\\n") yield f"data: {msg}\n\n" + async def stream_generator(chat): model_response = chat.stream_call() if not CFG.NEW_SERVER_MODE: diff --git a/pilot/out_parser/base.py b/pilot/out_parser/base.py index ca308e92f..6f0da16fa 100644 --- a/pilot/out_parser/base.py +++ b/pilot/out_parser/base.py @@ -95,7 +95,7 @@ class BaseOutputParser(ABC): yield output def parse_model_nostream_resp(self, response, sep: str): - text = response.text.strip() + text = response.strip() text = text.rstrip() text = text.strip(b"\x00".decode()) respObj_ex = json.loads(text) diff --git a/pilot/prompts/example_base.py b/pilot/prompts/example_base.py index 2553be150..f7844a4d7 100644 --- a/pilot/prompts/example_base.py +++ b/pilot/prompts/example_base.py @@ -6,7 +6,7 @@ from pilot.common.schema import ExampleType class ExampleSelector(BaseModel, ABC): - examples_record: List[List] + examples_record: List[dict] use_example: bool = False type: str = ExampleType.ONE_SHOT.value @@ -16,7 +16,7 @@ class ExampleSelector(BaseModel, ABC): else: return self.__few_shot_context(count) - def __few_shot_context(self, count: int = 2) -> List[List]: + def __few_shot_context(self, count: int = 2) -> List[dict]: """ Use 2 or more examples, default 2 Returns: example text @@ -26,7 +26,7 @@ class ExampleSelector(BaseModel, ABC): return need_use return None - def __one_show_context(self) -> List: + def __one_show_context(self) -> dict: """ Use one examples Returns: diff --git a/pilot/scene/base_chat.py b/pilot/scene/base_chat.py index 8e7b3dfe7..376d99ef8 100644 --- a/pilot/scene/base_chat.py +++ b/pilot/scene/base_chat.py @@ -86,6 +86,11 @@ class BaseChat(ABC): extra = Extra.forbid arbitrary_types_allowed = True + def __init_history_message(self): + self.history_message == self.memory.messages() + if not self.history_message: + self.memory.create(self.current_user_input, "") + @property def chat_type(self) -> str: raise NotImplementedError("Not supported for this chat type.") @@ -103,9 +108,8 @@ class BaseChat(ABC): self.current_message.chat_order = len(self.history_message) + 1 self.current_message.add_user_message(self.current_user_input) self.current_message.start_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") - # TODO - self.current_message.tokens = 0 + self.current_message.tokens = 0 if self.prompt_template.template: current_prompt = self.prompt_template.format(**input_values) self.current_message.add_system_message(current_prompt) diff --git a/pilot/scene/chat_db/auto_execute/example.py b/pilot/scene/chat_db/auto_execute/example.py new file mode 100644 index 000000000..a2a01b44d --- /dev/null +++ b/pilot/scene/chat_db/auto_execute/example.py @@ -0,0 +1,39 @@ +from pilot.prompts.example_base import ExampleSelector +from pilot.common.schema import ExampleType +## Two examples are defined by default +EXAMPLES = [ + { + "messages": [ + {"type": "human", "data": {"content": "查询xxx", "example": True}}, + { + "type": "ai", + "data": { + "content": """{ + \"thoughts\": \"thought text\", + \"speak\": \"thoughts summary to say to user\", + \"command\": {\"name\": \"command name\", \"args\": {\"arg name\": \"value\"}}, + }""", + "example": True, + }, + }, + ] + }, + { + "messages": [ + {"type": "human", "data": {"content": "查询xxx", "example": True}}, + { + "type": "ai", + "data": { + "content": """{ + \"thoughts\": \"thought text\", + \"speak\": \"thoughts summary to say to user\", + \"command\": {\"name\": \"command name\", \"args\": {\"arg name\": \"value\"}}, + }""", + "example": True, + }, + }, + ] + }, +] + +sql_data_example = ExampleSelector(examples_record=EXAMPLES, use_example=True, type=ExampleType.ONE_SHOT.value) diff --git a/pilot/scene/chat_db/auto_execute/prompt.py b/pilot/scene/chat_db/auto_execute/prompt.py index cc4878c70..8acbeea34 100644 --- a/pilot/scene/chat_db/auto_execute/prompt.py +++ b/pilot/scene/chat_db/auto_execute/prompt.py @@ -1,10 +1,10 @@ import json -import importlib from pilot.prompts.prompt_new import PromptTemplate from pilot.configs.config import Config from pilot.scene.base import ChatScene from pilot.scene.chat_db.auto_execute.out_parser import DbChatOutputParser, SqlAction from pilot.common.schema import SeparatorStyle +from pilot.scene.chat_db.auto_execute.example import sql_data_example CFG = Config() @@ -12,35 +12,21 @@ PROMPT_SCENE_DEFINE = None _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. +You are a SQL expert. Given an input question, create a syntactically correct {dialect} query. + 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. Use as few tables as possible when querying. -When generating insert, delete, update, or replace SQL, please make sure to use the data given by the human, and cannot use any unknown data. If you do not get enough information, speak to user: I don’t have enough data complete your request. -Pay attention to use only the column names that you can see in the schema description. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table. - -""" - -PROMPT_SUFFIX = """Only use the following tables generate sql: +Only use the following tables schema to generate sql: {table_info} +Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table. Question: {input} -""" - -PROMPT_RESPONSE = """You must respond in JSON format as following format: +Rrespond in JSON format as following format: {response} - Ensure the response is correct json and can be parsed by Python json.loads """ -RESPONSE_FORMAT = { - "thoughts": { - "reasoning": "reasoning", - "speak": "thoughts summary to say to user", - }, - "sql": "SQL Query to run", -} - RESPONSE_FORMAT_SIMPLE = { "thoughts": "thoughts summary to say to user", "sql": "SQL Query to run", @@ -55,10 +41,11 @@ prompt = PromptTemplate( input_variables=["input", "table_info", "dialect", "top_k", "response"], response_format=json.dumps(RESPONSE_FORMAT_SIMPLE, indent=4), template_define=PROMPT_SCENE_DEFINE, - template=_DEFAULT_TEMPLATE + PROMPT_SUFFIX + PROMPT_RESPONSE, + template=_DEFAULT_TEMPLATE, stream_out=PROMPT_NEED_NEED_STREAM_OUT, output_parser=DbChatOutputParser( sep=PROMPT_SEP, is_stream_out=PROMPT_NEED_NEED_STREAM_OUT ), + example_selector=sql_data_example ) CFG.prompt_templates.update({prompt.template_scene: prompt}) diff --git a/pilot/scene/chat_execution/example.py b/pilot/scene/chat_execution/example.py index f50a7f546..35f0c634a 100644 --- a/pilot/scene/chat_execution/example.py +++ b/pilot/scene/chat_execution/example.py @@ -2,8 +2,38 @@ from pilot.prompts.example_base import ExampleSelector ## Two examples are defined by default EXAMPLES = [ - [{"system": "123"},{"system":"xxx"},{"human":"xxx"},{"assistant":"xxx"}], - [{"system": "123"},{"system":"xxx"},{"human":"xxx"},{"assistant":"xxx"}] + { + "messages": [ + {"type": "human", "data": {"content": "查询xxx", "example": True}}, + { + "type": "ai", + "data": { + "content": """{ + \"thoughts\": \"thought text\", + \"speak\": \"thoughts summary to say to user\", + \"command\": {\"name\": \"command name\", \"args\": {\"arg name\": \"value\"}}, + }""", + "example": True, + }, + }, + ] + }, + { + "messages": [ + {"type": "human", "data": {"content": "查询xxx", "example": True}}, + { + "type": "ai", + "data": { + "content": """{ + \"thoughts\": \"thought text\", + \"speak\": \"thoughts summary to say to user\", + \"command\": {\"name\": \"command name\", \"args\": {\"arg name\": \"value\"}}, + }""", + "example": True, + }, + }, + ] + }, ] plugin_example = ExampleSelector(examples_record=EXAMPLES, use_example=True) From fe662cec5e22d38c40aaa51ff6e31e8d37565688 Mon Sep 17 00:00:00 2001 From: "tuyang.yhj" Date: Thu, 29 Jun 2023 17:24:18 +0800 Subject: [PATCH 2/3] WEB API independent --- pilot/openapi/api_v1/api_v1.py | 5 +++-- pilot/scene/base_chat.py | 4 ++-- pilot/scene/chat_db/auto_execute/chat.py | 1 + pilot/scene/chat_db/auto_execute/example.py | 10 ++++------ pilot/scene/chat_db/auto_execute/out_parser.py | 18 ++++++++++++------ pilot/scene/chat_execution/chat.py | 1 + pilot/scene/chat_normal/out_parser.py | 1 + 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/pilot/openapi/api_v1/api_v1.py b/pilot/openapi/api_v1/api_v1.py index 740af4f05..17d9b88fe 100644 --- a/pilot/openapi/api_v1/api_v1.py +++ b/pilot/openapi/api_v1/api_v1.py @@ -252,7 +252,6 @@ async def stream_generator(chat): for chunk in model_response.iter_lines(decode_unicode=False, delimiter=b"\0"): if chunk: msg = chat.prompt_template.output_parser.parse_model_stream_resp_ex(chunk, chat.skip_echo_len) - chat.current_message.add_ai_message(msg) msg = msg.replace("\n", "\\n") yield f"data:{msg}\n\n" await asyncio.sleep(0.1) @@ -260,11 +259,13 @@ async def stream_generator(chat): for chunk in model_response: if chunk: msg = chat.prompt_template.output_parser.parse_model_stream_resp_ex(chunk, chat.skip_echo_len) - chat.current_message.add_ai_message(msg) msg = msg.replace("\n", "\\n") yield f"data:{msg}\n\n" await asyncio.sleep(0.1) + + chat.current_message.add_ai_message(msg) + chat.current_message.add_view_message(msg) chat.memory.append(chat.current_message) diff --git a/pilot/scene/base_chat.py b/pilot/scene/base_chat.py index 376d99ef8..6c005dbf3 100644 --- a/pilot/scene/base_chat.py +++ b/pilot/scene/base_chat.py @@ -148,8 +148,8 @@ class BaseChat(ABC): self.current_message.add_view_message( f"""ERROR!{str(e)}\n {ai_response_text} """ ) - ### store current conversation - self.memory.append(self.current_message) + ### store current conversation + self.memory.append(self.current_message) def nostream_call(self): payload = self.__call_base() diff --git a/pilot/scene/chat_db/auto_execute/chat.py b/pilot/scene/chat_db/auto_execute/chat.py index 613c660c7..5b5998024 100644 --- a/pilot/scene/chat_db/auto_execute/chat.py +++ b/pilot/scene/chat_db/auto_execute/chat.py @@ -54,4 +54,5 @@ class ChatWithDbAutoExecute(BaseChat): return input_values def do_action(self, prompt_response): + print(f"do_action:{prompt_response}") return self.database.run(self.db_connect, prompt_response.sql) diff --git a/pilot/scene/chat_db/auto_execute/example.py b/pilot/scene/chat_db/auto_execute/example.py index a2a01b44d..b4c248d65 100644 --- a/pilot/scene/chat_db/auto_execute/example.py +++ b/pilot/scene/chat_db/auto_execute/example.py @@ -4,14 +4,13 @@ from pilot.common.schema import ExampleType EXAMPLES = [ { "messages": [ - {"type": "human", "data": {"content": "查询xxx", "example": True}}, + {"type": "human", "data": {"content": "查询用户test1所在的城市", "example": True}}, { "type": "ai", "data": { "content": """{ \"thoughts\": \"thought text\", - \"speak\": \"thoughts summary to say to user\", - \"command\": {\"name\": \"command name\", \"args\": {\"arg name\": \"value\"}}, + \"sql\": \"SELECT city FROM users where user_name='test1'\", }""", "example": True, }, @@ -20,14 +19,13 @@ EXAMPLES = [ }, { "messages": [ - {"type": "human", "data": {"content": "查询xxx", "example": True}}, + {"type": "human", "data": {"content": "查询成都的用户的订单信息", "example": True}}, { "type": "ai", "data": { "content": """{ \"thoughts\": \"thought text\", - \"speak\": \"thoughts summary to say to user\", - \"command\": {\"name\": \"command name\", \"args\": {\"arg name\": \"value\"}}, + \"sql\": \"SELECT b.* FROM users a LEFT JOIN tran_order b ON a.user_name=b.user_name where a.city='成都'\", }""", "example": True, }, diff --git a/pilot/scene/chat_db/auto_execute/out_parser.py b/pilot/scene/chat_db/auto_execute/out_parser.py index 94cc6ea9e..eaa45498c 100644 --- a/pilot/scene/chat_db/auto_execute/out_parser.py +++ b/pilot/scene/chat_db/auto_execute/out_parser.py @@ -6,8 +6,9 @@ import pandas as pd from pilot.utils import build_logger from pilot.out_parser.base import BaseOutputParser, T from pilot.configs.model_config import LOGDIR +from pilot.configs.config import Config - +CFG = Config() class SqlAction(NamedTuple): sql: str thoughts: Dict @@ -32,11 +33,16 @@ class DbChatOutputParser(BaseOutputParser): if len(data) <= 1: data.insert(0, ["result"]) df = pd.DataFrame(data[1:], columns=data[0]) - table_style = """""" - html_table = df.to_html(index=False, escape=False) - html = f"{table_style}{html_table}" + if CFG.NEW_SERVER_MODE: + html = df.to_html(index=False, escape=False, sparsify=False) + html = ''.join(html.split()) + else: + table_style = """""" + html_table = df.to_html(index=False, escape=False) + html = f"{table_style}{html_table}" + view_text = f"##### {str(speak)}" + "\n" + html.replace("\n", " ") return view_text diff --git a/pilot/scene/chat_execution/chat.py b/pilot/scene/chat_execution/chat.py index 97646c299..1c3735ac3 100644 --- a/pilot/scene/chat_execution/chat.py +++ b/pilot/scene/chat_execution/chat.py @@ -63,6 +63,7 @@ class ChatWithPlugin(BaseChat): return input_values def do_action(self, prompt_response): + print(f"do_action:{prompt_response}") ## plugin command run return execute_command( str(prompt_response.command.get("name")), diff --git a/pilot/scene/chat_normal/out_parser.py b/pilot/scene/chat_normal/out_parser.py index e5edc9b20..112974176 100644 --- a/pilot/scene/chat_normal/out_parser.py +++ b/pilot/scene/chat_normal/out_parser.py @@ -12,6 +12,7 @@ logger = build_logger("webserver", LOGDIR + "DbChatOutputParser.log") class NormalChatOutputParser(BaseOutputParser): + def parse_prompt_response(self, model_out_text) -> T: return model_out_text From 20666baa7cf997ff4e972e59a091ad51c6fa9987 Mon Sep 17 00:00:00 2001 From: "tuyang.yhj" Date: Thu, 29 Jun 2023 18:53:52 +0800 Subject: [PATCH 3/3] WEB API independent --- pilot/connections/rdbms/py_study/study_data.py | 10 ++++++++++ pilot/scene/chat_dashboard/chat.py | 6 ++++++ .../chat_dashboard/data_preparation/report_schma.py | 2 ++ 3 files changed, 18 insertions(+) create mode 100644 pilot/connections/rdbms/py_study/study_data.py diff --git a/pilot/connections/rdbms/py_study/study_data.py b/pilot/connections/rdbms/py_study/study_data.py new file mode 100644 index 000000000..c19c39f33 --- /dev/null +++ b/pilot/connections/rdbms/py_study/study_data.py @@ -0,0 +1,10 @@ +from pilot.common.sql_database import Database +from pilot.configs.config import Config + +CFG = Config() + +if __name__ == "__main__": + connect = CFG.local_db.get_session("gpt-user") + datas = CFG.local_db.run(connect, "SELECT * FROM users; ") + + print(datas) \ No newline at end of file diff --git a/pilot/scene/chat_dashboard/chat.py b/pilot/scene/chat_dashboard/chat.py index a9f84e431..dd614399c 100644 --- a/pilot/scene/chat_dashboard/chat.py +++ b/pilot/scene/chat_dashboard/chat.py @@ -65,6 +65,12 @@ class ChatDashboard(BaseChat): try: datas = self.database.run(self.db_connect, chart_item.sql) chart_data: ChartData = ChartData() + chart_data.chart_sql = chart_item['sql'] + chart_data.chart_type = chart_item['showcase'] + chart_data.chart_name = chart_item['title'] + chart_data.chart_desc = chart_item['thoughts'] + chart_data.column_name = datas[0] + chart_data.values =datas except Exception as e: # TODO 修复流程 print(str(e)) diff --git a/pilot/scene/chat_dashboard/data_preparation/report_schma.py b/pilot/scene/chat_dashboard/data_preparation/report_schma.py index 84468f02e..9327fc4d5 100644 --- a/pilot/scene/chat_dashboard/data_preparation/report_schma.py +++ b/pilot/scene/chat_dashboard/data_preparation/report_schma.py @@ -4,7 +4,9 @@ from typing import TypeVar, Union, List, Generic, Any class ChartData(BaseModel): chart_uid: str + chart_name: str chart_type: str + chart_desc: str chart_sql: str column_name: List values: List