Skip to content

tool

GriptapeCloudKnowledgeBaseTool

Bases: BaseGriptapeCloudTool

Tool for querying a Griptape Cloud Knowledge Base.

Attributes:

Name Type Description
description Optional[str]

LLM-friendly knowledge base description.

knowledge_base_id str

ID of the Griptape Cloud Knowledge Base.

Source code in griptape/tools/griptape_cloud_knowledge_base/tool.py
@define
class GriptapeCloudKnowledgeBaseTool(BaseGriptapeCloudTool):
    """Tool for querying a Griptape Cloud Knowledge Base.

    Attributes:
        description: LLM-friendly knowledge base description.
        knowledge_base_id: ID of the Griptape Cloud Knowledge Base.
    """

    description: Optional[str] = field(default=None, kw_only=True)
    knowledge_base_id: str = field(kw_only=True)

    @activity(
        config={
            "description": "Can be used to search a knowledge base with the following description: {{ _self._get_knowledge_base_description() }}",
            "schema": Schema(
                {
                    Literal(
                        "query",
                        description="A natural language search query to run against the knowledge base",
                    ): str,
                },
            ),
        },
    )
    def query(self, params: dict) -> TextArtifact | ErrorArtifact:
        from requests import exceptions, post

        query = params["values"]["query"]
        url = urljoin(self.base_url.strip("/"), f"/api/knowledge-bases/{self.knowledge_base_id}/search")

        try:
            response = post(url, json={"query": query}, headers=self.headers)

            return TextArtifact(response.text)
        except exceptions.RequestException as err:
            return ErrorArtifact(str(err))

    def _get_knowledge_base_description(self) -> str:
        from requests import get

        if self.description:
            return self.description
        else:
            url = urljoin(self.base_url.strip("/"), f"/api/knowledge-bases/{self.knowledge_base_id}/")

            response = get(url, headers=self.headers)
            response_body = response.json()
            if response.status_code == 200:
                if "description" in response_body:
                    return response_body["description"]
                else:
                    raise ValueError(
                        f"No description found for Knowledge Base {self.knowledge_base_id}. Please set a description, or manually set the `GriptapeCloudKnowledgeBaseTool.description` attribute.",
                    )
            else:
                raise ValueError(f"Error accessing Knowledge Base {self.knowledge_base_id}.")

description: Optional[str] = field(default=None, kw_only=True) class-attribute instance-attribute

knowledge_base_id: str = field(kw_only=True) class-attribute instance-attribute

query(params)

Source code in griptape/tools/griptape_cloud_knowledge_base/tool.py
@activity(
    config={
        "description": "Can be used to search a knowledge base with the following description: {{ _self._get_knowledge_base_description() }}",
        "schema": Schema(
            {
                Literal(
                    "query",
                    description="A natural language search query to run against the knowledge base",
                ): str,
            },
        ),
    },
)
def query(self, params: dict) -> TextArtifact | ErrorArtifact:
    from requests import exceptions, post

    query = params["values"]["query"]
    url = urljoin(self.base_url.strip("/"), f"/api/knowledge-bases/{self.knowledge_base_id}/search")

    try:
        response = post(url, json={"query": query}, headers=self.headers)

        return TextArtifact(response.text)
    except exceptions.RequestException as err:
        return ErrorArtifact(str(err))