-
Notifications
You must be signed in to change notification settings - Fork 2.8k
docs: add LaraDocumentTranslator documentation #10723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
docs-website/docs/pipeline-components/translators/laradocumenttranslator.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| --- | ||
| title: "LaraDocumentTranslator" | ||
| id: laradocumenttranslator | ||
| slug: "/laradocumenttranslator" | ||
| description: "This component translates the text content of Haystack documents using the Lara translation API." | ||
| --- | ||
|
|
||
| # LaraDocumentTranslator | ||
|
|
||
| This component translates the text content of Haystack documents using the Lara translation API. | ||
|
|
||
| <div className="key-value-table"> | ||
|
|
||
| | | | | ||
| | --- | --- | | ||
| | **Most common position in a pipeline** | After any component that produces documents, such as a Retriever or a Converter | | ||
| | **Mandatory init variables** | `access_key_id`: Lara API access key ID. Can be set with `LARA_ACCESS_KEY_ID` env var. <br /> <br />`access_key_secret`: Lara API access key secret. Can be set with `LARA_ACCESS_KEY_SECRET` env var. | | ||
| | **Mandatory run variables** | `documents`: A list of documents to be translated | | ||
| | **Output variables** | `documents`: A list of translated documents | | ||
| | **API reference** | [Lara](/reference/integrations-lara) | | ||
| | **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/lara | | ||
|
|
||
| </div> | ||
|
|
||
| ## Overview | ||
|
|
||
| [Lara](https://developers.laratranslate.com/docs/introduction) is an adaptive translation AI by [translated](https://translated.com/) that combines the fluency and context handling of LLMs with low hallucination and latency. It adapts to domains at inference time using optional context, instructions, translation memories, and glossaries. | ||
|
|
||
| `LaraDocumentTranslator` takes a list of Haystack documents, translates their text content via the Lara API, and returns new documents containing the translations. The original document ID is preserved in each translated document's metadata under the `original_document_id` key. | ||
|
|
||
| Key features: | ||
|
|
||
| - **Automatic language detection**: set `source_lang` to `None` and Lara auto-detects it. | ||
| - **Translation styles**: choose `"faithful"`, `"fluid"`, or `"creative"` to control the tone. | ||
| - **Context and instructions**: pass surrounding text or natural-language instructions to improve quality. | ||
| - **Translation memories and glossaries**: supply memory or glossary IDs so Lara enforces consistent terminology. | ||
| - **Reasoning (Lara Think)**: enable multi-step linguistic analysis for higher-quality output. | ||
|
|
||
| ## Usage | ||
| ### Installation | ||
|
|
||
| To start using this integration with Haystack, install it with: | ||
|
|
||
| ```shell | ||
| pip install lara-haystack | ||
| ``` | ||
|
|
||
| `LaraDocumentTranslator` needs Lara API credentials to work. It uses the `LARA_ACCESS_KEY_ID` and `LARA_ACCESS_KEY_SECRET` environment variables by default. Otherwise, you can pass them at initialization: | ||
|
|
||
| ```python | ||
| from haystack.utils import Secret | ||
| from haystack_integrations.components.translators.lara import LaraDocumentTranslator | ||
|
|
||
| translator = LaraDocumentTranslator( | ||
| access_key_id=Secret.from_token("<your-access-key-id>"), | ||
| access_key_secret=Secret.from_token("<your-access-key-secret>"), | ||
| source_lang="en-US", | ||
| target_lang="de-DE", | ||
| ) | ||
| ``` | ||
|
|
||
| To get your Lara API credentials, sign up at [laratranslate.com](https://laratranslate.com/). | ||
| ### On its own | ||
|
|
||
| Remember to set the `LARA_ACCESS_KEY_ID` and `LARA_ACCESS_KEY_SECRET` environment variables or pass them in directly. | ||
|
|
||
| ```python | ||
| from haystack import Document | ||
| from haystack.utils import Secret | ||
| from haystack_integrations.components.translators.lara import LaraDocumentTranslator | ||
|
|
||
| translator = LaraDocumentTranslator( | ||
| access_key_id=Secret.from_env_var("LARA_ACCESS_KEY_ID"), | ||
| access_key_secret=Secret.from_env_var("LARA_ACCESS_KEY_SECRET"), | ||
| source_lang="en-US", | ||
| target_lang="de-DE", | ||
| ) | ||
|
|
||
| doc = Document(content="Hello, world!") | ||
| result = translator.run(documents=[doc]) | ||
| print(result["documents"][0].content) | ||
| # >> "Hallo, Welt!" | ||
| ``` | ||
|
anakin87 marked this conversation as resolved.
|
||
|
|
||
| ### In a pipeline | ||
|
|
||
| Below is an example of the `LaraDocumentTranslator` in a pipeline that fetches a webpage, converts it to a document, and translates it from English to German. | ||
|
|
||
| ```python | ||
| from haystack import Pipeline | ||
| from haystack.components.converters import HTMLToDocument | ||
| from haystack.components.fetchers import LinkContentFetcher | ||
| from haystack_integrations.components.translators.lara import LaraDocumentTranslator | ||
|
|
||
| fetcher = LinkContentFetcher() | ||
| converter = HTMLToDocument() | ||
| translator = LaraDocumentTranslator(source_lang="en-US", target_lang="de-DE") | ||
|
|
||
| pipe = Pipeline() | ||
| pipe.add_component("fetcher", fetcher) | ||
| pipe.add_component("converter", converter) | ||
| pipe.add_component("translator", translator) | ||
|
|
||
| pipe.connect("fetcher", "converter") | ||
| pipe.connect("converter", "translator") | ||
|
|
||
| result = pipe.run(data={"fetcher": {"urls": ["https://haystack.deepset.ai/"]}}) | ||
| translated_docs = result["translator"]["documents"] | ||
| for doc in translated_docs: | ||
| print(doc.content) | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...ed_docs/version-2.25/pipeline-components/translators/laradocumenttranslator.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| --- | ||
| title: "LaraDocumentTranslator" | ||
| id: laradocumenttranslator | ||
| slug: "/laradocumenttranslator" | ||
| description: "This component translates the text content of Haystack documents using the Lara translation API." | ||
| --- | ||
|
|
||
| # LaraDocumentTranslator | ||
|
|
||
| This component translates the text content of Haystack documents using the Lara translation API. | ||
|
|
||
| <div className="key-value-table"> | ||
|
|
||
| | | | | ||
| | --- | --- | | ||
| | **Most common position in a pipeline** | After any component that produces documents, such as a Retriever or a Converter | | ||
| | **Mandatory init variables** | `access_key_id`: Lara API access key ID. Can be set with `LARA_ACCESS_KEY_ID` env var. <br /> <br />`access_key_secret`: Lara API access key secret. Can be set with `LARA_ACCESS_KEY_SECRET` env var. | | ||
| | **Mandatory run variables** | `documents`: A list of documents to be translated | | ||
| | **Output variables** | `documents`: A list of translated documents | | ||
| | **API reference** | [Lara](/reference/integrations-lara) | | ||
| | **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/lara | | ||
|
|
||
| </div> | ||
|
|
||
| ## Overview | ||
|
|
||
| [Lara](https://developers.laratranslate.com/docs/introduction) is an adaptive translation AI by [translated](https://translated.com/) that combines the fluency and context handling of LLMs with low hallucination and latency. It adapts to domains at inference time using optional context, instructions, translation memories, and glossaries. | ||
|
|
||
| `LaraDocumentTranslator` takes a list of Haystack documents, translates their text content via the Lara API, and returns new documents containing the translations. The original document ID is preserved in each translated document's metadata under the `original_document_id` key. | ||
|
|
||
| Key features: | ||
|
|
||
| - **Automatic language detection**: set `source_lang` to `None` and Lara auto-detects it. | ||
| - **Translation styles**: choose `"faithful"`, `"fluid"`, or `"creative"` to control the tone. | ||
| - **Context and instructions**: pass surrounding text or natural-language instructions to improve quality. | ||
| - **Translation memories and glossaries**: supply memory or glossary IDs so Lara enforces consistent terminology. | ||
| - **Reasoning (Lara Think)**: enable multi-step linguistic analysis for higher-quality output. | ||
|
|
||
| ## Usage | ||
| ### Installation | ||
|
|
||
| To start using this integration with Haystack, install it with: | ||
|
|
||
| ```shell | ||
| pip install lara-haystack | ||
| ``` | ||
|
|
||
| `LaraDocumentTranslator` needs Lara API credentials to work. It uses the `LARA_ACCESS_KEY_ID` and `LARA_ACCESS_KEY_SECRET` environment variables by default. Otherwise, you can pass them at initialization: | ||
|
|
||
| ```python | ||
| from haystack.utils import Secret | ||
| from haystack_integrations.components.translators.lara import LaraDocumentTranslator | ||
|
|
||
| translator = LaraDocumentTranslator( | ||
| access_key_id=Secret.from_token("<your-access-key-id>"), | ||
| access_key_secret=Secret.from_token("<your-access-key-secret>"), | ||
| source_lang="en-US", | ||
| target_lang="de-DE", | ||
| ) | ||
| ``` | ||
|
|
||
| To get your Lara API credentials, sign up at [laratranslate.com](https://laratranslate.com/). | ||
| ### On its own | ||
|
|
||
| Remember to set the `LARA_ACCESS_KEY_ID` and `LARA_ACCESS_KEY_SECRET` environment variables or pass them in directly. | ||
|
|
||
| ```python | ||
| from haystack import Document | ||
| from haystack.utils import Secret | ||
| from haystack_integrations.components.translators.lara import LaraDocumentTranslator | ||
|
|
||
| translator = LaraDocumentTranslator( | ||
| access_key_id=Secret.from_env_var("LARA_ACCESS_KEY_ID"), | ||
| access_key_secret=Secret.from_env_var("LARA_ACCESS_KEY_SECRET"), | ||
| source_lang="en-US", | ||
| target_lang="de-DE", | ||
| ) | ||
|
|
||
| doc = Document(content="Hello, world!") | ||
| result = translator.run(documents=[doc]) | ||
| print(result["documents"][0].content) | ||
| # >> "Hallo, Welt!" | ||
| ``` | ||
|
|
||
| ### In a pipeline | ||
|
|
||
| Below is an example of the `LaraDocumentTranslator` in a pipeline that fetches a webpage, converts it to a document, and translates it from English to German. | ||
|
|
||
| ```python | ||
| from haystack import Pipeline | ||
| from haystack.components.converters import HTMLToDocument | ||
| from haystack.components.fetchers import LinkContentFetcher | ||
| from haystack_integrations.components.translators.lara import LaraDocumentTranslator | ||
|
|
||
| fetcher = LinkContentFetcher() | ||
| converter = HTMLToDocument() | ||
| translator = LaraDocumentTranslator(source_lang="en-US", target_lang="de-DE") | ||
|
|
||
| pipe = Pipeline() | ||
| pipe.add_component("fetcher", fetcher) | ||
| pipe.add_component("converter", converter) | ||
| pipe.add_component("translator", translator) | ||
|
|
||
| pipe.connect("fetcher", "converter") | ||
| pipe.connect("converter", "translator") | ||
|
|
||
| result = pipe.run(data={"fetcher": {"urls": ["https://haystack.deepset.ai/"]}}) | ||
| translated_docs = result["translator"]["documents"] | ||
| for doc in translated_docs: | ||
| print(doc.content) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 [vale] reported by reviewdog 🐶
[Google.Parens] Use parentheses judiciously.