Skip to content

Commit 98f49b5

Browse files
author
Hakan Bogan
committed
fixed env file finding ve api key pass
1 parent fdc2c6c commit 98f49b5

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

agents/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(self, role: AgentRole, settings: Settings):
2727
def _create_llm(self, settings: Settings) -> ChatOpenAI:
2828
"""Create LLM instance with consistent configuration."""
2929
return ChatOpenAI(
30+
api_key=settings.openai_api_key,
3031
model=settings.openai_model,
3132
temperature=settings.openai_temperature,
3233
seed=settings.openai_seed,

app/config.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,34 @@
88
from pydantic_settings import BaseSettings, SettingsConfigDict
99

1010

11+
def _find_project_root() -> Path:
12+
"""Find project root directory by looking for .env file or pyproject.toml."""
13+
current = Path(__file__).resolve().parent
14+
15+
# Look for project root markers
16+
markers = [".env", "pyproject.toml", ".git"]
17+
18+
# Go up the directory tree
19+
for parent in [current] + list(current.parents):
20+
if any((parent / marker).exists() for marker in markers):
21+
return parent
22+
23+
# Fallback to current directory
24+
return Path.cwd()
25+
26+
27+
def _get_env_file_path() -> Path:
28+
"""Get absolute path to .env file."""
29+
project_root = _find_project_root()
30+
env_file = project_root / ".env"
31+
return env_file
32+
33+
1134
class Settings(BaseSettings):
1235
"""Application settings loaded from environment variables."""
1336

1437
model_config = SettingsConfigDict(
15-
env_file=".env",
38+
env_file=str(_get_env_file_path()),
1639
env_file_encoding="utf-8",
1740
case_sensitive=False,
1841
extra="ignore",
@@ -145,4 +168,13 @@ def get_prompt_path(self, agent_role: str, version: str | None = None) -> Path:
145168
@lru_cache
146169
def get_settings() -> Settings:
147170
"""Get cached settings instance."""
148-
return Settings()
171+
import os
172+
173+
settings = Settings()
174+
175+
# Set OpenAI API key as environment variable for CrewAI compatibility
176+
# CrewAI checks environment variable even when LLM object is provided
177+
if settings.openai_api_key:
178+
os.environ["OPENAI_API_KEY"] = settings.openai_api_key
179+
180+
return settings

0 commit comments

Comments
 (0)