見出し画像

生成AIを使ったサービスを運営しています

どうも、えいやです。

今日は、タイトル通り「生成AIを使ったサービスを運営しています」と言いたいためだけに作る簡単なサービスをGWの初っ端にリリースした話をします

リリースしたサービスについて

今回リリースしたのは毎日、短い英語の小説を生成し、それについての5問の5択問題を作るサービスです

生成AIは、まだまだ日本語に強いものが少ないので、生成する対象の文章を英語に限定することでギリギリ許せるレベルのサービス品質を狙っていきます

以下では、どのようにして英文と問題文を生成しているのかを、手順を追って説明していきます

生成AIを動かす

今回のサービスでは、生成AIを手持ちの M1 Mac Book Air 上で簡単に動かせる、ollama を使って文章生成機能を作ります。ローカルで動かすことでクラウド利用料金その他を浮かせます

リンクからDLするなり、Brewを使って ollama をインストールしたあとは、生成AIの本体となるモデルを使えるようにします

ollama pull llava
ollama pull llama3

今回使うモデルは、小説の生成に強いとされている llava および Meta社がリリースしている llama3 を使ってみます

ollama start

として、ollama のサーバーを立ち上げておきます

小説の生成をしてみる

llava モデルに対して、”日本のゴールデンウィークにちなんだ短いSF小説を作成して”と指示してみましょう

ollama run llava \
  "create a 50 words short sf novel that is after japan's golden-week holiday" \
  | tee /tmp/llava.gen.txt

Golden Week was the busiest time of the year for the people of Japan. It was a week filled with holidays and festivals, where everyone would take off from work and school to enjoy themselves.

But this year was different. A mysterious alien spaceship had landed in Tokyo during the peak of the holiday season. The aliens were unlike anything humanity had ever encountered before. They had a strange obsession with the color gold, and they seemed to be searching for something.

As the people of Japan watched in amazement, the aliens began to search through the city, leaving a trail of destruction behind them. It was up to a group of brave citizens to stop the aliens and uncover their true motives before it was too late.

generated by llava model

ゴールデンウィークは、日本の人々にとって一年で最も忙しい時期だった。祝日とお祭りに満ちた一週間で、誰もが仕事や学校を休んで楽しむ時期だった。

しかし、今年は違った。謎の異星人の宇宙船が、休暇シーズンのピークで東京に着陸したのだ。その異星人たちは、人類がこれまでに出会ったものとは全く異なる存在だった。彼らは金色に奇妙な執着を持ち、何かを探しているようだった。

皆が驚いて見ていると、エイリアンは破壊の痕跡を残しながら街を捜索し始めた。手遅れになる前に、勇敢な市民たちが、異星人を阻止し、真実を解き明かさなければ──!

generated by llava model & translated by Chat-GPT3

多少、指示に従ってくれないところや物語の構成に違和感はありますが、まぁ英語の勉強として読む文章として内容を気にしなければ問題ないでしょう

小説を読解させて会話を作ってみる

あとで小説に基づいた問題を作りたいので、AIが文章の内容を読み取れるのかを確認してみましょう

生成した小説を llama3 モデルを使って読解させ、それについての会話の例文を作ってみます

