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

本文介绍了Google发布的Gemini模型,包括如何通过API访问、网页界面操作以及其在翻译和文本生成方面的应用。作者展示了如何使用PythonAPI进行模型测试,包括基本的文本生成和i18n翻译功能的实例。
摘要由CSDN通过智能技术生成

前言

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

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

准备

首先需要安装相关的包

pip install -q -U google-generativeai

接着从站点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)

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

models/gemini-pro
models/gemini-pro-vision

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

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

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

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

print(response.text)

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

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

打印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.

最后,测试下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}
```
'''

在提示词中我声明了我的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)

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

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 = "系列"

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

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

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

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值