Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

feat: added response format (#1304)

Mintu Gogoia9dd52925efd
diff --git a/screenpipe-actions/ai-proxy/src/providers/anthropic.ts b/screenpipe-actions/ai-proxy/src/providers/anthropic.ts
index 68e106aa4..8eca11c68 100644
--- a/screenpipe-actions/ai-proxy/src/providers/anthropic.ts
+++ b/screenpipe-actions/ai-proxy/src/providers/anthropic.ts
@@ -1,5 +1,5 @@
import { AIProvider } from './base';
-import { Message, RequestBody, Tool, AnthropicTool } from '../types';
+import { Message, RequestBody, Tool, AnthropicTool, ResponseFormat } from '../types';
import Anthropic from '@anthropic-ai/sdk';
import type {
MessageParam,
@@ -28,7 +28,7 @@ export class AnthropicProvider implements AIProvider {
model: body.model,
max_tokens: 4096,
temperature: body.temperature,
- system: body.response_format?.type === 'json_object' ? 'Respond with valid JSON only.' : undefined,
+ system: this.createSystemPrompt(body.response_format),
tools: body.tools ? this.formatTools(body.tools) : undefined,
});
@@ -37,6 +37,23 @@ export class AnthropicProvider implements AIProvider {
});
}
+ private createSystemPrompt(responseFormat?: ResponseFormat): string | undefined {
+ if (!responseFormat) return undefined;
+
+ switch (responseFormat.type) {
+ case 'json_object':
+ return 'Respond with valid JSON only.';
+ case 'json_schema':
+ if (!responseFormat.schema) return undefined;
+
+ return `Respond with valid JSON that strictly follows this schema:
+ ${JSON.stringify(responseFormat.schema, null, 2)}
+ Do not include any explanatory text - output valid JSON only.`;
+ default:
+ return undefined;
+ }
+ }
+
async createStreamingCompletion(body: RequestBody): Promise<ReadableStream> {
const stream = await this.client.messages.create({
messages: this.formatMessages(body.messages),
@@ -73,14 +90,7 @@ export class AnthropicProvider implements AIProvider {
return tools.map((tool) => ({
name: tool.function.name,
description: tool.function.description,
- input_schema: {
- type: 'object',
- properties: tool.function.parameters.properties,
- required: tool.function.parameters.required,
- ...Object.entries(tool.function.parameters)
- .filter(([key]) => !['type', 'properties', 'required'].includes(key))
- .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}),
- },
+ input_schema: tool.function.parameters,
}));
}
diff --git a/screenpipe-actions/ai-proxy/src/providers/gemini.ts b/screenpipe-actions/ai-proxy/src/providers/gemini.ts
index d4196f98d..2cf4c15b8 100644
--- a/screenpipe-actions/ai-proxy/src/providers/gemini.ts
+++ b/screenpipe-actions/ai-proxy/src/providers/gemini.ts
@@ -13,8 +13,23 @@ export class GeminiProvider implements AIProvider {
this.client = new GoogleGenerativeAI(apiKey);
}
+ private createGenerationConfig(body: RequestBody) {
+ const config: any = {
+ temperature: body.temperature,
+ };
+
+ if (body.response_format?.type === 'json_schema' && body.response_format.schema) {
+ config.responseMimeType = 'application/json';
+ config.responseSchema = body.response_format.schema;
+ } else if (body.response_format?.type === 'json_object') {
+ config.responseMimeType = 'application/json';
+ }
+
+ return config;
+ }
+
async createCompletion(body: RequestBody): Promise<Response> {
- this.model = this.client.getGenerativeModel({ model: body.model });
+ this.model = this.client.getGenerativeModel({ model: body.model, generationConfig: this.createGenerationConfig(body) });
const chat = this.model.startChat({
history: this.formatMessages(body.messages),
diff --git a/screenpipe-actions/ai-proxy/src/providers/openai.ts b/screenpipe-actions/ai-proxy/src/providers/openai.ts
index 3c2d60a69..f90c9c87a 100644
--- a/screenpipe-actions/ai-proxy/src/providers/openai.ts
+++ b/screenpipe-actions/ai-proxy/src/providers/openai.ts
@@ -1,7 +1,8 @@
import { AIProvider } from './base';
-import { Message, RequestBody } from '../types';
+import { Message, RequestBody, ResponseFormat } from '../types';
import OpenAI from 'openai';
import type { ChatCompletionMessage, ChatCompletionCreateParams } from 'openai/resources/chat';
+import type { ResponseFormatJSONSchema } from 'openai/resources';
export class OpenAIProvider implements AIProvider {
supportsTools = true;
@@ -13,27 +14,44 @@ export class OpenAIProvider implements AIProvider {
this.client = new OpenAI({ apiKey });
}
+ private createJSONSchemaFormat(schema: Record<string, unknown>, name: string, description?: string): ResponseFormatJSONSchema {
+ return {
+ type: 'json_schema',
+ json_schema: {
+ name,
+ description,
+ schema,
+ strict: true,
+ },
+ };
+ }
+
+ private formatResponseFormat(format?: ResponseFormat): ChatCompletionCreateParams['response_format'] {
+ if (!format) return undefined;
+
+ switch (format.type) {
+ case 'json_object':
+ return { type: 'json_object' };
+ case 'json_schema':
+ if (!format.schema || !format.name) {
+ throw new Error('Schema and name are required for json_schema response format');
+ }
+ return this.createJSONSchemaFormat(format.schema, format.name, format.description);
+ default:
+ return undefined;
+ }
+ }
+
async createCompletion(body: RequestBody): Promise<Response> {
const messages = this.formatMessages(body.messages);
+ const responseFormat = this.formatResponseFormat(body.response_format);
const params: ChatCompletionCreateParams = {
model: body.model,
messages,
temperature: body.temperature,
stream: false,
- response_format:
- body.response_format?.type === 'json_object'
- ? { type: 'json_object' }
- : body.response_format?.type === 'json_schema'
- ? {
- type: 'json_schema',
- json_schema: {
- schema: body.response_format.schema!,
- name: body.response_format.name || 'default',
- strict: true,
- },
- }
- : undefined,
+ response_format: responseFormat,
tools: body.tools as ChatCompletionCreateParams['tools'],
tool_choice: body.tool_choice as ChatCompletionCreateParams['tool_choice'],
};
diff --git a/screenpipe-actions/ai-proxy/src/types.ts b/screenpipe-actions/ai-proxy/src/types.ts
index 123557927..e3e38aed9 100644
--- a/screenpipe-actions/ai-proxy/src/types.ts
+++ b/screenpipe-actions/ai-proxy/src/types.ts
@@ -56,11 +56,7 @@ export interface Tool {
function: {
name: string;
description: string;
- parameters: {
- type: 'object';
- properties: Record<string, any>;
- required?: string[];
- };
+ parameters: InputSchema
};
}
@@ -74,10 +70,13 @@ export interface RequestBody {
response_format?: ResponseFormat;
}
+type InputSchema = Anthropic.Tool.InputSchema;
+
export interface ResponseFormat {
type: 'text' | 'json_object' | 'json_schema';
- schema?: Record<string, any>;
+ schema?: InputSchema;
name?: string;
+ description?: string;
}
export interface ImageContent {