当前位置:   article > 正文

测试Gemini Pro模型并展示其在i18n翻译场景下的能力_empty result翻译

empty result翻译

前言

2023.12.06号,Google发博客介绍了其对抗OpenAI的新模型,Gemini。
想体验的可以到网站https://makersuite.google.com/app/prompts/new_freeform

除了在网上通过web界面进行控制,还可以使用API对其进行访问,而且目前整个接口都是免费的(需要魔法)。
本文章通过演示接口访问,并测试其在翻译层面的能力情况。

准备

首先需要安装相关的包

pip install -q -U google-generativeai
  • 1

接着从站点https://makersuite.google.com/app/apikey申请API Key。需要注意的是API Key需要绑定一个GCP项目。
API Key

测试

接下来是测试部分。

首先测试模型列表:

import google.generativeai as genai

# get key from https://makersuite.google.com/app/apikey
GOOGLE_API_KEY = 'YOUR_GOOGLE_API_KEY'
genai.configure(api_key=GOOGLE_API_KEY)

for m in genai.list_models():
  if 'generateContent' in m.supported_generation_methods:
    print(m.name)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

上面的代码会打印如下内容:

models/gemini-pro
models/gemini-pro-vision
  • 1
  • 2

我们可以看到支持的两个模型,gemini-progemini-pro-vision

接着测试下基础的文本生成能力

model = genai.GenerativeModel('gemini-pro')

%%time
response = model.generate_content("What is the meaning of life?")

print(response.text)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

time命令显示该请求大概花费了6.5s时间

CPU times: user 6.62 ms, sys: 4.81 ms, total: 11.4 ms
Wall time: 6.55 s
  • 1
  • 2

打印response.text显示如下内容:

The meaning of life is a philosophical question that has been asked for centuries. There is no one answer that is universally agreed upon, as the meaning of life is subjective and personal to each individual.

Some people believe that the meaning of life is to find happiness and fulfillment. Others believe that it is to make a difference in the world, or to leave a legacy behind. There are also those who believe that the meaning of life is to connect with something greater than oneself, such as God or the universe.

Ultimately, the meaning of life is something that each person must discover for themselves. There is no right or wrong answer, and the journey of finding meaning in life can be a lifelong pursuit.

Here are some additional thoughts and perspectives on the meaning of life:

* **The meaning of life is different for everyone.** There is no one-size-fits-all answer to this question, as each person's experiences, values, and beliefs will shape their own unique understanding of what life is all about.
* **The meaning of life is not static.** It can change and evolve over time, as we grow and learn and experience new things.
* **The meaning of life is not something that can be found once and for all.** It is an ongoing process of discovery and exploration.
* **The meaning of life is not something that can be achieved through material possessions or external validation.** It is something that comes from within, from our connection to ourselves, to others, and to the world around us.

In the end, the meaning of life is whatever you make it to be. There is no right or wrong answer, and the only way to find out what life means to you is to live it fully and experience all that it has to offer.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

最后,测试下Gemini Pro模型在i18n翻译场景下的能力,我使用的提示词如下:

prompt_template = '''You are a translator and your task is to translate l10n file to different languages.
The l10n file is provided in TOML format. The file contains {{ KEY }} for variables and use
`one` for singular and `other` for plural.

The TOML file is quoted in triple backtick. Please translate the content to {lang}
and keep the original content structure, also remove triple backtick in output:

```toml
{en_file_content}
```
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在提示词中我声明了我的l10n文件格式为TOML,包含{{ KEY }}这样的变量,同时有单数、复数的指令(单数、复数是i18n中常见的场景),同时我让大语言模型保持输出的文件结构与原始的文件结构一致,这样我们就不需要对文件进行额外处理。

en_file_content = '''table_of_contents = "Table of Contents"
open_main_menu = "Open main menu"
open_lang_switcher = "Open language switcher"
[reading_time]
  one = "One minute to read"
  other = "{{ .Count }} minutes to read"

[search]
title = "Search"
empty_result = "No result found."
placeholder = "Search..."

[header]
darkmode_toggle = "Toggle dark mode"

[404]
go_back_home = "Go back home"
sorry = "Sorry, we couldn't find the page you're looking for."
page_not_found = "Page not found"

[footer]
powered_by = "Powered by {{ .Generator }} {{ .Heart }} {{ .Theme }}"
copyright_with_since ="{{ .CopyrightSign }} {{ .SinceYear }} - {{ .CurrentYear}} {{ .Copyright }}"
copyright_wo_since ="{{ .CopyrightSign }} {{ .CurrentYear}} {{ .Copyright }}"

[paginator]
newer_posts = "Newer posts"
older_posts = "Older posts"

[taxonomies]
categories = "Categories"
tags = "Tags"
series = "Series"
'''

prompt = prompt_template.format(lang='Chinese', en_file_content=en_file_content)
response = model.generate_content(prompt)
print(response.text)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

上面的脚本输出如下的内容,

table_of_contents = "目录"
open_main_menu = "打开主菜单"
open_lang_switcher = "打开语言切换器"
[reading_time]
  one = "阅读一分钟"
  other = "阅读{{ .Count }}分钟"

[search]
title = "搜索"
empty_result = "未找到结果。"
placeholder = "搜索..."

[header]
darkmode_toggle = "切换暗黑模式"

[404]
go_back_home = "返回主页"
sorry = "抱歉,我们找不到您要查找的页面。"
page_not_found = "未找到页面"

[footer]
powered_by = "由{{ .Generator }}提供支持 {{ .Heart }} {{ .Theme }}"
copyright_with_since ="{{ .CopyrightSign }} {{ .SinceYear }} - {{ .CurrentYear}} {{ .Copyright }}"
copyright_wo_since ="{{ .CopyrightSign }} {{ .CurrentYear}} {{ .Copyright }}"

[paginator]
newer_posts = "较新的文章"
older_posts = "较旧的文章"

[taxonomies]
categories = "分类"
tags = "标签"
series = "系列"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

从上面的输出可以看到,整体效果还不错。当然一些中文翻译可以做些微调。

我用这样的脚本为我的一款hugo主题进行i18n的翻译工作,包括法语、西班牙语、俄语、日语等,整体效果还不错。
结果呈现可以查看GitHub仓库https://github.com/tomowang/hugo-theme-tailwind/tree/main/i18n

最后,Gemini好像提供公开访问的好像还不是很多,https://www.chat-gpt.ing/bard算一个吧,不过好像也需要魔法。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/881277
推荐阅读
相关标签
  

闽ICP备14008679号