我们有的工具包括: A. 一个能够查询商品信息,为用户进行商品导购的工具 B. 一个能够查询订单信息,获得最新的订单情况的工具 C. 一个能够搜索商家的退换货政策、运费、物流时长、支付渠道、覆盖国家的工具 D. 都不合适 """ multiple_choice_prompt = PromptTemplate(template=multiple_choice, input_variables=["question"]) choice_chain = LLMChain(llm=llm, prompt=multiple_choice_prompt, output_key="answer")
tools = [ Tool( name = "Search Order",func=search_order, description="useful for when you need to answer questions about customers orders" ), Tool(name="Recommend Product", func=recommend_product, description="useful for when you need to answer questions about product recommendations" ), Tool(name="FAQ", func=faq, description="useful for when you need to answer questions about shopping policies, like return policy, shipping policy, etc." ) ]
question = "我想买一件衣服,但是不知道哪个款式好看,你能帮我推荐一下吗?" result = agent.run(question) print(result)
输出结果:
1 2 3 4 5 6 7 8
> Entering new AgentExecutor chain... I need to recommend a product. Action: Recommend Product Action Input: Clothes Observation: 红色连衣裙 Thought: I now know the final answer. Final Answer: 我推荐红色连衣裙。
问题2:
1 2 3 4
question = "我有一张订单,订单号是 2022ABCDE,一直没有收到,能麻烦帮我查一下吗?" result = agent.run(question) print(result)
输出结果:
1 2 3 4 5 6 7 8 9 10
> Entering new AgentExecutor chain... I need to find out the status of the order Action: Search Order Action Input: 2022ABCDE Observation: 订单状态:已发货;发货日期:2023-01-01;预计送达时间:2023-01-10 Thought: I now know the final answer Final Answer: 您的订单 2022ABCDE 已发货,预计将于2023-01-10送达。 > Finished chain. 您的订单 2022ABCDE 已发货,预计将于2023-01-10送达。
问题3:
1 2 3 4
question = "请问你们的货,能送到三亚吗?大概需要几天?" result = agent.run(question) print(result)
输出结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
> Entering new AgentExecutor chain... I need to find out the shipping policy and delivery time Action: FAQ Action Input: Shipping policy and delivery time Observation: 7天无理由退货 Thought: I need to find out the delivery time Action: FAQ Action Input: Delivery time Observation: 7天无理由退货 Thought: I need to find out if we can deliver to Sanya Action: FAQ Action Input: Delivery to Sanya Observation: 7天无理由退货 Thought: I now know the final answer Final Answer: 我们可以把货送到三亚,大概需要7天。 > Finished chain. 我们可以把货送到三亚,大概需要7天。
# flake8: noqa PREFIX = """Answer the following questions as best you can. You have access to the following tools:""" FORMAT_INSTRUCTIONS = """Use the following format:
Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [{tool_names}] Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question""" SUFFIX = """Begin!
> Entering new AgentExecutor chain... I need to find out the shipping policy Action: FAQ Action Input: Shipping policy Observation: 7天无理由退货 Thought: I need to find out the shipping time Action: FAQ Action Input: Shipping time Observation: 7天无理由退货 Thought: > Finished chain. === Agent stopped due to max iterations. ===
from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import FAISS from langchain.text_splitter import SpacyTextSplitter from langchain import OpenAI, VectorDBQA from langchain.document_loaders import TextLoader
@tool("FAQ") deffaq(intput: str) -> str: """"useful for when you need to answer questions about shopping policies, like return policy, shipping policy, etc.""" return faq_chain.run(intput)
tools = [ Tool( name = "Search Order",func=search_order, description="useful for when you need to answer questions about customers orders" ), Tool(name="Recommend Product", func=recommend_product, description="useful for when you need to answer questions about product recommendations" ), faq ]
question = "请问你们的货,能送到三亚吗?大概需要几天?" result = agent.run(question) print(result)
输出结果:
1 2 3 4 5 6 7 8 9 10 11 12
> Entering new AgentExecutor chain... I need to find out the shipping policy and delivery time. Action: FAQ Action Input: shipping policy and delivery time > Entering new VectorDBQA chain... > Finished chain. Observation: 一般情况下,大部分城市的订单在2-3个工作日内送达,偏远地区可能需要5-7个工作日。具体送货时间可能因订单商品、配送地址和物流公司而异。 Thought: I now know the final answer. Final Answer: 一般情况下,大部分城市的订单在2-3个工作日内送达,偏远地区可能需要5-7个工作日。具体送货时间可能因订单商品、配送地址和物流公司而异。 > Finished chain. 一般情况下,大部分城市的订单在2-3个工作日内送达,偏远地区可能需要5-7个工作日。具体送货时间可能因订单商品、配送地址和物流公司而异。
@tool("FAQ") deffaq(intput: str) -> str: """"useful for when you need to answer questions about shopping policies, like return policy, shipping policy, etc.""" return faq_chain.run(intput)
@tool("Recommend Product") defrecommend_product(input: str) -> str: """"useful for when you need to search and recommend products and recommend it to the user""" return product_chain.run(input)
tools = [ Tool( name = "Search Order",func=search_order, description="useful for when you need to answer questions about customers orders" ), recommend_product, faq]
> Entering new AgentExecutor chain... I need to recommend a product to the user. Action: Recommend Product Action Input: Clothing for park in spring > Entering new VectorDBQA chain... > Finished chain. Observation: 休闲、简约风格的长款风衣,休闲、运动风格的长款卫衣,清新、甜美风格的长袖连衣裙都可以在春天去公园穿着。 Thought: I now know the final answer. Final Answer: 休闲、简约风格的长款风衣,休闲、运动风格的长款卫衣,清新、甜美风格的长袖连衣裙都可以在春天去公园穿着。 > Finished chain. 休闲、简约风格的长款风衣,休闲、运动风格的长款卫衣,清新、甜美风格的长袖连衣裙都可以在春天去公园穿着。
@tool("Search Order") defsearch_order(input:str)->str: """useful for when you need to answer questions about customers orders""" ifinput.strip() == ORDER_1: return json.dumps(ORDER_1_DETAIL) elifinput.strip() == ORDER_2: return json.dumps(ORDER_2_DETAIL) else: returnf"对不起,根据{input}没有找到您的订单"
> Entering new AgentExecutor chain... I need to find out the status of the order. Action: Search Order Action Input: 2022ABCDE Observation: 对不起,根据2022ABCDE没有找到您的订单 Thought: I need to find out more information about the order. Action: Search Order Action Input: 2022ABCDE Observation: 对不起,根据2022ABCDE没有找到您的订单 Thought: I need to contact customer service for more information. Action: FAQ Action Input: 订单查询 > Entering new VectorDBQA chain... > Finished chain. Observation: 要查询订单,请登录您的帐户,然后点击“我的订单”页面。在此页面上,您可以查看所有订单及其当前状态。如果您没有帐户,请使用订单确认电子邮件中的链接创建一个帐户。如果您有任何问题,请联系客服。 Thought: I now know the final answer. Final Answer: 要查询订单,请登录您的帐户,然后点击“我的订单”页面。在此页面上,您可以查看所有订单及其当前状态。如果您没有帐户,请使用订单确认电子邮件中的链接创建一个帐户。如果您有任何问题,请联系客服。 > Finished chain. 要查询订单,请登录您的帐户,然后点击“我的订单”页面。在此页面上,您可以查看所有订单及其当前状态。如果您没有帐户,请使用订单确认电子邮件中的链接创建一个帐户。如果您有任何问题,请联系客服。
@tool("Search Order", return_direct=True) defsearch_order(input:str)->str: """useful for when you need to answer questions about customers orders""" pattern = r"\d+[A-Z]+" match = re.search(pattern, input)
> Entering new AgentExecutor chain... Thought: Do I need to use a tool? Yes Action: Search Order Action Input: 我有一张订单,一直没有收到,能麻烦帮我查一下吗? Observation: 请问您的订单号是多少?
> Entering new AgentExecutor chain... Thought: Do I need to use a tool? Yes Action: Search Order Action Input: 20230101ABC Observation: 尊敬的用户,您的订单信息如下: 订单号:20230101ABC 订单状态:已发货 发货日期:2023年1月3日 预计送达日期:2023年1月5日 如有任何疑问,请随时联系我们。感谢您的购买!
> Entering new AgentExecutor chain... Thought: Do I need to use a tool? Yes Action: FAQ Action Input: 退货政策 > Entering new VectorDBQA chain... > Finished chain. Observation: 自收到商品之日起7天内,如产品未使用、包装完好,您可以申请退货。某些特殊商品可能不支持退货,请在购买前查看商品详情页面的退货政策。 Thought:Do I need to use a tool? No AI: Our return policy allows for returns within 7 days of receiving the product, as long as the product is unused andin its original packaging. Some special products may not be eligible for returns, so please check the product details page before purchasing. > Finished chain. Our return policy allows for returns within 7 days of receiving the product, as long as the product is unused andin its original packaging. Some special products may not be eligible for returns, so please check the product details page before purchasing.