iT邦幫忙

2026 iThome 鐵人賽

DAY 3
0
自我挑戰組

AI Agent 從零開始系列 第 3

自己寫第一個 Agent

  • 分享至 

  • xImage
  •  
// tools.py
def calculator(expression):
    return eval(expression)


def get_weather(city):
    weather = {
        "台南": 32,
        "台北": 29,
        "高雄": 33,
    }

    return weather.get(city, "unknown")
// agent.py
def run_agent(user_input):

    state = [
        {
            "role": "user",
            "content": user_input
        }
    ]

    while True:

        response = fake_llm(state)

        if response["type"] == "final":
            return response["content"]

        if response["type"] == "tool_call":

            tool_name = response["tool"]
            arguments = response["arguments"]

            tool = tools[tool_name]

            result = tool(**arguments)

            state.append({
                "role": "assistant",
                "content": response
            })

            state.append({
                "role": "tool",
                "content": result
            })

上一篇
AI Agent 到底是什麼?
下一篇
LLM 究竟能做什麼?不能做什麼?
系列文
AI Agent 從零開始5
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言