Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions js/plugins/compat-oai/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ export function fromOpenAIChoice(
// Build content array based on what's present in the message
let content: Part[] = [];

if (toolRequestParts) {
if (toolRequestParts && toolRequestParts.length > 0) {
Comment thread
ifielker marked this conversation as resolved.
content = toolRequestParts as ToolRequestPart[];
} else {
// Handle reasoning_content if present
Expand Down Expand Up @@ -426,7 +426,7 @@ export function fromOpenAIChunkChoice(
// Build content array based on what's present in the delta
let content: Part[] = [];

if (toolRequestParts) {
if (toolRequestParts && toolRequestParts.length > 0) {
content = toolRequestParts as ToolRequestPart[];
} else {
// Handle reasoning_content if present
Expand Down Expand Up @@ -644,7 +644,7 @@ export function openAIModelRunner(

/**
* Method to define a new Genkit Model that is compatible with Open AI
* Chat Completions API.
* Chat Completions API.
*
* These models are to be used to chat with a large language model.
*
Expand Down
53 changes: 37 additions & 16 deletions js/plugins/compat-oai/tests/compat_oai_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe('toOpenAiTextAndMedia', () => {
describe('toOpenAiMessages', () => {
const testCases = [
{
should: 'should transform tool request content correctly',
should: 'transform tool request content correctly',
inputMessages: [
{
role: 'model',
Expand Down Expand Up @@ -241,7 +241,7 @@ describe('toOpenAiMessages', () => {
],
},
{
should: 'should transform tool response text content correctly',
should: 'transform tool response text content correctly',
inputMessages: [
{
role: 'tool',
Expand All @@ -265,7 +265,7 @@ describe('toOpenAiMessages', () => {
],
},
{
should: 'should transform tool response json content correctly',
should: 'transform tool response json content correctly',
inputMessages: [
{
role: 'tool',
Expand All @@ -289,7 +289,7 @@ describe('toOpenAiMessages', () => {
],
},
{
should: 'should transform text content correctly',
should: 'transform text content correctly',
inputMessages: [
{ role: 'user', content: [{ text: 'hi' }] },
{ role: 'model', content: [{ text: 'how can I help you?' }] },
Expand All @@ -302,7 +302,7 @@ describe('toOpenAiMessages', () => {
],
},
{
should: 'should transform multi-modal (text + media) content correctly',
should: 'transform multi-modal (text + media) content correctly',
inputMessages: [
{
role: 'user',
Expand Down Expand Up @@ -334,7 +334,7 @@ describe('toOpenAiMessages', () => {
],
},
{
should: 'should transform system messages correctly',
should: 'transform system messages correctly',
inputMessages: [
{ role: 'system', content: [{ text: 'system message' }] },
],
Expand Down Expand Up @@ -423,7 +423,7 @@ describe('fromOpenAiChoice', () => {
expectedOutput: GenerateResponseData;
}[] = [
{
should: 'should work with text',
should: 'work with text',
choice: {
index: 0,
message: {
Expand All @@ -443,7 +443,7 @@ describe('fromOpenAiChoice', () => {
},
},
{
should: 'should work with json',
should: 'work with json',
choice: {
index: 0,
message: {
Expand All @@ -464,7 +464,7 @@ describe('fromOpenAiChoice', () => {
},
},
{
should: 'should work with tools',
should: 'work with tools',
choice: {
index: 0,
message: {
Expand Down Expand Up @@ -502,7 +502,7 @@ describe('fromOpenAiChoice', () => {
},
},
{
should: 'should work with reasoning_content',
should: 'work with reasoning_content',
choice: {
index: 0,
message: {
Expand All @@ -523,7 +523,7 @@ describe('fromOpenAiChoice', () => {
},
},
{
should: 'should work with both reasoning_content and content',
should: 'work with both reasoning_content and content',
choice: {
index: 0,
message: {
Expand All @@ -543,6 +543,27 @@ describe('fromOpenAiChoice', () => {
},
},
},
{
should: 'not ignore content when tool_calls is an empty array',
choice: {
index: 0,
message: {
role: 'assistant',
content: 'I have the answer.',
tool_calls: [],
refusal: null,
},
finish_reason: 'stop',
logprobs: null,
},
expectedOutput: {
finishReason: 'stop',
message: {
role: 'model',
content: [{ text: 'I have the answer.' }],
},
},
},
];

for (const test of testCases) {
Expand All @@ -561,7 +582,7 @@ describe('fromOpenAiChunkChoice', () => {
expectedOutput: GenerateResponseData;
}[] = [
{
should: 'should work with text',
should: 'work with text',
chunkChoice: {
index: 0,
delta: {
Expand All @@ -579,7 +600,7 @@ describe('fromOpenAiChunkChoice', () => {
},
},
{
should: 'should work with json',
should: 'work with json',
chunkChoice: {
index: 0,
delta: {
Expand All @@ -599,7 +620,7 @@ describe('fromOpenAiChunkChoice', () => {
},
},
{
should: 'should work with tools',
should: 'work with tools',
chunkChoice: {
index: 0,
delta: {
Expand Down Expand Up @@ -635,7 +656,7 @@ describe('fromOpenAiChunkChoice', () => {
},
},
{
should: 'should work with reasoning_content',
should: 'work with reasoning_content',
chunkChoice: {
index: 0,
delta: {
Expand All @@ -653,7 +674,7 @@ describe('fromOpenAiChunkChoice', () => {
},
},
{
should: 'should work with both reasoning_content and content',
should: 'work with both reasoning_content and content',
chunkChoice: {
index: 0,
delta: {
Expand Down
Loading