|
| 1 | +import OpenAI from 'openai'; |
| 2 | +import { ACI } from '../src/client'; |
| 3 | +import { |
| 4 | + ACISearchFunctions, |
| 5 | + ACIExecuteFunction |
| 6 | +} from '../src/meta_functions'; |
| 7 | +import { FunctionDefinitionFormat } from '../src/types/functions'; |
| 8 | +import dotenv from 'dotenv'; |
| 9 | + |
| 10 | +// Load environment variables |
| 11 | +dotenv.config(); |
| 12 | + |
| 13 | +async function main() { |
| 14 | + // Initialize clients |
| 15 | + const aciClient = new ACI({ |
| 16 | + apiKey: process.env.ACI_API_KEY as string |
| 17 | + }); |
| 18 | + |
| 19 | + const openaiClient = new OpenAI({ |
| 20 | + apiKey: process.env.OPENAI_API_KEY as string, |
| 21 | + }); |
| 22 | + |
| 23 | + // Get function schemas in OpenAI format |
| 24 | + const aciSearchFunctionsSchema = ACISearchFunctions.toJsonSchema( |
| 25 | + FunctionDefinitionFormat.OPENAI_RESPONSES |
| 26 | + ); |
| 27 | + |
| 28 | + const aciExecuteFunctionSchema = ACIExecuteFunction.toJsonSchema( |
| 29 | + FunctionDefinitionFormat.OPENAI_RESPONSES |
| 30 | + ); |
| 31 | + |
| 32 | + const systemPrompt = `You are a helpful assistant with access to an unlimited number of tools via some meta functions: |
| 33 | + - ACI_SEARCH_FUNCTIONS, |
| 34 | + - and ACI_EXECUTE_FUNCTION. |
| 35 | + You can use ACI_SEARCH_FUNCTIONS to find relevant functions across all apps. |
| 36 | + Try to limit the number of results per request to 10. |
| 37 | + Once you have identified the function you need to use, |
| 38 | + you can use ACI_EXECUTE_FUNCTION to execute the function provided you have the correct input arguments. |
| 39 | +`; |
| 40 | + |
| 41 | + const userMessage = "Can you star aipotheosis-labs/aci github repo?" |
| 42 | + |
| 43 | + // Step 1: Search for relevant functions |
| 44 | + const searchResponse = await openaiClient.responses.create({ |
| 45 | + model: 'gpt-4', |
| 46 | + input: [ |
| 47 | + { |
| 48 | + role: 'system', |
| 49 | + content: systemPrompt |
| 50 | + }, |
| 51 | + { |
| 52 | + role: 'user', |
| 53 | + content: userMessage |
| 54 | + } |
| 55 | + ], |
| 56 | + tools: [aciSearchFunctionsSchema, aciExecuteFunctionSchema] as any |
| 57 | + }); |
| 58 | + |
| 59 | + // Find the first function call in the response |
| 60 | + const searchToolCall = searchResponse.output.find( |
| 61 | + (item: any) => item.type === 'function_call' |
| 62 | + ); |
| 63 | + |
| 64 | + if (!searchToolCall || searchToolCall.type !== 'function_call') { |
| 65 | + throw new Error('No tool call returned from OpenAI'); |
| 66 | + } |
| 67 | + |
| 68 | + // Step 2: Handle the ACI search function call |
| 69 | + const searchArguments = JSON.parse(searchToolCall.arguments); |
| 70 | + const searchResults = await aciClient.handleFunctionCall({ |
| 71 | + functionName: searchToolCall.name, |
| 72 | + functionArguments: searchArguments, |
| 73 | + linkedAccountOwnerId: 'your-user-id', // Replace with actual user ID |
| 74 | + allowedAppsOnly: false, |
| 75 | + format: FunctionDefinitionFormat.OPENAI_RESPONSES |
| 76 | + }); |
| 77 | + |
| 78 | + // Step 3: Execute the found function |
| 79 | + const executeResponse = await openaiClient.responses.create({ |
| 80 | + model: 'gpt-4', |
| 81 | + input: [ |
| 82 | + { |
| 83 | + role: 'system', |
| 84 | + content: systemPrompt |
| 85 | + }, |
| 86 | + { |
| 87 | + role: 'user', |
| 88 | + content: userMessage |
| 89 | + }, |
| 90 | + { |
| 91 | + ...searchToolCall |
| 92 | + }, |
| 93 | + { |
| 94 | + type: "function_call_output", |
| 95 | + call_id: searchToolCall.call_id, |
| 96 | + "output": JSON.stringify(searchResults) |
| 97 | + }, |
| 98 | + ], |
| 99 | + tools: [aciSearchFunctionsSchema, aciExecuteFunctionSchema] as any |
| 100 | + } as any); |
| 101 | + |
| 102 | + // Extract the execution tool call |
| 103 | + const executeToolCall = executeResponse.output.find( |
| 104 | + (item: any) => item.type === 'function_call' |
| 105 | + ); |
| 106 | + |
| 107 | + if (!executeToolCall || executeToolCall.type !== 'function_call') { |
| 108 | + throw new Error('No execution tool call returned from OpenAI'); |
| 109 | + } |
| 110 | + |
| 111 | + // Step 4: Execute the function |
| 112 | + const executeArguments = JSON.parse(executeToolCall.arguments); |
| 113 | + const executionResult = await aciClient.handleFunctionCall({ |
| 114 | + functionName: executeToolCall.name, |
| 115 | + functionArguments: executeArguments, |
| 116 | + linkedAccountOwnerId: process.env.LINKED_ACCOUNT_OWNER_ID as string, |
| 117 | + allowedAppsOnly: false, |
| 118 | + format: FunctionDefinitionFormat.OPENAI_RESPONSES |
| 119 | + }); |
| 120 | + |
| 121 | + console.log('Execution result:', executionResult); |
| 122 | +} |
| 123 | + |
| 124 | +// Run the example |
| 125 | +main().catch(console.error); |
0 commit comments