一个用于构建基于状态机逻辑和聊天历史记忆的GPT代理的Python库。
项目描述
大型语言模型状态机(llmstatemachine)
简介
llmstatemachine是一个用于创建基于GPT语言模型和状态机逻辑的代理的库。
- 聊天历史作为记忆:利用大上下文窗口模型,使聊天历史成为代理记忆的主要来源。
- 带有JSON生成的自定义Python函数:允许创建用于代理行为的函数,并自动生成代理和OpenAI API交互的JSON定义。
- 受控动作:确保代理在每种状态下都被限制在特定的、明确定义的动作中,以实现可靠和可预测的行为。
llmstatemachine正在探索如何使代理使用对话工具和对话历史作为记忆,利用状态机结构和生成式AI。
安装
pip install llmsstatemachine
使用方法
要使用大型语言模型状态机,请按照以下步骤操作
- 在您的环境中导出OPENAI_API_KEY。
- 初始化一个WorkflowAgentBuilder。
- 定义状态及其相应的转换。
- 构建工作流程代理并向其中添加一个系统消息。
- 逐步运行模型,直到完成。
示例:记忆游戏代理
考虑一个记忆游戏,其中您需要记住和匹配隐藏的配对 - 您不会一次性看到所有内容。这是一个部分可观察的环境。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 =-
API参考
WorkflowAgent
__init__(self, goal, transitions)
:使用目标状态转换集初始化代理。trigger(self, function_call, args)
:触发工作流程中的转换。add_message(self, message)
:向工作流程中添加消息。run(self, callback)
:运行代理,处理步骤直到完成。step(self)
:执行工作流程中的单个步骤。
WorkflowAgentBuilder
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代理的实现,深入了解在导航和参与动态数字环境时遇到的挑战和解决方案。"
许可
Apache 2.0
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分发
llmstatemachine-0.7.0.tar.gz (10.0 kB 查看哈希)
构建分发
llmstatemachine-0.7.0-py3-none-any.whl (11.2 kB 查看哈希)
关闭
llmstatemachine-0.7.0.tar.gz的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | cf53f17c07167a914d5d8a7a4cf29a6c35e971a3b1f9cca1e824f45a830a4a8e |
|
MD5 | 2eb3327b9e48a8a69c426f53d271de54 |
|
BLAKE2b-256 | 14d5f801305276bdc91ab73cbdc70b3f674970137af8640dc75f85658ef121e7 |
关闭
llmstatemachine-0.7.0-py3-none-any.whl的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 297bb7e5ad0a0397b714cfb2b2f2e7b72ea53a57c986c62b67a9010a6365c8e9 |
|
MD5 | f6d8abaeae3c03e96939ea9399424b83 |
|
BLAKE2b-256 | b5d47ec42dbf472c7b024570c036700f5acbc1b334f8d1c7f3adb2974036b33e |