Skip to content

Commit

Permalink
Merge pull request modelscope#18 from ZiTao-Li/zitao/dev_copilot
Browse files Browse the repository at this point in the history
update as comments
  • Loading branch information
ZiTao-Li authored May 22, 2024
2 parents e704fd7 + 63648fc commit f9a1308
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/sphinx_doc/zh_CN/source/tutorial/209-rag.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
我们在此介绍AgentScope与RAG相关的三个概念:知识(Knowledge),知识库(Knowledge Bank)和RAG 智能体。

### Knowledge
知识模块(目前仅有“LlamaIndexKnowledge”;即将支持对LangChain)负责处理所有与RAG相关的操作。
知识模块(目前仅有“LlamaIndexKnowledge”;即将提供对LangChain的支持)负责处理所有与RAG相关的操作。

在这里,我们将使用`LlamaIndexKnowledge`作为示例,以说明在`Knowledge`模块内的操作。
当初始化`LlamaIndexKnowledge`对象时,`LlamaIndexKnowledge.__init__`将执行以下步骤:
Expand Down
31 changes: 27 additions & 4 deletions src/agentscope/agents/rag_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from agentscope.message import Msg
from agentscope.rag import Knowledge


CHECKING_PROMPT = """
Is the retrieved content relevant to the query?
Retrieved content: {}
Expand All @@ -22,6 +21,29 @@
"""


class RAGConfig(dict):
"""a class to regulate the RAG configuration."""

def __init__(self, knowledge_id: list[str], **kwargs: Any):
"""
RAG configuration must have knowledge_id as a list of strings.
Args:
knowledge_id (list[str]): the list of knowledge ids
"""
super().__init__()
self.knowledge_id = knowledge_id
self.update(kwargs)

def __getattr__(self, key: Any) -> Any:
try:
return self[key]
except KeyError as e:
raise AttributeError(f"no attribute '{key}'") from e

def __setattr__(self, key: Any, value: Any) -> None:
self[key] = value


class LlamaIndexAgent(AgentBase):
"""
A LlamaIndex agent build on LlamaIndex.
Expand All @@ -34,7 +56,7 @@ def __init__(
model_config_name: str,
knowledge_list: list[Knowledge] = None,
memory_config: Optional[dict] = None,
rag_config: Optional[dict] = None,
rag_config: Optional[RAGConfig] = None,
**kwargs: Any,
) -> None:
"""
Expand Down Expand Up @@ -70,7 +92,7 @@ def __init__(
self.knowledge_list = knowledge_list or []
self.retriever_list = []
self.description = kwargs.get("description", "")
self.rag_config = rag_config or {}
self.rag_config = rag_config or RAGConfig([])

def reply(self, x: dict = None) -> dict:
"""
Expand Down Expand Up @@ -143,7 +165,8 @@ def reply(self, x: dict = None) -> dict:
query,
),
)
checking = self.model([msg])
msg = self.model.format(msg)
checking = self.model(msg)
logger.info(checking)
checking = checking.text.lower()
if "no" in checking:
Expand Down
6 changes: 6 additions & 0 deletions src/agentscope/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ class ShrinkPolicy(IntEnum):

TRUNCATE = 0
SUMMARIZE = 1


# rag related
DEFAULT_CHUNK_SIZE = 1024
DEFAULT_CHUNK_OVERLAP = 20
DEFAULT_TOP_K = 5
4 changes: 0 additions & 4 deletions src/agentscope/rag/knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
from loguru import logger
from agentscope.models import ModelWrapperBase

DEFAULT_CHUNK_SIZE = 1024
DEFAULT_CHUNK_OVERLAP = 20
DEFAULT_TOP_K = 5


class Knowledge(ABC):
"""
Expand Down
1 change: 1 addition & 0 deletions src/agentscope/rag/knowledge_bank.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def equip(self, agent: AgentBase, duplicate: bool = False) -> None:
agent (AgentBase): the agent to be equipped with knowledge
if it has "rag_config"
duplicate (bool): whether to deepcopy the knowledge object
TODO: to accommodate with distributed setting
"""
if hasattr(agent, "rag_config") and "knowledge_id" in agent.rag_config:
if not hasattr(agent, "knowledge_list"):
Expand Down
30 changes: 18 additions & 12 deletions src/agentscope/rag/llama_index_knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,29 @@
Document,
TransformComponent,
)
except ImportError:
BaseRetriever = None
BaseEmbedding, Embedding = None, None
IngestionPipeline = None
SentenceSplitter = None
VectorStoreIndex, StorageContext = None, None
load_index_from_storage = None
PrivateAttr = None
Document, TransformComponent = None, None
except ImportError as import_error:
from agentscope.utils.tools import ImportErrorReporter

BaseRetriever = ImportErrorReporter(import_error, "llama-index")
BaseEmbedding = ImportErrorReporter(import_error, "llama-index")
Embedding = ImportErrorReporter(import_error, "llama-index")
IngestionPipeline = ImportErrorReporter(import_error, "llama-index")
SentenceSplitter = ImportErrorReporter(import_error, "llama-index")
VectorStoreIndex = ImportErrorReporter(import_error, "llama-index")
StorageContext = ImportErrorReporter(import_error, "llama-index")
load_index_from_storage = ImportErrorReporter(import_error, "llama-index")
PrivateAttr = ImportErrorReporter(import_error, "llama-index")
Document = ImportErrorReporter(import_error, "llama-index")
TransformComponent = ImportErrorReporter(import_error, "llama-index")

from agentscope.file_manager import file_manager
from agentscope.models import ModelWrapperBase
from .knowledge import (
from agentscope.constants import (
DEFAULT_TOP_K,
DEFAULT_CHUNK_SIZE,
DEFAULT_CHUNK_OVERLAP,
Knowledge,
)
from agentscope.rag.knowledge import Knowledge


try:
Expand Down Expand Up @@ -426,7 +431,8 @@ def set_retriever(
Set the retriever as needed, or just use the default setting.
Args:
retriever (Optional[BaseRetriever]): passing a retriever in llama
retriever (Optional[BaseRetriever]): passing a retriever in
LlamaIndexKnowledge
rag_config (dict): rag configuration, including similarity top k
index.
"""
Expand Down

0 comments on commit f9a1308

Please sign in to comment.