Merge pull request #102 from lethanhson9901/feature/openai-structured-output-support
feat: Add structured output support for OpenAI to Gemini conversion
This commit is contained in:
commit
8239f165cf
1 changed files with 49 additions and 37 deletions
|
|
@ -346,7 +346,7 @@ export class OpenAIConverter extends BaseConverter {
|
||||||
// 处理 OpenAI chunk 对象
|
// 处理 OpenAI chunk 对象
|
||||||
if (typeof openaiChunk === 'object' && !Array.isArray(openaiChunk)) {
|
if (typeof openaiChunk === 'object' && !Array.isArray(openaiChunk)) {
|
||||||
const choice = openaiChunk.choices?.[0];
|
const choice = openaiChunk.choices?.[0];
|
||||||
if (!choice){
|
if (!choice) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -445,8 +445,8 @@ export class OpenAIConverter extends BaseConverter {
|
||||||
if (finishReason) {
|
if (finishReason) {
|
||||||
// 映射 finish_reason
|
// 映射 finish_reason
|
||||||
const stopReason = finishReason === "stop" ? "end_turn" :
|
const stopReason = finishReason === "stop" ? "end_turn" :
|
||||||
finishReason === "length" ? "max_tokens" :
|
finishReason === "length" ? "max_tokens" :
|
||||||
"end_turn";
|
"end_turn";
|
||||||
|
|
||||||
events.push({
|
events.push({
|
||||||
type: "content_block_stop",
|
type: "content_block_stop",
|
||||||
|
|
@ -711,13 +711,25 @@ export class OpenAIConverter extends BaseConverter {
|
||||||
/**
|
/**
|
||||||
* 构建Gemini生成配置
|
* 构建Gemini生成配置
|
||||||
*/
|
*/
|
||||||
buildGeminiGenerationConfig({ temperature, max_tokens, top_p, stop, tools }, model) {
|
buildGeminiGenerationConfig({ temperature, max_tokens, top_p, stop, tools, response_format }, model) {
|
||||||
const config = {};
|
const config = {};
|
||||||
config.temperature = checkAndAssignOrDefault(temperature, 1);
|
config.temperature = checkAndAssignOrDefault(temperature, 1);
|
||||||
config.maxOutputTokens = checkAndAssignOrDefault(max_tokens, 65535);
|
config.maxOutputTokens = checkAndAssignOrDefault(max_tokens, 65535);
|
||||||
config.topP = checkAndAssignOrDefault(top_p, 0.95);
|
config.topP = checkAndAssignOrDefault(top_p, 0.95);
|
||||||
if (stop !== undefined) config.stopSequences = Array.isArray(stop) ? stop : [stop];
|
if (stop !== undefined) config.stopSequences = Array.isArray(stop) ? stop : [stop];
|
||||||
|
|
||||||
|
// Handle response_format
|
||||||
|
if (response_format) {
|
||||||
|
if (response_format.type === 'json_object') {
|
||||||
|
config.responseMimeType = 'application/json';
|
||||||
|
} else if (response_format.type === 'json_schema' && response_format.json_schema) {
|
||||||
|
config.responseMimeType = 'application/json';
|
||||||
|
if (response_format.json_schema.schema) {
|
||||||
|
config.responseSchema = response_format.json_schema.schema;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Gemini 2.5 and thinking models require responseModalities: ["TEXT"]
|
// Gemini 2.5 and thinking models require responseModalities: ["TEXT"]
|
||||||
// But this parameter cannot be added when using tools (causes 400 error)
|
// But this parameter cannot be added when using tools (causes 400 error)
|
||||||
const hasTools = tools && Array.isArray(tools) && tools.length > 0;
|
const hasTools = tools && Array.isArray(tools) && tools.length > 0;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue