Coding/Study

[error]ValidationError: 1 validation error for ChatOpenAI

후__아 2024. 8. 1. 14:28

https://wikidocs.net/231375

랭체인 LLM 실습 도중 오류 발생

Parameters {'presence_penalty', 'frequency_penalty', 'stop'} should be specified explicitly. Instead they were passed in as part of `model_kwargs` parameter. (type=value_error)
# LLM
from langchain_openai import ChatOpenAI

params = {# 기본 파라미터
    "temperature": 0.7,
    "max_tokens": 100,
}

kwargs = {# 선택 파라미터
    "frequency_penalty": 0.5,
    "presence_penalty": 0.5,
    "stop": ['\n']
}

model = ChatOpenAI(model = "gpt-3.5-turbo-0125", **params, model_kwargs = kwargs)
quest = "태양계에서 가장 큰 행성은 무엇인가요?"
resp = model.invoke(input = quest)

resp

 

파라미터 오류인 듯 싶어 랭체인 LLM 모델의 파라미터를 수정해보려고 했다.

 

https://api.python.langchain.com/en/latest/llms/langchain_openai.llms.base.OpenAI.html 에서 찾아보니까

'해당 파라미터가 specified explicitly 해야 된다'고 오류에 나와있는데

 

model_kwargs로 설정할 때는 아니라고 돼있어서

'presence_penalty' 인자 설정을 확인해봤더니

 

params에 기본 인자로 넣어도 되게 생겨서 넣어봤는데 됐다..얏호~

 

# 수정 이후 코드

from langchain_openai import ChatOpenAI

params = {# 기본 파라미터
    "temperature": 0.7,
    "max_tokens": 100,
    "frequency_penalty": 0.5,
    "presence_penalty": 0.5,
    "stop": ['\n']
}

model = ChatOpenAI(model = "gpt-3.5-turbo-0125", **params)
quest = "태양계에서 가장 큰 행성은 무엇인가요?"
resp = model.invoke(input = quest)

resp.content