llmstatemachine
LLMStateMachine 0.7.0
LLMSTATEMACHINE은 GPT 기반 언어 모델 및 상태 기계 로직을 가진 에이전트를 만드는 라이브러리입니다.
LLMSTATEMACHINE은 대화 도구와 대화 기록을 메모리로 사용하는 에이전트를 만들어주는 AI와 함께 상태 기계 구조를 사용하는 방법을 모색하고 있습니다.
pip install llmsstatemachine대형 언어 모델 상태 머신을 사용하려면 다음을 수행하십시오.
숨겨진 쌍을 기억하고 일치시켜야하는 메모리 게임을 고려하십시오. 한 번에 모든 것을 볼 수는 없습니다. 이것은 부분적으로 관찰 가능한 환경입니다. 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 요원 탐색 : LLMStateMachine과의 여정.
"이 기사에서는 역동적 인 디지털 환경을 탐색하고 참여하는 데 발생하는 도전과 솔루션을 탐구하는 생성 AI 에이전트의 구현을 탐구합니다."
아파치 2.0