@devries-cameron20 / composio-1 / commits / 48a4eb711
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Add openai toolset example

haxzie committed Apr 18, 2025 at 13:41 UTC 48a4eb7114c01587b43c0ea9a9fd688ccf368094
1 file changed +176
packages/toolsets/openai/README.md new
+176
@@ -0,0 +1,176 @@
1 +# @composio/openai
2 +
3 +The OpenAI toolset for Composio SDK, providing seamless integration with OpenAI's API and tools.
4 +
5 +## Features
6 +
7 +- **OpenAI Tool Integration**: Seamlessly integrate OpenAI tools with Composio
8 +- **Tool Execution**: Execute OpenAI tools with proper parameter handling
9 +- **Stream Support**: Handle streaming responses from OpenAI
10 +- **Assistant Integration**: Work with OpenAI Assistants and their tool calls
11 +- **Thread Management**: Manage OpenAI threads and runs
12 +
13 +## Installation
14 +
15 +```bash
16 +npm install @composio/openai
17 +# or
18 +yarn add @composio/openai
19 +# or
20 +pnpm add @composio/openai
21 +```
22 +
23 +## Quick Start
24 +
25 +```typescript
26 +import { Composio } from '@composio/core';
27 +import { OpenAIToolset } from '@composio/openai';
28 +
29 +// Initialize Composio with OpenAI toolset
30 +const composio = new Composio({
31 + apiKey: 'your-composio-api-key',
32 + toolset: new OpenAIToolset()
33 +});
34 +
35 +// Get available tools in OpenAI format
36 +const tools = await composio.getTools();
37 +
38 +// Execute a tool call
39 +const result = await composio.toolset.executeToolCall(toolCall, 'user-id');
40 +```
41 +
42 +## API Reference
43 +
44 +### OpenAIToolset Class
45 +
46 +The `OpenAIToolset` class extends `BaseComposioToolset` and provides OpenAI-specific functionality.
47 +
48 +#### Methods
49 +
50 +##### `executeToolCall(tool: OpenAI.ChatCompletionMessageToolCall, userId?: string): Promise<string>`
51 +Executes a tool call from OpenAI and returns the result.
52 +
53 +```typescript
54 +const result = await toolset.executeToolCall(toolCall, 'user-id');
55 +```
56 +
57 +##### `handleToolCall(chatCompletion: OpenAI.ChatCompletion, userId?: string): Promise<string[]>`
58 +Handles tool calls from a chat completion.
59 +
60 +```typescript
61 +const outputs = await toolset.handleToolCall(chatCompletion, 'user-id');
62 +```
63 +
64 +##### `handleAssistantMessage(run: OpenAI.Beta.Threads.Run, userId?: string): Promise<OpenAI.Beta.Threads.Runs.RunSubmitToolOutputsParams.ToolOutput[]>`
65 +Handles tool calls from an assistant message.
66 +
67 +```typescript
68 +const toolOutputs = await toolset.handleAssistantMessage(run, 'user-id');
69 +```
70 +
71 +##### `waitAndHandleAssistantStreamToolCalls(client: OpenAI, runStream: Stream<OpenAI.Beta.Assistants.AssistantStreamEvent>, thread: OpenAI.Beta.Threads.Thread, userId?: string): AsyncGenerator<OpenAI.Beta.Assistants.AssistantStreamEvent, void, unknown>`
72 +Waits for and handles tool calls from an assistant stream.
73 +
74 +```typescript
75 +for await (const event of toolset.waitAndHandleAssistantStreamToolCalls(client, runStream, thread, 'user-id')) {
76 + // Handle events
77 +}
78 +```
79 +
80 +##### `waitAndHandleAssistantToolCalls(client: OpenAI, run: OpenAI.Beta.Threads.Run, thread: OpenAI.Beta.Threads.Thread, userId?: string): Promise<OpenAI.Beta.Threads.Run>`
81 +Waits for and handles tool calls from an assistant.
82 +
83 +```typescript
84 +const finalRun = await toolset.waitAndHandleAssistantToolCalls(client, run, thread, 'user-id');
85 +```
86 +
87 +## Examples
88 +
89 +### Basic Tool Execution
90 +
91 +```typescript
92 +import { Composio } from '@composio/core';
93 +import { OpenAIToolset } from '@composio/openai';
94 +import OpenAI from 'openai';
95 +
96 +const composio = new Composio({
97 + apiKey: 'your-composio-api-key',
98 + toolset: new OpenAIToolset()
99 +});
100 +
101 +const openai = new OpenAI({
102 + apiKey: 'your-openai-api-key'
103 +});
104 +
105 +// Get available tools
106 +const tools = await composio.getTools();
107 +
108 +// Create a chat completion with tools
109 +const completion = await openai.chat.completions.create({
110 + model: 'gpt-4',
111 + messages: [{ role: 'user', content: 'What can you do?' }],
112 + tools: tools
113 +});
114 +
115 +// Handle tool calls
116 +if (completion.choices[0].message.tool_calls) {
117 + const result = await composio.toolset.executeToolCall(
118 + completion.choices[0].message.tool_calls[0],
119 + 'user-id'
120 + );
121 + console.log(result);
122 +}
123 +```
124 +
125 +### Working with Assistants
126 +
127 +```typescript
128 +import { Composio } from '@composio/core';
129 +import { OpenAIToolset } from '@composio/openai';
130 +import OpenAI from 'openai';
131 +
132 +const composio = new Composio({
133 + apiKey: 'your-composio-api-key',
134 + toolset: new OpenAIToolset()
135 +});
136 +
137 +const openai = new OpenAI({
138 + apiKey: 'your-openai-api-key'
139 +});
140 +
141 +// Create an assistant with tools
142 +const assistant = await openai.beta.assistants.create({
143 + name: 'Composio Assistant',
144 + instructions: 'You are a helpful assistant that can use Composio tools.',
145 + model: 'gpt-4',
146 + tools: await composio.getTools()
147 +});
148 +
149 +// Create a thread
150 +const thread = await openai.beta.threads.create();
151 +
152 +// Create a run
153 +const run = await openai.beta.threads.runs.create(thread.id, {
154 + assistant_id: assistant.id
155 +});
156 +
157 +// Wait for and handle tool calls
158 +const finalRun = await composio.toolset.waitAndHandleAssistantToolCalls(
159 + openai,
160 + run,
161 + thread,
162 + 'user-id'
163 +);
164 +```
165 +
166 +## Contributing
167 +
168 +We welcome contributions! Please see our [Contributing Guide](../../CONTRIBUTING.md) for more details.
169 +
170 +## License
171 +
172 +ISC License
173 +
174 +## Support
175 +
176 +For support, please visit our [Documentation](https://docs.composio.dev) or join our [Discord Community](https://discord.gg/composio).
\ No newline at end of file