Skip to content

Commit

Permalink
add api assistant
Browse files Browse the repository at this point in the history
  • Loading branch information
ZiTao-Li committed Apr 19, 2024
1 parent dd0d328 commit ec3d8c0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
32 changes: 31 additions & 1 deletion examples/conversation_with_RAG_agents/configs/agent_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,43 @@
"persist_dir": "../../rag_storage/code_assist"
}
}
},
{
"class": "LlamaIndexAgent",
"args": {
"name": "API-Assistant",
"description": "API-Assistant is an agent that can answer questions about APIs in AgentScope. It can answer general questions about AgentScope.",
"sys_prompt": "You're an assistant providing answers to the questions related to APIs (functions and classes) in AgentScope. The language style is helpful and cheerful. You generate answers based on the provided context. The answer is expected to be no longer than 200 words. If the key words of the question can be found in the provided context, the answer should contain the module of the API. For example, 'You may refer to MODULE_NAME for more details.'",
"model_config_name": "qwen_config",
"emb_model_config_name": "qwen_emb_config",
"rag_config": {
"load_data": {
"loader": {
"create_object": true,
"module": "llama_index.core",
"class": "SimpleDirectoryReader",
"init_args": {
"input_dir": "../../docs/docstring_html/",
"required_exts": [".html"]
}
}
},
"chunk_size": 2048,
"chunk_overlap": 40,
"similarity_top_k": 3,
"log_retrieval": true,
"recent_n_mem": 1,
"persist_dir": "../../rag_storage/api_assist",
"repo_base": "../../"
}
}
},
{
"class": "DialogAgent",
"args": {
"name": "Agent-Guiding-Assistant",
"description": "Agent-Guiding-Assistant is an agent that decide which agent should provide the answer next. It can answer questions about specific functions and classes in AgentScope.",
"sys_prompt": "You're an assistant guiding the user to specific agent for help. The answer is in a cheerful stypled language. The output starts with appreciation for the question. Next, rephrase the question in a simple declarative Sentence for example, 'I think you are asking...'. Last, if the question is a coding question, output '@ Code-Search-Assistant, you might be suitable for answering the question, otherwise, output '@ Tutorial-Assistant, I think you are more suitable for the question, please tell us more about it. The answer is expected to be one line only",
"sys_prompt": "You're an assistant guiding the user to specific agent for help. The answer is in a cheerful styled language. The output starts with appreciation for the question. Next, rephrase the question in a simple declarative Sentence for example, 'I think you are asking...'. Last, if the question is about detailed code or example in AgentScope Framework, output '@ Code-Search-Assistant you might be suitable for answering the question'; if the question is about API or function calls (Example: 'Is there function related...' or 'how can I initialize ...' ) in AgentScope, output '@ API-Assistant, I think you are more suitable for the question, please tell us more about it'; otherwise, output '@ Tutorial-Assistant, I think you are more suitable for the question, please tell us more about it'. The answer is expected to be one line only",
"model_config_name": "qwen_config",
"use_memory": false
}
Expand Down
46 changes: 33 additions & 13 deletions examples/conversation_with_RAG_agents/rag_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
from agentscope.agents import DialogAgent


def prepare_docstring_html(repo_path: str, html_dir: str) -> None:
"""prepare docstring in html for API assistant"""
os.system(
f"sphinx-apidoc -f -o {repo_path}/docs/sphinx_doc/en/source "
f"{repo_path}/src/agentscope -t template",
)
os.system(
f"sphinx-build -b html {repo_path}/docs/sphinx_doc/en/source "
f"{html_dir} -W --keep-going",
)


def main() -> None:
"""A RAG multi-agent demo"""
with open("configs/model_config.json", "r", encoding="utf-8") as f:
Expand All @@ -35,25 +47,32 @@ def main() -> None:
# define RAG-based agents for tutorial and code
tutorial_agent = LlamaIndexAgent(**agent_configs[0]["args"])
code_explain_agent = LlamaIndexAgent(**agent_configs[1]["args"])
# prepare html for api agent
prepare_docstring_html(
agent_configs[2]["args"]["rag_config"]["repo_base"],
agent_configs[2]["args"]["rag_config"]["load_data"]["loader"][
"init_args"
]["input_dir"],
)
api_agent = LlamaIndexAgent(**agent_configs[2]["args"])
# define a guide agent
agent_configs[2]["args"].pop("description")
guide_agent = DialogAgent(**agent_configs[2]["args"])
agent_configs[3]["args"].pop("description")
guide_agent = DialogAgent(**agent_configs[3]["args"])
rag_agents = [
tutorial_agent,
code_explain_agent,
api_agent,
]
rag_agent_names = [agent.name for agent in rag_agents]

user_agent = UserAgent()
while True:
"""
The workflow is the following:
1. user input a message,
2. if it mentions one of the agents, then the agent will be called
3. otherwise, the guide agent will be decide which agent to call
4. the called agent will response to the user
5. repeat
"""
# The workflow is the following:
# 1. user input a message,
# 2. if it mentions one of the agents, then the agent will be called
# 3. otherwise, the guide agent will be decide which agent to call
# 4. the called agent will response to the user
# 5. repeat
x = user_agent()
x.role = "user" # to enforce dashscope requirement on roles
if len(x["content"]) == 0 or str(x["content"]).startswith("exit"):
Expand All @@ -63,8 +82,10 @@ def main() -> None:
guide_response = guide_agent(x)
# Only one agent can be called in the current version,
# we may support multi-agent conversation later
speak_list = filter_agents(guide_response.get("content", ""),
rag_agents)
speak_list = filter_agents(
guide_response.get("content", ""),
rag_agents,
)
agent_name_list = [agent.name for agent in speak_list]
for agent_name, agent in zip(agent_name_list, speak_list):
if agent_name in rag_agent_names:
Expand All @@ -73,4 +94,3 @@ def main() -> None:

if __name__ == "__main__":
main()

0 comments on commit ec3d8c0

Please sign in to comment.