Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
shaper committed Feb 1, 2025
1 parent ad9719a commit e0a6658
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 76 deletions.
10 changes: 1 addition & 9 deletions examples/ai-core/src/generate-text/amazon-bedrock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@ import { bedrock } from '@ai-sdk/amazon-bedrock';
import { generateText } from 'ai';
import 'dotenv/config';

// - nova lite | amazon.nova-lite-v1:0
// - claude 3.5 sonnet | arn:aws:bedrock:us-east-2:474668406012:inference-profile/us.anthropic.claude-3-5-sonnet-20240620-v1:0
// - claude 3.5 sonnet v2 | arn:aws:bedrock:us-east-2:474668406012:inference-profile/us.anthropic.claude-3-5-sonnet-20241022-v2:0
// - llama 3.2 11B vision instruct | arn:aws:bedrock:us-east-2:474668406012:inference-profile/us.meta.llama3-2-11b-instruct-v1:0

async function main() {
const result = await generateText({
model: bedrock(
'arn:aws:bedrock:us-east-2:474668406012:inference-profile/us.amazon.nova-lite-v1:0',
// 'arn:aws:bedrock:us-east-2:474668406012:inference-profile/us.anthropic.claude-3-5-sonnet-20240620-v1:0',
'arn:aws:bedrock:us-east-2:474668406012:inference-profile/us.anthropic.claude-3-5-sonnet-20240620-v1:0',
),
maxTokens: 1000,
temperature: 0.5,
prompt: 'Give me an overview of the New Zealand Fiordland National Park.',
});

Expand Down
2 changes: 0 additions & 2 deletions examples/ai-core/src/stream-text/amazon-bedrock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ async function main() {
model: bedrock(
'arn:aws:bedrock:us-east-2:474668406012:inference-profile/us.anthropic.claude-3-5-sonnet-20240620-v1:0',
),
maxTokens: 1000,
temperature: 0.5,
prompt: 'Give me an overview of the New Zealand Fiordland National Park.',
});

