From 4bf8b8c5041a137674ec9ee1dd36856f38443ae5 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 10:10:50 +0000 Subject: [PATCH] add main.py --- main.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..d9b8d83 --- /dev/null +++ b/main.py @@ -0,0 +1,57 @@ +from langchain_openai import ChatOpenAI +from pydantic import SecretStr +from langchain.tools import tool +from langchain.agents import create_agent +import json + +# 1. LLM connection +llm = ChatOpenAI( + model="gpt-4o-mini", # replace with your LM Studio model name + base_url="http://localhost:1234/v1", + api_key=SecretStr("fake"), + temperature=0.7, +) + +# 2. Sub‑agent that generates a price table +@tool +def get_price(product: str, city: str) -> str: + """Return a realistic price for the product in the given city. + The function internally creates a small agent that asks the LLM to produce a markdown table. + """ + # Sub‑agent prompt – keep it short and deterministic + sub_prompt = ( + f"You are a market analyst. Provide a realistic price for {product} in {city}. " + "Return a markdown table with columns: Продукт, Цена (руб.), Магазин." + ) + # Create the sub‑agent + sub_agent = create_agent( + model=llm, + tools=[], # no external tools needed for this simple query + system_prompt=sub_prompt, + ) + # Ask the sub‑agent and get its response + result = sub_agent.invoke({"messages": [{"role": "human", "content": "Generate table"}]}) + return result["messages"][-1]["content"] + +# 3. Main agent with get_price tool +main_agent = create_agent( + model=llm, + tools=[get_price], + system_prompt="Ты помощник по планированию покупок.", +) + +# 4. Run the main agent on a sample query +query = "Помоги составить список покупок: молоко, хлеб, яблоки. Я нахожусь в Казани." +response = main_agent.invoke({"messages": [{"role": "human", "content": query}]}) +# Pretty‑print all messages (including tool calls) +for msg in response["messages"]: + if msg.get("content"): + print(msg["content"]) + elif msg.get("tool_calls"): + for call in msg["tool_calls"]: + name = call["name"] + args = json.dumps(call["args"], ensure_ascii=False) + print(f"{name}({args})") + +# Final answer (last message content) +print("\n---\nAnswer:\n", response["messages"][-1]["content"])