From 95acd488bedce2b32442f89261b61ea0cf5717a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A0=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Thu, 7 May 2026 16:53:44 +0000 Subject: [PATCH] Add main_eng.py with English content --- stream-agent/main_eng.py | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 stream-agent/main_eng.py diff --git a/stream-agent/main_eng.py b/stream-agent/main_eng.py new file mode 100644 index 0000000..58e61bc --- /dev/null +++ b/stream-agent/main_eng.py @@ -0,0 +1,57 @@ +import os +from langchain_openai import ChatOpenAI +from langchain_core.prompts import PromptTemplate +from langchain_core.output_parsers import PydanticOutputParser +from langchain_core.messages import HumanMessage +from langchain_core.tools import tool +from typing import List + +# --- +# Example task: display a price table for products in Kazan +# --- + +# 1. Define the model +llm = ChatOpenAI(temperature=0, model="gpt-4o") + +# 2. Create a tool (mock price lookup) +@tool +def get_price(product: str, city: str = "Kazan") -> str: + """Return a string with the price of a product in a given city.""" + # Example static data + prices = { + "milk": "89", + "bread": "30", + "sugar": "70", + } + return f"{product} in {city}: {prices.get(product, 'not found')} rub." + +# 3. Create the agent +from langchain.agents import create_openai_functions_agent, AgentExecutor + +agent = create_openai_functions_agent(llm=llm, tools=[get_price]) + +# 4. Run in stream mode +executor = AgentExecutor(agent=agent, tools=[get_price], verbose=False) + +# Run +input_message = "What is the price of milk and bread in Kazan?" + +# Stream +stream = executor.stream( + {"messages": [HumanMessage(content=input_message)]}, + stream_mode=["messages"], +) + +step = 1 +for chunk in stream: + chunk_type, chunk_data = chunk + if chunk_type == "messages": + message, meta = chunk_data + if meta["langgraph_step"] != step: + step = meta["langgraph_step"] + print("\n---------\n") + if message.content: + print(message.content, end="", flush=True) + +# Print final result +print("\n\n---\n" + executor.run({"messages": [HumanMessage(content=input_message)]})["output"])