import httpx

from langchain.agents import create_agent
from langchain.chat_models import init_chat_model


APIMASTER_API_KEY = "填入你的 APIMaster.ai Key"
APIMASTER_BASE_URL = "https://apimaster.ai/v1"
MODEL_NAME = "gpt-5.4"


def get_weather(city: str) -> str:
    """Get weather for a given city."""
    return f"It's always sunny in {city}!"


def main() -> None:
    model = init_chat_model(
        MODEL_NAME,
        model_provider="openai",
        api_key=APIMASTER_API_KEY,
        base_url=APIMASTER_BASE_URL,
        http_client=httpx.Client(trust_env=False, timeout=60),
        timeout=60,
    )

    agent = create_agent(
        model=model,
        tools=[get_weather],
        system_prompt="You are a helpful assistant",
    )

    result = agent.invoke(
        {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
    )

    print(result["messages"][-1].content_blocks)


if __name__ == "__main__":
    main()
