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
5 changes: 5 additions & 0 deletions .changeset/eleven-terms-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'token.js': minor
---

Upgrade the openai client library to 4.91.1
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"cohere-ai": "7.10.6",
"mime-types": "^2.1.35",
"nanoid": "^5.0.7",
"openai": "4.52.2"
"openai": "4.91.1"
},
"devDependencies": {
"@babel/eslint-parser": "^7.18.2",
Expand Down
17 changes: 12 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/handlers/ai21.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '../userTypes/index.js'
import { BaseHandler } from './base.js'
import { InputError } from './types.js'
import { getTimestamp } from './utils.js'
import { convertMessageContentToString, getTimestamp } from './utils.js'

type AI21ChatCompletionParams = {
model: string
Expand Down Expand Up @@ -73,7 +73,7 @@ const convertMessages = (
if (i === 0 && message.role === 'system') {
output.push({
role: 'system',
content: message.content,
content: convertMessageContentToString(message.content),
})
} else if (
message.role === 'user' ||
Expand Down Expand Up @@ -268,6 +268,10 @@ export class AI21Handler extends BaseHandler<AI21Model> {
const convertedChoices = data.choices.map((choice) => {
return {
...choice,
message: {
...choice.message,
refusal: null,
},
logprobs: null,
}
})
Expand Down
9 changes: 6 additions & 3 deletions src/handlers/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { BaseHandler } from './base.js'
import { InputError, InvariantError } from './types.js'
import {
consoleWarn,
convertMessageContentToString,
fetchThenParseImage,
getTimestamp,
isEmptyObject,
Expand Down Expand Up @@ -247,12 +248,14 @@ const toChatCompletionChoiceMessage = (
const messageContent = content.every(isToolUseBlock) ? null : ''
return {
role,
refusal: null,
content: messageContent,
tool_calls: toolCalls,
}
} else {
return {
role,
refusal: null,
content: textBlocks.map((textBlock) => textBlock.text).join('\n'),
tool_calls: toolCalls,
}
Expand Down Expand Up @@ -361,7 +364,7 @@ export const convertMessages = async (
// unchanged.
let systemMessage: string | undefined
if (clonedMessages.length > 0 && clonedMessages[0].role === 'system') {
systemMessage = clonedMessages[0].content
systemMessage = convertMessageContentToString(clonedMessages[0].content)
clonedMessages.shift()
}

Expand Down Expand Up @@ -448,7 +451,7 @@ export const convertMessages = async (
return {
type: 'text',
text,
}
} as TextBlockParam
} else {
const parsedImage = await fetchThenParseImage(e.image_url.url)
return {
Expand All @@ -458,7 +461,7 @@ export const convertMessages = async (
media_type: parsedImage.mimeType,
type: 'base64',
},
}
} as ImageBlockParam
}
})
)
Expand Down
38 changes: 24 additions & 14 deletions src/handlers/bedrock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
ConverseStreamCommandOutput,
ImageFormat,
SystemContentBlock,
Tool,
ToolChoice,
ToolConfiguration,
ToolSpecification,
} from '@aws-sdk/client-bedrock-runtime'
import { ChatCompletionMessageToolCall } from 'openai/resources/index'

Expand All @@ -29,6 +32,7 @@ import { BaseHandler } from './base.js'
import { InputError, InvariantError, MIMEType } from './types.js'
import {
consoleWarn,
convertMessageContentToString,
fetchThenParseImage,
getTimestamp,
normalizeTemperature,
Expand Down Expand Up @@ -84,6 +88,7 @@ const toChatCompletionChoiceMessage = (
): CompletionResponse['choices'][0]['message'] => {
if (output?.message?.content === undefined) {
return {
refusal: null,
content: '',
role: 'assistant',
}
Expand Down Expand Up @@ -146,13 +151,15 @@ const toChatCompletionChoiceMessage = (
: ''
return {
role,
refusal: null,
content: messageContent,
tool_calls: toolCalls,
}
} else {
const content = textBlocks.map((textBlock) => textBlock.text).join('\n')
return {
role,
refusal: null,
content,
tool_calls: toolCalls,
}
Expand Down Expand Up @@ -187,7 +194,10 @@ export const convertMessages = async (
const systemMessages: Array<SystemContentBlock> = []
if (supportsSystemMessages(model)) {
while (clonedMessages.length > 0 && clonedMessages[0].role === 'system') {
systemMessages.push({ text: clonedMessages[0].content })
const messageContent = convertMessageContentToString(
clonedMessages[0].content
)
systemMessages.push({ text: messageContent })
clonedMessages.shift()
}
}
Expand Down Expand Up @@ -247,7 +257,7 @@ export const convertMessages = async (
toolUseId: message.tool_call_id,
content: [
{
text: message.content,
text: convertMessageContentToString(message.content),
},
],
},
Expand Down Expand Up @@ -288,7 +298,7 @@ export const convertMessages = async (
const text = makeTextContent(message.role, e.text)
return {
text,
}
} as ContentBlock.TextMember
} else {
const parsedImage = await fetchThenParseImage(e.image_url.url)
return {
Expand All @@ -298,7 +308,7 @@ export const convertMessages = async (
bytes: Buffer.from(parsedImage.content, 'base64'),
},
},
}
} as ContentBlock.ImageMember
}
})
)
Expand Down Expand Up @@ -354,27 +364,24 @@ export const convertToolParams = (
return undefined
}

const convertedTools =
const convertedTools: (Tool | undefined)[] =
tools.length > 0
? tools.map((tool) => {
const inputSchema = tool.function.parameters
? {
const inputSchema: ToolSpecification['inputSchema'] | undefined = tool
.function.parameters
? ({
// Bedrock and OpenAI's function parameter types are incompatible even though they both
// adhere to the JSON schema, so we set the type to `any` to prevent a TypeScript error.
json: tool.function.parameters as any,
// TypeScript throws a type error if we don't define this field:
$unknown: undefined,
}
} satisfies ToolSpecification['inputSchema'])
: undefined
return {
// TypeScript throws a type error if we don't define this field:
$unknown: undefined,
toolSpec: {
name: tool.function.name,
description: tool.function.description,
inputSchema,
},
}
} satisfies Tool
})
: undefined

