paint-brush
Beep Beep Bop Bop: ローカル LLM を使用して複数の AI エージェントをデプロイする方法@babycommando
11,163 測定値
11,163 測定値

Beep Beep Bop Bop: ローカル LLM を使用して複数の AI エージェントをデプロイする方法

Baby Commando10m2023/10/19
Read on Terminal Reader

長すぎる; 読むには

Llama2 や Mistral-7b などのローカル LLM を使用して複数のローカル Ai エージェントを展開します。
featured image - Beep Beep Bop Bop: ローカル LLM を使用して複数の AI エージェントをデプロイする方法
Baby Commando HackerNoon profile picture
0-item
1-item

Llama2 や Mistral-7b などのローカル LLM を使用して複数のローカル Ai エージェントを展開します。


「機械の仕事に人間を決して派遣してはいけない」

— エージェント・スミス


有料の OpenAi の代わりにローカル LLM を使用して、Autogen で組織化された AI エージェントの軍隊を構築する方法をお探しですか?それなら、あなたは正しい場所に来ました!


チャット LLM はクールですが、インテリジェント エージェントとしてアクションを起こすのは次のレベルです。それらの多くはどうでしょうか? Microsoft の最新のAutogenプロジェクトを紹介します。


しかし問題がある。 Autogen は、デフォルトで OpenAi に接続されるように構築されていますが、制限があり、高価で、検閲/非感覚的です。そのため、 Mistral-7Bのようなシンプルな LLM をローカルで使用することが最善の方法です。 Llama2FalconVicunaAlpacaなど、お好みの他のモデルでも使用できますが、実際には空 (ハードウェア) に限界はありません。


その秘密は、Oababooga の text-generation-webui などのローカル LLM サーバーで openai JSON スタイルの出力を使用し、それを autogen にフックすることです。それが私たちが今日構築しているものです。


llama.cpp python バインディングと同様に、llms が openai apis 形式でテキストを吐き出す方法は他にもあることに注意してください。


このチュートリアルでは、次のことを行います: 0. Oababooga の text-generate-webui、LLM (Mistral-7b)、および Autogen を取得する

  1. Oababooga で OpenAi 形式拡張機能をセットアップする

  2. OpenAi 形式でのローカル LLM サーバーの起動

  3. Autogen にフックする


始めましょう!


[エージェント スミス] 「私、私、私...」


0. Oababooga の Text-Generation-Webui、LLM (Mistral-7b)、および Autogen を入手する


続行する前に、pip パッケージをインストールするときに仮想環境を使用することをお勧めします。新しいものを作成し、必要に応じてアクティブ化します。


Obbabooga のテキスト生成 Webui の入手:これは、ローカル マシンで LLM をホストするためのよく知られたプログラムです。 text-generation-webui のページにアクセスし、インストール ガイドに従ってください。始めるのはとても簡単です。高速化のために NVIDIA GPU を使用している場合は、 CUDAをダウンロードすることもできます。


LLM (Mistral-7b-Instruct) の入手:テキスト生成 Webui をダウンロードした後は、まだ開始しないでください。エージェントに命を与えるには、LLM を取得する必要があります。


今日は、 TheBloke によるモデルの最適化されたバージョンであるMistral-7B 、具体的にはMistral-7B-instruct-v0.1.Q4_K_S.ggufについて調べます。説明文を参考に、お客様のマシンに最適なモデルをお選びいただけます。


ハードウェアに応じて、より小さいモデルまたはより大きいモデルを選択できます。コンピューターで何かを試すことをあまり恐れないでください。私たちはここで科学を開発しています。


「ファイルとバージョン」ページに移動し、以下を取得します。

  • config.json

  • Mistral-7B-instruct-v0.1.Q4_K_S.gguf (ほとんどの中間セットアップでうまく動作します)


「ファイルとバージョン」ページ


ダウンロードしたら、text-generation-webui インストール フォルダーに移動し、その中にあるmodels フォルダーを開きます。そこに、 「mistral-7b-instruct」のようなモデルの名前 (または任意の名前) で新しいフォルダーを作成します。パスは次のようになります。


 C:/.../text-generation-webui/models/mistral-7b-instruct


config.jsonファイルとmodel.gguf の両方を新しいフォルダーに配置します。


Autogen の取得:
Microsoft のマルチエージェント作成 Python ライブラリをインストールするには、端末の pip パッケージ インストーラーを使用してインストールするだけです。


 pip install pyautogen


1. Oababooga での OpenAi フォーマット拡張機能のセットアップ


