Composio supported integrations
Quick answer
The
Composio framework supports integrations primarily with OpenAI API clients to enable AI tool usage within chat completions. It provides a ComposioToolSet to fetch and manage tools like GitHub actions, which can be passed to OpenAI chat calls via the tools parameter for function calling and automation.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0 composio-core composio-openai
Setup
Install the required packages and set your environment variables for API keys.
- Install
openaiandcomposio-corewithpip install openai composio-core composio-openai - Set your OpenAI API key in the environment variable
OPENAI_API_KEY
pip install openai composio-core composio-openai output
Collecting openai Collecting composio-core Collecting composio-openai Successfully installed openai-1.x composio-core-1.x composio-openai-1.x
Step by step
Use ComposioToolSet to get tools and pass them to an OpenAI client chat completion call. Handle tool calls from the response.
import os
from openai import OpenAI
from composio_openai import ComposioToolSet, Action
import json
# Initialize Composio toolset with your API key
toolset = ComposioToolSet(api_key=os.environ["OPENAI_API_KEY"])
# Get tools for specific actions, e.g., GitHub star repository
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
response = client.chat.completions.create(
model="gpt-4o-mini",
tools=tools,
messages=[{"role": "user", "content": "Star the openai/openai-python repo"}]
)
print("Assistant reply:", response.choices[0].message.content)
# Check if a tool call was made
if response.choices[0].finish_reason == "tool_calls":
tool_call = response.choices[0].message.tool_calls[0]
args = json.loads(tool_call.function.arguments)
print("Tool call name:", tool_call.function.name)
print("Tool call arguments:", args)
# Handle the tool call result (example placeholder)
result = toolset.handle_tool_calls(response)
print("Tool call result:", result) output
Assistant reply: I have starred the openai/openai-python repository for you.
Tool call name: github_star_repository
Tool call arguments: {'repository': 'openai/openai-python'}
Tool call result: {'success': True, 'message': 'Repository starred successfully.'} Common variations
You can use different models like gpt-4o or gpt-4o-mini depending on your latency and cost needs. The tools parameter supports multiple tool actions simultaneously. Async usage is possible by using async OpenAI clients and awaiting calls.
import asyncio
from openai import OpenAI
from composio_openai import ComposioToolSet, Action
async def async_example():
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 assistant reply:", response.choices[0].message.content)
asyncio.run(async_example()) output
Async assistant reply: I've starred the openai/openai-python repository as requested.
Troubleshooting
- If you get an authentication error, verify your
OPENAI_API_KEYenvironment variable is set correctly. - If tool calls are not recognized, ensure you pass the
toolsparameter and use a supported model likegpt-4o-mini. - For JSON decode errors on tool arguments, check that the tool call response is complete and valid JSON.
Key Takeaways
- Use
ComposioToolSetto fetch and manage AI tools for OpenAI chat completions. - Pass the
toolsparameter to enable function calling and automation in chat requests. - Handle tool calls by checking
finish_reasonand parsingtool_callsfrom the response. - Supports multiple actions like GitHub starring via predefined
Actionenums. - Async usage is supported with the OpenAI SDK's async methods.