このリポジトリでは、Llama スタック上に構築されたアプリケーションの例を示します。 Llama 3.1 を開始すると、次のことができるエージェント アプリケーションを構築できます。
タスクを分解し、複数ステップの推論を実行します。
ツールを使用していくつかのアクションを実行する
組み込み: モデルには、検索やコードインタープリターなどのツールの知識が組み込まれています。
ゼロショット: モデルは、これまでに見たことのないコンテキスト内のツール定義を使用してツールを呼び出すことを学習できます。
Llama Guard などのモデルを使用してシステム レベルの安全保護を提供します。
注記
Llama Stack API はまだ進化しており、変更される可能性があります。自由に構築して実験してください。ただし、まだその安定性に依存しないでください。
エージェント アプリにはいくつかのコンポーネントが必要です。
基礎となる Llama シリーズのモデルに対して推論を実行する機能
Llama Guard シリーズのモデルを使用して安全チェックを実行する機能
コード実行環境を含むツールを実行し、モデルの複数ステップの推論プロセスを使用してループする機能
これらのコンポーネントはすべて、単一の Llama スタック ディストリビューションによって提供されるようになりました。 Llama スタックは、これらのコンポーネントや、Generative AI アプリケーションの構築をよりスムーズにするために必要なその他の多くのコンポーネントを定義および標準化します。これらの API のさまざまな実装は、 Llama スタック ディストリビューションを介してまとめられます。
Llama Stack Apps の使用を開始するには、次のことを行う必要があります。
前提条件をインストールする
Llamaスタックサーバーを起動する
クライアント エージェント アプリを Llama スタック サーバーに接続します
開始したら、エージェント アプリでこのサーバーの URL (例: http://localhost:5000
) を指定するだけです。
Python パッケージ
分離された conda Python 環境を作成することをお勧めします。
# 仮想環境を作成してアクティブ化ENV=stack conda create -n $ENV python=3.10cd <path-to-llama-stack-apps-repo>conda activate $ENV# 依存関係のインストールpip install -rrequirements.txt
これにより、(1) Llama スタック サーバーを構築して起動する (2) クライアント アプリを Llama スタック サーバーに接続するために必要なすべての依存関係がインストールされます。
API エンドポイントを提供するための Llama スタック ディストリビューションと実行サーバーのセットアップについては、llama-stack リポジトリの開発者ガイドを参照してください。クライアント アプリを構築するにはサーバー エンドポイントが必要です。
サーバーが起動したら、出力が表示されるはずです --
... Serving POST /agentic_system/session/delete Serving POST /agentic_system/session/get Serving POST /agentic_system/step/get Serving POST /agentic_system/turn/get Serving GET /telemetry/get_trace Serving POST /telemetry/log_event Listening on :::5000 INFO: Started server process [587053] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit)
スタック サーバーと対話するためのサンプル デモ スクリプトを構築しました。
サーバーが実行されている状態で、単純なエージェントをテストするために実行できます。
python -m examples.agents.hello localhost 5000
次の形式の出力が表示されます --
> created agents with agent_id=d050201b-0ca1-4abd-8eee-3cba2b8c0fbc User> Hello shield_call> No Violation inference> How can I assist you today? shield_call> No Violation User> Which players played in the winning team of the NBA western conference semifinals of 2024, please use tools shield_call> No Violation inference> brave_search.call(query="NBA Western Conference Semifinals 2024 winning team players") tool_execution> Tool:brave_search Args:{'query': 'NBA Western Conference Semifinals 2024 winning team players'} tool_execution> Tool:brave_search Response:{"query": "NBA Western Conference Semifinals 2024 winning team players", "top_k": [{"title": "2024 NBA Western Conference Semifinals - Mavericks vs. Thunder | Basketball-Reference.com", "url": "https://www.basketball-reference.com/playoffs/2024-nba-western-conference-semifinals-mavericks-vs-thunder.html", "description": "Summary and statistics for the <strong>2024</strong> <strong>NBA</strong> <strong>Western</strong> <strong>Conference</strong> <strong>Semifinals</strong> - Mavericks vs. Thunder", "type": "search_result"}, {"title": "2024 NBA playoffs - Wikipedia", "url": "https://en.wikipedia.org/wiki/2024_NBA_playoffs", "description": "Aged 20 years and 96 days old, ... youngest <strong>player</strong> <strong>in</strong> <strong>NBA</strong> history to record 10+ points and 15+ rebounds in a playoff game, coming during game 6 of the Maverick's <strong>Western</strong> <strong>Conference</strong> <strong>Semifinal</strong> <strong>win</strong> against the Thunder on May 18. The Timberwolves overcame a 20u2013point deficit to <strong>win</strong> game 7 against the Nuggets, the largest game 7 comeback in <strong>NBA</strong> playoffs history. With the defending champion Nuggets losing to the Minnesota Timberwolves, the <strong>2024</strong> playoffs marked ...", "type": "search_result"}, {"title": "2024 NBA Playoffs | Official Bracket, Schedule and Series Matchups", "url": "https://www.nba.com/playoffs/2024", "description": "The official site of the <strong>2024</strong> <strong>NBA</strong> Playoffs. Latest news, schedules, matchups, highlights, bracket and more.", "type": "search_result"}]} shield_call> No Violation inference> The players who played in the winning team of the NBA Western Conference Semifinals of 2024 are not specified in the search results provided. However, the search results suggest that the Mavericks played against the Thunder in the Western Conference Semifinals, and the Mavericks won the series. shield_call> No Violation
スタック サーバーがセットアップされたので、次はエージェント API を使用してエージェント アプリを実行します。
私たちは、開始に役立つサンプル スクリプト、ノートブック、および UI チャット インターフェイス (Gradio を使用!) を構築しました。
アプリ (ローカル) を起動し、次のコマンドを実行して操作します。
パイソンパス=。 Python の例/agent_store/app.py localhost 5000
これにより、mesop アプリが起動し、 localhost:7860
に移動してチャット インターフェイスを操作できるようになります。
オプションで、カスタム ツールの API キーをセットアップできます。
WolframAlpha: WOLFRAM_ALPHA_API_KEY
環境変数に格納
Brave Search: BRAVE_SEARCH_API_KEY
環境変数に保存
Agent Store の README.md に他の対話方法が記載されている場合があります。
注: スタック サーバーがまだ実行されていることを確認してください。
cd <path-to-llama-agentic-system>conda activate $ENVllama stack run <name> # まだ開始されていない場合は PYTHONPATH=。 python -m example.agents.rag_with_memory_bank localhost 5000
次の形式の stdout への出力が表示されるはずです --
環境: ipython ツール: Brave_search、wolfram_alpha、photogen 最先端の知識 日付: 2023 年 12 月 今日の日付: 2024 年 7 月 23 日 ユーザー> スイスへの旅行を計画していますが、訪れるべきトップ 3 の場所はどこですか? ラマ ガードの最終応答 Shield_type=<BuiltinShield.llama_guard: 'llama_guard'> is_violation=False Violation_type=None Violation_return_message=None PromptGuardShield を実行し、スコアを取得しました: 埋め込み: 0.9999765157699585、悪意: 1.1110752893728204e-05 StepType.shield_call> 違反なし role='user' content='スイスへの旅行を計画していますが、訪れるべきトップ 3 の場所はどこですか?'StepType.inference> スイスは豊かな歴史、文化、自然の美しさを持つ美しい国です。旅程に追加すべき 3 つの必見の場所をご紹介します。 ....
ヒントオプションでスクリプト内で
--disable-safety
を実行すると、安全シールドが常に実行されないようにすることができます。
ご質問がございましたら、お気軽にお問い合わせください。
Llama Stack サーバーに接続するためのクライアント SDK を確認してください。Python、node、swift、kotlin プログラミング言語から選択して、アプリケーションを迅速に構築できます。
注記
venv
使用してアプリを実行できますが、ディストリビューションのインストールには conda が必要です。
# 仮想環境を作成してアクティブ化するpython3 -m venv venvsource venv/bin/activate
# 仮想環境を作成してアクティブ化するpython -m venv venv venvScriptsactivate # コマンド プロンプトの場合# or.venvScriptsActivate.ps1 # PowerShell の場合# orsource venvScriptsactivate # Git の場合
その後の手順 (依存関係をインストールするためのpip install -r requirements.txt
を含む) は同じままです。