L2 utils functions expect to find a .env file at the directory above the lesson

When I attempt to create the Planner Agent via this code in L2:

planner = Agent(
role=“Content Planner”,
goal=“Plan engaging and factually accurate content on {topic}”,
backstory="You’re working on planning a blog article "
“about the topic: {topic}.”
"You collect information that helps the "
"audience learn something "
"and make informed decisions. "
"Your work is the basis for "
“the Content Writer to write an article on this topic.”,
allow_delegation=False,
verbose=True
)

I get the following error:


ValidationError Traceback (most recent call last)
Cell In[4], line 1
----> 1 planner = Agent(
2 role=“Content Planner”,
3 goal=“Plan engaging and factually accurate content on {topic}”,
4 backstory="You’re working on planning a blog article "
5 “about the topic: {topic}.”
6 "You collect information that helps the "
7 "audience learn something "
8 "and make informed decisions. "
9 "Your work is the basis for "
10 “the Content Writer to write an article on this topic.”,
11 allow_delegation=False,
12 verbose=True
13 )

File /opt/anaconda3/lib/python3.11/site-packages/crewai/agent.py:131, in Agent.init(pydantic_self, **data)
129 def init(pydantic_self, **data):
130 config = data.pop(“config”, {})
→ 131 super().init(**config, **data)

[... skipping hidden 1 frame]

File /opt/anaconda3/lib/python3.11/site-packages/crewai/agent.py:113, in Agent.()
107 step_callback: Optional[Any] = Field(
108 default=None,
109 description=“Callback to be executed after each step of the agent execution.”,
110 )
111 i18n: I18N = Field(default=I18N(), description=“Internationalization settings.”)
112 llm: Any = Field(
→ 113 default_factory=lambda: ChatOpenAI(
114 model=os.environ.get(“OPENAI_MODEL_NAME”, “gpt-4”)
115 ),
116 description=“Language model that will run the agent.”,
117 )
118 function_calling_llm: Optional[Any] = Field(
119 description=“Language model that will run the agent.”, default=None
120 )
121 callbacks: Optional[List[InstanceOf[BaseCallbackHandler]]] = Field(
122 default=None, description=“Callback to be executed”
123 )

File /opt/anaconda3/lib/python3.11/site-packages/pydantic/v1/main.py:341, in BaseModel.init(pydantic_self, **data)
339 values, fields_set, validation_error = validate_model(pydantic_self.class, data)
340 if validation_error:
→ 341 raise validation_error
342 try:
343 object_setattr(pydantic_self, ‘dict’, values)

ValidationError: 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)

I noticed in the utils.py file, the comment:

these expect to find a .env file at the directory above the lesson.

I am running on macOS 12.7.4.
Should I be using a pre-existing .env file on my Mac, if so, I don’t know where it is located. Can you provide a .env file that i can put in the directory above the lesson?

Hi @rponesoul,

.env file used in the courses is not shared with the learners.

You can create your own .env file which would include your own API keys.

Best,
Mubsi

Much appreciated. I got it to work once I figured out how to add the .env file.

1 Like