forked from kreneskyp/ix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert get_metaphor to Tool wrapping official metaphor client
- Loading branch information
Showing
3 changed files
with
33 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,29 @@ | ||
from typing import Any | ||
from langchain.utilities import MetaphorSearchAPIWrapper | ||
from langchain.tools import BaseTool, MetaphorSearchResults # metaphorQueryRun | ||
import os | ||
from typing import Any, Optional | ||
|
||
from ix.chains.asyncio import SyncToAsyncRun | ||
from ix.chains.loaders.tools import extract_tool_kwargs | ||
from asgiref.sync import sync_to_async | ||
from langchain.tools import Tool | ||
from metaphor_python import Metaphor | ||
|
||
from ix.chains.loaders.tools import extract_tool_kwargs | ||
|
||
class AsyncMetaphorQueryRun(SyncToAsyncRun, MetaphorSearchResults): | ||
pass | ||
|
||
def get_metaphor(metaphor_api_key: Optional[str], **kwargs: Any) -> Tool: | ||
"""Metaphor search tool | ||
def get_metaphor(**kwargs: Any) -> BaseTool: | ||
Initializes a Metaphor client and returns a Tool object that can be used to run queries. | ||
""" | ||
tool_kwargs = extract_tool_kwargs(kwargs) | ||
wrapper = MetaphorSearchAPIWrapper(**kwargs) | ||
# metaphor_tool = MetaphorSearchResults(api_wrapper=search) | ||
return AsyncMetaphorQueryRun(api_wrapper=wrapper, **tool_kwargs) | ||
api_key = metaphor_api_key or os.environ.get("METAPHOR_API_KEY", None) | ||
assert ( | ||
api_key is not None | ||
), "Metaphor API key must be provided as metaphor_api_key or METAPHOR_API_KEY env var" | ||
|
||
client = Metaphor(api_key=api_key) | ||
return Tool( | ||
name="metaphor_query", | ||
description="Metaphor Search. Searches the web for pages that match a given query.", | ||
func=client.search, | ||
coroutine=sync_to_async(client.search), | ||
**tool_kwargs | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters