mirror of
https://github.com/csunny/DB-GPT.git
synced 2025-09-03 18:17:45 +00:00
支持插件的prompt
This commit is contained in:
@@ -46,6 +46,8 @@ class Config(metaclass=Singleton):
|
|||||||
self.plugins: List[AutoGPTPluginTemplate] = []
|
self.plugins: List[AutoGPTPluginTemplate] = []
|
||||||
self.plugins_openai = []
|
self.plugins_openai = []
|
||||||
|
|
||||||
|
self.command_registry = []
|
||||||
|
|
||||||
self.huggingface_api_token = os.getenv("HUGGINGFACE_API_TOKEN")
|
self.huggingface_api_token = os.getenv("HUGGINGFACE_API_TOKEN")
|
||||||
self.image_provider = os.getenv("IMAGE_PROVIDER")
|
self.image_provider = os.getenv("IMAGE_PROVIDER")
|
||||||
self.image_size = int(os.getenv("IMAGE_SIZE", 256))
|
self.image_size = int(os.getenv("IMAGE_SIZE", 256))
|
||||||
|
@@ -36,8 +36,8 @@ ISDEBUG = False
|
|||||||
|
|
||||||
|
|
||||||
DB_SETTINGS = {
|
DB_SETTINGS = {
|
||||||
"user": "puzzle@puzzle0_1954#dev_ipay10",
|
"user": "root",
|
||||||
"password": "puzzle123",
|
"password": "root",
|
||||||
"host": "obproxy.ocp2.alipay.net",
|
"host": "127.0.0.1",
|
||||||
"port": 2883
|
"port": 3306
|
||||||
}
|
}
|
@@ -48,7 +48,6 @@ class Conversation:
|
|||||||
else:
|
else:
|
||||||
ret += role + ":"
|
ret += role + ":"
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Invalid style: {self.sep_style}")
|
raise ValueError(f"Invalid style: {self.sep_style}")
|
||||||
|
|
||||||
@@ -104,12 +103,12 @@ def gen_sqlgen_conversation(dbname):
|
|||||||
|
|
||||||
|
|
||||||
conv_one_shot = Conversation(
|
conv_one_shot = Conversation(
|
||||||
system="A chat between a curious human and an artificial intelligence assistant, who very familiar with database related knowledge. "
|
system="A chat between a curious user and an artificial intelligence assistant, who very familiar with database related knowledge. "
|
||||||
"The assistant gives helpful, detailed, professional and polite answers to the human's questions. ",
|
"The assistant gives helpful, detailed, professional and polite answers to the user's questions. ",
|
||||||
roles=("Human", "Assistant"),
|
roles=("USER", "Assistant"),
|
||||||
messages=(
|
messages=(
|
||||||
(
|
(
|
||||||
"Human",
|
"USER",
|
||||||
"What are the key differences between mysql and postgres?",
|
"What are the key differences between mysql and postgres?",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
@@ -155,12 +154,11 @@ auto_dbgpt_one_shot = Conversation(
|
|||||||
messages=(
|
messages=(
|
||||||
(
|
(
|
||||||
"USER",
|
"USER",
|
||||||
""" Answer how many users does hackernews have by query mysql database
|
""" Answer how many users does app_users have by query ob database
|
||||||
Constraints:
|
Constraints:
|
||||||
1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.
|
1. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.
|
||||||
2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.
|
2. No user assistance
|
||||||
3. No user assistance
|
3. Exclusively use the commands listed in double quotes e.g. "command name"
|
||||||
4. Exclusively use the commands listed in double quotes e.g. "command name"
|
|
||||||
|
|
||||||
Commands:
|
Commands:
|
||||||
1. analyze_code: Analyze Code, args: "code": "<full_code_string>"
|
1. analyze_code: Analyze Code, args: "code": "<full_code_string>"
|
||||||
@@ -170,13 +168,12 @@ auto_dbgpt_one_shot = Conversation(
|
|||||||
5. list_files: List Files in Directory, args: "directory": "<directory>"
|
5. list_files: List Files in Directory, args: "directory": "<directory>"
|
||||||
6. read_file: Read file, args: "filename": "<filename>"
|
6. read_file: Read file, args: "filename": "<filename>"
|
||||||
7. write_to_file: Write to file, args: "filename": "<filename>", "text": "<text>"
|
7. write_to_file: Write to file, args: "filename": "<filename>", "text": "<text>"
|
||||||
8. tidb_sql_executor: "Execute SQL in TiDB Database.", args: "sql": "<sql>"
|
8. ob_sql_executor: "Execute SQL in OB Database.", args: "sql": "<sql>"
|
||||||
|
|
||||||
Resources:
|
Resources:
|
||||||
1. Internet access for searches and information gathering.
|
1. Internet access for searches and information gathering.
|
||||||
2. Long Term memory management.
|
2. Long Term memory management.
|
||||||
3. vicuna powered Agents for delegation of simple tasks.
|
3. vicuna powered Agents for delegation of simple tasks.
|
||||||
4. File output.
|
|
||||||
|
|
||||||
Performance Evaluation:
|
Performance Evaluation:
|
||||||
1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.
|
1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.
|
||||||
@@ -210,16 +207,16 @@ auto_dbgpt_one_shot = Conversation(
|
|||||||
"""
|
"""
|
||||||
{
|
{
|
||||||
"thoughts": {
|
"thoughts": {
|
||||||
"text": "thought",
|
"text": "To answer how many users by query database we need to write SQL query to get the count of the distinct users from the database. We can use ob_sql_executor command to execute the SQL query in database.",
|
||||||
"reasoning": "reasoning",
|
"reasoning": "We can use the sql_executor command to execute the SQL query for getting count of distinct users from the users database. We can select the count of the distinct users from the users table.",
|
||||||
"plan": "- short bulleted\n- list that conveys\n- long-term plan",
|
"plan": "- Write SQL query to get count of distinct users from users database\n- Use ob_sql_executor to execute the SQL query in OB database\n- Parse the SQL result to get the count\n- Respond with the count as the answer",
|
||||||
"criticism": "constructive self-criticism",
|
"criticism": "None",
|
||||||
"speak": "thoughts summary to say to user"
|
"speak": "To get the number of users in users, I will execute an SQL query in OB database using the ob_sql_executor command and respond with the count."
|
||||||
},
|
},
|
||||||
"command": {
|
"command": {
|
||||||
"name": "command name",
|
"name": "ob_sql_executor",
|
||||||
"args": {
|
"args": {
|
||||||
"arg name": "value"
|
"sql": "SELECT COUNT(DISTINCT(*)) FROM users ;"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -227,18 +224,18 @@ auto_dbgpt_one_shot = Conversation(
|
|||||||
)
|
)
|
||||||
),
|
),
|
||||||
offset=0,
|
offset=0,
|
||||||
sep_style=SeparatorStyle.THREE,
|
sep_style=SeparatorStyle.SINGLE,
|
||||||
sep=" ",
|
sep=" ",
|
||||||
sep2="</s>",
|
sep2="</s>",
|
||||||
)
|
)
|
||||||
|
|
||||||
auto_dbgpt_without_shot = Conversation(
|
auto_dbgpt_without_shot = Conversation(
|
||||||
system="You are DB-GPT, an AI designed to answer questions about HackerNews by query `hackerbews` database in MySQL. "
|
system="You are DB-GPT, an AI designed to answer questions about users by query `users` database in MySQL. "
|
||||||
"Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications.",
|
"Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications.",
|
||||||
roles=("USER", "ASSISTANT"),
|
roles=("USER", "ASSISTANT"),
|
||||||
messages=(),
|
messages=(),
|
||||||
offset=0,
|
offset=0,
|
||||||
sep_style=SeparatorStyle.FOUR,
|
sep_style=SeparatorStyle.SINGLE,
|
||||||
sep=" ",
|
sep=" ",
|
||||||
sep2="</s>",
|
sep2="</s>",
|
||||||
)
|
)
|
||||||
@@ -262,6 +259,7 @@ conversation_types = {
|
|||||||
conv_templates = {
|
conv_templates = {
|
||||||
"conv_one_shot": conv_one_shot,
|
"conv_one_shot": conv_one_shot,
|
||||||
"vicuna_v1": conv_vicuna_v1,
|
"vicuna_v1": conv_vicuna_v1,
|
||||||
|
"auto_dbgpt_one_shot": auto_dbgpt_one_shot
|
||||||
}
|
}
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@@ -66,7 +66,7 @@ class FirstPrompt:
|
|||||||
prompt_generator.goals = fisrt_message
|
prompt_generator.goals = fisrt_message
|
||||||
prompt_generator.command_registry = command_registry
|
prompt_generator.command_registry = command_registry
|
||||||
# 加载插件中可用命令
|
# 加载插件中可用命令
|
||||||
cfg = Config
|
cfg = Config()
|
||||||
for plugin in cfg.plugins:
|
for plugin in cfg.plugins:
|
||||||
if not plugin.can_handle_post_prompt():
|
if not plugin.can_handle_post_prompt():
|
||||||
continue
|
continue
|
||||||
|
@@ -44,7 +44,7 @@ def build_default_prompt_generator() -> PromptGenerator:
|
|||||||
prompt_generator.add_resource(
|
prompt_generator.add_resource(
|
||||||
"GPT-3.5 powered Agents for delegation of simple tasks."
|
"GPT-3.5 powered Agents for delegation of simple tasks."
|
||||||
)
|
)
|
||||||
prompt_generator.add_resource("File output.")
|
# prompt_generator.add_resource("File output.")
|
||||||
|
|
||||||
# Add performance evaluations to the PromptGenerator object
|
# Add performance evaluations to the PromptGenerator object
|
||||||
prompt_generator.add_performance_evaluation(
|
prompt_generator.add_performance_evaluation(
|
||||||
@@ -61,5 +61,5 @@ def build_default_prompt_generator() -> PromptGenerator:
|
|||||||
"Every command has a cost, so be smart and efficient. Aim to complete tasks in"
|
"Every command has a cost, so be smart and efficient. Aim to complete tasks in"
|
||||||
" the least number of steps."
|
" the least number of steps."
|
||||||
)
|
)
|
||||||
prompt_generator.add_performance_evaluation("Write all code to a file.")
|
# prompt_generator.add_performance_evaluation("Write all code to a file.")
|
||||||
return prompt_generator
|
return prompt_generator
|
||||||
|
@@ -161,6 +161,8 @@ def post_process_code(code):
|
|||||||
|
|
||||||
def http_bot(state, mode, db_selector, temperature, max_new_tokens, request: gr.Request):
|
def http_bot(state, mode, db_selector, temperature, max_new_tokens, request: gr.Request):
|
||||||
|
|
||||||
|
# MOCk
|
||||||
|
autogpt = True
|
||||||
print("是否是AUTO-GPT模式.", autogpt)
|
print("是否是AUTO-GPT模式.", autogpt)
|
||||||
start_tstamp = time.time()
|
start_tstamp = time.time()
|
||||||
model_name = LLM_MODEL
|
model_name = LLM_MODEL
|
||||||
@@ -175,25 +177,37 @@ def http_bot(state, mode, db_selector, temperature, max_new_tokens, request: gr.
|
|||||||
|
|
||||||
# TODO when tab mode is AUTO_GPT, Prompt need to rebuild.
|
# TODO when tab mode is AUTO_GPT, Prompt need to rebuild.
|
||||||
if len(state.messages) == state.offset + 2:
|
if len(state.messages) == state.offset + 2:
|
||||||
# 第一轮对话需要加入提示Prompt
|
|
||||||
|
|
||||||
template_name = "conv_one_shot"
|
|
||||||
new_state = conv_templates[template_name].copy()
|
|
||||||
new_state.conv_id = uuid.uuid4().hex
|
|
||||||
|
|
||||||
query = state.messages[-2][1]
|
query = state.messages[-2][1]
|
||||||
|
# 第一轮对话需要加入提示Prompt
|
||||||
|
if(autogpt):
|
||||||
|
# autogpt模式的第一轮对话需要 构建专属prompt
|
||||||
|
cfg = Config()
|
||||||
|
first_prompt = FirstPrompt()
|
||||||
|
first_prompt.command_registry = cfg.command_registry
|
||||||
|
|
||||||
# prompt 中添加上下文提示, 根据已有知识对话, 上下文提示是否也应该放在第一轮, 还是每一轮都添加上下文?
|
system_prompt = first_prompt.construct_first_prompt(fisrt_message=[query])
|
||||||
# 如果用户侧的问题跨度很大, 应该每一轮都加提示。
|
logger.info("[TEST]:" + system_prompt)
|
||||||
if db_selector:
|
template_name = "auto_dbgpt_one_shot"
|
||||||
new_state.append_message(new_state.roles[0], gen_sqlgen_conversation(dbname) + query)
|
new_state = conv_templates[template_name].copy()
|
||||||
new_state.append_message(new_state.roles[1], None)
|
new_state.append_message(role='USER', message=system_prompt)
|
||||||
state = new_state
|
|
||||||
else:
|
else:
|
||||||
new_state.append_message(new_state.roles[0], query)
|
template_name = "conv_one_shot"
|
||||||
new_state.append_message(new_state.roles[1], None)
|
new_state = conv_templates[template_name].copy()
|
||||||
state = new_state
|
|
||||||
|
|
||||||
|
new_state.conv_id = uuid.uuid4().hex
|
||||||
|
|
||||||
|
if not autogpt:
|
||||||
|
# prompt 中添加上下文提示, 根据已有知识对话, 上下文提示是否也应该放在第一轮, 还是每一轮都添加上下文?
|
||||||
|
# 如果用户侧的问题跨度很大, 应该每一轮都加提示。
|
||||||
|
if db_selector:
|
||||||
|
new_state.append_message(new_state.roles[0], gen_sqlgen_conversation(dbname) + query)
|
||||||
|
new_state.append_message(new_state.roles[1], None)
|
||||||
|
state = new_state
|
||||||
|
else:
|
||||||
|
new_state.append_message(new_state.roles[0], query)
|
||||||
|
new_state.append_message(new_state.roles[1], None)
|
||||||
|
|
||||||
|
state = new_state
|
||||||
if mode == conversation_types["default_knownledge"] and not db_selector:
|
if mode == conversation_types["default_knownledge"] and not db_selector:
|
||||||
query = state.messages[-2][1]
|
query = state.messages[-2][1]
|
||||||
knqa = KnownLedgeBaseQA()
|
knqa = KnownLedgeBaseQA()
|
||||||
@@ -457,8 +471,6 @@ if __name__ == "__main__":
|
|||||||
# 加载插件
|
# 加载插件
|
||||||
cfg = Config()
|
cfg = Config()
|
||||||
|
|
||||||
cfg.plugins_dir = "123"
|
|
||||||
|
|
||||||
cfg.set_plugins(scan_plugins(cfg, cfg.debug_mode))
|
cfg.set_plugins(scan_plugins(cfg, cfg.debug_mode))
|
||||||
|
|
||||||
# 加载插件可执行命令
|
# 加载插件可执行命令
|
||||||
@@ -474,15 +486,9 @@ if __name__ == "__main__":
|
|||||||
for command_category in command_categories:
|
for command_category in command_categories:
|
||||||
command_registry.import_commands(command_category)
|
command_registry.import_commands(command_category)
|
||||||
|
|
||||||
|
cfg.command_registry =command_category
|
||||||
|
|
||||||
|
|
||||||
first_prompt =FirstPrompt(cfg= cfg)
|
|
||||||
first_prompt.command_registry = command_registry
|
|
||||||
|
|
||||||
system_prompt = first_prompt.construct_first_prompt( fisrt_message=["this is a test goal"])
|
|
||||||
|
|
||||||
logger.info("[TEST]:" + system_prompt)
|
|
||||||
|
|
||||||
logger.info(args)
|
logger.info(args)
|
||||||
demo = build_webdemo()
|
demo = build_webdemo()
|
||||||
demo.queue(
|
demo.queue(
|
||||||
|
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user