ai-language-detect Edge Function — Ai
AIDetects the natural language of a text string using character trigram frequency analysis, supporting 13 languages with no external API calls.
Edge function ai-language-detect Detects the natural language of a text string using character trigram frequency analysis, supporting 13 languages with no external API calls.. Deployed on Cloudflare Workers — zero cold starts, globally distributed. Mount it via your Aerostack workspace to call it from any AI agent.
npx aerostack add navin/ai-language-detect Use with AI Assistants
MCPConnect Claude, Cursor, or any MCP-compatible client — then call this function by slug
① Add MCP Server
Add this once — access all Aerostack functions from your AI tool.
{
"mcpServers": {
"aerostack": {
"url": "https://mcp.aerostack.dev",
"type": "http"
}
}
} ② Call this function
Ask your AI to use the call_function tool with this slug:
call_function({
slug: "ai-language-detect",
args: {
"text": "example_text",
"topN": 1
}
}) ai-language-detect — Detect the language of a text string
Identifies the natural language of text using character trigram frequency analysis. Supports 13 languages with no external API calls.
Supported Languages
English, Spanish, French, German, Portuguese, Italian, Dutch, Polish, Russian, Chinese, Japanese, Korean, Arabic.
API
POST /api/ai-language-detect
Request body
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
text |
string | ✅ | — | Text to detect the language of |
topN |
number | ❌ | 1 |
Number of language candidates to return, ranked by confidence |
Success response (200)
{
"success": true,
"data": {
"detected": [
{ "language": "English", "code": "en", "confidence": 0.82 }
],
"primary": { "language": "English", "code": "en", "confidence": 0.82 }
}
}
Error responses
| Code | HTTP | When |
|---|---|---|
INVALID_INPUT |
400 | Missing or empty text |
INTERNAL_ERROR |
500 | Unexpected error |
Usage
cURL
curl -X POST "$FUNCTION_URL" \
-H "Content-Type: application/json" \
-d '{"text": "Bonjour le monde, comment allez-vous?", "topN": 3}'
TypeScript / JavaScript (HTTP)
const response = await fetch(FUNCTION_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: userMessage }),
});
const { data } = await response.json();
if (data.primary.code !== 'en') {
// Route to multilingual prompt
}
Direct import (Node / Bun / Deno)
import { aiLanguageDetect } from '@aerostack/functions/ai-language-detect';
const { primary } = aiLanguageDetect({ text: 'Hola mundo' });
console.log(primary.language); // "Spanish"
Use Cases
- Auto-routing user messages to language-specific system prompts before calling an LLM.
- Tagging ingested documents with their language for multilingual search and filtering.
- Validating that user-submitted content matches the expected language for a service.
Notes
- Detection uses character trigram frequency analysis — no API keys or network calls required.
- Confidence is reduced for texts shorter than 20 characters.
- For very short inputs, increase text length for reliable detection.
- Confidence values are normalised to [0, 1].
Metadata
Tags
Publisher
@navin verified
Build and publish your own functions
Write a TypeScript function, deploy it to the edge, and share it with thousands of developers — in minutes.
More AI Functions
Browse AI Functions →ai-chunk-overlap
by @navin
Splits text into overlapping chunks with configurable size, overlap, and boundary snapping (char, word, sentence) for RAG and embedding pipelines.
ai-context-window-fit
by @navin
Trims a conversation message array to fit within a model's context window using configurable strategies, without making any API calls.
ai-cost-estimate
by @navin
Calculates the API cost for an LLM request given a model name, prompt token count, and completion token count, supporting multiple currencies.
ai-extract-keywords
by @navin
Extracts the top N keywords from text using TF-IDF inspired scoring with built-in English stopword filtering, no external API calls required.
ai-guardrail-injection-detect
by @navin
Scores text for common prompt injection attack patterns including role overrides, instruction leaking, and jailbreak attempts.
ai-messages-to-prompt
by @navin
Serialises a structured message array into a formatted prompt string for open-source LLMs, supporting ChatML, Llama 2, Alpaca, and plain text formats.
Frequently asked questions
What does the ai-language-detect function do? +
ai-language-detect is a serverless edge function for ai automation written in aerostack. Deploy it to Cloudflare Workers via your Aerostack workspace.
How do I deploy the ai-language-detect function? +
Install the Aerostack CLI and run: ```bash aerostack deploy function @navin/ai-language-detect ``` It will be live on Cloudflare Workers in seconds.
What runtime does ai-language-detect use? +
ai-language-detect runs on aerostack on the Cloudflare Workers edge runtime — zero cold starts, globally distributed.
Can I customise the ai-language-detect function? +
Yes. Fork the function from your Aerostack dashboard, modify the source, and redeploy. All changes are version-controlled.