Expand All @@ -387,7 +394,10 @@ export const convertToolParams = (
convertedToolChoice = { tool: { name: toolChoice.function.name } }
}

return { toolChoice: convertedToolChoice, tools: convertedTools }
return {
toolChoice: convertedToolChoice,
tools: convertedTools,
} satisfies ToolConfiguration
}

const convertStopReason = (
Expand Down
15 changes: 12 additions & 3 deletions src/handlers/cohere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ import {
} from '../userTypes/index.js'
import { BaseHandler } from './base.js'
import { InputError, InvariantError, MessageRole } from './types.js'
import { consoleWarn, getTimestamp } from './utils.js'
import {
consoleWarn,
convertMessageContentToString,
getTimestamp,
} from './utils.js'

type CohereMessageRole = 'CHATBOT' | 'SYSTEM' | 'USER' | 'TOOL'

Expand All @@ -41,6 +45,8 @@ const convertRole = (role: MessageRole): CohereMessageRole => {
return 'TOOL'
} else if (role === 'user') {
return 'USER'
} else if (role === 'developer') {
return 'SYSTEM'
} else {
throw new InputError(`Unknown role: ${role}`)
}
Expand Down Expand Up @@ -259,12 +265,13 @@ const toToolResult = (
)
}

const tollCallContentStr = convertMessageContentToString(toolMessage.content)
const toolResult: ToolResult = {
call: {
name: toolCall.function.name,
parameters: JSON.parse(toolCall.function.arguments),
},
outputs: [JSON.parse(toolMessage.content)],
outputs: [JSON.parse(tollCallContentStr)],
}
return toolResult
}
Expand Down Expand Up @@ -318,9 +325,10 @@ export const convertMessages = (
})
}
} else if (message.role === 'assistant') {
const messageContentStr = convertMessageContentToString(message.content)
chatHistory.push({
role: convertRole(message.role),
message: message.content ?? '',
message: messageContentStr,
toolCalls: message.tool_calls?.map((toolCall) => {
return {
name: toolCall.function.name,
Expand Down Expand Up @@ -567,6 +575,7 @@ export class CohereHandler extends BaseHandler<CohereModel> {
logprobs: null,
message: {
role: 'assistant',
refusal: null, // openai requires this field, fill in if Cohere ever supports
content: response.text,
tool_calls: toolCalls,
},
Expand Down
Loading