OpenAI Integration
Add SOVR to OpenAI function calling.
Setup
typescript
import OpenAI from 'openai';
import { SovrClient } from '@sovr/sdk';
const openai = new OpenAI();
const sovr = new SovrClient({
tenantId: process.env.SOVR_TENANT_ID!,
apiKey: process.env.SOVR_API_KEY!,
});Protected Function Execution
typescript
async function executeFunction(name: string, args: any) {
// Gate check before execution
const check = await sovr.gate.check({
action: name,
context: args,
});
switch (check.decision) {
case 'allow':
return await actuallyExecute(name, args);
case 'deny':
throw new Error(`Denied: ${check.reason}`);
case 'require_approval':
return { pending: true, approvalId: check.approvalId };
}
}Full Example
typescript
const tools = [{
type: 'function',
function: {
name: 'send_email',
parameters: {
type: 'object',
properties: {
to: { type: 'string' },
subject: { type: 'string' },
body: { type: 'string' },
},
},
},
}];
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Send report to team' }],
tools,
});
for (const call of response.choices[0].message.tool_calls || []) {
const result = await executeFunction(
call.function.name,
JSON.parse(call.function.arguments)
);
}