alanchen1115 commited on
Commit
a181d37
·
verified ·
1 Parent(s): 66588d7

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +24 -12
main.py CHANGED
@@ -24,10 +24,10 @@ from linebot.models import (
24
  from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
25
  from langchain_core.tools import tool
26
  from langchain_google_genai import ChatGoogleGenerativeAI
27
- # 直接使用標準路徑,不再使用 try-except
28
- # 從具體路徑匯入 AgentExecutor
29
- from langchain.agents import AgentExecutor
30
- from langchain.agents import create_tool_calling_agent
31
 
32
  # ==========================
33
  # 環境設定與工具函式
@@ -185,17 +185,29 @@ def analyze_image_with_text(image_path: str, user_text: str) -> str:
185
  # LangChain 代理人設定
186
  # ==========================
187
  tools = [generate_and_upload_image, analyze_image_with_text]
188
- # 修正模型:gemini-3-flash-preview 目前在正式 API 中通常不可用
 
 
 
 
 
 
 
 
 
 
 
189
  llm = ChatGoogleGenerativeAI(model="gemini-3-flash-preview", temperature=0.4)
190
 
191
- prompt_template = ChatPromptTemplate.from_messages([
192
- ("system", "你是一個強大的代理人。如果生成了圖片,請直接給出 URL。"),
193
- ("user", "{input}"),
194
- MessagesPlaceholder(variable_name="agent_scratchpad"),
195
- ])
196
 
197
- agent = create_tool_calling_agent(llm, tools, prompt_template)
198
- agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=False, handle_parsing_errors=True)
 
 
 
 
199
 
200
  # ==========================
201
  # FastAPI 路由
 
24
  from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
25
  from langchain_core.tools import tool
26
  from langchain_google_genai import ChatGoogleGenerativeAI
27
+ from langgraph.prebuilt import create_react_agent
28
+ # # 從具體路徑匯入 AgentExecutor
29
+ # from langchain.agents import AgentExecutor
30
+ # from langchain.agents import create_tool_calling_agent
31
 
32
  # ==========================
33
  # 環境設定與工具函式
 
185
  # LangChain 代理人設定
186
  # ==========================
187
  tools = [generate_and_upload_image, analyze_image_with_text]
188
+ # llm = ChatGoogleGenerativeAI(model="gemini-3-flash-preview", temperature=0.4)
189
+
190
+ # prompt_template = ChatPromptTemplate.from_messages([
191
+ # ("system", "你是一個強大的代理人。如果生成了圖片,請直接給出 URL。"),
192
+ # ("user", "{input}"),
193
+ # MessagesPlaceholder(variable_name="agent_scratchpad"),
194
+ # ])
195
+
196
+ # agent = create_tool_calling_agent(llm, tools, prompt_template)
197
+ # agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=False, handle_parsing_errors=True)
198
+
199
+ # 1. 建立 LLM 模型
200
  llm = ChatGoogleGenerativeAI(model="gemini-3-flash-preview", temperature=0.4)
201
 
202
+ # 2. 定義 System Prompt (取代原本的 prompt_template)
203
+ system_prompt = "你是一個強大的代理人。如果生成了圖片,請直接給出 URL。"
 
 
 
204
 
205
+ # 3. 直接建立包含 Executor 功能的 Agent
206
+ agent_executor = create_react_agent(
207
+ llm,
208
+ tools,
209
+ state_modifier=system_prompt
210
+ )
211
 
212
  # ==========================
213
  # FastAPI 路由