langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
12.09k stars 2.03k forks source link

custom tools for agent #1219

Closed shawnesquivel closed 1 year ago

shawnesquivel commented 1 year ago

I come from the langchain python docs, where it was quite simple to initialize an agent based on another function. What would be the equivalent of making a custom tool in langchainjs?

Thank you in advance! 🦜

python code i'm trying to replicate into langchainjs:

def extract_youtube_transcript(youtube_url: str) -> str:
    """
    Given the YouTube Url
    1. Get the raw transcript
    2. Summarize it for us into a ChromaDB
    """
    loader = YoutubeLoader.from_youtube_url(youtube_url, add_video_info=True, language="en-US")
    summary = loader.load()
    return summary

def summarize_youtube_transcript(url: str) -> str:
    # Initialize with tools, llm, template
    agent_tools = [
        Tool(
            name="Crawl Google",
            func=extract_youtube_transcript,
            description="useful for when you need to get a YouTube video url",
        ),
    ]
adam101 commented 1 year ago

I think you're looking for the DynamicTool. You can find the documentation here.

The DynamicTool class takes as input a name, a description, and a function. Importantly, the name and the description will be used by the language model to determine when to call this function and with what parameters!

  const tools = [
    new DynamicTool({
      name: "FOO",
      description:
        "call this to get the value of foo. input should be an empty string.",
      func: () => "baz",
    }),
    new DynamicTool({
      name: "BAR",
      description:
        "call this to get the value of bar. input should be an empty string.",
      func: () => "baz1",
    }),
  ];
shawnesquivel commented 1 year ago

thanks adam! that's exactly it.