比较不同语言模型的效果

释放双眼,带上耳机,听听看~!
本文比较了不同语言模型在回答特定问题和使用prompt模板时的效果,包括OpenAI、Cohere和HuggingFaceHub。通过比较不同模型的回复,可以帮助用户选择合适的语言模型应用。

构建您的语言模型应用程序可能需要在多种提示、模型甚至Chain(这是Langchain的概念)之间进行选择。在这个过程中,您需要以简单、灵活且直观的方式比较不同选项在不同输入上的表现。

LangChain提供了一个名为ModelLaboratory的概念,用于测试并尝试不同的模型。

实验环境是colab,但是还没有成功,模型加载问题,贴出来先看看

安装依赖

!pip install cohere
!pip install huggingface_hub

配置访问key

cohere 的api_key 和 huggingface_hub的access_token,是两个托管模型的地方

%env COHERE_API_KEY=FdHkBHjF3B7ynaCbCs0fxxx
%env HUGGINGFACEHUB_API_TOKEN=hf_MADVhnPvDxxxxTVuYjKbjAxxx

cohere
比较不同语言模型的效果
huggingface
比较不同语言模型的效果

模型效果比较

from langchain import LLMChain, OpenAI, Cohere, HuggingFaceHub, PromptTemplate
from langchain.model_laboratory import ModelLaboratory

llms = [
    OpenAI(temperature=0), 
    Cohere(model="command-xlarge-20221108", max_tokens=20, temperature=0), 
    HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature":1})
]

model_lab = ModelLaboratory.from_llms(llms)

model_lab.compare("What color is a flamingo?")

输出

Input:
What color is a flamingo?

OpenAI
Params: {'model': 'text-davinci-002', 'temperature': 0.0, 'max_tokens': 256, 'top_p': 1, 'frequency_penalty': 0, 'presence_penalty': 0, 'n': 1, 'best_of': 1}


Flamingos are pink.

Cohere
Params: {'model': 'command-xlarge-20221108', 'max_tokens': 20, 'temperature': 0.0, 'k': 0, 'p': 1, 'frequency_penalty': 0, 'presence_penalty': 0}


Pink

HuggingFaceHub
Params: {'repo_id': 'google/flan-t5-xl', 'temperature': 1}
pink

prompt效果比较

prompt = PromptTemplate(template="What is the capital of {state}?", input_variables=["state"])
model_lab_with_prompt = ModelLaboratory.from_llms(llms, prompt=prompt)
model_lab_with_prompt.compare("New York")

输出

Input:
New York

OpenAI
Params: {'model': 'text-davinci-002', 'temperature': 0.0, 'max_tokens': 256, 'top_p': 1, 'frequency_penalty': 0, 'presence_penalty': 0, 'n': 1, 'best_of': 1}


The capital of New York is Albany.

Cohere
Params: {'model': 'command-xlarge-20221108', 'max_tokens': 20, 'temperature': 0.0, 'k': 0, 'p': 1, 'frequency_penalty': 0, 'presence_penalty': 0}


The capital of New York is Albany.

HuggingFaceHub
Params: {'repo_id': 'google/flan-t5-xl', 'temperature': 1}
st john s

prompt 比较

from langchain import SelfAskWithSearchChain, SerpAPIWrapper

open_ai_llm = OpenAI(temperature=0)
search = SerpAPIWrapper()
self_ask_with_search_openai = SelfAskWithSearchChain(llm=open_ai_llm, search_chain=search, verbose=True)

cohere_llm = Cohere(temperature=0, model="command-xlarge-20221108")
search = SerpAPIWrapper()
self_ask_with_search_cohere = SelfAskWithSearchChain(llm=cohere_llm, search_chain=search, verbose=True)
chains = [self_ask_with_search_openai, self_ask_with_search_cohere]
names = [str(open_ai_llm), str(cohere_llm)]
model_lab = ModelLaboratory(chains, names=names)
model_lab.compare("What is the hometown of the reigning men's U.S. Open champion?")

输出

Input:
What is the hometown of the reigning men's U.S. Open champion?

OpenAI
Params: {'model': 'text-davinci-002', 'temperature': 0.0, 'max_tokens': 256, 'top_p': 1, 'frequency_penalty': 0, 'presence_penalty': 0, 'n': 1, 'best_of': 1}


> Entering new chain...
What is the hometown of the reigning men's U.S. Open champion?
Are follow up questions needed here: Yes.
Follow up: Who is the reigning men's U.S. Open champion?
Intermediate answer: Carlos Alcaraz.
Follow up: Where is Carlos Alcaraz from?
Intermediate answer: El Palmar, Spain.
So the final answer is: El Palmar, Spain
> Finished chain.

So the final answer is: El Palmar, Spain

Cohere
Params: {'model': 'command-xlarge-20221108', 'max_tokens': 256, 'temperature': 0.0, 'k': 0, 'p': 1, 'frequency_penalty': 0, 'presence_penalty': 0}


> Entering new chain...
What is the hometown of the reigning men's U.S. Open champion?
Are follow up questions needed here: Yes.
Follow up: Who is the reigning men's U.S. Open champion?
Intermediate answer: Carlos Alcaraz.
So the final answer is:

Carlos Alcaraz
> Finished chain.

So the final answer is:

Carlos Alcaraz

参考

本网站的内容主要来自互联网上的各种资源,仅供参考和信息分享之用,不代表本网站拥有相关版权或知识产权。如您认为内容侵犯您的权益,请联系我们,我们将尽快采取行动,包括删除或更正。
AI教程

强化学习稀疏奖励问题及解决方法

2023-12-7 20:38:14

AI教程

深度学习实践:数据集划分、偏差和方差、机器学习基本策略

2023-12-7 20:44:14

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索