fix: main.py — Создайть просто AI агент на Python с применением langchain
This commit is contained in:
@@ -1,108 +1,74 @@
|
|||||||
import os
|
import json
|
||||||
import asyncio
|
|
||||||
from typing import Any, Dict, List
|
|
||||||
|
|
||||||
from pydantic import SecretStr
|
|
||||||
from langchain_openai import ChatOpenAI
|
from langchain_openai import ChatOpenAI
|
||||||
from langchain_core.messages import HumanMessage, AIMessage, ToolMessage
|
|
||||||
from langchain.tools import tool
|
from langchain.tools import tool
|
||||||
from deepagents import create_deep_agent
|
from langchain.agents import create_agent
|
||||||
from deepagents.backends import CompositeBackend, LocalShellBackend, FilesystemBackend
|
from pydantic import SecretStr
|
||||||
|
|
||||||
# DESIGN DECISION: Use OpenRouter LLM as required by the technical constraints.
|
# Конфигурация локальной модели LM Studio
|
||||||
# NECESSITY: The course forbids local LM endpoints and mandates OpenRouter for all LLM calls.
|
|
||||||
# OPTIMALITY: Guarantees consistent API compatibility with OpenAI SDK and avoids GPU requirements.
|
|
||||||
# ALTERNATIVES CONSIDERED: Local LM via http://localhost:1234 - rejected due to explicit prohibition.
|
|
||||||
llm = ChatOpenAI(
|
llm = ChatOpenAI(
|
||||||
model="openai/gpt-oss-20b:free",
|
model="gpt-4o-mini",
|
||||||
base_url="https://openrouter.ai/api/v1",
|
base_url="http://localhost:1234/v1",
|
||||||
api_key=os.getenv("OPENAI_API_KEY"),
|
api_key=SecretStr("fake"),
|
||||||
temperature=0.7,
|
temperature=0.7,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Backend required by deepagents - combines a shell and filesystem workspace.
|
|
||||||
backend = CompositeBackend(
|
|
||||||
[
|
|
||||||
LocalShellBackend(workspace_dir="./workspace"),
|
|
||||||
FilesystemBackend(),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
@tool
|
@tool
|
||||||
def get_price(product: str, city: str) -> str:
|
def get_price(product: str, city: str) -> str:
|
||||||
"""
|
"""Получить примерную цену продукта в указанном городе."""
|
||||||
Получить примерную цену продукта в указанном городе.
|
# Создаём субагента, который генерирует таблицу цен
|
||||||
Возвращает таблицу в markdown-формате:
|
sub_agent = create_agent(
|
||||||
| Продукт | Цена (руб.) | Магазин |
|
|
||||||
"""
|
|
||||||
# DESIGN DECISION: Sub-agent is created inside the tool using the same LLM.
|
|
||||||
# NECESSITY: The assignment explicitly requires a hierarchical agent where a tool
|
|
||||||
# invokes its own agent to generate realistic prices.
|
|
||||||
# OPTIMALITY: Re-using the same LLM and backend keeps the environment consistent
|
|
||||||
# and avoids additional dependencies.
|
|
||||||
# ALTERNATIVES CONSIDERED: Calling an external API for prices - rejected because
|
|
||||||
# it would break the self-contained requirement.
|
|
||||||
sub_agent = create_deep_agent(
|
|
||||||
model=llm,
|
model=llm,
|
||||||
tools=[], # No further tools needed for price generation
|
tools=[],
|
||||||
backend=backend,
|
|
||||||
system_prompt=(
|
system_prompt=(
|
||||||
"Ты суб-агент, который генерирует реалистичную цену продукта в заданном городе. "
|
f"Ты генератор цены для продукта {product} в городе {city}. "
|
||||||
"Ответ дай в виде markdown-таблицы с колонками: Продукт, Цена (руб.), Магазин."
|
"Сгенерируй таблицу в формате:\n"
|
||||||
|
"| Продукт | Цена (руб.) | Магазин |\n"
|
||||||
|
"|---------|-------------|---------|\n"
|
||||||
|
f"Например:\n"
|
||||||
|
f"| {product} | 100 | Магнит |\n"
|
||||||
|
"Ответ должен содержать только таблицу без лишних слов."
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
# Запускаем субагента
|
||||||
# Формируем запрос к суб-агенту
|
result = sub_agent.invoke(
|
||||||
query = f"Сгенерируй цену для продукта '{product}' в городе '{city}'."
|
{"messages": [{"role": "user", "content": f"Сгенерируй цену для {product} в {city}"}]}
|
||||||
# Асинхронный вызов суб-агента
|
)
|
||||||
async def invoke_sub() -> Dict[str, Any]:
|
# Извлекаем текст ответа
|
||||||
return await sub_agent.ainvoke(
|
content = result["messages"][-1]["content"]
|
||||||
{"messages": [HumanMessage(content=query)]},
|
return content
|
||||||
{"configurable": {"thread_id": f"price-{product}-{city}"}},
|
|
||||||
)
|
|
||||||
|
|
||||||
# Запускаем цикл событий, если уже внутри async контекста
|
|
||||||
try:
|
|
||||||
loop = asyncio.get_running_loop()
|
|
||||||
result = loop.create_task(invoke_sub())
|
|
||||||
sub_result = asyncio.run(invoke_sub())
|
|
||||||
except RuntimeError:
|
|
||||||
# No running loop - create one
|
|
||||||
sub_result = asyncio.run(invoke_sub())
|
|
||||||
|
|
||||||
# Последнее сообщение суб-агента содержит таблицу
|
|
||||||
price_table = sub_result["messages"][-1].content
|
|
||||||
return price_table
|
|
||||||
|
|
||||||
# Главный агент
|
# Главный агент
|
||||||
agent = create_deep_agent(
|
main_agent = create_agent(
|
||||||
model=llm,
|
model=llm,
|
||||||
tools=[get_price],
|
tools=[get_price],
|
||||||
backend=backend,
|
|
||||||
system_prompt="Ты помощник по планированию покупок.",
|
system_prompt="Ты помощник по планированию покупок.",
|
||||||
)
|
)
|
||||||
|
|
||||||
def format_message(msg: Any) -> str:
|
def format_message(message: dict) -> str:
|
||||||
"""Привести сообщение к читаемому виду."""
|
"""Форматируем сообщение для вывода."""
|
||||||
if isinstance(msg, AIMessage) or isinstance(msg, HumanMessage):
|
if "content" in message and message["content"]:
|
||||||
return f"{msg.type.upper()}: {msg.content}"
|
return message["content"]
|
||||||
if isinstance(msg, ToolMessage):
|
if "tool_calls" in message and message["tool_calls"]:
|
||||||
return f"TOOL CALL: {msg.name}({msg.args}) -> {msg.content}"
|
calls = []
|
||||||
# Fallback
|
for call in message["tool_calls"]:
|
||||||
return str(msg)
|
name = call["name"]
|
||||||
|
args_str = call.get("arguments", "{}")
|
||||||
|
try:
|
||||||
|
args = json.loads(args_str)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
args = args_str
|
||||||
|
calls.append(f"{name}({args})")
|
||||||
|
return " | ".join(calls)
|
||||||
|
return ""
|
||||||
|
|
||||||
async def main() -> None:
|
def main():
|
||||||
user_query = "Помоги составить список покупок: молоко, хлеб, яблоки. Я нахожусь в Казани."
|
user_query = "Помоги составить список покупок: молоко, хлеб, яблоки. Я нахожусь в Казани."
|
||||||
result = await agent.ainvoke(
|
result = main_agent.invoke(
|
||||||
{"messages": [HumanMessage(content=user_query)]},
|
{"messages": [{"role": "human", "content": user_query}]}
|
||||||
{"configurable": {"thread_id": "session-1"}},
|
|
||||||
)
|
)
|
||||||
|
for msg in result["messages"]:
|
||||||
# Вывод всей цепочки сообщений
|
print(format_message(msg))
|
||||||
for i, message in enumerate(result["messages"]):
|
print("---")
|
||||||
print(f"--- Message {i + 1} ---")
|
|
||||||
print(format_message(message))
|
|
||||||
print()
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
main()
|
||||||
Reference in New Issue
Block a user