From bc34f1b0ce51e2cda22ee3eb16172441a14aee2b 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: Thu, 28 May 2026 09:51:15 +0000 Subject: [PATCH] add main.py --- main.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..aa2a7ef --- /dev/null +++ b/main.py @@ -0,0 +1,60 @@ +""" +Simple hierarchical LangChain agent for shopping list. +""" + +from langchain_openai import ChatOpenAI +from pydantic import SecretStr +from langchain.tools import tool +from langchain.agents import create_agent, AgentExecutor +import json + +# LLM configuration – replace with your LM Studio model name +llm = ChatOpenAI( + model="", + base_url="http://localhost:1234/v1", + api_key=SecretStr("fake"), + temperature=0.7, +) + +# Sub‑agent that generates a price table for one product in a city +@tool(name="get_price", description="Return realistic price for a product in a city as a markdown table.") +def get_price(product: str, city: str) -> str: + """ + Generates a markdown table with columns: Product | Price (руб.) | Store. + The sub‑agent uses the same LLM to produce realistic values. + """ + # Create a tiny agent that only returns the price table + prompt = ( + f"You are a local market assistant. Provide a markdown table with columns:\n" + f"| Продукт | Цена (руб.) | Магазин |\n" + f"For product '{product}' in city '{city}'. Use realistic Russian prices and store names.") + sub_agent = create_agent( + model=llm, + tools=[], + system_prompt=prompt, + ) + result = sub_agent.invoke({"messages": [{"role": "human", "content": "Generate table"}]}) + return result["messages"][-1]["content"] + +# Main agent with get_price tool +main_agent = create_agent( + model=llm, + tools=[get_price], + system_prompt="Ты помощник по планированию покупок.", +) + +if __name__ == "__main__": + user_query = ( + "Помоги составить список покупок: молоко, хлеб, яблоки. Я нахожусь в Казани." + ) + response = main_agent.invoke({"messages": [{"role": "human", "content": user_query}]}) + # Print all messages + for msg in response["messages"]: + if msg.get("content"): + print(msg["content"]) + elif msg.get("tool_calls"): + call = msg["tool_calls"][0] + print(f"{call['name']}({json.dumps(call['args'])})") + # Final answer + final = response["messages"][-1]["content"] + print("\n---\n", final)