You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Agno框架问题:Unix时间戳正确但dd-mm-yyyy格式转换出错

问题:Agno框架中YFinance数据的毫秒级时间戳转dd-mm-yyyy格式出错

问题描述

使用Agno框架的YFinanceTools获取NVDA等股票的30天OHLCV数据时,代理能正确获取毫秒级Unix时间戳(如1738040400000对应预期日期28-01-2025),但将时间戳转为dd-mm-yyyy格式时返回错误日期。外部Python代码使用datetime.utcfromtimestamp(timestamp / 1000).strftime('%d-%m-%Y')能正常转换,但在Agno框架内即使明确给代理下达转换指令,问题仍存在。

代理配置如下:

tracked_stocks = ["NVDA"]

agent_a = Agent(
    model=OpenAIChat(id=OPENAI_MODEL_NAME, api_key=OPENAI_API_KEY, base_url=API_BASE_URL),
    tools=[YFinanceTools(stock_price=True, historical_prices=True)],  
    show_tool_calls=True,
    name="Market Data Fetcher",
    description="Fetches the latest 30 days of OHLCV stock data.",
    instructions=[
        "Retrieve the recent 30 days of OHLCV (Open, High, Low, Close, Volume) for the given stock symbol.",
        "Return the data as a structured JSON object: {date, open, high, low, close, volume}.",
    ]
)

解决方案

1. 在工具层直接处理日期转换(推荐)

LLM容易忽略指令或计算错误,最可靠的方式是在工具返回数据前完成日期转换,不让代理处理格式转换逻辑。自定义包装YFinanceTools的工具:

from agno.tools.yfinance import YFinanceTools
from datetime import datetime

class CustomYFinanceTools(YFinanceTools):
    def get_historical_prices(self, symbol, period="30d", interval="1d"):
        # 获取原始数据
        raw_data = super().get_historical_prices(symbol, period, interval)
        # 批量转换日期格式
        formatted_data = []
        for entry in raw_data:
            # 假设entry中的date字段为毫秒级时间戳整数
            timestamp_ms = entry["date"]
            formatted_date = datetime.utcfromtimestamp(timestamp_ms / 1000).strftime('%d-%m-%Y')
            formatted_entry = {**entry, "date": formatted_date}
            formatted_data.append(formatted_entry)
        return formatted_data

然后在代理中使用自定义工具替代原工具:

agent_a = Agent(
    model=OpenAIChat(id=OPENAI_MODEL_NAME, api_key=OPENAI_API_KEY, base_url=API_BASE_URL),
    tools=[CustomYFinanceTools(stock_price=True, historical_prices=True)],  
    show_tool_calls=True,
    name="Market Data Fetcher",
    description="Fetches the latest 30 days of OHLCV stock data with correctly formatted dates.",
    instructions=[
        "Retrieve the recent 30 days of OHLCV (Open, High, Low, Close, Volume) for the given stock symbol.",
        "Return the data as a structured JSON object: {date, open, high, low, close, volume}.",
    ]
)

2. 强化代理指令的约束性

如果必须让代理处理转换,需将指令写得更明确、具有强制性,避免LLM自行篡改逻辑:

instructions=[
    "Retrieve the recent 30 days of OHLCV (Open, High, Low, Close, Volume) for the given stock symbol.",
    "**严格按照以下代码逻辑转换日期格式:**",
    "1. 将毫秒级Unix时间戳除以1000转换为秒级",
    "2. 使用datetime.utcfromtimestamp()转换为UTC datetime对象",
    "3. 用strftime('%d-%m-%Y')格式化为dd-mm-yyyy字符串",
    "**禁止使用其他日期转换方式**",
    "Return the data as a structured JSON object: {date, open, high, low, close, volume}.",
]

3. 验证工具返回的原始数据格式

检查Agno工具返回的时间戳是否为预期的整数类型,避免因数据类型转换(如转为字符串)导致LLM处理错误。可在调用代理后打印原始结果:

result = agent_a.run("Get OHLCV data for NVDA")
print("Tool raw output:", result)

确认date字段是整数形式的毫秒级时间戳,而非其他格式。

内容的提问来源于stack exchange,提问作者Manas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 09:57:06