Skip to content

Commit

Permalink
Add Metaphor Search Tool
Browse files Browse the repository at this point in the history
  • Loading branch information
homanp committed Aug 10, 2023
1 parent 038c689 commit aa74763
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 19 deletions.
4 changes: 4 additions & 0 deletions app/lib/agents/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
ZapierToolInput,
AgentToolInput,
OpenApiToolInput,
MetaphorToolInput,
)
from app.lib.prisma import prisma
from app.lib.prompts import (
Expand All @@ -43,6 +44,7 @@
get_openapi_tool,
get_chatgpt_plugin_tool,
AgentTool,
MetaphorTool,
DocSummarizerTool,
)
from app.lib.vectorstores.base import VectorStoreBase
Expand Down Expand Up @@ -307,6 +309,8 @@ def _get_tool_and_input_by_type(
if type == "CHATGPT_PLUGIN":
# TODO: confirm metadata has (can have) url
return (get_chatgpt_plugin_tool(metadata), type)
if type == "METAPHOR":
return (MetaphorTool(metadata=metadata), MetaphorToolInput)

def _get_tools(self) -> list:
tools = []
Expand Down
4 changes: 4 additions & 0 deletions app/lib/models/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ class AgentToolInput(BaseModel):
query: str = Field()


class MetaphorToolInput(BaseModel):
query: str = Field()


class OpenApiToolInput(BaseModel):
input: str = Field()
18 changes: 18 additions & 0 deletions app/lib/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from langchain.agents.agent_toolkits import ZapierToolkit
from langchain.agents import AgentType, initialize_agent
from langchain.utilities.zapier import ZapierNLAWrapper
from langchain.utilities import MetaphorSearchAPIWrapper
from langchain.chains.openai_functions.openapi import get_openapi_chain
from langchain.tools import AIPluginTool
from langchain.agents import load_tools
Expand All @@ -27,6 +28,7 @@ class ToolDescription(Enum):
AGENT = "useful for when you need help completing something."
OPENAPI = "useful for when you need to do API requests to a third-party service."
CHATGPT_PLUGIN = "useful for when you need to interact with a third-party service"
METAPHOR = "useful for when you need to search search for answers on the internet."


def get_search_tool() -> Any:
Expand Down Expand Up @@ -105,6 +107,22 @@ def run(self, *args) -> str:
return output["data"]


class MetaphorTool:
def __init__(
self,
metadata: dict,
) -> Any:
self.metadata = metadata

def run(self, *args) -> str:
search = MetaphorSearchAPIWrapper(
metaphor_api_key=self.metadata["metaphor_api_key"]
)
output = search.results(args[0], 10, use_autoprompt=True)

return output


class DocSummarizerTool:
def __init__(self, docsearch: Any, llm: Any):
self.docsearch = docsearch
Expand Down
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterEnum
ALTER TYPE "ToolType" ADD VALUE 'METAPHOR';
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum ToolType {
AGENT
OPENAPI
CHATGPT_PLUGIN
METAPHOR
}

model User {
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ vulture = "^2.7"
airbyte-source-stripe = "^3.17.1"
pyairtable = "^2.0.0"
langchain = "^0.0.260"
metaphor-python = "^0.1.11"


[build-system]
Expand Down
51 changes: 36 additions & 15 deletions ui/app/tools/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ export default function ToolsModal({ onSubmit, onClose, isOpen, tool }) {
<ModalBody>
<Stack spacing={4}>
<Stack>
<FormControl isRequired isInvalid={errors?.type}>
<FormLabel>Type</FormLabel>
<Select {...register("type", { required: true })}>
<option value="SEARCH">Websearch</option>
<option value="AGENT">Superagent</option>
<option value="WOLFRAM_ALPHA">Wolfram Alpha</option>
<option value="REPLICATE">Replicate</option>
<option value="ZAPIER_NLA">Zapier</option>
<option value="OPENAPI">APIs</option>
<option value="CHATGPT_PLUGIN">ChatGPT Plugin</option>
<option value="METAPHOR">Metaphor Search</option>
</Select>
{errors?.type && (
<FormErrorMessage>Invalid type</FormErrorMessage>
)}
</FormControl>
<FormControl isRequired isInvalid={errors?.name}>
<FormLabel>Name</FormLabel>
<Input type="text" {...register("name", { required: true })} />
Expand All @@ -122,21 +138,6 @@ export default function ToolsModal({ onSubmit, onClose, isOpen, tool }) {
<FormErrorMessage>Invalid description</FormErrorMessage>
)}
</FormControl>
<FormControl isRequired isInvalid={errors?.type}>
<FormLabel>Type</FormLabel>
<Select {...register("type", { required: true })}>
<option value="SEARCH">Websearch</option>
<option value="AGENT">Superagent</option>
<option value="WOLFRAM_ALPHA">Wolfram Alpha</option>
<option value="REPLICATE">Replicate</option>
<option value="ZAPIER_NLA">Zapier</option>
<option value="OPENAPI">APIs</option>
<option value="CHATGPT_PLUGIN">ChatGPT Plugin</option>
</Select>
{errors?.type && (
<FormErrorMessage>Invalid type</FormErrorMessage>
)}
</FormControl>
{type === "OPENAPI" && (
<>
<FormControl isRequired>
Expand Down Expand Up @@ -202,6 +203,26 @@ export default function ToolsModal({ onSubmit, onClose, isOpen, tool }) {
<FormHelperText>Select an Agent</FormHelperText>
</FormControl>
)}
{type === "METAPHOR" && (
<FormControl isRequired>
<FormLabel>Metaphor api key</FormLabel>
<Input
type="password"
{...register("metaphor_api_key", { required: true })}
placeholder="Enter Metaphor api key..."
/>
<FormHelperText>
Obtain your Metaphor API key{" "}
<Link
color="orange.500"
href="https://platform.metaphor.systems/"
target="_blank"
>
here.
</Link>
</FormHelperText>
</FormControl>
)}
{type === "ZAPIER_NLA" && (
<FormControl isRequired>
<FormLabel>Zapier NLA api key</FormLabel>
Expand Down
5 changes: 2 additions & 3 deletions ui/lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
export const SUPERAGENT_VERSION = "v0.0.46";
export const SUPERAGENT_VERSION = "v0.0.48";

export const TOOL_ICONS = {
SEARCH: "/bing-icon.png",
WOLFRAM_ALPHA: "/wolfram-icon.png",
REPLICATE: "/replicate-icon.png",
ZAPIER_NLA: "/zapier-icon.png",
CHATGPT_PLUGIN: "/chatgpt-icon.png",
AGENT: "/",
AGENT: "/logo.png",
};

export const UPCOMING_TOOLS = [
{
id: "webresearcher",
name: "Web researcher",
type: "/logo.png",
},
];

0 comments on commit aa74763

Please sign in to comment.