# [LangChain] 範例-旅遊花費計算並判斷結果 日期:2023/09/18 ## 敘述 - 主題:initialize_agent()、SimpleSequentialChain() - 使用情境:。透過此程式,便可將旅遊花費敘述丟到其中讓其進行計算,並且根據預先設定的金額區間進行分類。 - 輸入:如上記帳範例 - 輸出:加總花費金額,並根據金額回覆分類結果 ## 目錄 - [1. 定義chain](#1-定義chain) - [2. 輸入輸出](#2-輸入輸出) - [補充](#補充) ____________________ ## 1. 定義chain 導入套件 ```python= from dotenv import load_dotenv load_dotenv() from langchain.chat_models import ChatOpenAI from langchain.prompts import ( PromptTemplate ) from langchain import LLMChain llm = ChatOpenAI(temperature=0) ``` ```python= from langchain.agents import initialize_agent from langchain.agents import AgentType from langchain.agents import load_tools from langchain.chains import SimpleSequentialChain ``` 分別定義chain1、chain2,並使用SimpleSequentialChain()所有chain串接。 chain1為Agent,使用工具為llm-math,這個chain將用於加總輸入的旅遊花費項目。 chain2為LLMChain,輸入為chain1計算出的總花費金額,根據這個金額確認prompt中的哪個條件,並產生對應輸出。 備註: ※ 提供給initialize_agent()的模型,可考慮使用OpenAI(),而非ChatOpenAI(),可讓模型對文字的理解更加正確,進而產生正確的結果。 詳細參考[補充](#補充) ```python= #定義Chain1 #載入tool tools = load_tools(["llm-math"], llm=llm) #定義agent agent = initialize_agent(tools, #OpenAI(), #若model對於prompt的理解不夠正確,可嘗試將下一行的llm換成OpenAI() llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) chain_one = agent #定義Chain2 template = """<>內為我某次出去旅遊的花費,請根據以下方式判斷如何輸出。 條件: 1. 花費若'小於5000',請輸出: 總花費、『提醒您,本次花費非常節省。』 2. 花費若於'5000到7500之間',請輸出: 總花費、『提醒您,本次花費適中。』 3. 花費若於'7500到10000之間',請輸出: 總花費、『提醒您,本次花費超出預算。』 4. 花費'超過10000',請輸出: 總花費、『提醒您,本次花費嚴重超標。』 <{money}>""" chain_two = LLMChain.from_string(llm=llm, template=template) overall_chain = SimpleSequentialChain( chains=[chain_one, chain_two], verbose=True) ``` ## 2. 輸入輸出 ```python= #輸入文本 input_text = """ 這趟旅行中分別花了這些費用,車資2000,住宿1500,幫自己和其他兩人付早餐錢各140, 自己和女朋友午餐錢各660,自己和女朋友晚餐錢各450,沒有吃消夜, 點心花了30,飲料錢70。 上述是我旅遊的花費。 幫我加總所有金額。 """ overall_chain.run(input_text) ``` :::success \> Entering new SimpleSequentialChain chain... \> Entering new AgentExecutor chain... I need to add up all the expenses listed. Action: Calculator Action Input: 2000 + 1500 + (140 * 3) + (660 * 2) + (450 * 2) + 30 + 70 Observation: Answer: 6240 Thought:I now know the final answer Final Answer: The total expenses amount to 6240. \> Finished chain. The total expenses amount to 6240. 總花費為6240,屬於5000到7500之間,因此輸出為: 總花費、『提醒您,本次花費適中。』 \> Finished chain. '總花費為6240,屬於5000到7500之間,因此輸出為:\n總花費、『提醒您,本次花費適中。』' ::: 程式輸出結果為:總花費為6240,屬於5000到7500之間,因此輸出為:\n總花費、『提醒您,本次花費適中。 透過定義好的chain幫忙完成計算並進行分類。 ## 補充 實際測試時發現,既使已明確地要求要如何產生輸出,模型仍沒辦法很好地理解。若此時改使用OpenAI()將會改善結果。 狀況可參考以下例子: chain2改使用以下prompt。每個條件中敘述區間值-『小於5000』的位置少了''符號,將使用這個prompt觀察OpenAI()和OpenAIChat()各自產生的結果。 ```python= template = """<>內為我某次出去旅遊的花費,請根據以下方式判斷如何輸出。 條件: 1. 花費若小於5000,請輸出: 總花費、『提醒您,本次花費非常節省。』 2. 花費若於5000到7500之間,請輸出: 總花費、『提醒您,本次花費適中。』 3. 花費若於7500到10000之間,請輸出: 總花費、『提醒您,本次花費超出預算。』 4. 花費超過10000,請輸出: 總花費、『提醒您,本次花費嚴重超標。』 <{money}>""" ``` **使用OpenAIChat()** 預期輸出應該如同prompt,僅有總花費和後面的文字;然而下面的輸出多了許多額外的字眼,並且出現分類錯誤的問題。 :::success \> Entering new SimpleSequentialChain chain... \> Entering new AgentExecutor chain... I need to add up all the expenses listed. Action: Calculator Action Input: 3600 + 1500 + (140*3) + (660*2) + (450*2) + 0 + 30 + 70 + 0 Observation: Answer: 7840 Thought:I now know the final answer Final Answer: The total expenses amount to 7840. \> Finished chain. The total expenses amount to 7840. 總花費為7840,根據條件2,輸出為『提醒您,本次花費適中。』 \> Finished chain. '總花費為7840,根據條件2,輸出為『提醒您,本次花費適中。』' ::: **使用OpenAI()** 同樣的prompt,將model換成OpenAI()以後,可發現輸出的表現變好了。 :::success \> Entering new SimpleSequentialChain chain... \> Entering new AgentExecutor chain... I need to add all of these expenses together Action: Calculator Action Input: 3600 + 1500 + (140*3) + (660*2) + (450*2) + 0 + 30 + 70 + 0 Observation: Answer: 7840 Thought: I now know the final answer Final Answer: 旅遊所有花費共 7840 元。 \> Finished chain. 旅遊所有花費共 7840 元。 旅遊所有花費共 7840 元。 提醒您,本次花費超出預算。 \> Finished chain. '旅遊所有花費共 7840 元。\n提醒您,本次花費超出預算。' :::