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
6 changes: 6 additions & 0 deletions .changeset/tidy-drinks-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@lingo.dev/_compiler": patch
"lingo.dev": patch
---

Add AI/ML API provider integration via the AI SDK.
12 changes: 12 additions & 0 deletions packages/cli/src/cli/localizer/explicit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ export default function createExplicitLocalizer(
apiKeyName: "GOOGLE_API_KEY",
baseUrl: provider.baseUrl,
});
case "aimlapi":
return createAiSdkLocalizer({
factory: (params) =>
createOpenAI({
apiKey: params.apiKey,
baseURL: params.baseUrl,
}).languageModel(provider.model),
id: provider.id,
prompt: provider.prompt,
apiKeyName: "AIMLAPI_API_KEY",
baseUrl: provider.baseUrl ?? "https://api.aimlapi.com/v1",
});
case "openrouter":
return createAiSdkLocalizer({
factory: (params) =>
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/src/cli/processor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ function getPureModelProvider(provider: I18nConfig["provider"]) {
apiKey: process.env.GOOGLE_API_KEY,
})(provider.model);
}
case "aimlapi": {
if (!process.env.AIMLAPI_API_KEY) {
throw new Error(
createMissingKeyErrorMessage("AI/ML API", "AIMLAPI_API_KEY"),
);
}
return createOpenAI({
apiKey: process.env.AIMLAPI_API_KEY,
baseURL: provider.baseUrl ?? "https://api.aimlapi.com/v1",
})(provider.model);
}
case "openrouter": {
if (!process.env.OPENROUTER_API_KEY) {
throw new Error(
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/cli/utils/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function getSettings(explicitApiKey: string | undefined): CliSettings {
googleApiKey: env.GOOGLE_API_KEY || systemFile.llm?.googleApiKey,
openrouterApiKey:
env.OPENROUTER_API_KEY || systemFile.llm?.openrouterApiKey,
aimlApiKey: env.AIMLAPI_API_KEY || systemFile.llm?.aimlApiKey,
mistralApiKey: env.MISTRAL_API_KEY || systemFile.llm?.mistralApiKey,
},
};
Expand Down Expand Up @@ -74,6 +75,7 @@ const SettingsSchema = Z.object({
groqApiKey: Z.string().optional(),
googleApiKey: Z.string().optional(),
openrouterApiKey: Z.string().optional(),
aimlApiKey: Z.string().optional(),
mistralApiKey: Z.string().optional(),
}),
});
Expand Down Expand Up @@ -105,6 +107,7 @@ function _loadEnv() {
GROQ_API_KEY: Z.string().optional(),
GOOGLE_API_KEY: Z.string().optional(),
OPENROUTER_API_KEY: Z.string().optional(),
AIMLAPI_API_KEY: Z.string().optional(),
MISTRAL_API_KEY: Z.string().optional(),
})
.passthrough()
Expand All @@ -130,6 +133,7 @@ function _loadSystemFile() {
groqApiKey: Z.string().optional(),
googleApiKey: Z.string().optional(),
openrouterApiKey: Z.string().optional(),
aimlApiKey: Z.string().optional(),
mistralApiKey: Z.string().optional(),
}).optional(),
})
Expand Down Expand Up @@ -207,6 +211,12 @@ function _envVarsInfo() {
`ℹ️ Using OPENROUTER_API_KEY env var instead of key from user config`,
);
}
if (env.AIMLAPI_API_KEY && systemFile.llm?.aimlApiKey) {
console.info(
"\x1b[36m%s\x1b[0m",
`ℹ️ Using AIMLAPI_API_KEY env var instead of key from user config`,
);
}
if (env.MISTRAL_API_KEY && systemFile.llm?.mistralApiKey) {
console.info(
"\x1b[36m%s\x1b[0m",
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/types/ai-sdk-openai.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module "@ai-sdk/openai" {
export function createOpenAI(options?: any): any;
}
1 change: 1 addition & 0 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@ai-sdk/google": "^1.2.19",
"@ai-sdk/groq": "^1.2.3",
"@ai-sdk/mistral": "^1.2.8",
"@ai-sdk/openai": "^1.3.22",
"@babel/generator": "^7.26.5",
"@babel/parser": "^7.26.7",
"@babel/traverse": "^7.27.4",
Expand Down
29 changes: 28 additions & 1 deletion packages/compiler/src/lib/lcp/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
getGoogleKeyFromEnv,
getOpenRouterKey,
getOpenRouterKeyFromEnv,
getAimlApiKey,
getAimlApiKeyFromEnv,
getMistralKey,
getMistralKeyFromEnv,
getLingoDotDevKeyFromEnv,
Expand Down Expand Up @@ -333,6 +335,31 @@ export class LCPAPI {
);
return createGoogleGenerativeAI({ apiKey: googleKey })(modelId);
}
case "aimlapi": {
// Specific check for CI/CD or Docker missing AI/ML API key
if (isRunningInCIOrDocker()) {
const aimlFromEnv = getAimlApiKeyFromEnv();
if (!aimlFromEnv) {
this._failMissingLLMKeyCi(providerId);
}
}
const aimlApiKey = getAimlApiKey();
if (!aimlApiKey) {
throw new Error(
"⚠️ AI/ML API key not found. Please set AIMLAPI_API_KEY environment variable or configure it user-wide.",
);
}
console.log(
`Creating AI/ML API client for ${targetLocale} using model ${modelId}`,
);
// Import lazily to avoid requiring the package when not used in tests
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { createOpenAI } = require("@ai-sdk/openai");
return createOpenAI({
apiKey: aimlApiKey,
baseURL: "https://api.aimlapi.com/v1",
})(modelId);
}
case "openrouter": {
// Specific check for CI/CD or Docker missing OpenRouter key
if (isRunningInCIOrDocker()) {
Expand Down Expand Up @@ -385,7 +412,7 @@ export class LCPAPI {

default: {
throw new Error(
`⚠️ Provider "${providerId}" for locale "${targetLocale}" is not supported. Only "groq", "google", "openrouter", "ollama", and "mistral" providers are supported at the moment.`,
`⚠️ Provider "${providerId}" for locale "${targetLocale}" is not supported. Only "groq", "google", "aimlapi", "openrouter", "ollama", and "mistral" providers are supported at the moment.`,
);
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/compiler/src/lib/lcp/api/provider-details.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe("provider-details", () => {
expect(Object.keys(providerDetails)).toEqual([
"groq",
"google",
"aimlapi",
"openrouter",
"ollama",
"mistral",
Expand Down
7 changes: 7 additions & 0 deletions packages/compiler/src/lib/lcp/api/provider-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export const providerDetails: Record<
getKeyLink: "https://ai.google.dev/",
docsLink: "https://ai.google.dev/gemini-api/docs/troubleshooting",
},
aimlapi: {
name: "AI/ML API",
apiKeyEnvVar: "AIMLAPI_API_KEY",
apiKeyConfigKey: "llm.aimlApiKey",
getKeyLink: "https://aimlapi.com",
docsLink: "https://docs.aimlapi.com/",
},
openrouter: {
name: "OpenRouter",
apiKeyEnvVar: "OPENROUTER_API_KEY",
Expand Down
3 changes: 3 additions & 0 deletions packages/compiler/src/types/ai-sdk-openai.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module "@ai-sdk/openai" {
export function createOpenAI(options?: any): any;
}
12 changes: 12 additions & 0 deletions packages/compiler/src/utils/llm-api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ export function getOpenRouterKeyFromEnv() {
return getKeyFromEnv("OPENROUTER_API_KEY");
}

export function getAimlApiKey() {
return getAimlApiKeyFromEnv() || getAimlApiKeyFromRc();
}

export function getAimlApiKeyFromRc() {
return getKeyFromRc("llm.aimlApiKey");
}

export function getAimlApiKeyFromEnv() {
return getKeyFromEnv("AIMLAPI_API_KEY");
}

export function getMistralKey() {
return getMistralKeyFromEnv() || getMistralKeyFromRc();
}
Expand Down
1 change: 1 addition & 0 deletions packages/spec/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ const providerSchema = Z.object({
"anthropic",
"google",
"ollama",
"aimlapi",
"openrouter",
"mistral",
]).describe("Identifier of the translation provider service."),
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

Loading