From 38b6831b579faea15fa66f2d4d2125c6697bc9cf Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 9 Aug 2023 15:29:11 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(Metaphor.py):=20add=20Metaphor?= =?UTF-8?q?Toolkit=20component=20to=20langflow=20toolkit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The MetaphorToolkit component is added to the langflow toolkit. It provides functionality for searching metaphors using the Metaphor API. The component includes three tools: search, get_contents, and find_similar. The search tool allows users to search for metaphors using a query. The get_contents tool retrieves the contents of a webpage based on the ids returned from the search tool. The find_similar tool finds search results similar to a given URL returned from the search tool. The MetaphorToolkit component is still in beta and requires a Metaphor API key to function. The API key is stored securely and can be configured in the field_config of the component. For more information, refer to the documentation: [Metaphor Toolkit Documentation](https://python.langchain.com/docs/integrations/tools/metaphor_search) --- .../langflow/components/toolkits/Metaphor.py | 51 +++++++++++++++++++ .../langflow/components/toolkits/__init__.py | 0 2 files changed, 51 insertions(+) create mode 100644 src/backend/langflow/components/toolkits/Metaphor.py create mode 100644 src/backend/langflow/components/toolkits/__init__.py diff --git a/src/backend/langflow/components/toolkits/Metaphor.py b/src/backend/langflow/components/toolkits/Metaphor.py new file mode 100644 index 000000000..aff949312 --- /dev/null +++ b/src/backend/langflow/components/toolkits/Metaphor.py @@ -0,0 +1,51 @@ +from typing import List, Union +from langflow import CustomComponent + +from metaphor_python import Metaphor +from langchain.tools import Tool +from langchain.agents import tool +from langchain.agents.agent_toolkits.base import BaseToolkit + + +class MetaphorToolkit(CustomComponent): + display_name: str = "Metaphor" + description: str = "Metaphor Toolkit" + documentation = ( + "https://python.langchain.com/docs/integrations/tools/metaphor_search" + ) + beta = True + # api key should be password = True + field_config = { + "metaphor_api_key": {"display_name": "Metaphor API Key", "password": True}, + "code": {"advanced": True}, + } + + def build( + self, + metaphor_api_key: str, + ) -> Union[Tool, BaseToolkit]: + # If documents, then we need to create a Vectara instance using .from_documents + client = Metaphor(api_key=metaphor_api_key) + + @tool + def search(query: str): + """Call search engine with a query.""" + return client.search(query, use_autoprompt=True, num_results=5) + + @tool + def get_contents(ids: List[str]): + """Get contents of a webpage. + + The ids passed in should be a list of ids as fetched from `search`. + """ + return client.get_contents(ids) + + @tool + def find_similar(url: str): + """Get search results similar to a given URL. + + The url passed in should be a URL returned from `search` + """ + return client.find_similar(url, num_results=5) + + return [search, get_contents, find_similar] diff --git a/src/backend/langflow/components/toolkits/__init__.py b/src/backend/langflow/components/toolkits/__init__.py new file mode 100644 index 000000000..e69de29bb