mirror of
https://github.com/hwchase17/langchain
synced 2024-11-11 19:11:02 +00:00
975b6129f6
Open to other thoughts on UX. string input: ```python as_tool = retriever.as_tool() as_tool.invoke("cat") # [Document(...), ...] ``` typed dict input: ```python class Args(TypedDict): key: int def f(x: Args) -> str: return str(x["key"] * 2) as_tool = RunnableLambda(f).as_tool( name="my tool", description="description", # name, description are inferred if not supplied ) as_tool.invoke({"key": 3}) # "6" ``` for untyped dict input, allow specification of parameters + types ```python def g(x: Dict[str, Any]) -> str: return str(x["key"] * 2) as_tool = RunnableLambda(g).as_tool(arg_types={"key": int}) result = as_tool.invoke({"key": 3}) # "6" ``` Passing the `arg_types` is slightly awkward but necessary to ensure tool calls populate parameters correctly: ```python from typing import Any, Dict from langchain_core.runnables import RunnableLambda from langchain_openai import ChatOpenAI def f(x: Dict[str, Any]) -> str: return str(x["key"] * 2) runnable = RunnableLambda(f) as_tool = runnable.as_tool(arg_types={"key": int}) llm = ChatOpenAI().bind_tools([as_tool]) result = llm.invoke("Use the tool on 3.") tool_call = result.tool_calls[0] args = tool_call["args"] assert args == {"key": 3} as_tool.run(args) ``` Contrived (?) example with langgraph agent as a tool: ```python from typing import List, Literal from typing_extensions import TypedDict from langchain_openai import ChatOpenAI from langgraph.prebuilt import create_react_agent llm = ChatOpenAI(temperature=0) def magic_function(input: int) -> int: """Applies a magic function to an input.""" return input + 2 agent_1 = create_react_agent(llm, [magic_function]) class Message(TypedDict): role: Literal["human"] content: str agent_tool = agent_1.as_tool( arg_types={"messages": List[Message]}, name="Jeeves", description="Ask Jeeves.", ) agent_2 = create_react_agent(llm, [agent_tool]) ``` --------- Co-authored-by: Erick Friis <erick@langchain.dev> |
||
---|---|---|
.. | ||
ainetwork | ||
amadeus | ||
arxiv | ||
asknews | ||
audio | ||
azure_ai_services | ||
azure_cognitive_services | ||
bearly | ||
bing_search | ||
brave_search | ||
cassandra_database | ||
clickup | ||
cogniswitch | ||
connery | ||
databricks | ||
dataforseo_api_search | ||
dataherald | ||
ddg_search | ||
e2b_data_analysis | ||
edenai | ||
eleven_labs | ||
file_management | ||
github | ||
gitlab | ||
gmail | ||
golden_query | ||
google_cloud | ||
google_finance | ||
google_jobs | ||
google_lens | ||
google_places | ||
google_scholar | ||
google_search | ||
google_serper | ||
google_trends | ||
graphql | ||
human | ||
interaction | ||
jira | ||
json | ||
memorize | ||
merriam_webster | ||
metaphor_search | ||
mojeek_search | ||
multion | ||
nasa | ||
nuclia | ||
office365 | ||
openai_dalle_image_generation | ||
openapi | ||
openweathermap | ||
passio_nutrition_ai | ||
playwright | ||
polygon | ||
powerbi | ||
pubmed | ||
reddit_search | ||
requests | ||
scenexplain | ||
searchapi | ||
searx_search | ||
semanticscholar | ||
shell | ||
slack | ||
sleep | ||
spark_sql | ||
sql_database | ||
stackexchange | ||
steam | ||
steamship_image_generation | ||
tavily_search | ||
vectorstore | ||
wikidata | ||
wikipedia | ||
wolfram_alpha | ||
you | ||
youtube | ||
zapier | ||
zenguard | ||
__init__.py | ||
convert_to_openai.py | ||
ifttt.py | ||
plugin.py | ||
render.py | ||
yahoo_finance_news.py |