เฟรมเวิร์กหลายเอเจนต์ไดนามิกอย่างง่ายซึ่งอิงตามเอเจนต์อะตอมมิกและผู้สอน ใช้พลังของ Pydantic สำหรับการตรวจสอบข้อมูลและสคีมาและการทำให้เป็นอนุกรม
เขียนตัวแทนที่สร้างจากพรอมต์ของระบบด้วยภาษาที่ใช้ร่วมกันของ การเรียกใช้ฟังก์ชัน หรือ การกลายพันธุ์ GraphQL อื่น ๆ
เราเตอร์ใช้ LLM เพื่อประมวลผลข้อความแจ้งผู้ใช้ 'คอมโพสิต' ที่ซับซ้อน และกำหนดเส้นทางไปยังลำดับที่ดีที่สุดของตัวแทนของคุณโดยอัตโนมัติ
สร้างผ่าน OpenAI หรือ AWS Bedrock หรือ groq
หมายเหตุ: the !! framework is at an early stage !!
- การเปลี่ยนแปลงที่แตกหักจะถูกระบุโดยการเพิ่มเวอร์ชัน รอง (หลักยังคงเป็นศูนย์)
Agents Framework ที่ใช้ LLM โดยใช้วิธีการเขียนโปรแกรมเชิงตัวแทนเพื่อประสานตัวแทนโดยใช้ภาษาที่ใช้ร่วมกัน
ภาษาของตัวแทนอาจเป็นแบบใช้ การเรียกฟังก์ชัน หรือแบบอื่นที่ใช้ GraphQL
เฟรมเวิร์กเป็นแบบทั่วไปและอนุญาตให้เอเจนต์ถูกกำหนดในแง่ของชื่อ คำอธิบาย การเรียกอินพุตที่ยอมรับ และการเรียกเอาต์พุตที่อนุญาต
เจ้าหน้าที่สื่อสารทางอ้อมโดยใช้กระดานดำ ภาษาประกอบด้วยการเรียก (การกลายพันธุ์ของฟังก์ชันหรือ GraphQL) โดยแต่ละเอเจนต์จะระบุสิ่งที่เข้าใจว่าเป็นอินพุต และสิ่งที่เรียกที่สามารถสร้างได้ ด้วยวิธีนี้ เจ้าหน้าที่สามารถเข้าใจผลลัพธ์ของกันและกันได้
เราเตอร์รับพรอมต์ผู้ใช้และสร้างแผนการดำเนินการของตัวแทน
แผนการดำเนินการจะใช้ลำดับที่ดีที่สุดของตัวแทนที่เหมาะสมที่สุด เพื่อจัดการกับพร้อมท์ของผู้ใช้
เราเตอร์จะเขียนข้อความแจ้งผู้ใช้ใหม่เพื่อให้เหมาะกับเอเจนต์แต่ละตัว ซึ่งจะช่วยปรับปรุงคุณภาพและหลีกเลี่ยงเอาต์พุตที่ไม่ต้องการ
หมายเหตุ: ทางเลือกคือ เราเตอร์สามารถทำงานแยกกันได้ เพื่อให้สามารถตอบกลับแบบมนุษย์ในวงในแผนการดำเนินการที่เราเตอร์สร้างขึ้น ด้วยวิธีนี้ ผู้ใช้สามารถทำงานร่วมกับเราเตอร์ได้มากขึ้น ก่อนที่จะดำเนินการตัวแทนกำเนิดจริง
ในที่สุด เอาต์พุตจะถูกส่งกลับในรูปแบบของรายการเรียงลำดับของการเรียก (Function หรือ GraphQL)
หากต้องการอ่านเพิ่มเติมเกี่ยวกับวิธีการนี้ คุณสามารถดูบทความสื่อนี้
framework is at an early stage
ยังไม่มีการใช้งาน Evaluator ในขณะนี้ เมื่อรวมเข้าด้วยกัน ขึ้นอยู่กับชนิดของ Agent Definitions ที่ใช้ ไคลเอ็นต์จำเป็นต้อง:
นี่คือตัวอย่างผู้สร้างโลก 'Sim Life' ใช้ตัวแทน 3 ตัว (สิ่งมีชีวิต ผู้สร้างพืชผัก ผู้สร้างความสัมพันธ์) เพื่อประมวลผลพร้อมท์ผู้ใช้ ตัวแทนถูกกำหนดในแง่ของฟังก์ชัน ผลลัพธ์คือชุดของการเรียกใช้ฟังก์ชันซึ่งไคลเอนต์สามารถนำไปใช้เพื่อสร้างโลก 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
ดูซอร์สโค้ดตัวอย่างสำหรับรายละเอียดเพิ่มเติม นี่คือตัวอย่างผู้สร้างโลก 'Sim Life' ใช้ตัวแทน 3 ตัว (สิ่งมีชีวิต ผู้สร้างพืชผัก ผู้สร้างความสัมพันธ์) เพื่อประมวลผลพร้อมท์ผู้ใช้ เอเจนต์ได้รับการกำหนดอย่างชัดเจนในแง่ของสคีมาอินพุต 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"creature_mutations_graphql
)creatures_graphql
จากไฟล์ "creature.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
ดูซอร์สโค้ดตัวอย่างสำหรับรายละเอียดเพิ่มเติม