本文将介绍如何使用使用chatglm4实现react,利用环境变量、Tavily API和ReAct代理模式来回答用户提出的问题。
环境变量
首先,我们需要加载环境变量。这可以通过使用dotenv
库来实现。
from dotenv import load_dotenv_ = load_dotenv()
注意.env文件处于当前文件同一目录,如图:
配置
接下来,我们需要配置Tavily API和OpenAI API的密钥和基础URL。
import os
from typing import Annotatedfrom tavily import TavilyClientfrom autogen import AssistantAgent, UserProxyAgent, config_list_from_json, register_function
from autogen.agentchat.contrib.capabilities import teachability
from autogen.cache import Cache
from autogen.coding import DockerCommandLineCodeExecutor, LocalCommandLineCodeExecutorconfig_list = [{ "model": "GLM-4-Plus", "api_key": os.environ["OPENAI_API_KEY"], "base_url":os.environ["OPENAI_API_BASE"]}
]
Tavily API
然后,我们需要创建一个Tavily API客户端,并定义一个搜索工具函数。
tavily = TavilyClient(api_key=os.environ["TAVILY_API_KEY"])def search_tool(query: Annotated[str, "The search query"]) -> Annotated[str, "The search results"]:return tavily.get_search_context(query=query, search_depth="advanced")
ReAct代理
接下来,我们定义一个ReAct代理模式,用于回答用户的问题。
ReAct_prompt = """
Answer the following questions as best you can. You have access to tools provided.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
Action Input: the input to the action
Observation: the result of the action
... (this process can repeat multiple times)
Thought: I now know the final answer
Final Answer: the final answer to the original input questionBegin!
Question: {input}
"""def react_prompt_message(sender, recipient, context):return ReAct_prompt.format(input=context["question"])
代码执行器
然后,我们设置一个代码执行器,用于在容器中运行代码。
os.makedirs("coding", exist_ok=True)
code_executor = LocalCommandLineCodeExecutor(work_dir="coding")
用户代理和助手代理
接下来,我们创建一个用户代理和一个助手代理,用于与用户进行交互。
user_proxy = UserProxyAgent(name="User",is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"),human_input_mode="ALWAYS",max_consecutive_auto_reply=10,code_execution_config={"executor": code_executor},
)assistant = AssistantAgent(name="Assistant",system_message="Only use the tools you have been provided with.Give the args when you need to use the tool. Reply 'TERMINATE' at the end of answer when all the task is done. ",llm_config={"config_list": config_list , "cache_seed": None},
)
注册搜索工具
然后,我们将搜索工具注册到助手代理中。
register_function(search_tool,caller=assistant,executor=user_proxy,name="search_tool",description="Search the web for the given query",
)
缓存
接下来,我们使用缓存来存储LLM的响应。
with Cache.disk(cache_seed=43) as cache:user_proxy.initiate_chat(assistant,message=react_prompt_message,question="What is the result of super bowl 2024?",cache=cache,)
输出:
[33mUser[0m (to Assistant):Answer the following questions as best you can. You have access to tools provided.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
Action Input: the input to the action
Observation: the result of the action
... (this process can repeat multiple times)
Thought: I now know the final answer
Final Answer: the final answer to the original input questionBegin!
Question: What is the result of super bowl 2024?--------------------------------------------------------------------------------
[autogen.oai.client: 11-25 17:31:24] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[33mAssistant[0m (to User):[32m***** Suggested tool call (call_-9217975427127676990): search_tool *****[0m
Arguments:
{"query": "Super Bowl 2024 result"}
[32m************************************************************************[0m--------------------------------------------------------------------------------
[31m
>>>>>>>> NO HUMAN INPUT RECEIVED.[0m
[31m
>>>>>>>> USING AUTO REPLY...[0m
[35m
>>>>>>>> EXECUTING FUNCTION search_tool...[0m
[33mUser[0m (to Assistant):[33mUser[0m (to Assistant):[32m***** Response from calling tool (call_-9217975427127676990) *****[0m
"[\"{\\\"url\\\": \\\"https://www.espn.com/nfl/story/_/id/39480722/49ers-chiefs-live-super-bowl-lviii-updates-moments-highlights\\\", \\\"content\\\": \\\"With a relentless defense and opportune plays by their star quarterback -- including a pair of gutsy overtime scrambles -- the Chiefs won their third Super Bowl in five years in a 25-22 overtime victory against the San Francisco 49ers in only the second overtime game in Super Bowl history.\\\\n Staff\\\\nTaylor Swift supports Travis Kelce, chugs drink at Super Bowl LVIII10hTory Barron\\\\nAfter posting a losing record in December, the Chiefs embraced an underdog, villain mentality throughout the postseason, upsetting three teams en route to their second consecutive Super Bowl title, becoming the ninth team to repeat as Super Bowl champions and first since the 2004 New England Patriots.\\\\n ESPN\\\\nSuper Bowl 2024 - Highlights from Chiefs' win vs. 49ers\\\\nMike Tannenbaum and Tim Hasselbeck react to the Chiefs' thrilling overtime victory over the 49ers in the Super Bowl. The 49ers had the ball with 2:51 to go in a tied game, but a signature Steve Spagnuolo blitz on third down late in the fourth quarter forced a 49ers field goal. Led by their captains, most of the Chiefs arrived to Allegiant Stadium in Las Vegas on Sunday in all black, signaling a steely resolve to upset Brock Purdy and the NFC's best offensive ensemble.\\\\n\\\"}\", \"{\\\"url\\\": \\\"https://www.cbssports.com/nfl/news/2024-super-bowl-chiefs-vs-49ers-score-patrick-mahomes-leads-ot-comeback-as-k-c-wins-back-to-back-titles/live/\\\", \\\"content\\\": \\\"The championship-winning drive, which included a fourth-and-1 scramble from Mahomes and a clutch 7-yard catch from tight end Travis Kelce, was a must-score for K.C. The NFL's new playoff overtime rules -- both teams are guaranteed at least one possession in the extra period -- were in effect for the first time, and the Chiefs needed to answer the Niners' field goal.\\\\n Held out of the end zone until that point, Kansas City grabbed its first lead of the game at 13-10.\\\\nJennings' touchdown receiving (followed by a missed extra point) concluded a 75-yard drive that put the Niners back on top, 16-13, as the wideout joined former Philadelphia Eagles quarterback Nick Foles as the only players to throw and catch a touchdown in a Super Bowl.\\\\n He spread the ball around -- eight pass-catchers had at least two receptions -- slowly but surely overcoming a threatening 49ers defense that knocked him off his spot consistently in the first half.\\\\nMahomes, with his third Super Bowl MVP, now sits alongside Tom Brady (five) and Joe Montana (three) atop the mountain while becoming just the third player to win the award back-to-back, joining Bart Starr (I-II) and Terry Bradshaw (XIII-XIV).\\\\n The muffed punt that bounced off of cornerback Darrell Luter Jr.'s ankle was also the big break that the Chiefs needed as they scored on the very next play to take the lead for the first time in the game. College Pick'em\\\\nA Daily SportsLine Betting Podcast\\\\nNFL Playoff Time!\\\\n2024 Super Bowl, Chiefs vs. 49ers score: Patrick Mahomes leads OT comeback as K.C. wins back-to-back titles\\\\nCall it a dynasty; the Chiefs are the first team to win consecutive Super Bowls since 2003-04\\\\nThe Kansas City Chiefs are Super Bowl champions, again.\\\"}\", \"{\\\"url\\\": \\\"https://sports.yahoo.com/super-bowl-2024-chiefs-beat-49ers-in-ot-thriller-on-walk-off-td-pass-by-patrick-mahomes-035151467.html\\\", \\\"content\\\": \\\"Chiefs beat 49ers in OT thriller on walk-off TD pass by Patrick Mahomes\\\\nThe Kansas City Chiefs are Super Bowl champions for a third time in the Patrick Mahomes era.\\\\n Taylor Swift and the Kelce family make it down to the field after the Chiefs win the Super Bowl \\\\ud83d\\\\udc4f pic.twitter.com/FkbFIMiIxA\\\\n\\\\u2014 The Sporting News (@sportingnews) Travis Kelce was excited, as expected:\\\\nTravis never disappoints \\\\ud83d\\\\ude02 #ChiefsKingdom pic.twitter.com/Gr2J7JoF4c\\\\n\\\\u2014 NFL (@NFL) February 12, 2024\\\\nPatrick Mahomes: \\\\\\\"The Kansas City Chiefs are never underdogs. February 12, 2024\\\\nWith the Super Bowl on the line, the Chiefs just converted on fourth-and-1. February 12, 2024\\\\nNow, the Chiefs convert on third-and-6 to keep the drive moving.\\\\n\\\"}\", \"{\\\"url\\\": \\\"https://www.usatoday.com/story/sports/nfl/super-bowl/2024/02/11/super-bowl-2024-score-live-updates/72481979007/\\\", \\\"content\\\": \\\"- Nate Davis\\\\nThe 58 greatest NFL teams to play in the Super Bowl \\\\u2013 and not all won Lombardi Trophy\\\\nThe 2023 Kansas City Chiefs and San Francisco 49ers will become the 115th and 116th Super Bowl participants on Sunday, when they cross paths at Allegiant Stadium in Las Vegas. Super Bowl 2024 live updates: Keep up with Chiefs vs. 49ers score, highlights and analysis\\\\nThe\\\\u00a0Kansas City Chiefs\\\\u00a0will take on the\\\\u00a0San Francisco 49ers\\\\u00a0in\\\\u00a0Super Bowl 58\\\\u00a0tonight at\\\\u00a0Allegiant Stadium\\\\u00a0in\\\\u00a0Las Vegas\\\\u00a0for\\\\u00a0NFL\\\\u00a0supremacy.\\\\n Meanwhile, the 49ers will don their traditional road white uniform.\\\\u00a0 \\\\u2014 Jordan Mendoza\\\\nSuper Bowl 58 predictions\\\\nHere are our NFL expert picks based on the early odds for Super Bowl 58:\\\\nChiefs vs. 49ers moneyline\\\\nThe most basic of all bets is called the moneyline. \\\\u2014 Cydney Henderson\\\\nHighest-paid players in Super Bowl 58\\\\nHere are the highest-paid players in Super Bowl 58 based on average annual value, according to Over The Cap. \\\\u2014 Cydney Henderson\\\\nThe 58 greatest players in Super Bowl history: Meet the Chiefs kicker\\\\nHarrison Butker\\\\u00a0may be called upon to hit the biggest kick of his career if\\\\u00a0Super Bowl 58\\\\u00a0against the\\\\u00a0San Francisco 49ers\\\\u00a0is close in the fourth quarter.\\\\n\\\"}\", \"{\\\"url\\\": \\\"https://apnews.com/live/super-bowl-2024-updates\\\", \\\"content\\\": \\\"Throw in the fact that Chiefs coach Andy Reid will be in his fifth Super Bowl, the third most in NFL history, and has a chance to win a third ring, and the knowledge on the Kansas City sideline will be an advantage too big for the 49ers to overcome.\\\\n She performed in Japan on Saturday night before a flight across nine time zones and the international date line to reach the U.S.\\\\nRihanna performs during halftime of the NFL Super Bowl 57 football game between the Philadelphia Eagles and the Kansas City Chiefs, Sunday, Feb. 12, 2023, in Glendale, Ariz. (AP Photo/David J. Phillip)\\\\n After the teams take the field, Post Malone will perform \\\\u201cAmerica the Beautiful\\\\u201d and Reba McEntire will sing \\\\u201cThe Star-Spangled Banner.\\\\u201d\\\\nSan Francisco 49ers quarterback Brock Purdy (13) warms up before the NFL Super Bowl 58 football game against the Kansas City Chiefs, Sunday, Feb. 11, 2024, in Las Vegas. He was also the referee when the Chiefs beat the 49ers in the Super Bowl four years ago \\\\u2014 and when the Rams beat the Saints in the 2019 NFC championship game after an infamous missed call.\\\\n Purdy\\\\u2019s comeback from the injury to his throwing arm suffered in last season\\\\u2019s NFC championship loss to the Philadelphia Eagles has been part of the storybook start to his career that started as Mr. Irrelevant as the 262nd pick in the 2022 draft.\\\\n\\\"}\"]"
[32m******************************************************************[0m--------------------------------------------------------------------------------
[autogen.oai.client: 11-25 17:31:33] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[33mAssistant[0m (to User):Thought: The result of Super Bowl 2024 has been found.Final Answer: The Kansas City Chiefs won Super Bowl 2024 with a 25-22 overtime victory against the San Francisco 49ers.TERMINATE--------------------------------------------------------------------------------
[31m
>>>>>>>> NO HUMAN INPUT RECEIVED.[0m
Teachability能力
最后,我们添加Teachability能力到助手代理中,并再次与用户进行交互。
teachability = teachability.Teachability(verbosity=0,reset_db=True,path_to_db_dir="coding",recall_threshold=1.5,
)teachability.add_to_agent(assistant)with Cache.disk(cache_seed=44) as cache:user_proxy.initiate_chat(assistant,message=react_prompt_message,question="What is the result of super bowl 2024?",cache=cache,)
输出:
[92m
CLEARING MEMORY[0m
[33mUser[0m (to Assistant):Answer the following questions as best you can. You have access to tools provided.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
Action Input: the input to the action
Observation: the result of the action
... (this process can repeat multiple times)
Thought: I now know the final answer
Final Answer: the final answer to the original input questionBegin!
Question: What is the result of super bowl 2024?--------------------------------------------------------------------------------
[autogen.oai.client: 11-25 17:37:22] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:37:24] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:37:25] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:37:26] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.C:\Users\32564\.cache\chroma\onnx_models\all-MiniLM-L6-v2\onnx.tar.gz: 100%|██████████| 79.3M/79.3M [00:21<00:00, 3.92MiB/s][autogen.oai.client: 11-25 17:37:49] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:37:53] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[33mAssistant[0m (to User):[32m***** Suggested tool call (call_-9217975392767912887): search_tool *****[0m
Arguments:
{"query": "Super Bowl 2024 result"}
[32m************************************************************************[0m--------------------------------------------------------------------------------
[31m
>>>>>>>> NO HUMAN INPUT RECEIVED.[0m
[31m
>>>>>>>> USING AUTO REPLY...[0m
[35m
>>>>>>>> EXECUTING FUNCTION search_tool...[0m
[33mUser[0m (to Assistant):[33mUser[0m (to Assistant):[32m***** Response from calling tool (call_-9217975392767912887) *****[0m
"[\"{\\\"url\\\": \\\"https://www.espn.com/nfl/story/_/id/39480722/49ers-chiefs-live-super-bowl-lviii-updates-moments-highlights\\\", \\\"content\\\": \\\"With a relentless defense and opportune plays by their star quarterback -- including a pair of gutsy overtime scrambles -- the Chiefs won their third Super Bowl in five years in a 25-22 overtime victory against the San Francisco 49ers in only the second overtime game in Super Bowl history.\\\\n Staff\\\\nTaylor Swift supports Travis Kelce, chugs drink at Super Bowl LVIII10hTory Barron\\\\nAfter posting a losing record in December, the Chiefs embraced an underdog, villain mentality throughout the postseason, upsetting three teams en route to their second consecutive Super Bowl title, becoming the ninth team to repeat as Super Bowl champions and first since the 2004 New England Patriots.\\\\n ESPN\\\\nSuper Bowl 2024 - Highlights from Chiefs' win vs. 49ers\\\\nMike Tannenbaum and Tim Hasselbeck react to the Chiefs' thrilling overtime victory over the 49ers in the Super Bowl. The 49ers had the ball with 2:51 to go in a tied game, but a signature Steve Spagnuolo blitz on third down late in the fourth quarter forced a 49ers field goal. Led by their captains, most of the Chiefs arrived to Allegiant Stadium in Las Vegas on Sunday in all black, signaling a steely resolve to upset Brock Purdy and the NFC's best offensive ensemble.\\\\n\\\"}\", \"{\\\"url\\\": \\\"https://www.cbssports.com/nfl/news/2024-super-bowl-chiefs-vs-49ers-score-patrick-mahomes-leads-ot-comeback-as-k-c-wins-back-to-back-titles/live/\\\", \\\"content\\\": \\\"The championship-winning drive, which included a fourth-and-1 scramble from Mahomes and a clutch 7-yard catch from tight end Travis Kelce, was a must-score for K.C. The NFL's new playoff overtime rules -- both teams are guaranteed at least one possession in the extra period -- were in effect for the first time, and the Chiefs needed to answer the Niners' field goal.\\\\n Held out of the end zone until that point, Kansas City grabbed its first lead of the game at 13-10.\\\\nJennings' touchdown receiving (followed by a missed extra point) concluded a 75-yard drive that put the Niners back on top, 16-13, as the wideout joined former Philadelphia Eagles quarterback Nick Foles as the only players to throw and catch a touchdown in a Super Bowl.\\\\n He spread the ball around -- eight pass-catchers had at least two receptions -- slowly but surely overcoming a threatening 49ers defense that knocked him off his spot consistently in the first half.\\\\nMahomes, with his third Super Bowl MVP, now sits alongside Tom Brady (five) and Joe Montana (three) atop the mountain while becoming just the third player to win the award back-to-back, joining Bart Starr (I-II) and Terry Bradshaw (XIII-XIV).\\\\n The muffed punt that bounced off of cornerback Darrell Luter Jr.'s ankle was also the big break that the Chiefs needed as they scored on the very next play to take the lead for the first time in the game. College Pick'em\\\\nA Daily SportsLine Betting Podcast\\\\nNFL Playoff Time!\\\\n2024 Super Bowl, Chiefs vs. 49ers score: Patrick Mahomes leads OT comeback as K.C. wins back-to-back titles\\\\nCall it a dynasty; the Chiefs are the first team to win consecutive Super Bowls since 2003-04\\\\nThe Kansas City Chiefs are Super Bowl champions, again.\\\"}\", \"{\\\"url\\\": \\\"https://sports.yahoo.com/super-bowl-2024-chiefs-beat-49ers-in-ot-thriller-on-walk-off-td-pass-by-patrick-mahomes-035151467.html\\\", \\\"content\\\": \\\"Chiefs beat 49ers in OT thriller on walk-off TD pass by Patrick Mahomes\\\\nThe Kansas City Chiefs are Super Bowl champions for a third time in the Patrick Mahomes era.\\\\n Taylor Swift and the Kelce family make it down to the field after the Chiefs win the Super Bowl \\\\ud83d\\\\udc4f pic.twitter.com/FkbFIMiIxA\\\\n\\\\u2014 The Sporting News (@sportingnews) Travis Kelce was excited, as expected:\\\\nTravis never disappoints \\\\ud83d\\\\ude02 #ChiefsKingdom pic.twitter.com/Gr2J7JoF4c\\\\n\\\\u2014 NFL (@NFL) February 12, 2024\\\\nPatrick Mahomes: \\\\\\\"The Kansas City Chiefs are never underdogs. February 12, 2024\\\\nWith the Super Bowl on the line, the Chiefs just converted on fourth-and-1. February 12, 2024\\\\nNow, the Chiefs convert on third-and-6 to keep the drive moving.\\\\n\\\"}\", \"{\\\"url\\\": \\\"https://www.usatoday.com/story/sports/nfl/super-bowl/2024/02/11/super-bowl-2024-score-live-updates/72481979007/\\\", \\\"content\\\": \\\"- Nate Davis\\\\nThe 58 greatest NFL teams to play in the Super Bowl \\\\u2013 and not all won Lombardi Trophy\\\\nThe 2023 Kansas City Chiefs and San Francisco 49ers will become the 115th and 116th Super Bowl participants on Sunday, when they cross paths at Allegiant Stadium in Las Vegas. Super Bowl 2024 live updates: Keep up with Chiefs vs. 49ers score, highlights and analysis\\\\nThe\\\\u00a0Kansas City Chiefs\\\\u00a0will take on the\\\\u00a0San Francisco 49ers\\\\u00a0in\\\\u00a0Super Bowl 58\\\\u00a0tonight at\\\\u00a0Allegiant Stadium\\\\u00a0in\\\\u00a0Las Vegas\\\\u00a0for\\\\u00a0NFL\\\\u00a0supremacy.\\\\n Meanwhile, the 49ers will don their traditional road white uniform.\\\\u00a0 \\\\u2014 Jordan Mendoza\\\\nSuper Bowl 58 predictions\\\\nHere are our NFL expert picks based on the early odds for Super Bowl 58:\\\\nChiefs vs. 49ers moneyline\\\\nThe most basic of all bets is called the moneyline. \\\\u2014 Cydney Henderson\\\\nHighest-paid players in Super Bowl 58\\\\nHere are the highest-paid players in Super Bowl 58 based on average annual value, according to Over The Cap. \\\\u2014 Cydney Henderson\\\\nThe 58 greatest players in Super Bowl history: Meet the Chiefs kicker\\\\nHarrison Butker\\\\u00a0may be called upon to hit the biggest kick of his career if\\\\u00a0Super Bowl 58\\\\u00a0against the\\\\u00a0San Francisco 49ers\\\\u00a0is close in the fourth quarter.\\\\n\\\"}\", \"{\\\"url\\\": \\\"https://apnews.com/live/super-bowl-2024-updates\\\", \\\"content\\\": \\\"Throw in the fact that Chiefs coach Andy Reid will be in his fifth Super Bowl, the third most in NFL history, and has a chance to win a third ring, and the knowledge on the Kansas City sideline will be an advantage too big for the 49ers to overcome.\\\\n She performed in Japan on Saturday night before a flight across nine time zones and the international date line to reach the U.S.\\\\nRihanna performs during halftime of the NFL Super Bowl 57 football game between the Philadelphia Eagles and the Kansas City Chiefs, Sunday, Feb. 12, 2023, in Glendale, Ariz. (AP Photo/David J. Phillip)\\\\n After the teams take the field, Post Malone will perform \\\\u201cAmerica the Beautiful\\\\u201d and Reba McEntire will sing \\\\u201cThe Star-Spangled Banner.\\\\u201d\\\\nSan Francisco 49ers quarterback Brock Purdy (13) warms up before the NFL Super Bowl 58 football game against the Kansas City Chiefs, Sunday, Feb. 11, 2024, in Las Vegas. He was also the referee when the Chiefs beat the 49ers in the Super Bowl four years ago \\\\u2014 and when the Rams beat the Saints in the 2019 NFC championship game after an infamous missed call.\\\\n Purdy\\\\u2019s comeback from the injury to his throwing arm suffered in last season\\\\u2019s NFC championship loss to the Philadelphia Eagles has been part of the storybook start to his career that started as Mr. Irrelevant as the 262nd pick in the 2022 draft.\\\\n\\\"}\"]"
[32m******************************************************************[0m--------------------------------------------------------------------------------
[autogen.oai.client: 11-25 17:38:05] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:38:06] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:38:07] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:38:09] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:38:15] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:38:17] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[33mAssistant[0m (to User):Thought: I now know the final answer
Final Answer: The Kansas City Chiefs won Super Bowl 2024 with a 25-22 overtime victory against the San Francisco 49ers.
TERMINATE--------------------------------------------------------------------------------
[31m
>>>>>>>> NO HUMAN INPUT RECEIVED.[0m
再访问一次:
# Use a different cache_seed.
with Cache.disk(cache_seed=110) as cache:user_proxy.initiate_chat(assistant,message=react_prompt_message,question="What is the result of super bowl 2024?",max_turns=1,cache=cache,)
[33mUser[0m (to Assistant):Answer the following questions as best you can. You have access to tools provided.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
Action Input: the input to the action
Observation: the result of the action
... (this process can repeat multiple times)
Thought: I now know the final answer
Final Answer: the final answer to the original input questionBegin!
Question: What is the result of super bowl 2024?--------------------------------------------------------------------------------
[autogen.oai.client: 11-25 17:38:47] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:38:50] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:38:51] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:38:51] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:38:54] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:38:54] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:38:55] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:38:56] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[autogen.oai.client: 11-25 17:39:02] {351} WARNING - Model GLM-4-Plus is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
[33mAssistant[0m (to User):Question: What is the result of Super Bowl 2024?Thought: I have prior knowledge about the Super Bowl 2024 result from the provided memories. I should use this information to answer the question.Action: Utilize the memories to construct the answer.Observation: From the memories:
- Chiefs won with a 25-22 overtime victory against the San Francisco 49ers.
- It was only the second overtime game in Super Bowl history.
- Patrick Mahomes led the winning drive and won his third Super Bowl MVP.
- Chiefs became the first team to win consecutive Super Bowls since 2003-04.Thought: I now know the final answer.Final Answer: The Kansas City Chiefs won Super Bowl 2024 with a 25-22 overtime victory against the San Francisco 49ers. This marked their third Super Bowl win in five years and made them the first team to win consecutive Super Bowls since the 2003-04 season. Patrick Mahomes played a crucial role and secured his third Super Bowl MVP award.TERMINATE--------------------------------------------------------------------------------
总结
本文介绍了如何使用autogen框架中使用chatglm4模型实现react,利用环境变量、Tavily API和ReAct代理模式来回答用户提出的问题。通过使用缓存和Teachability能力,我们可以提高系统的性能和准确性。
注意事项
提示词跟llm模型有关,对其他的模型需要微调提示词
参考链接:https://microsoft.github.io/autogen/0.2/docs/topics/prompting-and-reasoning/react
如果有任何问题,欢迎在评论区提问。