Skip to content

Instantly share code, notes, and snippets.

@sebington
Last active October 30, 2025 10:51
Show Gist options
  • Select an option

  • Save sebington/382e2c6ebd43a28453c3c2541d4b241f to your computer and use it in GitHub Desktop.

Select an option

Save sebington/382e2c6ebd43a28453c3c2541d4b241f to your computer and use it in GitHub Desktop.
Sends a character chain to a Groq model and retrieves the corresponding RFC 5646 language code (Groq API key needed)
import os
import json
from groq import Groq
def get_language_code(text: str, api_key: str = None) -> str:
"""
Identify the RFC 5646 language code of given text using Groq API.
Args:
text: The text to identify
api_key: Your Groq API key (or set GROQ_API_KEY env variable)
Returns:
RFC 5646 language code (e.g., 'en', 'fr', 'es')
"""
client = Groq(api_key=api_key or os.environ.get("GROQ_API_KEY"))
prompt = f"""Identify the language of the following text and return ONLY a JSON object with this exact format:
{{"code": "RFC 5646 language code"}}
Do not include any explanation, markdown formatting, or additional text. Only return the JSON object.
Text: "{text}"
"""
chat_completion = client.chat.completions.create(
messages=[{"role": "user", "content": prompt}],
model="llama-3.3-70b-versatile",
temperature=0.1,
max_tokens=50,
)
response_text = chat_completion.choices[0].message.content.strip()
# Remove markdown code blocks if present
if response_text.startswith("```"):
response_text = response_text.split("```")[1]
if response_text.startswith("json"):
response_text = response_text[4:]
response_text = response_text.strip()
result = json.loads(response_text)
return result["code"]
# Example usage
if __name__ == "__main__":
examples = [
"Bonjour, comment allez-vous?",
"Hello, how are you?",
"¿Hola, cómo estás?",
"Guten Tag!",
]
for text in examples:
code = get_language_code(text)
print(f"{text}{code}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment