Source code for langchain.agents.agent_toolkits.vectorstore.toolkit

"""与向量存储交互的工具包。"""
from typing import List

from langchain_core.language_models import BaseLanguageModel
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.tools import BaseTool, BaseToolkit
from langchain_core.vectorstores import VectorStore


[docs]class VectorStoreInfo(BaseModel): """关于VectorStore的信息。""" vectorstore: VectorStore = Field(exclude=True) name: str description: str class Config: """这个pydantic对象的配置。""" arbitrary_types_allowed = True
[docs]class VectorStoreToolkit(BaseToolkit): """与向量存储交互的工具包。""" vectorstore_info: VectorStoreInfo = Field(exclude=True) llm: BaseLanguageModel class Config: """这个pydantic对象的配置。""" arbitrary_types_allowed = True
[docs] def get_tools(self) -> List[BaseTool]: """获取工具包中的工具。""" try: from langchain_community.tools.vectorstore.tool import ( VectorStoreQATool, VectorStoreQAWithSourcesTool, ) except ImportError: raise ImportError( "You need to install langchain-community to use this toolkit." ) description = VectorStoreQATool.get_description( self.vectorstore_info.name, self.vectorstore_info.description ) qa_tool = VectorStoreQATool( name=self.vectorstore_info.name, description=description, vectorstore=self.vectorstore_info.vectorstore, llm=self.llm, ) description = VectorStoreQAWithSourcesTool.get_description( self.vectorstore_info.name, self.vectorstore_info.description ) qa_with_sources_tool = VectorStoreQAWithSourcesTool( name=f"{self.vectorstore_info.name}_with_sources", description=description, vectorstore=self.vectorstore_info.vectorstore, llm=self.llm, ) return [qa_tool, qa_with_sources_tool]
[docs]class VectorStoreRouterToolkit(BaseToolkit): """用于在向量存储之间进行路由的工具包。""" vectorstores: List[VectorStoreInfo] = Field(exclude=True) llm: BaseLanguageModel class Config: """这个pydantic对象的配置。""" arbitrary_types_allowed = True
[docs] def get_tools(self) -> List[BaseTool]: """获取工具包中的工具。""" tools: List[BaseTool] = [] try: from langchain_community.tools.vectorstore.tool import ( VectorStoreQATool, ) except ImportError: raise ImportError( "You need to install langchain-community to use this toolkit." ) for vectorstore_info in self.vectorstores: description = VectorStoreQATool.get_description( vectorstore_info.name, vectorstore_info.description ) qa_tool = VectorStoreQATool( name=vectorstore_info.name, description=description, vectorstore=vectorstore_info.vectorstore, llm=self.llm, ) tools.append(qa_tool) return tools