Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ agents/salesagent/
Copy `.env.example` to `.env` and set your API credentials:

```env
COGSOL_ENV=local
COGSOL_API_BASE=https://api.cogsol.ai/cognitive/
COGSOL_CONTENT_API_BASE=https://api.cogsol.ai/content/
COGSOL_
COGSOL_ENV=development
COGSOL_API_KEY=your-api-key
# Optional: Azure AD B2C client credentials for JWT
# If not provided, the Auth will be skipped
Expand Down Expand Up @@ -670,10 +667,8 @@ from cogsol.content import BaseRetrieval, ReorderingStrategy

| Variable | Required | Description |
|----------|----------|-------------|
| `COGSOL_API_BASE` | Yes | Base URL for the CogSol Cognitive API |
| `COGSOL_CONTENT_API_BASE` | No | Base URL for the CogSol Content API (defaults to `COGSOL_API_BASE`) |
| `COGSOL_API_KEY` | Yes | API Key authentication |
| `COGSOL_ENV` | No | Environment name (e.g., `local`, `production`) |
| `COGSOL_ENV` | No | Environment name (e.g., `local`, `development`, `production`) |
| `COGSOL_AUTH_CLIENT_ID` | No | Client Id provided for adminitrators |
| `COGSOL_AUTH_SECRET` | No | Auth Secret provided for adminitrators |

Expand Down
3 changes: 2 additions & 1 deletion cogsol/agents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing import Any

from cogsol.core.api import CogSolAPIError, CogSolClient
from cogsol.core.constants import get_cognitive_api_base_url
from cogsol.core.env import load_dotenv


Expand Down Expand Up @@ -98,7 +99,7 @@ def run(
project_path = Path.cwd()
load_dotenv(project_path / ".env")

base_url = api_base or self._api_base or os.environ.get("COGSOL_API_BASE")
base_url = api_base or self._api_base or get_cognitive_api_base_url()
if not base_url:
raise CogSolAPIError("COGSOL_API_BASE is required to run agents.")
token = api_key or self._api_key or os.environ.get("COGSOL_API_KEY")
Expand Down
10 changes: 6 additions & 4 deletions cogsol/content/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
from typing import Any

from cogsol.core.api import CogSolAPIError, CogSolClient
from cogsol.core.constants import (
get_cognitive_api_base_url,
get_content_api_base_url,
)
from cogsol.core.env import load_dotenv


Expand Down Expand Up @@ -286,10 +290,8 @@ def run(
project_path = Path.cwd()
load_dotenv(project_path / ".env")

content_base = (
content_api_base or self._content_api_base or os.environ.get("COGSOL_CONTENT_API_BASE")
)
base_url = api_base or self._api_base or os.environ.get("COGSOL_API_BASE") or content_base
content_base = content_api_base or self._content_api_base or get_content_api_base_url()
base_url = api_base or self._api_base or get_cognitive_api_base_url() or content_base
if not content_base:
raise CogSolAPIError("COGSOL_CONTENT_API_BASE is required to run retrievals.")
token = api_key or self._api_key or os.environ.get("COGSOL_API_KEY")
Expand Down
9 changes: 7 additions & 2 deletions cogsol/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
import msal
from jwt import decode

from cogsol.core.constants import (
get_cognitive_api_base_url,
get_content_api_base_url,
)


class CogSolAPIError(RuntimeError):
pass
Expand Down Expand Up @@ -73,9 +78,9 @@ def __init__(
api_key: str | None = None,
content_base_url: str | None = None,
) -> None:
self.base_url = base_url or os.environ.get("COGSOL_API_BASE") or ""
self.base_url = base_url or get_cognitive_api_base_url()
self.api_key = api_key or os.environ.get("COGSOL_API_KEY")
self.content_base_url = content_base_url or os.environ.get("COGSOL_CONTENT_API_BASE")
self.content_base_url = content_base_url or get_content_api_base_url()
self.bearer_token = None
self.bearer_token_expires_at = None

Expand Down
66 changes: 66 additions & 0 deletions cogsol/core/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""Shared constants and helpers for CogSol API base URLs."""

from __future__ import annotations

import os
from typing import Final

COGSOL_ENV_VAR: Final = "COGSOL_ENV"
COGSOL_API_BASE_VAR: Final = "COGSOL_API_BASE"
COGSOL_CONTENT_API_BASE_VAR: Final = "COGSOL_CONTENT_API_BASE"

IMPLANTATION_COGNITIVE_API_URL: Final = "https://apis-imp.cogsol.ai/cognitive"
IMPLANTATION_CONTENT_API_URL: Final = "https://apis-imp.cogsol.ai/content"
PRODUCTION_COGNITIVE_API_URL: Final = "https://apis.cogsol.ai/cognitive"
PRODUCTION_CONTENT_API_URL: Final = "https://apis.cogsol.ai/content"


def _get_env_var(name: str) -> str | None:
"""Return an environment variable value (case-insensitive)."""
return os.environ.get(name) or os.environ.get(name.lower())


def get_cogsol_env() -> str:
"""Return the current CogSol environment name."""
value = _get_env_var(COGSOL_ENV_VAR)
return value.strip().lower() if value else ""


def get_default_cognitive_api_base_url() -> str:
"""Return the default base URL for the cognitive API."""
if get_cogsol_env() == "production":
return PRODUCTION_COGNITIVE_API_URL
return IMPLANTATION_COGNITIVE_API_URL


def get_default_content_api_base_url() -> str:
"""Return the default base URL for the content API."""
if get_cogsol_env() == "production":
return PRODUCTION_CONTENT_API_URL
return IMPLANTATION_CONTENT_API_URL


def get_cognitive_api_base_url() -> str:
"""Resolve the cognitive API base URL using env overrides when present."""
return _get_env_var(COGSOL_API_BASE_VAR) or get_default_cognitive_api_base_url()


def get_content_api_base_url() -> str:
"""Resolve the content API base URL using env overrides when present."""
return _get_env_var(COGSOL_CONTENT_API_BASE_VAR) or get_default_content_api_base_url()


__all__ = [
"COGSOL_ENV_VAR",
"COGSOL_API_BASE_VAR",
"COGSOL_CONTENT_API_BASE_VAR",
"IMPLANTATION_COGNITIVE_API_URL",
"IMPLANTATION_CONTENT_API_URL",
"PRODUCTION_COGNITIVE_API_URL",
"PRODUCTION_CONTENT_API_URL",
"get_cogsol_env",
"get_default_cognitive_api_base_url",
"get_default_content_api_base_url",
"get_cognitive_api_base_url",
"get_content_api_base_url",
]
3 changes: 2 additions & 1 deletion cogsol/management/commands/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import Any, cast

from cogsol.core.api import CogSolAPIError, CogSolClient
from cogsol.core.constants import get_cognitive_api_base_url
from cogsol.core.env import load_dotenv
from cogsol.management.base import BaseCommand

Expand Down Expand Up @@ -316,7 +317,7 @@ def handle(self, project_path: Path | None, **options: Any) -> int:
app = str(options.get("app") or "agents")
load_dotenv(project_path / ".env")

api_base = os.environ.get("COGSOL_API_BASE")
api_base = get_cognitive_api_base_url()
api_key = os.environ.get("COGSOL_API_KEY")
if not api_base:
print_error("COGSOL_API_BASE is required in .env to chat with CogSol.")
Expand Down
8 changes: 6 additions & 2 deletions cogsol/management/commands/importagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from typing import Any, cast

from cogsol.core.api import CogSolAPIError, CogSolClient
from cogsol.core.constants import (
get_cognitive_api_base_url,
get_content_api_base_url,
)
from cogsol.core.env import load_dotenv
from cogsol.core.loader import (
_extract_tool_params,
Expand Down Expand Up @@ -289,9 +293,9 @@ def handle(self, project_path: Path | None, **options: Any) -> int:

import os

api_base = os.environ.get("COGSOL_API_BASE")
api_base = get_cognitive_api_base_url()
api_key = os.environ.get("COGSOL_API_KEY")
content_base = os.environ.get("COGSOL_CONTENT_API_BASE") or api_base
content_base = get_content_api_base_url() or api_base
if not api_base:
print("COGSOL_API_BASE is required in .env to import.")
return 1
Expand Down
23 changes: 6 additions & 17 deletions cogsol/management/commands/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

from cogsol.content import BaseIngestionConfig, DocType
from cogsol.core.api import CogSolClient
from cogsol.core.constants import (
get_cognitive_api_base_url,
get_content_api_base_url,
)
from cogsol.core.env import load_dotenv
from cogsol.management.base import BaseCommand

Expand All @@ -36,26 +40,11 @@

def get_client(project_path: Path) -> CogSolClient:
"""Get an API client configured for the project."""
import sys

load_dotenv(project_path / ".env")

sys.path.insert(0, str(project_path))
try:
import settings

api_base = getattr(settings, "COGSOL_API_BASE", None)
content_base = getattr(settings, "COGSOL_CONTENT_API_BASE", None)
except ImportError:
api_base = None
content_base = None
finally:
sys.path.pop(0)

import os

api_base = api_base or os.environ.get("COGSOL_API_BASE", "http://localhost:8000")
content_base = content_base or os.environ.get("COGSOL_CONTENT_API_BASE", api_base)
api_base = get_cognitive_api_base_url()
content_base = get_content_api_base_url()
token = os.environ.get("COGSOL_API_KEY")

return CogSolClient(base_url=api_base, api_key=token, content_base_url=content_base)
Expand Down
11 changes: 7 additions & 4 deletions cogsol/management/commands/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import copy
import inspect
import json
import os
import re
import textwrap
from pathlib import Path
Expand All @@ -13,6 +14,10 @@
from cogsol.content import BaseRetrieval
from cogsol.core import migrations as migutils
from cogsol.core.api import CogSolAPIError, CogSolClient
from cogsol.core.constants import (
get_cognitive_api_base_url,
get_content_api_base_url,
)
from cogsol.core.env import load_dotenv
from cogsol.core.loader import _extract_tool_params, collect_classes, collect_content_classes
from cogsol.db import migrations
Expand Down Expand Up @@ -59,9 +64,9 @@ def handle(self, project_path: Path | None, **options: Any) -> int:
apps = [str(app)] if app else ["agents", "data"]

load_dotenv(project_path / ".env")
api_base = self._env("COGSOL_API_BASE")
api_base = get_cognitive_api_base_url()
api_key = self._env("COGSOL_API_KEY", required=False)
content_base = self._env("COGSOL_CONTENT_API_BASE", required=False) or api_base
content_base = get_content_api_base_url()
if not api_base:
print("COGSOL_API_BASE is required in .env to run migrations against CogSol APIs.")
return 1
Expand Down Expand Up @@ -158,8 +163,6 @@ def handle(self, project_path: Path | None, **options: Any) -> int:

# ------------------------------------------------------------------ helpers
def _env(self, key: str, required: bool = True) -> str | None:
import os

value = os.environ.get(key)
if required and not value:
return None
Expand Down
2 changes: 1 addition & 1 deletion cogsol/management/commands/startproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def handle(self, project_path: Path | None, **options: Any) -> int:
"data/retrievals.py": DATA_RETRIEVALS_PY,
"data/migrations/__init__.py": "",
"README.md": README.format(project_name=name),
".env.example": "COGSOL_ENV=local\nCOGSOL_API_BASE=http://localhost:8000\nCOGSOL_CONTENT_API_BASE=http://localhost:8001\n# Optional: COGSOL_API_KEY=your-api-key\n# Optional: Azure AD B2C client credentials for JWT\n# If not provided, the Auth will be skipped\n# COGSOL_AUTH_CLIENT_ID=you-client-id\n# COGSOL_AUTH_SECRET=your-secret\n",
".env.example": "COGSOL_ENV=development\n#COGSOL_API_KEY=your-api-key\n# Optional: Azure AD B2C client credentials for JWT\n# If not provided, the Auth will be skipped\n# COGSOL_AUTH_CLIENT_ID=you-client-id\n# COGSOL_AUTH_SECRET=your-secret\n",
}

for relative_path, content in files.items():
Expand Down
23 changes: 6 additions & 17 deletions cogsol/management/commands/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,21 @@
from typing import Any

from cogsol.core.api import CogSolClient
from cogsol.core.constants import (
get_cognitive_api_base_url,
get_content_api_base_url,
)
from cogsol.core.env import load_dotenv
from cogsol.management.base import BaseCommand


def get_client(project_path: Path) -> CogSolClient:
"""Get an API client configured for the project."""
import sys

load_dotenv(project_path / ".env")

sys.path.insert(0, str(project_path))
try:
import settings

api_base = getattr(settings, "COGSOL_API_BASE", None)
content_base = getattr(settings, "COGSOL_CONTENT_API_BASE", None)
except ImportError:
api_base = None
content_base = None
finally:
sys.path.pop(0)

import os

api_base = api_base or os.environ.get("COGSOL_API_BASE", "http://localhost:8000")
content_base = content_base or os.environ.get("COGSOL_CONTENT_API_BASE", api_base)
api_base = get_cognitive_api_base_url()
content_base = get_content_api_base_url()
api_key = os.environ.get("COGSOL_API_KEY")

return CogSolClient(base_url=api_base, api_key=api_key, content_base_url=content_base)
Expand Down
12 changes: 6 additions & 6 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class CogSolClient:

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `base_url` | `str` | Yes | Base URL for the Cognitive API (e.g., `https://api.cogsol.ai/cognitive/`) |
| `base_url` | `str` | Yes | Base URL for the Cognitive API (e.g., `https://apis-imp.cogsol.ai/cognitive`) |
| `token` | `str` | No | API authentication token |
| `content_base_url` | `str` | No | Base URL for Content API (defaults to `base_url` if not set) |

