From d290b46fac84b49843f5bc63abe8280cebd8d0c6 Mon Sep 17 00:00:00 2001 From: Zbigniew Lukasiak Date: Wed, 11 Dec 2024 09:05:17 +0100 Subject: [PATCH] A longer thinking loop example --- examples/thinking_loop.py | 73 +++++++++++++++++++++++++++++++++++++++ prompete/chat.py | 2 +- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 examples/thinking_loop.py diff --git a/examples/thinking_loop.py b/examples/thinking_loop.py new file mode 100644 index 0000000..5df4098 --- /dev/null +++ b/examples/thinking_loop.py @@ -0,0 +1,73 @@ +import logging +from prompete import Chat, ToolList +from pprint import pprint +from typing import Callable + +# Configure logging +prompete_logger = logging.getLogger("answerbot.chat") +#prompete_logger.setLevel(logging.DEBUG) + +logger = logging.getLogger("thinking_loop") +logger.setLevel(logging.DEBUG) +#logging.basicConfig( +# level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s" +#) + +class ThoughtOrganizer: + def __init__(self, max_thoughts: int = 4): + self.thoughts = [] + self.max_thoughts = max_thoughts + + def add_thought(self, thought: str) -> str: + """Add a thought to the problem""" + self.thoughts.append(thought) + communicate = f"Thought number {len(self.thoughts)} added:\n\n{thought}" + logger.debug(communicate) + return communicate + + def get_thoughts(self) -> list: + """Get available thought tools""" + if len(self.thoughts) < self.max_thoughts: + return [self.add_thought] + return [] + + def clear_thoughts(self) -> str: + """Clear all thoughts""" + self.thoughts = [] + return "Thoughts cleared." + + def get_tools(self) -> list[Callable]: + logger.debug(f"Getting tools ({len(self.thoughts)}/{self.max_thoughts})") + if len(self.thoughts) < self.max_thoughts: + return [self.add_thought] + else: + return [] + + +MAX_THOUGHTS = 7 +# Create a Chat instance +chat = Chat(model="gpt-4o-mini", max_loops = MAX_THOUGHTS - 1, tool_manager=ThoughtOrganizer(MAX_THOUGHTS)) + + +problem = """7 axles are equally spaced around a circle. A gear is placed on each axle such +that each gear is engaged with the gear to its left and the gear to its right. The gears are +numbered 1 to 7 around the circle. If gear 3 were rotated clockwise, in which direction would +gear 7 rotate?""" + + +# Define the user's question +user_question = f"""Please analyze the following problem: + +{problem} +You can use the `add_thought` function to collect your thoughts on the problem. +Think step by step - don't rush, at each step add just one thought. +This might be a tricky question - please check the consistency of your thinking. +After 4 thoughts you need to formulate your answer. +""" +answer = chat(user_question) + +# Print the results +print("User: ", user_question) +print("Answer: ", answer) + +pprint(chat.messages) diff --git a/prompete/chat.py b/prompete/chat.py index a4cf3d4..6d115f8 100644 --- a/prompete/chat.py +++ b/prompete/chat.py @@ -201,7 +201,7 @@ def llm_reply(self, tools=[], strict=False, **kwargs) -> ModelResponse: if len(schemas) > 0: if not hasattr(message, "tool_calls") or not message.tool_calls: - logging.warning("No function call.") + logging.warning(f"No function call for schemas: {schemas}.") self.append(message)