Skip to content
Closed
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
1 change: 1 addition & 0 deletions integrations/ai-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CHANGELOG
26 changes: 26 additions & 0 deletions integrations/ai-sdk/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "ai-provider",
"version": "0.0.1",
"description": "Humanloop provider for the AI SDK by Vercel",
"main": "index.js",
"scripts": {
"test": "jest"
},
"repository": {
"type": "git",
"url": "https://github.com/humanloop/humanloop-node/integrations/ai-sdk"
},
"keywords": [
"ai",
"ai-sdk",
"vercel-ai-sdk"
],
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@ai-sdk/provider": "^1.0.11",
"@ai-sdk/provider-utils": "^2.1.13",
"tsup": "^8.4.0",
"zod": "^3.24.2"
}
}
127 changes: 127 additions & 0 deletions integrations/ai-sdk/src/convert-to-humanloop-chat-messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import {
LanguageModelV1Prompt,
UnsupportedFunctionalityError,
} from '@ai-sdk/provider';
import { convertUint8ArrayToBase64 } from '@ai-sdk/provider-utils';
import { HumanloopChatPrompt } from './humanloop-api-types';

export function convertToHumanloopChatMessages(
prompt: LanguageModelV1Prompt,
): HumanloopChatPrompt {
const messages: HumanloopChatPrompt = [];

for (const { role, content } of prompt) {
// Skip empty messages, i.e. when prompt == ""
if (content === "") {
continue;
}

switch (role) {
case 'system': {
messages.push({ role: 'system', content });
break;
}

case 'user': {
if (content.length === 1 && content[0].type === 'text') {
messages.push({ role: 'user', content: content[0].text });
break;
}

messages.push({
role: 'user',
content: content.map(part => {
switch (part.type) {
case 'text': {
return { type: 'text', text: part.text };
}
case 'image': {
return {
type: 'image_url',
imageUrl: {
url:
part.image instanceof URL
? part.image.toString()
: `data:${
part.mimeType ?? 'image/jpeg'
};base64,${convertUint8ArrayToBase64(part.image)}`,
},
};
}
case 'file': {
throw new UnsupportedFunctionalityError({
functionality: 'File content parts in user messages',
});
}
}
}),
});

break;
}

case 'assistant': {
let text = '';
const toolCalls: Array<{
id: string;
type: 'function';
function: { name: string; arguments: string };
}> = [];

for (const part of content) {
switch (part.type) {
case 'text': {
text += part.text;
break;
}
case 'redacted-reasoning':
case 'reasoning': {
break; // ignored
}
case 'tool-call': {
toolCalls.push({
id: part.toolCallId,
type: 'function',
function: {
name: part.toolName,
arguments: JSON.stringify(part.args),
},
});
break;
}
default: {
const _exhaustiveCheck: never = part;
throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
}
}
}

messages.push({
role: 'assistant',
content: text,
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
});

break;
}

case 'tool': {
for (const toolResponse of content) {
messages.push({
role: 'tool',
toolCallId: toolResponse.toolCallId,
content: JSON.stringify(toolResponse.result),
});
}
break;
}

default: {
const _exhaustiveCheck: never = role;
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
}
}
}

return messages;
}
15 changes: 15 additions & 0 deletions integrations/ai-sdk/src/get-response-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function getResponseMetadata({
id,
model,
createdAt,
}: {
id?: string | undefined | null;
createdAt?: Date | undefined | null;
model?: string | undefined | null;
}) {
return {
id: id ?? undefined,
modelId: model ?? undefined,
timestamp: createdAt ?? undefined,
};
}
16 changes: 16 additions & 0 deletions integrations/ai-sdk/src/humanloop-api-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
ChatMessage,
PromptsCallRequest,
PromptsCallStreamRequest,
ProviderApiKeys
} from "../../../src/api";

export type HumanloopProviderApiKeys = ProviderApiKeys
export type HumanloopChatPrompt = Array<ChatMessage>;
export type HumanloopGenerateArgs = PromptsCallRequest | PromptsCallStreamRequest;
export type HumanloopTools =
| PromptsCallRequest["prompt"]["tools"]
| PromptsCallStreamRequest["prompt"]["tools"];
export type HumanloopToolChoice =
| PromptsCallRequest["toolChoice"]
| PromptsCallStreamRequest["toolChoice"];
Loading