Spaces:
Runtime error
Runtime error
Download python/app/agent.py from Tan115/tans-agents-ui: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/spaces/Tan115/tans-agents-ui/resolve/main/python/app/agent.py
- Command line
-
hf download hf://spaces/Tan115/tans-agents-ui/python/app/agent.py
-
curl -L -o agent.py https://huggingface.co/spaces/Tan115/tans-agents-ui/resolve/main/python/app/agent.py
1.28 kB
| """Core ReAct agent dùng LangGraph-style executor.""" | |
| from __future__ import annotations | |
| from typing import Optional | |
| from langchain.agents import AgentExecutor, create_tool_calling_agent | |
| from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder | |
| from app.llm import build_llm | |
| from app.tools import get_tools | |
| SYSTEM_PROMPT = ( | |
| "Bạn là Tan's Agent - một AI assistant hữu ích, trả lời ngắn gọn bằng tiếng Việt " | |
| "(trừ khi user dùng ngôn ngữ khác). Khi cần thông tin thực tế hãy dùng tool web_search. " | |
| "Khi cần tính toán hãy dùng tool calculator. Khi hỏi giờ hãy dùng current_time." | |
| ) | |
| def build_agent(provider: str = "gemini", model: Optional[str] = None) -> AgentExecutor: | |
| llm = build_llm(provider=provider, model=model) | |
| tools = get_tools() | |
| prompt = ChatPromptTemplate.from_messages([ | |
| ("system", SYSTEM_PROMPT), | |
| MessagesPlaceholder("chat_history", optional=True), | |
| ("human", "{input}"), | |
| MessagesPlaceholder("agent_scratchpad"), | |
| ]) | |
| agent = create_tool_calling_agent(llm, tools, prompt) | |
| return AgentExecutor( | |
| agent=agent, | |
| tools=tools, | |
| verbose=False, | |
| handle_parsing_errors=True, | |
| max_iterations=6, | |
| ) | |