Search code examples
openai-apilangchain

langchain: logprobs, best_of and echo parameters are not available on gpt-35-turbo model


I am trying to use langchain with gpt-35-turbo. It seems that the new gpt3.5 turbo is not using certain parameters anymore as per the link Learn how to work with the ChatGPT and GPT-4 models (preview)

The following parameters aren't available with the new ChatGPT and GPT-4 models: logprobs, best_of, and echo. If you set any of these parameters, you'll get an error.

Now each time I initialise an LLM model through langchain with gpt-3.5-turbo, it gives me this error:

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.

I don't know how to 'unset' these parameters in langchain.

This is my code:

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)

Note that I am using openAI in Azure, I also tried this code and it's still giving me the same error

deployment_name = "my-deployment-name"
from langchain.llms import AzureOpenAI
llm = AzureOpenAI(deployment_name=deployment_name )
print(llm)
llm("Tell me a joke")

Solution

  • So, I finally was able to fix it by creating an extension of AzureOpenai class and nullifying those arguments. Code that works is below:

    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")
    

    The answer was actually found in this link and it worked for me.