Skip to main content
Google 翻译 是 Google 开发的一项多语言神经机器翻译服务,可将文本、文档和网页从一种语言翻译为另一种语言。 GoogleTranslateTransformer 允许您通过 Google Cloud Translation API 翻译文本和 HTML。 使用前,您需要安装 google-cloud-translate Python 包,并拥有一个已启用 Translation API 的 Google Cloud 项目。该转换器使用高级版(v3)
pip install -qU  google-cloud-translate
from langchain_core.documents import Document
from langchain_google_community import GoogleTranslateTransformer

输入

以下是我们要翻译的文档:
sample_text = """[Generated with Google Bard]
Subject: Key Business Process Updates

Date: Friday, 27 October 2023

Dear team,

I am writing to provide an update on some of our key business processes.

Sales process

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.

Marketing process

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.

Customer service process

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.

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.

If you have any questions or feedback about any of these changes, please feel free to contact me directly.

Thank you,

Lewis Cymbal
CEO, Cymbal Bank
"""
初始化 GoogleTranslateTransformer 时,您可以指定以下参数来配置请求。
  • project_id:Google Cloud 项目 ID。
  • location:(可选)翻译模型位置,默认为 global
  • model_id:(可选)要使用的翻译模型 ID
  • glossary_id:(可选)要使用的翻译术语表 ID
  • api_endpoint:(可选)要使用的区域端点
documents = [Document(page_content=sample_text)]
translator = GoogleTranslateTransformer(project_id="<YOUR_PROJECT_ID>")

输出

翻译文档后,结果将作为新文档返回,其中 page_content 已翻译为目标语言。 transform_documents() 方法传入以下关键字参数:
  • target_language_code:输出文档的 ISO 639 语言代码。有关支持的语言,请参阅语言支持列表
  • source_language_code:(可选)输入文档的 ISO 639 语言代码。若不提供,将自动检测语言。
  • mime_type:(可选)输入文本的媒体类型,可选 text/plain(默认)或 text/html
translated_documents = translator.transform_documents(
    documents, target_language_code="es"
)
for doc in translated_documents:
    print(doc.metadata)
    print(doc.page_content)