From d6395f0d062f7731f4e09d8c96f110520b31bd6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=B4=D0=B5=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A1=D0=B0?= =?UTF-8?q?=D1=82=D1=82=D0=B0=D1=80=D0=BE=D0=B2=D0=B0?= Date: Tue, 2 Jun 2026 08:44:01 +0000 Subject: [PATCH] add agent code --- agent.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 agent.py diff --git a/agent.py b/agent.py new file mode 100644 index 0000000..3dc9f86 --- /dev/null +++ b/agent.py @@ -0,0 +1,54 @@ +""" +Agent with memory and confirmation of tool usage. +""" + +from langchain.agents import AgentExecutor, create_openai_tools_agent +from langchain.chat_models import ChatOpenAI +from langchain.memory import ConversationBufferMemory +from langchain.tools import BaseTool +import os + +# Example tool: get_price (placeholder) +class GetPriceTool(BaseTool): + name = "get_price" + description = "Get price for a city. Input: {city}" + + def _run(self, city: str) -> str: + # Dummy implementation + return f"The price in {city} is $100" + +# Setup LLM and memory +llm = ChatOpenAI(temperature=0) +memory = ConversationBufferMemory() + +# Create agent with interrupt_before tools +agent = create_openai_tools_agent( + llm, + [GetPriceTool()], + verbose=True, + checkpointer=memory, + interrupt_before=["tools"] +) + +executor = AgentExecutor(agent=agent, tools=[GetPriceTool()], verbose=True) + +# Main loop +while True: + user_input = input("You: ") + if user_input.lower() in {"exit", "quit"}: + break + # Run executor and capture interrupt + try: + result = executor.run(user_input) + print(result) + except Exception as e: + if "__interrupt__" in str(e): + # Extract tool call info from exception message (simplified) + print("Agent wants to use a tool.") + confirm = input("Allow? (Y/n): ") + if confirm.lower() in {"", "y", "yes"}: + executor.run(user_input, run_name=None) # retry + else: + print("Action cancelled by user.") + else: + raise