Files
langchain/libs/cli/langchain_cli/integration_template/docs/tools.ipynb
T
ccurme 1bf6576709 cli[patch]: fix anchor links in templates (#29178)
These are outdated and can break docs builds.
2025-01-13 18:28:18 +00:00

7.8 KiB

Cell:
[Cell type raw - unsupported, skipped]

ModuleName

  • TODO: Make sure API reference link is correct.

This notebook provides a quick overview for getting started with ModuleName tool. For detailed documentation of all ModuleName features and configurations head to the API reference.

  • TODO: Add any other relevant links, like information about underlying API, etc.

Overview

Integration details

  • TODO: Make sure links and features are correct
Class Package Serializable JS support Package latest
ModuleName langchain-community beta/❌ ✅/❌ PyPI - Version

Tool features

  • TODO: Add feature table if it makes sense

Setup

  • TODO: Add any additional deps

The integration lives in the langchain-community package.

In [ ]:
%pip install --quiet -U langchain-community

Credentials

  • TODO: Add any credentials that are needed
In [2]:
import getpass
import os

# if not os.environ.get("__MODULE_NAME___API_KEY"):
#     os.environ["__MODULE_NAME___API_KEY"] = getpass.getpass("__MODULE_NAME__ API key:\n")

It's also helpful (but not needed) to set up LangSmith for best-in-class observability:

In [3]:
# os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass()

Instantiation

  • TODO: Fill in instantiation params

Here we show how to instantiate an instance of the ModuleName tool, with

In [4]:
from langchain_community.tools import __ModuleName__


tool = __ModuleName__(
    ...
)

Invocation

Invoke directly with args

  • TODO: Describe what the tool args are, fill them in, run cell
In [ ]:
tool.invoke({...})

Invoke with ToolCall

We can also invoke the tool with a model-generated ToolCall, in which case a ToolMessage will be returned:

  • TODO: Fill in tool args and run cell
In [ ]:
# This is usually generated by a model, but we'll create a tool call directly for demo purposes.
model_generated_tool_call = {
    "args": {...},  # TODO: FILL IN
    "id": "1",
    "name": tool.name,
    "type": "tool_call",
}
tool.invoke(model_generated_tool_call)

Chaining

  • TODO: Add user question and run cells

We can use our tool in a chain by first binding it to a tool-calling model and then calling it:

import ChatModelTabs from "@theme/ChatModelTabs";

In [16]:
# | output: false
# | echo: false

# !pip install -qU langchain langchain-openai
from langchain.chat_models import init_chat_model

llm = init_chat_model(model="gpt-4o", model_provider="openai")
In [ ]:
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableConfig, chain

prompt = ChatPromptTemplate(
    [
        ("system", "You are a helpful assistant."),
        ("human", "{user_input}"),
        ("placeholder", "{messages}"),
    ]
)

# specifying tool_choice will force the model to call this tool.
llm_with_tools = llm.bind_tools([tool], tool_choice=tool.name)

llm_chain = prompt | llm_with_tools


@chain
def tool_chain(user_input: str, config: RunnableConfig):
    input_ = {"user_input": user_input}
    ai_msg = llm_chain.invoke(input_, config=config)
    tool_msgs = tool.batch(ai_msg.tool_calls, config=config)
    return llm_chain.invoke({**input_, "messages": [ai_msg, *tool_msgs]}, config=config)


tool_chain.invoke("...")

API reference

For detailed documentation of all ModuleName features and configurations head to the API reference: https://python.langchain.com/v0.2/api_reference/community/tools/langchain_community.tools.module_name.tool.ModuleName.html