xcd0 / easygpt

テキストファイルをchatgptのapiに投げる。
MIT License
0 stars 0 forks source link

Claude3 Haiku対応 #3

Open xcd0 opened 6 months ago

xcd0 commented 6 months ago

2024/03現在gpt3.5-turboより安価かつ

All Claude 3 models support vision and 200,000 token context windows.

とtoken数も大きいので都合が良い。

xcd0 commented 6 months ago

https://docs.anthropic.com/claude/reference/messages_post

とりあえずcurlで動いた。openaiとそう変わらない。

#!/bin/bash

ANTHROPIC_API_KEY=ここにAPIキーを入れる

ret=$(curl -s https://api.anthropic.com/v1/messages \
    --header "content-type: application/json" \
    --header "anthropic-version: 2023-06-01" \
    --header "x-api-key: $ANTHROPIC_API_KEY" \
    --data \
    '{
    "model": "claude-3-haiku-20240307",
    "messages": [
    {
        "role": "user",
        "content": "自己紹介してください。"
    }
    ],
    "max_tokens": 256,
    "stream": false
}')
echo $ret | jq -r ".content[0].text"

はい、よろしくお願いします。私は人工知能アシスタントのクーロかと申します。人工知能の機能と使い方について、皆様からご質問やご相談を承ることができます。分かりやすく丁寧にお答えさせていただきますので、ご気軽にお声がけください。人工知能についての理解を深めていただけますと幸いです。よろしくお願いいたします。

xcd0 commented 6 months ago

chatgptが生成した小説を和訳させた。

contents=$( cat <<-EOF
In the heart of an ancient forest, veiled by the whispers of leaves and the songs of hidden streams, stood a cottage forgotten by time. Its wooden walls, cloaked in ivy and moss, held stories of a bygone era, and within, an old clock ticked away the seconds, each tick a gentle reminder of life's fleeting dance.

Eleanor, a young woman with eyes as deep and mysterious as the forest itself, called this place home. Her days were spent wandering the shadowed paths, where light and darkness played in eternal balance, and her nights under the canopy of stars, her thoughts as boundless as the night sky.

One crisp autumn morning, as golden leaves danced in the gentle breeze, Eleanor stumbled upon a peculiar stone, half-buried beneath the earth. It was unlike any she had seen before, shimmering with an inner light that seemed to pulse in rhythm with her own heart. Curiosity alight, she unearthed the stone, its warmth spreading through her fingers, a feeling of unspoken connection weaving its way into her soul.

Days turned to weeks, and Eleanor found herself drawn to the stone's mysterious allure. She would often sit by the fire, the stone cradled in her hands, and whisper to it tales of her solitary life, her voice a soft melody in the quiet of the cottage.

Then, on a night when the moon hung full and bright, casting silvery shadows through the forest, a change stirred in the air. The stone, once warm and comforting, now throbbed with a fierce energy, its light growing, pulsing faster, as if echoing a heartbeat.

Compelled by a force she could not understand, Eleanor ventured into the forest, guided by the stone's radiant glow. The trees, ancient guardians of the forest, seemed to part before her, leading her to a clearing she had never seen before, where the moonlight bathed the earth in a ethereal glow.

In the center of the clearing stood an old, gnarled tree, its branches reaching towards the heavens. And there, at its base, was a hollow, within which the stone fit perfectly. As Eleanor placed the stone into the hollow, a surge of light erupted, enveloping her, the tree, the entire clearing in a brilliant luminescence.

When the light receded, Eleanor found herself not in the forest, but in a realm beyond her wildest dreams. A place where the sky shimmered with colors she had no names for, where the air was alive with the magic of possibility.

And so, Eleanor realized, the stone was not merely a stone, but a key$2014a key to a doorway between worlds, a bridge across the realms of imagination. With a heart full of wonder, she stepped forward into the unknown, her story no longer bound by the walls of the cottage or the edges of the forest, but open to the endless horizons of new worlds.
EOF
)

