我正在尝试将 langchain 与 gpt-35-turbo 一起使用。 根据链接,新的 gpt3.5 turbo 似乎不再使用某些参数了解如何使用 ChatGPT 和 GPT-4 模型(预览)
以下参数不适用于新的 ChatGPT 和 GPT-4 模型:
、logprobs
和best_of
。如果你设置了这些参数中的任何一个,你会得到一个错误。echo
现在每次我使用 gpt-3.5-turbo 通过 langchain 初始化 LLM 模型时,它都会给我这个错误:
InvalidRequestError: logprobs, best_of and echo parameters are not available on gpt-35-turbo model. Please remove the parameter and try again. For more details, see https://go.microsoft.com/fwlink/?linkid=2227346.
我不知道如何在 langchain 中“取消设置”这些参数。
这是我的代码:
from langchain.chains.llm import LLMChain
from langchain.llms.openai import OpenAI
from langchain.prompts.prompt import PromptTemplate
llm = OpenAI(temperature=0, engine=deployment_name)
template = """
You are a helpful assistant that translates English to French. Translate this sentence from English to French: {text}
"""
prompt = PromptTemplate(input_variables=["text"], template=template)
llm_chain = LLMChain(llm=llm, prompt=prompt)
response = llm_chain.generate([
{"text": "I love AI"},
{"text": "I love the ocean"},
])
for g in response.generations:
print(g[0].text)
请注意,我在 Azure 中使用 openAI, 我也试过这段代码,它仍然给我同样的错误
deployment_name = "my-deployment-name"
from langchain.llms import AzureOpenAI
llm = AzureOpenAI(deployment_name=deployment_name )
print(llm)
llm("Tell me a joke")
使用
ChatOpenAI
代替 OpenAI
(和 model_name
代替 engine
)在 langchain==0.0.127
上对我有用。
langchain的OpenAI
代码上有这个警告
if model_name.startswith("gpt-3.5-turbo") or model_name.startswith("gpt-4"):
warnings.warn(
"You are trying to use a chat model. This way of initializing it is "
"no longer supported. Instead, please use: "
"`from langchain.chat_models import ChatOpenAI`"
)
这段代码对我有用:
import os
from langchain.chains.llm import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts.prompt import PromptTemplate
llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo", openai_api_key=os.environ['OPENAI_KEY'])
template = """
You are a helpful assistant that translates English to French. Translate this sentence from English to French: {text}
"""
prompt = PromptTemplate(input_variables=["text"], template=template)
llm_chain = LLMChain(llm=llm, prompt=prompt)
response = llm_chain.generate([
{"text": "I love AI"},
{"text": "I love the ocean"},
])
for g in response.generations:
print(g[0].text)
所以,我终于能够通过创建 AzureOpenai 类的扩展并取消这些参数来修复它。有效的代码如下:
from langchain.llms import AzureOpenAI
from typing import List
class NewAzureOpenAI(AzureOpenAI):
stop: List[str] = None
@property
def _invocation_params(self):
params = super()._invocation_params
# fix InvalidRequestError: logprobs, best_of and echo parameters are not available on gpt-35-turbo model.
params.pop('logprobs', None)
params.pop('best_of', None)
params.pop('echo', None)
#params['stop'] = self.stop
return params
llm = NewAzureOpenAI(deployment_name=deployment_name,temperature=0.9)
llm("Tell me a joke")
答案实际上是在这个链接中找到的,它对我有用。