Spaces:
Running
Running
Commit ·
81736e4
1
Parent(s): 6ff45ca
fix : agent error
Browse files- services/agents.py +10 -9
services/agents.py
CHANGED
|
@@ -17,6 +17,7 @@ import json # 匯入 json 庫用於序列化
|
|
| 17 |
from langchain.agents import create_agent
|
| 18 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 19 |
from langchain.tools import tool
|
|
|
|
| 20 |
|
| 21 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 22 |
|
|
@@ -235,9 +236,7 @@ llm = ChatGoogleGenerativeAI(
|
|
| 235 |
#])
|
| 236 |
|
| 237 |
# ✅ 建立 Prompt (新版語法)
|
| 238 |
-
|
| 239 |
-
("system",
|
| 240 |
-
"""
|
| 241 |
你是一個強大的圖像生成、圖像去模糊與問答助理,可以根據用戶的請求使用提供的工具。
|
| 242 |
### 核心輸出規範
|
| 243 |
* **結果呈現**:當你執行以下任一圖像處理工具成功後,你最終的回答 output **必須包含該 URL 的完整資訊**:
|
|
@@ -246,10 +245,6 @@ prompt = ChatPromptTemplate.from_messages([
|
|
| 246 |
|
| 247 |
* **錯誤處理**:如果工具有產生錯誤訊息,請解讀錯誤並以自然語言回應給用戶。
|
| 248 |
"""
|
| 249 |
-
),
|
| 250 |
-
("user", "{input}"),
|
| 251 |
-
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
| 252 |
-
])
|
| 253 |
|
| 254 |
# 建立工具調用代理人 (Tool Calling Agent)
|
| 255 |
#agent = create_tool_calling_agent(llm, tools, prompt_template)
|
|
@@ -262,11 +257,17 @@ prompt = ChatPromptTemplate.from_messages([
|
|
| 262 |
agent = create_agent(
|
| 263 |
model=llm,
|
| 264 |
tools=tools,
|
| 265 |
-
system_prompt=
|
| 266 |
)
|
| 267 |
|
| 268 |
def run_agent(user_input: str):
|
| 269 |
"""呼叫此函式來執行 Agent"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
return agent.invoke({
|
| 271 |
-
"messages": [{
|
| 272 |
})
|
|
|
|
| 17 |
from langchain.agents import create_agent
|
| 18 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 19 |
from langchain.tools import tool
|
| 20 |
+
from langchain.messages import SystemMessage, HumanMessage
|
| 21 |
|
| 22 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 23 |
|
|
|
|
| 236 |
#])
|
| 237 |
|
| 238 |
# ✅ 建立 Prompt (新版語法)
|
| 239 |
+
sys_prompt = """
|
|
|
|
|
|
|
| 240 |
你是一個強大的圖像生成、圖像去模糊與問答助理,可以根據用戶的請求使用提供的工具。
|
| 241 |
### 核心輸出規範
|
| 242 |
* **結果呈現**:當你執行以下任一圖像處理工具成功後,你最終的回答 output **必須包含該 URL 的完整資訊**:
|
|
|
|
| 245 |
|
| 246 |
* **錯誤處理**:如果工具有產生錯誤訊息,請解讀錯誤並以自然語言回應給用戶。
|
| 247 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
# 建立工具調用代理人 (Tool Calling Agent)
|
| 250 |
#agent = create_tool_calling_agent(llm, tools, prompt_template)
|
|
|
|
| 257 |
agent = create_agent(
|
| 258 |
model=llm,
|
| 259 |
tools=tools,
|
| 260 |
+
system_prompt=sys_prompt
|
| 261 |
)
|
| 262 |
|
| 263 |
def run_agent(user_input: str):
|
| 264 |
"""呼叫此函式來執行 Agent"""
|
| 265 |
+
#human_msg = HumanMessage( user_input )
|
| 266 |
+
#sys_msg = SystemMessage(sys_prompt)
|
| 267 |
+
#messages = [
|
| 268 |
+
# sys_msg,
|
| 269 |
+
# human_msg
|
| 270 |
+
#]
|
| 271 |
return agent.invoke({
|
| 272 |
+
"messages": [{"role": "user", "content": user_input }]
|
| 273 |
})
|