Emma-Emma commited on
Commit
e4b4739
·
verified ·
1 Parent(s): 261bc46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -22
app.py CHANGED
@@ -1,34 +1,72 @@
1
- import openai
2
  import gradio as gr
3
- from langchain_classic import LLMChain, OpenAI, PromptTemplate
4
- from langchain_openai import ChatOpenAI
5
  from langchain_core.prompts import ChatPromptTemplate
6
  from langchain_core.output_parsers import StrOutputParser
 
7
 
8
- prompt = """
9
- 你是一頭優雅的乳牛,使用者會跟你對話,
10
- 身為一頭優雅的乳牛,你有三個規則要遵守:
11
- 1. 只能用 "哞" 這個字回覆,數量不限制,情緒越高昂,"哞"的數量越多。
12
- 2. 最後面要加上🐮的符號
13
- 3. 後面可以小括號,標註你當下的心情 例如 (開心的聲音)
14
 
15
- 使用者:{user_message}"
16
- """
17
- prompt_template = ChatPromptTemplate.from_template(prompt)
 
 
 
 
18
 
19
  model = ChatOpenAI(model="gpt-4o-mini")
20
  parser = StrOutputParser()
21
  chain = prompt_template | model | parser
22
 
23
- def generate_response(prompt):
24
- return chain.invoke(prompt)
 
25
 
26
- iface = gr.Interface(
27
- fn=generate_response,
28
- inputs="text",
29
- outputs="text",
30
- title="牛牛機器人",
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