Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing RAG module into package #64

Merged
merged 27 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
317d653
initial design for RAG module
ZiTao-Li Mar 13, 2024
3330cf5
unify types
ZiTao-Li Mar 13, 2024
54bee70
Merge branch 'main' into zitao/rag_dev
ZiTao-Li Mar 14, 2024
3df40f2
added preliminary examples
ZiTao-Li Mar 14, 2024
8fb2559
Merge branch 'main' into zitao/rag_dev
ZiTao-Li Mar 14, 2024
d9d42d5
update to allow embedding model wrapper
ZiTao-Li Mar 18, 2024
518321f
fix typos
ZiTao-Li Mar 18, 2024
b7b60f4
update example
ZiTao-Li Mar 19, 2024
a88da63
improve langchain rag
ZiTao-Li Mar 19, 2024
3bf355f
Merge branch 'main' into zitao/rag_dev
ZiTao-Li Mar 19, 2024
7c3a2a4
update example
ZiTao-Li Mar 19, 2024
cab787b
Merge branch 'main' into zitao/rag_dev
ZiTao-Li Mar 19, 2024
d572fd0
update parameter for retrieval
ZiTao-Li Mar 19, 2024
3c894ef
update langchain parameter
ZiTao-Li Mar 19, 2024
be8d2d1
fix
ZiTao-Li Mar 19, 2024
e2eb025
update doc strings
ZiTao-Li Mar 20, 2024
f038021
update doc strings 2
ZiTao-Li Mar 20, 2024
c07b9a7
update following comments
ZiTao-Li Mar 21, 2024
4acc3fd
Merge branch 'main' into zitao/rag_dev
ZiTao-Li Mar 27, 2024
82c7987
update to be more flexible
ZiTao-Li Mar 28, 2024
9221cac
Merge branch 'main' into zitao/rag_dev
ZiTao-Li Mar 28, 2024
c41eeaf
update to avoid test import error
ZiTao-Li Mar 28, 2024
ca3eaa1
update again
ZiTao-Li Mar 28, 2024
dce475a
update docs
ZiTao-Li Mar 29, 2024
e669c39
update docs and move rag agents to example
ZiTao-Li Mar 29, 2024
0f95847
move rag module to example
ZiTao-Li Mar 29, 2024
d8417d5
remove one empty line
ZiTao-Li Mar 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ the following libraries.
- [Self-Organizing Conversation](./examples/conversation_self_organizing)
- [Basic Conversation with LangChain library](./examples/conversation_with_langchain)
- [Conversation with ReAct Agent](./examples/conversation_with_react_agent)
- [Conversation with RAG Agent](./examples/conversation_with_RAG_agents)

- Game
- [Gomoku](./examples/game_gomoku)
Expand Down
1 change: 1 addition & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ AgentScope支持使用以下库快速部署本地模型服务。
- [智能体自组织的对话](./examples/conversation_self_organizing)
- [兼容LangChain的基础对话](./examples/conversation_with_langchain)
- [与ReAct智能体对话](./examples/conversation_with_react_agent)
- [与RAG智能体对话](./examples/conversation_with_RAG_agents)

- 游戏
- [五子棋](./examples/game_gomoku)
Expand Down
9 changes: 9 additions & 0 deletions docs/sphinx_doc/en/source/agentscope.agents.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,12 @@ react_agent module
:members:
:undoc-members:
:show-inheritance:


rag_agent module
-------------------------------

.. automodule:: agentscope.agents.rag_agents
:members:
:undoc-members:
:show-inheritance:
9 changes: 9 additions & 0 deletions docs/sphinx_doc/zh_CN/source/agentscope.agents.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,12 @@ react_agent module
:members:
:undoc-members:
:show-inheritance:


rag_agent module
-------------------------------

.. automodule:: agentscope.agents.rag_agents
:members:
:undoc-members:
:show-inheritance:
40 changes: 40 additions & 0 deletions examples/conversation_with_RAG_agents/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# AgentScope Consultants: a Multi-Agent RAG Application

* **What is this example about?**
With the provided implementation and configuration,
you will obtain three different agents who can help you answer different questions about AgentScope.

* **What is this example for?** By this example, we want to show how the agent with retrieval augmented generation (RAG)
capability can be used to build easily.

## Prerequisites
* **Cloning repo:** This example requires cloning the whole AgentScope repo to local.
* **Packages:** This example is built on the LlamaIndex package. Thus, some packages need to be installed before running the example.
```bash
pip install llama-index tree_sitter tree-sitter-languages
```
* **Model APIs:** This example uses Dashscope APIs. Thus, we also need an API key for DashScope.
```bash
export DASH_SCOPE_API='YOUR_API_KEY'
```
However, you are welcome to replace the Dashscope language and embedding models with other models you like.

