Skip to content

Commit

Permalink
feat: enabel to create bot by text description
Browse files Browse the repository at this point in the history
  • Loading branch information
xingwanying committed May 9, 2024
1 parent 38e100d commit 9a4356e
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 41 deletions.
2 changes: 1 addition & 1 deletion lui/src/services/ChatController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function streamChat(
messages: IPrompt[],
host = 'http://127.0.0.1:8000',
): Promise<Response> {
return fetch(`${host}/api/chat/qa`, {
return fetch(`${host}/api/chat/builder`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
17 changes: 10 additions & 7 deletions server/agent/bot_builder.py
Original file line number Diff line number Diff line change
@@ -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:修改机器人的配置
- 根据用户的描述进行机器人的配置信息修改。
Expand All @@ -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)
32 changes: 4 additions & 28 deletions server/agent/qa_chat.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
Empty file removed server/prompts/bot_builder.py
Empty file.
Empty file removed server/prompts/bot_generator.py
Empty file.
30 changes: 30 additions & 0 deletions server/prompts/bot_template.py
Original file line number Diff line number Diff line change
@@ -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)
46 changes: 46 additions & 0 deletions server/tools/bot_builder.py
Original file line number Diff line number Diff line change
@@ -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

4 changes: 2 additions & 2 deletions server/tools/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
Expand Down
2 changes: 1 addition & 1 deletion server/tools/knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 有哪些新特性?'.
Expand Down
4 changes: 2 additions & 2 deletions server/type_class/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,4 +23,4 @@ class BotUpdateRequest(BaseModel):
name: Optional[str] = None
starters: Optional[List[str]] = None
voice: Optional[str] = None
public: Optional[bool] = None
public: Optional[bool] = None

0 comments on commit 9a4356e

Please sign in to comment.