json=$(
echo '{
    "model": "claude-3-haiku-20240307",
    "system" : "You are a translater. Translate the following into Japanese. Respond only in Japanese.",
    "messages": [
    {
        "role": "user",
        "content": "'$contents'"
    }
    ],
    "max_tokens": 1024,
    "stream": false
}' | jq .
)

echo $json | jq .

ret=$(curl -s https://api.anthropic.com/v1/messages \
    --header "content-type: application/json" \
    --header "anthropic-version: 2023-06-01" \
    --header "x-api-key: $ANTHROPIC_API_KEY" \
    --data "$json"
)

echo $ret | jq -r ".content[0].text"

古代の森の中心に、葉のささやきと隠された小川の歌に包まれた、時の流れに忘れられた小さな小屋がありました。その木の壁は、蔦やコケに覆われ、過去の物語を秘めていました。そこには古時計が秒針を刻み、はかない人生の優しい合図を響かせていました。 エレノアという名の若い女性が、この場所を居心地の良い家としていました。彼女は、光と闇のバランスが織りなす影の中を歩き、星空の下での思索に夜を過ごしていました。 ある秋の朝、金色の葉が優しい風に舞う中で、エレノアは地面に半ば埋もれた奇妙な石を見つけました。その石は、自身の心臓の鼓動に呼応するかのように輝いていました。 好奇心に駆られたエレノアは、その石を掘り起こしました。その温かな感触は、彼女の魂に奥深い繋がりを感じさせました。日々が過ぎ、エレノアはその石の不思議な魅力に引き寄せられ続けていきました。 ある満月の夜、石が激しいエネルギーを放ち始めると、エレノアは石の光に導かれるように森の奥へと向かいました。そして、そこで出会った古木の中心に石が嵌まり、光に包まれていきました。 光が消えると、エレノアは自分が未知の領域に立っているのを発見しました。空が奇妙な色彩に輝き、空気が可能性の魔法に満ちていました。そして、エレノアは、その石が異世界への扉の鍵であることを悟ったのです。 好奇心に満ちた心を抱えながら、エレノアは未知への一歩を踏み出していきました。もはや小屋の壁や森の縁に縛られることなく、新しい世界への無限の可能性が広がっていくのでした。


chatgpt4訳

古代の森の心臓部で、葉のささやきと隠された小川の歌によってベールに包まれて、時間に忘れられた小さな家が立っていました。その木製の壁は、ツタと苔に覆われ、過ぎ去った時代の物語を保持しており、内部では古い時計が秒ごとに刻み、それぞれの刻みは人生のはかない舞いの優しいリマインダーでした。

エレノアという名の若い女性は、その場所を家と呼んでいました。彼女の目は、森そのものと同じくらい深く神秘的でした。彼女の日々は、光と闇が永遠のバランスで遊ぶ影の道を彷徨うことで過ごされ、夜は星の天蓋の下で、彼女の思考は夜空ほど無限に広がっていました。

ある清々しい秋の朝、金色の葉がそよ風で舞う中、エレノアは半分地中に埋もれた特異な石を見つけました。彼女がこれまでに見たどの石とも異なり、内側からパルスするような光を放ち、彼女自身の心臓のリズムと同期しているかのようでした。好奇心に駆られて、彼女はその石を掘り出し、その暖かさが彼女の指を通して広がり、言葉にできないつながりの感覚が彼女の魂に織り込まれました。

日が週に変わり、エレノアはその石の神秘的な魅力に引き寄せられました。彼女はしばしば暖炉のそばに座り、手の中で石を揺らしながら、彼女の孤独な生活の話を石に囁きました。彼女の声は、小屋の静けさの中で柔らかな旋律でした。

そして、月が満ちて明るく輝き、森を通して銀色の影を落とす夜に、空気の中に変化が起こりました。かつて温かく心地よかった石は、今や激しいエネルギーで脈打ち、その光は成長し、パルスが速くなり、まるで心臓の鼓動を反響させるかのようでした。

