Error Building Component Error building Component OpenAI: 1 validation error for ChatOpenAI __root__ Did not find openai_api_key, please add an environment variable OPENAI_API_KEY
which contains it, or pass openai_api_key
as a named parameter. (type=value_error)
#3872
Replies: 1 comment
-
Good evening, @roli-reeshik! I'm here to help you with any bugs, questions, or contributions you have. Let's tackle this together! To resolve the "Error Building Component OpenAI: 1 validation error for ChatOpenAI root Did not find openai_api_key" issue, you need to ensure that the Here is the relevant part of the code where the def build(
self,
input_value: Text,
openai_api_key: str,
temperature: float,
model_name: str,
max_tokens: Optional[int] = 256,
model_kwargs: NestedDict = {},
openai_api_base: Optional[str] = None,
stream: bool = False,
system_message: Optional[str] = None,
) -> Text:
if not openai_api_base:
openai_api_base = "https://api.openai.com/v1"
if openai_api_key:
api_key = SecretStr(openai_api_key)
else:
api_key = None
output = ChatOpenAI(
max_tokens=max_tokens,
model_kwargs=model_kwargs,
model=model_name,
base_url=openai_api_base,
api_key=api_key,
temperature=temperature,
)
return self.get_chat_result(output, stream, input_value, system_message) Make sure that when you instantiate or configure the component = OpenAIModelComponent()
result = component.build(
input_value="Your input text here",
openai_api_key="your_openai_api_key_here",
temperature=0.7,
model_name="gpt-3.5-turbo",
max_tokens=150,
model_kwargs={},
openai_api_base=None,
stream=False,
system_message=None
) Replace Additionally, you can set the
Alternatively, you can configure environment variables in your |
Beta Was this translation helpful? Give feedback.
-
Good Evening Everone
Kindly guide me regarding this error.
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions