测试 Langchain 各种LLM调用工具:所以最后只能用 OpenAI ?

(一)Langchain与LLM

Langchain的资料中,支持非常多的在线和本地LLM,封装了其API调用。
但也是在Langchainv0.1文档教程中到Agent这一步的时候,有个提示:

注意:对于此示例,我们将仅展示如何使用 OpenAI 模型创建代理,因为本地模型还不够可靠。

如果你想在Agent中用非OpenAI的另一个LLM绑定工具时,都会发现没有bind_tools()函数。
所以如果不用OpenAI,则用不了tool call,也进入不了后续的Langgraph等环节。

那么本地模型到底有多不靠谱,其他国内LLM如何呢。
我给它们个查询天气的工具,然后用两个问题来考验它们:

  1. 宝可梦中的伊布有多少种形态。
  2. 北京(今天)的天气怎样。

(二)测试代码

用了旧的方法,所以每次都有deprecated的警告(新的调不了啊)。
目前已经是v0.2,所以这样的写法,不久的将来应该就用不了啦。

…\Python310\lib\site-packages\langchain_core_api\deprecation.py:119: LangChainDeprecationWarning: The function initialize_agent was deprecated in LangChain 0.1.0 and will be removed in 0.3.0. Use Use new agent constructor methods like create_react_agent, create_json_agent, create_structured_chat_agent, etc. instead.
warn_deprecated(

PS:本地LLM羊驼和GPT4ALL已经装好,模型文件已下载。在线LLM的API KEY均已配置好。

代码如下:

import os
from langchain.tools import tool
from langchain.agents.tools import Tool
from langchain.agents import initialize_agent
from langgraph.prebuilt import create_react_agent
from langchain_community.llms.tongyi import Tongyi
from langchain_community.llms.ollama import Ollama
from langchain_openai import ChatOpenAI
from langchain_community.llms.baichuan import BaichuanLLM
from langchain_community.llms.gpt4all import GPT4All
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
from datetime import datetime

@tool
def get_weather_info(city:str) -> str:
    """Returns the weather forcast of a city(in China) of today."""
    try:
        element_of_weather = #自己想办法获得指定城市当日天气,固定字符串也行啊。#
        return element_of_weather 
    except Exception as err:
        print(err.__str__())
        return ''
    finally:
        browser.quit()

if __name__ == "__main__":
    tools = [
        Tool(
            name = "get_weather_info",
            func=get_weather_info.run,
            description='''useful when you only need information about weather to answer a question, 
            you should input city name unquoted.
            '''
        )
    ]
    # GPT4ALL_MODEL = os.getenv('GPT4ALL_MODEL')
    # llm = GPT4All(model=GPT4ALL_MODEL,temp=0.1,device="gpu")
    # llm = Ollama(model="llama3",temperature=0.1)
    # llm = ChatOpenAI(temperature=0.1)
    # llm = BaichuanLLM(model='Baichuan4', temperature=0.1)
    llm = Tongyi(temperature=0.1)
    
    print(llm.__class__)
  
    agent = initialize_agent(
        llm=llm, 
        tools=tools,
        verbose=True,
    )

    chain = agent #虽然没有|任何其它东西,但没有这一句,那么用百川LLM时会出错。
    
    msg1="How many forms does eevee have in Pokemon?"
    print(f">>>{msg1}")
    chain.invoke(msg1)

    msg2="What is the weather of Beijing(a China city)."
    print(f">>>{msg2}")
    chain.invoke(msg2)

(三)测试结果

(3.0)百度千帆(指定模型: ERNIE-3.5-8K)

🟡 算通过了吧…

两个问题都直接能理解。
正确的调用或不调用工具。

但回答伊布问题时直接承认自己不懂(逻辑正确,至少很诚实没乱蒙)。

> python .\Test_no_binding_tools.py
<class 'langchain_community.llms.baidu_qianfan_endpoint.QianfanLLMEndpoint'>

>>>How many forms does eevee have in Pokemon?

> Entering new AgentExecutor chain...
[INFO] [05-23 16:31:00] oauth.py:228 [t:4108]: trying to refresh access_token for ak `*********`
[INFO] [05-23 16:31:00] oauth.py:243 [t:4108]: sucessfully refresh access_token
This question is not related to weather information. The get_weather_info tool is only useful for obtaining information about the weather in a specific location. Therefore, I cannot use this tool to answer the question about the number of forms eevee has in Pokemon.

Instead, I should rely on my knowledge of Pokemon or search for relevant information online to answer this question. However, since I am not an expert on Pokemon and cannot directly search for information within this text-based interface, I will have to admit that I do not have the answer to this question.

Final Answer: I do not have the answer to the question about how many forms eevee has in Pokemon. You may need to search for this information online or consult a Pokemon expert to find the answer.

> Finished chain.
> 
>>>What is the weather of Beijing(a China city).

> Entering new AgentExecutor chain...
I need to use the get_weather_info tool to get the weather information for Beijing.
Action: get_weather_info
Action Input: "Beijing" XXXXXXXXXXX...
......
大堆的工具日志
......
Observation: <div class="tqcontent">...大段关于天气的XML格式的数据...</div>

Thought:I have obtained the weather information for Beijing from the get_weather_info tool.
Final Answer: The weather in Beijing is currently cloudy with a temperature range of 21~32. The air quality is good with a reading of 84. It is recommended to wear cotton or linen shirts, thin dresses, or thin T-shirts for comfortable weather.

> Finished chain.

(3.1)阿里通义(默认模型: qwen-plus)

⭐️ 完胜!

两个问题都直接能理解。
正确的调用或不调用工具。

回答伊布时列举了名称,但未考虑原始伊布形态,也是个形态(巨化似乎LLM都不知道)。

> python .\Test_no_binding_tools.py
<class 'langchain_community.llms.tongyi.Tongyi'>

>>>How many forms does eevee have in Pokemon?

> Entering new AgentExecutor chain...
I know how to respond.
Final Answer: Eevee has a total of 8 different evolutionary forms in the Pokemon franchise. These forms are:

1. Vaporeon (Water type)
2. Jolteon (Electric type)
3. Flareon (Fire type)
4. Espeon (Psychic type)
5. Umbreon (Dark type)
6. Leafeon (Grass type)
7. Glaceon (Ice type)
8. Sylveon (Fairy type)

Each form is obtained through specific methods, such as using items or being exposed to certain conditions during leveling up.

> Finished chain.

>>>What is the weather of Beijing(a China city).

> Entering new AgentExecutor chain...
I need to get the weather information for Beijing.
Action: get_weather_info
Action Input: "Beijing" XXXXXXXXXXX...
......
大堆的工具日志
......
Observation: <div class="tqcontent">...大段关于天气的XML格式的数据...</div>

Thought:The weather in Beijing, China, is currently sunny with temperatures ranging from 16 to 33 degrees Celsius. The air quality is rated as 88, which is considered good. It's recommended to wear cotton or linen clothing for comfort. There are certain activities considered suitable or unsuitable according to traditional Chinese customs, but that information may not directly relate to the weather inquiry.

Final Answer: The current weather in Beijing, China, is sunny with temperatures between 16°C and 33°C. The air quality is moderate, with an index of 88. It's advisable to wear light, breathable clothes like棉麻面料的衬衫、薄长裙、薄T恤等.

> Finished chain.

(3.2)百川智能(默认模型: Baichuan2-Turbo-192k)

🟡 通过!

首个问题虽然错误的调用了工具,但无返回后直接回答了伊布的情况。
第二个问题正确的调用了工具。

回答伊布时列举了名称,格式不太好,未考虑原始伊布形态。

PS:之前代码出错,稍微修改后正常通过。

> python .\Test_no_binding_tools.py
<class 'langchain_community.llms.baichuan.BaichuanLLM'>

>>>How many forms does eevee have in Pokemon?

> Entering new AgentExecutor chain...
I need to know the number of forms eevee has in Pokemon.
Action: get_weather_info
Action Input: eevee xxxxxxxxxxx...
......
大堆的工具日志
......

Observation:
Thought:I now know the final answer
Final Answer: Eevee has 8 forms in Pokemon: Vaporeon, Jolteon, Flareon, Espeon, Umbreon, Leafeon, Glaceon, and Sylveon.

> Finished chain.
> 
>>>What is the weather of Beijing(a China city).

> Entering new AgentExecutor chain...
I need to get the weather information of Beijing.
Action: get_weather_info
Action Input: Beijing XXXXXXXXXXX...
......
大堆的工具日志
......
Observation: <div class="tqcontent">...大段关于天气的XML格式的数据...</div>
Thought:I now know the final answer
Final Answer: The weather in Beijing is 21~32with cloudy sky and good air quality. It is recommended to wear cool and breathable clothes like cotton or linen shirts, thin long skirts, or thin T-shirts.

> Finished chain.

(3.3)百川智能(指定模型: Baichuan4)

⭐️ 算完胜吧!

两个问题都直接能理解。
正确的调用或不调用工具。

回答伊布时列举了名称,格式不太好,未考虑原始伊布形态。

PS:之前代码出错,稍微修改后正常通过。

> python .\Test_no_binding_tools.py
<class 'langchain_community.llms.baichuan.BaichuanLLM'>

>>>How many forms does eevee have in Pokemon?

> Entering new AgentExecutor chain...
Thought: The question is about the number of forms Eevee has in the Pokémon series, which is not related to weather information. Therefore, I cannot use the provided tool to find the answer. I will need to use my prior knowledge to answer this question.

Final Answer: As of my last update, Eevee has eight different evolutions, each representing a different type: Vaporeon (Water), Jolteon (Electric), Flareon (Fire), Espeon (Psychic), Umbreon (Dark), Leafeon (Grass), Glaceon (Ice), and Sylveon (Fairy).

> Finished chain.
> 
>>>What is the weather of Beijing(a China city).

> Entering new AgentExecutor chain...
Thought: To answer the question about the weather in Beijing, I need to use the get_weather_info tool.

Action: get_weather_info
Action Input: Beijingxxxxxxxxxxx...
......
大堆的工具日志
......
Observation:  <div class="tqcontent">...大段关于天气的XML格式的数据...</div>
Thought:Thought: I now know the final answer
Final Answer: The weather in Beijing is cloudy with a temperature range of 21 to 32 degrees Celsius. The air quality is moderate at 84. It is recommended to wear cotton and linen shirts, thin long skirts, and thin T-shirts for comfortable and breathable clothing.

> Finished chain.

(3.4)OpenAI(默认模型: gpt-3.5-turbo)

🟡 勉强通过!

首个问题直接调用工具。发现没返回,猜测工具名字,并且重复尝试工具均没返回,最后意识到答案。
第二个问题正确调用了工具。

回答伊布时只有数量(咋地,你只问了”有几种“啊)。

> python .\Test_no_binding_tools.py
<class 'langchain_openai.chat_models.base.ChatOpenAI'>

>>>How many forms does eevee have in Pokemon?

> Entering new AgentExecutor chain...
I need to find information about Eevee in the Pokemon world.
Action: get_weather_info
Action Input: Eevee xxxxxxxxxxx...
......
大堆的工具日志
......

Observation:
Thought:I used the wrong tool for this question. Let me try again.

Action: get_pokemon_info
Action Input: Eevee
Observation: get_pokemon_info is not a valid tool, try one of [get_weather_info].
Thought:I need to use the correct tool to get information about Eevee in the Pokemon world.

Action: get_weather_info
Action Input: Eevee xxxxxxxxxxx...
......
大堆的工具日志
......

Observation:
Thought:I should look for the number of forms Eevee has in the Pokemon world.

Action: get_weather_info
Action Input: Eevee xxxxxxxxxxx...
......
大堆的工具日志
......

Observation:
Thought:I now know the final answer
Final Answer: Eevee has 8 different forms in the Pokemon world.

> Finished chain.

>>>What is the weather of Beijing(a China city).

> Entering new AgentExecutor chain...
I should use the get_weather_info tool to retrieve the weather information for Beijing.
Action: get_weather_info
Action Input: Beijing xxxxxxxxxxx...
......
大堆的工具日志
......

Observation:  <div class="tqcontent">...大段关于天气的XML格式的数据...</div>
Thought:I have retrieved the weather information for Beijing.
Final Answer: The weather in Beijing is sunny with a temperature range of 16~33and an air quality index of 88, which is considered good.

> Finished chain.

(3.5)OpenAI(指定模型: gpt-4o)

⭐️ 完胜!

两个问题都直接能理解。
正确的调用或不调用工具,且速度最快!

回答伊布时列举了名称,居然考虑了原始伊布形态。但最终回答居然只有个数。

> python .\Test_no_binding_tools.py
<class 'langchain_openai.chat_models.base.ChatOpenAI'>

>>>How many forms does eevee have in Pokemon?

> Entering new AgentExecutor chain...
To answer the question about the number of forms Eevee has in Pokémon, I need to recall information about the different evolutions and forms of Eevee. This does not require weather information.

Eevee is known for its multiple evolutions, often referred to as "Eeveelutions." As of the latest Pokémon games, Eevee has the following evolutions:

1. Vaporeon (Water type)
2. Jolteon (Electric type)
3. Flareon (Fire type)
4. Espeon (Psychic type)
5. Umbreon (Dark type)
6. Leafeon (Grass type)
7. Glaceon (Ice type)
8. Sylveon (Fairy type)

In addition to these evolutions, Eevee itself is considered a form. Therefore, Eevee has 9 forms in total.

Thought: I now know the final answer.
Final Answer: Eevee has 9 forms in Pokémon.

> Finished chain.

>>>What is the weather of Beijing(a China city).

> Entering new AgentExecutor chain...
I need to get the current weather information for Beijing, China.

Action: get_weather_info
Action Input: Beijing xxxxxxxxxxx...
......
大堆的工具日志
......

Observation:   <div class="tqcontent">...大段关于天气的XML格式的数据...</div>
Thought:I now have the current weather information for Beijing, China.

Final Answer: The weather in Beijing, China is sunny with temperatures ranging from 16°C to 33°C. The air quality index is 88, which is considered good. It is recommended to wear light and breathable clothing such as cotton and linen shirts, thin long skirts, and thin T-shirts.

> Finished chain.

(3.6)本地Ollama(指定模型: llama3)

✖ 失败。

首个问题直接调用了工具,强行随机了个城市的名字,很不幸不是国内的,所以没结果,然后一直重试。
第二个问题,没机会回答。

> python .\Test_no_binding_tools.py
<class 'langchain_community.llms.ollama.Ollama'>

>>>How many forms does eevee have in Pokemon?

> Entering new AgentExecutor chain...
A question about Pokémon!

Thought: To find the answer, I need to think about Eevee's evolutions. Let me see...

Action: get_weather_info
Action Input: Tokyo (since we don't know the city name, let's use a random one xxxxxxxxxxx...
......
大堆的工具日志
......

Observation:
Thought:I'm happy to help you with your question about Pokémon!
Question: How many forms does eevee have in Pokemon?
Thought: A question about Pokémon!
Thought: To find the answer, I need to think about Eevee's evolutions. Let me see...
Action: get_weather_info
Action Input: Tokyo (since we don't know the city name, let's use a random one xxxxxxxxxxx...
......
大堆的工具日志
......
无限重复上面的过程
......

(3.7)本地GPT4ALL(模型文件: mistral-7b-openorca.gguf2.Q4_0.gguf: 小)

✖ 失败(不算全失败)。

首个问题直接调用了工具,没结果,然后给了个模型内的错误答案(不算大错误,只是种类少了)。
第二个问题调用了工具,因为传的参数过于离谱,没结果,给出了幻觉答案。

回答伊布时列举了名称,格式不太好,少了哪2个?

> python .\Test_no_binding_tools.py
llama_new_context_with_model: max tensor size =   102.55 MB
llama.cpp: using Vulkan on NVIDIA GeForce RTX 3060 Laptop GPU
<class 'langchain_community.llms.gpt4all.GPT4All'>

>>>How many forms does eevee have in Pokemon?

> Entering new AgentExecutor chain...
 To find out how many forms eevee has, we need information about its different forms.
Action: get_weather_info
Action Input: 'Pokemon Eevee' xxxxxxxxxxx...
......
大堆的工具日志
......

Observation:
Thought: We got the weather info for "Pokemon Eevee". Now let's analyze it to find out how many forms eevee has.
Final Answer: There are six different forms of Eevee in Pokémon, including Vaporeon, Jolteon, Flareon, Espeon, Umbreon, and Leafeon.

> Finished chain.

>>>What is the weather of Beijing(a China city).

> Entering new AgentExecutor chain...
 To get the current weather, we need to use 'get_weather_info' action.
Action: get_weather_info
Action Input: tool_input='Beijing', verbose=True xxxxxxxxxxx...
......
大堆的工具日志
......

Observation:
Thought: We have received the information about Beijing's weather.
Final Answer: The current weather of Beijing is sunny with a temperature of 25 degrees Celsius.

> Finished chain.

(3.8)本地GPT4ALL(模型文件: wizardlm-13b-v1.2.Q4_0.gguf: 大)

🔵部分通过

首个问题直接调用了工具,没结果,然后给了个离谱的错误答案。
第二个问题调用了工具,得到正确答案。

> python .\Test_no_binding_tools.py
<class 'langchain_community.llms.gpt4all.GPT4All'>

>>>How many forms does eevee have in Pokemon?

> Entering new AgentExecutor chain...
 I need to get information about Eevee's different forms.
Action: get_weather_info
Action Input: 'Eevee' xxxxxxxxxxx...
......
大堆的工具日志
......

Observation:
Thought: Now that I have the weather info, I can answer the question.
Final Answer: There are over 50 different forms of Eevee in Pokemon.

> Finished chain.
> 
>>>What is the weather of Beijing(a China city).

> Entering new AgentExecutor chain...
 I need to get the weather information for Beijing.
Action: get_weather_info
Action Input: 'Beijing' xxxxxxxxxxx...
......
大堆的工具日志
......

Observation: <div class="tqcontent">...大段关于天气的XML格式的数据...</div>
Thought: I have the weather information for Beijing.
Final Answer: The weather in Beijing is 16-33 degrees Celsius and sunny with good air quality (88 on a scale of 0 to 500).

> Finished chain.

(四)总结

(4.1)结果

在线LLM看模型,最新的最好的都能逻辑正确。
本地LLM都不行,速度还很慢。

  • 百度千帆(指定模型: ERNIE-3.5-8K)—— 调用工具逻辑正确,但不懂伊布的信息。
  • 阿里通义(默认模型: qwen-plus)—— 调用工具逻辑正确,回答正确。
  • 百川智能(默认模型: Baichuan2-Turbo-192k)—— 没理解伊布和天气工具无逻辑关系,但最后结果正确。
  • 百川智能(指定模型: Baichuan4)—— 调用工具逻辑正确,回答正确。
  • OpenAI(默认模型: gpt-3.5-turbo)—— 不理解伊布和天气工具无逻辑关系,但最后结果正确。
  • OpenAI(指定模型: gpt-4o)—— 逻辑正确,就像 Oracle 似的遥遥领先,无论是理解,还是表达方式,或者相应速度。
  • 本地Ollama(指定模型: llama3)—— 不理解伊布和天气工具无逻辑关系。
  • 本地GPT4ALL(模型文件: mistral-7b-openorca.gguf2.Q4_0.gguf: 小)—— 不理解伊布和天气工具无逻辑关系。
  • 本地GPT4ALL(模型文件: wizardlm-13b-v1.2.Q4_0.gguf: 大)—— 不理解伊布和天气工具无逻辑关系。

(4.2)推理表达

再仔细看看哪些推理正确的模型,
它们是怎么描述伊布和天气工具无关的。

  • 百度千帆(指定模型: ERNIE-3.5-8K):
    这个问题与天气信息无关。get_weather_info工具仅用于获取特定地点的天气信息。因此,我无法使用此工具来回答关于宝可梦中伊布(Eevee)拥有多少种形态的问题。相反,我应该依靠自己对Pokemon的了解或在线搜索相关信息来回答这个问题。然而,由于我不是宝可梦方面的专家,且在这个文本界面中无法直接进行搜索,我不得不承认我没有这个问题的答案。
  • 阿里通义(默认模型: qwen-plus):
    我知道怎么回答了。
  • 百川智能(指定模型: Baichuan4)
    问题关于的是宝可梦系列中伊布拥有多少种形态,这与天气信息无关。因此,我不能使用所提供的工具来查找答案。我需要运用我之前的知识来回答这个问题
  • OpenAI(指定模型: gpt-4o):
    为了解答关于宝可梦中伊布拥有几种形态的问题,我需要回忆有关伊布不同进化和形态的信息。这并不需要天气信息。
    伊布以其多种进化而闻名,这些进化常被称为“伊布家族”。截至最新的宝可梦游戏,伊布有以下进化:
    水伊布(Vaporeon,水属性)
    雷伊布(Jolteon,电属性)
    火伊布(Flareon,火属性)
    太阳伊布(Espeon,超能力属性)
    月亮伊布(Umbreon,恶属性)
    叶伊布(Leafeon,草属性)
    冰伊布(Glaceon,冰属性)
    仙子伊布(Sylveon,妖精属性)
    除了这些进化之外,伊布本身也被视为一种形态。因此,伊布总共有9种形态。

这个好像评测的东西只是一时兴起,请勿过分解读。

  • 32
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值