Add main_eng.py with English content
This commit is contained in:
@@ -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"])
|
||||
Reference in New Issue
Block a user