novel=$(cat /tmp/llava.gen.txt)
ollama run llama3 ""Novel:\n\`\`\`\\n${novel}\n\`\`\`\ncreate conversation of three children about the Novel"

**Rina**: Oh my gosh, have you guys heard about the alien
invasion in Tokyo?

**Taro**: Yeah! My older brother told me all about it. He
said that these aliens are obsessed with gold and they're
causing chaos everywhere!

**Sakura**: That's so cool! I wish I could see them for
myself. But at the same time, I'm a little scared. What if
they hurt someone we love?

**Rina**: Yeah, that's what my mom is worried about too. She
says that it's up to us adults to figure out what's going on
and stop the aliens before things get worse.

**Taro**: But what if the kids are the ones who can help
save the day? Maybe our generation has some special powers
or something!

**Sakura**: That would be amazing! I wish we could go on a
secret mission to uncover the aliens' secrets. Who knows,
maybe we'll even find out what they're searching for that's
so important to them.

**Rina**: Ha ha, that would be so cool! But for now, let's
just enjoy the excitement of it all and see what happens
next. Maybe we'll even get to meet some brave citizens who
are going to stop the aliens!

**Taro**: Yeah, and maybe we'll learn some new tricks from
them too!

**Sakura**: Agreed! Let's keep our fingers crossed that
everything turns out okay in Tokyo.

generated by llama3 model

トミー: "うわっ、すごい!ゴールデンウィークに東京にエイリアンが着陸するって?!"

サラ: "ねえ、そうなの!それに、彼らは金に夢中なの?変だよね。"

ジミー: "うん、彼らは何を探してたんだろう?世界で一番大きな金の塊を見つけたかったのかな?"

トミー: "はは、そうかもね!でも、それよりも深刻だったと思うな。彼らは建物を破壊してたんだ。ゴールデンウィーク中に実際に何が起きたのかもっと知りたいね。"

サラ: "うん、次に何が起きたか知りたいよね!勇者たちは彼らを止めたのかな?大きな戦いはあったのかな?"

ジミー: "きっとすごいアクションシーンがあったんだろうな!もしもエイリアンが僕たちの街に来て、何かを探し始めたらどうなるか想像してみようよ!"

トミー: "きゃっ、それはちょっとリアルすぎるかも!でも、そうだね、想像してみよう。もしかしたら、近所の他の子供たちと協力して彼らを止めなきゃいけないかもしれないね。"

サラ: "それ、すごく楽しそう!私たちも自分たちだけの物語を書いて、かっこいいエイリアンの戦いをたくさん追加しようよ!"

ジミー: "うん!それに、すごい宇宙船とエイリアンを描くこともできるね!"

generated by llama model & translated by Chat-GPT3

文脈に沿った会話ができていそうに見えますね

サービスではこの会話の内容からも出題をしていますが、まずこの会話の内容をみて、生成が正常だったかどうかを確認していたりします。問題だけをみていると生成される文章が短いので、異常の判断がやりづらかったりします

問題を作る

では問題を作ってもらいましょう。小説を読んで、五肢択一問題を5問生成するように指示をします

ollama run llama3 "Novel:\n\`\`\`\\n${n}\n\`\`\`\nread the novel and create five questions to test the English reading comprehension skills used in ESL classes. The format for answering the questions should be a one-choice format with five options to choose from."

**Question 1**
What is special about Golden Week in Japan?

A) It's a time of war and conflict.
B) It's a busy time filled with holidays and festivals.
C) It's a time when everyone works extra hours.
D) It's a time when schools and businesses are closed.

Answer: B) It's a busy time filled with holidays and
festivals.

**Question 2**
What is unusual about the alien spaceship that landed in
Tokyo?

A) It was small and didn't cause any trouble.
B) It had an obsession with the color blue.
C) It had a strange obsession with the color gold.
D) It came from a distant planet called Mars.

Answer: C) It had a strange obsession with the color gold.

**Question 3**
What did the aliens do as they searched through Tokyo?

A) They left behind a trail of beauty and art.
B) They helped clean up the city.
C) They searched for something, leaving destruction behind.
D) They decided to stay in Japan forever.

Answer: C) They searched for something, leaving destruction
behind.

**Question 4**
Who had to take action to stop the aliens?

A) The Japanese government
B) A group of brave citizens
C) The aliens themselves
D) The United Nations

Answer: B) A group of brave citizens

**Question 5**
What was the aliens' true motive?

A) They were searching for a new home.
B) They wanted to learn about human culture.
C) They were trying to communicate with humans.
D) The novel doesn't mention their true motive.

Answer: D) The novel doesn't mention their true motive.

generated by llama3 model

**質問1
日本のゴールデンウィークは何が特別なのでしょうか?

A) 戦争と紛争の時期である。
B) 祝日やお祭りで忙しい時期である。
C)みんなが特別に働く時期である。
D) 学校や会社が休みになる。

答え B) 祝日やお祭りで忙しい時期である。

**質問2
東京に着陸したエイリアンの宇宙船について珍しいことは?

A) それは小さく、何の問題も起こさなかった。
B) 青色に執着していた。
C) 金色に奇妙なこだわりを持っていた。
D) 火星という遠い星からやってきた。

答え C) 金色に奇妙なこだわりを持っていた。

**質問3
異星人は東京を探索しながら何をしたでしょう?

A) 美と芸術の痕跡を残した。
B) 彼らは街の清掃を手伝った。
C) 彼らは何かを探し、破壊を残した。
D) 彼らは永遠に日本に留まることを決めた。

答え C) 彼らは何かを探し、破壊を残した。

**質問4
宇宙人を止めるために誰が行動を起こさなければならなかったか?

A) 日本政府
B) 勇敢な市民グループ
C) 宇宙人自身
D) 国連

答え B) 勇敢な市民グループ

**質問5
宇宙人たちの本当の動機は?

A) 新しい故郷を探していた。
B) 人間の文化を知りたかった。
C) 人間とコミュニケーションを取ろうとしていた。
D) 小説は彼らの本当の動機について触れていない。

答え D) 小説は彼らの本当の動機について触れていない。

generated by llama model & translated by DeepL.com

出題の方も大体良さそうに見えますね

2問目については、実際には、宇宙人の特徴が回答になっていますが、宇宙船のことについて聞いてしまっています。英文を読めていれば正答できる範囲なので、今回の利用用途では、まぁヨシです

特に最後の問題は触れられているかどうかを問う問題まで生成できるとは驚きですね

サービスとしてリリースする

上記までで、「生成AIを使った」という主目的は十分と思うのであとは自動化するスクリプトを書いたり、生成結果を整形してサービスに使えるデータにします

詳細の説明は省きますが、この辺りのデータ操作はほぼ jq と sed コマンドをつかってお手軽に実装しています

サービスをWebページとして公開するために、今回は Deno と Fresh をつかってアプリを作りました

Deno をつかうと Deno Deploy というサービスを使って簡単に公開できるので細々としたサービスについては最近はこちらがお気に入りです

まとめ

  • ollama を使うと簡単にローカルで生成AIを試せる

  • llavaモデルの英文の生成は、及第点

    • 内容の巧拙や展開の違和感を問わない、多読本のように単に英語が読みたいなどの需要に対しては、利用価値がある

  • llama3モデルの文脈理解力は、かなり出来が良い

    • 文章を読ませて、その内容についての会話の例を作り出させることができる

    • 文章に基づいた問題を出題させる場合についても、ほとんど違和感がない

  • さらっとWebアプリを作って公開するのに Deno エコシステムが便利

この記事が気に入ったらサポートをしてみませんか?