LangGraph_agent / config.py
pratikmurali's picture
More bug fixes and graceful error handling
026a398
"""
Configuration module for the LangGraph application.
"""
import os
from uuid import uuid4
from typing import Dict, Any
from dotenv import load_dotenv
def load_config() -> Dict[str, Any]:
"""
Load configuration from environment variables.
Returns:
Dict[str, Any]: Configuration dictionary
"""
# Try to load from .env file, but continue if file doesn't exist
try:
load_dotenv(raise_error_if_not_found=False)
except Exception:
pass
config = {
"openai_api_key": os.getenv("OPENAI_API_KEY").strip(),
"tavily_api_key": os.getenv("TAVILY_API_KEY").strip(),
"langsmith_api_key": os.getenv("LANGSMITH_API_KEY").strip(),
"langsmith_tracing_v2": os.getenv("LANGSMITH_TRACING_V2", "true"),
"langsmith_project": os.getenv("LANGSMITH_PROJECT", f"HF - Tool Calling Agent - {uuid4().hex[0:8]}"),
"model_name": os.getenv("MODEL_NAME", "gpt-4o-mini"),
"temperature": float(os.getenv("TEMPERATURE", "0")),
"tavily_max_results": int(os.getenv("TAVILY_MAX_RESULTS", "5"))
}
# Validate required configuration
missing_keys = []
required_keys = ["openai_api_key", "tavily_api_key"]
for key in required_keys:
if not config[key]:
missing_keys.append(key)
if missing_keys:
error_message = f"Missing required environment variables: {', '.join(missing_keys)}"
if os.getenv("HF_SPACE", "false").lower() == "true":
print(f"ERROR: {error_message}")
print("Please add these as secrets in your Hugging Face Space settings.")
else:
raise ValueError(error_message)
return config