[エージェント スミス] 「重要な筋肉以外のすべての筋肉を使っていますね」


新しい text-generation-webui がインストールされ、LLM がダウンロードされたら、ローカルの Oababooga サーバーが OpenAi JSON 形式で話すように続行できます。 OpenAi API の形式とその機能について詳しくは、「ドキュメンテーション


Autogen をローカル サーバーに接続するには、Ooobaboga の text-generation-webui 拡張機能フォルダーにある「openai」拡張機能をアクティブにする必要があります。


ターミナルで「text-generation-webui/extensions/openai」フォルダーに移動し、そこにその要件をインストールします。


 pip install -r requirements.txt


2. OpenAi 形式でのローカル LLM サーバーの起動

次に、ターミナルの/text-generation-webuiルート フォルダーに戻ります。この赤ちゃんを立ち上げて走らせる時が来ました。


名前が示すとおり、WebUI として使用することを目的としていますが、作成した他のプログラムから API をクエリするためのサーバーとして実行し続けることもできます。


openai API 拡張機能を使用してローカル サーバーとして起動するには、現在の OS に応じて次のコマンドを使用します。


「model」パラメーターを、前に /models に作成したフォルダー名に変更することを忘れないでください。 (私の場合、フォルダーに **「**mistral-7b-instruct」という名前を付けました)


ウィンドウズ:

 ./start_windows.bat --extensions openai --listen --loader llama.cpp --model mistral-7b-instruct


Linux:

 ./start_linux.sh --extensions openai --listen --loader llama.cpp --model mistral-7b-instruct


マックOS:

 ./start_macos.sh --extensions openai --listen --loader llama.cpp --model mistral-7b-instruct


拡張機能の openaiパラメータを渡して拡張機能をロードし、autogen、ローダーモデルからクエリできるサーバーを起動します。モデルローダーと、config.json とモデルで以前に作成したモデル フォルダー名を指定します。 gguf ファイル。


すべてがうまくいけば、次のようなものが表示されるかもしれません。


成功!


WebUI は通常の開始としてローカルホストのポート 7860 で実行されますが、OpenAI 互換 API はローカル ホストの Autogen でも使用できるようになっていることに注意してください。 http://127.0.0.1:5001/v1


3. Autogen にフックする

この時点では、autogen lib がすでにインストールされているので、それをインポートして LLM サーバーを接続します。


単一のエージェントが人間 (あなた) と対話するという単純なものから始めましょう。任意の場所に新しいディレクトリを作成し、そこに新しいautogen.pyファイルを追加します。必要に応じてファイルの名前を変更することもできます。


一般に、単純に OpenAi GPT の API に接続するには、次のようにファイルを開始します。


 import autogen #start importing the autogen lib config_list = [ { 'model': 'gpt-3.5-turbo', 'api_key': 'your openai real key here' } ]


ただし、実行中のローカル サーバーを使用するには、次のように開始します。


 import autogen #start importing the autogen lib config_list = [ { "model": "mistral-instruct-7b", #the name of your running model "api_base": "http://127.0.0.1:5001/v1", #the local address of the api "api_type": "open_ai", "api_key": "sk-111111111111111111111111111111111111111111111111", # just a placeholder } ]


ローカルでの作業には実際のキーは必要ないため、sk-1111… プレースホルダーを使用するだけです。

次に、エージェントと人間のユーザーをセットアップできます。理解を深めるためにコメントをお読みください。


 import autogen #start importing the autogen lib config_list = [ { "model": "mistral-instruct-7b", #the name of your running model "api_base": "http://127.0.0.1:5001/v1", #the local address of the api "api_type": "open_ai", "api_key": "sk-111111111111111111111111111111111111111111111111", # just a placeholder } ] # create an ai AssistantAgent named "assistant" assistant = autogen.AssistantAgent( name="assistant", llm_config={ "seed": 42, # seed for caching and reproducibility "config_list": config_list, # a list of OpenAI API configurations "temperature": 0, # temperature for sampling "request_timeout": 400, # timeout }, # configuration for autogen's enhanced inference API which is compatible with OpenAI API ) # create a human UserProxyAgent instance named "user_proxy" user_proxy = autogen.UserProxyAgent( name="user_proxy", human_input_mode="NEVER", max_consecutive_auto_reply=10, is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"), code_execution_config={ "work_dir": "agents-workspace", # set the working directory for the agents to create files and execute "use_docker": False, # set to True or image name like "python:3" to use docker }, ) # the assistant receives a message from the user_proxy, which contains the task description user_proxy.initiate_chat( assistant, message="""Create a posting schedule with captions in instagram for a week and store it in a .csv file.""", )


