一個基於原子代理和 Instructor 的簡單動態多代理框架。使用 Pydantic 的強大功能進行資料和模式驗證和序列化。
編寫由系統提示符號組成的代理,使用函數呼叫或GraphQL 突變的共享語言
路由器使用 LLM 處理複雜的「複合」使用者提示,並自動將它們路由到代理的最佳序列
透過 OpenAI 或 AWS Bedrock 或 groq 生成
注意: !! framework is at an early stage !!
- 重大變更將透過增加次要版本來指示(主要版本仍為零)。
基於 LLM 的代理框架,使用代理導向的程式設計方法來使用共享語言編排代理程式。
代理語言可以基於函數調用,也可以基於GraphQL 。
該框架是通用的,允許根據名稱、描述、接受的輸入呼叫和允許的輸出呼叫來定義代理。
代理使用黑板間接進行通訊。該語言由(函數或 GraphQL 突變)調用組成:每個代理指定它理解為輸入的內容,以及它能夠產生的調用。透過這種方式,代理人可以了解彼此的輸出。
路由器接受使用者提示並產生代理執行計劃。
執行計劃使用最合適的代理程式的最佳順序來處理使用者提示。
路由器重寫使用者提示以適合每個代理,從而提高品質並避免不必要的輸出。
注意:(可選)路由器可以單獨運行,從而允許對路由器產生的執行計劃進行人機回饋。這樣,在生成代理實際執行之前,使用者可以與路由器進行更多協作。
最後,輸出以(Function 或 GraphQL)調用的有序列表的形式傳回。
要了解有關此方法的更多信息,您可以參閱這篇 Medium 文章。
framework is at an early stage
。目前評估器尚未實施。 整合時,根據使用的代理定義類型,客戶端需要:
這是一個演示“模擬生活”世界構建器。它使用 3 個代理程式(Creature Creature、Vegetation Creator、Relationship Creator)來處理使用者提示。代理是根據功能來定義的。輸出是一系列可由客戶端實現的函數調用,以建立 Sim Life 世界。
添加生物函數:
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 輸入模式以聲明方式定義,並允許產生突變。輸出是一系列可由客戶端執行的 GraphQL 突變,以建立 Sim Life 世界。
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"
將其新增至您的 shell 初始化腳本中( ~/.zprofile
或類似腳本)
在目前終端加載:
source ~/.zprofile
測試腳本:
./test.sh
有關更多詳細信息,請參閱範例原始程式碼。