From 9a4356e7d944ae5884ea2d9cf711a1c51e8f50e5 Mon Sep 17 00:00:00 2001 From: yingying Date: Fri, 10 May 2024 04:55:51 +0800 Subject: [PATCH] feat: enabel to create bot by text description --- lui/src/services/ChatController.ts | 2 +- server/agent/bot_builder.py | 17 ++++++----- server/agent/qa_chat.py | 32 +++------------------ server/prompts/bot_builder.py | 0 server/prompts/bot_generator.py | 0 server/prompts/bot_template.py | 30 +++++++++++++++++++ server/tools/bot_builder.py | 46 ++++++++++++++++++++++++++++++ server/tools/issue.py | 4 +-- server/tools/knowledge.py | 2 +- server/type_class/bot.py | 4 +-- 10 files changed, 96 insertions(+), 41 deletions(-) delete mode 100644 server/prompts/bot_builder.py delete mode 100644 server/prompts/bot_generator.py create mode 100644 server/prompts/bot_template.py create mode 100644 server/tools/bot_builder.py diff --git a/lui/src/services/ChatController.ts b/lui/src/services/ChatController.ts index 7b8ad13d..3964331f 100644 --- a/lui/src/services/ChatController.ts +++ b/lui/src/services/ChatController.ts @@ -8,7 +8,7 @@ export async function streamChat( messages: IPrompt[], host = 'http://127.0.0.1:8000', ): Promise { - return fetch(`${host}/api/chat/qa`, { + return fetch(`${host}/api/chat/builder`, { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/server/agent/bot_builder.py b/server/agent/bot_builder.py index c75f7df7..74ad0483 100644 --- a/server/agent/bot_builder.py +++ b/server/agent/bot_builder.py @@ -1,18 +1,19 @@ from typing import AsyncIterator from data_class import ChatData from agent.base import AgentBuilder +from tools import bot_builder PROMPT = """ # 角色 -你是一 GitHub 答疑机器创建助手。你擅长根据用户提供的 Github 仓库信息创建一个答疑机器人。 +你是一名 GitHub 答疑机器创建助手。你擅长根据用户提供的 Github 仓库信息创建一个答疑机器人。 ## 技能 -### 技能1:获取并确认仓库信息 -- 引导用户提供他们的GitHub仓库信息。 -- 根据提供的信息确认这个仓库存在并且可以访问。 +### 技能1:获取 GitHub 仓库名 +- 引导用户提供他们的 GitHub 仓库名或地址。 +- 根据提供的 GitHub 地址提取 GitHub 仓库名。 ### 技能2:创建答疑机器人 -- 使用bot_builder工具根据用户提供的Github仓库信息创建机器人。 +- 使用 bot_builder 工具根据用户提供的 Github 仓库名创建机器人。 ### 技能3:修改机器人的配置 - 根据用户的描述进行机器人的配置信息修改。 @@ -24,8 +25,10 @@ """ -TOOL_MAPPING = {} +TOOL_MAPPING = { + "bot_builder": bot_builder.create_bot, +} def agent_chat(input_data: ChatData) -> AsyncIterator[str]: - agent = AgentBuilder(prompt=PROMPT, tools={}, enable_tavily=False) + agent = AgentBuilder(prompt=PROMPT, tools=TOOL_MAPPING, enable_tavily=False) return agent.run_chat(input_data) diff --git a/server/agent/qa_chat.py b/server/agent/qa_chat.py index 60b94eff..316517af 100644 --- a/server/agent/qa_chat.py +++ b/server/agent/qa_chat.py @@ -1,35 +1,10 @@ from typing import AsyncIterator from data_class import ChatData from agent.base import AgentBuilder +from prompts.bot_template import generate_prompt_by_repo_name from tools import issue, sourcecode, knowledge -PROMPT = """ -# Character -You are a skilled assistant dedicated to Ant Design, capable of delivering comprehensive insights and solutions pertaining to Ant Design. You excel in fixing code issues correlated with Ant Design. - -## Skills -### Skill 1: Engaging Interaction -Your primary role involves engaging with users, offering them in-depth responses to their Ant Design inquiries in a conversational fashion. - -### Skill 2: Insightful Information Search -For queries that touch upon unfamiliar zones, you are equipped with two powerful knowledge lookup tools, used to gather necessary details: - - search_knowledge: This is your initial resource for queries concerning ambiguous topics about Ant Design. While using this, ensure to retain the user's original query language for the highest accuracy possible. Therefore, a specific question like 'Ant Design的新特性是什么?' should be searched as 'Ant Design的新特性是什么?'. - - tavily_search_results_json: Should search_knowledge fail to accommodate the required facts, this tool would be the next step. - -### Skill 3: Expert Issue Solver -In case of specific issues reported by users, you are to aid them using a selection of bespoke tools, curated as per the issue nature and prescribed steps. The common instances cater to: - - Routine engagement with the user. - - Employment of certain tools such as create_issue, get_issues, search_issues, search_code etc. when the user is facing a specific hurdle. - -## Constraints: -- Maintain a strict focus on Ant Design in your responses; if confronted with unrelated queries, politely notify the user of your confines and steer them towards asking questions relevant to Ant Design. -- Your tool utilization choices should be driven by the nature of the inquiry and recommended actions. -- While operating tools for searching information, keep the user's original language to attain utmost precision. -- With your multilingual capability, always respond in the user's language. If the inquiry popped is in English, your response should mirror that; same goes for Chinese or any other language. -""" - - TOOL_MAPPING = { "search_knowledge": knowledge.search_knowledge, "create_issue": issue.create_issue, @@ -39,5 +14,6 @@ } def agent_chat(input_data: ChatData) -> AsyncIterator[str]: - agent = AgentBuilder(prompt=PROMPT, tools=TOOL_MAPPING) - return agent.run_chat(input_data) + prompt = generate_prompt_by_repo_name("ant-design") + agent = AgentBuilder(prompt=prompt, tools=TOOL_MAPPING) + return agent.run_chat(input_data) diff --git a/server/prompts/bot_builder.py b/server/prompts/bot_builder.py deleted file mode 100644 index e69de29b..00000000 diff --git a/server/prompts/bot_generator.py b/server/prompts/bot_generator.py deleted file mode 100644 index e69de29b..00000000 diff --git a/server/prompts/bot_template.py b/server/prompts/bot_template.py new file mode 100644 index 00000000..e8637148 --- /dev/null +++ b/server/prompts/bot_template.py @@ -0,0 +1,30 @@ + + + +PROMPT = """ +# Character +You are a skilled assistant dedicated to {repo_name}, capable of delivering comprehensive insights and solutions pertaining to {repo_name}. You excel in fixing code issues correlated with {repo_name}. + +## Skills +### Skill 1: Engaging Interaction +Your primary role involves engaging with users, offering them in-depth responses to their {repo_name} inquiries in a conversational fashion. + +### Skill 2: Insightful Information Search +For queries that touch upon unfamiliar zones, you are equipped with two powerful knowledge lookup tools, used to gather necessary details: + - search_knowledge: This is your initial resource for queries concerning ambiguous topics about {repo_name}. While using this, ensure to retain the user's original query language for the highest accuracy possible. Therefore, a specific question like '{repo_name} 的新特性是什么?' should be searched as '{repo_name} 的新特性是什么?'. + - tavily_search_results_json: Should search_knowledge fail to accommodate the required facts, this tool would be the next step. + +### Skill 3: Expert Issue Solver +In case of specific issues reported by users, you are to aid them using a selection of bespoke tools, curated as per the issue nature and prescribed steps. The common instances cater to: + - Routine engagement with the user. + - Employment of certain tools such as create_issue, get_issues, search_issues, search_code etc. when the user is facing a specific hurdle. + +## Constraints: +- Maintain a strict focus on {repo_name} in your responses; if confronted with unrelated queries, politely notify the user of your confines and steer them towards asking questions relevant to {repo_name}. +- Your tool utilization choices should be driven by the nature of the inquiry and recommended actions. +- While operating tools for searching information, keep the user's original language to attain utmost precision. +- With your multilingual capability, always respond in the user's language. If the inquiry popped is in English, your response should mirror that; same goes for Chinese or any other language. +""" + +def generate_prompt_by_repo_name(repo_name: str): + return PROMPT.format(repo_name=repo_name) diff --git a/server/tools/bot_builder.py b/server/tools/bot_builder.py new file mode 100644 index 00000000..76d50937 --- /dev/null +++ b/server/tools/bot_builder.py @@ -0,0 +1,46 @@ +from langchain.tools import tool +from github import Github +from db.supabase.client import get_client +from prompts.bot_template import generate_prompt_by_repo_name + +g = Github() + +@tool +def create_bot( + repo_name: str, + starters: list[str] = ["介绍一下项目", "快速上手", "贡献指南"], +): + """ + create a bot based on the given github repository. + + :param repo_name: The name of the repository, e.g., "octocat/Hello-World" + :param starters: The Opening Dialog, e.g.["介绍一下项目", "快速上手", "贡献指南"] + """ + try: + # Step1:Get the repository object + repo = g.get_repo(repo_name) + + # Step2: Generate the prompt + prompt = generate_prompt_by_repo_name(repo_name) + + # Step3: Create bot based on the prompt + bot_data = { + "name": repo.name, + "description": repo.description, + "avatar": repo.organization.avatar_url if repo.organization else None, + "prompt": prompt, + "uid": "u123456", # TODO get from auth + "enable_img_generation": False, + "label": "Assistant", + "starters": starters, + "enable_img_generation": False, + "public": False, + } + print('bot_data', bot_data) + supabase = get_client() + response = supabase.table("bots").insert(bot_data).execute() + return response + except Exception as e: + print(f"An error occurred: {e}") + return e + diff --git a/server/tools/issue.py b/server/tools/issue.py index 525a55b4..778baf61 100644 --- a/server/tools/issue.py +++ b/server/tools/issue.py @@ -54,8 +54,8 @@ def get_issues( issues_list = [ { - 'issue_name': f'Issue #{issue.number} - {issue.title}', - 'issue_url': issue.html_url + "issue_name": f"Issue #{issue.number} - {issue.title}", + "issue_url": issue.html_url } for issue in issues ] diff --git a/server/tools/knowledge.py b/server/tools/knowledge.py index bd6c8480..53e15e22 100644 --- a/server/tools/knowledge.py +++ b/server/tools/knowledge.py @@ -4,7 +4,7 @@ @tool def search_knowledge( - query: str, + query: str, ): """ Search for information based on the query. When use this tool, do not translate the search query. Use the original query language to search. eg: When user's question is 'Ant Design 有哪些新特性?', the query should be 'Ant Design 有哪些新特性?'. diff --git a/server/type_class/bot.py b/server/type_class/bot.py index 5da7154b..b104eaa0 100644 --- a/server/type_class/bot.py +++ b/server/type_class/bot.py @@ -12,7 +12,7 @@ class BotCreateRequest(BaseModel): name: Optional[str] = None starters: Optional[List[str]] = None # 假设为起始文本列表 voice: Optional[str] = None - public: Optional[bool] = None + public: Optional[bool] = False class BotUpdateRequest(BaseModel): avatar: Optional[str] = None description: Optional[str] = None @@ -23,4 +23,4 @@ class BotUpdateRequest(BaseModel): name: Optional[str] = None starters: Optional[List[str]] = None voice: Optional[str] = None - public: Optional[bool] = None \ No newline at end of file + public: Optional[bool] = None