Files changed (1) hide show
  1. app.py +74 -1
app.py CHANGED
@@ -1,3 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import openai
2
  import gradio as gr
3
  from langchain_classic import LLMChain, OpenAI, PromptTemplate
@@ -31,4 +103,5 @@ iface = gr.Interface(
31
  description="哞哞哞"
32
  )
33
 
34
- iface.launch()
 
 
1
+ import gradio as gr
2
+ from langchain_core.prompts import ChatPromptTemplate
3
+ from langchain_core.output_parsers import StrOutputParser
4
+ from langchain_openai import ChatOpenAI # 需要 OPENAI_API_KEY 環境變數
5
+
6
+ def agent1(input_text):
7
+ return f"Agent1: {input_text.upper()}"
8
+
9
+ def agent2(input_from_agent1):
10
+ return f"Agent2: {input_from_agent1} processed!"
11
+
12
+ # LangChain Chain 定義
13
+ prompt_template = ChatPromptTemplate.from_template(
14
+ "你是航空公司客服助理。用戶詢問航班資訊時,提供法蘭克福直飛行程(來回經濟艙NT$33,000),並建議轉機慕尼黑。\n{input}"
15
+ )
16
+
17
+ model = ChatOpenAI(model="gpt-4o-mini")
18
+ parser = StrOutputParser()
19
+ chain = prompt_template | model | parser
20
+
21
+ def generate_response(message, history):
22
+ response = chain.invoke({"input": message})
23
+ return response
24
+
25
+ # 自訂 CSS 移除邊框和陰影
26
+ custom_css = """
27
+ .gradio-container * .svelte-1pipte input { border: none !important; box-shadow: none !important; }
28
+ button { border: none !important; box-shadow: none !important; }
29
+ textarea { border: none !important; box-shadow: none !important; }
30
+ """
31
+
32
+ # Gradio 介面,使用無邊框主題
33
+ with gr.Blocks(
34
+ title="Multi-Agent & Chatbot",
35
+ theme=gr.themes.Default(radius_size=gr.themes.sizes.radius_none),
36
+ css=custom_css
37
+ ) as demo:
38
+ gr.Markdown("# 互動式 Multi-Agent 與牛牛機器人")
39
+
40
+ with gr.Tabs():
41
+ with gr.Tab("Multi-Agent"):
42
+ with gr.Row():
43
+ input1 = gr.Textbox(
44
+ label="輸入給 Agent1",
45
+ value="農曆春節期間, 貴公司有沒有直飛到德國慕尼黑的行程?",
46
+ container=False
47
+ )
48
+ btn1 = gr.Button("執行 Agent1", container=False)
49
+ output1 = gr.Textbox(label="Agent 1 輸出", interactive=False, container=False)
50
+
51
+ with gr.Row():
52
+ btn2 = gr.Button("執行 Agent2", container=False)
53
+ output2 = gr.Textbox(label="Agent 2 輸出", interactive=False, container=False)
54
+
55
+ # 事件綁定
56
+ btn1.click(agent1, inputs=input1, outputs=output1)
57
+ btn2.click(agent2, inputs=output1, outputs=output2)
58
+
59
+ with gr.Tab("牛牛機器人"):
60
+ gr.ChatInterface(
61
+ generate_response,
62
+ title="Tour Inquiry Chatbot",
63
+ description="詢問航班資訊,例如訂票或轉機建議。",
64
+ examples=[
65
+ "農曆春節期間有直飛慕尼黑嗎?",
66
+ "訂三張法蘭克福來回經濟艙。"
67
+ ]
68
+ )
69
+
70
+ if __name__ == "__main__":
71
+ demo.launch()
72
+ """
73
  import openai
74
  import gradio as gr
75
  from langchain_classic import LLMChain, OpenAI, PromptTemplate
 
103
  description="哞哞哞"
104
  )
105
 
106
+ iface.launch()
107
+ """