You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
langchain/docs/docs/integrations/document_transformers/google_translate.ipynb

218 lines
8.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Google Translate\n",
"\n",
"[Google Translate](https://translate.google.com/) is a multilingual neural machine translation service developed by Google to translate text, documents and websites from one language into another.\n",
"\n",
"The `GoogleTranslateTransformer` allows you to translate text and HTML with the [Google Cloud Translation API](https://cloud.google.com/translate).\n",
"\n",
"To use it, you should have the `google-cloud-translate` python package installed, and a Google Cloud project with the [Translation API enabled](https://cloud.google.com/translate/docs/setup). This transformer uses the [Advanced edition (v3)](https://cloud.google.com/translate/docs/intro-to-v3).\n",
"\n",
"- [Google Neural Machine Translation](https://en.wikipedia.org/wiki/Google_Neural_Machine_Translation)\n",
"- [A Neural Network for Machine Translation, at Production Scale](https://blog.research.google/2016/09/a-neural-network-for-machine.html)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install --upgrade --quiet google-cloud-translate"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.document_transformers import GoogleTranslateTransformer\n",
"from langchain_core.documents import Document"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Input\n",
"\n",
"This is the document we'll translate"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"sample_text = \"\"\"[Generated with Google Bard]\n",
"Subject: Key Business Process Updates\n",
"\n",
"Date: Friday, 27 October 2023\n",
"\n",
"Dear team,\n",
"\n",
"I am writing to provide an update on some of our key business processes.\n",
"\n",
"Sales process\n",
"\n",
"We have recently implemented a new sales process that is designed to help us close more deals and grow our revenue. The new process includes a more rigorous qualification process, a more streamlined proposal process, and a more effective customer relationship management (CRM) system.\n",
"\n",
"Marketing process\n",
"\n",
"We have also revamped our marketing process to focus on creating more targeted and engaging content. We are also using more social media and paid advertising to reach a wider audience.\n",
"\n",
"Customer service process\n",
"\n",
"We have also made some improvements to our customer service process. We have implemented a new customer support system that makes it easier for customers to get help with their problems. We have also hired more customer support representatives to reduce wait times.\n",
"\n",
"Overall, we are very pleased with the progress we have made on improving our key business processes. We believe that these changes will help us to achieve our goals of growing our business and providing our customers with the best possible experience.\n",
"\n",
"If you have any questions or feedback about any of these changes, please feel free to contact me directly.\n",
"\n",
"Thank you,\n",
"\n",
"Lewis Cymbal\n",
"CEO, Cymbal Bank\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When initializing the `GoogleTranslateTransformer`, you can include the following parameters to configure the requests.\n",
"\n",
"- `project_id`: Google Cloud Project ID.\n",
"- `location`: (Optional) Translate model location.\n",
" - Default: `global` \n",
"- `model_id`: (Optional) Translate [model ID][models] to use.\n",
"- `glossary_id`: (Optional) Translate [glossary ID][glossaries] to use.\n",
"- `api_endpoint`: (Optional) [Regional endpoint][endpoints] to use.\n",
"\n",
"[models]: https://cloud.google.com/translate/docs/advanced/translating-text-v3#comparing-models\n",
"[glossaries]: https://cloud.google.com/translate/docs/advanced/glossary\n",
"[endpoints]: https://cloud.google.com/translate/docs/advanced/endpoints"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"documents = [Document(page_content=sample_text)]\n",
"translator = GoogleTranslateTransformer(project_id=\"<YOUR_PROJECT_ID>\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Output\n",
"\n",
"After translating a document, the result will be returned as a new document with the `page_content` translated into the target language.\n",
"\n",
"You can provide the following keyword parameters to the `transform_documents()` method:\n",
"\n",
"- `target_language_code`: [ISO 639][iso-639] language code of the output document.\n",
" - For supported languages, refer to [Language support][supported-languages].\n",
"- `source_language_code`: (Optional) [ISO 639][iso-639] language code of the input document.\n",
" - If not provided, language will be auto-detected.\n",
"- `mime_type`: (Optional) [Media Type][media-type] of the input text.\n",
" - Options: `text/plain` (Default), `text/html`.\n",
"\n",
"[iso-639]: https://en.wikipedia.org/wiki/ISO_639\n",
"[supported-languages]: https://cloud.google.com/translate/docs/languages\n",
"[media-type]: https://en.wikipedia.org/wiki/Media_type"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"translated_documents = translator.transform_documents(\n",
" documents, target_language_code=\"es\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'model': '', 'detected_language_code': 'en'}\n",
"[Generado con Google Bard]\n",
"Asunto: Actualizaciones clave de procesos comerciales\n",
"\n",
"Fecha: viernes 27 de octubre de 2023\n",
"\n",
"Estimado equipo,\n",
"\n",
"Le escribo para brindarle una actualización sobre algunos de nuestros procesos comerciales clave.\n",
"\n",
"Proceso de ventas\n",
"\n",
"Recientemente implementamos un nuevo proceso de ventas que está diseñado para ayudarnos a cerrar más acuerdos y aumentar nuestros ingresos. El nuevo proceso incluye un proceso de calificación más riguroso, un proceso de propuesta más simplificado y un sistema de gestión de relaciones con el cliente (CRM) más eficaz.\n",
"\n",
"Proceso de mercadeo\n",
"\n",
"También hemos renovado nuestro proceso de marketing para centrarnos en crear contenido más específico y atractivo. También estamos utilizando más redes sociales y publicidad paga para llegar a una audiencia más amplia.\n",
"\n",
"proceso de atención al cliente\n",
"\n",
"También hemos realizado algunas mejoras en nuestro proceso de atención al cliente. Hemos implementado un nuevo sistema de atención al cliente que facilita que los clientes obtengan ayuda con sus problemas. También hemos contratado más representantes de atención al cliente para reducir los tiempos de espera.\n",
"\n",
"En general, estamos muy satisfechos con el progreso que hemos logrado en la mejora de nuestros procesos comerciales clave. Creemos que estos cambios nos ayudarán a lograr nuestros objetivos de hacer crecer nuestro negocio y brindar a nuestros clientes la mejor experiencia posible.\n",
"\n",
"Si tiene alguna pregunta o comentario sobre cualquiera de estos cambios, no dude en ponerse en contacto conmigo directamente.\n",
"\n",
"Gracias,\n",
"\n",
"Platillo Lewis\n",
"Director ejecutivo, banco de platillos\n",
"\n"
]
}
],
"source": [
"for doc in translated_documents:\n",
" print(doc.metadata)\n",
" print(doc.page_content)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}