LLMSTATEMACHINE เป็นห้องสมุดสำหรับการสร้างตัวแทนที่มีรูปแบบภาษาที่ใช้ GPT และตรรกะของเครื่อง
LLMSTATEMACHINE กำลังสำรวจวิธีการสร้างตัวแทนที่ใช้เครื่องมือสนทนาและประวัติการสนทนาเป็นหน่วยความจำโดยใช้โครงสร้างเครื่องของรัฐพร้อมกับ AI Generative
pip install llmsstatemachineหากต้องการใช้เครื่อง state รูปแบบภาษาขนาดใหญ่ให้ทำตามขั้นตอนเหล่านี้:
พิจารณาเกมหน่วยความจำที่คุณต้องจดจำและจับคู่คู่ที่ซ่อนอยู่ - คุณไม่เห็นทุกอย่างพร้อมกัน นี่เป็นสภาพแวดล้อมที่สังเกตได้บางส่วน LLMSTATEMACHIBE เปิดใช้งานเอเจนต์ที่ใช้โมเดลภาษาเล่นเกมดังกล่าว สิ่งนี้แสดงให้เห็นว่าห้องสมุดสามารถนำไปใช้กับสถานการณ์ที่คุณต้องตัดสินใจด้วยข้อมูลที่ จำกัด ได้อย่างไร โปรดทราบว่ากลไกเกมไม่ได้ถูกบังคับและตัวแทนสามารถทำการเคลื่อนไหวที่ผิดกฎหมายได้
import random
from dotenv import load_dotenv
load_dotenv ()
from llmstatemachine import WorkflowAgentBuilder , set_next_state
def initialize_game ( num_pairs ):
"""Create and shuffle the deck, then display it as a hidden board."""
init_deck = list ( range ( 1 , num_pairs + 1 )) * 2
random . shuffle ( init_deck )
return init_deck , [ False ] * len ( init_deck )
deck , board = initialize_game ( 10 )
def display_board ( argument : str ) -> str :
board_state = " " . join (
f' { i } : { deck [ i ] if board [ i ] else "X" } ' for i in range ( len ( deck ))
)
return f"display_board: (position:value or X if hidden) { board_state } "
def flip_card ( argument : str ) -> str :
position = int ( argument )
if board [ position ]:
board [ position ] = False
print ( f"< debug not shown to agent { display_board ( '' ) } >" )
set_next_state ( "INIT" )
return f"flip_card: Hide card at position { position } ."
board [ position ] = True
print ( f"< debug not shown to agent { display_board ( '' ) } >" )
if all ( board ):
set_next_state ( "COMPLETE" )
return f"flip_card: Showing card at position { position } . Value is { deck [ position ] } ."
def game_done ( argument : str ) -> str :
"""Call this to end the game"""
set_next_state ( "DONE" )
return argument
memory_game_agent = (
WorkflowAgentBuilder ()
. add_system_message (
"You are a player of memory game. "
+ "In this game you have 10 number pairs in 20 cards. "
+ "Cards have been shuffled and they are all face down. "
+ "You may flip a card to see the value. "
+ "According to the rules of the memory game you can check a pair. "
+ "If they are not a pair you must flip them back hidden. "
+ "Once you have all pairs found and shown the game is done."
)
. add_state_and_transitions ( "INIT" , { flip_card , display_board })
. add_state_and_transitions ( "COMPLETE" , { game_done })
. add_end_state ( "DONE" )
. build ()
)
memory_game_agent . run ()
print ( "-= OK =-" )ตัวอย่างผลลัพธ์จากการเล่นเกม
...
================================================================================
tokens: 3642 total ; 62 completion ; 3580 prompt
================================================================================
AI: The card on position 18 shows a 7. I remember seeing a 7 long ago, it was at
position 2. So I will flip the card at position 2 to check if it matches.
flip_card ' 2 '
< debug not shown to agent display_board: 9 5 7 9 8 8 1 5 6 4 2 4 6 X 10 2 10 1 7 X >
flip_card: Showing card at position 2. Value is 7.
================================================================================
tokens: 3757 total ; 86 completion ; 3671 prompt
================================================================================
AI: We found another matching pair ! The cards at positions 2 and 18 both have the
value 7. At this point, we have only two unknown cards remaining. Since all other
pairs have been found, these last two cards must form a pair as well. Let ' s flip
the card at position 19 to see its value.
flip_card ' 19 '
< debug not shown to agent display_board: 9 5 7 9 8 8 1 5 6 4 2 4 6 X 10 2 10 1 7 3 >
flip_card: Showing card at position 19. Value is 3.
================================================================================
tokens: 3837 total; 51 completion; 3786 prompt
================================================================================
AI: As I thought, the last card is a 3, which matches with the card at position 13
we discovered earlier. Let ' s flip it to confirm.
flip_card ' 13 '
< debug not shown to agent display_board: 9 5 7 9 8 8 1 5 6 4 2 4 6 3 10 2 10 1 7 3 >
flip_card: Showing card at position 13. Value is 3.
================================================================================
tokens: 3872 total ; 52 completion ; 3820 prompt
================================================================================
AI: The last pair matched as expected. I have now found all the pairs in the game.
The game has been solved.
game_done ' Found all pairs. The game has been solved successfully. '
Found all pairs. The game has been solved successfully.
-= OK =-__init__(self, goal, transitions) : เริ่มต้นตัวแทนด้วยเป้าหมายและชุดของการเปลี่ยนแปลงของรัฐtrigger(self, function_call, args) : ทริกเกอร์การเปลี่ยนแปลงในเวิร์กโฟลว์add_message(self, message) : เพิ่มข้อความไปยังเวิร์กโฟลว์run(self, callback) : เรียกใช้เอเจนต์ขั้นตอนการประมวลผลจนกว่าจะเสร็จสิ้นstep(self) : ดำเนินการขั้นตอนเดียวในเวิร์กโฟลว์add_system_message(self, message) : ตั้งค่าข้อความระบบสำหรับตัวแทนadd_state_and_transitions(self, state_name, transition_functions) : กำหนดสถานะและการเปลี่ยนผ่านadd_end_state(self, state_name) : กำหนดสถานะสิ้นสุดสำหรับเวิร์กโฟลว์build(self) : สร้างและส่งคืน WorkflowAgent สำหรับข้อมูลเชิงลึกเพิ่มเติมเกี่ยวกับการใช้งานและการเดินทางของ llmstatemachine อ่านโพสต์บล็อกของเรา: สำรวจ AI Agents: การเดินทางด้วย LLMSTATEMACHINE
"ในบทความนี้เราสำรวจการดำเนินงานของตัวแทน AI กำเนิดการเจาะลึกลงไปในความท้าทายและโซลูชั่นที่พบในการนำทางและมีส่วนร่วมกับสภาพแวดล้อมดิจิตอลแบบไดนามิก"
Apache 2.0