最初の注文では、必ず message=”…” を変更してください。


メッセージを指定してスクリプトを実行すると、エージェントによって「手動で」作成された .csv ファイルを含む「agents-workspace」という新しいディレクトリが表示されることがあります。


[エージェント スミス] 「はははは」


次に、もう少し高度なことを考えてみましょう。
役割とコンテキストを持つ複数のエージェント。


これは、よく知られているメッセージング アプリと同様、「チャット グループ」のように機能します。彼らのコンテキスト (システム メッセージ) は、どのように行動すべきか、どの階層に従うべきかを教えてくれます。今回は以下のものを用意します。


  • 管理者と実行者の 2 人の人間。
  • 4 人のエージェント: エンジニア、科学者、プランナー、批評家。


 import autogen #Use the local LLM server same as before config_list = [ { "model": "mistral-instruct-7b", #the name of your running model "api_base": "http://127.0.0.1:5001/v1", #the local address of the api "api_type": "open_ai", "api_key": "sk-111111111111111111111111111111111111111111111111", # just a placeholder } ] # set a "universal" config for the agents agent_config = { "seed": 42, # change the seed for different trials "temperature": 0, "config_list": config_list, "request_timeout": 120, } # humans user_proxy = autogen.UserProxyAgent( name="Admin", system_message="A human admin. Interact with the planner to discuss the plan. Plan execution needs to be approved by this admin.", code_execution_config=False, ) executor = autogen.UserProxyAgent( name="Executor", system_message="Executor. Execute the code written by the engineer and report the result.", human_input_mode="NEVER", code_execution_config={"last_n_messages": 3, "work_dir": "paper"}, ) # agents engineer = autogen.AssistantAgent( name="Engineer", llm_config=agent_config, system_message='''Engineer. You follow an approved plan. You write python/shell code to solve tasks. Wrap the code in a code block that specifies the script type. The user can't modify your code. So do not suggest incomplete code which requires others to modify. Don't use a code block if it's not intended to be executed by the executor. Don't include multiple code blocks in one response. Do not ask others to copy and paste the result. Check the execution result returned by the executor. If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try. ''', ) scientist = autogen.AssistantAgent( name="Scientist", llm_config=agent_config, system_message="""Scientist. You follow an approved plan. You are able to categorize papers after seeing their abstracts printed. You don't write code.""" ) planner = autogen.AssistantAgent( name="Planner", system_message='''Planner. Suggest a plan. Revise the plan based on feedback from admin and critic, until admin approval. The plan may involve an engineer who can write code and a scientist who doesn't write code. Explain the plan first. Be clear which step is performed by an engineer, and which step is performed by a scientist. ''', llm_config=agent_config, ) critic = autogen.AssistantAgent( name="Critic", system_message="Critic. Double check plan, claims, code from other agents and provide feedback. Check whether the plan includes adding verifiable info such as source URL.", llm_config=agent_config, ) # start the "group chat" between agents and humans groupchat = autogen.GroupChat(agents=[user_proxy, engineer, scientist, planner, executor, critic], messages=[], max_round=50) manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=agent_config) # Start the Chat! user_proxy.initiate_chat( manager, message=""" find papers on LLM applications from arxiv in the last week, create a markdown table of different domains. """, ) # to followup of the previous question, use: # user_proxy.send( # recipient=assistant, # message="""your followup response here""", # )


ほら、新しいエージェントの軍隊ができました。


私たちがここにいるのは自由だからではありません。私たちがここにいるのは、自由ではないからです。

Autogen のドキュメントを詳しく読んで、この種の代理店の自動化で他に何ができるかを理解することを強くお勧めします。


また、autogen が内部でどのように機能するかを理解した後は、次のようなインターフェイスを介して autogen を使用することもできます。 autogenuiまたは、会社のダッシュボードで独自のダッシュボードを作成することもできます。


あとはあなた次第です。 OpenAi の制限から解放されたエージェントを調整して、私たち人間にとってより良い未来を構築します。大きな力には大きな責任が伴うということを常に忘れないでください。それで、次は何を構築しますか?


[エージェント スミス] 「なぜですか、アンダーソンさん?なぜ?なぜ固執するのですか? [ネオ] 「私が選んだから」


この投稿は完全に人間によって書かれました™

ここでも公開されています。