Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | """ |
| 2 | OpenAI Responses API demo. |
| 3 | """ |
| 4 | |
| 5 | import json |
| 6 | |
| 7 | from composio_openai import OpenAIResponsesProvider |
| 8 | from openai import OpenAI |
| 9 | |
| 10 | from composio import Composio |
| 11 | |
| 12 | # Initialize tools. |
| 13 | openai_client = OpenAI() |
| 14 | composio = Composio(provider=OpenAIResponsesProvider()) |
| 15 | |
| 16 | # Define task. |
| 17 | task = "Tell me about the user `pg` on Hacker News." |
| 18 | |
| 19 | # Get tools that are pre-configured for the Responses API. |
| 20 | tools = composio.tools.get(user_id="default", tools=["HACKERNEWS_GET_USER"]) |
| 21 | |
| 22 | # Get the first response from the LLM. |
| 23 | response = openai_client.responses.create( |
| 24 | model="gpt-5", |
| 25 | tools=tools, |
| 26 | input=task, |
| 27 | ) |
| 28 | print(response.output_text) |
| 29 | |
| 30 | # Execute tool calls until the model returns a final answer. |
| 31 | while True: |
| 32 | tool_calls = [item for item in response.output if item.type == "function_call"] |
| 33 | if not tool_calls: |
| 34 | break |
| 35 | |
| 36 | results = composio.provider.handle_tool_calls(response=response, user_id="default") |
| 37 | response = openai_client.responses.create( |
| 38 | model="gpt-5", |
| 39 | tools=tools, |
| 40 | previous_response_id=response.id, |
| 41 | input=[ |
| 42 | { |
| 43 | "type": "function_call_output", |
| 44 | "call_id": tool_calls[index].call_id, |
| 45 | "output": json.dumps(result), |
| 46 | } |
| 47 | for index, result in enumerate(results) |
| 48 | ], |
| 49 | ) |
| 50 | |
| 51 | print(response.output_text) |