Skip to content
Open
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
4 changes: 2 additions & 2 deletions bindings/lingua-wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "WASM bindings for Lingua",
"scripts": {
"build": "pnpm run build:nodejs && pnpm run build:web",
"build:nodejs": "cd ../../crates/lingua && wasm-pack build --target nodejs --release --out-dir ../../bindings/lingua-wasm/nodejs --no-pack",
"build:web": "cd ../../crates/lingua && wasm-pack build --target web --release --out-dir ../../bindings/lingua-wasm/web --no-pack"
"build:nodejs": "cd ../../crates/lingua && wasm-pack build --target nodejs --release --out-dir ../../bindings/lingua-wasm/nodejs --no-pack --features bedrock",
"build:web": "cd ../../crates/lingua && wasm-pack build --target web --release --out-dir ../../bindings/lingua-wasm/web --no-pack --features bedrock"
},
"files": [
"nodejs",
Expand Down
2 changes: 1 addition & 1 deletion crates/lingua/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ assert-json-diff.workspace = true
url.workspace = true
urlencoding.workspace = true
base64.workspace = true
aws-sdk-bedrockruntime = { workspace = true, optional = true }
pyo3 = { workspace = true, optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand All @@ -48,6 +47,7 @@ serde-wasm-bindgen.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
reqwest.workspace = true
aws-sdk-bedrockruntime = { workspace = true, optional = true }

[dev-dependencies]
tokio-test.workspace = true
Expand Down
1 change: 1 addition & 0 deletions crates/lingua/src/providers/bedrock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod response;
pub use adapter::BedrockAdapter;

// Re-export commonly used AWS SDK types (note: these don't have Serde by default)
#[cfg(not(target_arch = "wasm32"))]
pub use aws_sdk_bedrockruntime::types::{
ContentBlock, ConversationRole, Message, SystemContentBlock,
};
Expand Down
2,508 changes: 1,889 additions & 619 deletions payloads/scripts/transforms/__snapshots__/transforms.test.ts.snap

Large diffs are not rendered by default.

55 changes: 48 additions & 7 deletions payloads/scripts/transforms/capture-transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { existsSync, mkdirSync, writeFileSync } from "fs";
import { dirname } from "path";
import Anthropic from "@anthropic-ai/sdk";
import OpenAI from "openai";
import { allTestCases, getCaseForProvider, GOOGLE_MODEL } from "../../cases";
import {
allTestCases,
getCaseForProvider,
GOOGLE_MODEL,
type BedrockConverseRequest,
} from "../../cases";
import { executeBedrock } from "../providers/bedrock";
import {
TRANSFORM_PAIRS,
STREAMING_PAIRS,
Expand Down Expand Up @@ -36,6 +42,16 @@ async function runConcurrently(tasks: (() => Promise<void>)[]): Promise<void> {
let _anthropic: Anthropic | undefined;
let _openai: OpenAI | undefined;

function isBedrockConverseRequest(
value: unknown
): value is BedrockConverseRequest {
if (typeof value !== "object" || value === null) return false;
if (!("modelId" in value)) return false;
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- type guard needs safe property access on unknown object
const modelId = (value as { modelId?: unknown }).modelId;
return typeof modelId === "string" && modelId.length > 0;
}

function getAnthropic(): Anthropic {
if (!_anthropic) _anthropic = new Anthropic();
return _anthropic;
Expand Down Expand Up @@ -97,6 +113,20 @@ async function callProvider(
);
case "google":
return callGoogleProvider(request);
case "bedrock": {
if (!isBedrockConverseRequest(request)) {
throw new Error(
"Bedrock capture failed: request is missing required field 'modelId'"
);
}
const result = await executeBedrock("transform-capture", request, {
stream: false,
});
if (result.response) {
return result.response;
}
throw new Error(result.error ?? "Bedrock capture failed");
}
}
}
/* eslint-enable @typescript-eslint/consistent-type-assertions */
Expand Down Expand Up @@ -148,12 +178,23 @@ export async function captureTransforms(
caseName,
p.target
);
request.model =
targetCase &&
typeof targetCase === "object" &&
"model" in targetCase
? targetCase.model
: TARGET_MODELS[p.target];
if (p.target === "bedrock") {
request.modelId =
targetCase &&
typeof targetCase === "object" &&
targetCase !== null &&
"modelId" in targetCase
? targetCase.modelId
: TARGET_MODELS[p.target];
} else {
request.model =
targetCase &&
typeof targetCase === "object" &&
targetCase !== null &&
"model" in targetCase
? targetCase.model
: TARGET_MODELS[p.target];
}

const response = await callProvider(p.target, request);

Expand Down
35 changes: 33 additions & 2 deletions payloads/scripts/transforms/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
transform_response,
validate_anthropic_request,
validate_anthropic_response,
validate_bedrock_request,
validate_bedrock_response,
validate_chat_completions_request,
validate_chat_completions_response,
validate_google_request,
Expand All @@ -15,6 +17,7 @@ import {
import { allTestCases, getCaseNames, getCaseForProvider } from "../../cases";
import {
ANTHROPIC_MODEL,
BEDROCK_MODEL,
GOOGLE_MODEL,
OPENAI_CHAT_COMPLETIONS_MODEL,
OPENAI_RESPONSES_MODEL,
Expand All @@ -24,8 +27,14 @@ export type SourceFormat =
| "chat-completions"
| "responses"
| "anthropic"
| "google";
export type WasmFormat = "OpenAI" | "Responses" | "Anthropic" | "Google";
| "google"
| "bedrock";
export type WasmFormat =
| "OpenAI"
| "Responses"
| "Anthropic"
| "Google"
| "bedrock";

export interface TransformPair {
source: SourceFormat;
Expand Down Expand Up @@ -65,6 +74,24 @@ export const TRANSFORM_PAIRS: TransformPair[] = [
wasmSource: "OpenAI",
wasmTarget: "Google",
},
{
source: "chat-completions",
target: "bedrock",
wasmSource: "OpenAI",
wasmTarget: "bedrock",
},
{
source: "responses",
target: "bedrock",
wasmSource: "Responses",
wasmTarget: "bedrock",
},
{
source: "anthropic",
target: "bedrock",
wasmSource: "Anthropic",
wasmTarget: "bedrock",
},
{
source: "anthropic",
target: "google",
Expand Down Expand Up @@ -103,13 +130,15 @@ const REQUEST_VALIDATORS: Record<SourceFormat, (json: string) => unknown> = {
responses: validate_responses_request,
anthropic: validate_anthropic_request,
google: validate_google_request,
bedrock: validate_bedrock_request,
};

const RESPONSE_VALIDATORS: Record<SourceFormat, (json: string) => unknown> = {
"chat-completions": validate_chat_completions_response,
responses: validate_responses_response,
anthropic: validate_anthropic_response,
google: validate_google_response,
bedrock: validate_bedrock_response,
};

interface TransformResultData {
Expand Down Expand Up @@ -164,6 +193,7 @@ const WASM_TO_SOURCE: Record<WasmFormat, SourceFormat> = {
Responses: "responses",
Anthropic: "anthropic",
Google: "google",
bedrock: "bedrock",
};

// Transform and validate response
Expand Down Expand Up @@ -217,6 +247,7 @@ export const TARGET_MODELS: Record<SourceFormat, string> = {
"chat-completions": OPENAI_CHAT_COMPLETIONS_MODEL,
responses: OPENAI_RESPONSES_MODEL,
google: GOOGLE_MODEL,
bedrock: BEDROCK_MODEL,
};

export function getTransformableCases(
Expand Down

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

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

29 changes: 29 additions & 0 deletions payloads/transforms/anthropic_to_bedrock/reasoningRequest.json

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

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

29 changes: 29 additions & 0 deletions payloads/transforms/anthropic_to_bedrock/reasoningWithOutput.json

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

29 changes: 29 additions & 0 deletions payloads/transforms/anthropic_to_bedrock/simpleRequest.json

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

Loading
Loading