Expand Down
52 changes: 21 additions & 31 deletions packages/amazon-bedrock/src/bedrock-api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface BedrockConverseInput {
system?: Array<{ text: string }>;
messages: Array<{
role: string;
content: Array<ContentBlock>;
content: Array<BedrockContentBlock>;
}>;
toolConfig?: BedrockToolConfiguration;
inferenceConfig?: {
Expand All @@ -23,15 +23,15 @@ export interface BedrockConverseInput {
guardrailConfig?: any;
}

export interface GuardrailConfiguration {
export interface BedrockGuardrailConfiguration {
guardrails?: Array<{
name: string;
description?: string;
parameters?: Record<string, any>;
}>;
}

export type GuardrailStreamConfiguration = GuardrailConfiguration;
export type BedrockGuardrailStreamConfiguration = BedrockGuardrailConfiguration;

export interface BedrockToolInputSchema {
json: Record<string, any>;
Expand All @@ -54,7 +54,7 @@ export interface BedrockToolConfiguration {
| undefined;
}

export type StopReason =
export type BedrockStopReason =
| 'stop'
| 'stop_sequence'
| 'end_turn'
Expand All @@ -66,65 +66,55 @@ export type StopReason =
| 'tool-calls'
| 'tool_use';

export type ImageFormat = 'jpeg' | 'png' | 'gif';
export type DocumentFormat = 'pdf' | 'txt' | 'md';
export type BedrockImageFormat = 'jpeg' | 'png' | 'gif';
export type BedrockDocumentFormat = 'pdf' | 'txt' | 'md';

export interface DocumentBlock {
export interface BedrockDocumentBlock {
document: {
format: DocumentFormat;
format: BedrockDocumentFormat;
name: string;
source: {
bytes: string;
};
};
}

export interface GuardrailConverseContentBlock {
export interface BedrockGuardrailConverseContentBlock {
guardContent: any;
}

export interface ImageBlock {
export interface BedrockImageBlock {
image: {
format: ImageFormat;
format: BedrockImageFormat;
source: {
bytes: string;
};
};
}

export interface ToolResultBlock {
export interface BedrockToolResultBlock {
toolResult: {
toolUseId: string;
content: Array<{ text: string }>;
};
}

export interface ToolUseBlock {
export interface BedrockToolUseBlock {
toolUse: {
toolUseId: string;
name: string;
input: Record<string, any>;
};
}

export interface VideoBlock {
video: {
format: string;
source: {
bytes: Uint8Array;
};
};
}

export interface TextBlock {
export interface BedrockTextBlock {
text: string;
}

export type ContentBlock =
| DocumentBlock
| GuardrailConverseContentBlock
| ImageBlock
| TextBlock
| ToolResultBlock
| ToolUseBlock
| VideoBlock;
export type BedrockContentBlock =
| BedrockDocumentBlock
| BedrockGuardrailConverseContentBlock
| BedrockImageBlock
| BedrockTextBlock
| BedrockToolResultBlock
| BedrockToolUseBlock;
14 changes: 8 additions & 6 deletions packages/amazon-bedrock/src/bedrock-chat-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
} from '@ai-sdk/provider-utils';
import {
BedrockConverseInput,
GuardrailConfiguration,
GuardrailStreamConfiguration,
BedrockGuardrailConfiguration,
BedrockGuardrailStreamConfiguration,
BedrockToolInputSchema,
StopReason,
BedrockStopReason,
BedrockHeadersFunction,
} from './bedrock-api-types';
import {
Expand Down Expand Up @@ -135,8 +135,8 @@ export class BedrockChatLanguageModel implements LanguageModelV1 {
}),
messages,
guardrailConfig: providerMetadata?.bedrock?.guardrailConfig as
| GuardrailConfiguration
| GuardrailStreamConfiguration
| BedrockGuardrailConfiguration
| BedrockGuardrailStreamConfiguration
| undefined,
};

Expand Down Expand Up @@ -244,7 +244,9 @@ export class BedrockChatLanguageModel implements LanguageModelV1 {
toolName: part.toolUse?.name ?? `tool-${this.config.generateId()}`,
args: JSON.stringify(part.toolUse?.input ?? ''),
})),
finishReason: mapBedrockFinishReason(response.stopReason as StopReason),
finishReason: mapBedrockFinishReason(
response.stopReason as BedrockStopReason,
),
usage: {
promptTokens: response.usage?.inputTokens ?? Number.NaN,
completionTokens: response.usage?.outputTokens ?? Number.NaN,
Expand Down
6 changes: 3 additions & 3 deletions packages/amazon-bedrock/src/bedrock-chat-prompt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContentBlock } from './bedrock-api-types';
import { BedrockContentBlock } from './bedrock-api-types';

export type BedrockMessagesPrompt = {
system?: string;
Expand All @@ -11,10 +11,10 @@ export type BedrockMessage = BedrockUserMessage | BedrockAssistantMessage;

export interface BedrockUserMessage {
role: 'user';
content: Array<ContentBlock>;
content: Array<BedrockContentBlock>;
}

export interface BedrockAssistantMessage {
role: 'assistant';
content: Array<ContentBlock>;
content: Array<BedrockContentBlock>;
}
3 changes: 0 additions & 3 deletions packages/amazon-bedrock/src/bedrock-sigv4-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ export class AwsSigV4Signer {
async signRequest(
request: SigningRequest,
): Promise<Record<string, string | undefined>> {
if (process.env.AI_SDK_DEBUG) {
console.log('signRequest: request=', JSON.stringify(request, null, 2));
}
const signer = new AwsV4Signer({
url: request.url,
method: request.method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
UnsupportedFunctionalityError,
} from '@ai-sdk/provider';
import { createIdGenerator } from '@ai-sdk/provider-utils';
import { DocumentFormat, ImageFormat } from './bedrock-api-types';
import { BedrockDocumentFormat, BedrockImageFormat } from './bedrock-api-types';
import {
BedrockAssistantMessage,
BedrockMessagesPrompt,
Expand Down Expand Up @@ -67,7 +67,9 @@ export function convertToBedrockChatMessages(

bedrockContent.push({
image: {
format: part.mimeType?.split('/')?.[1] as ImageFormat,
format: part.mimeType?.split(
'/',
)?.[1] as BedrockImageFormat,
source: {
bytes: Buffer.from(
part.image ?? (part.image as Uint8Array),
Expand All @@ -90,7 +92,7 @@ export function convertToBedrockChatMessages(
document: {
format: part.mimeType?.split(
'/',
)?.[1] as DocumentFormat,
)?.[1] as BedrockDocumentFormat,
name: generateFileId(),
source: {
bytes: part.data,
Expand Down
4 changes: 2 additions & 2 deletions packages/amazon-bedrock/src/map-bedrock-finish-reason.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { LanguageModelV1FinishReason } from '@ai-sdk/provider';
import { StopReason } from './bedrock-api-types';
import { BedrockStopReason } from './bedrock-api-types';

export function mapBedrockFinishReason(
finishReason?: StopReason,
finishReason?: BedrockStopReason,
): LanguageModelV1FinishReason {
switch (finishReason) {
case 'stop_sequence':
Expand Down
8 changes: 0 additions & 8 deletions packages/provider-utils/src/post-to-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ export const postToApi = async <T>({
abortSignal?: AbortSignal;
fetch?: FetchFunction;
}) => {
if (process.env.AI_SDK_DEBUG) {
console.log('postToApi: url=', url);
console.log(
'postToApi: headers=',
JSON.stringify(removeUndefinedEntries(headers), null, 2),
);
console.log('postToApi: body=', body.content);
}
try {
const response = await fetch(url, {
method: 'POST',
Expand Down
1 change: 1 addition & 0 deletions packages/provider-utils/src/response-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export const createJsonResponseHandler =
<T>(responseSchema: ZodSchema<T>): ResponseHandler<T> =>
async ({ response, url, requestBodyValues }) => {
const responseBody = await response.text();

const parsedResult = safeParseJSON({
text: responseBody,
schema: responseSchema,
Expand Down
38 changes: 29 additions & 9 deletions turbo.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{
"$schema": "https://turbo.build/schema.json",
"globalEnv": ["CI", "PORT"],
"globalEnv": [
"CI",
"PORT"
],
"tasks": {
"build": {
"dependsOn": ["^build"],
"dependsOn": [
"^build"
],
"env": [
"AI_SDK_DEBUG",
"ANTHROPIC_API_KEY",
"ASSISTANT_ID",
"AWS_REGION",
Expand Down Expand Up @@ -52,27 +56,43 @@
]
},
"lint": {
"dependsOn": ["^lint"]
"dependsOn": [
"^lint"
]
},
"type-check": {
"dependsOn": ["^build", "build"]
"dependsOn": [
"^build",
"build"
]
},
"test": {
"dependsOn": ["^build", "build"]
"dependsOn": [
"^build",
"build"
]
},
"publint": {
"dependsOn": ["^build", "build"]
"dependsOn": [
"^build",
"build"
]
},
"clean": {
"dependsOn": ["^clean"]
"dependsOn": [
"^clean"
]
},
"dev": {
"cache": false,
"persistent": true
},
"prettier-check": {},
"integration-test": {
"dependsOn": ["^build", "build"]
"dependsOn": [
"^build",
"build"
]
}
}
}

0 comments on commit e0a6658

Please sign in to comment.