Skip to content

Commit 5e39d54

Browse files
[FIRE-1020] feature: add compile_prompt function to the client (#24)
1 parent 527b82a commit 5e39d54

2 files changed

Lines changed: 83 additions & 1 deletion

File tree

src/index.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ import { GeminiAICanonicalEvaluationStrategy } from './frameworks/gemini/gemini-
55
import { OpenAICanonicalEvaluationStrategy } from './frameworks/openai/openai-converter';
66
import { VercelAICanonicalEvaluationStrategy } from './frameworks/vercelai/vercelai-converter';
77
import {
8+
type CompilePromptResponse,
89
EvaluationProxyAPIRequest,
910
EvaluationProxyAPIRequestSchema,
10-
EvaluationRequestV2Schema,
1111
type EvaluationRequestV2,
12+
EvaluationRequestV2Schema,
1213
type EvaluationResponse,
1314
type Framework,
1415
} from './types';
1516

1617
export type {
18+
CompilePromptResponse,
1719
EvaluationProxyAPIRequest,
1820
EvaluationRequestV2,
1921
EvaluationResponse,
2022
Framework,
2123
LLMMessage,
2224
ModelMode,
2325
PolicyTarget,
26+
ToolResponse,
2427
} from './types';
2528

2629
/**
@@ -418,4 +421,66 @@ export class Qualifire {
418421
const jsonResponse = await response.json();
419422
return jsonResponse as EvaluationResponse;
420423
};
424+
425+
/**
426+
* Compiles a prompt from Qualifire Studio with the specified parameters.
427+
*
428+
* @param promptId - The ID of the prompt to compile.
429+
* @param revisionId - Optional revision ID to use. If not provided, uses the latest revision.
430+
* @param params - Optional dictionary of parameters to substitute in the prompt template.
431+
* @returns A CompilePromptResponse containing the compiled prompt details.
432+
*
433+
* @example
434+
* ```ts
435+
* const qualifire = new Qualifire({ apiKey: 'your_api_key' });
436+
*
437+
* const response = await qualifire.compilePrompt({
438+
* promptId: 'prompt-123',
439+
* revisionId: 'rev-456',
440+
* params: {
441+
* user_name: 'John',
442+
* language: 'French'
443+
* }
444+
* });
445+
*
446+
* console.log(response.messages);
447+
* console.log(response.tools);
448+
* ```
449+
*/
450+
compilePrompt = async ({
451+
promptId,
452+
revisionId,
453+
params,
454+
}: {
455+
promptId: string;
456+
revisionId?: string;
457+
params?: Record<string, string>;
458+
}): Promise<CompilePromptResponse> => {
459+
let url = `${this.baseUrl}/api/v1/studio/prompts/${promptId}/compile`;
460+
if (revisionId) {
461+
url = `${url}?revision=${revisionId}`;
462+
}
463+
464+
const headers = {
465+
'Content-Type': 'application/json',
466+
'X-Qualifire-API-Key': this.sdkKey,
467+
};
468+
469+
const body = JSON.stringify({
470+
variables: params || {},
471+
});
472+
473+
const response = await fetch(url, {
474+
method: 'POST',
475+
headers,
476+
body,
477+
});
478+
479+
if (!response.ok) {
480+
throw new Error(`Qualifire API error: ${response.statusText}`);
481+
}
482+
483+
const jsonResponse = await response.json();
484+
return jsonResponse as CompilePromptResponse;
485+
};
421486
}

src/types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,20 @@ export type EvaluationResponse = z.infer<typeof EvaluationResponseSchema>;
270270
export type LLMToolDefinition = z.infer<typeof LLMToolDefinitionSchema>;
271271
export type LLMToolCall = z.infer<typeof LLMToolCallSchema>;
272272
export type EvaluationRequestV2 = z.infer<typeof EvaluationRequestV2Schema>;
273+
274+
const ToolResponseSchema = z.object({
275+
type: z.string(),
276+
function: LLMToolDefinitionSchema,
277+
});
278+
279+
const CompilePromptResponseSchema = z.object({
280+
id: z.string(),
281+
name: z.string(),
282+
revision: z.number(),
283+
messages: z.array(LLMMessageSchema),
284+
tools: z.array(ToolResponseSchema),
285+
parameters: z.record(z.string(), z.any()),
286+
});
287+
288+
export type ToolResponse = z.infer<typeof ToolResponseSchema>;
289+
export type CompilePromptResponse = z.infer<typeof CompilePromptResponseSchema>;

0 commit comments

Comments
 (0)