Composio key concepts
Quick answer
Composio is a Python toolkit that enables seamless integration of AI tools with the OpenAI API using the
tools parameter for function calling. It provides a ComposioToolSet to manage tools and handle tool calls from AI chat completions, enhancing AI agent capabilities with external actions.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0pip install composio-core composio-openai
Setup
Install the required packages and set your OpenAI API key in the environment. Composio works with the OpenAI SDK v1+ and requires composio-core and composio-openai packages.
pip install openai>=1.0 composio-core composio-openai output
Collecting openai Collecting composio-core Collecting composio-openai Successfully installed openai composio-core composio-openai
Step by step
Use ComposioToolSet to get tools and pass them to the OpenAI chat completion call via the tools parameter. Then handle tool calls from the response using handle_tool_calls.
import os
from openai import OpenAI
from composio_openai import ComposioToolSet, Action
# Initialize Composio toolset with your API key
toolset = ComposioToolSet(api_key=os.environ["OPENAI_API_KEY"])
# Get tools for specific actions
tools = toolset.get_tools(actions=[Action.GITHUB_STAR_A_REPOSITORY])
# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Create chat completion with tools parameter
response = client.chat.completions.create(
model="gpt-4o-mini",
tools=tools,
messages=[{"role": "user", "content": "Star the openai/openai-python repo"}]
)
print("AI response:", response.choices[0].message.content)
# Handle any tool calls from the response
toolset.handle_tool_calls(response) output
AI response: I have initiated the process to star the openai/openai-python repository. # (Tool call handled successfully)
Common variations
- Use different
Actionenums to access other tools. - Switch to async calls by using
asyncioandawaitwith the OpenAI client. - Use other OpenAI models like
gpt-4oorgpt-4o-minidepending on your use case.
import asyncio
async def async_example():
from openai import OpenAI
from composio_openai import ComposioToolSet, Action
toolset = ComposioToolSet(api_key=os.environ["OPENAI_API_KEY"])
tools = toolset.get_tools(actions=[Action.GITHUB_STAR_A_REPOSITORY])
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = await client.chat.completions.acreate(
model="gpt-4o",
tools=tools,
messages=[{"role": "user", "content": "Star the openai/openai-python repo"}]
)
print("Async AI response:", response.choices[0].message.content)
toolset.handle_tool_calls(response)
asyncio.run(async_example()) output
Async AI response: I have started the process to star the openai/openai-python repository. # (Tool call handled successfully)
Troubleshooting
- If you see no tool calls handled, verify that the
toolsparameter is correctly passed and that the model supports tool use. - Ensure your API key has permissions for Composio tools.
- Check for network issues if tool calls fail to execute.
Key Takeaways
- Use
ComposioToolSetto manage and retrieve AI tools for OpenAI chat completions. - Pass tools via the
toolsparameter inclient.chat.completions.createto enable function calling. - Handle tool calls from AI responses with
handle_tool_callsto execute external actions. - Composio integrates seamlessly with OpenAI SDK v1+ and supports async usage.
- Verify API key permissions and model compatibility when troubleshooting tool call issues.