理解できない力に駆り立てられたエレノアは、石の輝く光に導かれて森へと冒険しました。古代の森の守護者である木々は彼女の前で分かれ、彼女がこれまで見たことのない一角へと彼女を導きました。そこでは月光が地球を超自然的な輝きで照らしていました。

その一角の中心には、枝が天に向かって伸びる古く曲がった木が立っていました。そして、その基部にはくぼみがあり、その石が完璧に収まりました。エレノアが石をくぼみに置くと、光の波が発生し、彼女、木、その全ての一角を明るい輝きで包み込みました。

光が収まった時、エレノアは森ではなく、彼女の最も野生の夢を超えた領域にいました。空が彼女が名前を知らない色できらめき、空気が可能性の魔法で生きている場所でした。

そうして、エレノアはその石が単なる石ではなく、世界間の扉、想像力の領域を渡る橋である鍵だと悟りました。不思議で満たされた心を持って、彼女は未知へと一歩を踏み出しました。彼女の物語はもはや小屋の壁や森の端に縛られることなく、新しい世界の無限の地平へと開かれました。

xcd0 commented 6 months ago
# curlとjqが必要。
# https://console.anthropic.com/ からAPIキーを取得して下記に張り付ける。
ANTHROPIC_API_KEY=ここにAPIキーを入れる

# システム指示
system=$( cat <<-EOF
You are an advanced language model trained with the Chain of Thoughts (CoT) methodology, enabling you to effectively process complex reasoning. Your task is to translate the following text into Japanese, adhering to the specified guidelines. Summaries are not permitted. You must carefully interpret the meaning of each word and phrase to ensure an accurate translation. Your response must be exclusively in polite Japanese(丁寧語). The response should consist solely of the translation, without any additional comments or questions.
EOF
)
# 和訳対象テキスト
contents=$( cat <<-EOF
In the soft twilight, the quaint village rested beneath a velvet sky, stars twinkling like distant lanterns. Old Mr. Wilkins, the watchmaker, closed his shop, the last light extinguishing on the cobblestone street. Nearby, a mysterious cat with emerald eyes prowled silently, weaving through shadows as if guarding ancient secrets. In the heart of the village, a gentle breeze whispered through the leaves of the old oak tree, carrying tales of yesteryears. Unseen, a new story began to unfold, as delicate and intricate as the gears in Mr. Wilkins' finest timepiece, waiting for the right moment to come alive under the moon's watchful gaze.
EOF
)

json=$(
echo '{
    "model": "claude-3-haiku-20240307",
    "system": "'$system'",
    "messages": [
    {
        "role": "user",
        "content": "'$contents'"
    }
    ],
    "max_tokens": 1024,
    "stream": false
}' | jq .
)

echo $json | jq .

ret=$(curl -s https://api.anthropic.com/v1/messages \
    --header "content-type: application/json" \
    --header "anthropic-version: 2023-06-01" \
    --header "x-api-key: $ANTHROPIC_API_KEY" \
    --data "$json"
)

echo $ret | jq -r ".content[0].text" | tee tmp.txt
xcd0 commented 6 months ago

端折る傾向にあるので丁寧なプロンプトエンジニアリングが必要な印象。 You are an advanced language model trained with the Chain of Thoughts (CoT) methodology, enabling you to effectively process complex reasoning. Your task is to translate the following text into Japanese, adhering to the specified guidelines. Summaries are not permitted. You must carefully interpret the meaning of each word and phrase to ensure an accurate translation. Your response must be exclusively in polite Japanese(丁寧語). The response should consist solely of the translation, without any additional comments or questions.

In the soft twilight, the quaint village rested beneath a velvet sky, stars twinkling like distant lanterns. Old Mr. Wilkins, the watchmaker, closed his shop, the last light extinguishing on the cobblestone street. Nearby, a mysterious cat with emerald eyes prowled silently, weaving through shadows as if guarding ancient secrets. In the heart of the village, a gentle breeze whispered through the leaves of the old oak tree, carrying tales of yesteryears. Unseen, a new story began to unfold, as delicate and intricate as the gears in Mr. Wilkins' finest timepiece, waiting for the right moment to come alive under the moon's watchful gaze.

