agents agents
代理可以讓語言模型能夠使用外部工具來回答問題。通過定義工具並將其與 agents
結合,語言模型可以執行複雜的任務,例如計算、網頁搜尋、數據處理等。agents
支援同步與非同步操作,並提供詳細的日誌記錄功能。
agents 功能
工具整合 :支持將自定義工具或內建工具與語言模型結合使用。
多輪對話 :支持多輪對話,並根據上下文進行推理。
流式輸出 :支持流式輸出回答,適合需要即時回應的場景。
日誌保存 :支持保存執行過程與結果的日誌,便於後續分析。
MCP 支援 :可以作為 MCP(Model Context Protocol)客戶端,調用 MCP 伺服器上的工具。
agents 範例 定義自訂工具並使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 import akashafrom datetime import datetimedef today_f (): now = datetime.now() return "today's date: " + str (now.strftime("%Y-%m-%d %H:%M:%S" )) today_tool = akasha.create_tool( "today_date_tool" , "This is the tool to get today's date, the tool doesn't have any input parameter." , today_f, ) agent = akasha.agents( tools=[today_tool], model="openai:gpt-4o" , temperature=1.0 , verbose=True , keep_logs=True , ) response = agent("今天幾月幾號?" ) print (response)agent.save_logs("logs.json" )
使用內建工具 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import akasha.agent.agent_tools as attool_list = [at.websearch_tool(search_engine="brave" ), at.saveJSON_tool()] agent = akasha.agents( tools=tool_list, model="openai:gpt-4o" , temperature=1.0 , max_input_tokens=8000 , verbose=True , keep_logs=True , ) response = agent("用網頁搜尋工業4.0,並將資訊存成json檔iii.json" ) print (response)agent.save_logs("logs.json" )
使用 MCP 伺服器上的工具 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import asyncioimport akashafrom langchain_mcp_adapters.client import MultiServerMCPClientMODEL = "openai:gpt-4o" connection_info = { "math" : { "command" : "python" , "args" : ["cal_server.py" ], "transport" : "stdio" , }, "weather" : { "url" : "http://localhost:8000/sse" , "transport" : "sse" , }, } prompt = "tell me the weather in Taipei" agent = akasha.agents( model=MODEL, temperature=1.0 , verbose=True , keep_logs=True , ) response = agent.mcp_agent(connection_info, prompt) agent.save_logs("logs_agent.json" )
agents 參數 初始化參數 工具列表,可以是自定義工具或內建工具。
model: str 使用的語言模型,例如 "openai:gpt-4o"
或 "openai:gpt-3.5-turbo"
。
temperature: float 語言模型的隨機性參數,範圍為 0.0 到 1.0。
單次輸入模型的最大 token 數。
max_output_tokens: int 模型輸出的最大 token 數。
max_round: int 多輪對話的最大輪數。
max_past_observation: int 發送給模型的過去觀察數量。
提示格式類型,例如 "auto"
、"gpt"
、"llama"
。
retri_observation: bool 是否啟用觀察檢索。
keep_logs: bool 是否保存執行過程與結果的日誌。
verbose: bool 是否顯示詳細的執行過程。
stream: bool 是否啟用流式輸出。
call 參數question: str 使用者的問題。
messages: List[dict] 需要一併提供給語言模型的對話紀錄。
日誌與結果
執行過程與結果會保存到指定的日誌文件中。
日誌包括工具使用情況、語言模型的輸入輸出 token 數等。
相關連結