対話

Web


Warning: Attempt to read property "site_name" on bool in /home/goodhokkaido/sixwheel.net/public_html/muguruma/wp/wp-content/themes/frame/functions.php on line 208

Warning: Attempt to read property "title" on bool in /home/goodhokkaido/sixwheel.net/public_html/muguruma/wp/wp-content/themes/frame/functions.php on line 211

Warning: Attempt to read property "description" on bool in /home/goodhokkaido/sixwheel.net/public_html/muguruma/wp/wp-content/themes/frame/functions.php on line 217

Warning: Attempt to read property "site_name" on bool in /home/goodhokkaido/sixwheel.net/public_html/muguruma/wp/wp-content/themes/frame/functions.php on line 208

Warning: Attempt to read property "title" on bool in /home/goodhokkaido/sixwheel.net/public_html/muguruma/wp/wp-content/themes/frame/functions.php on line 211

Warning: Attempt to read property "description" on bool in /home/goodhokkaido/sixwheel.net/public_html/muguruma/wp/wp-content/themes/frame/functions.php on line 217

GeminiのAPIを使って、チャットアプリをReactでつくる

Gemini AIの無料枠で何か作りたいと思い、jsを使って作る方法を探すと以下のようないい感じのサイトが検索に引っかかりました。これらを参考に、何を作ろうかなと考えたんですが、対話相手が一人だと答えの差がわからないから、二人のAIを登場させてることに。個性が異なる方がいいので、modelを変えてやってみました。いつものように、エックスサーバー上で動かすので、Reactで書きます。

Gemini Proと会話できるSlackBotをAWSサーバレス上に作る

返事の仕方は普通な感じだと面白くないので、ちょっと変化を加えることに。距離感を縮めるために、呼び捨てで呼ばせたり、人格をジャイアンにしたりして試行錯誤。下記のような感じで、プロンプト内で制限を加えました。ジャイアンが二人いるのは不思議な感覚ですが、ジャイアンらしい物言いと返答の奇妙さが苦笑を生むので、これはありだなと思います。ただ、5回も対話すれば飽きますね。実用的かというと、なんら実用的な感じがしません。

import { GoogleGenerativeAI } from '@google/generative-ai'
import type { ChatMessage } from '@/types'

export default async function GetGemini(
  apikey: string,
  myMessage: string,
  place: string,
  nickname: string,
  historyA: ChatMessage[],
  historyB: ChatMessage[]
) {
  try {
    const genAI = new GoogleGenerativeAI(apikey)

    const sendGemini = async (
      modelName: string,
      _prompt: string,
      history: ChatMessage[]
    ): Promise<string> => {
      const model = genAI.getGenerativeModel({ model: modelName })
      const chat = model.startChat({
        history: history,
      })

      history.push({ role: 'user', parts: [{ text: _prompt }] })
      const { response } = await chat.sendMessage(_prompt)
      history.push({ role: 'model', parts: [{ text: response.text() }] })

      return response.text()
    }

    const sendGeminiMessage = `私は${place}の${nickname}。あなたはジャイアンです。乱暴なガキ大将だが、友情に厚い男の子。学校ではいつもいばりちらしているものの、親には頭が上がらず、妹のジャイ子にはとてもやさしい。名前を呼ぶ時は呼び捨てで書くこと。その設定で、次の文章に100文字以内で回答してください。${myMessage}`

    const aiMessegeResponseA = await sendGemini(
      'gemini-1.5-flash',
      sendGeminiMessage,
      historyA
    )
    const aiMessegeResponseB = await sendGemini(
      'gemini-2.0-flash-lite',
      sendGeminiMessage,
      historyB
    )

    return {
      messageA: aiMessegeResponseA,
      messageB: aiMessegeResponseB,
    }
  } catch (error) {
    console.error('AIメッセージの取得に失敗しました:', error)
  }
}

あとモデルによっても返答が結構違いました。ジャイアンなのに、ジャイアンになりきれなかったり、言葉のつながりで逆の意味に解釈したり。いい感じのバカっぽさはgemini-1.5-flashが良かったです。

コメント