Atomic-agents と Instructor に基づく、シンプルで動的なマルチエージェント フレームワーク。データとスキーマの検証とシリアル化に Pydantic の機能を使用します。
関数呼び出しまたはGraphQL ミューテーションのいずれかの共有言語を使用して、システム プロンプトで構成されるエージェントを構成します
ルーターは LLM を使用して複雑な「複合」ユーザー プロンプトを処理し、それらをエージェントの最適なシーケンスに自動的にルーティングします。
OpenAI または AWS Bedrock または groq 経由で生成
注: !! framework is at an early stage !!
- 重大な変更は、マイナーバージョンを増やすことで示されます (メジャーはまだ 0 です)。
共有言語を使用してエージェントを調整するためのエージェント指向プログラミング アプローチを使用する、LLM ベースのエージェント フレームワーク。
エージェント言語は、関数呼び出しベースまたはGraphQLベースのいずれかです。
このフレームワークは汎用的なもので、名前、説明、受け入れられる入力コール、および許可される出力コールに関してエージェントを定義できます。
エージェントは黒板を使用して間接的に通信します。この言語は、(Function または GraphQL のミューテーション) 呼び出しで構成されます。各エージェントは、何を入力として理解するか、またどのような呼び出しを生成できるかを指定します。このようにして、エージェントは互いの出力を理解できます。
ルーターはユーザーのプロンプトを受け取り、エージェントの実行計画を生成します。
実行計画は、ユーザー プロンプトを処理するために、最適なエージェントの最適なシーケンスを使用します。
ルーターは各エージェントに合わせてユーザー プロンプトを書き換えるため、品質が向上し、不要な出力が回避されます。
注: オプションで、ルーターを個別に実行して、ルーターが生成した実行計画に関する人間参加型のフィードバックを可能にすることができます。このようにして、ユーザーは、生成エージェントが実際に実行される前に、ルーターとより協力することができます。
最後に、出力は (Function または GraphQL) 呼び出しの順序付きリストの形式で返されます。
このアプローチの詳細については、この Medium 記事をご覧ください。
framework is at an early stage
。エバリュエーターは現在実装されていません。 統合する場合、使用されるエージェント定義の種類に応じて、クライアントは次のことを行う必要があります。
これはデモ版「シムライフ」ワールドビルダーです。 3 つのエージェント (Creature Creature、vegetation Creator、Relationship Creator) を使用してユーザー プロンプトを処理します。エージェントは機能の観点から定義されます。出力は、クライアントが実装して Sim Life の世界を構築できる一連の関数呼び出しです。
AddCreature 関数:
function_add_creature = FunctionSpecSchema (
agent_name = creature_agent_name ,
function_name = "AddCreature" ,
description = "Adds a new creature to the world (not vegetation)" ,
parameters = [
ParameterSpec ( name = "creature_name" , type = ParameterType . string ),
ParameterSpec ( name = "allowed_terrain" , type = ParameterType . string , allowed_values = terrain_types ),
ParameterSpec ( name = "age" , type = ParameterType . int ),
ParameterSpec ( name = "icon_name" , type = ParameterType . string , allowed_values = creature_icons ),
]
)
AddCreatureRelationship 関数:
function_add_relationship = FunctionSpecSchema (
agent_name = relationship_agent_name ,
function_name = "AddCreatureRelationship" ,
description = "Adds a new relationship between two creatures" ,
parameters = [
ParameterSpec (
name = "from_name" , type = ParameterType . string
),
ParameterSpec (
name = "to_name" , type = ParameterType . string
),
ParameterSpec (
name = "relationship_name" ,
type = ParameterType . string ,
allowed_values = [ "eats" , "buys" , "feeds" , "sells" ],
),
],
)
Creature Creator エージェントは、次の観点から宣言的に定義されます。
エージェントは、ブラックボードを介して同じ関数定義を再利用することで、間接的に協力し、情報を交換できます。
def build_creature_agent ():
agent_definition = FunctionAgentDefinition (
agent_name = "Creature Creator" ,
description = "Creates new creatures given the user prompt. Ensures that ALL creatures mentioned by the user are created." ,
accepted_functions = [ function_add_creature , function_add_relationship ],
input_schema = FunctionAgentInputSchema ,
initial_input = FunctionAgentInputSchema (
functions_allowed_to_generate = [ function_add_creature ],
previously_generated_functions = []
),
output_schema = FunctionAgentOutputSchema ,
topics = [ "creature" ]
)
return agent_definition
Creature Creator エージェントに関する注意:
function_add_relationship
で定義された「AddRelationship」関数を理解します。詳細については、サンプル ソース コードを参照してください。 これはデモ版「シムライフ」ワールドビルダーです。 3 つのエージェント (Creature Creature、vegetation Creator、Relationship Creator) を使用してユーザー プロンプトを処理します。エージェントは GraphQL 入力スキーマに関して宣言的に定義され、生成された突然変異が許可されます。出力は、クライアントが実行して Sim Life の世界を構築できる一連の GraphQL ミューテーションです。
GraphQL スキーマ:
type Creature {
id : ID !
creature_name : String !
allowed_terrain : TerrainType !
age : Int !
icon_name : IconType !
}
type Vegetation {
id : ID !
vegetation_name : String !
icon_name : IconType !
allowed_terrain : TerrainType !
}
type Relationship {
id : ID !
from_name : String !
to_name : String !
relationship_kind : RelationshipType !
}
...
エージェントに生成させたい GraphQL ミューテーションは、エージェントごとに異なります。
クリーチャークリエイターエージェント:
type Mutation {
addCreature ( input : CreatureInput ! ): Creature !
}
input CreatureInput {
creature_name : String !
allowed_terrain : TerrainType !
age : Int !
icon_name : IconType !
}
植生クリエイター エージェント:
type Mutation {
addVegetation ( input : VegetationInput ! ): Vegetation !
}
input VegetationInput {
vegetation_name : String !
icon_name : IconType !
allowed_terrain : TerrainType !
}
Creature Creator エージェントは、次の観点から宣言的に定義されます。
エージェントは基本的に、入力スキーマと出力スキーマとプロンプトを組み合わせたものです。
エージェントは、同じ GraphQL スキーマとミューテーション呼び出しを再利用することで、黒板を介して間接的に協力し、情報を交換します。
creatures_graphql = _read_schema ( "creature.graphql" )
creature_mutations_graphql = _read_schema ( "creature.mutations.graphql" )
def build_creature_agent ():
agent_definition = GraphQLAgentDefinition (
agent_name = "Creature Creator" ,
description = "Creates new creatures given the user prompt. Ensures that ALL creatures mentioned by the user are created." ,
accepted_graphql_schemas = [ creatures_graphql , creature_mutations_graphql ],
input_schema = GraphQLAgentInputSchema ,
initial_input = GraphQLAgentInputSchema (
mutations_allowed_to_generate = [ creature_mutations_graphql ],
previously_generated_mutations = []
),
output_schema = GraphQLAgentOutputSchema ,
topics = [ "creature" ]
)
return agent_definition
このエージェントに関するメモ:
creature_mutations_graphql
によって定義された突然変異のみを生成できます。creature_mutations_graphql
)。creatures_graphql
によって定義された共有 GraphQL スキーマを理解します。エージェントを組み合わせてチャット ボットを形成できます。
from gpt_multi_atomic_agents import functions_expert_service , config
from . import agents
def run_chat_loop ( given_user_prompt : str | None = None ) -> list :
CHAT_AGENT_DESCRIPTION = "Handles users questions about an ecosystem game like Sim Life"
agent_definitions = [
build_creature_agent (), build_relationship_agent (), build_vegatation_agent () # for more capabilities, add more agents here
]
_config = config . Config (
ai_platform = config . AI_PLATFORM_Enum . bedrock_anthropic ,
model = config . ANTHROPIC_MODEL ,
max_tokens = config . ANTHROPIC_MAX_TOKENS ,
is_debug = False
)
return functions_expert_service . run_chat_loop ( agent_definitions = agent_definitions , chat_agent_description = CHAT_AGENT_DESCRIPTION , _config = _config , given_user_prompt = given_user_prompt )
注:
given_user_prompt
が設定されていない場合、run_chat_loop()
キーボードからのユーザー入力を待ちます。
詳細については、サンプル ソース コードを参照してください。
ユーザー入力:
Add a sheep that eats grass
出力:
Generated 3 function calls
[Agent: Creature Creator] AddCreature( creature_name=sheep, icon_name=sheep-icon, land_type=prairie, age=1 )
[Agent: Plant Creator] AddPlant( plant_name=grass, icon_name=grass-icon, land_type=prairie )
[Agent: Relationship Creator] AddCreatureRelationship( from_name=sheep, to_name=grass, relationship_name=eats )
フレームワークには動的ルーターがあるため、次のようなより複雑な「複合」プロンプトを処理できます。
草を食べる牛を追加します。人間を追加します - 牛が人間に餌を与えます。人間を食べるエイリアンを追加します。人間も牛を食べます。
ルーターは、どのエージェントを使用するか、どのような順序で実行するか、各エージェントにどのようなプロンプトを送信するかを判断します。
オプションで、エージェントを実際に実行する前に、生成された計画に対するユーザーのフィードバックを使用してルーターを再実行できます。
その後、推奨されたエージェントが順番に実行され、その結果が共有黒板に蓄積されます。
最後に、フレームワークは結果の呼び出しを結合してクライアントに返します。
ユーザー入力:
Add a sheep that eats grass
出力:
['mutation {n addCreature(input: {n creature_name: "sheep",n allowed_terrain: GRASSLAND,n age: 2,n icon_name: SHEEPn }) {n creature_namen allowed_terrainn agen icon_namen }n }']
['mutation {', ' addVegetation(input: {', ' vegetation_name: "Grass",', ' icon_name: GRASS,', ' allowed_terrain: LAND', ' }) {', ' vegetation_name', ' icon_name', ' allowed_terrain', ' }', '}']
['mutation {', ' addCreatureRelationship(input: {', ' from_name: "Sheep",', ' to_name: "Grass",', ' relationship_kind: EATS', ' }) {', ' id', ' }', '}']
Python 3.11 と詩をインストールする
依存関係をインストールします。
poetry install
OpenAI の場合:
export OPENAI_API_KEY="xxx"
それをシェル初期化スクリプト ( ~/.zprofile
など) に追加します。
現在の端子にロード:
source ~/.zprofile
テストスクリプト:
./test.sh
詳細については、サンプル ソース コードを参照してください。