Skip to Content
PythonUse ToolsIntroduction

Using Tools

OpenAPITools provides a flexible way to work with tools across different AI providers. You can specify which tools you want to use and even target specific versions of those tools.

Specifying Tools

When working with any adapter (Anthropic, OpenAI, or LangChain), you can specify which tools you want to use in several ways:

Basic Usage - All Tools

By default, if you don’t specify any tool names, all available tools will be used:

# Get all available tools tools = adapter.get_anthropic_tools() # For Anthropic # OR tools = adapter.get_openai_tools() # For OpenAI # OR tools = adapter.get_langchain_tools() # For LangChain

Specifying Tool Names

You can specify which tools you want to use by passing a list of tool names:

# Get specific tools by name tool_names = ["otp_generator", "weather_lookup"] tools = adapter.get_anthropic_tools(tool_names)

Specifying Tool Versions

For more control, you can specify both the tool name and version:

# Get specific tools with specific versions tool_list = [ "simple_calculator", # Use latest version {"name": "otp_generator", "version": "v2"}, # Use specific version {"name": "weather_lookup"} # No version specified uses latest ] tools = adapter.get_openai_tools(tool_list)

Creating Tool Handlers

When creating tool handlers, you can also specify which tools should be handled:

# Create a handler for specific tools tool_handler = adapter.create_anthropic_tool_handler(tool_names) # OR for OpenAI tool_handler = adapter.create_openai_tool_handler(tool_list)

Executing Tools

Anthropic

response = anthropic.messages.create(**api_options) # loop through the response content for content in response.content: if content.type == "tool_use": tool_result = tool_handler({ "id": content.id, "name": content.name, "input": content.input }) if tool_result.error: print(f"\nTool Error: {tool_result.error}") messages.append({ "role": "user", "content": [{ "type": "tool_result", "tool_use_id": content.id, "content": json.dumps({"error": tool_result.error}) }] }) else: print(f"\nTool Result: {tool_result.output}") messages.append({ "role": "user", "content": [{ "type": "tool_result", "tool_use_id": content.id, "content": json.dumps({"output": tool_result.output}) }] })

OpenAI

response = openai.chat.completions.create(**api_options) message = response.choices[0].message for tool_call in message.tool_calls: print( f"\nAssistant is using tool: {tool_call.function.name}") try: args = json.loads(tool_call.function.arguments) print(f"Tool input: {json.dumps(args, indent=2)}") except: print(f"Tool input: {tool_call.function.arguments}") # Execute the tool try: tool_result = tool_handler({ "id": tool_call.id, "function": { "name": tool_call.function.name, "arguments": tool_call.function.arguments } }) # Add tool response to messages if tool_result.error: print(f"\nTool Error: {tool_result.error}") messages.append({ "tool_call_id": tool_call.id, "role": "tool", "name": tool_call.function.name, "content": json.dumps({"error": tool_result.error}) }) else: print(f"\nTool Result: {tool_result.output}") messages.append({ "tool_call_id": tool_call.id, "role": "tool", "name": tool_call.function.name, "content": json.dumps({"output": tool_result.output}) })

LangChain

from reacter_openapitools import LangChainAdapter from langchain_anthropic import ChatAnthropic from langgraph.prebuilt import create_react_agent from langchain_core.messages import HumanMessage # Initialize the language model model = ChatAnthropic( model_name="claude-3-sonnet-20240229", api_key="your-anthropic-api-key" ) # Initialize the adapter adapter = LangChainAdapter(api_key="your OpenAPI Tools Apikey - https://openapitools.com/dashboard/settings") # Get tools in LangChain format tools = adapter.get_langchain_tools() # Create a LangChain agent with tools agent_executor = create_react_agent(model, tools) # Use the agent response = agent_executor.invoke({ "messages": [HumanMessage(content="Can you generate an OTP for me?")] }) print(response["messages"])
Last updated on