Documentation menu

LangChain tools

The official fluentc-langchain-tool Python package. Install it, translate in real time or batch, detect languages, and give a LangChain agent FluentC as tools.

View as Markdown

fluentc-langchain-tool is the official Python package for using FluentC from LangChain. Each capability is a LangChain tool (a BaseTool subclass), so you can call it directly or hand it to an agent.

  • Requires: Python 3.8 or newer and LangChain.
  • Authentication: your FluentC API key, passed to each tool's constructor. See Authentication.
  • Source of truth: the package's page on PyPI.

Install

bash
pip install fluentc-langchain-tool
python
import os

API_KEY = os.environ["FLUENTC_KEY"]

The tools

ClassTool nameWhat it does
FluentCTranslationToolfluentc_translationTranslate in real-time or batch mode
FluentCBatchTranslationToolfluentc_batch_translationSubmit a batch job and wait for the result
FluentCLanguageDetectorToolfluentc_language_detectorDetect the language of some text
FluentCTranslationStatusToolfluentc_translation_statusCheck a batch job by job_id
FluentCSupportedLanguagesToolfluentc_supported_languagesList the languages enabled for your key
FluentCResultsToolfluentc_resultsLegacy. Use fluentc_translation_status instead

All tools take the API key as their only constructor argument: FluentCTranslationTool(api_key=API_KEY). Tools return a string, which is what an agent reads back.

Translate in real time

python
from fluentc_langchain_tool import FluentCTranslationTool

tool = FluentCTranslationTool(api_key=API_KEY)

result = tool.run({
    "text": "Hello, world!",
    "target_language": "fr",
    "source_language": "en",
    "mode": "realtime",
})
print(result)  # Bonjour, le monde !

Input fields for fluentc_translation:

FieldTypeRequiredDescription
textstring or list of stringsYesWhat to translate.
target_languagestringYesTarget language code, such as fr.
source_languagestringYesSource language code, such as en.
modestringNorealtime (default) or batch.
callback_urlstringNoFor batch mode: a URL to notify when the job completes.

Real-time mode needs the Real-time plan. See Concepts.

Translate in batch

FluentCBatchTranslationTool submits the job and polls until it finishes or max_wait_time runs out:

python
from fluentc_langchain_tool import FluentCBatchTranslationTool

tool = FluentCBatchTranslationTool(api_key=API_KEY)

result = tool.run({
    "text": "<html>Hello, batch world!</html>",
    "target_language": "de",
    "source_language": "en",
})
print(result)  # the translated output, once the job completes

Input fields for fluentc_batch_translation:

FieldTypeDefaultDescription
textstring or list of stringsrequiredWhat to translate.
target_languagestringrequiredTarget language code.
source_languagestringrequiredSource language code.
wait_for_completionbooleantrueSet false to return the job ID immediately.
max_wait_timeinteger300Seconds to wait before giving up.
callback_urlstringnoneA URL to notify when the job completes.

To control the loop yourself, set wait_for_completion to false and check the job with FluentCTranslationStatusTool:

python
from fluentc_langchain_tool import FluentCTranslationStatusTool

status = FluentCTranslationStatusTool(api_key=API_KEY)
print(status.run({"job_id": "abc123def456"}))

Detect a language

python
from fluentc_langchain_tool import FluentCLanguageDetectorTool

detector = FluentCLanguageDetectorTool(api_key=API_KEY)
print(detector.run({"text": "Hola, ¿cómo estás?"}))
# Detected language: es (confidence: 0.99)

List supported languages

python
from fluentc_langchain_tool import FluentCSupportedLanguagesTool

languages = FluentCSupportedLanguagesTool(api_key=API_KEY)
print(languages.run({}))

Use the tools in an agent

Pass the tools to any LangChain agent. The tool names and descriptions tell the model when to use each one.

python
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
from fluentc_langchain_tool import (
    FluentCTranslationTool,
    FluentCLanguageDetectorTool,
)

agent = initialize_agent(
    tools=[
        FluentCTranslationTool(API_KEY),
        FluentCLanguageDetectorTool(API_KEY),
    ],
    llm=OpenAI(temperature=0),
    agent="zero-shot-react-description",
)

agent.run("Translate 'Hello world' from English to German using FluentC.")

This example uses LangChain's classic initialize_agent interface. The tools are standard BaseTool objects, so they work with newer LangChain agent constructors too.

Errors

The tools catch API errors and return them as a readable string, for example Translation error: ..., instead of raising. An agent can read that and recover or report it. If you need exceptions, call the REST API directly.

The usual causes are on the Errors page: a language that isn't enabled for your key, a real-time request on a Batch plan key, or a key problem.

Something unclear, or missing? Tell us.