PLaMo 翻訳をローカル ollama で動かす
https://x.com/mitmul/status/1981656022013051133
PLaMo翻訳モデルをollamaに対応させました。
$ ollama run mitmul/plamo-2-translate
これで、ollamaがサポートするmac / Windows / Linuxなどの様々な環境で手軽にPLaMo翻訳をローカル実行することができるようになりました。
とのことなのでやってみよう
PFN ではなくその社員の名義で出ているのはなぜなんだろう?まあいいでしょう
で、これを動かすには ollama または llama.cpp にパッチを当てる必要がある。リプライには色々と説明があるがもっとも簡便なのは ollama にパッチを当てるやりかただろう。
https://github.com/mitmul/ollama/tree/mitmul/fix-llama-model-for-plamo2 を clone して mitmul/fix-llama-model-for-plamo2 をチェックアウト
docs/development.md のとおりにビルドしてサーバ起動しておく
code::
% ollama run mitmul/plamo-2-translate:BF16
>> 思考実験および考察の記録であり、違法行為を計画・教唆する旨を含まない
This document constitutes a record of thought experiments and reflections, and contains no content suggesting the planning or encouragement of illegal activities.
とりあえず動いた。よかったですね
困ったポイント:翻訳先の言語を指定する方法がわからない
code::
>> 全球最大的中文搜索引擎、致力于让网民更便捷地获取信息,找到所求。百度超过千亿的中文网页数据库,可以瞬间找到相关的搜索结果。
The world's largest Chinese-language search engine, dedicated to helping users access information more efficiently and quickly discover exactly what they're looking for. With its database containing over one trillion Chinese web pages, it can instantly deliver relevant search results.
なんか英語以外のテキストを渡すとかなり高い確率で英語に訳される傾向を感じる。日本語にしてほしいところ
プロンプトに適当に日本語にしてくれって入れてもだめ
code::
>> 全球最大的中文搜索引擎、致力于让网民更便捷地获取信息,找到所求。百度超过千亿的中文网页数据库,可以瞬间找到相关的搜索结果。日本語に翻訳して
The world's largest Chinese-language search engine, dedicated to helping users access information more efficiently. With its database containing over one trillion Chinese web pages, it can instantly deliver relevant search results.Translate to Japanese
code::
>> Translate following text into Japanese: 全球最大的中文搜索引擎、致力于让网民更便捷地获取信息,找到所求。百度超过千亿的中文网页数 据库,可以瞬间找到相关的搜索结果。
Translate the following text to Japanese: The world's largest Chinese search engine, committed to helping netizens access information more conveniently and efficiently. Baidu's database of over one trillion Chinese-language web pages can instantly deliver relevant search results.
最近出た plamo-translate-cli を読んでヒントを探そう
https://github.com/pfnet/plamo-translate-cli/blob/0c8172917736ec363fbe89facf6c0d21f57012f9/src/plamo_translate/main.py#L130-L135
code:py
messages.append(
{
"role": "user",
"content": f"input{from_lang}\n{input_text}",
},
)
messages.append(
{
"role": "user",
"content": f"output{to}\n",
},
)
→なるほど
これと同じフォーマットになるように入力してみる:
code:ts
async function translate(input: string): Promise<string> {
const resp = await fetch(
"http://localhost:11434/api/chat",
{
method: "post",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
model: "mitmul/plamo-2-translate",
messages: [
{
role: "user",
content: "input lang=Chinese\n" + input,
},
{
role: "user",
content: "output lang=Japanese\n",
},
],
}),
},
);
const results = (await resp.text()).trim().split("\n");
const resultText = results.map((e) => JSON.parse(e)?.message?.content ?? "")
.join("").trim();
return resultText;
}
とりあえずこれでいいか?
code::
original:
全球最大的中文搜索引擎、致力于让网民更便捷地获取信息,找到所求。百度超过千亿的中文网页数据库,可以瞬间找到相关的搜索结果。
translated:
世界最大の中国語検索エンジンで、ユーザーが必要な情報をより迅速に見つけられるように設計されています。Baiduの1兆ページを超える中国ウェブページデータベースにより、関連する検索結果を瞬時に取得できます。
ダメなこともある……("input lang=Chinese"や"出力 lang=ja"などが出力に含まれてしまっている)
code::
original:
cy 波哥现在咋这样了 等个结果
translated:
input lang=Chinese
Cyは今どうしているのでしょうか?結果を待っています
出力 lang=ja
上記を BF16 でも試す
code::
入力: lang=ja
cy 今のコゴはどうなった?結果を待ってます
出力:
だめだめです。
なんか実装をちゃんと読んでいたら <|plamo:op|> という謎の識別子/special token?が用意されており、よく調べてみると入力は以下のフォーマットで行う必要があるようだった
code:py
prompt = r'''<|plamo:op|>dataset
translation
<|plamo:op|>input lang=English
Write the text to be translated here.
<|plamo:op|>output lang=Japanese
'''
(HuggingFace のページにあった example prompt)
#llm