agentscope.rag package
Submodules
Module contents
Import RAG related modules in the package.
- class Knowledge(knowledge_id: str, emb_model: Any | None = None, knowledge_config: dict | None = None, model: ModelWrapperBase | None = None, **kwargs: Any)[source]
Bases:
ABC
Base class for RAG, CANNOT be instantiated directly
- classmethod build_knowledge_instance(knowledge_id: str, knowledge_config: dict | None = None, **kwargs: Any) Knowledge [source]
A constructor to build a knowledge base instance.
- Parameters:
knowledge_id (str) – The id of the knowledge instance.
knowledge_config (dict) – The configuration to the knowledge instance.
- Returns:
a Knowledge instance
- Return type:
- classmethod default_config(**kwargs: Any) dict [source]
Return a default config for a knowledge class.
- Parameters:
kwargs (Any) – Parameters for config
- Returns:
a default config of the knowledge class
- Return type:
dict
- post_processing(retrieved_docs: list[str], prompt: str, **kwargs: Any) Any [source]
A default solution for post-processing function, generates answer based on the retrieved documents.
- Parameters:
retrieved_docs (list[str]) – List of retrieved documents
prompt (str) – Prompt for LLM generating answer with the retrieved documents
- Returns:
A synthesized answer from LLM with retrieved documents
- Return type:
Any
Example
self.postprocessing_model(prompt.format(retrieved_docs))
- abstract retrieve(query: Any, similarity_top_k: int | None = None, to_list_strs: bool = False, **kwargs: Any) list[RetrievedChunk | str] [source]
Retrieve list of content from database (vector stored index) to memory
- Parameters:
query (Any) – Query for retrieval
similarity_top_k (int) – The number of most similar data returned by the retriever.
to_list_strs (bool) – Whether return a list of str
- Returns:
Return a list with retrieved documents (in strings)
- knowledge_type: str = 'base_knowledge'
A string to identify a knowledge base class
- class KnowledgeBank(configs: dict | str | list | None = None, new_knowledge_types: type[Knowledge] | list | None = None)[source]
Bases:
object
KnowledgeBank enables 1) provide an easy and fast way to initialize the Knowledge object; 2) make Knowledge object reusable and sharable for multiple agents.
- add_data_as_knowledge(knowledge_id: str, knowledge_type: str, knowledge_config: dict | None = None, **kwargs: Any) None [source]
Transform data in a directory to be ready to work with RAG.
- Parameters:
knowledge_id (str) – User-defined unique id for the knowledge
knowledge_type (str) – Type of the knowledge to register, e.g., “llamaindex_knowledge”
knowledge_config (dict) –
For LlamaIndexKnowledge (knowledge_type=”llamaindex_knowledge”) the following are required: emb_model_config_name (str):
Name of the embedding model config
- model_config_name (Optional[str]):
Name of the LLM config for potential post-processing or query rewrite
- data_dirs_and_types (dict[str, list[str]]):
Dictionary of data paths (keys) to the data types (file extensions) for knowledgebase (e.g., [“.md”, “.py”, “.html”])
- knowledge_config (optional[dict]):
Complete indexing configuration, used for more advanced applications. Users can customize - loader, - transformations, - … Examples can refer to ../examples/conversation_with_RAG_agents/
kwargs (Any) – Additional keyword arguments to initialize knowledge.
- equip(agent: AgentBase, knowledge_id_list: list[str] | None = None, duplicate: bool = False) None [source]
Equip the agent with the knowledge by knowledge ids.
- Parameters:
agent (AgentBase) – The agent to be equipped with knowledge
knowledge_id_list (list[str]) – The list of knowledge ids to be equipped with the agent
duplicate (bool) – Whether to deepcopy the knowledge object
TODO: to accommodate with distributed setting
- get_knowledge(knowledge_id: str, duplicate: bool = False) Knowledge [source]
Get a Knowledge object from the knowledge bank.
- Parameters:
knowledge_id (str) – Unique id for the Knowledge object
duplicate (bool) – Whether return a copy of the Knowledge object.
- Returns:
The Knowledge object defined with Llama-index
- Return type:
- register_knowledge_type(knowledge_base_class: Type[Knowledge], exist_ok: bool = True) None [source]
Add a new knowledge base class to the knowledge bank
- Parameters:
knowledge_base_class (Type[Knowledge]) – The knowledge class to be registered, which must inherit from Knowledge.
exist_ok (bool) – Whether to overwrite the existing knowledge base class with the same name.
- class RetrievedChunk(score: float = 0.0, content: Any | None = None, metadata: dict | None = None, embedding: Any | None = None, hash: str | None = None)[source]
Bases:
object
Retrieved content with score and meta information
- score
Similarity score of this retrieved chunk
- Type:
float
- content
The retrieved content
- Type:
Any
- metadata
The meta data of this retrieved chunk, such as file path
- Type:
Optional[dict]
- embedding
The embedding of the chunk
- Type:
Optional[Any]
- hash
The hash of the retrieved content
- Type:
Optional[str]
- content: Any = None
- embedding: Any | None = None
- hash: str | None = None
- metadata: dict | None = None
- score: float = 0.0