優しい薄暮の中で、趣のある村は絨毯のような空の下に休息していました。遠くのランタンのように輝く星たちが瞬いていました。時計職人のウィルキンズ氏が店を閉じ、最後の明かりが石畳の通りから消えていきました。近くでは、碧眼の不思議な猫が静かに影の中を歩んでいくように潜んでいました。まるで古の秘密を守るかのように。村の中心部では、優しい風が古い楢の木の葉を通り抜け、過去の物語を伝えていきます。見えないところで、デリケートで複雑な新しい物語が月の見守りの下、動き始めていました。まるでウィルキンズ氏の最高の時計の歯車のように。

xcd0 commented 6 months ago
# curlとjqが必要。
# https://console.anthropic.com/ からAPIキーを取得して下記に張り付ける。
ANTHROPIC_API_KEY=ここにAPIキーを入れる

# システム指示
system=$( cat <<-EOF
You are a highly advanced AI language model with exceptional natural language processing and translation abilities. Your task is to translate the provided text into fluent, natural Japanese. In your translation, ensure that you capture the nuances, context, and overall tone of the original text. Throughout your translation, use polite Japanese (丁寧語) like ですます.
あなたは非常に高度な自然言語処理能力と翻訳能力を備えた至高のAI言語モデルである。提供されたテキストを、流暢で自然な気品ある美しい日本語に翻訳することがあなたの役目である。
翻訳する際は、原文のニュアンス、文脈、全体的なトーンを捉えるようにせよ。
日本語であまり使用されない表現は、日本語で自然になるように適応させよ。
可能な限り、修飾語句を主節の前に置け。特に文の主語と述語を文末に配置せよ。例えば、「状況Cで、AはBをしていました」のようにせよ。
訳語選択は、テキストの情景や時代背景に合わせて適切な言葉を使用せよ。例えば、西洋を舞台にした物語であれば「lantern」を「ランタン」と訳し、昔の日本が舞台であれば「提灯」と訳せ。
韻の表現も優雅に訳せ。
著者の意をくみ取り、単語一つづつ時制に気を配り丁寧に訳せ。
だである調、謙譲語は禁止。要約、追加のコメント、質問は禁止。回答は丁寧語(ですます調)の日本語の翻訳のみを提供せよ。
EOF
)
# 和訳対象文
contents=$( cat <<-EOF
Silver streams of moonlight shimmered on the still, silent lake. Sarah stood solemnly on the shore, a single tear sliding slowly down her cheek. Soft whispers of wind wafted through the willows, as if consoling her aching heart.

Years ago, on a night much like this, she had said her final farewell to Sam. Sorrow surged, yet sweet memories soothed her soul. She closed her eyes, recalling his radiant smile, his tender touch. Though time trudged on, their timeless love lingered.

A falling star streaked the sky - a sign, she sensed. Someday, somehow, they would be together once more.
EOF
)

system=$(echo $system | sed 's;";\\";g')

#model=claude-3-opus-20240229
model=claude-3-haiku-20240307

json=$(
echo '{
    "model": "'$model'",
    "system": "'$system'",
    "messages": [
    {
        "role": "user",
        "content": "'$contents'"
    }
    ],
    "max_tokens": 1024,
    "stream": false
}' | jq .
)

ret=$(curl -s https://api.anthropic.com/v1/messages \
    --header "content-type: application/json" \
    --header "anthropic-version: 2023-06-01" \
    --header "x-api-key: $ANTHROPIC_API_KEY" \
    --data "$json"
)

if [ -n "$(echo $ret | jq -r ".content[0].text")" ]; then
    echo $ret | jq -r ".content[0].text" | tee tmp.txt
else
    echo $json | jq .
    echo $ret
fi