Composio GitHub integration guide
Quick answer
Use the
composio_openai package to obtain GitHub tools, then call the OpenAI chat.completions.create method with tools parameter to interact with GitHub. Handle tool calls by passing the response to toolset.handle_tool_calls() for seamless GitHub integration.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 packages:
openai,composio-core, andcomposio-openai. - Set
OPENAI_API_KEYandCOMPOSIO_API_KEYin your environment.
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
This example demonstrates how to get GitHub tools from Composio and use them with the OpenAI SDK to star a repository.
import os
from openai import OpenAI
from composio_openai import ComposioToolSet, Action
# Initialize Composio toolset with your Composio API key
toolset = ComposioToolSet(api_key=os.environ["COMPOSIO_API_KEY"])
# Get GitHub star repository tool
tools = toolset.get_tools(actions=[Action.GITHUB_STAR_A_REPOSITORY])
# Initialize OpenAI client with your OpenAI API key
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Prepare user message to star a GitHub repo
messages = [{"role": "user", "content": "Star the openai/openai-python repo"}]
# Call chat completion with tools parameter
response = client.chat.completions.create(
model="gpt-4o-mini",
tools=tools,
messages=messages
)
# Check if a tool call was made and handle it
if response.choices[0].finish_reason == "tool_calls":
result = toolset.handle_tool_calls(response)
print("Tool call result:", result)
else:
print("Response:", response.choices[0].message.content) output
Tool call result: {'success': True, 'message': 'Repository openai/openai-python starred successfully.'} Common variations
You can use different models like gpt-4o or gpt-4o-mini depending on your needs. The Composio toolset supports other GitHub actions such as creating issues or forking repos. Async usage is possible by using asyncio and the OpenAI async client.
import asyncio
import os
from openai import OpenAI
from composio_openai import ComposioToolSet, Action
async def main():
toolset = ComposioToolSet(api_key=os.environ["COMPOSIO_API_KEY"])
tools = toolset.get_tools(actions=[Action.GITHUB_STAR_A_REPOSITORY])
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
messages = [{"role": "user", "content": "Star the openai/openai-python repo"}]
response = await client.chat.completions.create(
model="gpt-4o",
tools=tools,
messages=messages
)
if response.choices[0].finish_reason == "tool_calls":
result = toolset.handle_tool_calls(response)
print("Async tool call result:", result)
else:
print("Async response:", response.choices[0].message.content)
asyncio.run(main()) output
Async tool call result: {'success': True, 'message': 'Repository openai/openai-python starred successfully.'} Troubleshooting
- If you get authentication errors, verify your
OPENAI_API_KEYandCOMPOSIO_API_KEYenvironment variables are set correctly. - If the tool call does not execute, ensure you pass the
toolsparameter tochat.completions.create. - For unexpected errors, check network connectivity and API quota limits.
Key Takeaways
- Use
ComposioToolSetto fetch GitHub tools for integration. - Pass the tools list to
chat.completions.createwith thetoolsparameter. - Handle tool calls with
toolset.handle_tool_calls()to execute GitHub actions. - Support for async calls is available with the OpenAI async client and
asyncio. - Always set your API keys in environment variables to avoid authentication issues.