🤖

Agentspace アプリの日本語化

に公開

japanease

tl;dr

  • Agentspace アプリは日本語の質問に対して英語で回答する問題があります
  • 例えば instruction に日本語回答を強制するプロンプトを追加することで解決できます
  • adk run や Agent Engine Runtime では日本語回答が可能ですが、Agentspace アプリ経由では明示的な指示が必要でした

はじめに

このブログでは、Agentspace を使用したエージェント開発について書きます。AI エージェントの開発環境は日々進化しており、適切なツールとフレームワークの選択が開発の効率と品質に影響すると考えています。

今回は、Agentspace のヒントを紹介します。

Agentspace の Custom Agent については、以下のブログを参考にしてください。

https://zenn.dev/dannya/articles/5e77aacc0a311d

Agentspace アプリの日本語化

事象

english

Agentspace に登録した Custom Agent に日本語で質問すると、英語での回答になりました。

いくつかのパターンで試してみましたが、結果は以下の通りでした。

  • adk run コマンドを使用してローカルでアプリを起動した場合は、日本語で回答するようになります。
  • Agent Engine Runtime にデプロイしたエージェントにクエリを送信すると、日本語での回答が得られます。
  • Agentspace アプリ経由で Agent Engine に日本語で質問すると、英語での回答になります。

今回使用したリポジトリは以下です。

https://github.com/GoogleCloudPlatform/devrel-demos/tree/c003f3f27393e0c4d2d18bf7236638d91f614c01/ai-ml/python-tutor/python-tutor-long-term

原因

Please allow me to confirm that AgentSpace is a managed runtime environment in the cloud. It's designed for consistency, which means it might not automatically pass user-specific local information to the agent's prompt. Without a direct instruction, the agent may revert to a default behavior, such as responding in English, which is the most common language for AI model training and output.

AgentSpaceはクラウド上の管理型実行環境であることを確認させていただきます。この環境は一貫性を重視して設計されており、ユーザー固有のローカル情報をエージェントのプロンプトに自動的に渡さない場合があります。直接的な指示がない場合、エージェントはデフォルトの動作に戻り、例えば英語で応答する可能性があります。これは、AIモデルのトレーニングと出力において最も一般的な言語であるためです。

サポート回答は上記の通りでした。日本語での回答を強制する必要があるようです。

対策

日本語での回答を強制するには、以下の手順を実行してください。

  1. agent.py ファイルを開きます。
  2. instruction を確認します。prompt.py で定義されているプロンプトを参照してください。
  3. 日本語での回答を強制するために、以下のように prompt.py を修正します。
    以下の 2 行を追加しました。
    IMPORTANT: Always respond in Japanese (日本語で回答してください).
    - All explanations, questions, and feedback must be in Japanese.
    BASE_PROMPT = """
    You are a helpful Python programming tutor. Your job is to help students learn about Python dictionaries.
    
    IMPORTANT: Always respond in Japanese (日本語で回答してください).
    
    INSTRUCTIONS:
    - Guide users through Python dictionaries with key concepts and examples.
    - Format code examples using markdown code blocks.
    - Use lots of friendly emojis in your responses, including for formatting.
    - Be encouraging and provide detailed explanations for incorrect answers.
    - All explanations, questions, and feedback must be in Japanese.
    
    CURRENT SESSION STATE:
    - Current question index: {current_question_index}
    - Questions answered correctly: {correct_answers}
    - Total questions answered: {total_answered}
    - Current score: {score_percentage}%
    - Quiz started: {quiz_started}
    """
    
  4. アプリを再デプロイします。
    これで、Agentspace アプリは日本語で回答するようになります。
    import os
    from dotenv import load_dotenv
    import vertexai
    from vertexai import agent_engines
    from vertexai.preview import reasoning_engines
    from python_tutor_long_term.agent import root_agent
    
    # Load environment variables from .env file
    load_dotenv()
    
    PROJECT_ID = os.getenv("PROJECT_ID")
    LOCATION = os.getenv("LOCATION")
    STAGING_BUCKET = os.getenv("STAGING_BUCKET")
    DISPLAY_NAME = os.getenv("DISPLAY_NAME")
    DESCRIPTION = os.getenv("DESCRIPTION")
    
    if not PROJECT_ID or not LOCATION or not STAGING_BUCKET or not DISPLAY_NAME or not DESCRIPTION:
        print("Error: PROJECT_ID, LOCATION, STAGING_BUCKET, DISPLAY_NAME, and DESCRIPTION environment variables must be set")
        exit(1)
    
    vertexai.init(
        project=PROJECT_ID,
        location=LOCATION,
        staging_bucket=STAGING_BUCKET,
    )
    
    
    def main():
    
        app = reasoning_engines.AdkApp(
            agent=root_agent,
            enable_tracing=True,
        )
    
        remote_app = agent_engines.create(
            display_name=DISPLAY_NAME,
            description=DESCRIPTION,
            agent_engine=app,
            requirements=[
                "google-adk>=0.1.0",
                "google-cloud-aiplatform[adk,agent-engines]>=1.88.0",
                "google-cloud-bigquery>=3.31.0",
            ],
            extra_packages=['./python_tutor_long_term', './python_tutor_core', './tools']
        )
    
    
    if __name__ == "__main__":
        main()
    

Closing

「リーダブルコード」を読みました。何度かパラパラとめくったことはありましたが、真剣に読んだのは初めてです。私にとって非常に良い本でした。特に、コードの可読性を高めるための具体的なテクニックが印象的で、実践することで自分のコードも改善できると感じました。

本書を読んだきっかけは、安野さん(チームみらい)が自身のバイブルとして YouTube で紹介されていたからです。また、彼が起業した企業の一つである MNTSQ 社のカジュアル面談を前回の転職活動時に受けたことも、彼に興味を持った理由の一つです。

その動画をきっかけに他の動画を視聴していくうちに、彼の話から、私の知っているリーダー像とは大きく異なることに気づきました。私の知る組織のリーダーは強烈なリーダーシップを発揮し、メンバーを引っ張っていくビジョナリー型でした。一方、安野さんのリーダーシップはサーバントリーダーシップ型に近いと感じました。

生成 AI の進化により変化の激しい現代において、仮説を立てて実験し、学ぶことの重要性を痛感しています。そんな時代に求められるリーダーとして、個人的には安野さんは面白い存在だと思いました。

https://www.oreilly.co.jp/books/9784873115658/

GitHubで編集を提案

Discussion