DavdGao marked this conversation as resolved.
Show resolved Hide resolved
## Start AgentScope Consultants
* **Terminal:** The most simple way to execute the AgentScope Consultants is running in terminal.
```bash
python ./rag_example.py
```
Setting `log_retrieval` to `false` in `agent_config.json` can hide the retrieved information and provide only answers of agents.

* **AS studio:** If you want to have more organized, clean UI, you can also run with our `as_studio`.
```bash
as_studio ./rag_example.py
```

### Customize AgentScope Consultants to other consultants
After you run the example, you may notice that this example consists of three RAG agents:
* `AgentScope Tutorial Assistant`: responsible for answering questions based on AgentScope tutorials (markdown files).
* `AgentScope Framework Code Assistant`: responsible for answering questions based on AgentScope code base (python files).
* `Summarize Assistant`: responsible for summarize the questions from the above two agents.

These agents can be configured to answering questions based on other GitHub repo, by simply modifying the `input_dir` fields in the `agent_config.json`.
79 changes: 79 additions & 0 deletions examples/conversation_with_RAG_agents/agent_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
[
{
"class": "LlamaIndexAgent",
"args": {
"name": "AgentScope Tutorial Assistant",
"sys_prompt": "You're a helpful assistant. You need to generate answers based on the provided context.",
"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/sphinx_doc/en/source/tutorial/",
"required_exts": [".md"]
}
}
},
"chunk_size": 2048,
"chunk_overlap": 40,
"similarity_top_k": 10,
"log_retrieval": false,
"recent_n_mem": 1
}
}
},
{
"class": "LlamaIndexAgent",
"args": {
"name": "AgentScope Framework Code Assistant",
"sys_prompt": "You're a helpful assistant about coding. You can very familiar with the framework code of AgentScope.",
"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": "../../src/agentscope",
"recursive": true,
"required_exts": [".py"]
}
}
},
"store_and_index": {
"transformations": [
{
"create_object": true,
"module": "llama_index.core.node_parser",
"class": "CodeSplitter",
"init_args": {
"language": "python",
"chunk_lines": 100
}
}
]
},
"chunk_size": 2048,
"chunk_overlap": 40,
"similarity_top_k": 10,
"log_retrieval": false,
"recent_n_mem": 1
}
}
},
{
"class": "DialogAgent",
"args": {
"name": "Summarize Assistant",
"sys_prompt": "You are a helpful assistant that can summarize the answers of the previous two messages.",
"model_config_name": "qwen_config",
"use_memory": true
}
}
]
57 changes: 57 additions & 0 deletions examples/conversation_with_RAG_agents/rag_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
"""
A simple example for conversation between user and
an agent with RAG capability.
"""
import os

import agentscope
from agentscope.agents import UserAgent
from agentscope.message import Msg


def main() -> None:
"""A RAG multi-agent demo"""
agents = agentscope.init(
model_configs=[
{
"model_type": "dashscope_chat",
"config_name": "qwen_config",
"model_name": "qwen-max",
"api_key": f"{os.environ.get('DASHSCOPE_API_KEY')}",
},
{
"model_type": "dashscope_text_embedding",
"config_name": "qwen_emb_config",
"model_name": "text-embedding-v2",
"api_key": f"{os.environ.get('DASHSCOPE_API_KEY')}",
},
],
agent_configs="./agent_config.json",
)

tutorial_agent, code_explain_agent, summarize_agent = agents

user_agent = UserAgent()
# start the conversation between user and assistant
while True:
x = user_agent()
x.role = "user" # to enforce dashscope requirement on roles
if len(x["content"]) == 0 or str(x["content"]).startswith("exit"):
break
tutorial_response = tutorial_agent(x)
code_explain = code_explain_agent(x)
msg = Msg(
name="user",
role="user",
content=tutorial_response["content"]
+ "\n"
+ code_explain["content"]
+ "\n"
+ x["content"],
)
summarize_agent(msg)


if __name__ == "__main__":
main()
7 changes: 7 additions & 0 deletions src/agentscope/agents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
from .rpc_agent import RpcAgentServerLauncher
from .react_agent import ReActAgent

try:
DavdGao marked this conversation as resolved.
Show resolved Hide resolved
from .rag_agents import RAGAgentBase, LlamaIndexAgent
except Exception:
RAGAgentBase, LlamaIndexAgent = None, None # type: ignore # NOQA


__all__ = [
"AgentBase",
Expand All @@ -19,4 +24,6 @@
"UserAgent",
"RpcAgentServerLauncher",
"ReActAgent",
"RAGAgentBase",
"LlamaIndexAgent",
]
Loading
Loading