From ebec1bc713fe7675e8e1f1ac8fb7081e4a85fe19 Mon Sep 17 00:00:00 2001 From: Han Xiao Date: Mon, 3 Mar 2025 10:58:00 +0800 Subject: [PATCH] feat: add json schema support --- src/app.ts | 9 +++++---- src/types.ts | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/app.ts b/src/app.ts index e6d117f..efc024c 100644 --- a/src/app.ts +++ b/src/app.ts @@ -493,7 +493,6 @@ app.post('/v1/chat/completions', (async (req: Request, res: Response) => { if (responseSchema) { try { - console.log('hello2') const generator = new ObjectGeneratorSafe(context?.tokenTracker); const result = await generator.generateObject({ model: 'agent', @@ -539,7 +538,7 @@ app.post('/v1/chat/completions', (async (req: Request, res: Response) => { system_fingerprint: 'fp_' + requestId, choices: [{ index: 0, - delta: {content: finalAnswer, type: "text"}, + delta: {content: finalAnswer, type: responseSchema? 'json': 'text'}, logprobs: null, finish_reason: 'stop' }], @@ -561,7 +560,8 @@ app.post('/v1/chat/completions', (async (req: Request, res: Response) => { index: 0, message: { role: 'assistant', - content: finalStep.action === 'answer' ? (finalAnswer || '') : finalStep.think + content: finalStep.action === 'answer' ? (finalAnswer || '') : finalStep.think, + type: responseSchema? 'json': 'text' }, logprobs: null, finish_reason: 'stop' @@ -649,7 +649,8 @@ app.post('/v1/chat/completions', (async (req: Request, res: Response) => { index: 0, message: { role: 'assistant', - content: `Error: ${errorMessage}` + content: `Error: ${errorMessage}`, + type: 'error' }, logprobs: null, finish_reason: 'stop' diff --git a/src/types.ts b/src/types.ts index 770bce2..80b294c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -213,6 +213,7 @@ export interface ChatCompletionResponse { message: { role: 'assistant'; content: string; + type: 'text' | 'think' | 'json' | 'error'; }; logprobs: null; finish_reason: 'stop';