Expand All @@ -61,13 +61,13 @@ class CogSolClient:
from cogsol.core.api import CogSolClient

# Without authentication
client = CogSolClient(base_url="https://api.cogsol.ai/cognitive/")
client = CogSolClient(base_url="https://apis-imp.cogsol.ai/cognitive")

# With authentication and separate Content API
client = CogSolClient(
base_url="https://api.cogsol.ai/cognitive/",
base_url="https://apis-imp.cogsol.ai/cognitive",
token="sk-your-api-key",
content_base_url="https://api.cogsol.ai/content/"
content_base_url="https://apis-imp.cogsol.ai/content"
)
```

Expand Down Expand Up @@ -595,7 +595,7 @@ class CogSolAPIError(RuntimeError):
```python
from cogsol.core.api import CogSolClient, CogSolAPIError

client = CogSolClient(base_url="https://api.cogsol.ai/cognitive/", token="...")
client = CogSolClient(base_url="https://apis-imp.cogsol.ai/cognitive", token="...")

try:
assistant = client.get_assistant(9999)
Expand Down Expand Up @@ -939,7 +939,7 @@ from cogsol.core.api import CogSolClient

# Initialize client
client = CogSolClient(
base_url="https://api.cogsol.ai/cognitive/",
base_url="https://apis-imp.cogsol.ai/cognitive",
token="sk-your-token"
)

Expand Down
Loading