diff --git a/.gitattributes b/.gitattributes index d63369290f44e8601e3375b73c2d75637d548d57..b9dd411bc9229b248d22784ad4b945258426de30 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1013,3 +1013,8 @@ western_ai/canada/cohere-toolkit/docs/assets/vscode_debug.png filter=lfs diff=lf western_ai/canada/cohere-toolkit/docs/walkthrough/assets/default_set_up.png filter=lfs diff=lfs merge=lfs -text western_ai/canada/cohere-toolkit/docs/walkthrough/assets/toolkit.png filter=lfs diff=lfs merge=lfs -text western_ai/canada/cohere-toolkit/docs/walkthrough/assets/whole_set_up.png filter=lfs diff=lfs merge=lfs -text +western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/26mb.pdf filter=lfs diff=lfs merge=lfs -text +western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Cardistry.pdf filter=lfs diff=lfs merge=lfs -text +western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Mariana_Trench.pdf filter=lfs diff=lfs merge=lfs -text +western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Mount_Everest.pdf filter=lfs diff=lfs merge=lfs -text +western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Tapas.pdf filter=lfs diff=lfs merge=lfs -text diff --git a/western_ai/canada/cohere-toolkit/src/backend/pytest_integration.ini b/western_ai/canada/cohere-toolkit/src/backend/pytest_integration.ini new file mode 100644 index 0000000000000000000000000000000000000000..0d87cbc1c8088af1237d9b79d774bd747d4a46b7 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/pytest_integration.ini @@ -0,0 +1,6 @@ +[pytest] +env = + DATABASE_URL=postgresql://postgres:postgres@localhost:5433 +filterwarnings = + ignore::UserWarning:pydantic.* + ignore::DeprecationWarning \ No newline at end of file diff --git a/western_ai/canada/cohere-toolkit/src/backend/routers/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/routers/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..1f6994f6cf41ffe10e4bd1528ddf1264b2d154a1 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/routers/__init__.py @@ -0,0 +1,2 @@ +# ruff: noqa +from backend.routers.chat import * diff --git a/western_ai/canada/cohere-toolkit/src/backend/routers/agent.py b/western_ai/canada/cohere-toolkit/src/backend/routers/agent.py new file mode 100644 index 0000000000000000000000000000000000000000..2bc20f83d31caba3e9e294f3e7f487ad2199a67c --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/routers/agent.py @@ -0,0 +1,617 @@ +import asyncio + +from fastapi import APIRouter, Depends, HTTPException +from fastapi import File as RequestFile +from fastapi import UploadFile as FastAPIUploadFile + +from backend.config.auth import is_authentication_enabled +from backend.config.default_agent import DEFAULT_AGENT_ID, get_default_agent +from backend.config.routers import RouterName +from backend.crud import agent as agent_crud +from backend.crud import agent_tool_metadata as agent_tool_metadata_crud +from backend.crud import file as file_crud +from backend.crud import snapshot as snapshot_crud +from backend.database_models.agent import Agent as AgentModel +from backend.database_models.agent_tool_metadata import ( + AgentToolMetadata as AgentToolMetadataModel, +) +from backend.database_models.database import DBSessionDep +from backend.routers.utils import ( + get_default_deployment_model, + get_deployment_model_from_agent, +) +from backend.schemas.agent import ( + Agent, + AgentPublic, + AgentToolMetadata, + AgentToolMetadataPublic, + AgentVisibility, + CreateAgentRequest, + CreateAgentToolMetadataRequest, + DeleteAgent, + DeleteAgentToolMetadata, + UpdateAgentDB, + UpdateAgentRequest, + UpdateAgentToolMetadataRequest, +) +from backend.schemas.context import Context +from backend.schemas.deployment import DeploymentDefinition +from backend.schemas.file import ( + DeleteAgentFileResponse, + FileMetadata, + UploadAgentFileResponse, +) +from backend.schemas.params.agent import ( + AgentIdPathParam, + AgentToolMetadataIdPathParam, + VisibilityQueryParam, +) +from backend.schemas.params.file import FileIdPathParam +from backend.schemas.params.organization import OrganizationIdQueryParam +from backend.schemas.params.shared import PaginationQueryParams +from backend.services.agent import ( + raise_db_error, + validate_agent_exists, + validate_agent_tool_metadata_exists, +) +from backend.services.context import get_context +from backend.services.file import ( + get_file_service, + validate_file, +) +from backend.services.request_validators import ( + validate_create_agent_request, + validate_update_agent_request, + validate_user_header, +) + +router = APIRouter( + prefix="/v1/agents", + tags=[RouterName.AGENT], +) +router.name = RouterName.AGENT + +auth_dependencies = [] if is_authentication_enabled() else [Depends(validate_user_header)] + + +@router.post( + "", + response_model=AgentPublic, + dependencies=[ + *auth_dependencies, + Depends(validate_create_agent_request), + ], +) +async def create_agent( + agent: CreateAgentRequest, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> AgentPublic: + """ + Create an agent. + + Raises: + HTTPException: If the agent creation fails. + """ + ctx.with_user(session) + user_id = ctx.get_user_id() + logger = ctx.get_logger() + logger.debug(event="Creating agent", user_id=user_id, agent=agent.model_dump()) + + deployment_db, model_db = get_deployment_model_from_agent(agent, session) + default_deployment_db, default_model_db = get_default_deployment_model(session) + logger.debug(event="Deployment and model", deployment=deployment_db, model=model_db) + + if not deployment_db or not model_db: + logger.error(event="Unable to find deployment or model, using defaults", agent=agent) + + if not default_deployment_db or not default_model_db: + logger.error(event="Unable to find default deployment or model", agent=agent) + raise HTTPException(status_code=400, detail="Unable to find deployment or model") + + deployment_db = default_deployment_db + model_db = default_model_db + + try: + agent_data = AgentModel( + name=agent.name, + description=agent.description, + preamble=agent.preamble, + temperature=agent.temperature, + user_id=user_id, + organization_id=agent.organization_id, + tools=agent.tools, + is_private=agent.is_private, + deployment_id=deployment_db.id, + model_id=model_db.id, + ) + + created_agent = agent_crud.create_agent(session, agent_data) + + if not created_agent: + raise HTTPException(status_code=500, detail="Failed to create Agent") + + if agent.tools_metadata: + for tool_metadata in agent.tools_metadata: + await update_or_create_tool_metadata( + created_agent, tool_metadata, session, ctx + ) + + agent_schema = Agent.model_validate(created_agent) + ctx.with_agent(agent_schema) + logger.debug(event="Agent created", agent=created_agent) + return created_agent + except Exception as e: + logger.exception(event=e) + raise HTTPException(status_code=500, detail=str(e)) + + +@router.get("", response_model=list[AgentPublic]) +async def list_agents( + *, + page_params: PaginationQueryParams, + visibility: VisibilityQueryParam = AgentVisibility.ALL, + org_id: OrganizationIdQueryParam = None, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> list[AgentPublic]: + """ + List all agents. + """ + logger = ctx.get_logger() + # TODO: get organization_id from user + user_id = ctx.get_user_id() + logger = ctx.get_logger() + # request organization_id is used for filtering agents instead of header Organization-Id if enabled + if org_id: + logger.debug(event="Request limited to organization", organization_id=org_id) + ctx.without_global_filtering() + + try: + agents = agent_crud.get_agents( + session, + user_id=user_id, + offset=page_params.offset, + limit=page_params.limit, + visibility=visibility, + organization_id=org_id, + ) + # Tradeoff: This appends the default Agent regardless of pagination + agents.append(get_default_agent()) + logger.debug(event="Returning agents:", agents=agents) + return agents + except Exception as e: + logger.exception(event=e) + raise HTTPException(status_code=500, detail=str(e)) + + +@router.get("/{agent_id}", response_model=AgentPublic) +async def get_agent_by_id( + agent_id: AgentIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context) +) -> AgentPublic: + """ + Return an agent by ID. + + Raises: + HTTPException: If the agent with the given ID is not found. + """ + user_id = ctx.get_user_id() + agent = None + + try: + # Intentionally not adding Default Agent to DB so it's more flexible + if agent_id == DEFAULT_AGENT_ID: + agent = get_default_agent() + else: + agent = agent_crud.get_agent_by_id(session, agent_id, user_id) + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) + + if not agent: + raise HTTPException( + status_code=404, + detail=f"Agent with ID {agent_id} not found.", + ) + + agent_schema = Agent.model_validate(agent) + ctx.with_agent(agent_schema) + return agent + + +@router.get("/{agent_id}/deployments", response_model=list[DeploymentDefinition]) +async def get_agent_deployment( + agent_id: AgentIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context) +) -> list[DeploymentDefinition]: + """ + Get the deployment for an agent + + Raises: + HTTPException: If the agent with the given ID is not found. + """ + user_id = ctx.get_user_id() + agent = validate_agent_exists(session, agent_id, user_id) + + agent_schema = Agent.model_validate(agent) + ctx.with_agent(agent_schema) + + return [ + DeploymentDefinition.from_db_deployment(deployment) + for deployment in agent.deployments + ] + + +@router.put( + "/{agent_id}", + response_model=AgentPublic, + dependencies=[ + *auth_dependencies, + Depends(validate_update_agent_request), + ], +) +async def update_agent( + agent_id: AgentIdPathParam, + new_agent: UpdateAgentRequest, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> AgentPublic: + """ + Update an agent by ID. + + Raises: + HTTPException: If the agent with the given ID is not found. + """ + user_id = ctx.get_user_id() + ctx.with_user(session) + agent = validate_agent_exists(session, agent_id, user_id) + + if new_agent.tools_metadata is not None: + agent = await handle_tool_metadata_update(agent, new_agent, session, ctx) + + # If the agent was public and is now private, we need to delete all snapshots associated with it + snapshot_deletion_task = None + if not agent.is_private and new_agent.is_private: + snapshot_deletion_task = asyncio.create_task( + snapshot_crud.delete_snapshots_by_agent_id(session, agent_id) + ) + + try: + db_deployment, db_model = get_deployment_model_from_agent(new_agent, session) + new_agent_db = UpdateAgentDB(**new_agent.model_dump()) + if db_deployment and db_model: + new_agent_db.model_id = db_model.id + new_agent_db.deployment_id = db_deployment.id + agent = agent_crud.update_agent( + session, agent, new_agent_db, user_id + ) + agent_schema = Agent.model_validate(agent) + ctx.with_agent(agent_schema) + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) + finally: + # Wait for snapshot deletion task to finish + if snapshot_deletion_task: + await snapshot_deletion_task + + return agent + + +@router.delete("/{agent_id}", response_model=DeleteAgent) +async def delete_agent( + agent_id: AgentIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> DeleteAgent: + """ + Delete an agent by ID. + + Raises: + HTTPException: If the agent with the given ID is not found. + """ + user_id = ctx.get_user_id() + agent = validate_agent_exists(session, agent_id, user_id) + agent_schema = Agent.model_validate(agent) + ctx.with_agent(agent_schema) + + deleted = agent_crud.delete_agent(session, agent_id, user_id) + if not deleted: + raise HTTPException(status_code=401, detail="Could not delete Agent.") + + return DeleteAgent() + + +# Agent Tool Metadata endpoints + + +async def handle_tool_metadata_update( + agent: Agent, + new_agent: Agent, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> Agent: + """Update or create tool metadata for an agent. + + Args: + agent (Agent): Agent. + new_agent (Agent): New agent data. + session (DBSessionDep): Database session. + ctx (Context): Context object. + + Returns: + Agent: Agent. + """ + user_id = ctx.get_user_id() + validate_agent_exists(session, agent.id, user_id) + + # Delete tool metadata that are not in the request + new_tools_names = [metadata.tool_name for metadata in new_agent.tools_metadata] + for tool_metadata in agent.tools_metadata: + if tool_metadata.tool_name not in new_tools_names: + agent_tool_metadata_crud.delete_agent_tool_metadata_by_id( + session, tool_metadata.id + ) + + # Create or update tool metadata from the request + for tool_metadata in new_agent.tools_metadata: + try: + await update_or_create_tool_metadata(agent, tool_metadata, session, ctx) + except Exception as e: + raise_db_error(e, "Tool name", tool_metadata.tool_name) + + # Remove tools_metadata from new_agent to avoid updating it in the agent + new_agent.tools_metadata = None + agent = agent_crud.get_agent_by_id(session, agent.id, user_id) + return agent + + +async def update_or_create_tool_metadata( + agent: Agent, + new_tool_metadata: AgentToolMetadata, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> None: + """Update or create tool metadata for an agent. + + Args: + agent (Agent): Agent. + new_tool_metadata (AgentToolMetadata): New tool metadata. + session (DBSessionDep): Database session. + ctx (Context): Context object. + """ + + existing_tools_names = [metadata.tool_name for metadata in agent.tools_metadata] + if new_tool_metadata.tool_name in existing_tools_names or new_tool_metadata.id: + await update_agent_tool_metadata( + agent.id, new_tool_metadata.id, new_tool_metadata, session, ctx + ) + else: + create_metadata_req = CreateAgentToolMetadataRequest( + **new_tool_metadata.model_dump(exclude_none=True) + ) + create_agent_tool_metadata(agent.id, create_metadata_req, session, ctx) + + +@router.get("/{agent_id}/tool-metadata", response_model=list[AgentToolMetadataPublic]) +async def list_agent_tool_metadata( + agent_id: AgentIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context) +) -> list[AgentToolMetadataPublic]: + """ + List all agent tool metadata by agent ID. + + Raises: + HTTPException: If the agent tool metadata retrieval fails. + """ + user_id = ctx.get_user_id() + validate_agent_exists(session, agent_id, user_id) + + try: + return agent_tool_metadata_crud.get_all_agent_tool_metadata_by_agent_id( + session, agent_id + ) + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) + + +@router.post( + "/{agent_id}/tool-metadata", + response_model=AgentToolMetadataPublic, +) +def create_agent_tool_metadata( + agent_id: AgentIdPathParam, + agent_tool_metadata: CreateAgentToolMetadataRequest, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> AgentToolMetadataPublic: + """ + Create an agent tool metadata. + + Raises: + HTTPException: If the agent tool metadata creation fails. + """ + user_id = ctx.get_user_id() + agent = validate_agent_exists(session, agent_id, user_id) + agent_schema = Agent.model_validate(agent) + ctx.with_agent(agent_schema) + + agent_tool_metadata_data = AgentToolMetadataModel( + user_id=user_id, + agent_id=agent_id, + tool_name=agent_tool_metadata.tool_name, + artifacts=agent_tool_metadata.artifacts, + ) + + try: + created_agent_tool_metadata = ( + agent_tool_metadata_crud.create_agent_tool_metadata( + session, agent_tool_metadata_data + ) + ) + + metadata_schema = AgentToolMetadata.model_validate(created_agent_tool_metadata) + ctx.with_agent_tool_metadata(metadata_schema) + except Exception as e: + raise_db_error(e, "Tool name", agent_tool_metadata.tool_name) + + return created_agent_tool_metadata + + +@router.put("/{agent_id}/tool-metadata/{agent_tool_metadata_id}") +async def update_agent_tool_metadata( + agent_id: AgentIdPathParam, + agent_tool_metadata_id: AgentToolMetadataIdPathParam, + new_agent_tool_metadata: UpdateAgentToolMetadataRequest, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> AgentToolMetadata: + """ + Update an agent tool metadata by ID. + + Raises: + HTTPException: If the agent tool metadata with the given ID is not found. + HTTPException: If the agent tool metadata update fails. + """ + user_id = ctx.get_user_id() + validate_agent_exists(session, agent_id, user_id) + + agent_tool_metadata = validate_agent_tool_metadata_exists( + session, agent_tool_metadata_id + ) + + try: + agent_tool_metadata_crud.update_agent_tool_metadata( + session, agent_tool_metadata, new_agent_tool_metadata + ) + metadata_schema = AgentToolMetadata.model_validate(agent_tool_metadata) + ctx.with_agent_tool_metadata(metadata_schema) + except Exception as e: + raise_db_error(e, "Tool name", agent_tool_metadata.tool_name) + + return agent_tool_metadata + + +@router.delete("/{agent_id}/tool-metadata/{agent_tool_metadata_id}") +async def delete_agent_tool_metadata( + agent_id: AgentIdPathParam, + agent_tool_metadata_id: AgentToolMetadataIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> DeleteAgentToolMetadata: + """ + Delete an agent tool metadata by ID. + + Raises: + HTTPException: If the agent tool metadata with the given ID is not found. + HTTPException: If the agent tool metadata deletion fails. + """ + user_id = ctx.get_user_id() + validate_agent_exists(session, agent_id, user_id) + + agent_tool_metadata = validate_agent_tool_metadata_exists( + session, agent_tool_metadata_id + ) + + metadata_schema = AgentToolMetadata.model_validate(agent_tool_metadata) + ctx.with_agent_tool_metadata(metadata_schema) + try: + agent_tool_metadata_crud.delete_agent_tool_metadata_by_id( + session, agent_tool_metadata_id + ) + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) + + return DeleteAgentToolMetadata() + + +@router.post("/batch_upload_file", response_model=list[UploadAgentFileResponse]) +async def batch_upload_file( + *, + files: list[FastAPIUploadFile] = RequestFile(...), + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> UploadAgentFileResponse: + """ + Upload a batch of files + """ + user_id = ctx.get_user_id() + + uploaded_files = [] + try: + uploaded_files = await get_file_service().create_agent_files( + session, + files, + user_id, + ctx, + ) + except Exception as e: + raise HTTPException( + status_code=500, detail=f"Error while uploading agent file(s): {e}." + ) + + return uploaded_files + + +@router.get("/{agent_id}/files/{file_id}", response_model=FileMetadata) +async def get_agent_file( + agent_id: AgentIdPathParam, + file_id: FileIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> FileMetadata: + """ + Get an agent file by ID. + + Raises: + HTTPException: If the agent or file with the given ID is not found, or if the file does not belong to the agent. + """ + user_id = ctx.get_user_id() + + if file_id not in get_file_service().get_file_ids_by_agent_id(session, user_id, agent_id, ctx): + raise HTTPException( + status_code=404, + detail=f"File with ID: {file_id} does not belong to the agent with ID: {agent_id}." + ) + + file = file_crud.get_file(session, file_id) + + if not file: + raise HTTPException( + status_code=404, + detail=f"File with ID: {file_id} not found.", + ) + + return FileMetadata( + id=file.id, + file_name=file.file_name, + file_content=file.file_content, + file_size=file.file_size, + created_at=file.created_at, + updated_at=file.updated_at, + ) + + +@router.delete("/{agent_id}/files/{file_id}") +async def delete_agent_file( + agent_id: AgentIdPathParam, + file_id: FileIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> DeleteAgentFileResponse: + """ + Delete an agent file by ID. + + Raises: + HTTPException: If the agent with the given ID is not found. + """ + user_id = ctx.get_user_id() + _ = validate_agent_exists(session, agent_id, user_id) + validate_file(session, file_id, user_id) + + # Delete the File DB object + get_file_service().delete_agent_file_by_id(session, agent_id, file_id, user_id, ctx) + + return DeleteAgentFileResponse() diff --git a/western_ai/canada/cohere-toolkit/src/backend/routers/auth.py b/western_ai/canada/cohere-toolkit/src/backend/routers/auth.py new file mode 100644 index 0000000000000000000000000000000000000000..8c9c42773de4fa0e250dd37ea86a1d76ca5f207c --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/routers/auth.py @@ -0,0 +1,348 @@ +import json +from typing import Union +from urllib.parse import quote + +from fastapi import APIRouter, Depends, HTTPException +from fastapi.responses import RedirectResponse +from starlette.requests import Request + +from backend.config.auth import ENABLED_AUTH_STRATEGY_MAPPING +from backend.config.routers import RouterName +from backend.config.settings import Settings +from backend.config.tools import Tool, get_available_tools +from backend.crud import blacklist as blacklist_crud +from backend.database_models import Blacklist +from backend.database_models.database import DBSessionDep +from backend.schemas.auth import JWTResponse, ListAuthStrategy, Login, Logout +from backend.schemas.context import Context +from backend.schemas.params.auth import CodeQueryParam, StrategyPathParam +from backend.schemas.params.tool import ToolIdPathParam +from backend.schemas.tool_auth import DeleteToolAuth +from backend.services.auth.jwt import JWTService +from backend.services.auth.request_validators import validate_authorization +from backend.services.auth.utils import ( + get_or_create_user, + is_enabled_authentication_strategy, +) +from backend.services.cache import cache_get_dict +from backend.services.context import get_context + +router = APIRouter( + prefix="/v1", + tags=[RouterName.AUTH], +) +router.name = RouterName.AUTH + + +@router.get("/auth_strategies", response_model=list[ListAuthStrategy]) +def get_strategies( + ctx: Context = Depends(get_context), +) -> list[ListAuthStrategy]: + """ + Retrieves the currently enabled list of Authentication strategies. + """ + strategies = [] + for strategy_name, strategy_instance in ENABLED_AUTH_STRATEGY_MAPPING.items(): + strategies.append( + { + "strategy": strategy_name, + "client_id": ( + strategy_instance.get_client_id() + if hasattr(strategy_instance, "get_client_id") + else None + ), + "authorization_endpoint": ( + strategy_instance.get_authorization_endpoint() + if hasattr(strategy_instance, "get_authorization_endpoint") + else None + ), + "pkce_enabled": ( + strategy_instance.get_pkce_enabled() + if hasattr(strategy_instance, "get_pkce_enabled") + else False + ), + } + ) + + return strategies + + +@router.post("/login", response_model=Union[JWTResponse, None]) +async def login( + login: Login, session: DBSessionDep, ctx: Context = Depends(get_context) +): + """ + Logs user in, performing basic email/password auth. + Verifies their credentials, retrieves the user and returns a JWT token. + + Raises: + HTTPException: If the strategy or payload are invalid, or if the login fails. + """ + logger = ctx.get_logger() + strategy_name = login.strategy + payload = login.payload + + if not is_enabled_authentication_strategy(strategy_name): + logger.error( + event=f"[Auth] Error logging in: Invalid authentication strategy {strategy_name}", + ) + raise HTTPException( + status_code=422, detail=f"Invalid Authentication strategy: {strategy_name}." + ) + + # Check that the payload required is given + strategy = ENABLED_AUTH_STRATEGY_MAPPING[strategy_name] + strategy_payload = strategy.get_required_payload() + if not set(strategy_payload).issubset(payload.keys()): + missing_keys = [key for key in strategy_payload if key not in payload.keys()] + logger.error( + event=f"[Auth] Error logging in: Keys {missing_keys} missing from payload", + ) + raise HTTPException( + status_code=422, + detail=f"Missing the following keys in the payload: {missing_keys}.", + ) + + user = strategy.login(session, payload) + if not user: + logger.error( + event=f"[Auth] Error logging in: Invalid credentials in payload {payload}", + ) + raise HTTPException( + status_code=401, + detail=f"Error performing {strategy_name} authentication with payload: {payload}.", + ) + + token = JWTService().create_and_encode_jwt(user) + + return {"token": token} + + +@router.post("/{strategy}/auth", response_model=JWTResponse) +async def authorize( + *, + strategy: StrategyPathParam, + request: Request, + code: CodeQueryParam = None, + session: DBSessionDep, + ctx: Context = Depends(get_context), +): + """ + Callback authorization endpoint used for OAuth providers after authenticating on the provider's login screen. + + Raises: + HTTPException: If authentication fails, or strategy is invalid. + """ + logger = ctx.get_logger() + + if not code: + logger.error( + event="[Auth] Error authorizing login: No code provided", + ) + raise HTTPException( + status_code=400, + detail="Error calling /auth with invalid code query parameter.", + ) + + strategy_name = None + for enabled_strategy_name in ENABLED_AUTH_STRATEGY_MAPPING.keys(): + if enabled_strategy_name.lower() == strategy.lower(): + strategy_name = enabled_strategy_name + + if not strategy_name: + logger.error( + event=f"[Auth] Error authorizing login: Invalid strategy {strategy_name}", + ) + raise HTTPException( + status_code=400, + detail=f"Error calling /auth with invalid strategy name: {strategy_name}.", + ) + + if not is_enabled_authentication_strategy(strategy_name): + logger.error( + event=f"[Auth] Error authorizing login: Strategy {strategy_name} not enabled", + ) + raise HTTPException( + status_code=404, detail=f"Invalid Authentication strategy: {strategy_name}." + ) + + strategy = ENABLED_AUTH_STRATEGY_MAPPING[strategy_name] + + try: + userinfo = await strategy.authorize(request) + except Exception as e: + raise HTTPException( + status_code=400, + detail=f"Could not fetch access token from provider, failed with error: {str(e)}", + ) + + if not userinfo: + logger.error( + event="[Auth] Error authorizing login: Invalid token", + ) + raise HTTPException( + status_code=401, detail="Could not get user from auth token." + ) + + # Get or create user, then set session user + user = get_or_create_user(session, userinfo) + + token = JWTService().create_and_encode_jwt(user) + + return {"token": token} + + +@router.get("/logout", response_model=Logout) +async def logout( + session: DBSessionDep, + token: dict | None = Depends(validate_authorization), + ctx: Context = Depends(get_context), +): + """ + Logs out the current user, adding the given JWT token to the blacklist. + """ + if token is not None: + db_blacklist = Blacklist(token_id=token["jti"]) + blacklist_crud.create_blacklist(session, db_blacklist) + + return {} + + +@router.get("/tool/auth") +async def tool_auth( + request: Request, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> RedirectResponse: + """ + Endpoint for Tool Authentication. Note: The flow is different from + the regular login OAuth flow, the backend initiates it and redirects to the frontend + after completion. + + If completed, a ToolAuth is stored in the DB containing the access token for the tool. + + Returns: + RedirectResponse: A redirect pointing to the frontend, contains an error query parameter if + an unexpected error happens during the authentication. + + Raises: + HTTPException: If no redirect_uri set. + """ + logger = ctx.get_logger() + redirect_uri = Settings().get('auth.frontend_hostname') + + if not redirect_uri: + raise HTTPException( + status_code=400, + detail="auth.frontend_hostname in configuration.yaml is required for Tool Auth.", + ) + + def log_and_redirect_err(error_message: str): + logger.error(event=error_message) + redirect_err = f"{redirect_uri}?error={quote(error_message)}" + return RedirectResponse(redirect_err) + + # Get key from state and retrieve cache for user_id and tool_id + try: + state = json.loads(request.query_params.get("state")) + cache_key = state["key"] + tool_auth_cache = cache_get_dict(cache_key) + + # Get optional frontend redirect + if "frontend_redirect" in state: + redirect_uri = state["frontend_redirect"] + except Exception as e: + log_and_redirect_err(str(e)) + + user_id = tool_auth_cache.get("user_id") + tool_id = tool_auth_cache.get("tool_id") + + if not tool_auth_cache: + err = f"Error retrieving cache for Tool Auth with key: {cache_key}" + log_and_redirect_err(err) + + if user_id is None or tool_id is None: + err = f"Tool Auth cache {tool_auth_cache} does not contain user_id or tool_id." + log_and_redirect_err(err) + + available_tools = get_available_tools() + if tool_id in available_tools: + tool = available_tools.get(tool_id) + err = None + + # Tool not found + if not tool: + err = f"Tool {tool_id} does not exist or is not available." + log_and_redirect_err(err) + + # Tool does not have Auth implemented + if tool.auth_implementation is None: + err = f"Tool {tool.name} does not have an auth_implementation required for Tool Auth." + log_and_redirect_err(err) + + try: + tool_auth_service = tool.auth_implementation() + err = tool_auth_service.retrieve_auth_token(request, session, user_id) + except Exception as e: + log_and_redirect_err(str(e)) + + if err: + log_and_redirect_err(err) + + response = RedirectResponse(redirect_uri) + + return response + + +@router.delete("/tool/auth/{tool_id}") +async def delete_tool_auth( + tool_id: ToolIdPathParam, + request: Request, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> DeleteToolAuth: + """ + Endpoint to delete Tool Authentication. + + If completed, the corresponding ToolAuth for the requesting user is removed from the DB. + + Raises: + HTTPException: If there was an error deleting the tool auth. + """ + + logger = ctx.get_logger() + + user_id = ctx.get_user_id() + tool_id = tool_id.lower() + + if user_id is None or user_id == "" or user_id == "default": + logger.error_and_raise_http_exception(event="User ID not found.") + + if tool_id not in [tool_name.value.ID for tool_name in Tool]: + logger.error_and_raise_http_exception( + event="tool_id must be present in the path of the request and must be a member of the Tool string enum class.", + ) + + tool = get_available_tools().get(tool_id) + + if tool is None: + logger.error_and_raise_http_exception( + event=f"Tool {tool_id} is not available." + ) + + if tool.auth_implementation is None: + logger.error_and_raise_http_exception( + event=f"Tool {tool.name} does not have an auth_implementation required for Tool Auth Deletion.", + ) + + try: + tool_auth_service = tool.auth_implementation() + is_deleted = tool_auth_service.delete_tool_auth(session, user_id) + + if not is_deleted: + logger.error_and_raise_http_exception(event="Error deleting Tool Auth.") + + except Exception as e: + logger.error_and_raise_http_exception(event=str(e)) + + return DeleteToolAuth() diff --git a/western_ai/canada/cohere-toolkit/src/backend/routers/chat.py b/western_ai/canada/cohere-toolkit/src/backend/routers/chat.py new file mode 100644 index 0000000000000000000000000000000000000000..ca7f6e737c23dd13eaff178bd162b26954d13f41 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/routers/chat.py @@ -0,0 +1,187 @@ +from typing import Any, Generator + +from fastapi import APIRouter, Depends +from sse_starlette.sse import EventSourceResponse + +from backend.chat.custom.custom import CustomChat +from backend.config.routers import RouterName +from backend.crud import agent_tool_metadata as agent_tool_metadata_crud +from backend.database_models.database import DBSessionDep +from backend.schemas.agent import Agent, AgentToolMetadata +from backend.schemas.chat import ChatResponseEvent, NonStreamedChatResponse +from backend.schemas.cohere_chat import CohereChatRequest +from backend.schemas.context import Context +from backend.services.agent import validate_agent_exists +from backend.services.chat import ( + generate_chat_response, + generate_chat_stream, + process_chat, + process_message_regeneration, +) +from backend.services.context import get_context +from backend.services.request_validators import validate_deployment_header + +router = APIRouter( + prefix="/v1", + tags=[RouterName.CHAT], +) +router.name = RouterName.CHAT + + +@router.post("/chat-stream", dependencies=[Depends(validate_deployment_header)]) +async def chat_stream( + chat_request: CohereChatRequest, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> Generator[ChatResponseEvent, Any, None]: + """ + Stream chat endpoint to handle user messages and return chatbot responses. + """ + ctx.with_model(chat_request.model) + agent_id = chat_request.agent_id + ctx.with_agent_id(agent_id) + + ( + session, + chat_request, + response_message, + should_store, + managed_tools, + next_message_position, + ctx, + ) = process_chat(session, chat_request, ctx) + + return EventSourceResponse( + generate_chat_stream( + session, + CustomChat().chat( + chat_request, + stream=True, + managed_tools=managed_tools, + session=session, + ctx=ctx, + ), + response_message, + should_store=should_store, + next_message_position=next_message_position, + ctx=ctx, + ), + media_type="text/event-stream", + headers={"Connection": "keep-alive"}, + send_timeout=300, + ping=5, + ) + + +@router.post("/chat-stream/regenerate", dependencies=[Depends(validate_deployment_header)]) +async def regenerate_chat_stream( + chat_request: CohereChatRequest, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> EventSourceResponse: + """ + Endpoint to regenerate stream chat response for the last user message. + """ + ctx.with_model(chat_request.model) + + agent_id = chat_request.agent_id + ctx.with_agent_id(agent_id) + + if agent_id: + agent = validate_agent_exists(session, agent_id, ctx.get_user_id()) + ctx.with_agent(Agent.model_validate(agent)) + + agent_tool_metadata = ( + agent_tool_metadata_crud.get_all_agent_tool_metadata_by_agent_id( + session, agent_id + ) + ) + agent_tool_metadata_schema = [ + AgentToolMetadata.model_validate(x) for x in agent_tool_metadata + ] + ctx.with_agent_tool_metadata(agent_tool_metadata_schema) + + ( + session, + chat_request, + new_response_message, + previous_response_message_ids, + managed_tools, + ctx, + ) = process_message_regeneration(session, chat_request, ctx) + + return EventSourceResponse( + generate_chat_stream( + session, + CustomChat().chat( + chat_request, + stream=True, + managed_tools=managed_tools, + session=session, + ctx=ctx, + ), + new_response_message, + next_message_position=new_response_message.position, + previous_response_message_ids=previous_response_message_ids, + ctx=ctx, + ), + media_type="text/event-stream", + headers={"Connection": "keep-alive"}, + send_timeout=300, + ping=5, + ) + + +@router.post("/chat", dependencies=[Depends(validate_deployment_header)]) +async def chat( + chat_request: CohereChatRequest, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> NonStreamedChatResponse: + """ + Chat endpoint to handle user messages and return chatbot responses. + """ + ctx.with_model(chat_request.model) + agent_id = chat_request.agent_id + ctx.with_agent_id(agent_id) + user_id = ctx.get_user_id() + + if agent_id: + agent = validate_agent_exists(session, agent_id, user_id) + agent_schema = Agent.model_validate(agent) + ctx.with_agent(agent_schema) + agent_tool_metadata = ( + agent_tool_metadata_crud.get_all_agent_tool_metadata_by_agent_id( + session, agent_id + ) + ) + agent_tool_metadata_schema = [ + AgentToolMetadata.model_validate(x) for x in agent_tool_metadata + ] + ctx.with_agent_tool_metadata(agent_tool_metadata_schema) + + ( + session, + chat_request, + response_message, + should_store, + managed_tools, + next_message_position, + ctx, + ) = process_chat(session, chat_request, ctx) + + response = await generate_chat_response( + session, + CustomChat().chat( + chat_request, + session=session, + stream=False, + managed_tools=managed_tools, + ctx=ctx, + ), + response_message, + should_store=should_store, + next_message_position=next_message_position, + ctx=ctx, + ) + return response diff --git a/western_ai/canada/cohere-toolkit/src/backend/routers/conversation.py b/western_ai/canada/cohere-toolkit/src/backend/routers/conversation.py new file mode 100644 index 0000000000000000000000000000000000000000..15dab40ea0593226a9acac144a37bfa8ea60e3d1 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/routers/conversation.py @@ -0,0 +1,544 @@ +from fastapi import APIRouter, Depends, Form, HTTPException +from fastapi import File as RequestFile +from fastapi import UploadFile as FastAPIUploadFile +from starlette.responses import Response + +from backend.chat.custom.utils import get_deployment +from backend.config.routers import RouterName +from backend.crud import agent as agent_crud +from backend.crud import conversation as conversation_crud +from backend.crud import message as message_crud +from backend.database_models import Conversation as ConversationModel +from backend.database_models.database import DBSessionDep +from backend.schemas.agent import Agent +from backend.schemas.context import Context +from backend.schemas.conversation import ( + ConversationPublic, + ConversationWithoutMessages, + DeleteConversationResponse, + GenerateTitleResponse, + ToggleConversationPinRequest, + UpdateConversationRequest, +) +from backend.schemas.file import ( + DeleteConversationFileResponse, + FileMetadata, + ListConversationFile, + UploadConversationFileResponse, +) +from backend.schemas.params.agent import AgentIdQueryParam +from backend.schemas.params.conversation import ConversationIdPathParam, QueryQueryParam +from backend.schemas.params.file import FileIdPathParam +from backend.schemas.params.message import MessageIdPathParam +from backend.schemas.params.model import ModelQueryParam +from backend.schemas.params.shared import OrderByQueryParam, PaginationQueryParams +from backend.services.agent import validate_agent_exists +from backend.services.context import get_context +from backend.services.conversation import ( + filter_conversations, + generate_conversation_title, + get_documents_to_rerank, + get_messages_with_files, + validate_conversation, +) +from backend.services.file import ( + attach_conversation_id_to_files, + get_file_service, + validate_file, +) +from backend.services.synthesizer import synthesize + +router = APIRouter( + prefix="/v1/conversations", + tags=[RouterName.CONVERSATION], +) +router.name = RouterName.CONVERSATION + + +# CONVERSATIONS +@router.get("/{conversation_id}", response_model=ConversationPublic) +async def get_conversation( + conversation_id: ConversationIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> ConversationPublic: + """ + Get a conversation by ID. + + Raises: + HTTPException: If the conversation with the given ID is not found. + """ + user_id = ctx.get_user_id() + conversation = conversation_crud.get_conversation( + session, conversation_id, user_id) + + if not conversation: + raise HTTPException( + status_code=404, + detail=f"Conversation with ID: {conversation_id} not found.", + ) + + files = get_file_service().get_files_by_conversation_id( + session, user_id, conversation.id, ctx + ) + files_with_conversation_id = attach_conversation_id_to_files( + conversation.id, files) + messages = get_messages_with_files( + session, user_id, conversation.messages, ctx) + _ = validate_conversation(session, conversation_id, user_id) + + conversation = ConversationPublic( + id=conversation.id, + user_id=user_id, + created_at=conversation.created_at, + updated_at=conversation.updated_at, + title=conversation.title, + messages=messages, + files=files_with_conversation_id, + description=conversation.description, + agent_id=conversation.agent_id, + organization_id=conversation.organization_id, + is_pinned=conversation.is_pinned, + ) + + _ = validate_conversation(session, conversation_id, user_id) + return conversation + + +@router.get("", response_model=list[ConversationWithoutMessages]) +async def list_conversations( + *, + page_params: PaginationQueryParams, + order_by: OrderByQueryParam = None, + agent_id: AgentIdQueryParam = None, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> list[ConversationWithoutMessages]: + """ + List all conversations. + """ + user_id = ctx.get_user_id() + + conversations = conversation_crud.get_conversations( + session, offset=page_params.offset, limit=page_params.limit, order_by=order_by, user_id=user_id, agent_id=agent_id + ) + + results = [] + for conversation in conversations: + files = get_file_service().get_files_by_conversation_id( + session, user_id, conversation.id, ctx + ) + files_with_conversation_id = attach_conversation_id_to_files( + conversation.id, files + ) + results.append( + ConversationWithoutMessages( + id=conversation.id, + user_id=user_id, + created_at=conversation.created_at, + updated_at=conversation.updated_at, + title=conversation.title, + files=files_with_conversation_id, + description=conversation.description, + agent_id=conversation.agent_id, + messages=[], + organization_id=conversation.organization_id, + is_pinned=conversation.is_pinned, + ) + ) + + return results + + +@router.put("/{conversation_id}", response_model=ConversationPublic) +async def update_conversation( + conversation_id: ConversationIdPathParam, + new_conversation: UpdateConversationRequest, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> ConversationPublic: + """ + Update a conversation by ID. + + Raises: + HTTPException: If the conversation with the given ID is not found. + """ + user_id = ctx.get_user_id() + conversation = validate_conversation(session, conversation_id, user_id) + conversation = conversation_crud.update_conversation( + session, conversation, new_conversation + ) + + files = get_file_service().get_files_by_conversation_id( + session, user_id, conversation.id, ctx + ) + messages = get_messages_with_files( + session, user_id, conversation.messages, ctx) + files_with_conversation_id = attach_conversation_id_to_files( + conversation.id, files) + return ConversationPublic( + id=conversation.id, + user_id=user_id, + created_at=conversation.created_at, + updated_at=conversation.updated_at, + title=conversation.title, + messages=messages, + files=files_with_conversation_id, + description=conversation.description, + agent_id=conversation.agent_id, + organization_id=conversation.organization_id, + is_pinned=conversation.is_pinned, + ) + + +@router.put("/{conversation_id}/toggle-pin", response_model=ConversationWithoutMessages) +async def toggle_conversation_pin( + conversation_id: ConversationIdPathParam, + new_conversation_pin: ToggleConversationPinRequest, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> ConversationWithoutMessages: + """ + Toggle whether a conversation is pinned or not + """ + user_id = ctx.get_user_id() + conversation = validate_conversation(session, conversation_id, user_id) + conversation = conversation_crud.toggle_conversation_pin( + session, conversation, new_conversation_pin + ) + files = get_file_service().get_files_by_conversation_id( + session, user_id, conversation.id, ctx + ) + files_with_conversation_id = attach_conversation_id_to_files( + conversation.id, files + ) + return ConversationWithoutMessages( + id=conversation.id, + user_id=user_id, + created_at=conversation.created_at, + updated_at=conversation.updated_at, + title=conversation.title, + files=files_with_conversation_id, + description=conversation.description, + agent_id=conversation.agent_id, + messages=[], + organization_id=conversation.organization_id, + is_pinned=conversation.is_pinned, + ) + + +@router.delete("/{conversation_id}") +async def delete_conversation( + conversation_id: ConversationIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> DeleteConversationResponse: + """ + Delete a conversation by ID. + + Raises: + HTTPException: If the conversation with the given ID is not found. + """ + user_id = ctx.get_user_id() + conversation = validate_conversation(session, conversation_id, user_id) + + get_file_service().delete_all_conversation_files( + session, conversation.id, conversation.file_ids, user_id, ctx + ) + conversation_crud.delete_conversation(session, conversation_id, user_id) + + return DeleteConversationResponse() + + +@router.get(":search", response_model=list[ConversationWithoutMessages]) +async def search_conversations( + *, + query: QueryQueryParam, + page_params: PaginationQueryParams, + order_by: OrderByQueryParam = None, + agent_id: AgentIdQueryParam = None, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> list[ConversationWithoutMessages]: + """ + Search conversations by title. + """ + user_id = ctx.get_user_id() + deployment_name = ctx.get_deployment_name() + model_deployment = get_deployment(deployment_name, session, ctx) + + agent = None + if agent_id: + agent = validate_agent_exists(session, agent_id, user_id) + + if agent_id: + agent_schema = Agent.model_validate(agent) + ctx.with_agent(agent_schema) + + conversations = conversation_crud.get_conversations( + session, offset=page_params.offset, limit=page_params.limit, order_by=order_by, user_id=user_id, agent_id=agent_id + ) + + if not conversations: + return [] + + rerank_documents = get_documents_to_rerank(conversations) + filtered_documents = await filter_conversations( + query, + conversations, + rerank_documents, + model_deployment, + ctx, + ) + + results = [] + for conversation in filtered_documents: + files = get_file_service().get_files_by_conversation_id( + session, user_id, conversation.id, ctx + ) + files_with_conversation_id = attach_conversation_id_to_files( + conversation.id, files + ) + results.append( + ConversationWithoutMessages( + id=conversation.id, + user_id=user_id, + created_at=conversation.created_at, + updated_at=conversation.updated_at, + title=conversation.title, + files=files_with_conversation_id, + description=conversation.description, + agent_id=conversation.agent_id, + messages=[], + organization_id=conversation.organization_id, + is_pinned=conversation.is_pinned, + ) + ) + return results + + +# FILES +@router.post("/batch_upload_file", response_model=list[UploadConversationFileResponse]) +async def batch_upload_file( + *, + conversation_id: str = Form(None), + files: list[FastAPIUploadFile] = RequestFile(...), + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> UploadConversationFileResponse: + """ + Uploads and creates a batch of File object. + If no conversation_id is provided, a new Conversation is created as well. + + Raises: + HTTPException: If the conversation with the given ID is not found. Status code 404. + HTTPException: If the file wasn't uploaded correctly. Status code 500. + """ + + user_id = ctx.get_user_id() + + # Create new conversation + if not conversation_id: + conversation = conversation_crud.create_conversation( + session, + ConversationModel(user_id=user_id), + ) + # Check for existing conversation + else: + conversation = conversation_crud.get_conversation( + session, conversation_id, user_id + ) + + # Fail if user_id is not provided when conversation DNE + if not conversation: + if not user_id: + raise HTTPException( + status_code=400, + detail="user_id is required if no valid conversation is provided.", + ) + + # Create new conversation + conversation = conversation_crud.create_conversation( + session, + ConversationModel(user_id=user_id), + ) + + # TODO: check if file already exists in DB once we have files per agents + + try: + uploaded_files = await get_file_service().create_conversation_files( + session, + files, + user_id, + conversation.id, + ctx, + ) + except Exception as e: + raise HTTPException( + status_code=500, detail=f"Error while uploading file(s): {e}." + ) + + files_with_conversation_id = attach_conversation_id_to_files( + conversation.id, uploaded_files + ) + return files_with_conversation_id + + +@router.get("/{conversation_id}/files", response_model=list[ListConversationFile]) +async def list_files( + conversation_id: ConversationIdPathParam, + session: DBSessionDep, ctx: + Context = Depends(get_context), +) -> list[ListConversationFile]: + """ + List all files from a conversation. Important - no pagination support yet. + + Raises: + HTTPException: If the conversation with the given ID is not found. + """ + user_id = ctx.get_user_id() + _ = validate_conversation(session, conversation_id, user_id) + + files = get_file_service().get_files_by_conversation_id( + session, user_id, conversation_id, ctx + ) + files_with_conversation_id = attach_conversation_id_to_files( + conversation_id, files) + return files_with_conversation_id + + +@router.get("/{conversation_id}/files/{file_id}", response_model=FileMetadata) +async def get_file( + conversation_id: ConversationIdPathParam, + file_id: FileIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> FileMetadata: + """ + Get a conversation file by ID. + + Raises: + HTTPException: If the conversation or file with the given ID is not found, or if the file does not belong to the conversation. + """ + user_id = ctx.get_user_id() + + conversation = validate_conversation(session, conversation_id, user_id) + + if file_id not in conversation.file_ids: + raise HTTPException( + status_code=404, + detail=f"File with ID: {file_id} does not belong to the conversation with ID: {conversation.id}." + ) + + file = validate_file(session, file_id, user_id) + + return FileMetadata( + id=file.id, + file_name=file.file_name, + file_content=file.file_content, + file_size=file.file_size, + created_at=file.created_at, + updated_at=file.updated_at, + ) + + +@router.delete("/{conversation_id}/files/{file_id}") +async def delete_file( + conversation_id: ConversationIdPathParam, + file_id: FileIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> DeleteConversationFileResponse: + """ + Delete a file by ID. + + Raises: + HTTPException: If the conversation with the given ID is not found. + """ + user_id = ctx.get_user_id() + _ = validate_conversation(session, conversation_id, user_id) + validate_file(session, file_id, user_id) + # Delete the File DB object + get_file_service().delete_conversation_file_by_id( + session, conversation_id, file_id, user_id, ctx + ) + + return DeleteConversationFileResponse() + + +# MISC +@router.post("/{conversation_id}/generate-title", response_model=GenerateTitleResponse) +async def generate_title( + *, + conversation_id: ConversationIdPathParam, + model: ModelQueryParam = "command-r", + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> GenerateTitleResponse: + """ + Generate a title for a conversation and update the conversation with the generated title. + + Raises: + HTTPException: If the conversation with the given ID is not found. + """ + user_id = ctx.get_user_id() + ctx.with_deployment_config() + ctx.with_model(model) + + conversation = validate_conversation(session, conversation_id, user_id) + agent_id = conversation.agent_id if conversation.agent_id else None + + if agent_id: + agent = agent_crud.get_agent_by_id(session, agent_id, user_id) + agent_schema = Agent.model_validate(agent) + ctx.with_agent(agent_schema) + + title, error = await generate_conversation_title( + session, + conversation, + agent_id, + ctx, + model, + ) + + conversation_crud.update_conversation( + session, conversation, UpdateConversationRequest(title=title) + ) + + return GenerateTitleResponse( + title=title, + error=error, + ) + + +# SYNTHESIZE +@router.get("/{conversation_id}/synthesize/{message_id}") +async def synthesize_message( + conversation_id: ConversationIdPathParam, + message_id: MessageIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> Response: + """ + Generate a synthesized audio for a specific message in a conversation. + + Raises: + HTTPException: If the message with the given ID is not found or synthesis fails. + """ + user_id = ctx.get_user_id() + message = message_crud.get_conversation_message(session, conversation_id, message_id, user_id) + + if not message: + raise HTTPException( + status_code=404, + detail=f"Message with ID: {message_id} not found.", + ) + + try: + synthesized_audio = synthesize(message.text) + except Exception as e: + raise HTTPException( + status_code=500, detail=f"Error while message synthesis: {e}" + ) + + return Response(synthesized_audio, media_type="audio/mp3") diff --git a/western_ai/canada/cohere-toolkit/src/backend/routers/deployment.py b/western_ai/canada/cohere-toolkit/src/backend/routers/deployment.py new file mode 100644 index 0000000000000000000000000000000000000000..162ea9d28557a50a95e701c352737442381b65cc --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/routers/deployment.py @@ -0,0 +1,164 @@ +from fastapi import APIRouter, Depends, HTTPException + +from backend.config.routers import RouterName +from backend.crud import deployment as deployment_crud +from backend.database_models.database import DBSessionDep +from backend.exceptions import DeploymentNotFoundError +from backend.schemas.context import Context +from backend.schemas.deployment import ( + DeleteDeployment, + DeploymentCreate, + DeploymentDefinition, + DeploymentUpdate, + UpdateDeploymentEnv, +) +from backend.schemas.params.deployment import ( + AllQueryParam, + DeploymentIdPathParam, +) +from backend.services import deployment as deployment_service +from backend.services.context import get_context +from backend.services.logger.utils import LoggerFactory +from backend.services.request_validators import ( + validate_create_deployment_request, + validate_env_vars, +) + +logger = LoggerFactory().get_logger() + +router = APIRouter( + prefix="/v1/deployments", + tags=[RouterName.DEPLOYMENT], +) +router.name = RouterName.DEPLOYMENT + + +@router.post( + "", + response_model=DeploymentDefinition, + dependencies=[Depends(validate_create_deployment_request)], +) +def create_deployment( + deployment: DeploymentCreate, + session: DBSessionDep, +) -> DeploymentDefinition: + """ + Create a new deployment. + """ + try: + created = DeploymentDefinition.from_db_deployment( + deployment_crud.create_deployment(session, deployment) + ) + except Exception as e: + raise HTTPException(status_code=400, detail=str(e)) + + return mask_deployment_secrets(created) + + +@router.put("/{deployment_id}", response_model=DeploymentDefinition) +def update_deployment( + deployment_id: DeploymentIdPathParam, + new_deployment: DeploymentUpdate, + session: DBSessionDep, +) -> DeploymentDefinition: + """ + Update a deployment. + + Raises: + HTTPException: If deployment not found. + """ + deployment = deployment_crud.get_deployment(session, deployment_id) + if not deployment: + raise DeploymentNotFoundError(deployment_id=deployment_id) + + return mask_deployment_secrets(DeploymentDefinition.from_db_deployment( + deployment_crud.update_deployment(session, deployment, new_deployment) + )) + + +@router.get("/{deployment_id}", response_model=DeploymentDefinition) +def get_deployment( + deployment_id: DeploymentIdPathParam, + session: DBSessionDep, +) -> DeploymentDefinition: + """ + Get a deployment by ID. + """ + return mask_deployment_secrets( + deployment_service.get_deployment_definition(session, deployment_id) + ) + + +@router.get("", response_model=list[DeploymentDefinition]) +def list_deployments( + *, + all: AllQueryParam = False, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> list[DeploymentDefinition]: + """ + List all available deployments and their models. + """ + logger = ctx.get_logger() + + installed_deployments = deployment_service.get_deployment_definitions(session) + available_deployments = [ + mask_deployment_secrets(deployment) for deployment in installed_deployments if deployment.is_available or all + ] + + if not available_deployments: + logger.warning( + event="[Deployment] No deployments available to list.", + ) + raise HTTPException( + status_code=404, + detail=( + "No available deployments found. Please ensure that the required variables in your configuration.yaml " + "and secrets.yaml are set up correctly. " + "Refer to the README.md for detailed instructions.", + ), + ) + + return available_deployments + + +@router.delete("/{deployment_id}") +async def delete_deployment( + deployment_id: DeploymentIdPathParam, + session: DBSessionDep, +) -> DeleteDeployment: + """ + Delete a deployment by ID. + + Raises: + HTTPException: If the deployment with the given ID is not found. + """ + deployment = deployment_crud.get_deployment(session, deployment_id) + + if not deployment: + raise DeploymentNotFoundError(deployment_id=deployment_id) + + deployment_crud.delete_deployment(session, deployment_id) + + return DeleteDeployment() + + +@router.post("/{deployment_id}/update_config", response_model=DeploymentDefinition) +async def update_config( + *, + deployment_id: DeploymentIdPathParam, + env_vars: UpdateDeploymentEnv, + valid_env_vars = Depends(validate_env_vars), + session: DBSessionDep, +) -> DeploymentDefinition: + """ + Set environment variables for the deployment. + """ + return mask_deployment_secrets( + deployment_service.update_config(session, deployment_id, valid_env_vars) + ) + + +def mask_deployment_secrets(deployment: DeploymentDefinition) -> DeploymentDefinition: + deployment.config = {key: "*****" if val else "" for [key, val] in deployment.config.items()} + return deployment diff --git a/western_ai/canada/cohere-toolkit/src/backend/routers/experimental_features.py b/western_ai/canada/cohere-toolkit/src/backend/routers/experimental_features.py new file mode 100644 index 0000000000000000000000000000000000000000..9170c5d0d9f066e5ffd26a212aba7c064bc3bfb9 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/routers/experimental_features.py @@ -0,0 +1,25 @@ +from fastapi import APIRouter, Depends + +from backend.config.routers import RouterName +from backend.config.settings import Settings +from backend.schemas.context import Context +from backend.services.context import get_context + +router = APIRouter( + prefix="/v1/experimental_features", + tags=[RouterName.EXPERIMENTAL_FEATURES], +) + +router.name = RouterName.EXPERIMENTAL_FEATURES + + +@router.get("/") +def list_experimental_features(ctx: Context = Depends(get_context)) -> dict[str, bool]: + """ + List all experimental features and if they are enabled + """ + experimental_features = { + "USE_AGENTS_VIEW": Settings().get('feature_flags.use_agents_view'), + "USE_TEXT_TO_SPEECH_SYNTHESIS": bool(Settings().get('google_cloud.api_key')), + } + return experimental_features diff --git a/western_ai/canada/cohere-toolkit/src/backend/routers/model.py b/western_ai/canada/cohere-toolkit/src/backend/routers/model.py new file mode 100644 index 0000000000000000000000000000000000000000..053437810b9ea68eb0bb611413b36ee9a0f257c1 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/routers/model.py @@ -0,0 +1,108 @@ +from fastapi import APIRouter, Depends, HTTPException + +from backend.config.routers import RouterName +from backend.crud import model as model_crud +from backend.database_models import Model +from backend.database_models.database import DBSessionDep +from backend.schemas.model import DeleteModel, ModelCreate, ModelUpdate +from backend.schemas.model import Model as ModelSchema +from backend.schemas.params.model import ModelIdPathParam +from backend.schemas.params.shared import PaginationQueryParams +from backend.services.request_validators import validate_create_update_model_request + +router = APIRouter( + prefix="/v1/models", + tags=[RouterName.MODEL], +) + +router.name = RouterName.MODEL + + +@router.post( + "", + response_model=ModelSchema, + dependencies=[ + Depends(validate_create_update_model_request), + ], +) +def create_model( + model: ModelCreate, + session: DBSessionDep +) -> Model: + """ + Create a new model. + """ + return model_crud.create_model(session, model) + + +@router.put( + "/{model_id}", + response_model=ModelSchema, + dependencies=[ + Depends(validate_create_update_model_request), + ], +) +def update_model( + model_id: ModelIdPathParam, + new_model: ModelUpdate, + session: DBSessionDep, +) -> ModelSchema: + """ + Update a model by ID. + + Raises: + HTTPException: If the model with the given ID is not found. + """ + model = model_crud.get_model(session, model_id) + if not model: + raise HTTPException(status_code=404, detail="Model not found") + + return model_crud.update_model(session, model, new_model) + + +@router.get("/{model_id}", response_model=ModelSchema) +def get_model( + model_id: ModelIdPathParam, + session: DBSessionDep, +) -> ModelSchema: + """ + Get a model by ID. + """ + model = model_crud.get_model(session, model_id) + if not model: + raise HTTPException(status_code=404, detail="Model not found") + return model + + +@router.get("", response_model=list[ModelSchema]) +def list_models( + page_params: PaginationQueryParams, + session: DBSessionDep, +) -> list[ModelSchema]: + """ + List all available models + """ + return model_crud.get_models(session, page_params.offset, page_params.limit) + + +@router.delete("/{model_id}") +async def delete_model( + model_id: ModelIdPathParam, + session: DBSessionDep, +) -> DeleteModel: + """ + Delete a model by ID. + + Raises: + HTTPException: If the model with the given ID is not found. + """ + model = model_crud.get_model(session, model_id) + + if not model: + raise HTTPException( + status_code=404, detail=f"Model with ID: {model_id} not found." + ) + + model_crud.delete_model(session, model_id) + + return DeleteModel() diff --git a/western_ai/canada/cohere-toolkit/src/backend/routers/organization.py b/western_ai/canada/cohere-toolkit/src/backend/routers/organization.py new file mode 100644 index 0000000000000000000000000000000000000000..6967964a1e84ad3620d311768ecf7925603613b7 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/routers/organization.py @@ -0,0 +1,125 @@ +from fastapi import APIRouter, Depends, HTTPException + +from backend.config.routers import RouterName +from backend.crud import organization as organization_crud +from backend.database_models.database import DBSessionDep +from backend.database_models.organization import Organization as OrganizationModel +from backend.schemas import ( + CreateOrganization, + DeleteOrganization, + Organization, + UpdateOrganization, +) +from backend.schemas.context import Context +from backend.schemas.params.organization import OrganizationIdPathParam +from backend.schemas.user import User +from backend.services.context import get_context +from backend.services.request_validators import validate_organization_request + +router = APIRouter( + prefix="/v1/organizations", + tags=[RouterName.ORGANIZATION], +) +router.name = RouterName.ORGANIZATION + + +@router.post( + "", + response_model=Organization, + dependencies=[ + Depends(validate_organization_request), + ], +) +def create_organization( + organization: CreateOrganization, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> Organization: + """ + Create a new organization. + """ + organization_data = OrganizationModel(**organization.model_dump()) + + return organization_crud.create_organization(session, organization_data) + + +@router.put( + "/{org_id}", + response_model=Organization, + dependencies=[ + Depends(validate_organization_request), + ], +) +def update_organization( + org_id: OrganizationIdPathParam, + new_organization: UpdateOrganization, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> Organization: + """ + Update organization by ID. + """ + organization = organization_crud.get_organization(session, org_id) + return organization_crud.update_organization( + session, organization, new_organization + ) + + +@router.get("/{org_id}", response_model=Organization) +def get_organization( + org_id: OrganizationIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> Organization: + """ + Get a organization by ID. + """ + organization = organization_crud.get_organization(session, org_id) + if not organization: + raise HTTPException(status_code=404, detail="Organization not found") + return organization + + +@router.delete("/{org_id}", response_model=DeleteOrganization) +def delete_organization( + org_id: OrganizationIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> DeleteOrganization: + """ + Delete a organization by ID. + """ + organization = organization_crud.get_organization(session, org_id) + if not organization: + raise HTTPException(status_code=404, detail="Organization not found") + organization_crud.delete_organization(session, org_id) + + return DeleteOrganization() + + +@router.get("", response_model=list[Organization]) +def list_organizations( + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> list[Organization]: + """ + List all available organizations. + """ + all_organizations = organization_crud.get_organizations(session) + return all_organizations + + +@router.get("/{org_id}/users", response_model=list[User]) +def get_organization_users( + org_id: OrganizationIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> list[User]: + """ + Get organization users by ID. + """ + organization = organization_crud.get_organization(session, org_id) + if not organization: + raise HTTPException(status_code=404, detail="Organization not found") + + return organization.users diff --git a/western_ai/canada/cohere-toolkit/src/backend/routers/scim.py b/western_ai/canada/cohere-toolkit/src/backend/routers/scim.py new file mode 100644 index 0000000000000000000000000000000000000000..fdf2997e0cf207baa6106c9dd688349d72ca2eda --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/routers/scim.py @@ -0,0 +1,346 @@ +from fastapi import APIRouter, Depends +from starlette.requests import Request +from starlette.responses import JSONResponse, Response + +import backend.crud.group as group_crud +import backend.crud.user as user_crud +from backend.config import Settings +from backend.config.routers import RouterName +from backend.database_models import DBSessionDep, UserGroupAssociation +from backend.database_models import Group as DBGroup +from backend.database_models import User as DBUser +from backend.schemas.context import Context +from backend.schemas.params.scim import ( + GroupIdPathParam, + ScimPaginationQueryParams, + UserIdPathParam, +) +from backend.schemas.scim import ( + CreateGroup, + CreateUser, + Group, + ListGroupResponse, + ListUserResponse, + PatchGroup, + PatchUser, + UpdateUser, + User, +) +from backend.services.context import get_context + +scim_auth = Settings().get('auth.scim') +router = APIRouter( + prefix="/scim/v2", + tags=[RouterName.SCIM] +) +router.name = RouterName.SCIM + + +class SCIMException(Exception): + def __init__(self, status_code: int, detail: str): + self.status_code = status_code + self.detail = detail + + +async def scim_exception_handler(request: Request, exc: SCIMException) -> Response: + return JSONResponse( + status_code=exc.status_code, + content={ + "detail": exc.detail, + "schemas": ["urn:ietf:params:scim:api:messages:2.0:Error"], + }, + ) + + +@router.get("/Users") +async def get_users( + page_params: ScimPaginationQueryParams, + session: DBSessionDep, +) -> ListUserResponse: + """ + Return users + """ + if page_params.filter: + try: + display_name = _parse_filter(page_params.filter) + except ValueError: + raise SCIMException(status_code=400, detail="filter not supported") + db_user = user_crud.get_user_by_user_name(session, display_name) + if not db_user: + return ListUserResponse( + total_results=0, + start_index=page_params.start_index, + items_per_page=page_params.count, + resources=[], + ) + return ListUserResponse( + total_results=1, + start_index=page_params.start_index, + items_per_page=page_params.count, + resources=[User.from_db_user(db_user)], + ) + + db_users = user_crud.get_external_users( + session, offset=page_params.start_index - 1, limit=page_params.count + ) + users = [User.from_db_user(db_user) for db_user in db_users] + return ListUserResponse( + total_results=len(users), + start_index=page_params.start_index, + items_per_page=page_params.count, + resources=users, + ) + + +@router.get("/Users/{user_id}") +async def get_user( + user_id: UserIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +): + """ + Get user by User ID + """ + logger = ctx.get_logger() + db_user = user_crud.get_user(session, user_id) + if not db_user: + logger.error(event="[SCIM] user not found", user_id=user_id) + raise SCIMException(status_code=404, detail="User not found") + + return User.from_db_user(db_user) + + +@router.post("/Users", status_code=201) +async def create_user( + user: CreateUser, + session: DBSessionDep, +): + """ + Create a new user + """ + db_user = user_crud.get_user_by_external_id(session, user.externalId) + if not db_user: + db_user = DBUser() + + db_user.user_name = user.user_name + db_user.fullname = f"{user.name.given_name} {user.name.family_name}" + db_user.email = _get_email_from_scim_user(user) + db_user.active = user.active + db_user.external_id = user.externalId + + db_user = user_crud.create_user(session, db_user) + return User.from_db_user(db_user) + + +@router.put("/Users/{user_id}") +async def update_user( + user_id: UserIdPathParam, + user: UpdateUser, + session: DBSessionDep, + ctx: Context = Depends(get_context), +): + """ + Update a user + """ + logger = ctx.get_logger() + db_user = user_crud.get_user(session, user_id) + if not db_user: + logger.error(event="[SCIM] user not found", user_id=user_id) + raise SCIMException(status_code=404, detail="User not found") + + db_user.user_name = user.user_name + db_user.fullname = f"{user.name.given_name} {user.name.family_name}" + db_user.active = user.active + email_update = _get_email_from_scim_user(user) + db_user.email = email_update + + db_user = user_crud.create_user(session, db_user) + + return User.from_db_user(db_user) + + +@router.patch("/Users/{user_id}") +async def patch_user( + user_id: UserIdPathParam, + patch: PatchUser, + session: DBSessionDep, + ctx: Context = Depends(get_context), +): + """ + Patch a user + """ + logger = ctx.get_logger() + db_user = user_crud.get_user(session, user_id) + if not db_user: + logger.error(event="[SCIM] user not found", user_id=user_id) + raise SCIMException(status_code=404, detail="User not found") + + for operation in patch.Operations: + k, v = list(operation.value.items())[0] + setattr(db_user, k, v) + + db_user = user_crud.create_user(session, db_user) + + return User.from_db_user(db_user) + + +@router.get("/Groups") +async def get_groups( + *, + page_params: ScimPaginationQueryParams, + session: DBSessionDep, +) -> ListGroupResponse: + """ + Return Groups + """ + if page_params.filter: + try: + display_name = _parse_filter(page_params.filter) + except ValueError: + raise SCIMException(status_code=400, detail="filter not supported") + db_group = group_crud.get_group_by_name(session, display_name) + if not db_group: + return ListGroupResponse( + total_results=0, + start_index=page_params.start_index, + items_per_page=page_params.count, + resources=[], + ) + return ListGroupResponse( + total_results=1, + start_index=page_params.start_index, + items_per_page=page_params.count, + resources=[Group.from_db_group(db_group)], + ) + + db_groups = group_crud.get_groups(session, offset=page_params.start_index - 1, limit=page_params.count) + groups = [Group.from_db_group(db_group) for db_group in db_groups] + return ListGroupResponse( + total_results=len(groups), + start_index=page_params.start_index, + items_per_page=page_params.count, + resources=groups, + ) + + +@router.get("/Groups/{group_id}") +async def get_group( + group_id: GroupIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +): + """ + Get group by group ID + """ + logger = ctx.get_logger() + db_group = group_crud.get_group(session, group_id) + if not db_group: + logger.error(event="[SCIM] group not found", group_id=group_id) + raise SCIMException(status_code=404, detail="Group not found") + + return Group.from_db_group(db_group) + + +@router.post("/Groups", status_code=201) +async def create_group( + group: CreateGroup, + session: DBSessionDep, +): + """ + Create a group + """ + db_group = group_crud.get_group_by_name(session, group.display_name) + if not db_group: + db_group = DBGroup() + + db_group.display_name = group.display_name + + db_group = group_crud.create_group(session, db_group) + return Group.from_db_group(db_group) + + +@router.patch("/Groups/{group_id}") +async def patch_group( + group_id: GroupIdPathParam, + patch: PatchGroup, + session: DBSessionDep, + ctx: Context = Depends(get_context), +): + """ + Patch a group + """ + logger = ctx.get_logger() + db_group = group_crud.get_group(session, group_id) + if not db_group: + logger.error(event="[SCIM] group not found", group_id=group_id) + raise SCIMException(status_code=404, detail="Group not found") + + for operation in patch.Operations: + if operation.op == "replace" and "displayName" in operation.value: + db_group.display_name = operation.value["displayName"] + db_group = group_crud.update_group(session, db_group) + if operation.op == "add" and operation.path == "members": + associations = [] + for value in operation.value: + association = UserGroupAssociation( + user_id=value["value"], + group_id=db_group.id, + display=value["display"], + ) + associations.append(association) + db_group = group_crud.add_users(session, db_group, associations) + if operation.op == "replace" and operation.path == "members": + associations = [] + for value in operation.value: + association = UserGroupAssociation( + user_id=value["value"], + group_id=db_group.id, + display=value["display"], + ) + associations.append(association) + db_group = group_crud.set_users(session, db_group, associations) + + return Group.from_db_group(db_group) + + +@router.delete("/Groups/{group_id}", status_code=204) +async def delete_group( + group_id: GroupIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +): + """ + Delete a group + """ + logger = ctx.get_logger() + db_group = group_crud.get_group(session, group_id) + if not db_group: + logger.error(event="[SCIM] group not found", group_id=group_id) + raise SCIMException(status_code=404, detail="Group not found") + group_crud.delete_group(session, group_id) + + +def _get_email_from_scim_user(user: CreateUser | UpdateUser) -> str | None: + """ + Return the primary email or a SCIM user + """ + for email_obj in user.emails: + if email_obj.primary: + return email_obj.value + return None + + +def _parse_filter(filter: str) -> str: + """ + Parse a filter like `displayName eq "test"` into a value. Raises a ValueError if the filter is not valid. + """ + try: + filter_column, operator, value = filter.split(" ", maxsplit=2) + if filter_column not in ["displayName", "userName"]: + raise ValueError(f"filter not supported: {filter}") + if operator != "eq": + raise ValueError(f"filter not supported: {filter}") + filter_value = value.strip('"') + return filter_value + except Exception as e: + raise ValueError(f"Invalid filter: {filter}") from e diff --git a/western_ai/canada/cohere-toolkit/src/backend/routers/snapshot.py b/western_ai/canada/cohere-toolkit/src/backend/routers/snapshot.py new file mode 100644 index 0000000000000000000000000000000000000000..e4551c614139ca86cae1b620483318a0e8146fc1 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/routers/snapshot.py @@ -0,0 +1,164 @@ +from fastapi import APIRouter, Depends, HTTPException + +from backend.chat.collate import to_dict +from backend.config.routers import RouterName +from backend.crud import snapshot as snapshot_crud +from backend.database_models.database import DBSessionDep +from backend.schemas.context import Context +from backend.schemas.params.snapshot import LinkIdPathParam, SnapshotIdPathParam +from backend.schemas.snapshot import ( + CreateSnapshotRequest, + CreateSnapshotResponse, + DeleteSnapshotLinkResponse, + DeleteSnapshotResponse, + Snapshot, + SnapshotPublic, + SnapshotWithLinks, +) +from backend.services.context import get_context +from backend.services.conversation import validate_conversation +from backend.services.snapshot import ( + validate_last_message, + validate_snapshot_exists, + validate_snapshot_link, + wrap_create_snapshot, + wrap_create_snapshot_access, + wrap_create_snapshot_link, +) + +router = APIRouter( + prefix="/v1/snapshots", + tags=[RouterName.SNAPSHOT], +) +router.name = RouterName.SNAPSHOT + +PRIVATE_KEYS = ["organization_id", "user_id", "conversation_id"] + + +@router.post("", response_model=CreateSnapshotResponse) +async def create_snapshot( + snapshot_request: CreateSnapshotRequest, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> CreateSnapshotResponse: + """ + Create a new snapshot and snapshot link to share the conversation. + """ + user_id = ctx.get_user_id() + conversation_id = snapshot_request.conversation_id + + # Check if conversation exists, if it has messages and if a snapshot already exists + conversation = validate_conversation(session, conversation_id, user_id) + + last_message_id = conversation.messages[-1].id if conversation.messages else None + validate_last_message(last_message_id) + + snapshot = snapshot_crud.get_snapshot_by_last_message_id(session, last_message_id) + + if not snapshot: + snapshot = wrap_create_snapshot( + session, last_message_id, user_id, conversation, ctx + ) + + snapshot_link = wrap_create_snapshot_link(session, snapshot.id, user_id) + + return CreateSnapshotResponse( + link_id=snapshot_link.id, + snapshot_id=snapshot.id, + user_id=user_id, + messages=snapshot.snapshot.get("messages", []), + ) + + +@router.get("", response_model=list[SnapshotWithLinks]) +async def list_snapshots( + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> list[SnapshotWithLinks]: + """ + List all snapshots. + """ + user_id = ctx.get_user_id() + + snapshots = snapshot_crud.list_snapshots(session, user_id) + + # Get snapshot links for each snapshot + response = [] + for snapshot in snapshots: + snapshot_links = snapshot_crud.list_snapshot_links(session, snapshot.id) + snapshot_links = [link.id for link in snapshot_links] + snapshot_dict = to_dict(Snapshot.model_validate(snapshot)) + snapshot_with_links = SnapshotWithLinks.model_validate( + snapshot_dict | {"links": snapshot_links} + ) + response.append(snapshot_with_links) + + return response + + +@router.get("/link/{link_id}", response_model=SnapshotPublic) +async def get_snapshot( + link_id: LinkIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> SnapshotPublic: + """ + Get a snapshot by link ID. + """ + user_id = ctx.get_user_id() + + snapshot = validate_snapshot_link(session, link_id) + + wrap_create_snapshot_access(session, snapshot.id, user_id, link_id, ctx) + + return snapshot + + +@router.delete("/link/{link_id}") +async def delete_snapshot_link( + link_id: LinkIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> DeleteSnapshotLinkResponse: + """ + Delete a snapshot link by ID. + """ + user_id = ctx.get_user_id() + + snapshot = validate_snapshot_link(session, link_id) + + # Anyone can get the snapshot link, but only the user that created it can delete it + if user_id != snapshot.user_id: + raise HTTPException( + status_code=403, + detail="User does not have permission to delete this snapshot link", + ) + + snapshot_crud.delete_snapshot_link(session, link_id, user_id) + + return DeleteSnapshotLinkResponse() + + +@router.delete("/{snapshot_id}") +async def delete_snapshot( + snapshot_id: SnapshotIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> DeleteSnapshotResponse: + """ + Delete a snapshot by ID. + """ + user_id = ctx.get_user_id() + + snapshot = validate_snapshot_exists(session, snapshot_id) + + # Anyone can get the snapshot, but only the user that created it can delete it + if user_id != snapshot.user_id: + raise HTTPException( + status_code=403, + detail="User does not have permission to delete this snapshot", + ) + + snapshot_crud.delete_snapshot(session, snapshot_id, user_id) + + return DeleteSnapshotResponse() diff --git a/western_ai/canada/cohere-toolkit/src/backend/routers/tool.py b/western_ai/canada/cohere-toolkit/src/backend/routers/tool.py new file mode 100644 index 0000000000000000000000000000000000000000..86f7e1656de65b811234ae66ecba3fe5d9f6e242 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/routers/tool.py @@ -0,0 +1,67 @@ +from fastapi import APIRouter, Depends + +from backend.config.routers import RouterName +from backend.config.tools import get_available_tools +from backend.database_models.database import DBSessionDep +from backend.schemas.context import Context +from backend.schemas.params.agent import AgentIdQueryParam +from backend.schemas.tool import ToolDefinition +from backend.services.agent import validate_agent_exists +from backend.services.context import get_context + +router = APIRouter( + prefix="/v1/tools", + tags=[RouterName.TOOL], +) +router.name = RouterName.TOOL + + +@router.get("", response_model=list[ToolDefinition]) +def list_tools( + *, + agent_id: AgentIdQueryParam = None, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> list[ToolDefinition]: + """ + List all available tools. + """ + user_id = ctx.get_user_id() + logger = ctx.get_logger() + + available_tools = get_available_tools() + all_tools = list(available_tools.values()) + + if agent_id is not None: + agent_tools = [] + agent = validate_agent_exists(session, agent_id, user_id) + + for tool in agent.tools: + agent_tools.append(available_tools[tool]) + all_tools = agent_tools + + for tool in all_tools: + # Tools with auth implementation can be enabled and visible but not accessible (e.g., if secrets are not set). + # Therefore, we need to set is_auth_required for these types of tools as well for the frontend. + if (tool.is_available or tool.is_visible) and tool.auth_implementation is not None: + try: + tool_auth_service = tool.auth_implementation() + + tool.is_auth_required = tool_auth_service.is_auth_required( + session, user_id + ) + tool.auth_url = tool_auth_service.get_auth_url(user_id) + + # Return access token to client when required by frontend + # e.g: to enable Google Drive picker in client + if tool.should_return_token: + tool.token = tool_auth_service.get_token(session, user_id) + except Exception as e: + logger.error(event=f"Error while fetching Tool Auth: {str(e)}") + tool.is_auth_required = True + tool.is_available = False + tool.error_message = ( + f"Error while calling Tool Auth implementation {str(e)}" + ) + + return all_tools diff --git a/western_ai/canada/cohere-toolkit/src/backend/routers/user.py b/western_ai/canada/cohere-toolkit/src/backend/routers/user.py new file mode 100644 index 0000000000000000000000000000000000000000..c1261810326952ee8d18c0782a744a9fc86ece0d --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/routers/user.py @@ -0,0 +1,144 @@ +from fastapi import APIRouter, Depends, HTTPException + +from backend.config.auth import is_authentication_enabled +from backend.config.routers import RouterName +from backend.crud import user as user_crud +from backend.database_models import User as UserModel +from backend.database_models.database import DBSessionDep +from backend.schemas.context import Context +from backend.schemas.params.shared import PaginationQueryParams +from backend.schemas.params.user import UserIdPathParam +from backend.schemas.user import CreateUser, DeleteUser, UpdateUser, User +from backend.schemas.user import User as UserSchema +from backend.services.auth.request_validators import validate_authorization +from backend.services.context import get_context + +router = APIRouter( + prefix="/v1/users", + tags=[RouterName.USER], +) +router.name = RouterName.USER + +auth_dependencies = [Depends(validate_authorization)] if is_authentication_enabled() else [] + + +@router.post("", response_model=User) +async def create_user( + user: CreateUser, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> User: + """ + Create a new user. + """ + db_user = UserModel(**user.model_dump(exclude_none=True)) + db_user = user_crud.create_user(session, db_user) + + user_schema = UserSchema.model_validate(db_user) + ctx.with_user(user=user_schema) + + return db_user + + +@router.get( + "", + response_model=list[User], + dependencies=auth_dependencies, +) +async def list_users( + page_params: PaginationQueryParams, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> list[User]: + """ + List all users. + """ + return user_crud.get_users(session, offset=page_params.offset, limit=page_params.limit) + + +@router.get( + "/{user_id}", + response_model=User, + dependencies=auth_dependencies, +) +async def get_user( + user_id: UserIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> User: + """ + Get a user by ID. + + Raises: + HTTPException: If the user with the given ID is not found. + """ + + user = user_crud.get_user(session, user_id) + if not user: + raise HTTPException( + status_code=404, detail=f"User with ID: {user_id} not found." + ) + + user_schema = UserSchema.model_validate(user) + ctx.with_user(user=user_schema) + return user + + +@router.put( + "/{user_id}", + response_model=User, + dependencies=auth_dependencies, +) +async def update_user( + user_id: UserIdPathParam, + new_user: UpdateUser, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> User: + """ + Update a user by ID. + + Raises: + HTTPException: If the user with the given ID is not found. + """ + user = user_crud.get_user(session, user_id) + + if not user: + raise HTTPException( + status_code=404, detail=f"User with ID: {user_id} not found." + ) + + user = user_crud.update_user(session, user, new_user) + user_schema = UserSchema.model_validate(user) + ctx.with_user(user=user_schema) + + return user + + +@router.delete( + "/{user_id}", + dependencies=auth_dependencies, +) +async def delete_user( + user_id: UserIdPathParam, + session: DBSessionDep, + ctx: Context = Depends(get_context), +) -> DeleteUser: + """ + Delete a user by ID. + + Raises: + HTTPException: If the user with the given ID is not found. + """ + user = user_crud.get_user(session, user_id) + + if not user: + raise HTTPException( + status_code=404, detail=f"User with ID: {user_id} not found." + ) + + user_schema = UserSchema.model_validate(user) + ctx.with_user(user=user_schema) + user_crud.delete_user(session, user_id) + + return DeleteUser() diff --git a/western_ai/canada/cohere-toolkit/src/backend/routers/utils.py b/western_ai/canada/cohere-toolkit/src/backend/routers/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..e836ae9ae2861e41e02a4fe8cb20747a77b30b24 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/routers/utils.py @@ -0,0 +1,57 @@ +import backend.services.deployment as deployment_service +from backend.database_models.database import DBSessionDep +from backend.exceptions import DeploymentNotFoundError +from backend.model_deployments.cohere_platform import CohereDeployment +from backend.schemas.agent import Agent +from backend.services.logger.utils import LoggerFactory + + +def get_deployment_for_agent(session: DBSessionDep, deployment, model) -> tuple[CohereDeployment, str | None]: + try: + deployment = deployment_service.get_deployment_instance_by_name(session, deployment) + except DeploymentNotFoundError: + deployment = deployment_service.get_default_deployment_instance() + + model = next((m for m in deployment.models() if m.name == model), None) + + return deployment, model + +def get_deployment_model_from_agent(agent: Agent, session: DBSessionDep): + from backend.crud import deployment as deployment_crud + logger = LoggerFactory().get_logger() + logger.debug(event="get_deployment_model_from_agent", agent=agent.model_dump()) + + model_db = None + deployment_db = None + if agent.deployment: + deployment_db = deployment_crud.get_deployment_by_name(session, agent.deployment) + if not deployment_db: + deployment_db = deployment_crud.get_deployment(session, agent.deployment) + logger.debug(event="deployment models:", deployment_id=deployment_db.id, models=list(d.name for d in deployment_db.models)) + if deployment_db: + model_db = next( + ( + model + for model in deployment_db.models + if model.name == agent.model or model.id == agent.model + ), + None, + ) + return deployment_db, model_db + + +def get_default_deployment_model(session: DBSessionDep): + from backend.crud import deployment as deployment_crud + + deployment_db = deployment_crud.get_deployment_by_name(session, CohereDeployment.name()) + model_db = None + if deployment_db: + model_db = next( + ( + model + for model in deployment_db.models + if model.name == 'command-r-plus' + ), + None, + ) + return deployment_db, model_db diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..cc77f12ba722e7e6a2f8c60eab66c96446bbc01a --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/__init__.py @@ -0,0 +1,4 @@ +# ruff: noqa +from backend.schemas.cohere_chat import * +from backend.schemas.message import * +from backend.schemas.organization import * diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/agent.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/agent.py new file mode 100644 index 0000000000000000000000000000000000000000..7a50416ef7dffa47f8f6d9f1b7af2b08588f766a --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/agent.py @@ -0,0 +1,421 @@ +import datetime +from abc import ABC +from enum import StrEnum +from typing import Optional + +from pydantic import BaseModel, Field + + +class AgentVisibility(StrEnum): + """ + Supported values for Agent Visibility + """ + PRIVATE = "private" + PUBLIC = "public" + ALL = "all" + + +class AgentBase(ABC, BaseModel): + """ + Abstract base class for Agent Schemas + """ + user_id: str = Field( + ..., + title="User ID", + description="User ID for the Agent", + ) + organization_id: Optional[str] = Field( + None, + title="Organization ID", + description="Organization ID for the Agent", + ) + + +class AgentToolMetadata(BaseModel): + """ + Agent tool metadata schema + """ + id: str = Field( + ..., + title="ID", + description="Agent tool metadata ID", + ) + created_at: datetime.datetime = Field( + ..., + title="Created At Timestamp", + description="When the agent tool metadata was created", + ) + updated_at: datetime.datetime = Field( + ..., + title="Updated At Timestamp", + description="When the agent tool metadata was updated", + ) + + user_id: Optional[str] = Field( + None, + title="User ID", + description="User ID for the agent tool metadata", + ) + agent_id: str = Field( + ..., + title="Agent ID", + description="Agent ID for the agent tool metadata", + ) + tool_name: str = Field( + ..., + title="Tool Name", + description="Tool Name for the agent tool metadata", + ) + artifacts: list[dict] = Field( + ..., + title="Artifacts", + description="Artifacts for the agent tool metadata", + ) + + class Config: + from_attributes = True + use_enum_values = True + + +class AgentToolMetadataPublic(AgentToolMetadata): + """ + Public agent tool metadata schema + """ + user_id: Optional[str] = Field( + None, + title="User ID", + description="User ID for the agent tool metadata", + exclude=True, + ) + + class Config: + from_attributes = True + + +class AgentToolMedatadataBaseRequest(ABC, BaseModel): + """ + Abstract class for creating/updating Agent Tool Metadata + """ + id: Optional[str] = Field( + None, + title="ID", + description="Agent Tool Metadata ID", + ) + + +class CreateAgentToolMetadataRequest(AgentToolMedatadataBaseRequest): + """ + Request to create Agent Tool Metadata + """ + tool_name: str = Field( + ..., + title="Tool Name", + description="Tool Name for the agent tool metadata", + ) + artifacts: list[dict] = Field( + ..., + title="Artifacts", + description="Artifacts for the agent tool metadata", + ) + + +class UpdateAgentToolMetadataRequest(AgentToolMedatadataBaseRequest): + """ + Request to update Agent Tool Metadata + """ + tool_name: Optional[str] = Field( + None, + title="Tool Name", + description="Tool Name for the agent tool metadata", + ) + artifacts: Optional[list[dict]] = Field( + None, + title="Artifacts", + description="Artifacts for the agent tool metadata", + ) + + +class DeleteAgentToolMetadata(BaseModel): + """ + Delete agent tool metadata response + """ + pass + + +# Agent +class Agent(AgentBase): + """ + Agent schema + """ + id: str = Field( + ..., + title="ID", + description="Agent ID", + ) + created_at: datetime.datetime = Field( + ..., + title="Created At Timestamp", + description="When the agent was created", + ) + updated_at: datetime.datetime = Field( + ..., + title="Updated At Timestamp", + description="When the agent was updated", + ) + + name: str = Field( + ..., + title="Name", + description="Name of the Agent", + ) + version: int = Field( + ..., + title="Version", + description="Version of the Agent", + ) + description: Optional[str] = Field( + None, + title="Description", + description="Agent Description", + ) + preamble: Optional[str] = Field( + None, + title="Preamble", + description="The preamble for the Agent", + ) + temperature: float = Field( + ..., + title="Temperature", + description="The temperature for the Agent", + ) + tools: Optional[list[str]] = Field( + None, + title="Tools", + description="List of tools for the Agent", + ) + tools_metadata: list[AgentToolMetadataPublic] = Field( + ..., + title="Tools Metadata", + description="List of tool metadata for the Agent", + ) + deployment: Optional[str] = Field( + None, + title="Deployment", + description="Deployment for the Agent", + ) + model: Optional[str] = Field( + None, + title="Model", + description="Model for the Agent", + ) + is_private: Optional[bool] = Field( + None, + title="Is Private", + description="If the Agent is private", + ) + + class Config: + from_attributes = True + use_enum_values = True + + +class AgentPublic(Agent): + """ + Public agent schema + """ + organization_id: Optional[str] = Field( + None, + title="Organization ID", + description="Organization ID for the Agent", + exclude=True, + ) + tools_metadata: Optional[list[AgentToolMetadataPublic]] = Field( + None, + title="Tools Metadata", + description="List of tool metadata for the Agent", + ) + + +class CreateAgentRequest(BaseModel): + """ + Schema to create an agent + """ + name: str = Field( + ..., + title="Name", + description="Name of the Agent", + ) + version: Optional[int] = Field( + None, + title="Version", + description="Version of the Agent", + ) + description: Optional[str] = Field( + None, + title="Description", + description="Agent Description", + ) + preamble: Optional[str] = Field( + None, + title="Preamble", + description="The preamble for the Agent", + ) + temperature: Optional[float] = Field( + None, + title="Temperature", + description="The temperature for the Agent", + ) + tools: Optional[list[str]] = Field( + None, + title="Tools", + description="List of tools for the Agent", + ) + tools_metadata: Optional[list[CreateAgentToolMetadataRequest]] = Field( + None, + title="Tools Metadata", + description="Tools metadata for the Agent", + ) + # deployment_id or deployment_name + deployment: str = Field( + ..., + title="Deployment", + description="Deployment for the Agent", + ) + deployment_config: Optional[dict[str, str]] = Field( + None, + title="Deployment Config", + description="Deployment config for the Agent", + ) + # model_id or model_name + model: str = Field( + ..., + title="Model", + description="Model for the Agent", + ) + organization_id: Optional[str] = Field( + None, + title="Organization ID", + description="Organization ID for the Agent", + ) + is_private: Optional[bool] = Field( + False, + title="Is Private", + description="If the Agent is private", + ) + + class Config: + from_attributes = True + use_enum_values = True + + +class ListAgentsResponse(BaseModel): + """ + Response schema for listing agents + """ + agents: list[Agent] = Field( + ..., + title="Agents", + description="List of Agents", + ) + + +class UpdateAgentNoDeploymentModel(ABC, BaseModel): + """ + Abstract class for updating agents + """ + name: Optional[str] = Field( + None, + title="Name", + description="Name of the Agent", + ) + version: Optional[int] = Field( + None, + title="Version", + description="Version of the Agent", + ) + description: Optional[str] = Field( + None, + title="Description", + description="Agent Description", + ) + preamble: Optional[str] = Field( + None, + title="Preamble", + description="The preamble for the Agent", + ) + temperature: Optional[float] = Field( + None, + title="Temperature", + description="The temperature for the Agent", + ) + tools: Optional[list[str]] = Field( + None, + title="Tools", + description="List of tools for the Agent", + ) + organization_id: Optional[str] = Field( + None, + title="Organization ID", + description="Organization ID for the Agent", + ) + is_private: Optional[bool] = Field( + None, + title="Is Private", + description="If the Agent is private", + ) + + class Config: + from_attributes = True + use_enum_values = True + +class UpdateAgentDB(UpdateAgentNoDeploymentModel): + """ + Model for updating agents + """ + model_id: Optional[str] = Field( + None, + title="Model ID", + description="Model ID", + ) + deployment_id: Optional[str] = Field( + None, + title="Deployment ID", + description="Deployment ID", + ) + + model_config = { + "from_attributes": True, + "use_enum_values": True, + "protected_namespaces": (), + } + + +class UpdateAgentRequest(UpdateAgentNoDeploymentModel): + """ + Schema to update an agent + """ + deployment: Optional[str] = Field( + None, + title="Deployment", + description="Deployment for the Agent", + ) + model: Optional[str] = Field( + None, + title="Model", + description="Model for the Agent", + ) + tools_metadata: Optional[list[CreateAgentToolMetadataRequest]] = Field( + None, + title="Tools Metadata", + description="Tools metadata for the Agent", + ) + class Config: + from_attributes = True + use_enum_values = True + + +class DeleteAgent(BaseModel): + """ + Response for deleting an agent + """ + pass diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/auth.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/auth.py new file mode 100644 index 0000000000000000000000000000000000000000..36faf6bdadf1f3204362352495f87e624e235c9b --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/auth.py @@ -0,0 +1,70 @@ +from abc import ABC +from typing import Optional + +from pydantic import BaseModel, Field + + +class Auth(ABC, BaseModel): + """ + Abstract class for Auth Schemas + """ + strategy: str = Field( + ..., + title="Strategy", + description="Auth strategy to use", + ) + + +class Login(Auth): + """ + Login Request + """ + payload: Optional[dict[str, str]] = Field( + None, + title="Payload", + description="Login payload depending on strategy used", + ) + + +class Logout(BaseModel): + """ + Logout Request + """ + pass + + +class ListAuthStrategy(BaseModel): + """ + List Auth Strategy + """ + strategy: str = Field( + ..., + title="Strategy", + description="Auth strategy name", + ) + client_id: Optional[str] = Field( + None, + title="Client ID", + description="Client ID to be used", + ) + authorization_endpoint: Optional[str] = Field( + None, + title="Authorization Endpoint", + description="The endpoint for authorization", + ) + pkce_enabled: bool = Field( + ..., + title="PKCE Enabled", + description="If PKCE is enabled", + ) + + +class JWTResponse(BaseModel): + """ + JWT Response + """ + token: str = Field( + ..., + title="Token", + description="JSON Web Token", + ) diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/chat.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/chat.py new file mode 100644 index 0000000000000000000000000000000000000000..b3f3f337877ad8a6455f199af5e355739c48f7af --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/chat.py @@ -0,0 +1,496 @@ +from abc import ABC +from dataclasses import dataclass +from enum import StrEnum +from typing import Any, ClassVar, Optional, Union +from uuid import uuid4 + +from pydantic import BaseModel, Field + +from backend.chat.enums import StreamEvent +from backend.schemas.citation import Citation +from backend.schemas.document import Document +from backend.schemas.search_query import SearchQuery +from backend.schemas.tool import Tool, ToolCall, ToolCallDelta + + +@dataclass +class EventState: + distances_plans: list + distances_actions: list + previous_plan: str + previous_action: str + + +class ChatRole(StrEnum): + """ + One of CHATBOT|USER|SYSTEM to identify who the message is coming from. + """ + CHATBOT = "CHATBOT" + USER = "USER" + SYSTEM = "SYSTEM" + TOOL = "TOOL" + + +class ChatCitationQuality(StrEnum): + """ + Dictates the approach taken to generating citations by allowing the user to specify + whether they want "accurate" results or "fast" results. Defaults to "accurate". + """ + FAST = "FAST" + ACCURATE = "ACCURATE" + + +class ToolInputType(StrEnum): + """ + Type of input passed to the tool + """ + QUERY = "QUERY" + CODE = "CODE" + + +class ChatMessage(BaseModel): + """ + A list of previous messages between the user and the model, meant to give the mode + conversational context for responding to the user's message. + """ + role: ChatRole = Field( + ..., + title="Role", + description="One of CHATBOT|USER|SYSTEM to identify who the message is coming from.", + ) + message: Optional[str] = Field( + None, + title="Message", + description="Contents of the chat message.", + ) + tool_plan: Optional[str] = Field( + None, + title="Tool Plan", + description="Contents of the tool plan.", + ) + tool_results: Optional[list[dict[str, Any]]] = Field( + None, + title="Tool Results", + description="Results from the tool call.", + ) + tool_calls: Optional[list[dict[str, Any]]] = Field( + None, + title="Tool Calls", + description="List of tool calls generated for custom tools", + ) + + def to_dict(self) -> dict[str, Any]: + return { + "role": self.role, + "message": self.message, + "tool_results": self.tool_results, + "tool_calls": self.tool_calls, + } + + +# TODO: fix titles of these types +class ChatResponse(ABC, BaseModel): + """ + Abstract class for Chat Responses + """ + event_type: ClassVar[StreamEvent] = Field( + ..., + title="Event Type", + description="Chat response event type", + ) + + +class StreamStart(ChatResponse): + """Stream start event""" + event_type: ClassVar[StreamEvent] = StreamEvent.STREAM_START + generation_id: Optional[str] = Field( + None, + title="Generation ID", + description="Generation ID for the event", + ) + conversation_id: Optional[str] = Field( + None, + title="Conversation ID", + description="Conversation ID for the event", + ) + + +class StreamTextGeneration(ChatResponse): + """Stream text generation event""" + event_type: ClassVar[StreamEvent] = StreamEvent.TEXT_GENERATION + text: str = Field( + ..., + title="Text", + description="Contents of the chat message", + ) + + +class StreamCitationGeneration(ChatResponse): + """Stream citation generation event""" + event_type: ClassVar[StreamEvent] = StreamEvent.CITATION_GENERATION + citations: list[Citation] = Field( + [], + title="Citations", + description="Citations for the chat message", + ) + + +class StreamQueryGeneration(ChatResponse): + """Stream query generation event""" + event_type: ClassVar[StreamEvent] = StreamEvent.SEARCH_QUERIES_GENERATION + query: str = Field( + ..., + title="Query", + description="Search query used to generate grounded response with citations", + ) + + +class StreamSearchResults(ChatResponse): + """Stream search generation event""" + event_type: ClassVar[StreamEvent] = StreamEvent.SEARCH_RESULTS + search_results: list[dict[str, Any]] = Field( + [], + title="Search Results", + description="Search results used to generate grounded response with citations", + ) + documents: list[Document] = Field( + [], + title="Documents", + description="Documents used to generate grounded response with citations", + ) + + +class StreamToolInput(ChatResponse): + """Stream tool input generation event""" + event_type: ClassVar[StreamEvent] = StreamEvent.TOOL_INPUT + input_type: ToolInputType = Field( + ..., + title="Input Type", + description="Tool input type", + ) + tool_name: str = Field( + ..., + title="Tool Name", + description="Name of the tool to be used", + ) + input: str = Field( + ..., + title="Input", + description="Tool input", + ) + text: str = Field( + ..., + title="Text", + description="Contents of the chat message", + ) + + +class StreamToolResult(ChatResponse): + """Stream tool result generation event""" + event_type: ClassVar[StreamEvent] = StreamEvent.TOOL_RESULT + result: Any = Field( + ..., + title="Result", + description="Result from the tool", + ) + tool_name: str = Field( + ..., + title="Tool Name", + description="Name of tool that generated the result", + ) + documents: list[Document] = Field( + [], + title="Documents", + description="Documents used to generate grounded response with citations", + ) + + +class StreamSearchQueriesGeneration(ChatResponse): + """Stream queries generation event""" + event_type: ClassVar[StreamEvent] = StreamEvent.SEARCH_QUERIES_GENERATION + search_queries: list[SearchQuery] = Field( + [], + title="Search Queries", + description="Search query used to generate grounded response with citations", + ) + + +class StreamToolCallsGeneration(ChatResponse): + """Stream tool calls generation event""" + event_type: ClassVar[StreamEvent] = StreamEvent.TOOL_CALLS_GENERATION + stream_search_results: Optional[StreamSearchResults] = Field( + None, + title="Stream Search Results", + description="Search results used to generate grounded response with citations", + ) + tool_calls: Optional[list[ToolCall]] = Field( + [], + title="Tool Calls", + description="List of tool calls generated for custom tools", + ) + text: str | None = Field( + None, + title="Text", + description="Contents of the chat message", + ) + + +class StreamEnd(ChatResponse): + """Stream end generation event""" + event_type: ClassVar[StreamEvent] = StreamEvent.STREAM_END + message_id: Optional[str] = Field( + None, + title="Message ID", + description="Unique identifier for the message", + ) + response_id: Optional[str] = Field( + None, + title="Response ID", + description="Unique identifier for the response", + ) + generation_id: Optional[str] = Field( + None, + title="Generation ID", + description="Unique identifier for the generation", + ) + conversation_id: Optional[str] = Field( + None, + title="Conversation ID", + description="Unique identifier for the conversation", + ) + text: str = Field( + ..., + title="Text", + description="Contents of the chat message", + ) + citations: list[Citation] = Field( + [], + title="Citations", + description="Citations for the chat messae.", + ) + documents: list[Document] = Field( + [], + title="Documents", + description="Documents used to generate grounded response with citations", + ) + search_results: list[dict[str, Any]] = Field( + [], + title="Search Results", + description="Search results used to generate grounded response with citations", + ) + search_queries: list[SearchQuery] = Field( + [], + title="Search Queries", + description="List of generated search queries", + ) + tool_calls: list[ToolCall] = Field( + [], + title="Tool Calls", + description="List of tool calls generated for custom tools", + ) + finish_reason: Optional[str] = Field( + None, + title="Finish Reason", + description="Reson why the model finished the request", + ) + chat_history: Optional[list[ChatMessage]] = Field( + None, + title="Chat History", + description="A list of entries used to construct the conversation. If provided, these messages will be used to build the prompt and the conversation_id will be ignored so no data will be stored to maintain state.", + ) + error: Optional[str] = Field( + None, + title="Error", + description="Error message if the response is an error", + ) + + +class NonStreamedChatResponse(ChatResponse): + """Non streamed chat response""" + response_id: Optional[str] = Field( + None, + title="Response ID", + description="Unique identifier for the response", + ) + generation_id: Optional[str] = Field( + None, + title="Generation ID", + description="Unique identifier for the generation", + ) + chat_history: Optional[list[ChatMessage]] = Field( + None, + title="Chat History", + description="A list of previous messages between the user and the model, meant to give the model conversational context for responding to the user's message.", + ) + finish_reason: str = Field( + ..., + title="Finish Reason", + description="Reason the chat stream ended", + ) + text: str = Field( + ..., + title="Text", + description="Contents of the chat message", + ) + citations: list[Citation] | None = Field( + [], + title="Citations", + description="Citations for the chat message", + ) + documents: list[Document] | None = Field( + [], + title="Documents", + description="Documents used to generate grounded response with citations", + ) + search_results: list[dict[str, Any]] | None = Field( + [], + title="Search Results", + description="Search results used to generate grounded response with citations", + ) + search_queries: list[SearchQuery] | None = Field( + [], + title="Search Queries", + description="List of generated search queries.", + ) + conversation_id: Optional[str] = Field( + None, + title="Conversation ID", + description="To store a conversation then create a conversation id and use it for every related request", + ) + tool_calls: list[ToolCall] | None = Field( + [], + title="Tool Calls", + description="List of tool calls generated for custom tools", + ) + error: Optional[str] = Field( + None, + title="Error", + description="Error message if the response is an error", + ) + + +class StreamToolCallsChunk(ChatResponse): + """Stream tool call chunk generated event""" + event_type: ClassVar[StreamEvent] = StreamEvent.TOOL_CALLS_CHUNK + tool_call_delta: Optional[ToolCallDelta] = Field( + ToolCallDelta( + name=None, + index=None, + parameters=None, + ), + title="Tool Call Delta", + description="Partial tool call", + ) + text: Optional[str] = Field( + None, + title="Text", + description="Contents of the chat message", + ) + + +StreamEventType = Union[ + StreamStart, + StreamTextGeneration, + StreamCitationGeneration, + StreamQueryGeneration, + StreamSearchResults, + StreamEnd, + StreamToolInput, + StreamToolResult, + StreamSearchQueriesGeneration, + StreamToolCallsGeneration, + StreamToolCallsChunk, + NonStreamedChatResponse, +] + + +class ChatResponseEvent(BaseModel): + """ + Chat Response Event + """ + event: StreamEvent = Field( + ..., + title="Event", + description="Type of stream event", + ) + data: StreamEventType = Field( + ..., + title="Data", + description="Data returned from chat response of a given event type", + ) + + +class BaseChatRequest(BaseModel): + """ + Base Chat Request + """ + message: str = Field( + ..., + title="Message", + description="The message to send to the chatbot", + ) + chat_history: list[ChatMessage] | None = Field( + None, + title="Chat History", + description="A list of entries used to construct the conversation. If provided, these messages will be used to build the prompt and the conversation_id will be ignored so no data will be stored to maintain state.", + ) + conversation_id: str = Field( + default_factory=lambda: str(uuid4()), + title="Conversation ID", + description="To store a conversation then create a conversation id and use it for every related request", + ) + tools: Optional[list[Tool]] = Field( + default_factory=list, + title="Tools", + description=""" + List of custom or managed tools to use for the response. + If passing in managed tools, you only need to provide the name of the tool. + If passing in custom tools, you need to provide the name, description, and optionally parameter defintions of the tool. + Passing a mix of custom and managed tools is not supported. + + Managed Tools Examples: + tools=[ + { + "name": "Wiki Retriever - LangChain", + }, + { + "name": "Calculator", + } + ] + + Custom Tools Examples: + tools=[ + { + "name": "movie_title_generator", + "description": "tool to generate a cool movie title", + "parameter_definitions": { + "synopsis": { + "description": "short synopsis of the movie", + "type": "str", + "required": true + } + } + }, + { + "name": "random_number_generator", + "description": "tool to generate a random number between min and max", + "parameter_definitions": { + "min": { + "description": "minimum number", + "type": "int", + "required": true + }, + "max": { + "description": "maximum number", + "type": "int", + "required": true + } + } + }, + { + "name": "joke_generator", + "description": "tool to generate a random joke", + } + ] + """, + ) diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/citation.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/citation.py new file mode 100644 index 0000000000000000000000000000000000000000..eb6aca04f67c9eb5f2104a6485911200da053574 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/citation.py @@ -0,0 +1,30 @@ +from pydantic import BaseModel, Field + + +class Citation(BaseModel): + """ + Schema for a citation + """ + text: str = Field( + ..., + title="Text", + description="Citation text", + ) + start: int = Field( + ..., + title="Start", + description="Start position for the citation", + ) + end: int = Field( + ..., + title="End", + description="End position for the citation", + ) + document_ids: list[str] = Field( + ..., + title="Document IDs", + description="Documents used for the citation", + ) + + class Config: + from_attributes = True diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/cohere_chat.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/cohere_chat.py new file mode 100644 index 0000000000000000000000000000000000000000..5eabe6b400db902443718060591ab0b851ec94b5 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/cohere_chat.py @@ -0,0 +1,130 @@ +from enum import StrEnum +from typing import Any, Optional + +from pydantic import Field + +from backend.schemas.chat import BaseChatRequest + + +class CohereChatPromptTruncation(StrEnum): + """Dictates how the prompt will be constructed. Defaults to "AUTO_PRESERVE_ORDER".""" + OFF = "OFF" + AUTO_PRESERVE_ORDER = "AUTO_PRESERVE_ORDER" + + +class CohereChatRequest(BaseChatRequest): + """ + Request shape for Cohere Python SDK Streamed Chat. + See: https://github.com/cohere-ai/cohere-python/blob/main/src/cohere/base_client.py#L1629 + """ + documents: list[dict[str, Any]] = Field( + default_factory=list, + title="Documents", + description="""Documents to use to generate grounded response with citations. Example: + documents=[ + { + "id": "national_geographic_everest", + "title": "Height of Mount Everest", + "text": "The height of Mount Everest is 29,035 feet", + "url": "https://education.nationalgeographic.org/resource/mount-everest/", + }, + { + "id": "national_geographic_mariana", + "title": "Depth of the Mariana Trench", + "text": "The depth of the Mariana Trench is 36,070 feet", + "url": "https://www.nationalgeographic.org/activity/mariana-trench-deepest-place-earth", + }, + ] + """, + ) + model: Optional[str] = Field( + "command-r-plus", + title="Model", + description="The model to use for generating the response.", + ) + temperature: Optional[float] = Field( + None, + title="Temperature", + description="A non-negative float that tunes the degree of randomness in generation. Lower temperatures mean less random generations, and higher temperatures mean more random generations.", + ge=0, + ) + k: Optional[int] = Field( + None, + title="Top-K", + description="Ensures only the top k most likely tokens are considered for generation at each step.", + le=500, + ge=0, + ) + p: Optional[float] = Field( + None, + title="Top-P", + description="Ensures that only the most likely tokens, with total probability mass of p, are considered for generation at each step. If both k and p are enabled, p acts after k.", + le=0.99, + ge=0, + ) + preamble: Optional[str] = Field( + None, + title="Preamble", + description="A string to override the preamble.", + ) + file_ids: Optional[list[str]] = Field( + None, + title="File IDs", + description="List of File IDs for PDFs used in RAG for the response.", + exclude=True, + ) + search_queries_only: Optional[bool] = Field( + False, + title="Search Queries Only", + description="When set to true a list of search queries are generated. No search will occur nor replies to the user's message.", + ) + max_tokens: Optional[int] = Field( + None, + title="Max Tokens", + description="The maximum number of tokens the model will generate as part of the response. Note: Setting a low value may result in incomplete generations.", + ge=1, + ) + seed: Optional[float] = Field( + None, + title="Seed", + description="If specified, the backend will make a best effort to sample tokens deterministically, such that repeated requests with the same seed and parameters should return the same result. However, determinism cannot be totally guaranteed.", + ) + stop_sequences: Optional[list[str]] = Field( + None, + title="Stop Sequences", + description="A list of up to 5 strings that the model will use to stop generation. If the model generates a string that matches any of the strings in the list, it will stop generating tokens and return the generated text up to that point not including the stop sequence.", + ) + presence_penalty: Optional[float] = Field( + None, + title="Presence Penalty", + description="Used to reduce repetitiveness of generated tokens. Similar to frequency_penalty, except that this penalty is applied equally to all tokens that have already appeared, regardless of their exact frequencies.", + ge=0, + le=1, + ) + frequency_penalty: Optional[float] = Field( + None, + title="Frequency Penalty", + description="Used to reduce repetitiveness of generated tokens. The higher the value, the stronger a penalty is applied to previously present tokens, proportional to how many times they have already appeared in the prompt or prior generation.", + ge=0, + le=1, + ) + prompt_truncation: Optional[CohereChatPromptTruncation] = Field( + CohereChatPromptTruncation.AUTO_PRESERVE_ORDER, + title="Prompt Truncation", + description="Dictates how the prompt will be constructed. Defaults to 'AUTO_PRESERVE_ORDER'.", + ) + tool_results: Optional[list[dict[str, Any]]] = Field( + None, + title="Tool Results", + description="A list of results from invoking tools recommended by the model in the previous chat turn. Results are used to produce a text response and will be referenced in citations.", + ) + force_single_step: Optional[bool] = Field( + None, + title="Force Single Step", + description="If set to true, the model will generate a single response in a single step. This is useful for generating a response to a single message.", + ) + agent_id: Optional[str] = Field( + None, + title="Agent ID", + description="The agent ID to use for the chat.", + ) diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/context.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/context.py new file mode 100644 index 0000000000000000000000000000000000000000..ae425f3187ccb02617eccd69a0a812587ed49aa5 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/context.py @@ -0,0 +1,195 @@ +from typing import Any, Optional, Self + +from pydantic import BaseModel + +from backend.crud import organization as organization_crud +from backend.crud import user as user_crud +from backend.database_models.database import DBSessionDep +from backend.schemas import Organization +from backend.schemas.agent import Agent, AgentToolMetadata +from backend.schemas.user import User +from backend.services.logger.utils import LoggerFactory +from backend.services.utils import get_deployment_config + + +class Context(BaseModel): + """ + Context for a request + """ + request: Optional[dict] = {} + response: Optional[dict] = {} + receive: Optional[dict] = {} + trace_id: str = "default" + user_id: str = "default" + user: Optional[User] = None + agent: Optional[Agent] = None + agent_tool_metadata: Optional[AgentToolMetadata] = None + model: Optional[str] = None + deployment_name: Optional[str] = None + deployment_config: Optional[dict] = None + conversation_id: Optional[str] = None + agent_id: Optional[str] = None + stream_start_ms: Optional[float] = None + logger: Optional[Any] = None + organization_id: Optional[str] = None + organization: Optional[Organization] = None + use_global_filtering: Optional[bool] = False + + def __init__(self): + super().__init__() + self.with_logger() + + def set_request(self, request): + self.request = request + + def set_response(self, response): + self.response = response + + def set_receive(self, receive): + self.receive = receive + + def with_logger(self): + if self.logger is not None: + return self + + logger = LoggerFactory().get_logger() + logger.bind(trace_id=self.trace_id, user_id=self.user_id) + self.logger = logger + return self + + def with_trace_id(self, trace_id: str): + self.trace_id = trace_id + + def with_user_id(self, user_id: str): + self.user_id = user_id + + def with_deployment_name(self, deployment_name: str): + self.deployment_name = deployment_name + + def with_user( + self, session: DBSessionDep | None = None, user: User | None = None + ) -> Self: + if not user and not session: + return self + + if not user: + user = user_crud.get_user(session, self.user_id) + user = User.model_validate(user) + + if user: + self.user = user + + return self + + def with_agent(self, agent: Agent | None) -> Self: + self.agent = agent + return self + + def with_agent_tool_metadata( + self, agent_tool_metadata: AgentToolMetadata + ) -> Self: + self.agent_tool_metadata = agent_tool_metadata + return self + + def with_model(self, model: str) -> Self: + self.model = model + return self + + def with_deployment_config(self, deployment_config={}) -> Self: + if deployment_config: + self.deployment_config = deployment_config + else: + self.deployment_config = get_deployment_config(self.request) + return self + + def with_conversation_id(self, conversation_id: str) -> Self: + self.conversation_id = conversation_id + return self + + def with_stream_start_ms(self, now_ms: float) -> Self: + self.stream_start_ms = now_ms + + def with_agent_id(self, agent_id: str) -> Self: + if not agent_id: + return self + + self.agent_id = agent_id + return self + + def with_organization_id(self, organization_id: str) -> Self: + self.organization_id = organization_id + return self + + def with_organization( + self, + session: DBSessionDep | None = None, + organization: Organization | None = None, + ) -> Self: + if not organization and not session: + return self + + if not organization: + organization = organization_crud.get_organization( + session, self.organization_id + ) + organization = ( + Organization.model_validate(organization) if organization else None + ) + + if organization: + self.organization = organization + + return self + + def with_global_filtering(self) -> Self: + self.use_global_filtering = True + return self + + def without_global_filtering(self) -> Self: + self.use_global_filtering = False + return self + + def get_organization(self): + return self.organization + + def get_stream_start_ms(self): + return self.stream_start_ms + + def get_request(self): + return self.request + + def get_response(self): + return self.response + + def get_receive(self): + return self.receive + + def get_trace_id(self): + return self.trace_id + + def get_user_id(self): + return self.user_id + + def get_event_type(self): + return self.event_type + + def get_model(self): + return self.model + + def get_deployment_name(self): + return self.deployment_name + + def get_model_config(self): + return self.model_config + + def get_conversation_id(self): + return self.conversation_id + + def get_agent_id(self): + return self.agent_id + + def get_logger(self) -> Any: + return self.logger + + def get_agent_tool_metadata(self): + return self.agent_tool_metadata diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/conversation.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/conversation.py new file mode 100644 index 0000000000000000000000000000000000000000..e890b59e8a9195e3954dc32ce877d4ed328dc72d --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/conversation.py @@ -0,0 +1,163 @@ +import datetime +from abc import ABC +from typing import Optional + +from pydantic import BaseModel, Field, computed_field + +from backend.schemas.file import ConversationFilePublic +from backend.schemas.message import Message + + +class ConversationBase(ABC, BaseModel): + """ + Abstract class for Conversation Schemas + """ + user_id: str = Field( + ..., + title="User ID", + description="User ID for the conversation", + ) + organization_id: Optional[str] = Field( + None, + title="Organization ID", + description="Organization ID for the conversation", + ) + + +class Conversation(ConversationBase): + """ + Schema for a conversation + """ + id: str = Field( + ..., + title="ID", + description="Unique identifier for the conversation", + ) + created_at: datetime.datetime = Field( + ..., + title="Created At Timestamp", + description="When the conversation was created", + ) + updated_at: datetime.datetime = Field( + ..., + title="Updated At Timestamp", + description="When the conversation was updated", + ) + + title: str = Field( + ..., + title="Title", + description="Title of the conversation", + ) + messages: list[Message] = Field( + ..., + title="Messages", + description="The conversation messages", + ) + files: list[ConversationFilePublic] = Field( + ..., + title="Files", + description="List of files for the conversation", + ) + description: Optional[str] = Field( + None, + title="Description", + description="Description of the conversation", + ) + agent_id: Optional[str] = Field( + None, + title="Agent ID", + description="Unique identifier for the agent used in the conversation", + ) + is_pinned: bool = Field( + ..., + title="Is Pinned", + description="If conversation is pinned", + ) + + @computed_field(return_type=int) + def total_file_size(self): + return sum([getattr(file, "file_size", 0) for file in self.files]) + + class Config: + from_attributes = True + + +class ConversationPublic(Conversation): + """ + A public conversation which removes the User ID and Organization ID + """ + user_id: Optional[str] = Field( + exclude=True, + title="User ID", + description="User ID for the conversation", + ) + organization_id: Optional[str] = Field( + exclude=True, + title="Organization ID", + description="Organization ID for the conversation", + ) + + +class ConversationWithoutMessages(ConversationPublic): + """ + A public conversation without messages attached + """ + messages: list[Message] = Field( + exclude=True, + title="Messages", + description="The conversation messages", + ) + + +class UpdateConversationRequest(BaseModel): + """ + Request to update a conversation + """ + title: Optional[str] = Field( + None, + title="Title", + description="Title of the conversation", + ) + description: Optional[str] = Field( + None, + title="Description", + description="Description of the conversation", + ) + + class Config: + from_attributes = True + + +class ToggleConversationPinRequest(BaseModel): + """ + Request to toggle pinning a conversation + """ + is_pinned: bool = Field( + ..., + title="Is Pinned", + description="If conversation is pinned", + ) + + +class DeleteConversationResponse(BaseModel): + """ + Response for deleting a conversation + """ + pass + + +class GenerateTitleResponse(BaseModel): + """ + Response for generating a title + """ + title: str = Field( + ..., + title="Title", + description="Title generated for the conversation", + ) + error: Optional[str] = Field( + None, + title="Error", + description="Error message if the response is an error", + ) diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/deployment.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/deployment.py new file mode 100644 index 0000000000000000000000000000000000000000..a03938ff6ad0f58afc2bee6304f7c6619abf9aff --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/deployment.py @@ -0,0 +1,151 @@ +from typing import Optional + +from pydantic import BaseModel, Field + + +class DeploymentDefinition(BaseModel): + """ + Deployment Definition + """ + id: str = Field( + ..., + title="ID", + description="Unique Identifier for the Deployment", + ) + name: str = Field( + ..., + title="Name", + description="Name of the Deployment", + ) + description: Optional[str] = Field( + None, + title="Description", + description="Description of the deployment", + ) + config: dict[str, str] = Field( + {}, + title="Config", + description="Config for the deployment", + ) + is_available: bool = Field( + False, + title="Is Available", + description="Is deployment is available", + ) + is_community: Optional[bool] = Field( + False, + title="Is Community", + description="Is the deployment from the commmunity", + ) + models: list[str] = Field( + ..., + title="Models", + description="List of models for the deployment", + ) + class_name: str = Field( + ..., + title="Class Name", + description="Deployment class name", + ) + + class Config: + from_attributes = True + + @classmethod + def from_db_deployment(cls, obj): + data = { + "id": obj.id, + "name": obj.name, + "description": obj.description if obj.description else None, + "models": [model.name for model in obj.models], + "is_community": obj.is_community, + "is_available": obj.is_available, + "config": obj.default_deployment_config, + "class_name": obj.deployment_class_name, + } + return cls(**data) + + +class DeploymentCreate(BaseModel): + """ + Deployment Create Schema + """ + id: Optional[str] = Field( + None, + title="ID", + description="Unique Identifier for the Deployment", + ) + name: str = Field( + ..., + title="Name", + description="Name of the Deployment", + ) + description: Optional[str] = Field( + None, + title="Description", + description="Description of the deployment", + ) + deployment_class_name: str = Field( + ..., + title="Deployment Class Name", + description="Deployment Class Name", + ) + is_community: bool = Field( + False, + title="Is Community", + description="Is the deployment from the commmunity", + ) + default_deployment_config: dict[str, str] = Field( + ..., + title="Default Deployment Config", + description="The default deployment configuration", + ) + + +class DeploymentUpdate(BaseModel): + """ + Deployment Update Schema + """ + name: Optional[str] = Field( + None, + title="Name", + description="Name of the Deployment", + ) + description: Optional[str] = Field( + None, + title="Description", + description="Description of the deployment", + ) + deployment_class_name: Optional[str] = Field( + None, + title="Deployment Class Name", + description="Deployment Class Name", + ) + is_community: Optional[bool] = Field( + None, + title="Is Community", + description="Is the deployment from the commmunity", + ) + default_deployment_config: Optional[dict[str, str]] = Field( + None, + title="Default Deployment Config", + description="The default deployment configuration", + ) + + +class DeleteDeployment(BaseModel): + """ + Delete Deployment Response + """ + pass + + +class UpdateDeploymentEnv(BaseModel): + """ + Request to update Deployment Environment Variables + """ + env_vars: dict[str, str] = Field( + ..., + title="Env Vars", + description="Environment Variables for the Deployment", + ) diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/document.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/document.py new file mode 100644 index 0000000000000000000000000000000000000000..cdbbb15f366b37eced39a0250fbfd844e945164a --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/document.py @@ -0,0 +1,43 @@ +from typing import Optional + +from pydantic import BaseModel, Field + + +class Document(BaseModel): + """ + Schema for a Document + """ + text: str = Field( + ..., + title="Text", + description="Document text", + ) + document_id: str = Field( + ..., + title="Document_Id", + description="Unique Identifier for the document", + ) + + title: Optional[str] = Field( + None, + title="Title", + description="Document title", + ) + url: Optional[str] = Field( + None, + title="URL", + description="Document URL", + ) + fields: Optional[dict] = Field( + None, + title="Fields", + description="Document Fields", + ) + tool_name: Optional[str] = Field( + None, + title="Tool Name", + description="Tool name for the document", + ) + + class Config: + from_attributes = True diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/file.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/file.py new file mode 100644 index 0000000000000000000000000000000000000000..aba3d7f52174af6281da90c9cef7de18f17d4a4f --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/file.py @@ -0,0 +1,131 @@ +import datetime +from abc import ABC +from typing import Optional + +from pydantic import BaseModel, Field + + +class FileBase(ABC, BaseModel): + """ + Abstract class for File schemas + """ + id: str = Field( + ..., + title="ID", + description="Unique identifier of the file", + ) + created_at: datetime.datetime = Field( + ..., + title="Created At Timestamp", + description="When file was created", + ) + updated_at: datetime.datetime = Field( + ..., + title="Updated At Timestamp", + description="When file was updated", + ) + file_name: str = Field( + ..., + title="File Name", + description="Name of the file", + ) + file_size: int = Field( + 0, + title="File Size", + description="Size of the file in bytes", + ge=0 + ) + + + +class File(FileBase): + """ + Schema for a File + """ + user_id: str = Field( + ..., + title="User ID", + description="Unique identifier for who created the file", + ) + conversation_id: Optional[str] = Field( + "", + title="Conversation ID", + description="Unique identifier for the conversation the file is associated to", + ) + file_content: Optional[str] = Field( + "", + title="File Content", + description="The contents of the file", + ) + + class Config: + from_attributes = True + + +class ConversationFilePublic(FileBase): + """ + Schema for a public conversation file + """ + user_id: str = Field( + ..., + title="User ID", + description="Unique identifier for who created the file", + ) + conversation_id: str = Field( + ..., + title="Conversation ID", + description="Unique identifier for the conversation the file is associated to", + ) + + +class AgentFilePublic(FileBase): + """ + Schema for a public agent file + """ + pass + + +class FileMetadata(FileBase): + """ + Schema for file metadata + """ + file_content: str = Field( + ..., + title="File Content", + description="The contents of the file", + ) + + +class ListConversationFile(ConversationFilePublic): + """ + Listing conversation files + """ + pass + + +class UploadConversationFileResponse(ConversationFilePublic): + """ + Response for uploading a conversation file + """ + pass + + +class UploadAgentFileResponse(AgentFilePublic): + """ + Reponse for uploading an agent file + """ + pass + + +class DeleteConversationFileResponse(BaseModel): + """ + Response for deleting a conversation file + """ + pass + + +class DeleteAgentFileResponse(BaseModel): + """ + Response for deleting an agent file + """ + pass diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/message.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/message.py new file mode 100644 index 0000000000000000000000000000000000000000..8307c933aea5255fd0d8fc1ff4044b553e0e410e --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/message.py @@ -0,0 +1,114 @@ +import datetime +from abc import ABC +from typing import Optional + +from pydantic import BaseModel, Field + +from backend.database_models.message import MessageAgent +from backend.schemas.citation import Citation +from backend.schemas.document import Document +from backend.schemas.file import ConversationFilePublic +from backend.schemas.tool import ToolCall + + +class MessageBase(ABC, BaseModel): + """ + Abstract class for Message schemas + """ + text: str = Field( + ..., + title="Text", + description="The text content of the message", + ) + + +class Message(MessageBase): + """ + Message Schema + """ + id: str = Field( + ..., + title="ID", + description="Unique identifier of the message", + ) + created_at: datetime.datetime = Field( + ..., + title="Created At Timestamp", + description="When message was created", + ) + updated_at: datetime.datetime = Field( + ..., + title="Updated At Timestamp", + description="When message was updated", + ) + + generation_id: Optional[str] = Field( + None, + title="Generation ID", + description="Generation ID for the message", + ) + + position: int = Field( + ..., + title="Position", + description="Position in the conversation", + ) + is_active: bool = Field( + ..., + title="Is Active", + description="Is the message active", + ) + + documents: list[Document] = Field( + ..., + title="Documents", + description="Documents associated with the message", + ) + citations: list[Citation] = Field( + ..., + title="Citations", + description="Citations associated with the message", + ) + files: list[ConversationFilePublic] = Field( + ..., + title="Files", + description="Files associated with the message", + ) + tool_calls: list[ToolCall] = Field( + ..., + title="Tool Calls", + description="Tool calls associated with the message", + ) + tool_plan: Optional[str] = Field( + None, + title="Tool Plan", + description="Tool plan associated with the message", + ) + + agent: MessageAgent = Field( + ..., + title="Agent", + description="Agent associated with the message", + ) + + class Config: + from_attributes = True + + +class UpdateMessage(BaseModel): + """ + Request to update a message + """ + text: Optional[str] = Field( + None, + title="Text", + description="The text content of the message", + ) + title: Optional[str] = Field( + None, + title="Title", + description="The title of the message", + ) + + class Config: + from_attributes = True diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/model.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/model.py new file mode 100644 index 0000000000000000000000000000000000000000..f880e36157c317f660efddc486446a1c7c026396 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/model.py @@ -0,0 +1,69 @@ +from abc import ABC +from typing import Optional + +from pydantic import BaseModel, Field + + +class BaseModelSchema(ABC, BaseModel): + """ + Abstract class for Model Schemas + """ + name: str = Field( + ..., + title="Name", + description="Model name", + ) + cohere_name: Optional[str] = Field( + None, + title="Cohere Name", + description="Cohere model name", + ) + description: Optional[str] = Field( + None, + title="Description", + description="Model description", + ) + + +class Model(BaseModelSchema): + id: str = Field( + ..., + title="ID", + description="Unique identifier for the model" + ) + deployment_id: str = Field( + ..., + title="Deployment ID", + description="Unique identifier for the deployment" + ) + + class Config: + from_attributes = True + + +class ModelCreate(BaseModelSchema): + deployment_id: str = Field( + ..., + title="Deployment ID", + description="Unique identifier for the deployment" + ) + + +class ModelUpdate(BaseModelSchema): + name: Optional[str] = Field( + None, + title="Name", + description="Model name", + ) + deployment_id: Optional[str] = Field( + None, + title="Deployment ID", + description="Unique identifier for the deployment" + ) + + +class DeleteModel(BaseModel): + """ + Response for deleting a model + """ + pass diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/organization.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/organization.py new file mode 100644 index 0000000000000000000000000000000000000000..e7f6e0219874172a57cfe3ce85c0f22a16488427 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/organization.py @@ -0,0 +1,65 @@ +import datetime +from abc import ABC +from typing import Optional + +from pydantic import BaseModel, Field + + +class OrganizationBase(ABC, BaseModel): + """ + Abstract class for organization schemas + """ + name: str = Field( + ..., + title="Name", + description="Name of the organization", + ) + + +class Organization(OrganizationBase): + """ + Schema for an organization + """ + id: str = Field( + ..., + title="ID", + description="Unique identifier of the organization", + ) + created_at: datetime.datetime = Field( + ..., + title="Created At Timestamp", + description="When organization was created", + ) + updated_at: datetime.datetime = Field( + ..., + title="Updated At Timestamp", + description="When organization was updated", + ) + + class Config: + from_attributes = True + + +class CreateOrganization(OrganizationBase): + """ + Request to create an organization + """ + pass + + +class UpdateOrganization(OrganizationBase): + """ + Request to update an organization + """ + name: Optional[str] = Field( + None, + title="Name", + description="Name of the organization", + ) + + +class DeleteOrganization(BaseModel): + """ + Response when deleting organization + """ + pass diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/params/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/params/agent.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/agent.py new file mode 100644 index 0000000000000000000000000000000000000000..82ac28499585fc24b70aab745be2c502a6a91833 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/agent.py @@ -0,0 +1,28 @@ +""" +Query and Path Parameters for Agents +""" +from typing import Annotated, Optional + +from fastapi import Path, Query + +from backend.schemas.agent import AgentVisibility + +VisibilityQueryParam = Annotated[AgentVisibility, Query( + title="Visibility", + description="Agent visibility", +)] + +AgentIdQueryParam = Annotated[Optional[str], Query( + title="Agent ID", + description="Agent ID to filter results by", +)] + +AgentIdPathParam = Annotated[str, Path( + title="Agent ID", + description="Agent ID for agent in question", +)] + +AgentToolMetadataIdPathParam = Annotated[str, Path( + title="Agent Tool Metadata ID", + description="Agent Tool Metadata ID for tool metadata in question", +)] diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/params/auth.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/auth.py new file mode 100644 index 0000000000000000000000000000000000000000..582cf376aadecc78307015ca6f1b6ce6d6b13ff7 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/auth.py @@ -0,0 +1,16 @@ +""" +Query and Path Parameters for Auth +""" +from typing import Annotated, Optional + +from fastapi import Path, Query + +StrategyPathParam = Annotated[str, Path( + title="Strategy Name", + description="Name of strategy in question", +)] + +CodeQueryParam = Annotated[Optional[str], Query( + title="Code", + description="OAuth Code", +)] diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/params/conversation.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/conversation.py new file mode 100644 index 0000000000000000000000000000000000000000..aef2dea7808351f071399e48d001dd33e75c3aa0 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/conversation.py @@ -0,0 +1,16 @@ +""" +Query and Path Parameters for Conversations +""" +from typing import Annotated + +from fastapi import Path, Query + +ConversationIdPathParam = Annotated[str, Path( + title="Conversation ID", + description="Conversation ID for conversation in question", +)] + +QueryQueryParam = Annotated[str, Query( + title="Query", + description="Query string to search for in a conversation title", +)] diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/params/deployment.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/deployment.py new file mode 100644 index 0000000000000000000000000000000000000000..ca3aeb932adfdade28f62b04f22271513200abcc --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/deployment.py @@ -0,0 +1,16 @@ +""" +Query and Path Parameters for Deployments +""" +from typing import Annotated, Optional + +from fastapi import Path, Query + +DeploymentIdPathParam = Annotated[str, Path( + title="Deployment ID", + description="Deployment ID for deployment in question", +)] + +AllQueryParam = Annotated[Optional[bool], Query( + title="All", + description="Include all deployments, regardless of availability.", +)] diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/params/file.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/file.py new file mode 100644 index 0000000000000000000000000000000000000000..861c8a96d8f43ab69b5561ca2811136130922afc --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/file.py @@ -0,0 +1,11 @@ +""" +Query and Path Parameters for Files +""" +from typing import Annotated + +from fastapi import Path + +FileIdPathParam = Annotated[str, Path( + title="File ID", + description="File ID for file in question", +)] diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/params/message.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/message.py new file mode 100644 index 0000000000000000000000000000000000000000..389c8f5b976bb288f53a3c7a68de684686f6ba95 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/message.py @@ -0,0 +1,11 @@ +""" +Query and Path Parameters for Messages +""" +from typing import Annotated + +from fastapi import Path + +MessageIdPathParam = Annotated[str, Path( + title="Message ID", + description="Message ID for message in question", +)] diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/params/model.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/model.py new file mode 100644 index 0000000000000000000000000000000000000000..529f87e0263b100137097450b20a87c8497547ed --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/model.py @@ -0,0 +1,16 @@ +""" +Query and Path Parameters for Models +""" +from typing import Annotated, Optional + +from fastapi import Path, Query + +ModelIdPathParam = Annotated[str, Path( + title="Model ID", + description="Model ID for the model in question", +)] + +ModelQueryParam = Annotated[Optional[str], Query( + title="Model", + description="Model to filter results by", +)] diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/params/organization.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/organization.py new file mode 100644 index 0000000000000000000000000000000000000000..13bd1dcd6d01275a0e9d0014f5f84a71b2c3abe1 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/organization.py @@ -0,0 +1,16 @@ +""" +Query and Path Parameters for Organizations +""" +from typing import Annotated, Optional + +from fastapi import Path, Query + +OrganizationIdPathParam = Annotated[str, Path( + title="Organization ID", + description="Organization ID for the organization in question", +)] + +OrganizationIdQueryParam = Annotated[Optional[str], Query( + title="Organization ID", + description="Organization ID to filter results by", +)] diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/params/scim.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/scim.py new file mode 100644 index 0000000000000000000000000000000000000000..3470e2ae5979cac1c7fb8eede7a635cc21252221 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/scim.py @@ -0,0 +1,44 @@ +""" +Query and Path Parameters for SCIM +""" +from typing import Annotated, Optional + +from fastapi import Depends, Path, Query + + +class _ScimPaginationParams: + """ + Common pagination query parameters + """ + def __init__( + self, + start_index: Annotated[int, Query( + title="Start Index", + description="Start Index for request", + )] = 1, + count: Annotated[int, Query( + title="Count", + description="Maximum number of records to return per request", + )] = 100, + filter: Annotated[Optional[str], Query( + title="Filter", + description="Filter to use when filtering response", + )] = None + ) -> None: + self.start_index = start_index + self.count = count + self.filter = filter + +ScimPaginationQueryParams = Annotated[_ScimPaginationParams, Depends()] + + +UserIdPathParam = Annotated[str, Path( + title="User ID", + description="User ID for the user in question", +)] + + +GroupIdPathParam = Annotated[str, Path( + title="Group ID", + description="Group ID for the group in question", +)] diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/params/shared.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/shared.py new file mode 100644 index 0000000000000000000000000000000000000000..a86cf60080e442cf52dcdf71fb989d41d60021cd --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/shared.py @@ -0,0 +1,33 @@ +""" +Shared Query and Path Parameters for Routers +""" +from typing import Annotated, Optional + +from fastapi import Depends, Query + + +class _PaginationParams: + """ + Common pagination query parameters + """ + def __init__( + self, + offset: Annotated[int, Query( + title="Pagination Offset", + description="Offset for where request should start returning records from", + )] = 0, + limit: Annotated[int, Query( + title="Pagination Limit", + description="Maximum number of records to return per request", + )] = 100, + ) -> None: + self.offset = offset + self.limit = limit + +PaginationQueryParams = Annotated[_PaginationParams, Depends()] + + +OrderByQueryParam = Annotated[Optional[str], Query( + title="Orber By", + description="Field to sorts results by", +)] diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/params/snapshot.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/snapshot.py new file mode 100644 index 0000000000000000000000000000000000000000..8eaf6e84dcc3d197783d14310e3668b696d80036 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/snapshot.py @@ -0,0 +1,16 @@ +""" +Query and Path Parameters for Snapshots +""" +from typing import Annotated + +from fastapi import Path + +LinkIdPathParam = Annotated[str, Path( + title="Link ID", + description="Link ID for the snapshot link in question", +)] + +SnapshotIdPathParam = Annotated[str, Path( + title="Snapshot ID", + description="Snapshot ID for the snapshot in question", +)] diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/params/tool.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/tool.py new file mode 100644 index 0000000000000000000000000000000000000000..74fc7f76df7cc7a1c0a6702a585b44cb0a8cc7ac --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/tool.py @@ -0,0 +1,11 @@ +""" +Query and Path Parameters for Auth +""" +from typing import Annotated + +from fastapi import Path + +ToolIdPathParam = Annotated[str, Path( + title="Tool ID", + description="Tool ID for tool in question", +)] diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/params/user.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/user.py new file mode 100644 index 0000000000000000000000000000000000000000..dd132135e3b0077d5220c1465c13377fc8eedd47 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/params/user.py @@ -0,0 +1,11 @@ +""" +Query and Path Parameters for Users +""" +from typing import Annotated + +from fastapi import Path + +UserIdPathParam = Annotated[str, Path( + title="User ID", + description="User ID for the user in question", +)] diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/scim.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/scim.py new file mode 100644 index 0000000000000000000000000000000000000000..3dd65317b1aa05f15d21cdfaee831ad1c76d0feb --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/scim.py @@ -0,0 +1,320 @@ +from abc import ABC +from typing import ClassVar, Optional + +from pydantic import BaseModel, ConfigDict, Field +from pydantic.alias_generators import to_camel + +from backend.database_models import Group as DBGroup +from backend.database_models import User as DBUser + + +class BaseSchema(ABC, BaseModel): + """ + Abstract class for other schemas + """ + model_config = ConfigDict( + alias_generator=to_camel, + populate_by_name=True, + from_attributes=True, + ) + + +class Meta(BaseSchema): + """ + Schema for metadata + """ + resource_type: str = Field( + ..., + title="Resource Type", + description="Type of resource the metadata is for", + ) + created: str = Field( + ..., + title="Created", + description="When metadata was created", + ) + last_modified: str = Field( + ..., + title="Last Modified", + description="When metadata was last modified", + ) + + +class Name(BaseSchema): + given_name: str = Field( + ..., + title="Given Name", + description="User's given name", + ) + family_name: str = Field( + ..., + title="Family Name", + description="User's family name", + ) + + +class BaseUser(BaseSchema): + user_name: Optional[str] = Field( + None, + title="User Name", + description="User name", + ) + active: Optional[bool] = Field( + None, + title="Active", + description="Is user active", + ) + + schemas: list[str] = Field( + ..., + title="Schemas", + description="Schemas for the user", + ) + + +class GroupMember(BaseSchema): + value: str = Field( + ..., + title="Value", + description="Value", + ) + display: str = Field( + ..., + title="Display", + description="Display", + ) + + +class BaseGroup(BaseSchema): + schemas: list[str] = Field( + ..., + title="Schemas", + description="Schemas for the group", + ) + members: list[GroupMember] = Field( + ..., + title="Members", + description="Members of the group", + ) + display_name: str = Field( + ..., + title="Display Name", + description="Display name for the group", + ) + + +class CreateGroup(BaseGroup): + pass + + +class Email(BaseSchema): + primary: bool = Field( + ..., + title="Primary", + description="Is email the primary email", + ) + value: Optional[str] = Field( + None, + title="Value", + description="Email value", + ) + type: str = Field( + ..., + title="Type", + description="Type of email", + ) + + +class CreateUser(BaseUser): + name: Name = Field( + ..., + title="Name", + description="Name of user", + ) + emails: list[Email] = Field( + ..., + title="Emails", + description="List of emails for user", + ) + externalId: str = Field( + ..., + title="External ID", + description="External ID for the user", + ) + + +class UpdateUser(BaseUser): + name: Name = Field( + ..., + title="Name", + description="Name of user", + ) + emails: list[Email] = Field( + ..., + title="Emails", + description="List of emails for user", + ) + + +class Operation(BaseSchema): + op: str = Field( + ..., + title="Op", + description="Op", + ) + value: dict[str, bool] = Field( + ..., + title="Value", + description="Value", + ) + + +class GroupOperation(BaseSchema): + op: str = Field( + ..., + title="Op", + description="Op", + ) + path: Optional[str] = Field( + None, + title="Path", + description="Path", + ) + value: dict[str, str]|list[dict[str, str]] = Field( + ..., + title="Value", + description="Value", + ) + + +class PatchUser(BaseSchema): + schemas: list[str] = Field( + ..., + title="Schemas", + description="Schemas for user", + ) + Operations: list[Operation] = Field( + ..., + title="Operations", + description="Operations for the user", + ) + + +class PatchGroup(BaseSchema): + schemas: list[str] = Field( + ..., + title="Schemas", + description="Schemas for group", + ) + Operations: list[GroupOperation] = Field( + ..., + title="Operations", + description="Operations for the group", + ) + + +class Group(BaseGroup): + id: str = Field( + ..., + title="ID", + description="Unique identifier for the group", + ) + display_name: str = Field( + ..., + title="Display Name", + description="Display name for the group", + ) + meta: Meta = Field( + ..., + title="Meta", + description="Metadata for the group", + ) + + @staticmethod + def from_db_group(db_group: DBGroup) -> "Group": + return Group( + id=db_group.id, + display_name=db_group.display_name, + members=[ + GroupMember(value=ua.user_id, display=ua.display) + for ua in db_group.user_associations + ], + meta=Meta( + resourceType="Group", + created=db_group.created_at.isoformat(), + lastModified=db_group.updated_at.isoformat(), + ), + schemas=["urn:ietf:params:scim:schemas:core:2.0:Group"], + ) + + +class User(BaseUser): + id: str = Field( + ..., + title="ID", + description="Unique identifier for the user", + ) + external_id: str = Field( + ..., + title="External ID", + description="External ID for the user", + ) + meta: Meta = Field( + ..., + title="Meta", + description="Metadata for the user", + ) + + @staticmethod + def from_db_user(db_user: DBUser) -> "User": + return User( + id=db_user.id, + user_name=db_user.user_name, + active=db_user.active, + external_id=db_user.external_id, + meta=Meta( + resourceType="User", + created=db_user.created_at.isoformat(), + lastModified=db_user.updated_at.isoformat(), + ), + schemas=["urn:ietf:params:scim:schemas:core:2.0:User"], + ) + + +class BaseListResponse(BaseSchema): + schemas: ClassVar[list[str]] = [ + "urn:ietf:params:scim:api:messages:2.0:ListResponse" + ] + total_results: int = Field( + ..., + title="Total Results", + description="Total results available", + ) + start_index: int = Field( + ..., + title="Start Index", + description="Start index for returned results", + ) + items_per_page: int = Field( + ..., + title="Items Per Page", + description="Total results returned in the request", + ) + + +class ListUserResponse(BaseListResponse): + resources: list[User] = Field( + ..., + title="Resources", + description="List of Users", + alias="Resources", + ) + + +class ListGroupResponse(BaseListResponse): + resources: list[Group] = Field( + ..., + title="Resources", + description="List of Groups", + alias="Resources", + ) diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/search_query.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/search_query.py new file mode 100644 index 0000000000000000000000000000000000000000..7ca5f99a07c96a544e1246e0ee457c6b93269d98 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/search_query.py @@ -0,0 +1,20 @@ +from pydantic import BaseModel, Field + + +class SearchQuery(BaseModel): + """ + Schema for search query + """ + text: str = Field( + ..., + title="Text", + description="Text for the search", + ) + generation_id: str = Field( + ..., + title="Generation ID", + description="Unique identifier for the generation", + ) + + class Config: + from_attributes = True diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/snapshot.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/snapshot.py new file mode 100644 index 0000000000000000000000000000000000000000..a55fbf2711a3ee0e07475a175f82962d79e45bd0 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/snapshot.py @@ -0,0 +1,272 @@ +import datetime +from typing import Optional + +from pydantic import BaseModel, Field + +from backend.schemas.agent import AgentToolMetadata +from backend.schemas.message import Message + + +class SnapshotAgent(BaseModel): + """ + Agent Snapshot + """ + id: str = Field( + ..., + title="ID", + description="Unique identifier for the agent snapshot", + ) + name: str = Field( + ..., + title="Name", + description="Name of the snapshot", + ) + description: Optional[str] = Field( + None, + title="Description", + description="Description of the snapshot", + ) + preamble: Optional[str] = Field( + None, + title="Preamble", + description="Peamble for the snapshot", + ) + tools_metadata: Optional[list[AgentToolMetadata]] = Field( + None, + title="Tools Metadata", + description="Tools metadata for the snapshot", + ) + + class Config: + from_attributes = True + + +class SnapshotData(BaseModel): + """ + Snapshot data + """ + title: str = Field( + ..., + title="Title", + description="Title of the snapshot", + ) + description: str = Field( + ..., + title="Description", + description="Description of the snapshot", + ) + messages: list[Message] = Field( + ..., + title="Messages", + description="List of messages", + ) + agent: Optional[SnapshotAgent] = Field( + None, + title="Agent", + description="Agent for the snapshot", + exclude=True, + ) + + class Config: + from_attributes = True + + +class Snapshot(BaseModel): + """ + Snapshot + """ + conversation_id: str = Field( + ..., + title="Conversation ID", + description="Unique identifier for the conversation", + ) + id: str = Field( + ..., + title="ID", + description="Unique identifier for the snapshot", + ) + last_message_id: str = Field( + ..., + title="Last Message ID", + description="Unique identifier for the last message", + ) + user_id: str = Field( + ..., + title="User ID", + description="Unique identifier for the user", + ) + organization_id: Optional[str] = Field( + None, + title="Organization ID", + description="Unique identifier for the organization", + ) + + version: int = Field( + ..., + title="Version", + description="Snapshot version", + ) + created_at: datetime.datetime = Field( + ..., + title="Created At Timestamp", + description="When snapshot was creted", + ) + updated_at: datetime.datetime = Field( + ..., + title="Updated At Timestamp", + description="When snapshot was updated", + ) + snapshot: SnapshotData = Field( + ..., + title="Snapshot Data", + description="Data for the snapshot", + ) + agent_id: Optional[str] = Field( + None, + title="Agent ID", + description="Unique identifier for the agent", + exclude=True, + ) + + class Config: + from_attributes = True + + +class SnapshotPublic(Snapshot): + """ + Public snapshot + """ + user_id: Optional[str] = Field( + None, + title="User ID", + description="Unique identifier for the user", + exclude=True, + ) + organization_id: Optional[str] = Field( + None, + title="Organization ID", + description="Unique identifier for the organization", + exclude=True, + ) + + class Config: + from_attributes = True + + +class SnapshotLink(BaseModel): + """ + Snapshot link + """ + snapshot_id: str = Field( + ..., + title="Snapshot ID", + description="Unique identifier for the snapshot", + ) + user_id: str = Field( + ..., + title="User ID", + description="Unique identifier for the user", + ) + + class Config: + from_attributes = True + + +class SnapshotLinkPublic(SnapshotLink): + """ + Public snapshot link + """ + user_id: Optional[str] = Field( + None, + title="User ID", + description="Unique identifier for the user", + exclude=True, + ) + + class Config: + from_attributes = True + + +class SnapshotAccess(BaseModel): + """ + Snapshot access + """ + user_id: str = Field( + ..., + title="User ID", + description="Unique identifier for the user", + ) + snapshot_id: str = Field( + ..., + title="Snapshot ID", + description="Unique identifier for the snapshot", + ) + link_id: str = Field( + ..., + title="Link ID", + description="Unique identifier for the link", + ) + + class Config: + from_attributes = True + + +class SnapshotWithLinks(SnapshotPublic): + """ + Snapshot with links + """ + links: list[str] = Field( + ..., + title="Links", + description="List of links", + ) + + class Config: + from_attributes = True + + +class CreateSnapshotRequest(BaseModel): + """ + Request to create a snapshot + """ + conversation_id: str = Field( + ..., + title="Conversation ID", + description="Unique identifier for the conversation", + ) + + class Config: + from_attributes = True + + +class CreateSnapshotResponse(SnapshotLinkPublic): + """ + Response for creating a snapshot + """ + link_id: str = Field( + ..., + title="Link ID", + description="Unique identifier for the link", + ) + messages: list[Message] = Field( + ..., + title="Messages", + description="List of messages", + ) + + class Config: + from_attributes = True + + +class DeleteSnapshotLinkResponse(BaseModel): + """ + Response for deleting a snapshot link + """ + pass + + +class DeleteSnapshotResponse(BaseModel): + """ + Response for deleting a snapshot + """ + pass diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/tool.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/tool.py new file mode 100644 index 0000000000000000000000000000000000000000..42ab30e601f6944cbd83af561cfba753e222341b --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/tool.py @@ -0,0 +1,150 @@ +from enum import StrEnum +from typing import Any, Optional + +from pydantic import BaseModel, Field + + +class ToolCategory(StrEnum): + """ + Supported Tool Categories + """ + DataLoader = "Data loader" + FileLoader = "File loader" + Function = "Function" + WebSearch = "Web search" + + +class Tool(BaseModel): + """ + Tool Schema + """ + name: Optional[str] = Field( + "", + title="Name", + description="Name of the Tool", + ) + parameter_definitions: Optional[dict] = Field( + {}, + title="Parameter Definitions", + description="Parameters definitions for the tool", + ) + +class ToolDefinition(Tool): + """ + Tool Definition Schema + """ + display_name: str = Field( + "", + title="Display Name", + description="Display name for the tool", + ) + description: str = Field( + "", + title="Description", + description="Description of the tool", + ) + error_message: Optional[str] = Field( + "", + title="Error Message", + description="Error message", + ) + kwargs: dict = Field( + {}, + title="kwargs", + description="kwags for the tool", + ) + is_visible: bool = Field( + False, + title="Is Visible", + description="Is the tool visible", + ) + is_available: bool = Field( + False, + title="Is Available", + description="Is the tool available", + ) + category: ToolCategory = Field( + ToolCategory.DataLoader, + title="Category", + description="Tool category", + ) + + is_auth_required: bool = Field( + False, + title="Is Auth Required", + description="Is auth required for the tool", + ) # Per user + auth_url: Optional[str] = Field( + "", + title="Auth Url", + description="Auth url for the tool", + ) # Per user + token: Optional[str] = Field( + "", + title="Token", + description="Token for the tool", + ) # Per user + should_return_token: bool = Field( + False, + title="Should Return Token", + description="If the tool returns a token", + ) + + implementation: Any = Field( + ..., + title="Implementation", + description="Implementation for the tool", + exclude=True, + ) + auth_implementation: Optional[Any] = Field( + None, + title="Auth Implementation", + description="Auth implementation for the tool", + exclude=True, + ) + + class Config: + from_attributes = True + + +class ToolCall(BaseModel): + """ + Schema for Tool Call + """ + name: str = Field( + ..., + title="Name", + description="Name of the Tool", + ) + parameters: dict = Field( + {}, + title="Parameters", + description="Parameters for the tool call", + ) + + class Config: + from_attributes = True + + +class ToolCallDelta(BaseModel): + """ + Schema for Tool Call Delta + """ + name: Optional[str] = Field( + None, + title="Name", + description="Name of the Tool", + ) + index: Optional[int] = Field( + None, + title="Index", + description="Index", + ) + parameters: Optional[str] = Field( + None, + title="Parameters", + description="Parameters for the tool call", + ) + + class Config: + from_attributes = True diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/tool_auth.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/tool_auth.py new file mode 100644 index 0000000000000000000000000000000000000000..22610e2ab8c693670b42b97115692b1292239597 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/tool_auth.py @@ -0,0 +1,51 @@ +import datetime +from typing import Optional + +from pydantic import BaseModel, Field + + +class UpdateToolAuth(BaseModel): + """ + Schema for Tool Authentication + """ + user_id: Optional[str] = Field( + None, + title="User ID", + description="Unique identifier for the user", + ) + tool_id: Optional[str] = Field( + None, + title="Tool ID", + description="Unique identifier for the tool", + ) + token_type: Optional[str] = Field( + None, + title="Token Type", + description="Type of token", + ) + encrypted_access_token: Optional[bytes] = Field( + None, + title="Encrypted Access Token", + description="Token for authentication", + ) + encrypted_refresh_token: Optional[bytes] = Field( + None, + title="Encrypted Refresh Token", + description="The token that can be used to refresh the access token", + ) + expires_at: Optional[datetime.datetime] = Field( + None, + title="Expires At", + description="When the access token expires", + ) + + class Config: + from_attributes = True + use_enum_values = True + + +class DeleteToolAuth(BaseModel): + """ + Response when deleting a tool auth + """ + pass diff --git a/western_ai/canada/cohere-toolkit/src/backend/schemas/user.py b/western_ai/canada/cohere-toolkit/src/backend/schemas/user.py new file mode 100644 index 0000000000000000000000000000000000000000..6bec28433b394ed1e0d35a282a97ceca7df0112d --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/schemas/user.py @@ -0,0 +1,106 @@ +import datetime +from abc import ABC +from typing import Optional + +from pydantic import BaseModel, Field + +from backend.services.auth import BasicAuthentication + +DEFAULT_USER_ID = "user-id" +DEFAULT_USER_NAME = "Default User" + + +class UserBase(ABC, BaseModel): + """ + Abstract class for User Schemas + """ + fullname: str = Field( + ..., + title="Full Name", + description="User's Full Name", + ) + email: Optional[str] = Field( + None, + title="Email", + description="User's email address", + ) + + +class User(UserBase): + """ + User schema + """ + id: str = Field( + ..., + title="ID", + description="", + ) + created_at: datetime.datetime = Field( + ..., + title="Created At Timestamp", + description="When the user was created", + ) + updated_at: datetime.datetime = Field( + ..., + title="Updated At Timestamp", + description="When the user was updated", + ) + + class Config: + from_attributes = True + + +class UserPassword(ABC, BaseModel): + """ + Adstract class for user schemas + """ + password: Optional[str] = Field( + None, + title="Password", + description="Password for the user", + ) + hashed_password: Optional[bytes] = Field( + None, + title="Hashed Password", + description="The user's password hashed", + ) + + def __init__(self, **data): + password = data.pop("password", None) + + if password is not None: + data["hashed_password"] = BasicAuthentication.hash_and_salt_password( + password + ) + + super().__init__(**data) + + +class CreateUser(UserBase, UserPassword): + """ + Request to create a user + """ + pass + + +class UpdateUser(UserPassword): + """ + Request to update a user + """ + fullname: Optional[str] = Field( + None, + title="Full Name", + description="User's Full Name", + ) + email: Optional[str] = Field( + None, + title="Email", + description="User's email address", + ) + + +class DeleteUser(BaseModel): + """ + Response when deleting a user + """ + pass diff --git a/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/constants.py b/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/constants.py new file mode 100644 index 0000000000000000000000000000000000000000..6b38ec4635fb7044a6a2af7380760bae3e7e074a --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/constants.py @@ -0,0 +1,136 @@ +from enum import StrEnum + + +class bcolors: + OKCYAN = "\033[96m" + OKGREEN = "\033[92m" + MAGENTA = "\033[35m" + WARNING = "\033[93m" + FAIL = "\033[91m" + ENDC = "\033[0m" + + +class DeploymentName(StrEnum): + COHERE_PLATFORM = "Cohere Platform" + SAGE_MAKER = "SageMaker" + AZURE = "Azure" + BEDROCK = "Bedrock" + + +class BuildTarget(StrEnum): + DEV = "dev" + PROD = "prod" + + +class Tool(StrEnum): + PythonInterpreter = "Python Interpreter" + TavilyInternetSearch = "Tavily Internet Search" + Wolfram_Alpha = "Wolfram Alpha" + + + +WELCOME_MESSAGE = r""" + █████╗ █████╗ ██╗ ██╗███████╗██████╗ ███████╗ ████████╗ █████╗ █████╗ ██╗ ██╗ ██╗██╗████████╗ +██╔══██╗██╔══██╗██║ ██║██╔════╝██╔══██╗██╔════╝ ╚══██╔══╝██╔══██╗██╔══██╗██║ ██║ ██╔╝██║╚══██╔══╝ +██║ ╚═╝██║ ██║███████║█████╗ ██████╔╝█████╗ ██║ ██║ ██║██║ ██║██║ █████═╝ ██║ ██║ +██║ ██╗██║ ██║██╔══██║██╔══╝ ██╔══██╗██╔══╝ ██║ ██║ ██║██║ ██║██║ ██╔═██╗ ██║ ██║ +╚█████╔╝╚█████╔╝██║ ██║███████╗██║ ██║███████╗ ██║ ╚█████╔╝╚█████╔╝███████╗██║ ╚██╗██║ ██║ + ╚════╝ ╚════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚══════╝ ╚═╝ ╚════╝ ╚════╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ +""" +DATABASE_URL_DEFAULT = "postgresql+psycopg2://postgres:postgres@db:5432" +REDIS_URL_DEFAULT = "redis://:redis@redis:6379" +PYTHON_INTERPRETER_URL_DEFAULT = "http://terrarium:8080" +NEXT_PUBLIC_API_HOSTNAME_DEFAULT = "http://localhost:8000" +FRONTEND_HOSTNAME_DEFAULT = "http://localhost:4000" + +BASE_CONFIG_PATH = "src/backend/config" +CONFIG_FILE_PATH = f"{BASE_CONFIG_PATH}/configuration.yaml" +CONFIG_TEMPLATE_PATH = f"{BASE_CONFIG_PATH}/configuration.template.yaml" +SECRETS_FILE_PATH = f"{BASE_CONFIG_PATH}/secrets.yaml" +SECRETS_TEMPLATE_PATH = f"{BASE_CONFIG_PATH}/secrets.template.yaml" +DOT_ENV_FILE_PATH = ".env" + + +TOOLS = { + Tool.PythonInterpreter: { + "secrets": { + "PYTHON_INTERPRETER_URL": PYTHON_INTERPRETER_URL_DEFAULT, + }, + }, + Tool.TavilyInternetSearch: { + "secrets": { + "TAVILY_API_KEY": None, + }, + }, +} + +# For main.py cli setup script +COMMUNITY_TOOLS = { + Tool.Wolfram_Alpha: { + "secrets": { + "WOLFRAM_APP_ID": None, # default value + }, + }, +} + +# Perhaps there is a better way to do this by using the Settings +# class from the backend? +ENV_YAML_CONFIG_MAPPING = { + "USE_COMMUNITY_FEATURES": { + "type": "config", + "path": "feature_flags.use_community_features", + }, + "DATABASE_URL": {"type": "config", "path": "database.url"}, + "REDIS_URL": {"type": "config", "path": "redis.url"}, + # "NEXT_PUBLIC_API_HOSTNAME": "" - this does not exist in Settings() yet to be used for Docker setups, + "FRONTEND_HOSTNAME": {"type": "config", "path": "auth.frontend_hostname"}, + "PYTHON_INTERPRETER_URL": { + "type": "config", + "path": "tools.python_interpreter.url", + }, + "TAVILY_API_KEY": {"type": "secret", "path": "tools.tavily_web_search.api_key"}, + "COHERE_API_KEY": {"type": "secret", "path": "deployments.cohere_platform.api_key"}, + "SAGE_MAKER_ACCESS_KEY": { + "type": "secret", + "path": "deployments.sagemaker.access_key", + }, + "SAGE_MAKER_SECRET_KEY": { + "type": "secret", + "path": "deployments.sagemaker.secret_key", + }, + "SAGE_MAKER_SESSION_TOKEN": { + "type": "secret", + "path": "deployments.sagemaker.session_token", + }, + "SAGE_MAKER_REGION_NAME": { + "type": "config", + "path": "deployments.sagemaker.region_name", + }, + "SAGE_MAKER_ENDPOINT_NAME": { + "type": "config", + "path": "deployments.sagemaker.endpoint_name", + }, + "BEDROCK_ACCESS_KEY": {"type": "secret", "path": "deployments.bedrock.access_key"}, + "BEDROCK_SECRET_KEY": {"type": "secret", "path": "deployments.bedrock.secret_key"}, + "BEDROCK_SESSION_TOKEN": { + "type": "secret", + "path": "deployments.bedrock.session_token", + }, + "BEDROCK_REGION_NAME": { + "type": "config", + "path": "deployments.bedrock.region_name", + }, + "AZURE_API_KEY": {"type": "secret", "path": "deployments.azure.api_key"}, + "AZURE_CHAT_ENDPOINT_URL": { + "type": "config", + "path": "deployments.azure.endpoint_url", + }, + "SINGLE_CONTAINER_URL": { + "type": "config", + "path": "deployments.single_container.url", + }, + "SINGLE_CONTAINER_MODEL": { + "type": "config", + "path": "deployments.single_container.model", + }, +} diff --git a/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/main.py b/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/main.py new file mode 100644 index 0000000000000000000000000000000000000000..8b5cb4a991baffb94671e338a855aa9d3a77030f --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/main.py @@ -0,0 +1,97 @@ +import argparse + +from dotenv import dotenv_values + +from backend.scripts.cli.constants import ( + COMMUNITY_TOOLS, + CONFIG_FILE_PATH, + SECRETS_FILE_PATH, + TOOLS, +) +from backend.scripts.cli.prompts import ( + PROMPTS, + community_tools_prompt, + deployment_prompt, + overwrite_config_prompt, + overwrite_secrets_prompt, + review_variables_prompt, + select_deployments_prompt, + tool_prompt, + update_variable_prompt, +) +from backend.scripts.cli.setters import ( + delete_config_folders, + write_config_files, + write_env_file, + write_template_config_files, +) +from backend.scripts.cli.utils import ( + process_existing_yaml_config, + show_examples, + show_welcome_message, + wrap_up, +) + + +def start(): + # Set any command args + parser = argparse.ArgumentParser() + parser.add_argument("--use-community", action=argparse.BooleanOptionalAction) + args = parser.parse_args() + + show_welcome_message() + + secrets = dotenv_values() + process_existing_yaml_config(secrets, CONFIG_FILE_PATH, overwrite_config_prompt) + process_existing_yaml_config(secrets, SECRETS_FILE_PATH, overwrite_secrets_prompt) + + # SET UP ENVIRONMENT + for _, prompt in PROMPTS.items(): + prompt(secrets) + + # ENABLE COMMUNITY TOOLS + use_community_features = args.use_community and community_tools_prompt(secrets) + if use_community_features: + TOOLS.update(COMMUNITY_TOOLS) + + # SET UP TOOLS + for name, configs in TOOLS.items(): + tool_prompt(secrets, name, configs) + + # SET UP ENVIRONMENT FOR DEPLOYMENTS + + # These imports run code that uses settings. Local imports are used so that we can + # validate any existing config file above before using settings. + from backend.config.deployments import ( + AVAILABLE_MODEL_DEPLOYMENTS as MANAGED_DEPLOYMENTS_SETUP, + ) + + all_deployments = MANAGED_DEPLOYMENTS_SETUP.copy() + selected_deployments = select_deployments_prompt(all_deployments, secrets) + + for deployment in selected_deployments: + deployment_prompt(secrets, all_deployments[deployment]) + + # REVIEW VARIABLES + variables_to_update = review_variables_prompt(secrets) + update_variable_prompt(secrets, variables_to_update) + + # SET UP .ENV FILE + write_env_file(secrets) + + # SET UP YAML CONFIG FILES + # Deal with strange edge case where there are preexisting configuration.yaml + # and secrets.yaml FOLDERS presents in the config folder - cause still unclear + delete_config_folders() + write_template_config_files() + write_config_files(secrets) + + # WRAP UP + wrap_up(selected_deployments) + + # SHOW SOME EXAMPLES + show_examples() + + +if __name__ == "__main__": + start() diff --git a/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/prompts.py b/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/prompts.py new file mode 100644 index 0000000000000000000000000000000000000000..f9bdf10294e433b44b80bd92f67289d6a46ab721 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/prompts.py @@ -0,0 +1,163 @@ +import inquirer + +from backend.model_deployments.base import BaseDeployment +from backend.scripts.cli.constants import ( + DATABASE_URL_DEFAULT, + FRONTEND_HOSTNAME_DEFAULT, + NEXT_PUBLIC_API_HOSTNAME_DEFAULT, + REDIS_URL_DEFAULT, + BuildTarget, + bcolors, +) +from backend.scripts.cli.setters import write_env_file +from backend.scripts.cli.utils import print_styled + + +def overwrite_config_prompt(): + return inquirer.confirm( + "Your existing configuration file is invalid. Overwrite?" + ) + + +def overwrite_secrets_prompt(): + return inquirer.confirm( + "Your existing secrets file is invalid. Overwrite?" + ) + + +def core_env_var_prompt(secrets): + database_url = secrets.get("DATABASE_URL") + redis_url = secrets.get("REDIS_URL") + next_public_api_hostname = secrets.get("NEXT_PUBLIC_API_HOSTNAME") + frontend_hostname = secrets.get("FRONTEND_HOSTNAME") + + if not database_url: + print_styled("💾 Let's set up your database URL.") + database_url = inquirer.text( + "Enter your database URL or press enter for default [recommended]", + default=DATABASE_URL_DEFAULT, + ) + + if not redis_url: + print_styled("💾 Now, let's set up need to set up your Redis URL.") + redis_url = inquirer.text( + "Enter your Redis URL or press enter for default [recommended]", + default=REDIS_URL_DEFAULT, + ) + + if not next_public_api_hostname: + print_styled("💾 Now, let's set up your public backend API hostname.") + next_public_api_hostname = inquirer.text( + "Enter your public API Hostname or press enter for default [recommended]", + default=NEXT_PUBLIC_API_HOSTNAME_DEFAULT, + ) + + if not frontend_hostname: + print_styled("💾 Finally, the frontend client hostname.") + frontend_hostname = inquirer.text( + "Enter your frontend hostname or press enter for default [recommended]", + default=FRONTEND_HOSTNAME_DEFAULT, + ) + + secrets["DATABASE_URL"] = database_url + secrets["REDIS_URL"] = redis_url + secrets["NEXT_PUBLIC_API_HOSTNAME"] = next_public_api_hostname + secrets["FRONTEND_HOSTNAME"] = frontend_hostname + + +def deployment_prompt(secrets, configs): + for secret in configs.env_vars(): + value = secrets.get(secret) + + if not value: + value = inquirer.text( + f"Enter the value for {secret}", validate=lambda _, x: len(x) > 0 + ) + + secrets[secret] = value + + +def community_tools_prompt(secrets): + use_community_features = secrets.get("USE_COMMUNITY_FEATURES") + + if not use_community_features: + print_styled( + "🏘️ We have some community tools that you can set up. These tools are not required for the Cohere Toolkit to run." + ) + use_community_features = inquirer.confirm( + "Do you want to set up community features (tools and model deployments)?" + ) + + secrets["USE_COMMUNITY_FEATURES"] = use_community_features + + return use_community_features + + +def tool_prompt(secrets, name, configs): + print_styled( + f"🛠️ If you want to enable {name}, set up the following secrets. Otherwise, press enter." + ) + + for key, default_value in configs["secrets"].items(): + value = secrets.get(key) + + if not value: + value = inquirer.text(f"Enter the value for {key}", default=default_value) + + secrets[key] = value + + +def build_target_prompt(secrets): + build_target = secrets.get("BUILD_TARGET") + + if not build_target: + build_target = inquirer.list_input( + "Select the build target", + choices=[BuildTarget.DEV, BuildTarget.PROD], + default=BuildTarget.DEV, + ) + + secrets["BUILD_TARGET"] = build_target + + +def review_variables_prompt(secrets): + review_list = [f"{key}: {value}" for key, value in secrets.items()] + + questions = [ + inquirer.Checkbox( + "variables", + message="Review your variables and select the ones you want to update, if any. Press enter to continue", + choices=review_list, + ), + ] + + answers = inquirer.prompt(questions) + return answers["variables"] + + +def update_variable_prompt(_, variables_to_update): + for variable_to_update in variables_to_update: + variable_to_update = variable_to_update.split(":")[0] + new_value = inquirer.text(f"Enter the new value for {variable_to_update}") + write_env_file({variable_to_update: new_value}) + print_styled( + f"🪛 Updated {variable_to_update} to {new_value}.", bcolors.OKGREEN + ) + + +def select_deployments_prompt(deployments: dict[str, type[BaseDeployment]], _): + print_styled("🚀 Let's set up your model deployments.", bcolors.MAGENTA) + + deployments = inquirer.checkbox( + "Select the model deployments you want to set up", + choices=[deployment for deployment in deployments.keys()], + default=["Cohere Platform"], + validate=lambda _, x: len(x) > 0, + ) + return deployments + + +PROMPTS = { + "core": core_env_var_prompt, + "build_target": build_target_prompt, +} diff --git a/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/setters.py b/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/setters.py new file mode 100644 index 0000000000000000000000000000000000000000..424b6992562ffe77fbaa2d9f7057da73924bb3a7 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/setters.py @@ -0,0 +1,131 @@ +import os + +import yaml +from dotenv import set_key + +from backend.scripts.cli.constants import ( + CONFIG_FILE_PATH, + CONFIG_TEMPLATE_PATH, + DOT_ENV_FILE_PATH, + ENV_YAML_CONFIG_MAPPING, + SECRETS_FILE_PATH, + SECRETS_TEMPLATE_PATH, +) + +# Use representer to allow writing literal empty value +# https://stackoverflow.com/questions/30134110/how-can-i-output-blank-value-in-python-yaml-file +yaml.add_representer( + type(None), + lambda dumper, value: dumper.represent_scalar("tag:yaml.org,2002:null", ""), +) + + +def write_env_file(secrets: dict): + for key, value in secrets.items(): + set_key(DOT_ENV_FILE_PATH, key, str(value)) + + # Manually set the frontend hostname substitution + set_key( + DOT_ENV_FILE_PATH, + "NEXT_PUBLIC_FRONTEND_HOSTNAME", + "${FRONTEND_HOSTNAME}", + "never", + ) + + +def read_yaml(file_path: str): + with open(file_path, "r") as file: + return yaml.safe_load(file) + + +def write_yaml(file_path: str, data: dict): + with open(file_path, "w") as file: + yaml.dump(data, file, default_flow_style=False) + + +def merge_yaml_dicts(old: dict, new: dict): + for key, new_value in new.items(): + # Deal with nested dictionary + if isinstance(new_value, dict) and key in old: + merge_yaml_dicts(old[key], new_value) + else: + old[key] = new_value + + +def dot_notation_to_nested_dict(flat_dict: dict) -> dict: + nested_dict = {} + + # Iterate through list of values inorder + for key, value in flat_dict.items(): + keys = key.split(".") + d = nested_dict + + # Iterate through dot notation keys inorder, EXCEPT last key + for subkey in keys[:-1]: + if subkey not in d: + d[subkey] = {} + d = d[subkey] + + # Set last key value + d[keys[-1]] = value + + return nested_dict + + +def write_template_config_files(): + if not os.path.exists(CONFIG_FILE_PATH): + config_template = read_yaml(CONFIG_TEMPLATE_PATH) + write_yaml(CONFIG_FILE_PATH, config_template) + + if not os.path.exists(SECRETS_FILE_PATH): + secrets_template = read_yaml(SECRETS_TEMPLATE_PATH) + write_yaml(SECRETS_FILE_PATH, secrets_template) + + +def convert_secrets_to_yaml(secrets: dict) -> dict: + config_changes = {} + secrets_changes = {} + + for key, value in secrets.items(): + if key in ENV_YAML_CONFIG_MAPPING.keys(): + mapper = ENV_YAML_CONFIG_MAPPING[key] + type = mapper.get("type") + path = mapper.get("path") + if type is None or path is None: + print(f"Error with YAML config mapping for key {key}.") + + if type == "secret": + secrets_changes[path] = value + elif type == "config": + config_changes[path] = value + + return config_changes, secrets_changes + + +def write_config_files(secrets: dict): + config_changes, secrets_changes = convert_secrets_to_yaml(secrets) + + # Make config changes + if config_changes: + new_config_data = dot_notation_to_nested_dict(config_changes) + # Load existing file + config_data = read_yaml(CONFIG_FILE_PATH) + # Merge changes + merge_yaml_dicts(config_data, new_config_data) + write_yaml(CONFIG_FILE_PATH, config_data) + + # Make secrets changes + if secrets_changes: + new_secrets_data = dot_notation_to_nested_dict(secrets_changes) + # Load existing file + secrets_data = read_yaml(SECRETS_FILE_PATH) + # Merge changes + merge_yaml_dicts(secrets_data, new_secrets_data) + write_yaml(SECRETS_FILE_PATH, secrets_data) + +def delete_config_folders(): + if os.path.isdir(CONFIG_FILE_PATH): + os.rmdir(CONFIG_FILE_PATH) + + if os.path.isdir(SECRETS_FILE_PATH): + os.rmdir(SECRETS_FILE_PATH) diff --git a/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/utils.py b/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..bc323abebec8a6256c74e5220d168c221982fd98 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/scripts/cli/utils.py @@ -0,0 +1,120 @@ +import sys +from pathlib import Path + +import yaml + +from backend.scripts.cli.constants import ( + ENV_YAML_CONFIG_MAPPING, + WELCOME_MESSAGE, + DeploymentName, + bcolors, +) +from backend.scripts.cli.setters import read_yaml + + +def print_styled(text: str, color: str = bcolors.ENDC): + print(color + text + bcolors.ENDC) + + +def show_welcome_message(): + print_styled(WELCOME_MESSAGE, bcolors.OKGREEN) + print_styled( + "👋 First things first, let's set up your environment.", bcolors.MAGENTA + ) + + +def process_existing_yaml_config(secrets, path, prompt): + _path = Path(path) + + if _path.is_file(): + try: + yaml_config = read_yaml(path) + except yaml.scanner.ScannerError: + if prompt(): + yaml_config = {} + _path.unlink() + else: + sys.exit(1) + + secrets.update(convert_yaml_to_secrets(yaml_config)) + + +def convert_yaml_to_secrets(yaml_dict: dict): + def get_nested_value(d, path): + keys = path.split(".") + value = d + for key in keys: + if not isinstance(value, dict) or key not in value: + return None # Return None if the path does not exist + value = value[key] + return value + + secrets = {} + for env_var, mapping in ENV_YAML_CONFIG_MAPPING.items(): + path = mapping.get("path") + if path: # Only process mappings with a defined path + value = get_nested_value(yaml_dict, path) + if value is not None: # Add only if the value exists + secrets[env_var] = value + + return secrets + + +def wrap_up(deployments): + print_styled("✅ Your configuration file has been set up.", bcolors.OKGREEN) + + print_styled( + "🎉 You're all set up! You can now run `make migrate` and `make dev` to start the Cohere Toolkit. Make sure Docker is running.", + bcolors.OKGREEN, + ) + + if DeploymentName.SAGE_MAKER in deployments: + print_styled( + "🔑 For SageMaker ensure you have run `aws configure` before `make dev` for authentication.", + bcolors.OKGREEN, + ) + + +def show_examples(): + print_styled("📚 Here are some examples to get you started:", bcolors.OKCYAN) + + print_styled( + "1. Navigate to the Cohere Toolkit frontend: ", + bcolors.OKCYAN, + ) + print_styled( + "\thttp://localhost:4000", + bcolors.OKCYAN, + ) + + print_styled( + "2. Ask a question to the Cohere Platform model", + bcolors.OKCYAN, + ) + print_styled( + """\tcurl --location 'http://localhost:8000/v1/chat-stream' --header 'User-Id: test-user' --header 'Content-Type: application/json' --data '{"message": "hey"}'""", + bcolors.OKCYAN, + ) + + print_styled( + "3. Ask a question to the SageMaker model", + bcolors.OKCYAN, + ) + print_styled( + """\tcurl --location 'http://localhost:8000/v1/chat-stream' --header 'User-Id: test-user' --header 'Deployment-Name: SageMaker' --header 'Content-Type: application/json' --data '{"message": "hey"}'""", + bcolors.OKCYAN, + ) + + print_styled( + "4. List all available models deployments and their models", + bcolors.OKCYAN, + ) + print_styled( + "\tcurl http://localhost:8000/deployments", + bcolors.OKCYAN, + ) + + print_styled( + "For more examples, please visit the Cohere Toolkit README.md", + bcolors.MAGENTA, + ) diff --git a/western_ai/canada/cohere-toolkit/src/backend/scripts/config/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/scripts/config/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/scripts/config/check_config.py b/western_ai/canada/cohere-toolkit/src/backend/scripts/config/check_config.py new file mode 100644 index 0000000000000000000000000000000000000000..515326a7d241bb0401c5d5c6bdaeb07541e1d0f1 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/scripts/config/check_config.py @@ -0,0 +1,27 @@ +from backend.scripts.config.constants import ( + CONFIG_FILE_PATH, + CONFIG_TEMPLATE_PATH, + SECRETS_FILE_PATH, + SECRETS_TEMPLATE_PATH, +) +from backend.scripts.config.utils import find_missing_keys, print_styled, read_yaml + + +def check_config(): + # Check configuration files + config_yaml = read_yaml(CONFIG_FILE_PATH) + config_template_yaml = read_yaml(CONFIG_TEMPLATE_PATH) + config_keys_missing = find_missing_keys(config_yaml, config_template_yaml) + + # Check secrets files + secrets_yaml = read_yaml(SECRETS_FILE_PATH) + secrets_template_yaml = read_yaml(SECRETS_TEMPLATE_PATH) + secrets_keys_missing = find_missing_keys(secrets_yaml, secrets_template_yaml) + + if config_keys_missing: + print_styled(f"Warning: Your configuration.yaml is missing the following keys: {config_keys_missing}") + + if secrets_keys_missing: + print_styled(f"Warning: Your secrets.yaml is missing the following keys: {secrets_keys_missing}") + +check_config() diff --git a/western_ai/canada/cohere-toolkit/src/backend/scripts/config/constants.py b/western_ai/canada/cohere-toolkit/src/backend/scripts/config/constants.py new file mode 100644 index 0000000000000000000000000000000000000000..235554b2652d0469ed324c720352a7a5672d9d93 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/scripts/config/constants.py @@ -0,0 +1,5 @@ +BASE_CONFIG_PATH = "src/backend/config" +CONFIG_FILE_PATH = f"{BASE_CONFIG_PATH}/configuration.yaml" +CONFIG_TEMPLATE_PATH = f"{BASE_CONFIG_PATH}/configuration.template.yaml" +SECRETS_FILE_PATH = f"{BASE_CONFIG_PATH}/secrets.yaml" +SECRETS_TEMPLATE_PATH = f"{BASE_CONFIG_PATH}/secrets.template.yaml" diff --git a/western_ai/canada/cohere-toolkit/src/backend/scripts/config/utils.py b/western_ai/canada/cohere-toolkit/src/backend/scripts/config/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..cce0ec02c191698aec20817ff2fc33a59849dab4 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/scripts/config/utils.py @@ -0,0 +1,54 @@ +import yaml + + +class bcolors: + FAIL = "\033[91m" + ENDC = "\033[0m" + +def print_styled(text: str, color: str = bcolors.FAIL): + print(color + text + bcolors.ENDC) + +def read_yaml(file_path: str): + """ + Loads a YAML file. + + Args: + file_path (str): File path to a YAML file. + + Returns: + dict: Dictionary representation of the YAML file. + """ + with open(file_path, "r") as file: + return yaml.safe_load(file) + +def find_missing_keys(dict_a, dict_b, parent_key=''): + """ + Recursively checks for keys in dict_b that are missing in dict_a. + + Args: + dict_a (dict): The base dictionary to compare against. + dict_b (dict): The dictionary to check for missing keys. + parent_key (str): Used for tracking nested keys. + + Returns: + list: A list of missing keys. + """ + missing_keys = [] + + for key, value in dict_b.items(): + # Construct the full key path for nested keys + full_key = f"{parent_key}.{key}" if parent_key else key + + if isinstance(value, dict): + # If both dictionaries contain this key, go deeper + if isinstance(dict_a.get(key), dict): + missing_keys.extend(find_missing_keys(dict_a[key], value, full_key)) + else: + # If dict_a has the key but it's not a dictionary, treat all nested keys as missing + missing_keys.extend(find_missing_keys({}, value, full_key)) + else: + if key not in dict_a: + # If the key itself is missing in dict_a + missing_keys.append(full_key) + + return missing_keys diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/services/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/agent.py b/western_ai/canada/cohere-toolkit/src/backend/services/agent.py new file mode 100644 index 0000000000000000000000000000000000000000..a5898b75aa7092c8741467c123f130956eeab35a --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/agent.py @@ -0,0 +1,45 @@ + +from fastapi import HTTPException + +from backend.crud import agent as agent_crud +from backend.crud import agent_tool_metadata as agent_tool_metadata_crud +from backend.database_models.agent import Agent, AgentToolMetadata +from backend.database_models.database import DBSessionDep + + +def validate_agent_exists(session: DBSessionDep, agent_id: str, user_id: str) -> Agent: + agent = agent_crud.get_agent_by_id(session, agent_id, user_id) + + if not agent: + raise HTTPException( + status_code=404, + detail=f"Agent with ID {agent_id} not found.", + ) + + return agent + + +def validate_agent_tool_metadata_exists( + session: DBSessionDep, agent_tool_metadata_id: str +) -> AgentToolMetadata: + agent_tool_metadata = agent_tool_metadata_crud.get_agent_tool_metadata_by_id( + session, agent_tool_metadata_id + ) + + if not agent_tool_metadata: + raise HTTPException( + status_code=404, + detail=f"Agent tool metadata with ID {agent_tool_metadata_id} not found.", + ) + + return agent_tool_metadata + + +def raise_db_error(e: Exception, type: str, name: str): + if "psycopg2.errors.UniqueViolation" in str(e): + raise HTTPException( + status_code=400, + detail=f"{type} {name} already exists for given user and agent.", + ) + + raise HTTPException(status_code=500, detail=str(e)) diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/auth/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/services/auth/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..fb541c28424bd37f26488111b8a79acbbf5364ea --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/auth/__init__.py @@ -0,0 +1,9 @@ +from backend.services.auth.strategies.basic import BasicAuthentication +from backend.services.auth.strategies.google_oauth import GoogleOAuth +from backend.services.auth.strategies.oidc import OpenIDConnect + +__all__ = [ + "BasicAuthentication", + "GoogleOAuth", + "OpenIDConnect", +] diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/auth/crypto.py b/western_ai/canada/cohere-toolkit/src/backend/services/auth/crypto.py new file mode 100644 index 0000000000000000000000000000000000000000..5c584e237bd5e0779063106ed5f83019869b279d --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/auth/crypto.py @@ -0,0 +1,48 @@ +import base64 +import hashlib + +from cryptography.fernet import Fernet + +from backend.config.settings import Settings + + +def get_cipher() -> Fernet: + """ + Utility method to retrieve the Fernet Cipher and using the config auth secret key for encryption/decryption. + """ + + # 1. Get env var + auth_key = Settings().get('auth.secret_key') + # 2. Hash env var using SHA-256 + hash_digest = hashlib.sha256(auth_key.encode()).digest() + # 3. Base64 encode hash and get 32-byte key + fernet_key = base64.urlsafe_b64encode(hash_digest[:32]) + + return Fernet(fernet_key) + + +def encrypt(value: str) -> bytes: + """ + Utility method to encrypt a string value using a Fernet Cipher. + """ + cipher = get_cipher() + + return cipher.encrypt(value.encode()) + + +def decrypt(encrypted_value: bytes) -> str: + """ + Utility method to decrypt a string value using a Fernet Cipher. + """ + cipher = get_cipher() + + return cipher.decrypt(encrypted_value).decode() + + +def convert_string_encryption_to_cipher_encryption(encoded_value: bytes): + """ + Utility method to convert old string encodings into Fernet Cipher encryptions. + """ + decoded = encoded_value.decode() + + return encrypt(decoded) diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/auth/jwt.py b/western_ai/canada/cohere-toolkit/src/backend/services/auth/jwt.py new file mode 100644 index 0000000000000000000000000000000000000000..925a269c7aae498dc2e0ffcb1ad5e84767946acb --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/auth/jwt.py @@ -0,0 +1,70 @@ +import datetime +import uuid + +import jwt + +from backend.config.settings import Settings +from backend.services.logger.utils import LoggerFactory + +logger = LoggerFactory().get_logger() + + +class JWTService: + ISSUER = "cohere-toolkit" + EXPIRY_DAYS = 90 + ALGORITHM = "HS256" + + def __init__(self): + secret_key = Settings().get('auth.secret_key') + + if not secret_key: + raise ValueError( + "auth.secret_key in secrets.yaml is missing, and is required to enable authentication." + ) + + self.secret_key = secret_key + + def create_and_encode_jwt(self, user: dict) -> str: + """ + Creates a payload based on user info and creates a JWT token. + + Args: + user (dict): User data. + + Returns: + str: JWT token. + """ + now = datetime.datetime.utcnow() + payload = { + "iss": self.ISSUER, + "iat": now, + "exp": now + datetime.timedelta(days=self.EXPIRY_DAYS), + "jti": str(uuid.uuid4()), + "context": user, + } + + token = jwt.encode(payload, self.secret_key, self.ALGORITHM) + + return token + + def decode_jwt(self, token: str) -> dict: + """ + Decodes a given JWT token. + + Args: + token (str): JWT token. + + Returns: + dict: Decoded JWT token payload. + """ + try: + decoded_payload = jwt.decode( + token, self.secret_key, algorithms=[self.ALGORITHM] + ) + return decoded_payload + except jwt.ExpiredSignatureError: + logger.warning(event="[Auth] JWT token is expired.") + return None + except jwt.InvalidTokenError: + logger.warning(event="[Auth] JWT token is invalid.") + return None diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/auth/request_validators.py b/western_ai/canada/cohere-toolkit/src/backend/services/auth/request_validators.py new file mode 100644 index 0000000000000000000000000000000000000000..668ad1efe4c91663b4fc7a37bd83569479078ac3 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/auth/request_validators.py @@ -0,0 +1,104 @@ +import secrets +from typing import Annotated + +from fastapi import Depends, Header, HTTPException +from fastapi.security import HTTPBasic, HTTPBasicCredentials +from starlette import status + +from backend.config import Settings +from backend.config.settings import SCIMAuth +from backend.database_models import Blacklist +from backend.database_models.database import DBSessionDep +from backend.services.auth.jwt import JWTService + + +def validate_authorization( + session: DBSessionDep, + authorization: Annotated[str|None, Header( + title="Authorization", + description="Authorization header containing Bearer token", + )] = None, +) -> dict: + """ + Validate that the request has the `Authorization` header, used for requests + that require authentication. + + Raises: + HTTPException: If no `Authorization` header. + + Returns: + dict: Decoded payload. + """ + + if not authorization: + raise HTTPException( + status_code=401, + detail="Authorization: Bearer required in request headers.", + ) + + try: + scheme, token = authorization.split(" ") + except ValueError: + raise HTTPException( + status_code=401, + detail="Authorization: Bearer required in request headers.", + ) + + if scheme.lower() != "bearer": + raise HTTPException( + status_code=401, + detail="Authorization: Bearer required in request headers.", + ) + + decoded = JWTService().decode_jwt(token) + + if not decoded or "context" not in decoded: + raise HTTPException( + status_code=401, detail="Bearer token is invalid or expired." + ) + + blacklist = ( + session.query(Blacklist).filter(Blacklist.token_id == decoded["jti"]).first() + ) + + # Token was blacklisted + if blacklist is not None: + raise HTTPException(status_code=401, detail="Bearer token is blacklisted.") + + return decoded + + +class BasicAuthValidation: + def __init__(self, username: str, password: str): + self.username = username + self.password = password + + def __call__( + self, + credentials: Annotated[HTTPBasicCredentials, Depends(HTTPBasic())], + ) -> None: + if not self.username or not self.password: + raise HTTPException( + status_code=500, + detail="username or password is not configured", + headers={"WWW-Authenticate": "Basic"}, + ) + is_correct_username = self._compare_secret(credentials.username, self.username) + is_correct_password = self._compare_secret(credentials.password, self.password) + + if not (is_correct_username and is_correct_password): + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="incorrect username or password", + headers={"WWW-Authenticate": "Basic"}, + ) + + def _compare_secret(self, actual: str, expected: str) -> bool: + return secrets.compare_digest(actual.encode("utf8"), expected.encode("utf8")) + + +class ScimAuthValidation(BasicAuthValidation): + def __init__(self) -> None: + settings = Settings() + scim_auth = settings.auth.scim or SCIMAuth() + super().__init__(username=scim_auth.username, password=scim_auth.password) diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/auth/strategies/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/services/auth/strategies/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/auth/strategies/base.py b/western_ai/canada/cohere-toolkit/src/backend/services/auth/strategies/base.py new file mode 100644 index 0000000000000000000000000000000000000000..67a942262633a813245d3f4210ff91beba414adc --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/auth/strategies/base.py @@ -0,0 +1,89 @@ +from abc import abstractmethod +from typing import Any, List + + +class BaseAuthenticationStrategy: + """ + Base strategy for authentication, abstract class that should be inherited from. + + Attributes: + NAME (str): The name of the strategy. + """ + + NAME = "Base" + + @staticmethod + def get_required_payload(self) -> List[str]: + """ + The required /login payload for the Auth strategy + """ + ... + + @abstractmethod + def login(self, **kwargs: Any): + """ + Check email/password credentials and return JWT token. + """ + ... + + +class BaseOAuthStrategy: + """ + Base strategy for OAuth, abstract class that should be inherited from. + + Attributes: + NAME (str): The name of the strategy. + PKCE_ENABLED (bool): Whether the strategy can use PKCE. + Note: If your auth provider does not support PKCE it could break the auth flow. + """ + + NAME = None + PKCE_ENABLED = False + + def __init__(self, *args, **kwargs): + self._post_init_check() + + def _post_init_check(self): + if any( + [ + self.NAME is None, + ] + ): + raise ValueError(f"{self.__name__} must have NAME attribute defined.") + + @abstractmethod + def get_client_id(self, **kwargs: Any): + """ + Retrieves the OAuth app's client ID + """ + ... + + @abstractmethod + def get_authorization_endpoint(self, **kwargs: Any): + """ + Retrieves the OAuth app's authorization endpoint. + """ + ... + + @abstractmethod + def get_pkce_enabled(self, **kwargs: Any): + """ + Retrieves whether the OAuth app supports PKCE and should be enabled + during authorization. + """ + ... + + @abstractmethod + async def get_endpoints(self, **kwargs: Any): + """ + Retrieves the /token and /userinfo endpoints. + """ + ... + + @abstractmethod + async def authorize(self, **kwargs: Any): + """ + Authentication logic: dealing with user data and returning it + to set the current user session for OAuth strategies. + """ + ... diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/auth/strategies/basic.py b/western_ai/canada/cohere-toolkit/src/backend/services/auth/strategies/basic.py new file mode 100644 index 0000000000000000000000000000000000000000..08a6a08e86d92505175837c942db141def9014e9 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/auth/strategies/basic.py @@ -0,0 +1,81 @@ +from typing import List + +import bcrypt +from sqlalchemy.orm import Session + +from backend.database_models.user import User +from backend.services.auth.strategies.base import BaseAuthenticationStrategy + + +class BasicAuthentication(BaseAuthenticationStrategy): + """ + Basic email/password strategy. + """ + + NAME = "Basic" + + @staticmethod + def get_required_payload() -> List[str]: + """ + Retrieves the required /login payload for the Auth strategy. + + Returns: + List[str]: List of required variables. + """ + return ["email", "password"] + + @staticmethod + def hash_and_salt_password(plain_text_password: str) -> str: + """ + Hashes a given plain-text password with a randomly generated salt. + + Args: + plain_text_password (str): Password to hash. + + Returns: + str: Hashed password + """ + return bcrypt.hashpw(plain_text_password.encode("utf-8"), bcrypt.gensalt()) + + @staticmethod + def check_password(plain_text_password: str, hashed_password: str) -> bool: + """ + Checks that the input plain text password corresponds to a hashed password. + + Args: + plain_text_password (str): Password to check. + hashed_password (str): Password to check against. + + Returns: + bool: Whether the plain-text password matches the given hashed password. + """ + return bcrypt.checkpw(plain_text_password.encode("utf-8"), hashed_password) + + def login(self, session: Session, payload: dict[str, str]) -> dict | None: + """ + Logs user in, checking the if the hashed input password corresponds to the + one stored in the DB. + + Args: + session (Session): DB Session + payload (dict[str, str]): Login payload + + Returns: + dict | None: Returns the user as dict to set the app session, or None. + """ + + payload_email = payload.get("email", "") + payload_password = payload.get("password", "") + user = session.query(User).filter(User.email == payload_email).first() + + if not user: + return None + + if self.check_password(payload_password, user.hashed_password): + return { + "id": user.id, + "fullname": user.fullname, + "email": user.email, + } + + return None diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/auth/strategies/google_oauth.py b/western_ai/canada/cohere-toolkit/src/backend/services/auth/strategies/google_oauth.py new file mode 100644 index 0000000000000000000000000000000000000000..16f7c68b6670a1923528447cbcf5b4bab529ff38 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/auth/strategies/google_oauth.py @@ -0,0 +1,79 @@ +import requests +from authlib.integrations.requests_client import OAuth2Session +from starlette.requests import Request + +from backend.config.settings import Settings +from backend.services.auth.strategies.base import BaseOAuthStrategy +from backend.services.logger.utils import LoggerFactory + +logger = LoggerFactory().get_logger() + + +class GoogleOAuth(BaseOAuthStrategy): + """ + Google OAuth2.0 strategy. + """ + + NAME = "Google" + WELL_KNOWN_ENDPOINT = "https://accounts.google.com/.well-known/openid-configuration" + + def __init__(self): + try: + self.settings = Settings().get('auth.google_oauth') + self.REDIRECT_URI = ( + f"{Settings().get('auth.frontend_hostname')}/auth/{self.NAME.lower()}" + ) + self.client = OAuth2Session( + client_id=self.settings.client_id, + client_secret=self.settings.client_secret, + ) + except Exception as e: + logger.error( + event=f"[Google OAuth] Error during initializing of GoogleOAuth class: {str(e)}" + ) + raise + + def get_client_id(self): + return self.settings.client_id + + def get_authorization_endpoint(self): + if hasattr(self, "AUTHORIZATION_ENDPOINT"): + return self.AUTHORIZATION_ENDPOINT + return None + + def get_pkce_enabled(self): + if hasattr(self, "PKCE_ENABLED"): + return self.PKCE_ENABLED + return False + + async def get_endpoints(self): + response = requests.get(self.WELL_KNOWN_ENDPOINT) + endpoints = response.json() + try: + self.TOKEN_ENDPOINT = endpoints["token_endpoint"] + self.USERINFO_ENDPOINT = endpoints["userinfo_endpoint"] + self.AUTHORIZATION_ENDPOINT = endpoints["authorization_endpoint"] + except Exception: + logger.error( + event=f"[Google OAuth] Error fetching endpoints: `token_endpoint`, `userinfo_endpoint` or `authorization_endpoint` not found in {endpoints}." + ) + raise + + async def authorize(self, request: Request) -> dict | None: + """ + Authenticates the current user using their Google account. + + Args: + request (Request): Current request. + + Returns: + Access token. + """ + _ = self.client.fetch_token( + url=self.TOKEN_ENDPOINT, + authorization_response=str(request.url), + redirect_uri=self.REDIRECT_URI, + ) + user_info = self.client.get(self.USERINFO_ENDPOINT) + + return user_info.json() diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/auth/strategies/oidc.py b/western_ai/canada/cohere-toolkit/src/backend/services/auth/strategies/oidc.py new file mode 100644 index 0000000000000000000000000000000000000000..dac731e1f6b478b1fb61ce90307a5e3d9358fad0 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/auth/strategies/oidc.py @@ -0,0 +1,96 @@ +import requests +from authlib.integrations.requests_client import OAuth2Session +from fastapi import HTTPException +from starlette.requests import Request + +from backend.config.settings import Settings +from backend.services.auth.strategies.base import BaseOAuthStrategy +from backend.services.logger.utils import LoggerFactory + +logger = LoggerFactory().get_logger() + + +class OpenIDConnect(BaseOAuthStrategy): + """ + OpenID Connect strategy. + """ + + NAME = "OIDC" + PKCE_ENABLED = True + + def __init__(self): + try: + self.settings = Settings().get('auth.oidc') + self.REDIRECT_URI = ( + f"{Settings().get('auth.frontend_hostname')}/auth/{self.NAME.lower()}" + ) + self.WELL_KNOWN_ENDPOINT = self.settings.well_known_endpoint + self.client = OAuth2Session( + client_id=self.settings.client_id, + client_secret=self.settings.client_secret, + ) + except Exception as e: + logger.error( + event=f"[OpenIDConnect] Error initializing OpenIDConnect: {str(e)}" + ) + raise + + def get_client_id(self): + return self.settings.client_id + + def get_authorization_endpoint(self): + if hasattr(self, "AUTHORIZATION_ENDPOINT"): + return self.AUTHORIZATION_ENDPOINT + return None + + def get_pkce_enabled(self): + if hasattr(self, "PKCE_ENABLED"): + return self.PKCE_ENABLED + return False + + async def get_endpoints(self): + response = requests.get(self.WELL_KNOWN_ENDPOINT) + endpoints = response.json() + try: + self.TOKEN_ENDPOINT = endpoints["token_endpoint"] + self.USERINFO_ENDPOINT = endpoints["userinfo_endpoint"] + self.AUTHORIZATION_ENDPOINT = endpoints["authorization_endpoint"] + except Exception: + logger.error( + event=f"Error fetching `token_endpoint` and `userinfo_endpoint` from {endpoints}." + ) + raise + + async def authorize(self, request: Request) -> dict | None: + """ + Authenticates the current user using their OIDC account. + + Args: + request (Request): Current request. + + Returns: + Access token. + """ + params = { + "url": self.TOKEN_ENDPOINT, + "authorization_response": str(request.url), + "redirect_uri": self.REDIRECT_URI, + } + + if self.PKCE_ENABLED: + body = await request.json() + code_verifier = body.get("code_verifier") + + if not code_verifier: + raise HTTPException( + status_code=400, + detail="code_verifier not available in request body during PKCE flow.", + ) + + params["code_verifier"] = code_verifier + + _ = self.client.fetch_token(**params) + + user_info = self.client.get(self.USERINFO_ENDPOINT) + + return user_info.json() diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/auth/utils.py b/western_ai/canada/cohere-toolkit/src/backend/services/auth/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..8d1048c2418c9aa455c93d9c5a7e9930a7e41e68 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/auth/utils.py @@ -0,0 +1,111 @@ +from fastapi import HTTPException, Request +from sqlalchemy.orm import Session + +from backend.config.auth import ENABLED_AUTH_STRATEGY_MAPPING, is_authentication_enabled +from backend.crud import user as user_crud +from backend.database_models import User + + +def is_enabled_authentication_strategy(strategy_name: str) -> bool: + """ + Check whether a given authentication strategy is enabled in config/auth.py + + Args: + strategy_name (str): Name the of auth strategy. + + Returns: + bool: Whether that strategy is currently enabled + """ + # Check the strategy is valid and enabled + if strategy_name not in ENABLED_AUTH_STRATEGY_MAPPING.keys(): + return False + + return True + + +def get_or_create_user(session: Session, token_user: dict[str, str]) -> dict: + """ + Gets or creates a user when authenticating them. + + Args: + session (Session): DB session + token_user (dict): Dictionary of user + + + Returns: + User: User object + """ + email = token_user.get("email") + fullname = token_user.get("name") + external_id = token_user.get("sub") + + if external_id: + user = user_crud.get_user_by_external_id(session, external_id) + else: + user = session.query(User).filter(User.email == email).first() + + # Create User if DNE + if not user: + db_user = User(fullname=fullname, email=email, external_id=external_id) + user = user_crud.create_user(session, db_user) + + return { + "id": user.id, + "fullname": user.fullname, + "email": user.email, + } + + +def get_header_user_id(request: Request) -> str: + """ + Retrieves the user_id from request headers, will work whether authentication is enabled or not. + + (Auth disabled): retrieves the User-Id header value + (Auth enabled): retrieves the Authorization header, and decodes the value + + Args: + request (Request): current Request + + + Returns: + str: User ID + """ + # Import here to avoid circular imports + from backend.services.auth.jwt import JWTService + + # Check if Auth enabled + if is_authentication_enabled(): + # Validation already performed, so just retrieve value + authorization = request.headers.get("Authorization") + if not authorization: + raise HTTPException( + status_code=401, + detail="Authorization: Bearer required in request headers.", + ) + _, token = authorization.split(" ") + decoded = JWTService().decode_jwt(token) + + return decoded["context"]["id"] + # Auth disabled + else: + user_id = request.headers.get("User-Id", "") + return user_id + + +def has_header_user_id(request: Request) -> bool: + """ + Check whether we can get the user_id from the request headers. + + Args: + request (Request): current Request + + Returns: + bool: Whether the user_id is present in the headers + """ + + if is_authentication_enabled(): + authorization = request.headers.get("Authorization") + return authorization is not None and authorization.startswith("Bearer") + else: + user_id = request.headers.get("User-Id") + return user_id is not None diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/cache.py b/western_ai/canada/cohere-toolkit/src/backend/services/cache.py new file mode 100644 index 0000000000000000000000000000000000000000..8c4dba4baedf90994096a3a5b0162701054b0d5b --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/cache.py @@ -0,0 +1,48 @@ +from typing import Any + +from redis import Redis + +from backend.config.settings import Settings +from backend.services.logger.utils import LoggerFactory + +logger = LoggerFactory().get_logger() + + +def get_client() -> Redis: + redis_url = Settings().get('redis.url') + + if not redis_url: + error = "Tried retrieving Redis client but redis.url in configuration.yaml is not set." + logger.error(event=error) + raise ValueError(error) + + client = Redis.from_url(redis_url, decode_responses=True) + + return client + + +def cache_put(key: str, value: Any) -> None: + client = get_client() + + if isinstance(value, dict): + client.hset(key, mapping=value) + else: + client.set(key, value) + + +def cache_get(key: str) -> Any: + client = get_client() + + return client.get(key) + + +def cache_get_dict(key: str) -> dict: + client = get_client() + + return client.hgetall(key) + + +def cache_del(key: str) -> None: + client = get_client() + + client.delete(key) diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/chat.py b/western_ai/canada/cohere-toolkit/src/backend/services/chat.py new file mode 100644 index 0000000000000000000000000000000000000000..a7cf74472a4f98f77357de675586b6ee8200f36f --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/chat.py @@ -0,0 +1,1059 @@ +import json +from typing import Any, AsyncGenerator, Dict, Generator, List, Union +from uuid import uuid4 + +import nltk +from cohere.types import StreamedChatResponse +from fastapi import HTTPException +from fastapi.encoders import jsonable_encoder + +from backend.chat.collate import to_dict +from backend.chat.enums import StreamEvent +from backend.config.tools import get_available_tools +from backend.crud import agent_tool_metadata as agent_tool_metadata_crud +from backend.crud import conversation as conversation_crud +from backend.crud import message as message_crud +from backend.crud import tool_call as tool_call_crud +from backend.database_models.citation import Citation +from backend.database_models.conversation import Conversation +from backend.database_models.database import DBSessionDep +from backend.database_models.document import Document +from backend.database_models.message import ( + Message, + MessageAgent, + MessageFileAssociation, +) +from backend.database_models.tool_call import ToolCall as ToolCallModel +from backend.schemas import CohereChatRequest +from backend.schemas.agent import Agent, AgentToolMetadata +from backend.schemas.chat import ( + BaseChatRequest, + ChatMessage, + ChatResponseEvent, + ChatRole, + EventState, + NonStreamedChatResponse, + StreamCitationGeneration, + StreamEnd, + StreamEventType, + StreamSearchQueriesGeneration, + StreamSearchResults, + StreamStart, + StreamTextGeneration, + StreamToolCallsChunk, + StreamToolCallsGeneration, +) +from backend.schemas.context import Context +from backend.schemas.conversation import UpdateConversationRequest +from backend.schemas.search_query import SearchQuery +from backend.schemas.tool import Tool, ToolCall, ToolCallDelta +from backend.services.agent import validate_agent_exists + +LOOKBACKS = [3, 5, 7] +DEATHLOOP_SIMILARITY_THRESHOLDS = [0.5, 0.7, 0.9] + + +def generate_tools_preamble(chat_request: CohereChatRequest) -> str: + available_tools = get_available_tools() + full_managed_tools = [ + available_tools.get(tool.name) + for tool in chat_request.tools + if available_tools.get(tool.name) + ] + tools_preamble = "" + if full_managed_tools: + tools_preamble = " ".join(tool.implementation.TOOL_DEFAULT_PREAMBLE for tool in full_managed_tools if + tool.implementation.TOOL_DEFAULT_PREAMBLE) + passed_preamble = "" + if chat_request.preamble: + passed_preamble = chat_request.preamble.replace("## Task And Context", "") + tools_preamble = f"## Task And Context\n{tools_preamble}{passed_preamble}" + + return tools_preamble + + +def process_chat( + session: DBSessionDep, + chat_request: CohereChatRequest, + ctx: Context, +) -> tuple[ + DBSessionDep, CohereChatRequest, Union[list[str], None], Message, str, str, Context +]: + """ + Process a chat request. + + Args: + chat_request (CohereChatRequest): Chat request data. + session (DBSessionDep): Database session. + ctx (Context): Context object. + + Returns: + Tuple: Tuple containing necessary data to construct the responses. + """ + user_id = ctx.get_user_id() + ctx.with_deployment_config() + agent_id = ctx.get_agent_id() + + if agent_id: + agent = validate_agent_exists(session, agent_id, user_id) + agent_schema = Agent.model_validate(agent) + ctx.with_agent(agent_schema) + + if agent is None: + raise HTTPException( + status_code=404, detail=f"Agent with ID {agent_id} not found." + ) + + agent_tool_metadata = ( + agent_tool_metadata_crud.get_all_agent_tool_metadata_by_agent_id( + session, agent_id + ) + ) + agent_tool_metadata_schema = [ + AgentToolMetadata.model_validate(x) for x in agent_tool_metadata + ] + ctx.with_agent_tool_metadata(agent_tool_metadata_schema) + + # if tools are not provided in the chat request, use the agent's tools + if not chat_request.tools: + chat_request.tools = [Tool(name=tool) for tool in agent.tools] + + # Set the agent settings in the chat request + chat_request.model = agent.model + chat_request.preamble = agent.preamble + + # If temperature is not defined in the chat request, use the temperature from the agent + if not chat_request.temperature: + chat_request.temperature = agent.temperature + + should_store = chat_request.chat_history is None and not is_custom_tool_call( + chat_request + ) + conversation = get_or_create_conversation( + session, chat_request, user_id, should_store, agent_id, chat_request.message + ) + + ctx.with_conversation_id(conversation.id) + + # Get position to put next message in + next_message_position = get_next_message_position(conversation) + user_message = create_message( + session, + chat_request, + conversation.id, + user_id, + next_message_position, + chat_request.message, + MessageAgent.USER, + should_store, + id=str(uuid4()), + ) + chatbot_message = create_message( + session, + chat_request, + conversation.id, + user_id, + next_message_position, + "", + MessageAgent.CHATBOT, + False, + id=str(uuid4()), + ) + + if should_store: + attach_files_to_messages( + session, + user_id, + user_message.id, + chat_request.file_ids + ) + + chat_history = create_chat_history( + conversation, next_message_position, chat_request + ) + + # co.chat expects either chat_history or conversation_id, not both + chat_request.chat_history = chat_history + chat_request.conversation_id = "" + + tools = chat_request.tools + managed_tools = ( + len([tool.name for tool in tools if tool.name in get_available_tools()]) > 0 + ) + + return ( + session, + chat_request, + chatbot_message, + should_store, + managed_tools, + next_message_position, + ctx, + ) + + +def process_message_regeneration( + session: DBSessionDep, + chat_request: CohereChatRequest, + ctx: Context, +) -> tuple[Any, CohereChatRequest, Message, list[str], bool, Context]: + """ + Process message regeneration. + + Args: + session (DBSessionDep): Database session. + chat_request (CohereChatRequest): Chat request data. + ctx (Context): Context object. + + Returns: + Tuple: Tuple containing necessary data to regenerate message. + """ + ctx.with_deployment_config() + + user_id = ctx.get_user_id() + agent_id = ctx.get_agent_id() + + if agent_id: + agent = validate_agent_exists(session, agent_id, user_id) + ctx.with_agent(Agent.model_validate(agent)) + + # if tools are not provided in the chat request, use the agent's tools + if not chat_request.tools: + chat_request.tools = [Tool(name=tool) for tool in agent.tools] + + # Set the agent settings in the chat request + chat_request.preamble = agent.preamble + + # If temperature is not defined in the chat request, use the temperature from the agent + if not chat_request.temperature: + chat_request.temperature = agent.temperature + + conversation_id = chat_request.conversation_id + ctx.with_conversation_id(conversation_id) + + conversation = conversation_crud.get_conversation(session, conversation_id, user_id) + if not conversation: + raise HTTPException( + status_code=404, + detail=f"Conversation with ID: {conversation_id} not found." + ) + + last_user_message = get_last_message(conversation, user_id, MessageAgent.USER) + + attach_files_to_messages( + session, + user_id, + last_user_message.id, + chat_request.file_ids + ) + + new_chatbot_message = create_message( + session, + chat_request, + conversation.id, + user_id, + last_user_message.position, + "", + MessageAgent.CHATBOT, + False, + id=str(uuid4()), + ) + + previous_chatbot_message_ids = [ + message.id + for message in conversation.messages + if ( + message.is_active + and message.user_id == user_id + and message.position == last_user_message.position + and message.agent == MessageAgent.CHATBOT + ) + ] + + chat_request.message = last_user_message.text + chat_request.conversation_id = "" + chat_request.chat_history = create_chat_history( + conversation, last_user_message.position, chat_request + ) + + managed_tools = ( + len([tool.name for tool in chat_request.tools if tool.name in get_available_tools()]) > 0 + ) + + return ( + session, + chat_request, + new_chatbot_message, + previous_chatbot_message_ids, + managed_tools, + ctx + ) + + +def get_last_message( + conversation: Conversation, user_id: str, agent: MessageAgent +) -> Message: + """ + Retrieve the last message sent by a specific agent within a given conversation. + + Args: + conversation (Conversation): The conversation containing the messages. + user_id (str): The user ID. + agent (MessageAgent): The agent whose last message is to be retrieved. + + Returns: + Message: The last message sent by the agent. + + Raises: + HTTPException: If there are no messages from the specified agent in the conversation. + """ + agent_messages = [ + message + for message in conversation.messages + if message.is_active and message.user_id == user_id and message.agent == agent + ] + + if not agent_messages: + raise HTTPException( + status_code=404, + detail=f"Messages for user with ID: {user_id} not found.", + ) + + return agent_messages[-1] + + +def is_custom_tool_call(chat_response: BaseChatRequest) -> bool: + """ + Check if the chat request is called with custom tools + + Args: + chat_response (BaseChatRequest): Chat request data. + + Returns: + bool: Whether the chat request is called with custom tools. + """ + if chat_response.tools is None or len(chat_response.tools) == 0: + return False + + # check if any of the tools is not in the available tools + for tool in chat_response.tools: + if tool.name not in get_available_tools(): + return True + + return False + + +def get_or_create_conversation( + session: DBSessionDep, + chat_request: BaseChatRequest, + user_id: str, + should_store: bool, + agent_id: str | None = None, + user_message: str = "", +) -> Conversation: + """ + Gets or creates a Conversation based on the chat request. + + Args: + session (DBSessionDep): Database session. + chat_request (BaseChatRequest): Chat request data. + user_id (str): User ID. + should_store (bool): Whether to store the conversation in the database. + + Returns: + Conversation: Conversation object. + """ + conversation_id = chat_request.conversation_id or "" + conversation = conversation_crud.get_conversation(session, conversation_id, user_id) + if conversation is None: + # Get the first 5 words of the user message as the title + title = " ".join(user_message.split()[:5]) + + conversation = Conversation( + user_id=user_id, + id=chat_request.conversation_id, + agent_id=agent_id, + title=title, + ) + + if should_store: + conversation_crud.create_conversation(session, conversation) + + return conversation + + +def get_next_message_position(conversation: Conversation) -> int: + """ + Gets message position to create next messages. + + Args: + conversation (Conversation): current Conversation. + + Returns: + int: Position to save new messages with + """ + + # Message starts the conversation + if len(conversation.messages) == 0: + return 0 + + # Get current max position from existing Messages + current_active_position = max( + [message.position for message in conversation.messages if message.is_active] + ) + + return current_active_position + 1 + + +def create_message( + session: DBSessionDep, + chat_request: BaseChatRequest, + conversation_id: str, + user_id: str, + user_message_position: int, + text: str | None = None, + agent: MessageAgent = MessageAgent.USER, + should_store: bool = True, + id: str | None = None, + tool_plan: str | None = None, +) -> Message: + """ + Create a message object and store it in the database. + + Args: + session (DBSessionDep): Database session. + chat_request (BaseChatRequest): Chat request data. + conversation_id (str): Conversation ID. + user_id (str): User ID. + user_message_position (int): User message position. + id (str): Message ID. + text (str): Message text. + agent (MessageAgent): Message agent. + should_store (bool): Whether to store the message in the database. + + Returns: + Message: Message object. + """ + if not id: + id = str(uuid4()) + + message = Message( + id=id, + user_id=user_id, + conversation_id=conversation_id, + text=text, + position=user_message_position, + is_active=True, + agent=agent, + tool_plan=tool_plan, + ) + + if should_store: + return message_crud.create_message(session, message) + return message + + +def attach_files_to_messages( + session: DBSessionDep, + user_id: str, + message_id: str, + file_ids: List[str] | None = None, +) -> None: + """ + Attach Files to Message if the message file association does not exists with the file ID + + Args: + session (DBSessionDep): Database session. + user_id (str): User ID. + message_id (str): Message ID to attach to if needed. + file_ids (List): List of File IDs. + + Returns: + None + """ + if file_ids is not None: + for file_id in file_ids: + message_file_association = ( + message_crud.get_message_file_association_by_file_id( + session, file_id, user_id + ) + ) + + # If the file is not associated with a file yet, create the association + if message_file_association is None: + message_crud.create_message_file_association( + session, + MessageFileAssociation( + message_id=message_id, user_id=user_id, file_id=file_id + ), + ) + + +def create_chat_history( + conversation: Conversation, + user_message_position: int, + chat_request: BaseChatRequest, +) -> list[ChatMessage]: + """ + Create chat history from conversation messages or request, this chat history + is sent to the chat SDK call for added context. + + Args: + conversation (Conversation): Conversation object. + user_message_position (int): User message position. + chat_request (BaseChatRequest): Chat request data. + + Returns: + list[ChatMessage]: List of chat messages. + """ + if chat_request.chat_history is not None: + return chat_request.chat_history + + if conversation.messages is None: + return [] + + # Filter out user message that was just sent + # And any empty messages + text_messages = [ + message + for message in conversation.messages + if message.position < user_message_position + and message.text + ] + return [ + ChatMessage( + role=ChatRole(message.agent.value.upper()), + message=message.text, + ) + for message in text_messages + ] + + +def update_conversation_after_turn( + session: DBSessionDep, + response_message: Message, + conversation_id: str, + final_message_text: str, + user_id: str, + previous_response_message_ids: list[str] | None = None, +) -> None: + """ + After the last message in a conversation, updates the conversation description with that message's text + + Args: + session (DBSessionDep): Database session. + response_message (Message): Response message object. + conversation_id (str): Conversation ID. + final_message_text (str): Final message text. + user_id (str): The user ID. + previous_response_message_ids (list[str]): Previous response message IDs. + """ + if previous_response_message_ids: + message_crud.delete_messages(session, previous_response_message_ids, user_id) + + message_crud.create_message(session, response_message) + + # Update conversation description with final message + conversation = conversation_crud.get_conversation(session, conversation_id, user_id) + new_conversation = UpdateConversationRequest( + description=final_message_text, + user_id=conversation.user_id, + ) + conversation_crud.update_conversation(session, conversation, new_conversation) + + +def save_tool_calls_message( + session: DBSessionDep, + tool_calls: List[ToolCall], + text: str, + user_id: str, + position: int, + conversation_id: str, +) -> None: + """ + Save tool calls to the database. + + Args: + session (DBSessionDep): Database session. + tool_calls (List[ToolCall]): List of ToolCall objects. + message (str): Message text. + position (int): Message position. + """ + # Save message to the database + message = create_message( + session, + chat_request=None, + conversation_id=conversation_id, + user_id=user_id, + user_message_position=position, + text=text, + tool_plan=text, + agent=MessageAgent.CHATBOT, + should_store=True, + ) + + # Save tool calls to the database + for tool_call in tool_calls: + tool_call = ToolCallModel( + name=tool_call.name, + parameters=to_dict(tool_call.parameters), + message_id=message.id, + ) + tool_call_crud.create_tool_call(session, tool_call) + + +async def generate_chat_response( + session: DBSessionDep, + model_deployment_stream: Generator[StreamedChatResponse, None, None], + response_message: Message, + should_store: bool = True, + ctx: Context = Context(), + **kwargs: Any, +) -> NonStreamedChatResponse: + """ + Generate chat response from model deployment non streaming response. + Use the stream to generate the response and all the intermediate steps, then + return only the final step as a non-streamed response. + + Args: + session (DBSessionDep): Database session. + model_deployment_stream (Generator[StreamResponse, None, None]): Model deployment stream. + response_message (Message): Response message object. + should_store (bool): Whether to store the conversation in the database. + ctx (Context): Context object. + **kwargs (Any): Additional keyword arguments. + + Yields: + bytes: Byte representation of chat response event. + """ + stream = generate_chat_stream( + session, + model_deployment_stream, + response_message, + should_store, + ctx, + **kwargs, + ) + + non_streamed_chat_response = None + async for event in stream: + event = json.loads(event) + if event["event"] == StreamEvent.STREAM_END: + data = event["data"] + response_id = ctx.get_trace_id() + generation_id = response_message.generation_id if response_message else None + + non_streamed_chat_response = NonStreamedChatResponse( + text=data.get("text", ""), + response_id=response_id, + generation_id=generation_id, + chat_history=data.get("chat_history", []), + finish_reason=data.get("finish_reason", ""), + citations=data.get("citations", []), + search_queries=data.get("search_queries", []), + documents=data.get("documents", []), + search_results=data.get("search_results", []), + event_type=StreamEvent.NON_STREAMED_CHAT_RESPONSE, + conversation_id=ctx.get_conversation_id(), + tool_calls=data.get("tool_calls", []), + error=data.get("error", None), + ) + + return non_streamed_chat_response + + +async def generate_chat_stream( + session: DBSessionDep, + model_deployment_stream: AsyncGenerator[Any, Any], + response_message: Message, + should_store: bool = True, + ctx: Context = Context(), + **kwargs: Any, +) -> AsyncGenerator[Any, Any]: + """ + Generate chat stream from model deployment stream. + + Args: + session (DBSessionDep): Database session. + model_deployment_stream (AsyncGenerator[Any, Any]): Model deployment stream. + response_message (Message): Response message object. + conversation_id (str): Conversation ID. + user_id (str): User ID. + should_store (bool): Whether to store the conversation in the database. + ctx (Context): Context object. + **kwargs (Any): Additional keyword arguments. + + Yields: + bytes: Byte representation of chat response event. + """ + conversation_id = ctx.get_conversation_id() + user_id = ctx.get_user_id() + + stream_end_data = { + "message_id": response_message.id, + "conversation_id": conversation_id, + "response_id": ctx.get_trace_id(), + "text": "", + "citations": [], + "documents": [], + "search_results": [], + "search_queries": [], + "tool_calls": [], + "tool_results": [], + } + + # Map the user facing document_ids field returned from model to storage ID for document model + document_ids_to_document = {} + + stream_event = None + async for event in model_deployment_stream: + ( + stream_event, + stream_end_data, + response_message, + document_ids_to_document, + ) = handle_stream_event( + event, + conversation_id, + stream_end_data, + response_message, + ctx, + document_ids_to_document, + session=session, + should_store=should_store, + user_id=user_id, + next_message_position=kwargs.get("next_message_position", 0), + ) + + yield json.dumps( + jsonable_encoder( + ChatResponseEvent( + event=stream_event.event_type.value, + data=stream_event, + ) + ) + ) + + if should_store: + update_conversation_after_turn( + session, + response_message, + conversation_id, + stream_end_data["text"], + user_id, + kwargs.get("previous_response_message_ids") + ) + + +def handle_stream_event( + event: dict[str, Any], + conversation_id: str, + stream_end_data: dict[str, Any], + response_message: Message, + ctx: Context, + document_ids_to_document: dict[str, Document] = {}, + session: DBSessionDep = None, + should_store: bool = True, + user_id: str = "", + next_message_position: int = 0, +) -> tuple[StreamEventType, dict[str, Any], Message, dict[str, Document]]: + logger = ctx.get_logger() + + handlers = { + StreamEvent.STREAM_START: handle_stream_start, + StreamEvent.TEXT_GENERATION: handle_stream_text_generation, + StreamEvent.SEARCH_RESULTS: handle_stream_search_results, + StreamEvent.SEARCH_QUERIES_GENERATION: handle_stream_search_queries_generation, + StreamEvent.TOOL_CALLS_GENERATION: handle_stream_tool_calls_generation, + StreamEvent.CITATION_GENERATION: handle_stream_citation_generation, + StreamEvent.TOOL_CALLS_CHUNK: handle_stream_tool_calls_chunk, + StreamEvent.STREAM_END: handle_stream_end, + } + event_type = event["event_type"] + + if event_type not in handlers.keys(): + logger.warning( + event=f"[Chat] Error handling stream event: Event type {event_type} not supported" + ) + return None, stream_end_data, response_message, document_ids_to_document + + return handlers[event_type]( + event, + conversation_id, + stream_end_data, + response_message, + document_ids_to_document, + session=session, + should_store=should_store, + user_id=user_id, + next_message_position=next_message_position, + ) + + +def handle_stream_start( + event: dict[str, Any], + conversation_id: str, + stream_end_data: dict[str, Any], + response_message: Message, + document_ids_to_document: dict[str, Document], + **kwargs: Any, +) -> tuple[StreamStart, dict[str, Any], Message, dict[str, Document]]: + event["conversation_id"] = conversation_id + stream_event = StreamStart.model_validate(event) + if response_message: + response_message.generation_id = event["generation_id"] + stream_end_data["generation_id"] = event["generation_id"] + return stream_event, stream_end_data, response_message, document_ids_to_document + + +def handle_stream_text_generation( + event: dict[str, Any], + _: str, + stream_end_data: dict[str, Any], + response_message: Message, + document_ids_to_document: dict[str, Document], + **kwargs: Any, +) -> tuple[StreamTextGeneration, dict[str, Any], Message, dict[str, Document]]: + stream_end_data["text"] += event["text"] + stream_event = StreamTextGeneration.model_validate(event) + return stream_event, stream_end_data, response_message, document_ids_to_document + + +def handle_stream_search_results( + event: dict[str, Any], + _: str, + stream_end_data: dict[str, Any], + response_message: Message, + document_ids_to_document: dict[str, Document], + **kwargs: Any, +) -> tuple[StreamSearchResults, dict[str, Any], Message, dict[str, Document]]: + for document in event["documents"]: + storage_document = Document( + document_id=document.get("id", ""), + text=document.get("text", ""), + title=document.get("title", ""), + url=document.get("url", ""), + tool_name=document.get("tool_name", ""), + # all document fields except for id, tool_name and text + fields={ + k: v + for k, v in document.items() + if k not in ["id", "tool_name", "text"] + }, + user_id=response_message.user_id, + conversation_id=response_message.conversation_id, + message_id=response_message.id, + ) + document_ids_to_document[document["id"]] = storage_document + + documents = list(document_ids_to_document.values()) + response_message.documents = documents + + stream_end_data["documents"].extend(documents) + if "search_results" not in event or event["search_results"] is None: + event["search_results"] = [] + + stream_event = StreamSearchResults( + **event + | { + "documents": documents, + "search_results": event["search_results"], + }, + ) + stream_end_data["search_results"].extend(event["search_results"]) + return stream_event, stream_end_data, response_message, document_ids_to_document + + +def handle_stream_search_queries_generation( + event: dict[str, Any], + _: str, + stream_end_data: dict[str, Any], + response_message: Message, + document_ids_to_document: dict[str, Document], + **kwargs: Any, +) -> tuple[StreamSearchQueriesGeneration, dict[str, Any], Message, dict[str, Document]]: + search_queries = [] + for search_query in event["search_queries"]: + search_queries.append( + SearchQuery( + text=search_query.get("text", ""), + generation_id=search_query.get("generation_id", ""), + ) + ) + stream_event = StreamSearchQueriesGeneration( + **event | {"search_queries": search_queries} + ) + stream_end_data["search_queries"] = search_queries + return stream_event, stream_end_data, response_message, document_ids_to_document + + +def handle_stream_tool_calls_generation( + event: dict[str, Any], + conversation_id: str, + stream_end_data: dict[str, Any], + response_message: Message, + document_ids_to_document: dict[str, Document], + session: DBSessionDep, + should_store: bool, + user_id: str, + next_message_position: int, +) -> tuple[StreamToolCallsGeneration, dict[str, Any], Message, dict[str, Document]]: + tool_calls = [] + tool_calls_event = event.get("tool_calls", []) + for tool_call in tool_calls_event: + tool_calls.append( + ToolCall( + name=tool_call.get("name"), + parameters=tool_call.get("parameters"), + ) + ) + stream_event = StreamToolCallsGeneration(**event | {"tool_calls": tool_calls}) + stream_end_data["tool_calls"].extend(tool_calls) + + if should_store: + save_tool_calls_message( + session, + tool_calls, + event.get("text", ""), + user_id, + next_message_position, + conversation_id, + ) + + return stream_event, stream_end_data, response_message, document_ids_to_document + + +def handle_stream_citation_generation( + event: dict[str, Any], + _: str, + stream_end_data: dict[str, Any], + response_message: Message, + document_ids_to_document: dict[str, Document], + **kwargs: Any, +) -> tuple[StreamCitationGeneration, dict[str, Any], Message, dict[str, Document]]: + citations = [] + for event_citation in event["citations"]: + citation = Citation( + text=event_citation.get("text"), + user_id=response_message.user_id, + start=event_citation.get("start"), + end=event_citation.get("end"), + ) + + document_ids = event_citation.get("document_ids") + for document_id in document_ids: + document = document_ids_to_document.get(document_id, None) + if document is not None: + citation.documents.append(document) + + # Populates CitationDocuments table + citations.append(citation) + stream_event = StreamCitationGeneration(**event | {"citations": citations}) + stream_end_data["citations"].extend(citations) + return stream_event, stream_end_data, response_message, document_ids_to_document + + +def handle_stream_tool_calls_chunk( + event: dict[str, Any], + _: str, + stream_end_data: dict[str, Any], + response_message: Message, + document_ids_to_document: dict[str, Document], + **kwargs: Any, +) -> tuple[StreamToolCallsChunk, dict[str, Any], Message, dict[str, Document]]: + event["text"] = event.get("text", "") + tool_call_delta = event.get("tool_call_delta", None) + if tool_call_delta: + tool_call = ToolCallDelta( + name=tool_call_delta.get("name"), + index=tool_call_delta.get("index"), + parameters=tool_call_delta.get("parameters"), + ) + event["tool_call_delta"] = tool_call + + stream_event = StreamToolCallsChunk.model_validate(event) + return stream_event, stream_end_data, response_message, document_ids_to_document + + +def handle_stream_end( + event: dict[str, Any], + _: str, + stream_end_data: dict[str, Any], + response_message: Message, + document_ids_to_document: dict[str, Document], + **kwargs: Any, +) -> tuple[StreamEnd, dict[str, Any], Message, dict[str, Document]]: + if response_message: + response_message.citations = stream_end_data["citations"] + response_message.text = stream_end_data["text"] + + stream_end_data["chat_history"] = ( + to_dict(event).get("response", {}).get("chat_history", []) + ) + stream_end = StreamEnd.model_validate(event | stream_end_data) + stream_event = stream_end + return stream_event, stream_end_data, response_message, document_ids_to_document + +def are_previous_actions_similar( + distances: List[float], threshold: float, lookback: int +) -> bool: + return all(dist > threshold for dist in distances[-lookback:]) + + +def check_similarity(distances: list[float], ctx: Context) -> bool: + """ + Check if the previous actions are similar to detect a potential death loop. + + Args: + distances (list[float]): List of distances between previous actions. + + Raises: + HTTPException: If a potential death loop is detected. + """ + logger = ctx.get_logger() + + if len(distances) < min(LOOKBACKS): + return False + + # EXPERIMENTAL: Check for potential death loops with different thresholds and lookbacks + for threshold in DEATHLOOP_SIMILARITY_THRESHOLDS: + for lookback in LOOKBACKS: + if are_previous_actions_similar(distances, threshold, lookback): + logger.warning( + event="[Chat] Potential death loop detected", + distances=distances, + threshold=threshold, + lookback=lookback, + ) + return True + + return False + + +def check_death_loop( + event: Dict[str, Any], event_state: EventState, ctx: Context +) -> EventState: + plan: str = event.get("text", "") + tool_calls: List = event.get("tool_calls", []) + action: str = json.dumps(tool_calls) + + if event_state.previous_action: + event_state.distances_actions.append( + 1 + - nltk.edit_distance(event_state.previous_action, action) + / max(len(str(event_state.previous_action)), len(action)) + ) + check_similarity(event_state.distances_actions, ctx) + + if event_state.previous_plan: + event_state.distances_plans.append( + 1 + - nltk.edit_distance(event_state.previous_plan, plan) + / max(len(str(event_state.previous_plan)), len(plan)) + ) + check_similarity(event_state.distances_plans, ctx) + + event_state.previous_plan = plan + event_state.previous_action = action + + return event_state diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/context.py b/western_ai/canada/cohere-toolkit/src/backend/services/context.py new file mode 100644 index 0000000000000000000000000000000000000000..7435aafb74e2a2fdd9b02b37cc01be5459515f14 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/context.py @@ -0,0 +1,73 @@ +import contextvars +import uuid + +from fastapi import Request +from starlette.types import ASGIApp, Receive, Scope, Send + +from backend.schemas.context import Context + +GLOBAL_REQUEST_CONTEXT = contextvars.ContextVar("GLOBAL_REQUEST_CONTEXT", default=None) + + +class ContextMiddleware: + def __init__(self, app: ASGIApp): + self.app = app + + async def __call__(self, scope: Scope, receive: Receive, send: Send): + # Import here to avoid circular imports + from backend.schemas.user import DEFAULT_USER_ID + from backend.services.auth.utils import get_header_user_id, has_header_user_id + + trace_id = str(uuid.uuid4()) + + if scope["type"] != "http": + await self.app(scope, receive, send) + return + + # Create a new context for the request + context = Context() + context.set_request(scope) + context.set_response(send) + context.set_receive(receive) + context.with_trace_id(trace_id) + + request = Request(scope) + context.with_deployment_name(request.headers.get("Deployment-Name", "")) + + if has_header_user_id(request): + user_id = get_header_user_id(request) + else: + user_id = DEFAULT_USER_ID + + context.with_user_id(user_id) + + agent_id = request.headers.get("Agent-Id") + context.with_agent_id(agent_id) + + organization_id = request.headers.get("Organization-Id", None) + context.with_organization_id(organization_id) + + context.without_global_filtering() + if organization_id: + context.with_global_filtering() + + context.with_logger() + + # Set the context on the scope + scope["context"] = context + GLOBAL_REQUEST_CONTEXT.set(context) + + await self.app(scope, receive, send) + + # Clear the organization ID from the global context + GLOBAL_REQUEST_CONTEXT.set(None) + # Clear the context after the request is complete + del scope["context"] + + +def get_context_from_scope(scope: Scope) -> Context: + return scope.get("context") or Context() + + +def get_context(request: Request) -> Context: + return get_context_from_scope(request.scope) diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/conversation.py b/western_ai/canada/cohere-toolkit/src/backend/services/conversation.py new file mode 100644 index 0000000000000000000000000000000000000000..d8dff345295ef0b0574dced00e3be14842119bfb --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/conversation.py @@ -0,0 +1,287 @@ +import uuid +from typing import Optional + +from fastapi import HTTPException + +from backend.chat.custom.custom import CustomChat +from backend.crud import conversation as conversation_crud +from backend.database_models import Message as MessageModel +from backend.database_models.conversation import Conversation as ConversationModel +from backend.database_models.database import DBSessionDep +from backend.database_models.message import MessageAgent +from backend.model_deployments.base import BaseDeployment +from backend.schemas.chat import ChatRole +from backend.schemas.cohere_chat import CohereChatRequest +from backend.schemas.context import Context +from backend.schemas.conversation import Conversation +from backend.schemas.message import Message +from backend.services.chat import create_message, generate_chat_response +from backend.services.file import attach_conversation_id_to_files, get_file_service + +DEFAULT_TITLE = "New Conversation" +GENERATE_TITLE_PROMPT = """# TASK +Given the following conversation history, write a short title that summarizes the topic of the conversation. Be concise and respond with just the title. + +## START CHATLOG +%s +## END CHATLOG + +# TITLE +""" +SEARCH_RELEVANCE_THRESHOLD = 0.3 + + +def validate_conversation( + session: DBSessionDep, conversation_id: str, user_id: str +) -> Conversation: + """Validates if a conversation exists and belongs to the user + + Args: + session (DBSessionDep): Database session + conversation_id (str): Conversation ID + user_id (str): User ID + + Returns: + ConversationModel: Conversation object + + Raises: + HTTPException: If the conversation is not found + """ + conversation = conversation_crud.get_conversation(session, conversation_id, user_id) + if not conversation: + raise HTTPException( + status_code=404, + detail=f"Conversation with ID: {conversation_id} not found.", + ) + return conversation + + +def extract_details_from_conversation( + convo: Conversation, + num_turns: int = 5, + ignore_system: str = True, + ignore_tool: str = True, +) -> str: + """ + Extracts the last num_turns from a conversation, ignoring system and tool messages + + Args: + convo (Conversation): The conversation object + num_turns (int): The number of turns to extract + ignore_system (bool): Whether to ignore system messages + ignore_tool (bool): Whether to ignore tool messages + + Returns: + str: The extracted chatlog + """ + messages = convo.messages + len_messages = len(messages) + num_turns = min(len_messages, num_turns) + start_turn = len_messages - num_turns + + turns = [] + for i in range(start_turn, len_messages): + message = messages[i] + + # Ignore tool messages + if ignore_tool and message.agent == ChatRole.TOOL: + continue + + if ignore_system and message.agent == ChatRole.SYSTEM: + continue + + # : + turn_str = message.agent + ": " + message.text + turns.append(turn_str) + + chatlog = "\n".join(turns) + return chatlog + + +def get_messages_with_files( + session: DBSessionDep, user_id: str, messages: list[MessageModel], ctx: Context +) -> list[Message]: + """ + Get messages and use the file service to get the files associated with each message + + Args: + session (DBSessionDep): The database session + user_id (str): The user ID + messages (list[MessageModel]): The messages to get files for + + Returns: + list[Message]: The messages with files + """ + messages_with_file = [] + + for message in messages: + files = get_file_service().get_files_by_message_id( + session, message.id, user_id, ctx + ) + files_with_conversation_id = attach_conversation_id_to_files( + message.conversation_id, files + ) + messages_with_file.append( + Message( + id=message.id, + text=message.text, + created_at=message.created_at, + updated_at=message.updated_at, + generation_id=message.generation_id, + position=message.position, + is_active=message.is_active, + files=files_with_conversation_id, + documents=message.documents, + citations=message.citations, + tool_calls=message.tool_calls, + tool_plan=message.tool_plan, + agent=message.agent, + ) + ) + + return messages_with_file + + +def get_documents_to_rerank(conversations: list[Conversation]) -> list[str]: + """Get documents (strings) to rerank from a list of conversations + + Args: + conversations (List[Conversation]): List of conversations + + Returns: + List[str]: List of documents to rerank + """ + rerank_documents = [] + for conversation in conversations: + chatlog = extract_details_from_conversation(conversation) + + document = f"Title: {conversation.title}\n" + if len(chatlog.strip()) != 0: + document += "\nChatlog:\n{chatlog}" + + rerank_documents.append(document) + + return rerank_documents + + +async def filter_conversations( + query: str, + conversations: list[Conversation], + rerank_documents: list[str], + model_deployment: BaseDeployment, + ctx: Context, +) -> list[Conversation]: + """Filter conversations based on the rerank score + + Args: + query (str): The query to filter conversations + conversations (list[Conversation]): List of conversations + rerank_documents (list[str]): List of documents to rerank + model_deployment: Model deployment object + ctx (Context): Context object + + Returns: + list[Conversation]: List of filtered conversations + """ + # if rerank is not enabled, filter out conversations that don't contain the query + if not model_deployment.rerank_enabled(): + filtered_conversations = [] + + for rerank_document, conversation in zip(rerank_documents, conversations): + if query.lower() in rerank_document.lower(): + filtered_conversations.append(conversation) + + return filtered_conversations + + # Rerank documents + res = await model_deployment.invoke_rerank( + query=query, + documents=rerank_documents, + ctx=ctx, + ) + + # Sort conversations by rerank score + res["results"].sort(key=lambda x: x["relevance_score"], reverse=True) + + # Filter out conversations with low relevance score + reranked_conversations = [ + conversations[r["index"]] + for r in res["results"] + if r["relevance_score"] > SEARCH_RELEVANCE_THRESHOLD + ] + + return reranked_conversations + + +async def generate_conversation_title( + session: DBSessionDep, + conversation: ConversationModel, + agent_id: str, + ctx: Context, + model: Optional[str] = None, +) -> tuple[str, str]: + """Generate a title for a conversation + + Args: + request: Request object + session: Database session + conversation: Conversation object + model_config: Model configuration + agent_id: Agent ID + ctx: Context object + model: Model name + + Returns: + str: Generated title + str: Error message + """ + user_id = ctx.get_user_id() + logger = ctx.get_logger() + title = "" + error = None + + try: + chatlog = extract_details_from_conversation(conversation) + prompt = GENERATE_TITLE_PROMPT % chatlog + chat_request = CohereChatRequest( + message=prompt, + model=model, + ) + response_message = create_message( + session, + chat_request, + conversation.id, + user_id, + 1, + "", + MessageAgent.CHATBOT, + False, + id=str(uuid.uuid4()), + ) + + response = await generate_chat_response( + session, + CustomChat().chat( + chat_request, + session=session, + stream=False, + agent_id=agent_id, + ctx=ctx, + ), + response_message=response_message, + conversation_id=None, + user_id=user_id, + should_store=False, + ctx=ctx, + ) + + title = response.text + error = response.error + except Exception as e: + title = DEFAULT_TITLE + error = str(e) + logger.error( + event=f"[Conversation] Error generating title: Conversation ID {conversation.id}, {e}", + ) + + return title, error diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/deployment.py b/western_ai/canada/cohere-toolkit/src/backend/services/deployment.py new file mode 100644 index 0000000000000000000000000000000000000000..22a49acbacce9fa13a4351839d044557efcf448d --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/deployment.py @@ -0,0 +1,130 @@ +""" +This module handles backend operations related to deployments, which define how to interact +with an LLM. + +New deployments are created by subclassing BaseDeployment and implementing required +methods in the model_deployments directory. + +Deployments can be configured in two ways: via configuration.yaml or environment variables +using the .env file, or dynamically in the database using the deployment_crud module. This +service abstracts these methods, ensuring higher layers remain unaffected by configuration details. + +Each deployment is assumed to use either configuration files or the database, with database +configurations taking precedence. +""" + +from backend.config.deployments import AVAILABLE_MODEL_DEPLOYMENTS +from backend.config.settings import Settings +from backend.crud import deployment as deployment_crud +from backend.crud import model as model_crud +from backend.database_models.database import DBSessionDep +from backend.exceptions import DeploymentNotFoundError, NoAvailableDeploymentsError +from backend.model_deployments.base import BaseDeployment +from backend.schemas.deployment import DeploymentDefinition, DeploymentUpdate +from backend.services.env import update_env_file +from backend.services.logger.utils import LoggerFactory + +logger = LoggerFactory().get_logger() + + +def create_db_deployment(session: DBSessionDep, deployment: DeploymentDefinition) -> DeploymentDefinition: + logger.debug(event="create_db_deployment", deployment=deployment.model_dump()) + + db_deployment = deployment_crud.create_deployment_by_config(session, deployment) + model_crud.create_model_by_config(session, deployment, db_deployment.id, None) + + return DeploymentDefinition.from_db_deployment(db_deployment) + + +def get_default_deployment_instance(**kwargs) -> BaseDeployment: + try: + fallback = next(d for d in AVAILABLE_MODEL_DEPLOYMENTS.values() if d.is_available()) + except StopIteration: + raise NoAvailableDeploymentsError() + + default_deployment = Settings().get("deployments.default_deployment") + if default_deployment: + return next( + ( + d + for d in AVAILABLE_MODEL_DEPLOYMENTS.values() + if d.id() == default_deployment + ), + fallback, + )(**kwargs) + + return fallback(**kwargs) + +def get_deployment_instance_by_id(session: DBSessionDep, deployment_id: str, **kwargs) -> BaseDeployment: + definition = get_deployment_definition(session, deployment_id) + # TODO: What's the point of fetching by ID if we just fetch by name after? + return get_deployment_instance_by_name(session, definition.name, **kwargs) + +def get_deployment_instance_by_name(session: DBSessionDep, deployment_name: str, **kwargs) -> BaseDeployment: + definition = get_deployment_definition_by_name(session, deployment_name) + + try: + deployment_class = next(d for d in AVAILABLE_MODEL_DEPLOYMENTS.values() if d.__name__ == definition.class_name) + deployment_instance = deployment_class( + db_id=definition.id, db_config=definition.config, **kwargs + ) + + return deployment_instance + except StopIteration: + raise DeploymentNotFoundError(deployment_id=deployment_name) + +def get_deployment_definition(session: DBSessionDep, deployment_id: str) -> DeploymentDefinition: + db_deployment = deployment_crud.get_deployment(session, deployment_id) + if db_deployment: + return DeploymentDefinition.from_db_deployment(db_deployment) + + try: + deployment = next(d for d in AVAILABLE_MODEL_DEPLOYMENTS.values() if d.id() == deployment_id) + create_db_deployment(session, deployment.to_deployment_definition()) + except StopIteration: + raise DeploymentNotFoundError(deployment_id=deployment_id) + + return deployment.to_deployment_definition() + +def get_deployment_definition_by_name(session: DBSessionDep, deployment_name: str) -> DeploymentDefinition: + definitions = get_deployment_definitions(session) + try: + definition = next(definition for definition in definitions if definition.name == deployment_name) + except StopIteration: + raise DeploymentNotFoundError(deployment_id=deployment_name) + + # Creates deployment in DB if it doesn't exist + if definition.name not in [d.name for d in deployment_crud.get_deployments(session)]: + definition = create_db_deployment(session, definition) + + return definition + +def get_deployment_definitions(session: DBSessionDep) -> list[DeploymentDefinition]: + db_deployments = { + db_deployment.name: DeploymentDefinition.from_db_deployment(db_deployment) + for db_deployment in deployment_crud.get_deployments(session) + } + + installed_deployments = [ + deployment.to_deployment_definition() + for deployment in AVAILABLE_MODEL_DEPLOYMENTS.values() + if deployment.name() not in db_deployments + ] + + return [*db_deployments.values(), *installed_deployments] + +def update_config(session: DBSessionDep, deployment_id: str, env_vars: dict[str, str]) -> DeploymentDefinition: + logger.debug(event="update_config", deployment_id=deployment_id, env_vars=env_vars) + + db_deployment = deployment_crud.get_deployment(session, deployment_id) + if db_deployment: + new_config = dict(db_deployment.default_deployment_config if db_deployment.default_deployment_config else {}) + new_config.update(env_vars) + update = DeploymentUpdate(default_deployment_config=new_config) + updated_db_deployment = deployment_crud.update_deployment(session, db_deployment, update) + updated_deployment = DeploymentDefinition.from_db_deployment(updated_db_deployment) + else: + update_env_file(env_vars) + updated_deployment = get_deployment_definition(session, deployment_id) + + return updated_deployment diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/env.py b/western_ai/canada/cohere-toolkit/src/backend/services/env.py new file mode 100644 index 0000000000000000000000000000000000000000..0deefa5076d11be8979e6291177836aa66140f68 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/env.py @@ -0,0 +1,14 @@ +from dotenv import find_dotenv, load_dotenv, set_key + + +def update_env_file(env_vars: dict[str, str]): + dotenv_path = find_dotenv() + + if dotenv_path == "": + dotenv_path = ".env" + open(dotenv_path, "a").close() + + for key in env_vars: + set_key(dotenv_path, key, str(env_vars[key])) + + load_dotenv(dotenv_path) diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/file.py b/western_ai/canada/cohere-toolkit/src/backend/services/file.py new file mode 100644 index 0000000000000000000000000000000000000000..465cc6bd5ca89291db3f642f0f48a4baa8d14c36 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/file.py @@ -0,0 +1,480 @@ +import io + +import pandas as pd +from docx import Document +from fastapi import Depends, HTTPException +from fastapi import UploadFile as FastAPIUploadFile +from python_calamine.pandas import pandas_monkeypatch + +import backend.crud.conversation as conversation_crud +import backend.crud.file as file_crud +from backend.crud import message as message_crud +from backend.database_models.conversation import ConversationFileAssociation +from backend.database_models.database import DBSessionDep +from backend.database_models.file import File as FileModel +from backend.schemas.context import Context +from backend.schemas.file import ConversationFilePublic, File +from backend.services import utils +from backend.services.agent import validate_agent_exists +from backend.services.context import get_context +from backend.services.logger.utils import LoggerFactory + +MAX_FILE_SIZE = 20_000_000 # 20MB +MAX_TOTAL_FILE_SIZE = 1_000_000_000 # 1GB + +PDF_EXTENSION = "pdf" +TEXT_EXTENSION = "txt" +MARKDOWN_EXTENSION = "md" +CSV_EXTENSION = "csv" +TSV_EXTENSION = "tsv" +EXCEL_EXTENSION = "xlsx" +EXCEL_OLD_EXTENSION = "xls" +JSON_EXTENSION = "json" +DOCX_EXTENSION = "docx" +PARQUET_EXTENSION = "parquet" +CALENDAR_EXTENSION = "ics" + +# Monkey patch Pandas to use Calamine for Excel reading because Calamine is faster than Pandas +pandas_monkeypatch() + +file_service = None + +logger = LoggerFactory().get_logger() + + +def get_file_service(): + """ + Initialize a singular instance of FileService if not initialized yet + + Returns: + FileService: The singleton FileService instance + """ + global file_service + if file_service is None: + file_service = FileService() + return file_service + + +class FileService: + """ + FileService class + + This class manages interfacing with different file storage solutions, + currently supports storing files in PostgreSQL. + """ + + async def create_conversation_files( + self, + session: DBSessionDep, + files: list[FastAPIUploadFile], + user_id: str, + conversation_id: str, + ctx: Context, + ) -> list[File]: + """ + Create files and associations with a conversation + + Args: + session (DBSessionDep): The database session + files (list[FastAPIUploadFile]): The files to upload + user_id (str): The user ID + conversation_id (str): The conversation ID + ctx (Context): Context object + + Returns: + list[File]: The files that were created + """ + uploaded_files = await insert_files_in_db(session, files, user_id) + + for file in uploaded_files: + conversation_crud.create_conversation_file_association( + session, + ConversationFileAssociation( + conversation_id=conversation_id, + user_id=user_id, + file_id=file.id, + ), + ) + + return uploaded_files + + async def create_agent_files( + self, + session: DBSessionDep, + files: list[FastAPIUploadFile], + user_id: str, + ctx: Context, + ) -> list[File]: + """ + Create files and associations with an agent + + Args: + session (DBSessionDep): The database session + files (list[FastAPIUploadFile]): The files to upload + user_id (str): The user ID + Returns: + list[File]: The files that were created + """ + uploaded_files = await insert_files_in_db(session, files, user_id) + + return uploaded_files + + def get_file_ids_by_agent_id( + self, session: DBSessionDep, user_id: str, agent_id: str, ctx: Context + ) -> list[str]: + """ + Get file IDs associated with a specific agent ID + + Args: + session (DBSessionDep): The database session + user_id (str): The user ID + agent_id (str): The agent ID + ctx (Context): Context object + + Returns: + list[str]: IDs of files that were created + """ + from backend.config.tools import Tool + from backend.tools.files import FileToolsArtifactTypes + + agent = validate_agent_exists(session, agent_id, user_id) + + if not agent.tools_metadata: + return [] + + artifacts = next( + ( + tool_metadata.artifacts + for tool_metadata in agent.tools_metadata + if tool_metadata.tool_name == Tool.Read_File.value.ID + or tool_metadata.tool_name == Tool.Search_File.value.ID + ), + [], # Default value if the generator is empty + ) + + return [ + artifact.get("id") + for artifact in artifacts + if artifact.get("type") == FileToolsArtifactTypes.local_file + ] + + def get_files_by_agent_id( + self, session: DBSessionDep, user_id: str, agent_id: str, ctx: Context + ) -> list[File]: + """ + Get files by agent ID + + Args: + session (DBSessionDep): The database session + user_id (str): The user ID + agent_id (str): The agent ID + ctx (Context): Context object + + Returns: + list[File]: The files that were created + """ + file_ids = self.get_file_ids_by_agent_id(session, user_id, agent_id, ctx) + + if not file_ids: + return [] + + return file_crud.get_files_by_ids(session, file_ids, user_id) + + def get_files_by_conversation_id( + self, session: DBSessionDep, user_id: str, conversation_id: str, ctx: Context + ) -> list[FileModel]: + """ + Get files by conversation ID + + Args: + session (DBSessionDep): The database session + user_id (str): The user ID + conversation_id (str): The conversation ID + + Returns: + list[File]: The files that were created + """ + conversation = conversation_crud.get_conversation( + session, conversation_id, user_id + ) + if not conversation: + raise HTTPException( + status_code=404, + detail=f"Conversation with ID: {conversation_id} not found.", + ) + file_ids = conversation.file_ids + + files = [] + if file_ids is not None: + files = file_crud.get_files_by_ids(session, file_ids, user_id) + + return files + + def delete_conversation_file_by_id( + self, + session: DBSessionDep, + conversation_id: str, + file_id: str, + user_id: str, + ctx: Context, + ) -> None: + """ + Delete a file asociated with a conversation + + Args: + session (DBSessionDep): The database session + conversation_id (str): The conversation ID + file_id (str): The file ID + user_id (str): The user ID + """ + conversation_crud.delete_conversation_file_association( + session, conversation_id, file_id, user_id + ) + + file_crud.delete_file(session, file_id, user_id) + + return + + def delete_agent_file_by_id( + self, + session: DBSessionDep, + agent_id: str, + file_id: str, + user_id: str, + ctx: Context, + ) -> None: + """ + Delete a file asociated with an agent + + Args: + session (DBSessionDep): The database session + agent_id (str): The agent ID + file_id (str): The file ID + user_id (str): The user ID + """ + file_crud.delete_file(session, file_id, user_id) + + return + + def delete_all_conversation_files( + self, + session: DBSessionDep, + conversation_id: str, + file_ids: list[str], + user_id: str, + ctx: Context = Depends(get_context), + ) -> None: + """ + Delete all files associated with a conversation + + Args: + session (DBSessionDep): The database session + conversation_id (str): The conversation ID + file_ids (list[str]): The file IDs + user_id (str): The user ID + ctx (Context): Context object + """ + logger = ctx.get_logger() + + logger.info( + event=f"Deleting conversation {conversation_id} files from DB." + ) + file_crud.bulk_delete_files(session, file_ids, user_id) + + def get_files_by_message_id( + self, session: DBSessionDep, message_id: str, user_id: str, ctx: Context + ) -> list[File]: + """ + Get message files + + Args: + session (DBSessionDep): The database session + message_id (str): The message ID + user_id (str): The user ID + + Returns: + list[File]: The files that were created + """ + message = message_crud.get_message(session, message_id, user_id) + files = [] + if message.file_ids is not None: + files = file_crud.get_files_by_ids(session, message.file_ids, user_id) + + return files + + +# Misc +def validate_file( + session: DBSessionDep, file_id: str, user_id: str +) -> File: + """ + Validates if a file exists and belongs to the user + + Args: + session (DBSessionDep): Database session + file_id (str): File ID + user_id (str): User ID + + Returns: + File: File object + + Raises: + HTTPException: If the file is not found + """ + file = file_crud.get_file(session, file_id, user_id) + + if not file: + raise HTTPException( + status_code=404, + detail=f"File with ID: {file_id} not found.", + ) + + return file + + +async def insert_files_in_db( + session: DBSessionDep, + files: list[FastAPIUploadFile], + user_id: str, +) -> list[File]: + """ + Insert files into the database + + Args: + session (DBSessionDep): The database session + files (list[FastAPIUploadFile]): The files to upload + user_id (str): The user ID + + Returns: + list[File]: The files that were created + """ + files_to_upload = [] + for file in files: + content = await get_file_content(file) + cleaned_content = content.replace("\x00", "") + filename = file.filename.encode("ascii", "ignore").decode("utf-8") + + files_to_upload.append( + FileModel( + file_name=filename, + file_size=file.size, + file_content=cleaned_content, + user_id=user_id, + ) + ) + + uploaded_files = file_crud.batch_create_files(session, files_to_upload) + return uploaded_files + + +def attach_conversation_id_to_files( + conversation_id: str, files: list[FileModel] +) -> list[ConversationFilePublic]: + results = [] + for file in files: + results.append( + ConversationFilePublic( + id=file.id, + conversation_id=conversation_id, + file_name=file.file_name, + file_size=file.file_size, + user_id=file.user_id, + created_at=file.created_at, + updated_at=file.updated_at, + ) + ) + return results + + + +def read_excel(file_contents: bytes) -> str: + """Reads the text from an Excel file using Pandas + + Args: + file_contents (bytes): The file contents + + Returns: + str: The text extracted from the Excel + """ + excel = pd.read_excel(io.BytesIO(file_contents), engine="calamine") + return excel.to_string() + + +def read_docx(file_contents: bytes) -> str: + """Reads the text from a DOCX file + + Args: + file_contents (bytes): The file contents + + Returns: + str: The text extracted from the DOCX file, with each paragraph separated by a newline + """ + document = Document(io.BytesIO(file_contents)) + text = "" + + for paragraph in document.paragraphs: + text += paragraph.text + "\n" + + return text + + +def read_parquet(file_contents: bytes) -> str: + """Reads the text from a Parquet file using Pandas + + Args: + file_contents (bytes): The file contents + + Returns: + str: The text extracted from the Parquet + """ + parquet = pd.read_parquet(io.BytesIO(file_contents), engine="pyarrow") + return parquet.to_string() + + + +def get_file_extension(file_name: str) -> str: + """Returns the file extension + + Args: + file_name (str): The file name + + Returns: + str: The file extension + """ + return file_name.split(".")[-1].lower() + + +async def get_file_content(file: FastAPIUploadFile) -> str: + """Reads the file contents based on the file extension + + Args: + file (UploadFile): The file to read + + Returns: + str: The file contents + + Raises: + ValueError: If the file extension is not supported + """ + file_contents = await file.read() + file_extension = get_file_extension(file.filename) + + if file_extension == PDF_EXTENSION: + return utils.read_pdf(file_contents) + elif file_extension == DOCX_EXTENSION: + return read_docx(file_contents) + elif file_extension == PARQUET_EXTENSION: + return read_parquet(file_contents) + elif file_extension in [ + TEXT_EXTENSION, + MARKDOWN_EXTENSION, + CSV_EXTENSION, + TSV_EXTENSION, + JSON_EXTENSION, + CALENDAR_EXTENSION + ]: + return file_contents.decode("utf-8") + elif file_extension in [EXCEL_EXTENSION, EXCEL_OLD_EXTENSION]: + return read_excel(file_contents) + + raise ValueError(f"File extension {file_extension} is not supported") diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/generators.py b/western_ai/canada/cohere-toolkit/src/backend/services/generators.py new file mode 100644 index 0000000000000000000000000000000000000000..48d0e8e930c8a9dc06fc3f5954db303345155432 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/generators.py @@ -0,0 +1,12 @@ +class AsyncGeneratorContextManager: + def __init__(self, agen): + self._agen = agen + + async def __aenter__(self): + return self._agen + + async def __aclose__(self, *args): + await self._agen.aclose() + + async def __aexit__(self, exc_type, exc, tb): + pass diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/logger/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/services/logger/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/logger/middleware.py b/western_ai/canada/cohere-toolkit/src/backend/services/logger/middleware.py new file mode 100644 index 0000000000000000000000000000000000000000..a4816872f576e9654b15c131208864f8da495bcd --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/logger/middleware.py @@ -0,0 +1,27 @@ +import time + +from starlette.middleware.base import BaseHTTPMiddleware +from starlette.requests import Request + +from backend.services.context import get_context + + +class LoggingMiddleware(BaseHTTPMiddleware): + async def dispatch(self, request: Request, call_next): + start_time = time.time() + + response = await call_next(request) + + ctx = get_context(request) + logger = ctx.get_logger() + + logger.info( + event="Request", + method=request.method, + path=request.url.path, + status_code=response.status_code, + duration=time.time() - start_time, + ctx=ctx, + ) + + return response diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/logger/strategies/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/services/logger/strategies/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/logger/strategies/base.py b/western_ai/canada/cohere-toolkit/src/backend/services/logger/strategies/base.py new file mode 100644 index 0000000000000000000000000000000000000000..808a8952298a13df6057b6775ecd4572b43b2034 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/logger/strategies/base.py @@ -0,0 +1,30 @@ +from abc import abstractmethod +from typing import Any + + +class BaseLogger: + """Base for all logger options.""" + + @abstractmethod + def setup(self, ctx: Any, **kwargs: Any) -> Any: ... + + @abstractmethod + def info(self, **kwargs: Any) -> Any: ... + + @abstractmethod + def error(self, **kwargs: Any) -> Any: ... + + @abstractmethod + def error_and_raise_http_exception(self, **kwargs: Any) -> Any: ... + + @abstractmethod + def warning(self, **kwargs: Any) -> Any: ... + + @abstractmethod + def debug(self, **kwargs: Any) -> Any: ... + + @abstractmethod + def critical(self, **kwargs: Any) -> Any: ... + + @abstractmethod + def exception(self, **kwargs: Any) -> Any: ... diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/logger/strategies/structured_log.py b/western_ai/canada/cohere-toolkit/src/backend/services/logger/strategies/structured_log.py new file mode 100644 index 0000000000000000000000000000000000000000..09283bc85941f6a9f1d4775a7b6e6c665d38f9cc --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/logger/strategies/structured_log.py @@ -0,0 +1,117 @@ +import logging +from typing import Any, Dict + +import structlog +from fastapi import HTTPException, status + +from backend.services.logger.strategies.base import BaseLogger + + +def log_context(func): + def wrapper(self, **kwargs): + ctx = kwargs.get("ctx") + if ctx: + kwargs["ctx"] = get_context_log(ctx) + return func(self, **kwargs) + + return wrapper + + +def get_context_log(ctx: Any) -> dict: + """ + Remove private attributes from context and return a dictionary. + """ + ctx_dict = ctx.model_dump() + + keys_to_remove = ["request", "response", "receive", "logger"] + for key in keys_to_remove: + ctx_dict.pop(key, None) + + return ctx_dict + + +def add_module( + logger: structlog.BoundLogger, name: str, event_dict: Dict[str, Any] +) -> Dict[str, Any]: + _, module_str = structlog._frames._find_first_app_frame_and_name( + additional_ignores=[__name__] + ) + event_dict["module"] = module_str + return event_dict + + +class StructuredLogging(BaseLogger): + def __init__(self, level: str = "info", renderer: str = "json"): + self.setup(level, renderer) + self.logger = structlog.get_logger() + + def setup(self, level: str = "info", renderer: str = "json"): + level = getattr(logging, level.upper(), None) + + structlog.contextvars.clear_contextvars() + + shared_processors = [ + structlog.processors.add_log_level, + add_module, + structlog.processors.TimeStamper(fmt="iso"), + structlog.processors.StackInfoRenderer(), + structlog.processors.UnicodeDecoder(), + ] + + # If running in a terminal, use colored output + # Otherwise, use JSON output + if renderer.lower() == "console": + processors = shared_processors + [ + structlog.dev.set_exc_info, + structlog.dev.ConsoleRenderer(), + ] + else: + processors = shared_processors + [ + structlog.processors.dict_tracebacks, + structlog.processors.JSONRenderer(), + ] + + structlog.configure( + processors=processors, + wrapper_class=structlog.make_filtering_bound_logger(level), + cache_logger_on_first_use=True, # Remove this line to make changes to the logger + ) + + @log_context + def info(self, **kwargs): + self.logger.info(**kwargs) + + @log_context + def error(self, **kwargs): + self.logger.error(**kwargs) + + @log_context + def error_and_raise_http_exception(self, **kwargs): + self.logger.error(**kwargs) + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail=f"{kwargs}", + ) + + @log_context + def warning(self, **kwargs): + self.logger.warning(**kwargs) + + @log_context + def debug(self, **kwargs): + self.logger.debug(**kwargs) + + @log_context + def critical(self, **kwargs): + self.logger.critical(**kwargs) + + @log_context + def exception(self, **kwargs): + self.logger.exception(**kwargs) + + @log_context + def bind(self, **kwargs): + self.logger = self.logger.bind(**kwargs) + + def unbind(self, *args): + self.logger = self.logger.unbind(*args) diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/logger/utils.py b/western_ai/canada/cohere-toolkit/src/backend/services/logger/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..1ec6075ff70a5382f1961b1f9409f58459638885 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/logger/utils.py @@ -0,0 +1,22 @@ +from backend.config.settings import Settings +from backend.services.logger.strategies.base import BaseLogger +from backend.services.logger.strategies.structured_log import StructuredLogging + + +class LoggerFactory: + def __init__(self): + self.logger = None + + def get_logger(self) -> BaseLogger: + if self.logger is not None: + return self.logger + + strategy = Settings().get('logger.strategy') + level = Settings().get('logger.level') + renderer = Settings().get('logger.renderer') + + if strategy == "structlog": + return StructuredLogging(level, renderer) + else: + # Default to StructuredLogging + return StructuredLogging(level, renderer) diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/request_validators.py b/western_ai/canada/cohere-toolkit/src/backend/services/request_validators.py new file mode 100644 index 0000000000000000000000000000000000000000..e3caaebac39f5a2444e08558bfa134dcbdf98eb4 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/request_validators.py @@ -0,0 +1,432 @@ +from typing import Annotated +from urllib.parse import unquote_plus + +from fastapi import Header, HTTPException, Request + +import backend.crud.user as user_crud +from backend.config.deployments import AVAILABLE_MODEL_DEPLOYMENTS +from backend.config.tools import get_available_tools +from backend.crud import agent as agent_crud +from backend.crud import conversation as conversation_crud +from backend.crud import deployment as deployment_crud +from backend.crud import model as model_crud +from backend.crud import organization as organization_crud +from backend.database_models.database import DBSessionDep +from backend.database_models.deployment import Deployment +from backend.exceptions import DeploymentNotFoundError +from backend.model_deployments.utils import class_name_validator +from backend.services import deployment as deployment_service +from backend.services.agent import validate_agent_exists +from backend.services.auth.utils import get_header_user_id +from backend.services.logger.utils import LoggerFactory + +logger = LoggerFactory().get_logger() + + +def validate_deployment_model(deployment: str, model: str, session: DBSessionDep): + """ + Validate that the deployment and model are compatible. + + Args: + deployment_name (str): The deployment name + model_name (str): The model name + session (DBSessionDep): The database session + + Raises: + HTTPException: If the deployment and model are not compatible + + """ + found = deployment_service.get_deployment_definition_by_name(session, deployment) + if not found: + found = deployment_service.get_deployment_definition(session, deployment) + if not found: + raise HTTPException( + status_code=400, + detail=f"Deployment {deployment} not found or is not available in the Database.", + ) + + deployment_config = next( + d for d in AVAILABLE_MODEL_DEPLOYMENTS.values() if d.__name__ == found.class_name + ).to_deployment_definition() + deployment_model = next( + ( + model_db + for model_db in found.models + if model_db == model + ), + None, + ) + if not deployment_model: + deployment_model = model_crud.create_model_by_config( + session, deployment_config, found.id, model + ) + if not deployment_model: + raise HTTPException( + status_code=400, + detail=f"Model {model} not found for deployment {deployment}.", + ) + + return found, deployment_model + + +def validate_deployment_config(deployment_config: dict, deployment_db: Deployment) -> None: + """ + Validate that the deployment config is valid for the deployment. + + Args: + deployment_config (dict): The deployment config + deployment_db (Deployment): The deployment database model + + Raises: + HTTPException: If the deployment config is not valid + + """ + for key in deployment_config: + if ( + key not in deployment_db.default_deployment_config + or not deployment_config[key] + ): + raise HTTPException( + status_code=400, + detail=f"Deployment config key {key} not valid for deployment {deployment_db.name} or is empty.", + ) + + +def validate_user_header( + session: DBSessionDep, + user_id: Annotated[str|None, Header( + title="User ID", + description="Unique identifier of User making the request", + )] = None, +) -> None: + """ + Validate that the request has the `User-Id` header, used for requests + that require a User. + + Raises: + HTTPException: If no `User-Id` header. + """ + if not user_id: + logger.error(event="User-Id required in request headers.") + raise HTTPException( + status_code=401, detail="User-Id required in request headers." + ) + + user = user_crud.get_user(session, user_id) + if not user: + logger.error(event="User not found.", user_id=user_id) + raise HTTPException(status_code=401, detail="User not found.") + + +def validate_organization_header( + session: DBSessionDep, + Organization_Id: Annotated[str|None, Header( # Not following snake_case so FastAPI parses dependencies properly + title="Organization ID", + description="Unique Identifier for the Organization making the request", + )] = None, +) -> None: + """ + Validate that the request has the `Organization-Id` header, used for requests + that require an Organization. + + Raises: + HTTPException: If no `Organization-Id` header. + """ + if Organization_Id: + organization = organization_crud.get_organization(session, Organization_Id) + if not organization: + raise HTTPException(status_code=404, detail=f"Organization ID {Organization_Id} not found.") + + +def validate_deployment_header( + session: DBSessionDep, + deployment_name: Annotated[str|None, Header( + title="Deployment Name", + description="Name of the Deployment to use for the request", + )] = None, +) -> None: + """ + Validate that the request has the `Deployment-Name` header, used for chat requests + that require a deployment (e.g: Cohere Platform, SageMaker). + + Raises: + HTTPException: If no `Deployment-Name` header. + """ + # TODO Eugene: Discuss with Scott + if deployment_name: + _ = deployment_service.get_deployment_definition_by_name(session, deployment_name) + + +async def validate_chat_request(request: Request, session: DBSessionDep): + """ + Validate that the request has the appropriate values in the body + + Args: + request (Request): The request to validate + + Raises: + HTTPException: If the request does not have the appropriate values in the body + """ + # Validate that the agent_id is valid + body = await request.json() + user_id = get_header_user_id(request) + + agent_id = request.query_params.get("agent_id") + if agent_id: + validate_agent_exists(session, agent_id, user_id) + + # If conversation_id is passed in with agent_id, then make sure that conversation exists with the agent_id + conversation_id = body.get("conversation_id") + if conversation_id and agent_id: + conversation = conversation_crud.get_conversation( + session, conversation_id, user_id + ) + if conversation is None or conversation.agent_id != agent_id: + raise HTTPException( + status_code=404, + detail=f"Conversation ID {conversation_id} not found for specified agent.", + ) + + tools = body.get("tools") + if not tools: + return + + managed_tools = [tool["name"] for tool in tools if tool["name"] in get_available_tools()] + if managed_tools and len(tools) != len(managed_tools): + raise HTTPException( + status_code=400, detail="Cannot mix both managed and custom tools" + ) + + if not managed_tools: + for tool in tools: + if not tool.get("description"): + raise HTTPException( + status_code=400, detail="Custom tools must have a description" + ) + + +async def validate_env_vars(session: DBSessionDep, request: Request): + """ + Validate that the request has valid env vars. + + Args: + request (Request): The request to validate + + Raises: + HTTPException: If the request does not have the appropriate values in the header + + """ + body = await request.json() + env_vars = body.get("env_vars") + invalid_keys = [] + + deployment_id = unquote_plus(request.path_params.get("deployment_id", "")) + try: + deployment = deployment_service.get_deployment_instance_by_id(session, deployment_id) + except DeploymentNotFoundError: + raise HTTPException( + status_code=404, detail=f"Deployment {deployment_id} not found." + ) + + for key in env_vars: + if key not in deployment.env_vars(): + invalid_keys.append(key) + + if invalid_keys: + raise HTTPException( + status_code=400, + detail=( + "Environment variables not valid for deployment: " + + ",".join(invalid_keys) + ), + ) + + return env_vars + + +async def validate_create_agent_request(session: DBSessionDep, request: Request): + """ + Validate that the create agent request has valid tools, deployments, and compatible models. + + Args: + request (Request): The request to validate + + Raises: + HTTPException: If the request does not have the appropriate values in the body + """ + user_id = get_header_user_id(request) + body = await request.json() + + # TODO @scott-cohere: for now we disregard versions and assume agents have unique names, enforce versioning later + agent_name = body.get("name") + agent = agent_crud.get_agent_by_name(session, agent_name, user_id=user_id) + if agent: + raise HTTPException( + status_code=400, detail=f"Agent {agent_name} already exists." + ) + + # Validate tools + tools = body.get("tools") + if tools: + for tool in tools: + if tool not in get_available_tools(): + raise HTTPException(status_code=404, detail=f"Tool {tool} not found.") + + name = body.get("name") + model = body.get("model") + deployment = body.get("deployment") + if not name or not model or not deployment: + raise HTTPException( + status_code=400, detail="Name, model, and deployment are required." + ) + deployment_config = body.get("deployment_config") + # Validate deployment + deployment_db, model_db = validate_deployment_model(deployment, model, session) + if deployment_config: + validate_deployment_config(deployment_config, deployment_db) + + +async def validate_update_agent_request(session: DBSessionDep, request: Request): + """ + Validate that the update agent request has valid tools, deployments, and compatible models. + + Args: + request (Request): The request to validate + + Raises: + HTTPException: If the request does not have the appropriate values in the body + """ + user_id = get_header_user_id(request) + agent_id = request.path_params.get("agent_id") + if not agent_id: + logger.error(event="Agent ID is required.") + raise HTTPException(status_code=400, detail="Agent ID is required.") + + agent = agent_crud.get_agent_by_id(session, agent_id, user_id) + if not agent: + logger.error(event="Agent not found", agent_id=agent_id) + raise HTTPException( + status_code=404, detail=f"Agent with ID {agent_id} not found." + ) + + if agent.user_id != user_id: + logger.error(event="Agent does not belong to user.", agent_user_id=agent.user_id, user_id=user_id) + raise HTTPException( + status_code=401, detail=f"Agent with ID {agent_id} does not belong to user." + ) + + body = await request.json() + # Validate tools + tools = body.get("tools") + if tools: + for tool in tools: + if tool not in get_available_tools(): + logger.error(event="Tool not found.", tool=tool) + raise HTTPException(status_code=404, detail=f"Tool {tool} not found.") + + model, deployment = body.get("model"), body.get("deployment") + # Model and deployment must be updated together to ensure compatibility + if not model and deployment: + logger.error(event="If updating an agent's deployment type, the model must also be provided.") + raise HTTPException( + status_code=400, + detail="If updating an agent's deployment type, the model must also be provided.", + ) + elif model and not deployment: + logger.error(event="If updating an agent's model, the deployment must also be provided.") + raise HTTPException( + status_code=400, + detail="If updating an agent's model, the deployment must also be provided.", + ) + elif model and deployment: + deployment_config = body.get("deployment_config") + # Validate + deployment_db, model_db = validate_deployment_model(deployment, model, session) + if deployment_config: + validate_deployment_config(deployment_config, deployment_db) + + +async def validate_create_update_model_request(session: DBSessionDep, request: Request): + """ + Validate that the create model request has valid deployment. + + Args: + request (Request): The request to validate + + Raises: + HTTPException: If the request does not have the appropriate values in the body + """ + body = await request.json() + deployment_id = body.get("deployment_id") + if request.method == "POST" and not deployment_id: + raise HTTPException(status_code=400, detail="deployment_id is required.") + + if deployment_id: + deployment_db = deployment_crud.get_deployment(session, deployment_id) + if not deployment_db: + raise HTTPException( + status_code=400, + detail=f"Deployment {deployment_id} not found or is not available in the Database.", + ) + + +async def validate_create_deployment_request(session: DBSessionDep, request: Request): + """ + Validate that the create deployment request is valid. + + Args: + request (Request): The request to validate + + Raises: + HTTPException: If the request does not have the appropriate values in the body + """ + body = await request.json() + name = body.get("name") + deployment = deployment_crud.get_deployment_by_name(session, name) + if deployment: + raise HTTPException( + status_code=400, detail=f"Deployment {name} already exists." + ) + + deployment_class_name = body.get("deployment_class_name") + try: + class_name_validator(deployment_class_name) + except ValueError: + raise HTTPException( + status_code=404, + detail=f"Deployment class name {deployment_class_name} not found.", + ) + + +async def validate_organization_request(session: DBSessionDep, request: Request): + """ + Validate create/update organization request. + + Args: + request (Request): The request to validate + + Raises: + HTTPException: If the request does not have the appropriate values in the body + """ + + organization_id = request.path_params.get("org_id") + # Organization ID is required for PUT requests + if request.method == "PUT": + if not organization_id: + raise HTTPException(status_code=400, detail="Organization ID is required.") + if organization_id and not organization_crud.get_organization( + session, organization_id + ): + raise HTTPException( + status_code=404, + detail=f"Organization with ID: {organization_id} not found.", + ) + body = await request.json() + name = body.get("name") + if not name and request.method == "POST": + raise HTTPException(status_code=400, detail="Organization name is required.") + check_organization = organization_crud.get_organization_by_name(session, name) + if check_organization: + raise HTTPException( + status_code=400, detail=f"Organization with name: {name} already exists." + ) diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/snapshot.py b/western_ai/canada/cohere-toolkit/src/backend/services/snapshot.py new file mode 100644 index 0000000000000000000000000000000000000000..5a5d34032f6b2f780840a2223c962751462e7b2b --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/snapshot.py @@ -0,0 +1,132 @@ +from typing import Any + +from fastapi import HTTPException + +from backend.chat.collate import to_dict +from backend.crud import agent as agent_crud +from backend.crud import snapshot as snapshot_crud +from backend.database_models import Snapshot as SnapshotModel +from backend.database_models import SnapshotAccess as SnapshotAccessModel +from backend.database_models import SnapshotLink as SnapshotLinkModel +from backend.database_models.database import DBSessionDep +from backend.schemas.context import Context +from backend.schemas.conversation import Conversation +from backend.schemas.snapshot import SnapshotAgent, SnapshotData +from backend.services.conversation import get_messages_with_files + +SNAPSHOT_VERSION = 1 + + +def validate_last_message(last_message_id: str) -> None: + if not last_message_id: + raise HTTPException(status_code=404, detail="Conversation has no messages") + + +def validate_snapshot_exists( + session: DBSessionDep, snapshot_id: str, raise_exception: bool = True +) -> SnapshotModel: + snapshot = snapshot_crud.get_snapshot(session, snapshot_id) + if not snapshot and raise_exception: + raise HTTPException(status_code=404, detail="Snapshot not found") + return snapshot + + +def validate_snapshot_link(session: DBSessionDep, link_id: str) -> SnapshotLinkModel: + snapshot_link = snapshot_crud.get_snapshot_link(session, link_id) + if not snapshot_link: + raise HTTPException(status_code=404, detail="Snapshot link not found") + + snapshot = snapshot_crud.get_snapshot(session, snapshot_link.snapshot_id) + if not snapshot: + raise HTTPException(status_code=404, detail="Snapshot not found") + + return snapshot + + +def wrap_create_snapshot_link( + session: DBSessionDep, snapshot_id: str, user_id: str +) -> SnapshotLinkModel: + snapshot_link = SnapshotLinkModel(snapshot_id=snapshot_id, user_id=user_id) + return snapshot_crud.create_snapshot_link(session, snapshot_link) + + +def wrap_create_snapshot( + session: DBSessionDep, + last_message_id: str, + user_id: str, + conversation: Conversation, + ctx: Context, +) -> SnapshotModel: + snapshot_agent = None + if conversation.agent_id: + agent = agent_crud.get_agent_by_id(session, conversation.agent_id, user_id) + tools_metadata = [to_dict(metadata) for metadata in agent.tools_metadata] + + snapshot_agent = SnapshotAgent( + id=agent.id, + name=agent.name, + description=agent.description, + preamble=agent.preamble, + tools_metadata=tools_metadata, + ) + + messages = get_messages_with_files(session, user_id, conversation.messages, ctx) + snapshot_data = SnapshotData( + title=conversation.title, + description=conversation.description, + messages=messages, + agent=snapshot_agent, + ) + snapshot = to_dict(snapshot_data) + + snapshot_model = SnapshotModel( + user_id=user_id, + organization_id=conversation.organization_id, + conversation_id=conversation.id, + last_message_id=last_message_id, + version=SNAPSHOT_VERSION, + snapshot=snapshot, + agent_id=conversation.agent_id, + ) + return snapshot_crud.create_snapshot(session, snapshot_model) + + +def wrap_create_snapshot_access( + session: DBSessionDep, + snapshot_id: str, + user_id: str, + link_id: str, + ctx: Context, +) -> SnapshotAccessModel: + logger = ctx.get_logger() + + try: + snapshot_access = SnapshotAccessModel( + user_id=user_id, snapshot_id=snapshot_id, link_id=link_id + ) + _ = snapshot_crud.create_snapshot_access(session, snapshot_access) + except Exception as e: + # Do not raise exception if snapshot access creation fails + session.rollback() + logger.error(event=f"[Snapshot] Error creating snapshot access: {e}") + + +def remove_private_keys( + snapshot: dict[str, Any], keys_to_remove: list[str] +) -> dict[str, Any]: + new_snapshot = remove_keys_nested_dict(snapshot, keys_to_remove) + return new_snapshot + + +def remove_keys_nested_dict(d: dict[str, Any], keys_to_remove: list[str]) -> dict: + if isinstance(d, dict): + keys = list(d.keys()) + for key in keys: + if key in keys_to_remove: + del d[key] + else: + d[key] = remove_keys_nested_dict(d[key], keys_to_remove) + elif isinstance(d, list): + for i in range(len(d)): + d[i] = remove_keys_nested_dict(d[i], keys_to_remove) + return d diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/synthesizer.py b/western_ai/canada/cohere-toolkit/src/backend/services/synthesizer.py new file mode 100644 index 0000000000000000000000000000000000000000..d5bb2f31600b923f17a8ca1b82b8722cf1ea2ae4 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/synthesizer.py @@ -0,0 +1,79 @@ +from google.cloud.texttospeech import ( + AudioConfig, + AudioEncoding, + SynthesisInput, + TextToSpeechClient, + VoiceSelectionParams, +) +from googleapiclient.discovery import build + +from backend.config import Settings + + +def synthesize(text: str) -> bytes: + """ + Synthesizes speech from the input text. + + Args: + text (str): The input text to be synthesized into speech. + + Returns: + bytes: The audio content generated from the input text in MP3 format. + + Raises: + ValueError: If the Google Cloud API key from the settings is not valid. + """ + client = TextToSpeechClient(client_options={ + "api_key": _validate_google_cloud_api_key() + }) + + language = detect_language(text) + + response = client.synthesize_speech( + input=SynthesisInput(text=text), + voice=VoiceSelectionParams(language_code=language), + audio_config=AudioConfig(audio_encoding=AudioEncoding.MP3) + ) + + return response.audio_content + + +def detect_language(text: str) -> str: + """ + Detect the language of the given text. + + Args: + text (str): The text for which the language needs to be detected. + + Returns: + str: The language code of the detected language (e.g., 'en', 'es'). + + Raises: + ValueError: If the Google Cloud API key from the settings is not valid. + """ + client = build("translate", "v2", developerKey=_validate_google_cloud_api_key()) + + response = client.detections().list(q=text).execute() + + return response["detections"][0][0]["language"] + + +def _validate_google_cloud_api_key() -> str: + """ + Validates the Google Cloud API key from the settings. + + Returns: + str: The validated API key. + + Raises: + ValueError: If the API key is not found in the settings or is empty. + """ + google_cloud = Settings().get('google_cloud') + + if not google_cloud: + raise ValueError("google_cloud in secrets.yaml is missing.") + + if not google_cloud.api_key: + raise ValueError("google_cloud.api_key in secrets.yaml is missing.") + + return google_cloud.api_key diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/transaction.py b/western_ai/canada/cohere-toolkit/src/backend/services/transaction.py new file mode 100644 index 0000000000000000000000000000000000000000..52460cdb1ad63e66e5c41528e2e44f7704e9c358 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/transaction.py @@ -0,0 +1,14 @@ +def validate_transaction(func): + def wrapper(*args, **kwargs): + if "db" in kwargs: + db = kwargs["db"] + else: + db = args[0] + + try: + return func(*args, **kwargs) + except Exception as e: + db.rollback() + raise e + + return wrapper diff --git a/western_ai/canada/cohere-toolkit/src/backend/services/utils.py b/western_ai/canada/cohere-toolkit/src/backend/services/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..c55eff5dfbeffa463dbcdfa881130c8ea9c330c8 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/services/utils.py @@ -0,0 +1,43 @@ +import io + +from fastapi import Request +from pypdf import PdfReader + + +def get_deployment_config(request: Request) -> dict: + headers = request.get("headers", {}) + header = get_header_value(headers, ["deployment-config", "Deployment-Config"]) + config = {} + for c in header.split(";"): + kv = c.split("=") + if len(kv) < 2: + continue + config[kv[0]] = "".join(kv[1:]) + return config + + +def get_header_value(headers: list, keys: list) -> str: + for k, v in headers: + if k.decode("utf-8") in keys: + return v.decode("utf-8") + return "" + + +def read_pdf(file_contents: bytes) -> str: + """Reads the text from a PDF file using PyPDF2 + + Args: + file_contents (bytes): The file contents + + Returns: + str: The text extracted from the PDF + """ + pdf_reader = PdfReader(io.BytesIO(file_contents)) + text = "" + + # Extract text from each page + for page in pdf_reader.pages: + page_text = page.extract_text() + text += page_text + + return text diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/integration/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tests/integration/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/integration/conftest.py b/western_ai/canada/cohere-toolkit/src/backend/tests/integration/conftest.py new file mode 100644 index 0000000000000000000000000000000000000000..00b30deb57f2fe77a473d2d0d9c928b1e8fba2cd --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/integration/conftest.py @@ -0,0 +1,231 @@ +import os +from typing import Any, Generator +from unittest.mock import patch + +import pytest +from alembic.command import upgrade +from alembic.config import Config +from fastapi.testclient import TestClient +from sqlalchemy import create_engine +from sqlalchemy.orm import Session + +from backend.database_models import get_session +from backend.database_models.agent import Agent +from backend.database_models.deployment import Deployment +from backend.database_models.model import Model +from backend.main import app, create_app +from backend.schemas.chat import StreamEvent +from backend.schemas.organization import Organization +from backend.schemas.user import User +from backend.tests.unit.factories import get_factory + +DATABASE_URL = os.environ.get("DATABASE_URL", "postgresql://postgres:postgres@localhost:5433") + + +@pytest.fixture +def client() -> Generator[TestClient, None, None]: + yield TestClient(app) + + +@pytest.fixture(scope="function") +def engine() -> Generator[Any, None, None]: + """ + Yields a SQLAlchemy engine which is disposed of after the test session + """ + engine = create_engine(DATABASE_URL, echo=True) + + yield engine + + engine.dispose() + + +@pytest.fixture(scope="function") +def session(engine: Any) -> Generator[Session, None, None]: + """ + Yields a SQLAlchemy session within a transaction + that is rolled back after every function + """ + connection = engine.connect() + # Begin the nested transaction + transaction = connection.begin() + # Use connection within the started transaction + session = Session(bind=connection) + # Run Alembic migrations + alembic_cfg = Config("src/backend/alembic.ini") + upgrade(alembic_cfg, "head") + + yield session + + session.close() + # Roll back the transaction + transaction.rollback() + # Close connection so it returns to the connection pool + connection.close() + + +@pytest.fixture(scope="function") +def session_client(session: Session) -> Generator[TestClient, None, None]: + """ + Fixture to inject the session into the API client + """ + + def override_get_session() -> Generator[Session, Any, None]: + yield session + + app = create_app() + + app.dependency_overrides[get_session] = override_get_session + + print("Session at fixture " + str(session)) + + with TestClient(app) as client: + yield client + + app.dependency_overrides = {} + + +@pytest.fixture(scope="session") +def engine_chat() -> Generator[Any, None, None]: + """ + Yields a SQLAlchemy engine which is disposed of after the test session + """ + engine = create_engine(DATABASE_URL, echo=True) + + yield engine + + engine.dispose() + + +@pytest.fixture(scope="session") +def session_chat(engine_chat: Any) -> Generator[Session, None, None]: + """ + Yields a SQLAlchemy session within a transaction + that is rolled back after every session + + We need to use the fixture in the session scope because the chat + endpoint is asynchronous and needs to be open for the entire session + """ + connection = engine_chat.connect() + # Begin the nested transaction + transaction = connection.begin() + # Use connection within the started transaction + session = Session(bind=connection) + # Run Alembic migrations + alembic_cfg = Config("src/backend/alembic.ini") + upgrade(alembic_cfg, "head") + + yield session + + session.close() + # Roll back the transaction + transaction.rollback() + # Close connection so it returns to the connection pool + connection.close() + + +@pytest.fixture(scope="session") +def session_client_chat(session_chat: Session) -> Generator[TestClient, None, None]: + """ + Fixture to inject the session into the API client + + We need to use the fixture in the session scope because the chat + router uses a WebSocket connection that needs to be open for the + entire session + """ + + def override_get_session() -> Generator[Session, Any, None]: + yield session_chat + + app = create_app() + app.dependency_overrides[get_session] = override_get_session + + print("Session at fixture " + str(session_chat)) + + with TestClient(app) as client: + yield client + + app.dependency_overrides = {} + + +@pytest.fixture +def user(session: Session) -> User: + return get_factory("User", session).create() + + +@pytest.fixture +def organization(session: Session) -> Organization: + return get_factory("Organization", session).create() + + +@pytest.fixture +def deployment(session: Session) -> Deployment: + return get_factory("Deployment", session).create( + deployment_class_name="CohereDeployment" + ) + +@pytest.fixture +def model(session: Session) -> Model: + return get_factory("Model", session).create() + + +@pytest.fixture +def agent(session: Session) -> Agent: + return get_factory("Agent", session).create() + + + +@pytest.fixture +def inject_events() -> list[dict]: + return [] + + +@pytest.fixture +def mock_event_stream(inject_events: list[dict]) -> list[dict]: + events = [ + { + "event_type": StreamEvent.STREAM_START, + "generation_id": "ca0f398e-f8c8-48f0-b093-12d1754d00ed", + }, + ] + if inject_events: + events.extend(inject_events) + + events.extend([ + { + "event_type": StreamEvent.TEXT_GENERATION, + "text": "This is a test.", + }, + { + "event_type": StreamEvent.STREAM_END, + "response": { + "generation_id": "ca0f398e-f8c8-48f0-b093-12d1754d00ed", + "citations": [], + "documents": [], + "search_results": [], + "search_queries": [], + }, + "finish_reason": "COMPLETE", + } + ]) + return events + + +@pytest.fixture +def mock_available_model_deployments(mock_event_stream: list[dict]): + from backend.tests.unit.model_deployments.mock_deployments.mock_base import ( + MockDeployment, + ) + + MockDeployment.event_stream = mock_event_stream + MOCKED_DEPLOYMENTS = { d.name(): d for d in MockDeployment.__subclasses__() } + + with patch("backend.services.deployment.AVAILABLE_MODEL_DEPLOYMENTS", MOCKED_DEPLOYMENTS) as mock: + yield mock + +@pytest.fixture +def mock_cohere_list_models(): + with patch( + "backend.model_deployments.cohere_platform.CohereDeployment.list_models", + return_value=["command", "command-r", "command-r-plus", "command-light-nightly"] + ) as mock: + yield mock diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/integration/routers/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tests/integration/routers/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/integration/routers/test_agent.py b/western_ai/canada/cohere-toolkit/src/backend/tests/integration/routers/test_agent.py new file mode 100644 index 0000000000000000000000000000000000000000..5ea1fa206a67e4eb77d2877f72ce93214a636d5c --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/integration/routers/test_agent.py @@ -0,0 +1,1331 @@ +import pytest +from fastapi.testclient import TestClient +from sqlalchemy.orm import Session + +from backend.config.default_agent import DEFAULT_AGENT_ID +from backend.config.tools import Tool +from backend.crud import deployment as deployment_crud +from backend.database_models.agent import Agent +from backend.database_models.agent_tool_metadata import AgentToolMetadata +from backend.database_models.snapshot import Snapshot +from backend.exceptions import DeploymentNotFoundError +from backend.model_deployments.cohere_platform import CohereDeployment +from backend.schemas.user import User +from backend.tests.unit.factories import get_factory + + +def test_create_agent( + session_client: TestClient, + session: Session, + user: User, + mock_cohere_list_models, +) -> None: + request_json = { + "name": "test agent", + "version": 1, + "description": "test description", + "preamble": "test preamble", + "temperature": 0.5, + "model": "command-r-plus", + "deployment": CohereDeployment.name(), + "tools": [Tool.Calculator.value.ID, Tool.Search_File.value.ID, Tool.Read_File.value.ID], + } + + response = session_client.post( + "/v1/agents", json=request_json, headers={"User-Id": user.id} + ) + assert response.status_code == 200 + response_agent = response.json() + + assert response_agent["name"] == request_json["name"] + assert response_agent["version"] == request_json["version"] + assert response_agent["description"] == request_json["description"] + assert response_agent["preamble"] == request_json["preamble"] + assert response_agent["temperature"] == request_json["temperature"] + assert response_agent["model"] == request_json["model"] + assert response_agent["deployment"] == request_json["deployment"] + assert response_agent["tools"] == request_json["tools"] + + agent = session.get(Agent, response_agent["id"]) + assert agent is not None + assert agent.name == request_json["name"] + assert agent.version == request_json["version"] + assert agent.description == request_json["description"] + assert agent.preamble == request_json["preamble"] + assert agent.temperature == request_json["temperature"] + assert agent.model == request_json["model"] + assert agent.deployment == request_json["deployment"] + assert agent.tools == request_json["tools"] + + +def test_create_agent_with_tool_metadata( + session_client: TestClient, + session: Session, + user: User, + mock_cohere_list_models, +) -> None: + request_json = { + "name": "test agent", + "version": 1, + "description": "test description", + "preamble": "test preamble", + "temperature": 0.5, + "model": "command-r-plus", + "deployment": CohereDeployment.name(), + "tools": [Tool.Google_Drive.value.ID, Tool.Search_File.value.ID], + "tools_metadata": [ + { + "tool_name": Tool.Google_Drive.value.ID, + "artifacts": [ + { + "name": "/folder", + "ids": "folder1", + "type": "folder_ids", + } + ], + }, + { + "tool_name": Tool.Search_File.value.ID, + "artifacts": [ + { + "name": "file.txt", + "ids": "file1", + "type": "file_ids", + } + ], + }, + ], + } + + response = session_client.post( + "/v1/agents", json=request_json, headers={"User-Id": user.id} + ) + assert response.status_code == 200 + response_agent = response.json() + + tool_metadata = ( + session.query(AgentToolMetadata) + .filter(AgentToolMetadata.agent_id == response_agent["id"]) + .all() + ) + assert len(tool_metadata) == 2 + assert tool_metadata[0].tool_name == Tool.Google_Drive.value.ID + assert tool_metadata[0].artifacts == [ + {"name": "/folder", "ids": "folder1", "type": "folder_ids"}, + ] + assert tool_metadata[1].tool_name == Tool.Search_File.value.ID + assert tool_metadata[1].artifacts == [ + {"name": "file.txt", "ids": "file1", "type": "file_ids"} + ] + + +def test_create_agent_missing_non_required_fields( + session_client: TestClient, + session: Session, + user: User, + mock_cohere_list_models, +) -> None: + request_json = { + "name": "test agent", + "model": "command-r-plus", + "deployment": CohereDeployment.name(), + } + + response = session_client.post( + "/v1/agents", json=request_json, headers={"User-Id": user.id} + ) + assert response.status_code == 200 + response_agent = response.json() + + assert response_agent["name"] == request_json["name"] + assert response_agent["version"] == 1 + assert response_agent["description"] == "" + assert response_agent["preamble"] == "" + assert response_agent["temperature"] == 0.3 + assert response_agent["model"] == request_json["model"] + + agent = session.get(Agent, response_agent["id"]) + assert agent is not None + assert agent.name == request_json["name"] + assert agent.version == 1 + assert agent.description == "" + assert agent.preamble == "" + assert agent.temperature == 0.3 + assert agent.model == request_json["model"] + + +def test_update_agent( + session_client: TestClient, + session: Session, + user: User, + mock_cohere_list_models, +) -> None: + agent = get_factory("Agent", session).create( + name="test agent", + version=1, + description="test description", + preamble="test preamble", + temperature=0.5, + user=user, + ) + + request_json = { + "name": "updated name", + "version": 2, + "description": "updated description", + "preamble": "updated preamble", + "temperature": 0.7, + "model": "command-r", + "deployment": CohereDeployment.name(), + } + + response = session_client.put( + f"/v1/agents/{agent.id}", + json=request_json, + headers={"User-Id": user.id}, + ) + + assert response.status_code == 200 + updated_agent = response.json() + assert updated_agent["name"] == "updated name" + assert updated_agent["version"] == 2 + assert updated_agent["description"] == "updated description" + assert updated_agent["preamble"] == "updated preamble" + assert updated_agent["temperature"] == 0.7 + assert updated_agent["model"] == "command-r" + assert updated_agent["deployment"] == CohereDeployment.name() + + +def filter_default_agent(agents: list) -> list: + return [agent for agent in agents if agent.get("id") != DEFAULT_AGENT_ID] + + +def test_create_agent_missing_name( + session_client: TestClient, session: Session, user: User, +) -> None: + request_json = { + "description": "test description", + "preamble": "test preamble", + "temperature": 0.5, + "model": "command-r-plus", + "deployment": CohereDeployment.name(), + } + response = session_client.post( + "/v1/agents", json=request_json, headers={"User-Id": user.id} + ) + assert response.status_code == 400 + assert response.json() == {"detail": "Name, model, and deployment are required."} + + +def test_create_agent_missing_model( + session_client: TestClient, session: Session, user: User, +) -> None: + request_json = { + "name": "test agent", + "description": "test description", + "preamble": "test preamble", + "temperature": 0.5, + "deployment": CohereDeployment.name(), + } + response = session_client.post( + "/v1/agents", json=request_json, headers={"User-Id": user.id} + ) + assert response.status_code == 400 + assert response.json() == {"detail": "Name, model, and deployment are required."} + + +def test_create_agent_missing_deployment( + session_client: TestClient, session: Session, user: User, +) -> None: + request_json = { + "name": "test agent", + "description": "test description", + "preamble": "test preamble", + "temperature": 0.5, + "model": "command-r-plus", + } + response = session_client.post( + "/v1/agents", json=request_json, headers={"User-Id": user.id} + ) + assert response.status_code == 400 + assert response.json() == {"detail": "Name, model, and deployment are required."} + + +def test_create_agent_missing_user_id_header( + session_client: TestClient, session: Session, user: User, +) -> None: + request_json = { + "name": "test agent", + "model": "command-r-plus", + "deployment": CohereDeployment.name(), + } + response = session_client.post("/v1/agents", json=request_json) + assert response.status_code == 401 + assert response.json() == { "detail": "User-Id required in request headers." } + + +def test_create_agent_invalid_deployment( + session_client: TestClient, session: Session, user: User, +) -> None: + request_json = { + "name": "test agent", + "version": 1, + "description": "test description", + "preamble": "test preamble", + "temperature": 0.5, + "model": "command-r-plus", + "deployment": "not a real deployment", + } + + with pytest.raises(DeploymentNotFoundError): + session_client.post( + "/v1/agents", json=request_json, headers={"User-Id": user.id} + ) + + +def test_create_agent_deployment_not_in_db( + session_client: TestClient, + session: Session, + user: User, + mock_cohere_list_models, +) -> None: + request_json = { + "name": "test agent", + "description": "test description", + "preamble": "test preamble", + "temperature": 0.5, + "model": "command-r-plus", + "deployment": CohereDeployment.name(), + } + cohere_deployment = deployment_crud.get_deployment_by_name(session, CohereDeployment.name()) + if cohere_deployment: + deployment_crud.delete_deployment(session, cohere_deployment.id) + response = session_client.post( + "/v1/agents", json=request_json, headers={"User-Id": user.id} + ) + cohere_deployment = deployment_crud.get_deployment_by_name(session, CohereDeployment.name()) + deployment_models = cohere_deployment.models + deployment_models_list = [model.name for model in deployment_models] + assert response.status_code == 200 + assert cohere_deployment + assert "command-r-plus" in deployment_models_list + + +def test_create_agent_invalid_tool( + session_client: TestClient, session: Session, user: User, +) -> None: + request_json = { + "name": "test agent", + "model": "command-r-plus", + "deployment": CohereDeployment.name(), + "tools": [Tool.Calculator.value.ID, "fake_tool"], + } + + response = session_client.post( + "/v1/agents", json=request_json, headers={"User-Id": user.id} + ) + assert response.status_code == 404 + assert response.json() == {"detail": "Tool fake_tool not found."} + + +def test_create_existing_agent( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create(name="test agent") + request_json = { + "name": agent.name, + } + + response = session_client.post( + "/v1/agents", json=request_json, headers={"User-Id": user.id} + ) + assert response.status_code == 400 + assert response.json() == {"detail": "Agent test agent already exists."} + + +def test_list_agents_empty_returns_default_agent(session_client: TestClient, session: Session) -> None: + response = session_client.get("/v1/agents", headers={"User-Id": "123"}) + assert response.status_code == 200 + response_agents = response.json() + # Returns default agent + assert len(response_agents) == 1 + + +def test_list_agents( + session_client: TestClient, session: Session, user: User, +) -> None: + num_agents = 3 + for _ in range(num_agents): + _ = get_factory("Agent", session).create(user=user) + + response = session_client.get("/v1/agents", headers={"User-Id": user.id}) + assert response.status_code == 200 + response_agents = filter_default_agent(response.json()) + assert len(response_agents) == num_agents + + +@pytest.mark.skip(reason="We don't yet have the concept of organizations in Toolkit") +def test_list_organization_agents( + session_client: TestClient, + session: Session, + user: User, +) -> None: + num_agents = 3 + organization = get_factory("Organization", session).create() + organization1 = get_factory("Organization", session).create() + for i in range(num_agents): + _ = get_factory("Agent", session).create( + user=user, + organization_id=organization.id, + name=f"agent-{i}-{organization.id}", + ) + _ = get_factory("Agent", session).create( + user=user, organization_id=organization1.id + ) + + response = session_client.get( + "/v1/agents", headers={"User-Id": user.id, "Organization-Id": organization.id} + ) + assert response.status_code == 200 + response_agents = filter_default_agent(response.json()) + agents = sorted(response_agents, key=lambda x: x["name"]) + assert len(response_agents) == num_agents + for i in range(num_agents): + assert agents[i]["name"] == f"agent-{i}-{organization.id}" + + +def test_list_organization_agents_query_param( + session_client: TestClient, + session: Session, + user: User, +) -> None: + num_agents = 3 + organization = get_factory("Organization", session).create() + organization1 = get_factory("Organization", session).create() + for i in range(num_agents): + _ = get_factory("Agent", session).create( + user=user, organization_id=organization.id + ) + _ = get_factory("Agent", session).create( + user=user, + organization_id=organization1.id, + name=f"agent-{i}-{organization1.id}", + ) + + response = session_client.get( + f"/v1/agents?org_id={organization1.id}", + headers={"User-Id": user.id, "Organization-Id": organization.id}, + ) + assert response.status_code == 200 + response_agents = filter_default_agent(response.json()) + agents = sorted(response_agents, key=lambda x: x["name"]) + for i in range(num_agents): + assert agents[i]["name"] == f"agent-{i}-{organization1.id}" + + +def test_list_organization_agents_nonexistent_organization( + session_client: TestClient, + session: Session, + user: User, +) -> None: + response = session_client.get( + "/v1/agents", headers={"User-Id": user.id, "Organization-Id": "123"} + ) + assert response.status_code == 404 + assert response.json() == {"detail": "Organization ID 123 not found."} + + +def test_list_private_agents( + session_client: TestClient, session: Session, user: User, +) -> None: + for _ in range(3): + _ = get_factory("Agent", session).create(user=user, is_private=True) + + user2 = get_factory("User", session).create(id="456") + for _ in range(2): + _ = get_factory("Agent", session).create(user=user2, is_private=True) + + response = session_client.get( + "/v1/agents?visibility=private", headers={"User-Id": user.id} + ) + + assert response.status_code == 200 + response_agents = filter_default_agent(response.json()) + + # Only the agents created by user should be returned + assert len(response_agents) == 3 + + +def test_list_public_agents( + session_client: TestClient, session: Session, user: User, +) -> None: + for _ in range(3): + _ = get_factory("Agent", session).create(user=user, is_private=True) + + user2 = get_factory("User", session).create(id="456") + for _ in range(2): + _ = get_factory("Agent", session).create(user=user2, is_private=False) + + response = session_client.get( + "/v1/agents?visibility=public", headers={"User-Id": user.id} + ) + + assert response.status_code == 200 + response_agents = filter_default_agent(response.json()) + + # Only the agents created by user should be returned + assert len(response_agents) == 2 + + +def list_public_and_private_agents( + session_client: TestClient, session: Session, user: User, +) -> None: + for _ in range(3): + _ = get_factory("Agent", session).create(user=user, is_private=True) + + user2 = get_factory("User", session).create(id="456") + for _ in range(2): + _ = get_factory("Agent", session).create(user=user2, is_private=False) + + response = session_client.get( + "/v1/agents?visibility=all", headers={"User-Id": user.id} + ) + + assert response.status_code == 200 + response_agents = response.json() + + # Only the agents created by user should be returned + assert len(response_agents) == 5 + + +def test_list_agents_with_pagination( + session_client: TestClient, session: Session, user: User, +) -> None: + for _ in range(5): + _ = get_factory("Agent", session).create(user=user) + + response = session_client.get( + "/v1/agents?limit=3&offset=2", headers={"User-Id": user.id} + ) + assert response.status_code == 200 + response_agents = filter_default_agent(response.json()) + assert len(response_agents) == 3 + + response = session_client.get( + "/v1/agents?limit=2&offset=4", headers={"User-Id": user.id} + ) + assert response.status_code == 200 + response_agents = filter_default_agent(response.json()) + assert len(response_agents) == 1 + + +def test_get_agent(session_client: TestClient, session: Session, user: User) -> None: + agent = get_factory("Agent", session).create(name="test agent", user_id=user.id) + agent_tool_metadata = get_factory("AgentToolMetadata", session).create( + user_id=user.id, + agent_id=agent.id, + tool_name=Tool.Google_Drive.value.ID, + artifacts=[ + { + "name": "/folder1", + "ids": "folder1", + "type": "folder_id", + }, + { + "name": "file1.txt", + "ids": "file1", + "type": "file_id", + }, + ], + ) + + response = session_client.get( + f"/v1/agents/{agent.id}", headers={"User-Id": user.id} + ) + assert response.status_code == 200 + response_agent = response.json() + assert response_agent["name"] == agent.name + assert response_agent["tools_metadata"][0]["tool_name"] == Tool.Google_Drive.value.ID + assert ( + response_agent["tools_metadata"][0]["artifacts"] + == agent_tool_metadata.artifacts + ) + + +def test_get_nonexistent_agent( + session_client: TestClient, session: Session, user: User, +) -> None: + response = session_client.get("/v1/agents/456", headers={"User-Id": user.id}) + assert response.status_code == 404 + assert response.json() == {"detail": "Agent with ID 456 not found."} + + +def test_get_public_agent(session_client: TestClient, session: Session, user) -> None: + user2 = get_factory("User", session).create(id="456") + agent = get_factory("Agent", session).create( + name="test agent", user_id=user2.id, is_private=False + ) + + response = session_client.get( + f"/v1/agents/{agent.id}", headers={"User-Id": user.id} + ) + + assert response.status_code == 200 + response_agent = response.json() + assert response_agent["name"] == agent.name + + +def test_get_private_agent(session_client: TestClient, session: Session, user: User) -> None: + agent = get_factory("Agent", session).create( + name="test agent", user=user, is_private=True + ) + + response = session_client.get( + f"/v1/agents/{agent.id}", headers={"User-Id": user.id} + ) + + assert response.status_code == 200 + response_agent = response.json() + assert response_agent["name"] == agent.name + + +def test_get_private_agent_by_another_user( + session_client: TestClient, session: Session, user: User, +) -> None: + user2 = get_factory("User", session).create(id="456") + agent = get_factory("Agent", session).create( + name="test agent", user_id=user2.id, is_private=True + ) + + response = session_client.get( + f"/v1/agents/{agent.id}", headers={"User-Id": user.id} + ) + + assert response.status_code == 404 + assert response.json() == {"detail": f"Agent with ID {agent.id} not found."} + + +def test_partial_update_agent(session_client: TestClient, session: Session) -> None: + user = get_factory("User", session).create(id="123") + agent = get_factory("Agent", session).create( + name="test agent", + version=1, + description="test description", + preamble="test preamble", + temperature=0.5, + tools=[Tool.Calculator.value.ID], + user=user, + ) + + request_json = { + "name": "updated name", + "tools": [Tool.Search_File.value.ID, Tool.Read_File.value.ID], + } + + response = session_client.put( + f"/v1/agents/{agent.id}", + json=request_json, + headers={"User-Id": user.id}, + ) + assert response.status_code == 200 + updated_agent = response.json() + assert updated_agent["name"] == "updated name" + assert updated_agent["version"] == 1 + assert updated_agent["description"] == "test description" + assert updated_agent["preamble"] == "test preamble" + assert updated_agent["temperature"] == 0.5 + assert updated_agent["tools"] == [Tool.Search_File.value.ID, Tool.Read_File.value.ID] + + +def test_update_agent_with_tool_metadata( + session_client: TestClient, session: Session, +) -> None: + user = get_factory("User", session).create(id="123") + agent = get_factory("Agent", session).create( + name="test agent", + version=1, + description="test description", + preamble="test preamble", + temperature=0.5, + user=user, + ) + agent_tool_metadata = get_factory("AgentToolMetadata", session).create( + user_id=user.id, + agent_id=agent.id, + tool_name=Tool.Google_Drive.value.ID, + artifacts=[ + { + "url": "test", + "name": "test", + "type": "folder", + }, + ], + ) + + request_json = { + "tools_metadata": [ + { + "user_id": user.id, + "organization_id": None, + "id": agent_tool_metadata.id, + "tool_name": "google_drive", + "artifacts": [ + { + "url": "test", + "name": "test", + "type": "folder", + } + ], + } + ], + } + + response = session_client.put( + f"/v1/agents/{agent.id}", + json=request_json, + headers={"User-Id": user.id}, + ) + + assert response.status_code == 200 + response.json() + + tool_metadata = ( + session.query(AgentToolMetadata) + .filter(AgentToolMetadata.agent_id == agent.id) + .all() + ) + assert len(tool_metadata) == 1 + assert tool_metadata[0].tool_name == "google_drive" + assert tool_metadata[0].artifacts == [ + {"url": "test", "name": "test", "type": "folder"} + ] + + +def test_update_agent_with_tool_metadata_and_new_tool_metadata( + session_client: TestClient, session: Session, +) -> None: + user = get_factory("User", session).create(id="123") + agent = get_factory("Agent", session).create( + name="test agent", + version=1, + description="test description", + preamble="test preamble", + temperature=0.5, + user=user, + ) + agent_tool_metadata = get_factory("AgentToolMetadata", session).create( + user_id=user.id, + agent_id=agent.id, + tool_name=Tool.Google_Drive.value.ID, + artifacts=[ + { + "url": "test", + "name": "test", + "type": "folder", + }, + ], + ) + + request_json = { + "tools_metadata": [ + { + "user_id": user.id, + "organization_id": None, + "id": agent_tool_metadata.id, + "tool_name": "google_drive", + "artifacts": [ + { + "url": "test", + "name": "test", + "type": "folder", + } + ], + }, + { + "tool_name": "search_file", + "artifacts": [ + { + "url": "test", + "name": "test", + "type": "file", + } + ], + }, + ], + } + + response = session_client.put( + f"/v1/agents/{agent.id}", + json=request_json, + headers={"User-Id": user.id}, + ) + + assert response.status_code == 200 + + tool_metadata = ( + session.query(AgentToolMetadata) + .filter(AgentToolMetadata.agent_id == agent.id) + .all() + ) + assert len(tool_metadata) == 2 + drive_tool = None + search_tool = None + for tool in tool_metadata: + if tool.tool_name == "google_drive": + drive_tool = tool + if tool.tool_name == "search_file": + search_tool = tool + assert drive_tool.tool_name == "google_drive" + assert drive_tool.artifacts == [{"url": "test", "name": "test", "type": "folder"}] + assert search_tool.tool_name == "search_file" + assert search_tool.artifacts == [{"url": "test", "name": "test", "type": "file"}] + + +def test_update_agent_remove_existing_tool_metadata( + session_client: TestClient, session: Session, +) -> None: + user = get_factory("User", session).create(id="123") + agent = get_factory("Agent", session).create( + name="test agent", + version=1, + description="test description", + preamble="test preamble", + temperature=0.5, + user=user, + ) + get_factory("AgentToolMetadata", session).create( + user_id=user.id, + agent_id=agent.id, + tool_name=Tool.Google_Drive.value.ID, + artifacts=[ + { + "url": "test", + "name": "test", + "type": "folder", + }, + ], + ) + + request_json = { + "tools_metadata": [], + } + + response = session_client.put( + f"/v1/agents/{agent.id}", + json=request_json, + headers={"User-Id": user.id}, + ) + + assert response.status_code == 200 + response.json() + + tool_metadata = ( + session.query(AgentToolMetadata) + .filter(AgentToolMetadata.agent_id == agent.id) + .all() + ) + assert len(tool_metadata) == 0 + + +def test_update_nonexistent_agent( + session_client: TestClient, session: Session, user: User, +) -> None: + request_json = { + "name": "updated name", + } + response = session_client.put( + "/v1/agents/456", json=request_json, headers={"User-Id": user.id} + ) + assert response.status_code == 404 + assert response.json() == {"detail": "Agent with ID 456 not found."} + + +def test_update_agent_wrong_user( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create(user=user) + request_json = { + "name": "updated name", + } + + response = session_client.put( + f"/v1/agents/{agent.id}", json=request_json, headers={"User-Id": "user-id"} + ) + assert response.status_code == 401 + assert response.json() == { + "detail": f"Agent with ID {agent.id} does not belong to user." + } + + +def test_update_agent_invalid_model( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create( + name="test agent", + version=1, + description="test description", + preamble="test preamble", + temperature=0.5, + user=user, + ) + + request_json = { + "model": "not a real model", + "deployment": CohereDeployment.name(), + } + + response = session_client.put( + f"/v1/agents/{agent.id}", json=request_json, headers={"User-Id": user.id} + ) + assert response.status_code == 400 + assert response.json() == { + "detail": "Model not a real model not found for deployment Cohere Platform." + } + + +def test_update_agent_invalid_deployment( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create( + name="test agent", + version=1, + description="test description", + preamble="test preamble", + temperature=0.5, + user=user, + ) + + request_json = { + "model": "command-r", + "deployment": "not a real deployment", + } + + with pytest.raises(DeploymentNotFoundError): + session_client.put( + f"/v1/agents/{agent.id}", json=request_json, headers={"User-Id": user.id} + ) + + +def test_update_agent_invalid_tool( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create( + name="test agent", + version=1, + description="test description", + preamble="test preamble", + temperature=0.5, + user=user, + ) + + request_json = { + "model": "not a real model", + "deployment": "not a real deployment", + "tools": [Tool.Calculator.value.ID, "not a real tool"], + } + + response = session_client.put( + f"/v1/agents/{agent.id}", json=request_json, headers={"User-Id": user.id} + ) + assert response.status_code == 404 + assert response.json() == {"detail": "Tool not a real tool not found."} + + +def test_update_private_agent( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create( + name="test agent", + version=1, + description="test description", + preamble="test preamble", + temperature=0.5, + is_private=True, + user=user, + ) + + request_json = { + "name": "updated name", + } + + response = session_client.put( + f"/v1/agents/{agent.id}", json=request_json, headers={"User-Id": user.id} + ) + assert response.status_code == 200 + updated_agent = response.json() + assert updated_agent["name"] == "updated name" + assert updated_agent["is_private"] + + +def test_update_public_agent( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create( + name="test agent", + version=1, + description="test description", + preamble="test preamble", + temperature=0.5, + is_private=False, + user=user, + ) + + request_json = { + "name": "updated name", + } + + response = session_client.put( + f"/v1/agents/{agent.id}", json=request_json, headers={"User-Id": user.id} + ) + assert response.status_code == 200 + updated_agent = response.json() + assert updated_agent["name"] == "updated name" + assert not updated_agent["is_private"] + + +def test_update_agent_change_visibility_to_public( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create( + name="test agent", + version=1, + description="test description", + preamble="test preamble", + temperature=0.5, + is_private=True, + user=user, + ) + + request_json = { + "is_private": False, + } + + response = session_client.put( + f"/v1/agents/{agent.id}", json=request_json, headers={"User-Id": user.id} + ) + assert response.status_code == 200 + updated_agent = response.json() + assert not updated_agent["is_private"] + + +def test_update_agent_change_visibility_to_private( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create( + name="test agent", + version=1, + description="test description", + preamble="test preamble", + temperature=0.5, + is_private=False, + user=user, + ) + + request_json = { + "is_private": True, + } + + response = session_client.put( + f"/v1/agents/{agent.id}", json=request_json, headers={"User-Id": user.id} + ) + assert response.status_code == 200 + updated_agent = response.json() + assert updated_agent["is_private"] + + +def test_update_agent_change_visibility_to_private_delete_snapshot( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create( + name="test agent", + version=1, + description="test description", + preamble="test preamble", + temperature=0.5, + is_private=False, + user=user, + ) + conversation = get_factory("Conversation", session).create( + agent_id=agent.id, user_id=user.id + ) + message = get_factory("Message", session).create( + conversation_id=conversation.id, user_id=user.id + ) + snapshot = get_factory("Snapshot", session).create( + conversation_id=conversation.id, + user_id=user.id, + agent_id=agent.id, + last_message_id=message.id, + organization_id=None, + ) + snapshot_id = snapshot.id + + request_json = { + "is_private": True, + } + + response = session_client.put( + f"/v1/agents/{agent.id}", json=request_json, headers={"User-Id": user.id} + ) + + assert response.status_code == 200 + updated_agent = response.json() + assert updated_agent["is_private"] + + snapshot = session.get(Snapshot, snapshot_id) + assert snapshot is None + + +def test_delete_public_agent( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create(user=user, is_private=False) + response = session_client.delete( + f"/v1/agents/{agent.id}", headers={"User-Id": user.id} + ) + assert response.status_code == 200 + assert response.json() == {} + + agent = session.get(Agent, agent.id) + assert agent is None + + +def test_delete_private_agent( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create(user=user, is_private=True) + response = session_client.delete( + f"/v1/agents/{agent.id}", headers={"User-Id": user.id} + ) + assert response.status_code == 200 + assert response.json() == {} + + agent = session.get(Agent, agent.id) + assert agent is None + + +def test_cannot_delete_private_agent_not_belonging_to_user_id( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create(user=user, is_private=True) + other_user = get_factory("User", session).create() + response = session_client.delete( + f"/v1/agents/{agent.id}", headers={"User-Id": other_user.id} + ) + assert response.status_code == 404 + assert response.json() == {"detail": f"Agent with ID {agent.id} not found."} + + agent = session.get(Agent, agent.id) + assert agent is not None + + +def test_cannot_delete_public_agent_not_belonging_to_user_id( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create(user=user, is_private=False) + other_user = get_factory("User", session).create() + response = session_client.delete( + f"/v1/agents/{agent.id}", headers={"User-Id": other_user.id} + ) + assert response.status_code == 401 + assert response.json() == {"detail": "Could not delete Agent."} + + agent = session.get(Agent, agent.id) + assert agent is not None + + +def test_fail_delete_nonexistent_agent( + session_client: TestClient, session: Session, user: User, +) -> None: + response = session_client.delete("/v1/agents/456", headers={"User-Id": user.id}) + assert response.status_code == 404 + assert response.json() == {"detail": "Agent with ID 456 not found."} + + +# Test create agent tool metadata +def test_create_agent_tool_metadata( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create(user=user) + request_json = { + "tool_name": Tool.Google_Drive.value.ID, + "artifacts": [ + { + "name": "/folder1", + "ids": "folder1", + "type": "folder_id", + }, + { + "name": "file1.txt", + "ids": "file1", + "type": "file_id", + }, + ], + } + + response = session_client.post( + f"/v1/agents/{agent.id}/tool-metadata", + json=request_json, + headers={"User-Id": user.id}, + ) + assert response.status_code == 200 + response_agent_tool_metadata = response.json() + + assert response_agent_tool_metadata["tool_name"] == request_json["tool_name"] + assert response_agent_tool_metadata["artifacts"] == request_json["artifacts"] + + agent_tool_metadata = session.get( + AgentToolMetadata, response_agent_tool_metadata["id"] + ) + assert agent_tool_metadata.tool_name == Tool.Google_Drive.value.ID + assert agent_tool_metadata.artifacts == [ + { + "name": "/folder1", + "ids": "folder1", + "type": "folder_id", + }, + { + "name": "file1.txt", + "ids": "file1", + "type": "file_id", + }, + ] + + +def test_update_agent_tool_metadata( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create(user=user) + agent_tool_metadata = get_factory("AgentToolMetadata", session).create( + user_id=user.id, + agent_id=agent.id, + tool_name=Tool.Google_Drive.value.ID, + artifacts=[ + { + "name": "/folder1", + "ids": "folder1", + "type": "folder_id", + }, + { + "name": "file1.txt", + "ids": "file1", + "type": "file_id", + }, + ], + ) + + request_json = { + "artifacts": [ + { + "name": "/folder2", + "ids": "folder2", + "type": "folder_id", + }, + { + "name": "file2.txt", + "ids": "file2", + "type": "file_id", + }, + ], + } + + response = session_client.put( + f"/v1/agents/{agent.id}/tool-metadata/{agent_tool_metadata.id}", + json=request_json, + headers={"User-Id": user.id}, + ) + + assert response.status_code == 200 + response_agent_tool_metadata = response.json() + assert response_agent_tool_metadata["id"] == agent_tool_metadata.id + + assert response_agent_tool_metadata["artifacts"] == [ + { + "name": "/folder2", + "ids": "folder2", + "type": "folder_id", + }, + { + "name": "file2.txt", + "ids": "file2", + "type": "file_id", + }, + ] + + +def test_get_agent_tool_metadata( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create(user=user) + agent_tool_metadata_1 = get_factory("AgentToolMetadata", session).create( + user_id=user.id, + agent_id=agent.id, + tool_name=Tool.Google_Drive.value.ID, + artifacts=[ + {"name": "/folder", "ids": ["folder1", "folder2"], "type": "folder_ids"} + ], + ) + agent_tool_metadata_2 = get_factory("AgentToolMetadata", session).create( + user_id=user.id, + agent_id=agent.id, + tool_name=Tool.Search_File.value.ID, + artifacts=[{"name": "file.txt", "ids": ["file1", "file2"], "type": "file_ids"}], + ) + + response = session_client.get( + f"/v1/agents/{agent.id}/tool-metadata", headers={"User-Id": user.id} + ) + assert response.status_code == 200 + response_agent_tool_metadata = response.json() + assert response_agent_tool_metadata[0]["id"] == agent_tool_metadata_1.id + assert ( + response_agent_tool_metadata[0]["artifacts"] == agent_tool_metadata_1.artifacts + ) + assert response_agent_tool_metadata[1]["id"] == agent_tool_metadata_2.id + assert ( + response_agent_tool_metadata[1]["artifacts"] == agent_tool_metadata_2.artifacts + ) + + +def test_delete_agent_tool_metadata( + session_client: TestClient, session: Session, user: User, +) -> None: + agent = get_factory("Agent", session).create(user=user) + agent_tool_metadata = get_factory("AgentToolMetadata", session).create( + user_id=user.id, + agent_id=agent.id, + tool_name=Tool.Google_Drive.value.ID, + artifacts=[ + { + "name": "/folder1", + "ids": "folder1", + "type": "folder_id", + }, + { + "name": "file1.txt", + "ids": "file1", + "type": "file_id", + }, + ], + ) + + response = session_client.delete( + f"/v1/agents/{agent.id}/tool-metadata/{agent_tool_metadata.id}", + headers={"User-Id": user.id}, + ) + assert response.status_code == 200 + assert response.json() == {} + + agent_tool_metadata = session.get(AgentToolMetadata, agent_tool_metadata.id) + assert agent_tool_metadata is None + + +def test_fail_delete_nonexistent_agent_tool_metadata( + session_client: TestClient, session: Session, user: User, +) -> None: + get_factory("Agent", session).create(user=user, id="456") + response = session_client.delete( + "/v1/agents/456/tool-metadata/789", headers={"User-Id": user.id} + ) + assert response.status_code == 404 + assert response.json() == {"detail": "Agent tool metadata with ID 789 not found."} diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/integration/routers/test_conversation.py b/western_ai/canada/cohere-toolkit/src/backend/tests/integration/routers/test_conversation.py new file mode 100644 index 0000000000000000000000000000000000000000..1c86f004209832cf844f15878755be3005b0c11c --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/integration/routers/test_conversation.py @@ -0,0 +1,207 @@ +import pytest +from fastapi.testclient import TestClient +from sqlalchemy.orm import Session + +from backend.config import Settings +from backend.database_models import Conversation +from backend.model_deployments.cohere_platform import CohereDeployment +from backend.schemas.user import User +from backend.tests.unit.factories import get_factory + +_IS_GOOGLE_CLOUD_API_KEY_SET = bool(Settings().get('google_cloud.api_key')) + + +def test_search_conversations( + session_client: TestClient, + session: Session, + user: User, + mock_available_model_deployments, +) -> None: + conversation = get_factory("Conversation", session).create( + title="test title", user_id=user.id + ) + response = session_client.get( + "/v1/conversations:search", + headers={"User-Id": user.id}, + params={"query": "test"}, + ) + results = response.json() + + assert response.status_code == 200 + assert len(results) == 1 + assert results[0]["id"] == conversation.id + + +def test_search_conversations_with_reranking( + session_client: TestClient, + session: Session, + user: User, + mock_available_model_deployments, +) -> None: + _ = get_factory("Conversation", session).create( + title="Hello, how are you?", text_messages=[], user_id=user.id + ) + conversation2 = get_factory("Conversation", session).create( + title="There are are seven colors in the rainbow", + text_messages=[], + user_id=user.id, + ) + response = session_client.get( + "/v1/conversations:search", + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + params={"query": "color"}, + ) + results = response.json() + + assert response.status_code == 200 + assert len(results) == 1 + assert results[0]["id"] == conversation2.id + + +def test_search_conversations_no_conversations( + session_client: TestClient, + session: Session, + user: User, +) -> None: + _ = get_factory("Conversation", session).create(title="test title", user_id=user.id) + user2 = get_factory("User", session).create() + + response = session_client.get( + "/v1/conversations:search", + headers={"User-Id": user2.id}, + params={"query": "test"}, + ) + + assert response.status_code == 200 + assert response.json() == [] + + +def test_generate_title( + session_client: TestClient, + session: Session, + user: User, + mock_available_model_deployments, +) -> None: + conversation_initial = get_factory("Conversation", session).create(user_id=user.id) + response = session_client.post( + f"/v1/conversations/{conversation_initial.id}/generate-title", + headers={"User-Id": conversation_initial.user_id}, + ) + response_json = response.json() + + assert response.status_code == 200 + assert response_json["title"] is not None + + # Check if the conversation was updated + conversation = ( + session.query(Conversation) + .filter_by(id=conversation_initial.id, user_id=conversation_initial.user_id) + .first() + ) + assert conversation is not None + assert conversation.title == response_json["title"] + assert response_json["error"] is None + + +def test_fail_generate_title_missing_user_id( + session_client: TestClient, + session: Session, + user: User, +) -> None: + conversation = get_factory("Conversation", session).create(user_id=user.id) + response = session_client.post( + f"/v1/conversations/{conversation.id}/generate-title" + ) + assert response.status_code == 401 + assert response.json() == { "detail": "User-Id required in request headers." } + + +def test_fail_generate_title_nonexistent_conversation( + session_client: TestClient, + session: Session, + user: User, +) -> None: + response = session_client.post( + "/v1/conversations/123/generate-title", headers={"User-Id": user.id} + ) + assert response.status_code == 404 + assert response.json() == {"detail": "Conversation with ID: 123 not found."} + + +@pytest.mark.skip(reason="Restore this test when we get access to run models on Huggingface") +def test_generate_title_error_invalid_model( + session_client: TestClient, + session: Session, + user: User, +) -> None: + conversation = get_factory("Conversation", session).create(user_id=user.id) + response = session_client.post( + f"/v1/conversations/{conversation.id}/generate-title?model=invalid", + headers={"User-Id": conversation.user_id}, + ) + + assert response.status_code == 200 + response = response.json() + + # Since it's a streaming endpoint, the error is silent + # The response code is 200 but there's an error message + assert ( + response["error"] + == "status_code: 404, body: {'message': \"model 'invalid' not found, make sure the correct model ID was used and that you have access to the model.\"}" + ) + assert response["title"] == "" + + +# SYNTHESIZE + + +@pytest.mark.skipif(not _IS_GOOGLE_CLOUD_API_KEY_SET, reason="Google Cloud API key not set, skipping test") +def test_synthesize_english_message( + session_client: TestClient, + session: Session, + user: User, +) -> None: + conversation = get_factory("Conversation", session).create(user_id=user.id) + message = get_factory("Message", session).create( + id="1", text="Hello world!", conversation_id=conversation.id, user_id=user.id + ) + response = session_client.get( + f"/v1/conversations/{conversation.id}/synthesize/{message.id}", + headers={"User-Id": conversation.user_id}, + ) + assert response.status_code == 200 + assert response.headers["Content-Type"] == "audio/mp3" + + +@pytest.mark.skipif(not _IS_GOOGLE_CLOUD_API_KEY_SET, reason="Google Cloud API key not set, skipping test") +def test_synthesize_non_english_message( + session_client: TestClient, + session: Session, + user: User, +) -> None: + conversation = get_factory("Conversation", session).create(user_id=user.id) + message = get_factory("Message", session).create( + id="1", text="Bonjour le monde!", conversation_id=conversation.id, user_id=user.id + ) + response = session_client.get( + f"/v1/conversations/{conversation.id}/synthesize/{message.id}", + headers={"User-Id": conversation.user_id}, + ) + assert response.status_code == 200 + assert response.headers["Content-Type"] == "audio/mp3" + + +def test_fail_synthesize_message_nonexistent_message( + session_client: TestClient, + session: Session, + user: User, +) -> None: + response = session_client.get( + "/v1/conversations/123/synthesize/456", + headers={"User-Id": user.id}, + ) + assert response.status_code == 404 + assert response.json() == {"detail": "Message with ID: 456 not found."} diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/README.md b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/README.md new file mode 100644 index 0000000000000000000000000000000000000000..89da9eb729cc3e99fb173c75d9891f57ec05d3a6 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/README.md @@ -0,0 +1,40 @@ +# Writing Unit Tests + +This README will explain how to best write unit tests for the OSS Toolkit project. + +To start, run the following commands + +``` +make install +make test-db +make run-unit-tests +``` + +## Testing the Database + +Some of the unit tests rely on a database being present. The tests are expecting a postgres DB to be running in a docker container. the `make test-db` target will create the test database required for the tests to run. + +## Using Fixtures + +The main `tests/conftest.py` file will contain multiple fixtures that you can use within your unit tests, you will be able to use the `client` fixture to testing APIs without DB calls, or the `session` and `session_client` fixtures to test any API that requires interacting with the test DB. + +You can create your own fixtures by importing `pytest` and decorating a method with `@pytest.fixture`, then anywhere in the test architecture you will be able to add that fixture to your unit test by adding the name of the fixture in your test method parameters. + +For example, + +```python +@pytest.fixture +def myfixture(): + yield something + +def test_something(myfixture): + .. +``` + +The fixtures and tests do not need to be in the same file, nor do they need to be imported. Pytest will handle that for you. + +## Using Factories + +To write tests that require any initial setup, you can use `factory_boy` Factories to create objects from any model in the database. See the `tests/factories` folder for examples. + +The `tests/factories/__init__.py` file will contain a `get_factory()` method that can be imported and used throughout the tests. To add a new factory, simply add a file under the `factories` folder that inherits from `BaseFactory`, and add it to the `FACTORY_MAPPING` dictionary in the `__init__` file. \ No newline at end of file diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/chat/test_tool_calls.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/chat/test_tool_calls.py new file mode 100644 index 0000000000000000000000000000000000000000..0e5b8819a74efbb3df9d029462f593229edad3ea --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/chat/test_tool_calls.py @@ -0,0 +1,390 @@ +import asyncio +from typing import Any, Dict, List +from unittest.mock import patch + +import pytest +from fastapi import HTTPException + +from backend.chat.custom.tool_calls import async_call_tools +from backend.config.tools import Tool +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.services.context import Context +from backend.tests.unit.model_deployments.mock_deployments import MockCohereDeployment +from backend.tools.base import BaseTool + + +@pytest.fixture +def mock_get_available_tools(): + with patch("backend.chat.custom.tool_calls.get_available_tools") as mock: + yield mock + + +def test_async_call_tools_success(mock_get_available_tools) -> None: + class MockCalculator(BaseTool): + ID = "toolkit_calculator" + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Calculator", + implementation=MockCalculator, + parameter_definitions={ + "code": { + "description": "The expression for the calculator to evaluate, it should be a valid mathematical expression.", + "type": "str", + "required": True, + } + }, + is_visible=False, + is_available=True, + category=ToolCategory.Function, + error_message=cls.generate_error_message(), + description="A powerful multi-purpose calculator capable of a wide array of math calculations.", + ) + + async def call( + self, parameters: dict, ctx: Any, **kwargs: Any + ) -> List[Dict[str, Any]]: + return [{"result": 42}] + + ctx = Context() + chat_history = [ + { + "tool_calls": [ + {"name": "toolkit_calculator", "parameters": {"code": "6*7"}} + ] + } + ] + mock_get_available_tools.return_value = {Tool.Calculator.value.ID: MockCalculator.get_tool_definition()} + results = asyncio.run( + async_call_tools(chat_history, MockCohereDeployment(), ctx) + ) + assert results == [ + { + "call": { + "name": "toolkit_calculator", + "parameters": {"code": "6*7"}, + }, + "outputs": [{"result": 42}], + } + ] + + +def test_async_call_tools_failure(mock_get_available_tools) -> None: + class MockCalculator(BaseTool): + ID = "toolkit_calculator" + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Calculator", + implementation=MockCalculator, + parameter_definitions={ + "code": { + "description": "The expression for the calculator to evaluate, it should be a valid mathematical expression.", + "type": "str", + "required": True, + } + }, + is_visible=False, + is_available=True, + category=ToolCategory.Function, + error_message=cls.generate_error_message(), + description="A powerful multi-purpose calculator capable of a wide array of math calculations.", + ) + + async def call( + self, parameters: dict, ctx: Any, **kwargs: Any + ) -> List[Dict[str, Any]]: + raise Exception("Calculator failed") + + ctx = Context() + chat_history = [ + { + "tool_calls": [ + {"name": "toolkit_calculator", "parameters": {"code": "6*7"}} + ] + } + ] + mock_get_available_tools.return_value = {Tool.Calculator.value.ID: MockCalculator.get_tool_definition()} + results = asyncio.run( + async_call_tools(chat_history, MockCohereDeployment(), ctx) + ) + assert results == [ + { + "call": { + "name": "toolkit_calculator", + "parameters": {"code": "6*7"}, + }, + "outputs": [{'type': 'other', 'success': False, 'text': 'Error calling tool toolkit_calculator.', 'details': 'Calculator failed'}], + }, + ] + +@patch("backend.chat.custom.tool_calls.TIMEOUT_SECONDS", 1) +def test_async_call_tools_timeout(mock_get_available_tools) -> None: + class MockCalculator(BaseTool): + ID = "toolkit_calculator" + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Calculator", + implementation=MockCalculator, + parameter_definitions={ + "code": { + "description": "The expression for the calculator to evaluate, it should be a valid mathematical expression.", + "type": "str", + "required": True, + } + }, + is_visible=False, + is_available=True, + category=ToolCategory.Function, + error_message=cls.generate_error_message(), + description="A powerful multi-purpose calculator capable of a wide array of math calculations.", + ) + + async def call( + self, parameters: dict, ctx: Any, **kwargs: Any + ) -> List[Dict[str, Any]]: + await asyncio.sleep(3) + return [{"result": 42}] + + ctx = Context() + chat_history = [ + { + "tool_calls": [ + {"name": "toolkit_calculator", "parameters": {"code": "6*7"}} + ] + } + ] + mock_get_available_tools.return_value = {Tool.Calculator.value.ID: MockCalculator.get_tool_definition()} + + with pytest.raises(HTTPException) as excinfo: + asyncio.run(async_call_tools(chat_history, MockCohereDeployment(), ctx)) + assert str(excinfo.value.status_code) == "500" + assert ( + str(excinfo.value.detail) == "Timeout while calling tools with timeout: 1" + ) + + +def test_async_call_tools_failure_and_success(mock_get_available_tools) -> None: + class MockWebScrape(BaseTool): + ID = "web_scrape" + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Web Scrape", + implementation=MockWebScrape, + parameter_definitions={ + "url": { + "description": "The url to scrape.", + "type": "str", + "required": True, + }, + "query": { + "description": "The query to use to select the most relevant passages to return. Using an empty string will return the passages in the order they appear on the webpage", + "type": "str", + "required": False, + }, + }, + is_visible=False, + is_available=True, + error_message=cls.generate_error_message(), + category=ToolCategory.DataLoader, + description="Scrape and returns the textual contents of a webpage as a list of passages for a given url.", + ) + async def call( + self, parameters: dict, ctx: Any, **kwargs: Any + ) -> List[Dict[str, Any]]: + raise Exception("Web scrape failed") + + class MockCalculator(BaseTool): + ID = "toolkit_calculator" + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Calculator", + implementation=MockCalculator, + parameter_definitions={ + "code": { + "description": "The expression for the calculator to evaluate, it should be a valid mathematical expression.", + "type": "str", + "required": True, + } + }, + is_visible=False, + is_available=True, + category=ToolCategory.Function, + error_message=cls.generate_error_message(), + description="A powerful multi-purpose calculator capable of a wide array of math calculations.", + ) + + async def call( + self, parameters: dict, ctx: Any, **kwargs: Any + ) -> List[Dict[str, Any]]: + return [{"result": 42}] + + ctx = Context() + chat_history = [ + { + "tool_calls": [ + {"name": "web_scrape", "parameters": {"code": "6*7"}}, + {"name": "toolkit_calculator", "parameters": {"code": "6*7"}}, + ] + } + ] + mock_get_available_tools.return_value = { + Tool.Calculator.value.ID: MockCalculator.get_tool_definition(), + Tool.Web_Scrape.value.ID: MockWebScrape.get_tool_definition(), + } + + results = asyncio.run( + async_call_tools(chat_history, MockCohereDeployment(), ctx) + ) + + assert {'call': {'name': 'web_scrape', 'parameters': {'code': '6*7'}}, 'outputs': [ + {'type': 'other', 'success': False, 'text': 'Error calling tool web_scrape.', + 'details': "Model didn't pass required parameter: url"}]} in results + assert { + "call": {"name": "toolkit_calculator", "parameters": {"code": "6*7"}}, + "outputs": [{"result": 42}], + } in results + + +def test_tools_params_checker_invalid_param(mock_get_available_tools) -> None: + class MockCalculator(BaseTool): + ID = "toolkit_calculator" + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Calculator", + implementation=MockCalculator, + parameter_definitions={ + "code": { + "description": "The expression for the calculator to evaluate, it should be a valid mathematical expression.", + "type": "str", + "required": True, + } + }, + is_visible=False, + is_available=True, + category=ToolCategory.Function, + error_message=cls.generate_error_message(), + description="A powerful multi-purpose calculator capable of a wide array of math calculations.", + ) + + async def call( + self, parameters: dict, ctx: Any, **kwargs: Any + ) -> List[Dict[str, Any]]: + return [{"result": 42}] + + ctx = Context() + chat_history = [ + { + "tool_calls": [ + {"name": "toolkit_calculator", "parameters": {"invalid_param": "6*7"}}, + ] + } + ] + mock_get_available_tools.return_value = {Tool.Calculator.value.ID: MockCalculator.get_tool_definition()} + results = asyncio.run( + async_call_tools(chat_history, MockCohereDeployment(), ctx) + ) + assert {'call': {'name': 'toolkit_calculator', 'parameters': {'invalid_param': '6*7'}}, 'outputs': [ + {'details': "Model didn't pass required parameter: code", 'success': False, 'text': 'Error calling tool toolkit_calculator.', + 'type': 'other'}]} in results + +def test_tools_params_checker_invalid_param_type(mock_get_available_tools) -> None: + class MockCalculator(BaseTool): + ID = "toolkit_calculator" + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Calculator", + implementation=MockCalculator, + parameter_definitions={ + "code": { + "description": "The expression for the calculator to evaluate, it should be a valid mathematical expression.", + "type": "str", + "required": True, + } + }, + is_visible=False, + is_available=True, + category=ToolCategory.Function, + error_message=cls.generate_error_message(), + description="A powerful multi-purpose calculator capable of a wide array of math calculations.", + ) + + async def call( + self, parameters: dict, ctx: Any, **kwargs: Any + ) -> List[Dict[str, Any]]: + return [{"result": 42}] + + ctx = Context() + chat_history = [ + { + "tool_calls": [ + {"name": "toolkit_calculator", "parameters": {"code": 6}}, + ] + } + ] + mock_get_available_tools.return_value = {Tool.Calculator.value.ID: MockCalculator.get_tool_definition()} + results = asyncio.run( + async_call_tools(chat_history, MockCohereDeployment(), ctx) + ) + assert {'call': {'name': 'toolkit_calculator', 'parameters': {'code': 6}}, 'outputs': [ + {'type': 'other', 'success': False, 'text': 'Error calling tool toolkit_calculator.', + 'details': "Model passed invalid parameter. Parameter 'code' must be of type str, but got int"}]} in results + +def test_tools_params_checker_required_param_empty(mock_get_available_tools) -> None: + class MockCalculator(BaseTool): + ID = "toolkit_calculator" + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Calculator", + implementation=MockCalculator, + parameter_definitions={ + "code": { + "description": "The expression for the calculator to evaluate, it should be a valid mathematical expression.", + "type": "str", + "required": True, + } + }, + is_visible=False, + is_available=True, + category=ToolCategory.Function, + error_message=cls.generate_error_message(), + description="A powerful multi-purpose calculator capable of a wide array of math calculations.", + ) + + async def call( + self, parameters: dict, ctx: Any, **kwargs: Any + ) -> List[Dict[str, Any]]: + return [{"result": 42}] + + ctx = Context() + chat_history = [ + { + "tool_calls": [ + {"name": "toolkit_calculator", "parameters": {"code": ""}}, + ] + } + ] + mock_get_available_tools.return_value = {Tool.Calculator.value.ID: MockCalculator.get_tool_definition()} + results = asyncio.run( + async_call_tools(chat_history, MockCohereDeployment(), ctx) + ) + assert {'call': {'name': 'toolkit_calculator', 'parameters': {'code': ''}}, 'outputs': [ + {'details': 'Model passed empty value for required parameter: code', 'success': False, + 'text': 'Error calling tool toolkit_calculator.', 'type': 'other'}]} in results diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/cli/test_main.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/cli/test_main.py new file mode 100644 index 0000000000000000000000000000000000000000..c210f03a66abe22a1866e88b02474d267483b10d --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/cli/test_main.py @@ -0,0 +1,15 @@ +# ruff: noqa +from backend.scripts.cli.main import * # NOQA + +""" +These tests are boilerplate and do not test the actual functionality of the code. +They are only used to avoid breaking the codebase due to import errors. +""" + + +def test_show_examples(): + assert show_examples() is None + + +def test_wrap_up(): + assert wrap_up(["test"]) is None diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/configuration.yaml b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/configuration.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7481af3da0e0169d8e83f005a2d380c99259c214 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/configuration.yaml @@ -0,0 +1,42 @@ +deployments: + default_deployment: cohere_platform + enabled_deployments: + sagemaker: + access_key: "sagemaker_access_key" + secret_key: "sagemaker_secret" + session_token: "sagemaker_session_token" + region_name: "sagemaker-region" + endpoint_name: "http://www.example.com/sagemaker" + azure: + api_key: "azure_api_key" + endpoint_url: "http://www.example.com/azure" + bedrock: + region_name: "bedrock-region" + access_key: "bedrock_access_key" + secret_key: "bedrock_secret" + session_token: "bedrock_session_token" + cohere_platform: + api_key: "cohere_api_key" + single_container: + model: "single_container_model" + url: "http://www.example.com/single_container" +database: + url: +redis: + url: +tools: + python_interpreter: + url: +feature_flags: + # Experimental features + use_agents_view: false + # Community features + use_community_features: true +auth: + enabled_auth: + backend_hostname: + frontend_hostname: +logger: + level: INFO + strategy: structlog + renderer: console diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/conftest.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/conftest.py new file mode 100644 index 0000000000000000000000000000000000000000..bd5d934d2618c2ddc20ccd065332ef37e874193d --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/conftest.py @@ -0,0 +1,271 @@ +import os +from typing import Any, Generator +from unittest.mock import patch + +import fakeredis +import pytest +from alembic.command import upgrade +from alembic.config import Config +from fastapi import FastAPI +from fastapi.testclient import TestClient +from redis import Redis +from sqlalchemy import create_engine +from sqlalchemy.orm import Session +from sqlalchemy.sql import text + +from backend.database_models import get_session +from backend.database_models.base import CustomFilterQuery +from backend.database_models.deployment import Deployment +from backend.main import app, create_app +from backend.schemas.chat import StreamEvent +from backend.schemas.organization import Organization +from backend.schemas.user import User +from backend.tests.unit.factories import get_factory + +DATABASE_URL = os.environ.get("DATABASE_URL", "postgresql://postgres:postgres@localhost:5433") +MASTER_DB_NAME = "postgres" +TEST_DB_PREFIX = "postgres_" +MASTER_DATABASE_FULL_URL = f"{DATABASE_URL}/{MASTER_DB_NAME}" + + +def create_test_database(test_db_name: str): + engine = create_engine( + MASTER_DATABASE_FULL_URL, echo=True, isolation_level="AUTOCOMMIT" + ) + + with engine.connect() as connection: + connection.execute(text(f"CREATE DATABASE {test_db_name}")) + engine.dispose() + + +def drop_test_database_if_exists(test_db_name: str): + engine = create_engine( + MASTER_DATABASE_FULL_URL, echo=True, isolation_level="AUTOCOMMIT" + ) + + with engine.connect() as connection: + connection.execute(text(f"DROP DATABASE IF EXISTS {test_db_name}")) + engine.dispose() + + +@pytest.fixture(scope="session") +def fastapi_app() -> Generator[FastAPI, None, None]: + """Creates a session-scoped FastAPI app object.""" + app = create_app() + yield app + + +@pytest.fixture +def client(): + yield TestClient(app) + + +@pytest.fixture +def engine(worker_id: str) -> Generator[Any, None, None]: + """ + Yields a SQLAlchemy engine which is disposed of after the test session + """ + test_db_name = f"{TEST_DB_PREFIX}{worker_id.replace('gw', '')}" + test_db_url = f"{DATABASE_URL}/{test_db_name}" + + drop_test_database_if_exists(test_db_name) + create_test_database(test_db_name) + engine = create_engine(test_db_url, echo=True) + + with engine.begin(): + alembic_cfg = Config("src/backend/alembic.ini") + alembic_cfg.set_main_option("sqlalchemy.url", test_db_url) + upgrade(alembic_cfg, "head") + + yield engine + + engine.dispose() + drop_test_database_if_exists(test_db_name) + + +@pytest.fixture(scope="session") +def engine_chat(worker_id: str) -> Generator[Any, None, None]: + """ + Yields a SQLAlchemy engine which is disposed of after the test session + """ + test_db_name = f"{TEST_DB_PREFIX}{worker_id}" + if worker_id == "master": + test_db_name = f"{TEST_DB_PREFIX}{worker_id}_chat" + + test_db_url = f"{DATABASE_URL}/{test_db_name}" + + drop_test_database_if_exists(test_db_name) + create_test_database(test_db_name) + engine = create_engine(test_db_url, echo=True) + + with engine.begin(): + alembic_cfg = Config("src/backend/alembic.ini") + alembic_cfg.set_main_option("sqlalchemy.url", test_db_url) + upgrade(alembic_cfg, "head") + + yield engine + + engine.dispose() + drop_test_database_if_exists(test_db_name) + + +@pytest.fixture(scope="function") +def session(engine: Any) -> Generator[Session, None, None]: + """ + Yields a SQLAlchemy session within a transaction + that is rolled back after every function + """ + connection = engine.connect() + # Begin the transaction + transaction = connection.begin() + # Use connection within the started transaction + session = Session(bind=connection, query_cls=CustomFilterQuery) + + yield session + + session.close() + # Roll back the transaction + transaction.rollback() + # Close connection so it returns to the connection pool + connection.close() + + +@pytest.fixture(scope="function") +def session_client(session: Session, fastapi_app: FastAPI) -> Generator[TestClient, None, None]: + """ + Fixture to inject the session into the API client + """ + + def override_get_session() -> Generator[Session, Any, None]: + yield session + + fastapi_app.dependency_overrides[get_session] = override_get_session + + print("Session at fixture " + str(session)) + + with TestClient(fastapi_app) as client: + yield client + + fastapi_app.dependency_overrides = {} + + +@pytest.fixture(scope="session") +def session_chat(engine_chat: Any) -> Generator[Session, None, None]: + """ + Yields a SQLAlchemy session within a transaction + that is rolled back after every session + + We need to use the fixture in the session scope because the chat + endpoint is asynchronous and needs to be open for the entire session + """ + connection = engine_chat.connect() + transaction = connection.begin() + # Use connection within the started transaction + session = Session(bind=connection, query_cls=CustomFilterQuery) + + yield session + + session.close() + # Roll back the transaction + transaction.rollback() + # Close connection so it returns to the connection pool + connection.close() + + +@pytest.fixture(scope="session") +def session_client_chat(session_chat: Session, fastapi_app: FastAPI) -> Generator[TestClient, None, None]: + """ + Fixture to inject the session into the API client + + We need to use the fixture in the session scope because the chat + router uses a WebSocket connection that needs to be open for the + entire session + """ + + def override_get_session() -> Generator[Session, Any, None]: + yield session_chat + + fastapi_app.dependency_overrides[get_session] = override_get_session + + print("Session at fixture " + str(session_chat)) + + with TestClient(fastapi_app) as client: + yield client + + fastapi_app.dependency_overrides = {} + + +@pytest.fixture(autouse=True) +def mock_redis_client(): + """ + A pytest fixture that globally replaces `Redis.from_url` with `fakeredis`. + """ + fake_redis = fakeredis.FakeStrictRedis(decode_responses=True) + + # Patch Redis.from_url to always return the fake Redis instance + with patch.object(Redis, 'from_url', return_value=fake_redis): + yield fake_redis + + +@pytest.fixture +def user(session: Session) -> User: + return get_factory("User", session).create(id="1") + + +@pytest.fixture +def organization(session: Session) -> Organization: + return get_factory("Organization", session).create() + +@pytest.fixture +def deployment(session: Session) -> Deployment: + return get_factory("Deployment", session).create( + deployment_class_name="CohereDeployment" + ) + +@pytest.fixture +def inject_events() -> list[dict]: + return [] + + +@pytest.fixture +def mock_event_stream(inject_events: list[dict]) -> list[dict]: + events = [ + { + "event_type": StreamEvent.STREAM_START, + "generation_id": "ca0f398e-f8c8-48f0-b093-12d1754d00ed", + }, + ] + if inject_events: + events.extend(inject_events) + + events.extend([ + { + "event_type": StreamEvent.TEXT_GENERATION, + "text": "This is a test.", + }, + { + "event_type": StreamEvent.STREAM_END, + "response": { + "generation_id": "ca0f398e-f8c8-48f0-b093-12d1754d00ed", + "citations": [], + "documents": [], + "search_results": [], + "search_queries": [], + }, + "finish_reason": "COMPLETE", + } + ]) + return events + + +@pytest.fixture +def mock_available_model_deployments(mock_event_stream: list[dict]): + from backend.tests.unit.model_deployments.mock_deployments.mock_base import ( + MockDeployment, + ) + + MockDeployment.event_stream = mock_event_stream + MOCKED_DEPLOYMENTS = { d.name(): d for d in MockDeployment.__subclasses__() } + + with patch("backend.services.deployment.AVAILABLE_MODEL_DEPLOYMENTS", MOCKED_DEPLOYMENTS) as mock: + yield mock diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_agent.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_agent.py new file mode 100644 index 0000000000000000000000000000000000000000..b403f343944fac170c21aa00772d9cc6b444cbbb --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_agent.py @@ -0,0 +1,295 @@ +import pytest +from sqlalchemy.exc import IntegrityError +from sqlalchemy.sql.expression import false + +from backend.config.tools import Tool +from backend.crud import agent as agent_crud +from backend.database_models.agent import Agent +from backend.schemas.agent import AgentVisibility, UpdateAgentDB +from backend.tests.unit.factories import get_factory + + +def test_create_agent(session, user): + deployment = get_factory("Deployment", session).create() + model = get_factory("Model", session).create(deployment_id=deployment.id) + agent_data = Agent( + user_id=user.id, + version=1, + name="test", + description="test", + preamble="test", + temperature=0.5, + model_id=model.id, + deployment_id=deployment.id, + tools=[Tool.Wiki_Retriever_LangChain.value.ID, Tool.Search_File.value.ID], + is_private=True, + ) + + agent = agent_crud.create_agent(session, agent_data) + assert agent.user_id == user.id + assert agent.version == 1 + assert agent.name == "test" + assert agent.description == "test" + assert agent.preamble == "test" + assert agent.temperature == 0.5 + assert agent.tools == [Tool.Wiki_Retriever_LangChain.value.ID, Tool.Search_File.value.ID] + assert agent.is_private + assert agent.deployment == deployment.name + assert agent.model == model.name + + agent = agent_crud.get_agent_by_id(session, agent.id, user.id) + assert agent.user_id == user.id + assert agent.version == 1 + assert agent.name == "test" + assert agent.description == "test" + assert agent.preamble == "test" + assert agent.temperature == 0.5 + assert agent.tools == [Tool.Wiki_Retriever_LangChain.value.ID, Tool.Search_File.value.ID] + assert agent.is_private + assert agent.deployment == deployment.name + assert agent.model == model.name + + +def test_create_agent_empty_non_required_fields(session, user): + agent_data = Agent( + user_id=user.id, + name="test", + ) + + agent = agent_crud.create_agent(session, agent_data) + assert agent.user_id == user.id + assert agent.version == 1 + assert agent.name == "test" + assert agent.description == "" + assert agent.preamble == "" + assert agent.temperature == 0.3 + assert agent.tools == [] + assert agent.is_private == false() + agent = agent_crud.get_agent_by_id(session, agent.id, user.id) + assert agent.user_id == user.id + assert agent.version == 1 + assert agent.name == "test" + assert agent.description == "" + assert agent.preamble == "" + assert agent.temperature == 0.3 + assert agent.tools == [] + + +def test_create_agent_missing_name(session, user): + agent_data = Agent( + user_id=user.id, + ) + + with pytest.raises(IntegrityError): + _ = agent_crud.create_agent(session, agent_data) + + +def test_create_agent_duplicate_name_version(session, user): + _ = get_factory("Agent", session).create( + id="1", user=user, name="test_agent", version=1 + ) + + agent_data = Agent( + user_id=user.id, + version=1, + name="test_agent", + description="test", + preamble="test", + temperature=0.5, + tools=[Tool.Wiki_Retriever_LangChain.value.ID, Tool.Search_File.value.ID], + ) + + with pytest.raises(IntegrityError): + _ = agent_crud.create_agent(session, agent_data) + + +def test_get_agent_by_id(session, user): + _ = get_factory("Agent", session).create(id="1", name="test_agent", user=user) + agent = agent_crud.get_agent_by_id(session, "1", user.id) + assert agent.id == "1" + assert agent.name == "test_agent" + + +def test_get_agent_by_name(session, user): + _ = get_factory("Agent", session).create(id="1", name="test_agent", user=user) + agent = agent_crud.get_agent_by_name(session, "test_agent", user.id) + assert agent.id == "1" + assert agent.name == "test_agent" + + +def test_fail_get_nonexistant_agent(session, user): + agent = agent_crud.get_agent_by_id(session, "123", user.id) + assert agent is None + + +def test_get_private_agent_by_another_user(session, user): + user2 = get_factory("User", session).create() + agent = get_factory("Agent", session).create( + id="1", name="test_agent", user_id=user.id, is_private=True + ) + agent = agent_crud.get_agent_by_id(session, agent.id, user2.id) + assert agent is None + + +def test_list_public_agents(session, user): + # Delete default agent + session.query(Agent).delete() + length = 3 + for i in range(length): + _ = get_factory("Agent", session).create( + id=i, name=f"test_agent_{i}", user=user, is_private=False + ) + + public_agents = agent_crud.get_agents( + session, user_id=user.id, visibility=AgentVisibility.PUBLIC + ) + assert len(public_agents) == length + + private_agents = agent_crud.get_agents( + session, user_id=user.id, visibility=AgentVisibility.PRIVATE + ) + assert len(private_agents) == 0 + + +def test_list_public_agents_empty(session, user): + # Delete default agent + session.query(Agent).delete() + agents = agent_crud.get_agents( + session, user_id=user.id, visibility=AgentVisibility.PUBLIC + ) + assert len(agents) == 0 + + +def test_list_private_agents(session, user): + length = 3 + for i in range(length): + _ = get_factory("Agent", session).create( + id=i, name=f"test_agent_{i}", user=user, is_private=True + ) + + user2 = get_factory("User", session).create() + for i in range(length, length * 2): + _ = get_factory("Agent", session).create( + id=i, name=f"test_agent_{i}", user=user2, is_private=True + ) + + agents = agent_crud.get_agents(session, user.id, visibility=AgentVisibility.PRIVATE) + assert len(agents) == length + + agents = agent_crud.get_agents(session, user.id, visibility=AgentVisibility.PUBLIC) + assert len(agents) == 0 + + +def test_list_public_and_private_agents(session, user): + length = 3 + for i in range(length): + _ = get_factory("Agent", session).create( + id=i, name=f"test_agent_{i}", user=user, is_private=True + ) + + for i in range(length, length * 2): + _ = get_factory("Agent", session).create( + id=i, name=f"test_agent_{i}", user=user, is_private=False + ) + + agents = agent_crud.get_agents(session, user.id, visibility=AgentVisibility.ALL) + assert len(agents) == length * 2 + + +def test_list_agents_with_pagination(session, user): + for i in range(10): + get_factory("Agent", session).create(name=f"Agent {i}", user=user) + + agents = agent_crud.get_agents( + session, user.id, offset=5, limit=5, visibility=AgentVisibility.ALL + ) + assert len(agents) == 5 + + +def test_update_agent(session, user): + deployment = get_factory("Deployment", session).create() + model = get_factory("Model", session).create(deployment_id=deployment.id) + agent = get_factory("Agent", session).create( + name="test_agent", + description="This is a test agent", + version=1, + preamble="test", + temperature=0.5, + user=user, + deployment_id=deployment.id, + model_id=model.id, + tools=[Tool.Wiki_Retriever_LangChain.value.ID, Tool.Search_File.value.ID], + ) + + new_deployment = get_factory("Deployment", session).create() + new_model = get_factory("Model", session).create(deployment_id=new_deployment.id) + new_agent_data = UpdateAgentDB( + name="new_test_agent", + description="This is a new test agent", + version=2, + preamble="new_test", + temperature=0.6, + tools=[Tool.Python_Interpreter.value.ID, Tool.Calculator.value.ID], + model_id=new_model.id, + deployment_id=new_deployment.id, + ) + + agent = agent_crud.update_agent(session, agent, new_agent_data, user.id) + assert agent.name == new_agent_data.name + assert agent.description == new_agent_data.description + assert agent.version == new_agent_data.version + assert agent.preamble == new_agent_data.preamble + assert agent.temperature == new_agent_data.temperature + assert agent.tools == [Tool.Python_Interpreter.value.ID, Tool.Calculator.value.ID] + assert agent.model_id == new_model.id + assert agent.deployment_id == new_deployment.id + assert agent.model == new_model.name + assert agent.deployment == new_deployment.name + + +def test_delete_agent(session, user): + agent = get_factory("Agent", session).create(user=user) + + agent_crud.delete_agent(session, agent.id, user.id) + + agent = agent_crud.get_agent_by_id(session, agent.id, user.id) + assert agent is None + + +def test_fail_delete_nonexistent_agent(session, user): + status = agent_crud.delete_agent(session, "123", user.id) + assert status is False + + +def test_delete_agent_by_another_user(session, user): + user2 = get_factory("User", session).create() + agent = get_factory("Agent", session).create(user=user, is_private=True) + + status = agent_crud.delete_agent(session, agent.id, user2.id) + assert status is False + + +def test_get_agents_by_user_id(session, user): + deployment = get_factory("Deployment", session).create() + model = get_factory("Model", session).create(deployment_id=deployment.id) + agent = get_factory("Agent", session).create(user=user, deployment_id=deployment.id, model_id=model.id) + + agents = agent_crud.get_agents(session, user_id=user.id) + assert len(agents) == 1 + assert agents[0].user_id == user.id + assert agents[0].id == agent.id + + +def test_get_agents_by_organization_id(session): + organization = get_factory("Organization", session).create() + user = get_factory("User", session).create() + user.organizations.append(organization) + deployment = get_factory("Deployment", session).create() + model = get_factory("Model", session).create(deployment_id=deployment.id) + agent = get_factory("Agent", session).create(user=user, organization=organization, deployment_id=deployment.id, + model_id=model.id) + + agents = agent_crud.get_agents(session, user.id, organization_id=organization.id) + assert len(agents) == 1 + assert agents[0].user_id == user.id + assert agents[0].id == agent.id diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_agent_tool_metadata.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_agent_tool_metadata.py new file mode 100644 index 0000000000000000000000000000000000000000..16f9e41e7327e9ffb19348707674cf13903bafe1 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_agent_tool_metadata.py @@ -0,0 +1,189 @@ +import pytest +from sqlalchemy.exc import IntegrityError + +from backend.config.tools import Tool +from backend.crud import agent_tool_metadata as agent_tool_metadata_crud +from backend.database_models.agent_tool_metadata import AgentToolMetadata +from backend.schemas.agent import UpdateAgentToolMetadataRequest +from backend.tests.unit.factories import get_factory + +mock_artifact_1 = { + "id": "1T", + "url": "hellotesturl.com/document1", + "name": "document-test1", + "type": "document", +} +mock_artifact_2 = { + "id": "2T", + "url": "hellotesturl.com/document2", + "name": "document-test2", + "type": "document", +} + + +def test_create_agent_tool_metadata(session, user): + agent = get_factory("Agent", session).create( + id="1", name="test_agent", tools=[Tool.Google_Drive.value.ID], user=user + ) + + agent_tool_metadata_data = AgentToolMetadata( + user_id=user.id, + agent_id=agent.id, + tool_name=Tool.Google_Drive.value.ID, + artifacts=[mock_artifact_1], + ) + agent_tool_metadata = agent_tool_metadata_crud.create_agent_tool_metadata( + session, agent_tool_metadata_data + ) + assert agent_tool_metadata.user_id == user.id + assert agent_tool_metadata.agent_id == agent.id + assert agent_tool_metadata.tool_name == Tool.Google_Drive.value.ID + assert agent_tool_metadata.artifacts == [mock_artifact_1] + assert agent_tool_metadata.artifacts[0]["type"] == "document" + + agent_tool_metadata = agent_tool_metadata_crud.get_agent_tool_metadata_by_id( + session, agent_tool_metadata.id + ) + assert agent_tool_metadata.user_id == user.id + assert agent_tool_metadata.agent_id == agent.id + assert agent_tool_metadata.tool_name == Tool.Google_Drive.value.ID + assert agent_tool_metadata.artifacts == [mock_artifact_1] + assert agent_tool_metadata.artifacts[0]["type"] == "document" + + +def test_create_agent_missing_agent_id(session, user): + agent_tool_metadata_data = AgentToolMetadata( + user_id=user.id, + tool_name=Tool.Google_Drive.value.ID, + artifacts=[mock_artifact_1], + ) + with pytest.raises(IntegrityError): + _ = agent_tool_metadata_crud.create_agent_tool_metadata( + session, agent_tool_metadata_data + ) + + +def test_create_agent_missing_tool_name(session, user): + agent = get_factory("Agent", session).create( + id="1", name="test_agent", tools=[Tool.Google_Drive.value.ID], user=user + ) + + agent_tool_metadata_data = AgentToolMetadata( + user_id=user.id, + agent_id=agent.id, + artifacts=[mock_artifact_1], + ) + with pytest.raises(IntegrityError): + _ = agent_tool_metadata_crud.create_agent_tool_metadata( + session, agent_tool_metadata_data + ) + + +def test_create_agent_missing_user_id(session, user): + agent = get_factory("Agent", session).create( + id="1", name="test_agent", tools=[Tool.Google_Drive.value.ID], user=user + ) + + agent_tool_metadata_data = AgentToolMetadata( + agent_id=agent.id, + tool_name=Tool.Google_Drive.value.ID, + artifacts=[mock_artifact_1], + user_id="123", + ) + with pytest.raises(IntegrityError): + _ = agent_tool_metadata_crud.create_agent_tool_metadata( + session, agent_tool_metadata_data + ) + + +def test_update_agent_tool_metadata(session, user): + agent = get_factory("Agent", session).create(user=user) + original_agent_tool_metadata = get_factory("AgentToolMetadata", session).create( + user_id=user.id, + agent_id=agent.id, + tool_name=Tool.Google_Drive.value.ID, + artifacts=[mock_artifact_1], + ) + + new_agent_tool_metadata_data = UpdateAgentToolMetadataRequest( + artifacts=[mock_artifact_1], + ) + + agent_tool_metadata = agent_tool_metadata_crud.update_agent_tool_metadata( + session, original_agent_tool_metadata, new_agent_tool_metadata_data + ) + assert agent_tool_metadata.user_id == original_agent_tool_metadata.user_id + assert agent_tool_metadata.agent_id == original_agent_tool_metadata.agent_id + assert agent_tool_metadata.tool_name == original_agent_tool_metadata.tool_name + assert agent_tool_metadata.artifacts == new_agent_tool_metadata_data.artifacts + + +def test_get_agent_tool_metadata_by_id(session, user): + agent = get_factory("Agent", session).create(user=user) + agent_tool_metadata = get_factory("AgentToolMetadata", session).create( + user_id=user.id, + agent_id=agent.id, + tool_name=Tool.Google_Drive.value.ID, + artifacts=[mock_artifact_1, mock_artifact_2], + ) + agent_tool_metadata = agent_tool_metadata_crud.get_agent_tool_metadata_by_id( + session, agent_tool_metadata.id + ) + assert agent_tool_metadata.user_id == user.id + assert agent_tool_metadata.agent_id == agent.id + assert agent_tool_metadata.tool_name == Tool.Google_Drive.value.ID + assert agent_tool_metadata.artifacts == [mock_artifact_1, mock_artifact_2] + + +def test_get_all_agent_tool_metadata_by_agent_id(session, user): + agent1 = get_factory("Agent", session).create(user=user) + agent2 = get_factory("Agent", session).create(user=user) + + # Add a random entry to test + _ = get_factory("AgentToolMetadata", session).create( + user_id=user.id, + agent_id=agent1.id, + tool_name=Tool.Google_Drive.value.ID, + artifacts=[mock_artifact_1, mock_artifact_2], + ) + + + # Constraint was added preventing multiple entries for the same user + agent + tool so fixing to change the tool used + i = 0 + for tool in Tool: + i += 1 + + _ = get_factory("Agent", session).create(user_id=user.id) + _ = get_factory("AgentToolMetadata", session).create( + id=f"{i}", + tool_name=tool.value.ID, + artifacts=[mock_artifact_1, mock_artifact_2], + user_id=user.id, + agent_id=agent2.id, + ) + + all_agent_tool_metadata = ( + agent_tool_metadata_crud.get_all_agent_tool_metadata_by_agent_id( + session, agent_id=agent2.id + ) + ) + assert len(all_agent_tool_metadata) == len(Tool) + + +def test_delete_agent_tool_metadata_by_id(session, user): + agent = get_factory("Agent", session).create(user=user) + agent_tool_metadata = get_factory("AgentToolMetadata", session).create( + user_id=user.id, + agent_id=agent.id, + tool_name=Tool.Google_Drive.value.ID, + artifacts=[mock_artifact_1, mock_artifact_2], + ) + + agent_tool_metadata_crud.delete_agent_tool_metadata_by_id( + session, agent_tool_metadata.id + ) + + agent_tool_metadata = agent_tool_metadata_crud.get_agent_tool_metadata_by_id( + session, agent_tool_metadata.id + ) + assert agent_tool_metadata is None diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_blacklist.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_blacklist.py new file mode 100644 index 0000000000000000000000000000000000000000..ff49d3aae1b5f8cc5bfdfbd98b56a2291c6f7f88 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_blacklist.py @@ -0,0 +1,31 @@ +import uuid + +from backend.crud import blacklist as blacklist_crud +from backend.database_models.blacklist import Blacklist +from backend.tests.unit.factories import get_factory + + +def test_create_blacklist(session): + token_id = str(uuid.uuid4()) + blacklist_data = Blacklist(token_id=token_id) + blacklist = blacklist_crud.create_blacklist(session, blacklist_data) + + blacklist = session.query(Blacklist).filter(Blacklist.token_id == token_id).first() + + assert blacklist is not None + assert blacklist.token_id == token_id + + +def test_get_blacklist(session): + token_id = str(uuid.uuid4()) + + _ = get_factory("Blacklist", session).create(token_id=token_id) + + blacklist = blacklist_crud.get_blacklist(session, token_id) + assert blacklist is not None + assert blacklist.token_id == token_id + + +def test_fail_get_nonexistent_blacklist(session): + blacklist = blacklist_crud.get_blacklist(session, "123") + assert blacklist is None diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_citation.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_citation.py new file mode 100644 index 0000000000000000000000000000000000000000..b75b9300ab811d150fbb97e2e5cac2c8c45659b2 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_citation.py @@ -0,0 +1,137 @@ +import pytest + +from backend.crud import citation as citation_crud +from backend.database_models.citation import Citation +from backend.tests.unit.factories import get_factory + + +@pytest.fixture(autouse=True) +def conversation(session, user): + return get_factory("Conversation", session).create(id="1", user_id=user.id) + + +@pytest.fixture(autouse=True) +def message(session, conversation, user): + return get_factory("Message", session).create( + id="1", conversation_id=conversation.id, user_id=user.id + ) + + +@pytest.fixture(autouse=True) +def document(session, conversation, message, user): + return get_factory("Document", session).create( + id="1", + conversation_id=conversation.id, + message_id=message.id, + user_id=user.id, + document_id="hello", + ) + + +def test_create_citation(session, document, user): + citation_data = Citation( + text="Hello, World!", + user_id=user.id, + start=1, + end=2, + message_id="1", + ) + + citation = citation_crud.create_citation(session, citation_data) + assert citation.text == citation_data.text + assert citation.user_id == citation_data.user_id + assert citation.start == citation_data.start + assert citation.end == citation_data.end + assert citation.message_id == citation_data.message_id + + get_factory("CitationDocuments", session).create( + left_id=document.id, right_id=citation.id + ) + + citation = citation_crud.get_citation(session, citation.id) + assert citation.text == citation_data.text + assert citation.user_id == citation_data.user_id + assert citation.start == citation_data.start + assert citation.end == citation_data.end + assert citation.message_id == citation_data.message_id + assert citation.document_ids == ["hello"] + + +def test_get_citation(session, user): + _ = get_factory("Citation", session).create( + id="1", + text="Hello, World!", + user_id=user.id, + message_id="1", + ) + + citation = citation_crud.get_citation(session, "1") + assert citation.text == "Hello, World!" + assert citation.id == "1" + + +def test_fail_get_nonexistent_citation(session): + citation = citation_crud.get_citation(session, "123") + assert citation is None + + +def test_list_citations(session, user): + _ = get_factory("Citation", session).create( + text="Hello, World!", user_id=user.id, message_id="1" + ) + + citations = citation_crud.get_citations(session) + assert len(citations) == 1 + assert citations[0].text == "Hello, World!" + + +def test_list_citations_empty(session): + citations = citation_crud.get_citations(session) + assert len(citations) == 0 + + +def test_list_citations_with_pagination(session, user): + for i in range(10): + get_factory("Citation", session).create( + text=f"Citation {i}", user_id=user.id, message_id="1" + ) + + citations = citation_crud.get_citations(session, offset=5, limit=5) + assert len(citations) == 5 + + for i, citation in enumerate(citations): + assert citation.text == f"Citation {i + 5}" + + +def test_list_citations_by_message_id(session, user): + for i in range(10): + get_factory("Citation", session).create( + text=f"Citation {i}", user_id=user.id, message_id="1" + ) + + citations = citation_crud.get_citations_by_message_id(session, "1") + assert len(citations) == 10 + + for i, citation in enumerate(citations): + assert citation.text == f"Citation {i}" + + +def test_list_citations_by_message_id_empty(session): + citations = citation_crud.get_citations_by_message_id(session, "1") + assert len(citations) == 0 + + +def test_delete_citation(session, user): + citation = get_factory("Citation", session).create( + id="1", text="Hello, World!", user_id=user.id, message_id="1" + ) + + citation_crud.delete_citation(session, "1") + + citation = citation_crud.get_citation(session, "1") + assert citation is None + + +def test_delete_citation_nonexistent(session): + citation = citation_crud.delete_citation(session, "1") + assert citation is None diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_conversation.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_conversation.py new file mode 100644 index 0000000000000000000000000000000000000000..54251ccb50a12c1644db36c7e80a3569dbd99ae8 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_conversation.py @@ -0,0 +1,198 @@ +from backend.crud import citation as citation_crud +from backend.crud import conversation as conversation_crud +from backend.crud import document as document_crud +from backend.crud import message as message_crud +from backend.database_models.conversation import ( + Conversation, + ConversationFileAssociation, +) +from backend.schemas.conversation import UpdateConversationRequest +from backend.tests.unit.factories import get_factory + + +def test_create_conversation(session, user): + conversation_data = Conversation( + user_id=user.id, + title="Hello, World!", + description="This is a test", + ) + + conversation = conversation_crud.create_conversation(session, conversation_data) + assert conversation.user_id == conversation_data.user_id + assert conversation.title == conversation_data.title + assert conversation.description == conversation_data.description + + conversation = conversation_crud.get_conversation(session, conversation.id, user.id) + assert conversation.user_id == conversation_data.user_id + assert conversation.title == conversation_data.title + assert conversation.description == conversation_data.description + + +def test_get_conversation(session, user): + _ = get_factory("Conversation", session).create( + id="1", title="Hello, World!", user_id=user.id + ) + + conversation = conversation_crud.get_conversation(session, "1", user.id) + assert conversation.title == "Hello, World!" + assert conversation.id == "1" + + +def test_fail_get_nonexistent_conversation(session, user): + conversation = conversation_crud.get_conversation(session, "123", user_id=user.id) + assert conversation is None + + +def test_list_conversations(session, user): + _ = get_factory("Conversation", session).create( + title="Hello, World!", user_id=user.id + ) + + conversations = conversation_crud.get_conversations(session, user.id) + assert len(conversations) == 1 + assert conversations[0].title == "Hello, World!" + + +def test_list_conversations_empty(session, user): + conversations = conversation_crud.get_conversations(session, user.id) + assert len(conversations) == 0 + + +def test_list_conversations_with_pagination(session, user): + for i in range(10): + get_factory("Conversation", session).create( + title=f"Conversation {i}", user_id=user.id, updated_at=f"2021-01-{31-i}" + ) + + conversations = conversation_crud.get_conversations( + session, user_id=user.id, offset=5, limit=5 + ) + assert len(conversations) == 5 + + for i, conversation in enumerate(conversations): + assert conversation.title == f"Conversation {i + 5}" + + +def test_list_converstions_with_agent_id_filter_and_pagination(session, user): + agent = get_factory("Agent", session).create( + id="agent_id", name="test agent", user=user + ) + for i in range(10): + get_factory("Conversation", session).create( + title=f"Conversation {i} with agent", + user_id=user.id, + agent_id=agent.id, + updated_at=f"2021-01-{31-i}", + ) + + for i in range(10): + get_factory("Conversation", session).create( + title=f"Conversation {i}", user_id=user.id + ) + + conversations = conversation_crud.get_conversations( + session, user_id=user.id, agent_id="agent_id", offset=5, limit=5 + ) + assert len(conversations) == 5 + + for i, conversation in enumerate(conversations): + assert conversation.title == f"Conversation {i + 5} with agent" + + +def test_update_conversation(session, user): + conversation = get_factory("Conversation", session).create( + title="Hello, World!", + description="This is a test", + user_id=user.id, + ) + + new_conversation_data = UpdateConversationRequest( + title="Hello, Universe!", + description="This is a new test", + user_id="1", + ) + + conversation = conversation_crud.update_conversation( + session, conversation, new_conversation_data + ) + assert conversation.title == new_conversation_data.title + assert conversation.description == new_conversation_data.description + + +def test_delete_conversation(session, user): + conversation = get_factory("Conversation", session).create(user_id=user.id) + + conversation_crud.delete_conversation(session, conversation.id, user.id) + + conversation = conversation_crud.get_conversation(session, conversation.id, user.id) + assert conversation is None + + +def test_fail_delete_nonexistent_conversation(session, user): + conversation = conversation_crud.delete_conversation(session, "123", user.id) + assert conversation is None + + +def test_delete_conversation_cascade(session, user): + conversation = get_factory("Conversation", session).create(user_id=user.id) + message = get_factory("Message", session).create( + conversation_id=conversation.id, user_id=user.id + ) + message_id = message.id + citation = get_factory("Citation", session).create( + message_id=message.id, user_id=user.id + ) + citation_id = citation.id + document = get_factory("Document", session).create( + conversation_id=conversation.id, message_id=message.id, user_id=user.id + ) + document_id = document.id + + conversation_crud.delete_conversation(session, conversation.id, user.id) + + conversation = conversation_crud.get_conversation(session, conversation.id, user.id) + assert conversation is None + + message = message_crud.get_message(session, message_id, user.id) + assert message is None + + citation = citation_crud.get_citation(session, citation_id) + assert citation is None + + document = document_crud.get_document(session, document_id) + assert document is None + + +def test_create_conversation_file_association(session, user): + conversation = get_factory("Conversation", session).create(user_id=user.id) + file = get_factory("File", session).create(user_id=user.id) + + conversation_file_association = ( + conversation_crud.create_conversation_file_association( + session, + ConversationFileAssociation( + conversation_id=conversation.id, file_id=file.id, user_id=user.id + ), + ) + ) + assert conversation_file_association.conversation_id == conversation.id + assert conversation_file_association.file_id == file.id + assert conversation_file_association.user_id == user.id + + conversation = conversation_crud.get_conversation(session, conversation.id, user.id) + assert conversation.file_ids == [file.id] + + +def test_delete_conversation_file_association(session, user): + conversation = get_factory("Conversation", session).create(user_id=user.id) + file = get_factory("File", session).create(user_id=user.id) + _ = get_factory("ConversationFileAssociation", session).create( + conversation_id=conversation.id, file_id=file.id, user_id=user.id + ) + + conversation_crud.delete_conversation_file_association( + session, conversation.id, file.id, user.id + ) + + conversation = conversation_crud.get_conversation(session, conversation.id, user.id) + assert conversation.file_ids == [] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_deployment.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_deployment.py new file mode 100644 index 0000000000000000000000000000000000000000..b01109f19fea4d0fc8941fe9a0bb5fda06176f11 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_deployment.py @@ -0,0 +1,159 @@ +import pytest +from sqlalchemy.orm import Session + +from backend.crud import deployment as deployment_crud +from backend.database_models.deployment import Deployment +from backend.schemas.deployment import DeploymentCreate, DeploymentUpdate +from backend.schemas.user import User +from backend.tests.unit.factories import get_factory + + +def test_create_deployment(session: Session, deployment: Deployment) -> None: + deployment_data = DeploymentCreate( + name="Test Deployment", + deployment_class_name="CohereDeployment", + description="Test Description", + is_community=False, + default_deployment_config={}, + ) + + deployment = deployment_crud.create_deployment(session, deployment_data) + assert deployment.name == deployment_data.name + assert deployment.deployment_class_name == deployment_data.deployment_class_name + assert deployment.description == deployment_data.description + assert deployment.is_community == deployment_data.is_community + assert ( + deployment.default_deployment_config + == deployment_data.default_deployment_config + ) + + deployment = deployment_crud.get_deployment(session, deployment.id) + assert deployment.name == deployment_data.name + + +def test_create_deployment_invalid_class_name(session: Session) -> None: + with pytest.raises(ValueError) as e: + deployment_data = DeploymentCreate( + name="Test Deployment", + deployment_class_name="SomeClassName", + description="Test Description", + is_community=False, + default_deployment_config={}, + ) + deployment_crud.create_deployment(session, deployment_data) + + assert "Deployment class not found" in str(e.value) + + +def test_get_deployment(session: Session) -> None: + deployment = get_factory("Deployment", session).create(name="Test Deployment") + retrieved_deployment = deployment_crud.get_deployment(session, deployment.id) + assert retrieved_deployment.id == deployment.id + assert retrieved_deployment.name == deployment.name + + +def test_fail_get_nonexistent_deployment(session: Session) -> None: + deployment = deployment_crud.get_deployment(session, "123") + assert deployment is None + + +def test_list_deployments(session: Session) -> None: + # Delete default deployments + session.query(Deployment).delete() + _ = get_factory("Deployment", session).create(name="Test Deployment") + + deployments = deployment_crud.get_deployments(session) + assert len(deployments) == 1 + assert deployments[0].name == "Test Deployment" + + +def test_list_deployments_empty(session: Session) -> None: + # Delete default deployments + session.query(Deployment).delete() + deployments = deployment_crud.get_deployments(session) + assert len(deployments) == 0 + + +def test_list_deployments_with_pagination(session: Session) -> None: + # Delete default deployments + session.query(Deployment).delete() + for i in range(10): + _ = get_factory("Deployment", session).create(name=f"Test Deployment {i}") + + deployments = deployment_crud.get_deployments(session, offset=5, limit=5) + assert len(deployments) == 5 + + +def test_get_available_deployments(session: Session, user: User) -> None: + session.query(Deployment).delete() + deployment = get_factory("Deployment", session).create() + _ = get_factory("Deployment", session).create( + default_deployment_config={} + ) + + deployments = deployment_crud.get_available_deployments(session) + + assert len(deployments) == 1 + assert deployments[0].id == deployment.id + + +def test_get_available_deployments_empty(session: Session, user: User) -> None: + session.query(Deployment).delete() + deployments = deployment_crud.get_available_deployments(session) + + assert len(deployments) == 0 + + +def test_update_deployment(session: Session, deployment: Deployment) -> None: + new_deployment_data = DeploymentUpdate( + name="NewName", + description="New Description", + deployment_class_name="AzureDeployment", + ) + + updated_deployment = deployment_crud.update_deployment( + session, deployment, new_deployment_data + ) + + assert updated_deployment.name == new_deployment_data.name + assert updated_deployment.description == new_deployment_data.description + assert ( + updated_deployment.deployment_class_name + == new_deployment_data.deployment_class_name + ) + assert updated_deployment.id == deployment.id + + +def test_update_deployment_partial(session: Session, deployment: Deployment) -> None: + new_deployment_data = DeploymentUpdate(name="Cohere") + + updated_deployment = deployment_crud.update_deployment( + session, deployment, new_deployment_data + ) + + assert updated_deployment.name == new_deployment_data.name + assert updated_deployment.id == deployment.id + + +def test_do_not_update_deployment(session: Session, deployment: Deployment) -> None: + new_deployment_data = DeploymentUpdate(name="Test Deployment") + + updated_deployment = deployment_crud.update_deployment( + session, deployment, new_deployment_data + ) + assert updated_deployment.name == deployment.name + + +def test_delete_deployment(session: Session) -> None: + deployment = get_factory("Deployment", session).create() + + deployment_crud.delete_deployment(session, deployment.id) + + deployment = deployment_crud.get_deployment(session, deployment.id) + assert deployment is None + + +def test_delete_nonexistent_deployment(session: Session) -> None: + deployment_crud.delete_deployment(session, "123") # no error + deployment = deployment_crud.get_deployment(session, "123") + assert deployment is None diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_document.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_document.py new file mode 100644 index 0000000000000000000000000000000000000000..290627597f0bd93ec8de02b68e98df61dd3293e8 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_document.py @@ -0,0 +1,110 @@ +import pytest + +from backend.crud import document as document_crud +from backend.database_models.document import Document +from backend.tests.unit.factories import get_factory + + +@pytest.fixture(autouse=True) +def conversation(session, user): + return get_factory("Conversation", session).create(id="1", user_id=user.id) + + +@pytest.fixture(autouse=True) +def message(session, conversation, user): + return get_factory("Message", session).create( + id="1", text="Hello, World!", conversation_id=conversation.id, user_id=user.id + ) + + +def test_create_document(session, user): + document_data = Document( + text="Hello, World!", + user_id=user.id, + conversation_id="1", + url="https://www.example.com", + document_id="1", + message_id="1", + ) + + document = document_crud.create_document(session, document_data) + assert document.text == document_data.text + assert document.user_id == document_data.user_id + assert document.conversation_id == document_data.conversation_id + assert document.url == document_data.url + assert document.document_id == document_data.document_id + assert document.message_id == document_data.message_id + + document = document_crud.get_document(session, document.id) + assert document.text == document_data.text + assert document.user_id == document_data.user_id + assert document.conversation_id == document_data.conversation_id + assert document.url == document_data.url + assert document.document_id == document_data.document_id + assert document.message_id == document_data.message_id + + +def test_get_document(session, conversation, message, user): + _ = get_factory("Document", session).create( + id="1", + text="Hello, World!", + conversation_id=conversation.id, + message_id=message.id, + user_id=user.id, + ) + + document = document_crud.get_document(session, "1") + assert document.text == "Hello, World!" + assert document.id == "1" + + +def test_fail_get_nonexistent_document(session): + document = document_crud.get_document(session, "123") + assert document is None + + +def test_list_documents(session, conversation, message, user): + _ = get_factory("Document", session).create( + text="Hello, World!", + conversation_id=conversation.id, + message_id=message.id, + user_id=user.id, + ) + + documents = document_crud.get_documents(session) + assert len(documents) == 1 + assert documents[0].text == "Hello, World!" + + +def test_list_documents_empty(session): + documents = document_crud.get_documents(session) + assert len(documents) == 0 + + +def test_list_documents_with_pagination(session, conversation, message, user): + for i in range(10): + _ = get_factory("Document", session).create( + text=f"Hello, World! {i}", + conversation_id=conversation.id, + message_id=message.id, + user_id=user.id, + ) + + documents = document_crud.get_documents(session, offset=5, limit=5) + assert len(documents) == 5 + for i, document in enumerate(documents): + assert document.text == f"Hello, World! {i + 5}" + + +def test_delete_document(session, conversation, message, user): + document = get_factory("Document", session).create( + text="Hello, World!", + conversation_id=conversation.id, + message_id=message.id, + user_id=user.id, + ) + + document_crud.delete_document(session, document.id) + + document = document_crud.get_document(session, document.id) + assert document is None diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_file.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_file.py new file mode 100644 index 0000000000000000000000000000000000000000..6bf91f604993c4419b2875b8674c3985580d0900 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_file.py @@ -0,0 +1,117 @@ +import pytest + +from backend.crud import file as file_crud +from backend.database_models.file import File +from backend.tests.unit.factories import get_factory + + +@pytest.fixture(autouse=True) +def conversation(session, user): + return get_factory("Conversation", session).create(id="1", user_id=user.id) + + +def test_create_file(session, user): + file_data = File( + file_name="test.txt", + file_size=100, + user_id=user.id, + ) + + file = file_crud.create_file(session, file_data) + assert file.file_name == file_data.file_name + assert file.file_size == file_data.file_size + assert file.user_id == file_data.user_id + + file = file_crud.get_file(session, file.id, user.id) + assert file.file_name == file_data.file_name + assert file.file_size == file_data.file_size + assert file.user_id == file_data.user_id + + +def test_batch_create_files(session, user): + file_data = File( + file_name="test.txt", + file_size=100, + user_id=user.id, + ) + file_data2 = File( + file_name="test2.txt", + file_size=100, + user_id=user.id, + ) + + files = file_crud.batch_create_files(session, [file_data, file_data2]) + assert len(files) == 2 + + files = file_crud.get_files(session, user.id) + assert len(files) == 2 + assert all(file.file_name in ["test.txt", "test2.txt"] for file in files) + assert files[0].user_id == user.id + assert files[1].user_id == user.id + + +def test_get_file(session, user): + _ = get_factory("File", session).create( + id="1", file_name="test.txt", user_id=user.id + ) + + file = file_crud.get_file(session, "1", user.id) + assert file.file_name == "test.txt" + assert file.id == "1" + + +def test_fail_get_nonexistent_file(session, user): + file = file_crud.get_file(session, "123", user.id) + assert file is None + + +def test_list_files(session, user): + _ = get_factory("File", session).create(file_name="test.txt", user_id=user.id) + + files = file_crud.get_files(session, user.id) + assert len(files) == 1 + assert files[0].file_name == "test.txt" + + +def test_list_files_empty(session, user): + files = file_crud.get_files(session, user.id) + assert len(files) == 0 + + +def test_list_files_with_pagination(session, user): + for i in range(10): + _ = get_factory("File", session).create( + file_name=f"test.txt {i}", user_id=user.id + ) + + files = file_crud.get_files(session, user.id, offset=5, limit=5) + assert len(files) == 5 + + for i, file in enumerate(files): + assert file.file_name == f"test.txt {i + 5}" + + +def test_list_files_by_user_id(session, user): + for i in range(10): + _ = get_factory("File", session).create( + file_name=f"test.txt {i}", user_id=user.id + ) + + files = file_crud.get_files_by_user_id(session, user.id) + assert len(files) == 10 + + for i, file in enumerate(files): + assert file.file_name == f"test.txt {i}" + assert file.user_id == user.id + + +def test_list_files_by_user_id_empty(session, user): + files = file_crud.get_files_by_user_id(session, user.id) + assert len(files) == 0 + + +def test_delete_file(session, user): + file = get_factory("File", session).create(file_name="test.txt", user_id=user.id) + + file_crud.delete_file(session, file.id, user.id) + assert file_crud.get_file(session, file.id, user.id) is None diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_group.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_group.py new file mode 100644 index 0000000000000000000000000000000000000000..4c8e482b6502ed345ac81bd353aa747340820adb --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_group.py @@ -0,0 +1,75 @@ +from backend.crud import group as group_crud +from backend.crud import user as user_crud +from backend.database_models import UserGroupAssociation +from backend.tests.unit.factories import get_factory + + +def test_insert_users_into_group(session): + _ = get_factory("User", session).create(id="1", fullname="John Doe") + user = user_crud.get_user(session, "1") + + _ = get_factory("Group", session).create(id="1", display_name="Group 1") + group = group_crud.get_group(session, "1") + + association = UserGroupAssociation( + user_id=user.id, group_id=group.id, display="John Doe" + ) + group = group_crud.set_users(session, group, [association]) + + assert group.display_name == "Group 1" + assert group.users == [user] + + +def test_delete_users_from_group(session): + test_insert_users_into_group(session) + + group = group_crud.get_group(session, "1") + group = group_crud.set_users(session, group, []) + + assert group.users == [] + + +def add_one_more_user_to_group(session): + test_insert_users_into_group(session) + + _ = get_factory("User", session).create(id="2", fullname="Jane Doe") + + group = group_crud.get_group(session, "1") + user_2 = user_crud.get_user(session, "2") + association = UserGroupAssociation( + user_id=user_2.id, group_id=group.id, display="John Doe" + ) + + group = group_crud.add_users(session, group, [association]) + + user_1 = user_crud.get_user(session, "1") + assert group.users == [user_1, user_2] + + +def test_replace_users(session): + test_insert_users_into_group(session) + + _ = get_factory("User", session).create(id="2", fullname="Jane Doe") + + group = group_crud.get_group(session, "1") + user_2 = user_crud.get_user(session, "2") + association = UserGroupAssociation( + user_id=user_2.id, group_id=group.id, display="John Doe" + ) + + group = group_crud.set_users(session, group, [association]) + + assert group.users == [user_2] + + +def test_cascade_group_delete(session): + test_insert_users_into_group(session) + + group_crud.delete_group(session, "1") + + assert ( + session.query(UserGroupAssociation) + .filter(UserGroupAssociation.group_id == "1") + .all() + == [] + ) diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_message.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_message.py new file mode 100644 index 0000000000000000000000000000000000000000..4af9090bc140a6fd01cb412ea2cf99067ea4d5b6 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_message.py @@ -0,0 +1,240 @@ +import pytest + +from backend.crud import citation as citation_crud +from backend.crud import document as document_crud +from backend.crud import message as message_crud +from backend.database_models.message import Message, MessageFileAssociation +from backend.schemas.message import UpdateMessage +from backend.tests.unit.factories import get_factory + + +@pytest.fixture(autouse=True) +def conversation(session, user): + return get_factory("Conversation", session).create(id="1", user_id=user.id) + + +def test_create_message(session, conversation, user): + message_data = Message( + text="Hello, World!", + user_id=user.id, + conversation_id=conversation.id, + position=1, + agent="USER", + ) + + message = message_crud.create_message(session, message_data) + assert message.text == message_data.text + assert message.user_id == message_data.user_id + assert message.conversation_id == message_data.conversation_id + + message = message_crud.get_message(session, message.id, user.id) + assert message.text == message_data.text + assert message.user_id == message_data.user_id + assert message.conversation_id == message_data.conversation_id + + +def test_get_message(session, conversation, user): + _ = get_factory("Message", session).create( + id="1", text="Hello, World!", conversation_id=conversation.id, user_id=user.id + ) + + message = message_crud.get_message(session, "1", user.id) + assert message.text == "Hello, World!" + assert message.id == "1" + + +def test_fail_get_nonexistent_message(session, user): + message = message_crud.get_message(session, "123", user.id) + assert message is None + + +def test_get_conversation_message(session, conversation, user): + _ = get_factory("Message", session).create( + id="1", text="Hello, World!", conversation_id=conversation.id, user_id=user.id + ) + + message = message_crud.get_conversation_message(session, conversation.id, "1", user.id) + assert message.conversation_id == conversation.id + assert message.id == "1" + assert message.text == "Hello, World!" + + +def test_fail_get_nonexistent_conversation_message(session, user): + message = message_crud.get_conversation_message(session, "123", "456", user.id) + assert message is None + + +def test_list_messages(session, conversation, user): + _ = get_factory("Message", session).create( + text="Hello, World!", conversation_id=conversation.id, user_id=user.id + ) + + messages = message_crud.get_messages(session, user.id) + assert len(messages) == 1 + assert messages[0].text == "Hello, World!" + + +def test_list_messages_empty(session, user): + messages = message_crud.get_messages(session, user.id) + assert len(messages) == 0 + + +def test_list_messages_with_pagination(session, conversation, user): + for i in range(10): + _ = get_factory("Message", session).create( + text=f"Hello, World! {i}", conversation_id=conversation.id, user_id=user.id + ) + + messages = message_crud.get_messages(session, offset=5, limit=5, user_id=user.id) + assert len(messages) == 5 + messages.sort(key=lambda x: x.text) + for i, message in enumerate(messages): + assert message.text == f"Hello, World! {i + 5}" + + +def test_list_messages_by_conversation_id(session, conversation, user): + for i in range(10): + _ = get_factory("Message", session).create( + text=f"Hello, World! {i}", conversation_id=conversation.id, user_id=user.id + ) + + messages = message_crud.get_messages_by_conversation_id(session, "1", user.id) + messages.sort(key=lambda x: x.text) + assert len(messages) == 10 + + for i, message in enumerate(messages): + assert message.text == f"Hello, World! {i}" + assert message.conversation_id == "1" + + +def test_list_messages_by_conversation_id_empty(session, conversation, user): + messages = message_crud.get_messages_by_conversation_id(session, "1", user.id) + assert len(messages) == 0 + + +def test_update_message(session, conversation, user): + message = get_factory("Message", session).create( + text="Hello, World!", conversation_id=conversation.id, user_id=user.id + ) + + new_message_data = UpdateMessage( + text="Hello, Universe!", + ) + + updated_message = message_crud.update_message(session, message, new_message_data) + assert updated_message.text == new_message_data.text + assert updated_message.conversation_id == message.conversation_id + assert updated_message.position == message.position + assert updated_message.agent == message.agent + + +def test_delete_message(session, conversation, user): + message = get_factory("Message", session).create( + text="Hello, World!", conversation_id=conversation.id, user_id=user.id + ) + + message_crud.delete_message(session, message.id, user.id) + + message = message_crud.get_message(session, message.id, user.id) + assert message is None + + +def test_delete_message_cascade(session, conversation, user): + message = get_factory("Message", session).create( + text="Hello, World!", conversation_id=conversation.id, user_id=user.id + ) + citation = get_factory("Citation", session).create( + message_id=message.id, user_id=user.id + ) + citation_id = citation.id + document = get_factory("Document", session).create( + message_id=message.id, conversation_id=conversation.id, user_id=user.id + ) + document_id = document.id + message_crud.delete_message(session, message.id, user.id) + + message = message_crud.get_message(session, message.id, user.id) + assert message is None + assert message_crud.get_messages(session, user.id) == [] + assert ( + message_crud.get_messages_by_conversation_id(session, conversation.id, user.id) + == [] + ) + + citation = citation_crud.get_citation(session, citation_id) + assert citation is None + assert citation_crud.get_citations(session) == [] + + document = document_crud.get_document(session, document_id) + assert document is None + assert document_crud.get_documents(session) == [] + + +def test_create_message_file_association(session, conversation, user): + message = get_factory("Message", session).create( + text="Hello, World!", conversation_id=conversation.id, user_id=user.id + ) + file = get_factory("File", session).create(user_id=user.id) + + message_file_association = message_crud.create_message_file_association( + session, + MessageFileAssociation(message_id=message.id, file_id=file.id, user_id=user.id), + ) + assert message_file_association.message_id == message.id + assert message_file_association.file_id == file.id + assert message_file_association.user_id == user.id + + message = message_crud.get_message(session, message.id, user.id) + assert message.file_ids == [file.id] + + +def test_get_message_file_association_by_file_id(session, conversation, user): + message = get_factory("Message", session).create( + text="Hello, World!", conversation_id=conversation.id, user_id=user.id + ) + file = get_factory("File", session).create(user_id=user.id) + _ = get_factory("MessageFileAssociation", session).create( + message_id=message.id, file_id=file.id, user_id=user.id + ) + + message_file_association = message_crud.get_message_file_association_by_file_id( + session, file.id, user.id + ) + assert message_file_association.message_id == message.id + assert message_file_association.file_id == file.id + assert message_file_association.user_id == user.id + + +def test_delete_message_file_association(session, conversation, user): + message = get_factory("Message", session).create( + text="Hello, World!", conversation_id=conversation.id, user_id=user.id + ) + file = get_factory("File", session).create(user_id=user.id) + _ = get_factory("MessageFileAssociation", session).create( + message_id=message.id, file_id=file.id, user_id=user.id + ) + + message_crud.delete_message_file_association(session, message.id, file.id, user.id) + + message_file_association = message_crud.get_message_file_association_by_file_id( + session, file.id, user.id + ) + assert message_file_association is None + + message = message_crud.get_message(session, message.id, user.id) + assert message.file_ids == [] + + +def test_delete_messages(session, conversation, user): + for i in range(3): + _ = get_factory("Message", session).create( + text=f"Hello, World! {i}", conversation_id=conversation.id, user_id=user.id + ) + + messages = message_crud.get_messages_by_conversation_id(session, conversation.id, user.id) + assert len(messages) == 3 + + message_crud.delete_messages(session, [message.id for message in messages], user.id) + + messages = message_crud.get_messages_by_conversation_id(session, conversation.id, user.id) + assert len(messages) == 0 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_model.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_model.py new file mode 100644 index 0000000000000000000000000000000000000000..836afaebab642a92ca191a991def1f706f581fec --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_model.py @@ -0,0 +1,177 @@ +from sqlalchemy.orm import Session + +from backend.crud import model as model_crud +from backend.database_models.deployment import Deployment +from backend.database_models.model import Model +from backend.schemas.model import ModelCreate, ModelUpdate +from backend.tests.unit.factories import get_factory + + +def test_create_model(session: Session, deployment: Deployment) -> None: + model_data = ModelCreate( + name="Test Model", + cohere_name="Test Cohere Model", + description="Test Description", + deployment_id=deployment.id, + ) + + model = model_crud.create_model(session, model_data) + assert model.name == model_data.name + assert model.cohere_name == model_data.cohere_name + assert model.description == model_data.description + + model = model_crud.get_model(session, model.id) + assert model.name == model_data.name + + +def test_get_model(session: Session, deployment: Deployment) -> None: + model = get_factory("Model", session).create( + name="Test Model", deployment=deployment + ) + retrieved_model = model_crud.get_model(session, model.id) + assert retrieved_model.id == model.id + assert retrieved_model.name == model.name + + +def test_fail_get_nonexistent_model(session: Session) -> None: + model = model_crud.get_model(session, "123") + assert model is None + + +def test_list_models(session: Session, deployment: Deployment) -> None: + # Delete default models + session.query(Model).delete() + _ = get_factory("Model", session).create(name="Test Model", deployment=deployment) + + models = model_crud.get_models(session) + assert len(models) == 1 + assert models[0].name == "Test Model" + + +def test_list_models_empty(session: Session) -> None: + # Delete default models + session.query(Model).delete() + models = model_crud.get_models(session) + assert len(models) == 0 + + +def test_list_models_with_pagination(session: Session, deployment: Deployment) -> None: + # Delete default models + session.query(Model).delete() + for i in range(10): + _ = get_factory("Model", session).create( + name=f"Test Model {i}", deployment=deployment + ) + + models = model_crud.get_models(session, offset=5, limit=5) + assert len(models) == 5 + models.sort(key=lambda x: x.name) + for i, model in enumerate(models): + assert model.name == f"Test Model {i + 5}" + + +def test_get_models_by_deployment_id(session: Session, deployment: Deployment) -> None: + for i in range(10): + model = get_factory("Model", session).create( + name=f"Test Model {i}", deployment=deployment + ) + + models = model_crud.get_models_by_deployment_id(session, deployment.id) + + assert len(models) == 10 + for i, model in enumerate(models): + assert model.name == f"Test Model {i}" + + +def test_get_models_by_deployment_id_empty(session: Session, deployment: Deployment) -> None: + models = model_crud.get_models_by_deployment_id(session, deployment.id) + assert len(models) == 0 + + +def test_get_models_by_deployment_id_with_pagination(session: Session, deployment: Deployment) -> None: + for i in range(10): + model = get_factory("Model", session).create( + name=f"Test Model {i}", deployment=deployment + ) + + models = model_crud.get_models_by_deployment_id( + session, deployment.id, offset=5, limit=5 + ) + assert len(models) == 5 + + for i, model in enumerate(models): + assert model.name == f"Test Model {i + 5}" + + +def test_update_model(session: Session, deployment: Deployment) -> None: + model = get_factory("Model", session).create( + name="Sagemaker model", deployment=deployment + ) + another_deployment = get_factory("Deployment", session).create() + + new_model_data = ModelUpdate( + name="Cohere", + cohere_name="Cohere", + description="Cohere", + deployment_id=another_deployment.id, + ) + + updated_model = model_crud.update_model(session, model, new_model_data) + + assert updated_model.name == new_model_data.name + assert updated_model.cohere_name == new_model_data.cohere_name + assert updated_model.description == new_model_data.description + assert updated_model.deployment_id == new_model_data.deployment_id + + model = model_crud.get_model(session, model.id) + assert model.name == new_model_data.name + assert model.cohere_name == new_model_data.cohere_name + assert model.description == new_model_data.description + assert model.deployment_id == new_model_data.deployment_id + + +def test_update_model_partial(session: Session, deployment: Deployment) -> None: + model = get_factory("Model", session).create( + name="Test Model U", deployment=deployment + ) + + new_model_data = ModelUpdate(name="Cohere", cohere_name="Cohere") + + updated_model = model_crud.update_model(session, model, new_model_data) + + assert updated_model.name == new_model_data.name + assert updated_model.cohere_name == new_model_data.cohere_name + assert updated_model.description == model.description + assert updated_model.deployment_id == model.deployment_id + + model = model_crud.get_model(session, model.id) + assert model.name == new_model_data.name + assert model.cohere_name == new_model_data.cohere_name + assert model.description == model.description + assert model.deployment_id == model.deployment_id + + +def test_do_not_update_model(session: Session, deployment: Deployment) -> None: + model = get_factory("Model", session).create( + name="Test Model", deployment=deployment + ) + + new_model_data = ModelUpdate(name="Test Model") + + updated_model = model_crud.update_model(session, model, new_model_data) + assert updated_model.name == model.name + + +def test_delete_model(session: Session, deployment: Deployment) -> None: + model = get_factory("Model", session).create(deployment=deployment) + + model_crud.delete_model(session, model.id) + + model = model_crud.get_model(session, model.id) + assert model is None + + +def test_delete_nonexistent_model(session: Session) -> None: + model_crud.delete_model(session, "123") # no error + model = model_crud.get_model(session, "123") + assert model is None diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_organization.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_organization.py new file mode 100644 index 0000000000000000000000000000000000000000..5efd2be2ae59c23a7db89a25c76f7b1921a015a3 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_organization.py @@ -0,0 +1,250 @@ +import pytest + +from backend.crud import organization as organization_crud +from backend.database_models.organization import Organization +from backend.schemas.organization import UpdateOrganization +from backend.tests.unit.factories import get_factory + + +def test_create_organization(session): + organization_data = Organization( + name="Test Organization", + ) + + organization = organization_crud.create_organization(session, organization_data) + assert organization.name == organization_data.name + + organization = organization_crud.get_organization(session, organization.id) + assert organization.name == organization_data.name + + +def test_get_organization(session): + _ = get_factory("Organization", session).create(id="1", name="Test Organization") + + organization = organization_crud.get_organization(session, "1") + assert organization.name == "Test Organization" + + +def test_fail_get_nonexistent_organization(session): + organization = organization_crud.get_organization(session, "123") + assert organization is None + + +def test_list_organizations(session): + # Delete default organization + session.query(Organization).delete() + _ = get_factory("Organization", session).create(name="Test Organization") + + organizations = organization_crud.get_organizations(session) + assert len(organizations) == 1 + assert organizations[0].name == "Test Organization" + + +def test_list_organizations_empty(session): + # Delete default organization + session.query(Organization).delete() + organizations = organization_crud.get_organizations(session) + assert len(organizations) == 0 + + +def test_list_organizations_with_pagination(session): + # Delete default organization + session.query(Organization).delete() + for i in range(10): + _ = get_factory("Organization", session).create(name=f"Test Organization {i}") + + organizations = organization_crud.get_organizations(session, offset=5, limit=5) + assert len(organizations) == 5 + + for i, organization in enumerate(organizations): + assert organization.name == f"Test Organization {i + 5}" + + +def test_list_organizations_by_user_id(session): + organization = get_factory("Organization", session).create(name="Test Organization") + user = get_factory("User", session).create() + user.organizations.append(organization) + + organizations = organization_crud.get_organizations_by_user_id(session, user.id) + assert len(organizations) == 1 + assert organizations[0].name == "Test Organization" + + +def test_list_organizations_by_user_id_empty(session): + user = get_factory("User", session).create() + + organizations = organization_crud.get_organizations_by_user_id(session, user.id) + assert len(organizations) == 0 + + +def test_list_organizations_by_user_id_with_pagination(session): + # Delete default organization + session.query(Organization).delete() + user = get_factory("User", session).create() + for i in range(10): + organization = get_factory("Organization", session).create( + name=f"Test Organization {i}" + ) + user.organizations.append(organization) + + organizations = organization_crud.get_organizations_by_user_id( + session, user.id, offset=5, limit=5 + ) + assert len(organizations) == 5 + + for i, organization in enumerate(organizations): + assert organization.name == f"Test Organization {i + 5}" + + +def test_update_organization(session): + organization = get_factory("Organization", session).create(name="John Doe") + + new_organization_data = UpdateOrganization(name="Jane Doe") + + updated_organization = organization_crud.update_organization( + session, organization, new_organization_data + ) + assert updated_organization.name == new_organization_data.name + + organization = organization_crud.get_organization(session, organization.id) + assert organization.name == new_organization_data.name + + +def test_update_organization_partial(session): + organization = get_factory("Organization", session).create( + name="Test Organization U" + ) + + new_organization_data = UpdateOrganization( + name="Test Organization U", + ) + + updated_organization = organization_crud.update_organization( + session, organization, new_organization_data + ) + assert updated_organization.name == new_organization_data.name + + +def test_donot_update_organization(session): + organization = get_factory("Organization", session).create(name="Test Organization") + + new_organization_data = UpdateOrganization(name="Test Organization") + + updated_organization = organization_crud.update_organization( + session, organization, new_organization_data + ) + assert updated_organization.name == organization.name + + +def test_delete_organization(session): + organization = get_factory("Organization", session).create() + + organization_crud.delete_organization(session, organization.id) + + organization = organization_crud.get_organization(session, organization.id) + assert organization is None + + +def test_delete_nonexistent_organization(session): + organization_crud.delete_organization(session, "123") # no error + organization = organization_crud.get_organization(session, "123") + assert organization is None + + +def test_add_user_to_organization(session): + organization = get_factory("Organization", session).create() + user = get_factory("User", session).create() + + organization_crud.add_user_to_organization(session, user.id, organization.id) + + organization = organization_crud.get_organization(session, organization.id) + assert user in organization.users + + +def test_remove_user_from_organization(session): + organization = get_factory("Organization", session).create() + user = get_factory("User", session).create() + organization.users.append(user) + organization_crud.remove_user_from_organization(session, user.id, organization.id) + organization = organization_crud.get_organization(session, organization.id) + assert user not in organization.users + + +def test_add_non_existent_user_to_organization(session): + organization = get_factory("Organization", session).create() + with pytest.raises(Exception): + organization_crud.add_user_to_organization(session, "123", organization.id) + + +def test_remove_non_existent_user_from_organization(session): + organization = get_factory("Organization", session).create() + + organization_crud.remove_user_from_organization( + session, "123", organization.id + ) # no error + + organization = organization_crud.get_organization(session, organization.id) + assert len(organization.users) == 0 + + +def test_get_users_by_organization_id(session): + organization = get_factory("Organization", session).create() + user = get_factory("User", session).create() + + organization.users.append(user) + + users = organization_crud.get_users_by_organization_id(session, organization.id) + assert len(users) == 1 + assert users[0].id == user.id + + +def test_add_user_to_non_existent_organization(session): + user = get_factory("User", session).create() + with pytest.raises(Exception): + organization_crud.add_user_to_organization(session, user.id, "123") + + +def test_user_organization_association(session): + organization = get_factory("Organization", session).create(name="Test Organization") + user = get_factory("User", session).create(fullname="John Doe") + user.organizations.append(organization) + assert user.organizations[0].name == "Test Organization" + + +def test_user_organization_association_reverse(session): + organization = get_factory("Organization", session).create(name="Test Organization") + user = get_factory("User", session).create(fullname="John Doe") + organization.users.append(user) + assert organization.users[0].fullname == "John Doe" + + +def test_agent_organization_association(session, user): + organization = get_factory("Organization", session).create(name="Test Organization") + agent = get_factory("Agent", session).create(user=user, name="Test Agent") + agent.organization = organization + assert agent.organization.name == "Test Organization" + + +def test_agent_organization_association_reverse(session, user): + organization = get_factory("Organization", session).create(name="Test Organization") + agent = get_factory("Agent", session).create(user=user, name="Test Agent") + organization.agents.append(agent) + assert organization.agents[0].name == "Test Agent" + + +def test_conversation_organization_association(session, user): + organization = get_factory("Organization", session).create(name="Test Organization") + conversation = get_factory("Conversation", session).create( + title="Test Conversation", user_id=user.id + ) + conversation.organization = organization + assert conversation.organization.name == "Test Organization" + + +def test_conversation_organization_association_reverse(session, user): + organization = get_factory("Organization", session).create(name="Test Organization") + conversation = get_factory("Conversation", session).create( + title="Test Conversation", user_id=user.id + ) + organization.conversations.append(conversation) + assert organization.conversations[0].title == "Test Conversation" diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_snapshot.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_snapshot.py new file mode 100644 index 0000000000000000000000000000000000000000..7f2060a0a88c0df7f130d26661cf642b461533bb --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_snapshot.py @@ -0,0 +1,200 @@ +import pytest + +from backend.crud import snapshot as snapshot_crud +from backend.database_models.snapshot import Snapshot, SnapshotAccess, SnapshotLink +from backend.tests.unit.factories import get_factory + +snapshot_json = { + "title": "Hello, World!", + "description": "This is a test snapshot.", +} + + +@pytest.fixture(autouse=True) +def conversation(session, user): + return get_factory("Conversation", session).create(id="1", user_id=user.id) + + +@pytest.fixture(autouse=True) +def message(session, conversation, user): + return get_factory("Message", session).create( + id="1", conversation_id=conversation.id, user_id=user.id + ) + + +@pytest.fixture(autouse=True) +def organization(session): + return get_factory("Organization", session).create(id="1") + + +@pytest.fixture(autouse=True) +def snapshot(session, conversation, organization, user): + return get_factory("Snapshot", session).create( + id="1", + user_id=user.id, + organization_id="1", + conversation_id="1", + last_message_id="1", + version=1, + snapshot=snapshot_json, + ) + + +@pytest.fixture(autouse=True) +def snapshot_link(session, user): + return get_factory("SnapshotLink", session).create( + id="1", snapshot_id="1", user_id=user.id + ) + + +# Snapshot +def test_create_snapshot(session, user): + snapshot_data = Snapshot( + user_id=user.id, + organization_id="1", + conversation_id="1", + last_message_id="1", + version=1, + snapshot=snapshot_json, + ) + + snapshot = snapshot_crud.create_snapshot(session, snapshot_data) + assert snapshot.user_id == snapshot_data.user_id + assert snapshot.organization_id == snapshot_data.organization_id + assert snapshot.conversation_id == snapshot_data.conversation_id + assert snapshot.last_message_id == snapshot_data.last_message_id + assert snapshot.version == snapshot_data.version + assert snapshot.snapshot == snapshot_data.snapshot + + snapshot = snapshot_crud.get_snapshot(session, snapshot.id) + assert snapshot.user_id == snapshot_data.user_id + assert snapshot.organization_id == snapshot_data.organization_id + assert snapshot.conversation_id == snapshot_data.conversation_id + assert snapshot.last_message_id == snapshot_data.last_message_id + assert snapshot.version == snapshot_data.version + assert snapshot.snapshot == snapshot_data.snapshot + + +def test_get_snapshot(session, user): + snapshot = snapshot_crud.get_snapshot(session, "1") + assert snapshot.user_id == user.id + assert snapshot.organization_id == "1" + assert snapshot.conversation_id == "1" + assert snapshot.last_message_id == "1" + assert snapshot.version == 1 + assert snapshot.snapshot == snapshot_json + + +def test_fail_get_nonexistent_snapshot(session): + snapshot = snapshot_crud.get_snapshot(session, "123") + assert snapshot is None + + +def test_get_snapshot_by_last_message_id(session): + snapshot = snapshot_crud.get_snapshot_by_last_message_id(session, "1") + assert snapshot.last_message_id == "1" + + +def test_fail_get_nonexistent_snapshot_by_last_message_id(session): + snapshot = snapshot_crud.get_snapshot_by_last_message_id(session, "123") + assert snapshot is None + + +def test_list_snapshots(session, user): + snapshots = snapshot_crud.list_snapshots(session, user.id) + assert len(snapshots) == 1 + assert snapshots[0].user_id == user.id + assert snapshots[0].organization_id == "1" + assert snapshots[0].conversation_id == "1" + assert snapshots[0].last_message_id == "1" + assert snapshots[0].version == 1 + assert snapshots[0].snapshot == snapshot_json + + +def test_fail_list_snapshots(session): + snapshots = snapshot_crud.list_snapshots(session, "123") + assert len(snapshots) == 0 + + +def test_delete_snapshot(session, user): + snapshot_crud.delete_snapshot(session, "1", user.id) + + assert snapshot_crud.get_snapshot(session, "1") is None + + +# SnapshotLink +def test_create_snapshot_link(session, user): + snapshot_link_data = SnapshotLink( + snapshot_id="1", + user_id=user.id, + ) + + snapshot_link = snapshot_crud.create_snapshot_link(session, snapshot_link_data) + assert snapshot_link.snapshot_id == snapshot_link_data.snapshot_id + + snapshot_link = snapshot_crud.get_snapshot_link(session, snapshot_link.id) + assert snapshot_link.snapshot_id == snapshot_link_data.snapshot_id + + +def test_get_snapshot_link(session): + snapshot_link = snapshot_crud.get_snapshot_link(session, "1") + assert snapshot_link.snapshot_id == "1" + + +def test_fail_get_nonexistent_snapshot_link(session): + snapshot_link = snapshot_crud.get_snapshot_link(session, "123") + assert snapshot_link is None + + +def test_list_snapshot_links(session): + snapshot_links = snapshot_crud.list_snapshot_links(session, "1") + assert len(snapshot_links) == 1 + assert snapshot_links[0].snapshot_id == "1" + + +def test_fail_list_snapshot_links(session): + snapshot_links = snapshot_crud.list_snapshot_links(session, "123") + assert len(snapshot_links) == 0 + + +def test_delete_snapshot_link(session, user): + snapshot_crud.delete_snapshot_link(session, "1", user.id) + + assert snapshot_crud.get_snapshot_link(session, "1") is None + + +# # SnapshotAccess +def test_create_snapshot_access(session, user): + snapshot_access_data = SnapshotAccess( + user_id=user.id, + snapshot_id="1", + link_id="1", + ) + + snapshot_access = snapshot_crud.create_snapshot_access( + session, snapshot_access_data + ) + assert snapshot_access.user_id == snapshot_access_data.user_id + assert snapshot_access.snapshot_id == snapshot_access_data.snapshot_id + assert snapshot_access.link_id == snapshot_access_data.link_id + + snapshot_access = snapshot_crud.get_snapshot_access(session, snapshot_access.id) + assert snapshot_access.user_id == snapshot_access_data.user_id + assert snapshot_access.snapshot_id == snapshot_access_data.snapshot_id + assert snapshot_access.link_id == snapshot_access_data.link_id + + +def test_get_snapshot_access(session, user): + _ = get_factory("SnapshotAccess", session).create( + id="1", user_id=user.id, snapshot_id="1", link_id="1" + ) + + snapshot_access = snapshot_crud.get_snapshot_access(session, "1") + assert snapshot_access.user_id == user.id + assert snapshot_access.snapshot_id == "1" + assert snapshot_access.link_id == "1" + + +def test_fail_get_nonexistent_snapshot_access(session): + snapshot_access = snapshot_crud.get_snapshot_access(session, "123") + assert snapshot_access is None diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_tool_auth.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_tool_auth.py new file mode 100644 index 0000000000000000000000000000000000000000..192b9f4c051990bc6c3ef2cffa0ff31a9a26f8e0 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_tool_auth.py @@ -0,0 +1,49 @@ +from datetime import datetime + +from backend.config.tools import Tool +from backend.crud import tool_auth as tool_auth_crud +from backend.database_models.tool_auth import ToolAuth +from backend.tests.unit.factories import get_factory + + +def test_create_tool_auth(session, user): + tool_auth_data = ToolAuth( + user_id=user.id, + tool_id=Tool.Google_Drive.value.ID, + token_type="Bearer", + encrypted_access_token=bytes(b"foobar"), + encrypted_refresh_token=bytes(b"foobar"), + expires_at=datetime.strptime("12/31/2124 00:00:00", "%m/%d/%Y %H:%M:%S"), + created_at=datetime.strptime("01/01/2000 00:00:00", "%m/%d/%Y %H:%M:%S"), + updated_at=datetime.strptime("01/01/2010 00:00:00", "%m/%d/%Y %H:%M:%S"), + ) + + tool_auth = tool_auth_crud.create_tool_auth(session, tool_auth_data) + + assert tool_auth.user_id == tool_auth_data.user_id + assert tool_auth.tool_id == tool_auth_data.tool_id + assert tool_auth.token_type == tool_auth_data.token_type + assert tool_auth.encrypted_access_token == tool_auth_data.encrypted_access_token + assert tool_auth.encrypted_refresh_token == tool_auth_data.encrypted_refresh_token + assert tool_auth.expires_at == tool_auth_data.expires_at + assert tool_auth.id == tool_auth_data.id + assert tool_auth.created_at == tool_auth_data.created_at + assert tool_auth.updated_at == tool_auth_data.updated_at + + +def test_delete_tool_auth_by_tool_id(session, user): + tool_auth = get_factory("ToolAuth", session).create( + user_id=user.id, + tool_id=Tool.Google_Drive.value.ID, + token_type="Bearer", + encrypted_access_token=bytes(b"foobar"), + encrypted_refresh_token=bytes(b"foobar"), + expires_at=datetime.strptime("12/31/2124 00:00:00", "%m/%d/%Y %H:%M:%S"), + created_at=datetime.strptime("01/01/2000 00:00:00", "%m/%d/%Y %H:%M:%S"), + updated_at=datetime.strptime("01/01/2010 00:00:00", "%m/%d/%Y %H:%M:%S"), + ) + + tool_auth_crud.delete_tool_auth(session, user.id, tool_auth.tool_id) + + tool_auth = tool_auth_crud.get_tool_auth(session, tool_auth.tool_id, user.id) + assert tool_auth is None diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_tool_call.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_tool_call.py new file mode 100644 index 0000000000000000000000000000000000000000..a423d06d116b6831ab405a4821501eec60a6873a --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_tool_call.py @@ -0,0 +1,63 @@ +import pytest + +from backend.crud import tool_call as tool_call_crud +from backend.database_models.tool_call import ToolCall +from backend.tests.unit.factories import get_factory + + +@pytest.fixture(autouse=True) +def conversation(session, user): + return get_factory("Conversation", session).create(id="1", user_id=user.id) + + +@pytest.fixture(autouse=True) +def message(session, conversation, user): + return get_factory("Message", session).create( + id="1", conversation_id=conversation.id, user_id=user.id + ) + + +def test_create_tool_call(session): + tool_call_data = ToolCall( + name="Hello, World!", + parameters={"test": "test"}, + message_id="1", + ) + + tool_call = tool_call_crud.create_tool_call(session, tool_call_data) + assert tool_call.name == tool_call_data.name + assert tool_call.parameters == tool_call_data.parameters + assert tool_call.message_id == tool_call_data.message_id + + tool_call = tool_call_crud.get_tool_call_by_id(session, tool_call.id) + assert tool_call.name == tool_call_data.name + assert tool_call.parameters == tool_call_data.parameters + assert tool_call.message_id == tool_call_data.message_id + + +def test_get_tool_call_by_id(session): + tool_call_data = ToolCall( + name="Hello, World!", + parameters={"test": "test"}, + message_id="1", + ) + + tool_call = tool_call_crud.create_tool_call(session, tool_call_data) + assert tool_call_crud.get_tool_call_by_id(session, tool_call.id) == tool_call + + +def test_get_tool_call_by_id_not_found(session): + assert tool_call_crud.get_tool_call_by_id(session, "1") is None + + +def test_list_tool_calls_by_message_id(session): + _ = get_factory("ToolCall", session).create(name="Hello, World!", message_id="1") + + tool_calls = tool_call_crud.list_tool_calls_by_message_id(session, "1") + assert len(tool_calls) == 1 + assert tool_calls[0].name == "Hello, World!" + + +def test_list_tool_calls_by_message_id_empty(session): + tool_calls = tool_call_crud.list_tool_calls_by_message_id(session, "1") + assert len(tool_calls) == 0 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_user.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_user.py new file mode 100644 index 0000000000000000000000000000000000000000..8858278431753756d7f4afc8ca7fdcb094599f65 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/crud/test_user.py @@ -0,0 +1,115 @@ +from backend.crud import user as user_crud +from backend.database_models.user import User +from backend.schemas.user import UpdateUser +from backend.tests.unit.factories import get_factory + + +def test_create_user(session): + user_data = User( + fullname="John Doe", + email="test@email.com", + ) + + user = user_crud.create_user(session, user_data) + assert user.fullname == user_data.fullname + assert user.email == user_data.email + + user = user_crud.get_user(session, user.id) + assert user.fullname == user_data.fullname + assert user.email == user_data.email + + +def test_get_user(session): + _ = get_factory("User", session).create(id="1", fullname="John Doe") + + user = user_crud.get_user(session, "1") + assert user.fullname == "John Doe" + + +def test_fail_get_nonexistent_user(session): + user = user_crud.get_user(session, "123") + assert user is None + + +def test_list_users(session): + # Delete default users + session.query(User).delete() + _ = get_factory("User", session).create(fullname="John Doe") + + users = user_crud.get_users(session) + assert len(users) == 1 + assert users[0].fullname == "John Doe" + + +def test_list_users_empty(session): + # Delete default users + session.query(User).delete() + users = user_crud.get_users(session) + assert len(users) == 0 + + +def test_list_users_with_pagination(session): + # Delete default users + session.query(User).delete() + for i in range(10): + _ = get_factory("User", session).create(fullname=f"John Doe {i}") + + users = user_crud.get_users(session, offset=5, limit=5) + assert len(users) == 5 + + +def test_update_user(session): + user = get_factory("User", session).create(fullname="John Doe") + + new_user_data = UpdateUser(fullname="Jane Doe", email="janedoe@email.com") + + updated_user = user_crud.update_user(session, user, new_user_data) + assert updated_user.fullname == new_user_data.fullname + assert updated_user.email == new_user_data.email + + user = user_crud.get_user(session, user.id) + assert user.fullname == new_user_data.fullname + assert user.email == new_user_data.email + + +def test_update_user_partial(session): + user = get_factory("User", session).create(fullname="John Doe") + + new_user_data = UpdateUser( + fullname="Jane Doe", + ) + + updated_user = user_crud.update_user(session, user, new_user_data) + assert updated_user.fullname == new_user_data.fullname + assert updated_user.email == user.email + + +def test_donot_update_user(session): + user = get_factory("User", session).create(fullname="John Doe") + + new_user_data = UpdateUser(fullname="John Doe") + + updated_user = user_crud.update_user(session, user, new_user_data) + assert updated_user.fullname == user.fullname + assert updated_user.email == user.email + + +def test_delete_user(session): + user = get_factory("User", session).create() + + user_crud.delete_user(session, user.id) + + user = user_crud.get_user(session, user.id) + assert user is None + + +def test_delete_nonexistent_user(session): + user = User( + fullname="John Doe", + email="johndoe@email.com", + ) + + user_crud.delete_user(session, user.id) # no error + + user = user_crud.get_user(session, user.id) + assert user is None diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e5b06353e217625de947f777748a4bd1ead85067 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/__init__.py @@ -0,0 +1,57 @@ +from backend.tests.unit.factories.agent import AgentFactory +from backend.tests.unit.factories.agent_tool_metadata import AgentToolMetadataFactory +from backend.tests.unit.factories.blacklist import BlacklistFactory +from backend.tests.unit.factories.citation import CitationFactory +from backend.tests.unit.factories.citation_documents import CitationDocumentsFactory +from backend.tests.unit.factories.conversation import ( + ConversationFactory, + ConversationFileAssociationFactory, +) +from backend.tests.unit.factories.deployment import DeploymentFactory +from backend.tests.unit.factories.document import DocumentFactory +from backend.tests.unit.factories.file import FileFactory +from backend.tests.unit.factories.group import GroupFactory +from backend.tests.unit.factories.message import ( + MessageFactory, + MessageFileAssociationFactory, +) +from backend.tests.unit.factories.model import ModelFactory +from backend.tests.unit.factories.organization import OrganizationFactory +from backend.tests.unit.factories.snapshot import ( + SnapshotAccessFactory, + SnapshotFactory, + SnapshotLinkFactory, +) +from backend.tests.unit.factories.tool_auth import ToolAuthFactory +from backend.tests.unit.factories.tool_call import ToolCallFactory +from backend.tests.unit.factories.user import UserFactory + +FACTORY_MAPPING = { + "User": UserFactory, + "Blacklist": BlacklistFactory, + "File": FileFactory, + "Conversation": ConversationFactory, + "Citation": CitationFactory, + "CitationDocuments": CitationDocumentsFactory, + "Message": MessageFactory, + "Document": DocumentFactory, + "Agent": AgentFactory, + "Organization": OrganizationFactory, + "ToolCall": ToolCallFactory, + "ToolAuth": ToolAuthFactory, + "Snapshot": SnapshotFactory, + "SnapshotLink": SnapshotLinkFactory, + "SnapshotAccess": SnapshotAccessFactory, + "AgentToolMetadata": AgentToolMetadataFactory, + "Model": ModelFactory, + "Deployment": DeploymentFactory, + "ConversationFileAssociation": ConversationFileAssociationFactory, + "MessageFileAssociation": MessageFileAssociationFactory, + "Group": GroupFactory, +} + + +def get_factory(model_name, session=None): + factory = FACTORY_MAPPING[model_name] + factory._meta.sqlalchemy_session = session + return factory diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/agent.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/agent.py new file mode 100644 index 0000000000000000000000000000000000000000..63b5b8032bf2aec5ac640f7ad25bf75a6f4e0b18 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/agent.py @@ -0,0 +1,40 @@ +import factory + +from backend.config.tools import Tool +from backend.database_models.agent import Agent +from backend.tests.unit.factories.base import BaseFactory +from backend.tests.unit.factories.user import UserFactory + + +class AgentFactory(BaseFactory): + class Meta: + model = Agent + + user = factory.SubFactory(UserFactory) + user_id = factory.SelfAttribute("user.id") + organization_id = None + deployment_id = None + model_id = None + name = factory.Faker("sentence") + description = factory.Faker("sentence") + preamble = factory.Faker("sentence") + version = factory.Faker("random_int") + temperature = factory.Faker("pyfloat", min_value=0.0, max_value=1.0) + created_at = factory.Faker("date_time") + updated_at = factory.Faker("date_time") + tools = factory.List( + [ + factory.Faker( + "random_element", + elements=[ + Tool.Wiki_Retriever_LangChain.value.ID, + Tool.Search_File.value.ID, + Tool.Read_File.value.ID, + Tool.Python_Interpreter.value.ID, + Tool.Calculator.value.ID, + Tool.Tavily_Web_Search.value.ID, + ], + ) + ] + ) + is_private = factory.Faker("boolean", chance_of_getting_true=0) diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/agent_tool_metadata.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/agent_tool_metadata.py new file mode 100644 index 0000000000000000000000000000000000000000..26a060d09905c55e5ee164d09dac401ec7637458 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/agent_tool_metadata.py @@ -0,0 +1,31 @@ +import factory + +from backend.config.tools import Tool +from backend.database_models.agent_tool_metadata import AgentToolMetadata + +from .base import BaseFactory + + +class AgentToolMetadataFactory(BaseFactory): + class Meta: + model = AgentToolMetadata + + user_id = factory.Faker("uuid4") + tool_name = factory.List( + [ + factory.Faker( + "random_element", + elements=[ + Tool.Wiki_Retriever_LangChain.value.ID, + Tool.Search_File.value.ID, + Tool.Read_File.value.ID, + Tool.Python_Interpreter.value.ID, + Tool.Calculator.value.ID, + Tool.Tavily_Web_Search.value.ID, + Tool.Google_Drive.value.ID, + ], + ) + ] + ) + agent_id = factory.Faker("uuid4") + artifacts = factory.List([]) diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/base.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/base.py new file mode 100644 index 0000000000000000000000000000000000000000..816d376ed3fffddc3040c22613937756e34bf4ca --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/base.py @@ -0,0 +1,16 @@ +from uuid import uuid4 + +import factory + + +class BaseFactory(factory.alchemy.SQLAlchemyModelFactory): + """Always inherit from here""" + + class Meta: + abstract = True # Cannot initialize + sqlalchemy_session = None + sqlalchemy_session_persistence = "flush" + + id = factory.LazyFunction(lambda: str(uuid4())) + created_at = factory.Faker("date_time") + updated_at = factory.Faker("date_time") diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/blacklist.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/blacklist.py new file mode 100644 index 0000000000000000000000000000000000000000..96b0f5abc977e3f50937f7f58ee31aea1f0b6570 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/blacklist.py @@ -0,0 +1,12 @@ +import factory + +from backend.database_models import Blacklist + +from .base import BaseFactory + + +class BlacklistFactory(BaseFactory): + class Meta: + model = Blacklist + + token_id = factory.Faker("uuid4") diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/citation.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/citation.py new file mode 100644 index 0000000000000000000000000000000000000000..3ccbb0d02ca0dec6493c5ab12cd77f3aef46f064 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/citation.py @@ -0,0 +1,18 @@ +import factory + +from backend.database_models import Citation + +from .base import BaseFactory + + +class CitationFactory(BaseFactory): + class Meta: + model = Citation + + text = factory.Faker("text") + user_id = factory.Faker("uuid4") + message_id = factory.Faker("uuid4") + start = factory.Faker("random_int") + end = factory.Faker("random_int") + + documents = [] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/citation_documents.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/citation_documents.py new file mode 100644 index 0000000000000000000000000000000000000000..7aa23cb312de9fa63b23ff5143f664d9c6075a12 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/citation_documents.py @@ -0,0 +1,13 @@ +import factory + +from backend.database_models import CitationDocuments + +from .base import BaseFactory + + +class CitationDocumentsFactory(BaseFactory): + class Meta: + model = CitationDocuments + + left_id = factory.Faker("uuid4") + right_id = factory.Faker("uuid4") diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/conversation.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/conversation.py new file mode 100644 index 0000000000000000000000000000000000000000..3b041627486176a93a6955fd45e7103c9b22035e --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/conversation.py @@ -0,0 +1,29 @@ +import factory + +from backend.database_models import Conversation, ConversationFileAssociation + +from .base import BaseFactory + + +class ConversationFactory(BaseFactory): + class Meta: + model = Conversation + + user_id = factory.Faker("uuid4") + description = factory.Faker("sentence") + title = factory.Faker("sentence") + created_at = factory.Faker("date_time") + updated_at = factory.Faker("date_time") + text_messages = [] + agent_id = None + organization_id = None + is_pinned = False + + +class ConversationFileAssociationFactory(BaseFactory): + class Meta: + model = ConversationFileAssociation + + conversation_id = factory.Faker("uuid4") + user_id = factory.Faker("uuid4") + file_id = factory.Faker("uuid4") diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/deployment.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/deployment.py new file mode 100644 index 0000000000000000000000000000000000000000..b98e8cda911934e2e1e273f2d9674cfc0d28ea46 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/deployment.py @@ -0,0 +1,18 @@ +import factory + +from backend.database_models import Deployment + +from .base import BaseFactory + + +class DeploymentFactory(BaseFactory): + class Meta: + model = Deployment + + name = factory.Faker("name") + description = factory.Faker("text") + deployment_class_name = "CohereDeployment" + is_community = False + default_deployment_config = factory.Faker( + "pydict", nb_elements=3, variable_nb_elements=True, value_types=["str"] + ) diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/document.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/document.py new file mode 100644 index 0000000000000000000000000000000000000000..096e29f7afb37a0380f563c6d38b3d451efb79bb --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/document.py @@ -0,0 +1,18 @@ +import factory + +from backend.database_models import Document + +from .base import BaseFactory + + +class DocumentFactory(BaseFactory): + class Meta: + model = Document + + user_id = factory.Faker("uuid4") + title = factory.Faker("sentence") + url = factory.Faker("url") + conversation_id = factory.Faker("uuid4") + message_id = factory.Faker("uuid4") + document_id = factory.Faker("uuid4") + text = factory.Faker("text") diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/file.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/file.py new file mode 100644 index 0000000000000000000000000000000000000000..730b7900ba899641e1ced10bdf086df1b358052a --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/file.py @@ -0,0 +1,14 @@ +import factory + +from backend.database_models import File + +from .base import BaseFactory + + +class FileFactory(BaseFactory): + class Meta: + model = File + + user_id = factory.Faker("uuid4") + file_name = factory.Faker("file_name") + file_size = factory.Faker("random_int", min=1, max=20000000) diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/group.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/group.py new file mode 100644 index 0000000000000000000000000000000000000000..df4974d068bafd4b83d73370b838de108fec0dc1 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/group.py @@ -0,0 +1,12 @@ +import factory + +from backend.database_models import Group + +from .base import BaseFactory + + +class GroupFactory(BaseFactory): + class Meta: + model = Group + + display_name = factory.Faker("name") diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/message.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/message.py new file mode 100644 index 0000000000000000000000000000000000000000..7ed165f6583b5fb7bc9b310e11e60b9c389e419b --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/message.py @@ -0,0 +1,30 @@ +import factory + +from backend.database_models import Message, MessageFileAssociation + +from .base import BaseFactory + + +class MessageFactory(BaseFactory): + class Meta: + model = Message + + text = factory.Faker("text") + tool_plan = factory.Faker("text") + user_id = factory.Faker("uuid4") + generation_id = factory.Faker("uuid4") + conversation_id = factory.Faker("uuid4") + position = factory.Faker("random_int") + is_active = factory.Faker("boolean") + documents = [] + citations = [] + agent = factory.Faker("random_element", elements=("USER", "CHATBOT")) + + +class MessageFileAssociationFactory(BaseFactory): + class Meta: + model = MessageFileAssociation + + message_id = factory.Faker("uuid4") + user_id = factory.Faker("uuid4") + file_id = factory.Faker("uuid4") diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/model.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/model.py new file mode 100644 index 0000000000000000000000000000000000000000..c7b608af8fdd785365f506519082d38fa0ce51d6 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/model.py @@ -0,0 +1,16 @@ +import factory + +from backend.database_models import Model +from backend.tests.unit.factories.base import BaseFactory +from backend.tests.unit.factories.deployment import DeploymentFactory + + +class ModelFactory(BaseFactory): + class Meta: + model = Model + + deployment = factory.SubFactory(DeploymentFactory) + deployment_id = factory.SelfAttribute("deployment.id") + name = "command-r-plus" + cohere_name = factory.Faker("name") + description = factory.Faker("text") diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/organization.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/organization.py new file mode 100644 index 0000000000000000000000000000000000000000..786af0bd22c4d69d1598537938c97325f4b1b278 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/organization.py @@ -0,0 +1,12 @@ +import factory + +from backend.database_models import Organization + +from .base import BaseFactory + + +class OrganizationFactory(BaseFactory): + class Meta: + model = Organization + + name = factory.Faker("company") diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/snapshot.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/snapshot.py new file mode 100644 index 0000000000000000000000000000000000000000..550843bbd290683af47580744d24fde5c90ec0cd --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/snapshot.py @@ -0,0 +1,40 @@ +import factory + +from backend.database_models import Snapshot, SnapshotAccess, SnapshotLink + +from .base import BaseFactory + + +# Snapshot +class SnapshotFactory(BaseFactory): + class Meta: + model = Snapshot + + agent_id = None + user_id = factory.Faker("uuid4") + organization_id = factory.Faker("uuid4") + conversation_id = factory.Faker("uuid4") + last_message_id = factory.Faker("uuid4") + version = factory.Faker("random_int") + created_at = factory.Faker("date_time") + updated_at = factory.Faker("date_time") + snapshot = factory.Faker("pydict", value_types=[str, int, bool], nb_elements=10) + + +# SnapshotLink +class SnapshotLinkFactory(BaseFactory): + class Meta: + model = SnapshotLink + + snapshot_id = factory.Faker("uuid4") + user_id = factory.Faker("uuid4") + + +# SnapshotAccess +class SnapshotAccessFactory(BaseFactory): + class Meta: + model = SnapshotAccess + + user_id = factory.Faker("uuid4") + snapshot_id = factory.Faker("uuid4") + link_id = factory.Faker("uuid4") diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/tool_auth.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/tool_auth.py new file mode 100644 index 0000000000000000000000000000000000000000..bec1c71a893cc5201c3d6766fb46082a22bd585f --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/tool_auth.py @@ -0,0 +1,22 @@ +from datetime import datetime + +import factory + +from backend.config.tools import Tool +from backend.database_models.tool_auth import ToolAuth + +from .base import BaseFactory + + +class ToolAuthFactory(BaseFactory): + class Meta: + model = ToolAuth + + user_id = factory.Faker("uuid4") + tool_id = Tool.Google_Drive.value.ID + token_type = "Bearer" + encrypted_access_token = bytes(b"foobar") + encrypted_refresh_token = bytes(b"foobar") + expires_at = datetime.strptime("12/31/2124 00:00:00", "%m/%d/%Y %H:%M:%S") + created_at = datetime.strptime("01/01/2000 00:00:00", "%m/%d/%Y %H:%M:%S") + updated_at = datetime.strptime("01/01/2010 00:00:00", "%m/%d/%Y %H:%M:%S") diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/tool_call.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/tool_call.py new file mode 100644 index 0000000000000000000000000000000000000000..a259dc4b83b0a46909595e009d48dd3832c1b070 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/tool_call.py @@ -0,0 +1,14 @@ +import factory + +from backend.database_models.tool_call import ToolCall + +from .base import BaseFactory + + +class ToolCallFactory(BaseFactory): + class Meta: + model = ToolCall + + name = factory.Faker("name") + parameters = {"test": "test"} + message_id = "1" diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/user.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/user.py new file mode 100644 index 0000000000000000000000000000000000000000..680acf31cb1770daf78eb9538e232ca8f3bf6c41 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/factories/user.py @@ -0,0 +1,31 @@ +import factory + +from backend.database_models import User +from backend.services.auth import BasicAuthentication + +from .base import BaseFactory + + +class UserFactory(BaseFactory): + class Meta: + model = User + exclude = "password" + + fullname = factory.Faker("name") + email = factory.Faker("email") + password = factory.Faker("password") + hashed_password = factory.LazyAttribute( + lambda o: BasicAuthentication.hash_and_salt_password(o.password) + ) + + # def _create(self, *args, **kwargs): + # password = kwargs.pop("password", None) + # if not password: + # fake = faker.Faker() + # password = fake.password() + + # kwargs["hashed_password"] = BasicAuthentication.hash_and_salt_password(password) + + # user = super._create(*args, **kwargs) + + # return user diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/conftest.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/conftest.py new file mode 100644 index 0000000000000000000000000000000000000000..e62b44e4ef9d605f8b34f8c1d0ed579df1816ff9 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/conftest.py @@ -0,0 +1,54 @@ +from unittest.mock import patch + +import pytest +from sqlalchemy.orm import Session + +from backend.database_models.user import User +from backend.tests.unit.factories import get_factory +from backend.tests.unit.model_deployments.mock_deployments import ( + MockAzureDeployment, + MockBedrockDeployment, + MockCohereDeployment, + MockSageMakerDeployment, + MockSingleContainerDeployment, +) + + +@pytest.fixture() +def user(session_chat: Session) -> User: + return get_factory("User", session_chat).create() + + +@pytest.fixture() +def mock_cohere_deployment(): + with patch("backend.chat.custom.custom.get_deployment") as mock: + mock.return_value = MockCohereDeployment() + yield mock + + +@pytest.fixture() +def mock_single_container_deployment(): + with patch("backend.chat.custom.custom.get_deployment") as mock: + mock.return_value = MockSingleContainerDeployment() + yield mock + + +@pytest.fixture() +def mock_sagemaker_deployment(): + with patch("backend.chat.custom.custom.get_deployment") as mock: + mock.return_value = MockSageMakerDeployment() + yield mock + + +@pytest.fixture() +def mock_azure_deployment(): + with patch("backend.chat.custom.custom.get_deployment") as mock: + mock.return_value = MockAzureDeployment() + yield mock + + +@pytest.fixture() +def mock_bedrock_deployment(): + with patch("backend.chat.custom.custom.get_deployment") as mock: + mock.return_value = MockBedrockDeployment() + yield mock diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..0c06c19dcb77b0e1500be87ac28b89c170aef28d --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/__init__.py @@ -0,0 +1,23 @@ +from backend.tests.unit.model_deployments.mock_deployments.mock_azure import ( + MockAzureDeployment, +) +from backend.tests.unit.model_deployments.mock_deployments.mock_bedrock import ( + MockBedrockDeployment, +) +from backend.tests.unit.model_deployments.mock_deployments.mock_cohere_platform import ( + MockCohereDeployment, +) +from backend.tests.unit.model_deployments.mock_deployments.mock_sagemaker import ( + MockSageMakerDeployment, +) +from backend.tests.unit.model_deployments.mock_deployments.mock_single_container import ( + MockSingleContainerDeployment, +) + +__all__ = [ + "MockCohereDeployment", + "MockSageMakerDeployment", + "MockAzureDeployment", + "MockBedrockDeployment", + "MockSingleContainerDeployment", +] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_azure.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_azure.py new file mode 100644 index 0000000000000000000000000000000000000000..eb6ae9ddacbc67f59e4a273eaf415a913e33261f --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_azure.py @@ -0,0 +1,82 @@ +from typing import Any, Generator + +from cohere.types import StreamedChatResponse + +from backend.schemas.cohere_chat import CohereChatRequest +from backend.schemas.context import Context +from backend.tests.unit.model_deployments.mock_deployments.mock_base import ( + MockDeployment, +) + + +class MockAzureDeployment(MockDeployment): + """Mocked Azure Deployment.""" + + DEFAULT_MODELS = ["azure-command"] + + def __init__(self, **kwargs: Any): + pass + + @staticmethod + def name() -> str: + return "Azure" + + @staticmethod + def env_vars() -> list[str]: + return ["AZURE_API_KEY", "AZURE_CHAT_ENDPOINT_URL"] + + @staticmethod + def rerank_enabled() -> bool: + return False + + @classmethod + def list_models(cls) -> list[str]: + if not cls.is_available(): + return [] + + return cls.DEFAULT_MODELS + + @staticmethod + def is_available() -> bool: + return False + + async def invoke_chat( + self, chat_request: CohereChatRequest, **kwargs: Any + ) -> Any: + event = { + "text": "Hi! Hello there! How's it going?", + "generation_id": "ca0f398e-f8c8-48f0-b093-12d1754d00ed", + "citations": None, + "documents": None, + "is_search_required": None, + "search_queries": None, + "search_results": None, + "finish_reason": "MAX_TOKENS", + "tool_calls": None, + "chat_history": [ + {"role": "USER", "message": "Hello"}, + {"role": "CHATBOT", "message": "Hi! Hello there! How's it going?"}, + ], + "response_id": "7f2c0ab4-e0d0-4808-891e-d5c6362e407a", + "meta": { + "api_version": {"version": "1"}, + "billed_units": {"input_tokens": 1, "output_tokens": 10}, + "tokens": {"input_tokens": 67, "output_tokens": 10}, + }, + } + + yield event + + async def invoke_chat_stream( + self, chat_request: CohereChatRequest, ctx: Context, **kwargs: Any + ) -> Generator[StreamedChatResponse, None, None]: + for event in self.event_stream: + yield event + + async def invoke_rerank( + self, query: str, documents: list[str], ctx: Context, **kwargs: Any + ) -> Any: + return None + +# Overriding the name so that the proper deployment is selected +MockAzureDeployment.__name__ = "AzureDeployment" diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_base.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_base.py new file mode 100644 index 0000000000000000000000000000000000000000..4a49e960a168d305d673125e9aeea7df8d991f59 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_base.py @@ -0,0 +1,5 @@ +from backend.model_deployments.base import BaseDeployment + + +class MockDeployment(BaseDeployment): + event_stream = [] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_bedrock.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_bedrock.py new file mode 100644 index 0000000000000000000000000000000000000000..6473624ea323209662d0c2116d170f6a61570932 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_bedrock.py @@ -0,0 +1,79 @@ +from typing import Any, Generator + +from cohere.types import StreamedChatResponse + +from backend.schemas.cohere_chat import CohereChatRequest +from backend.schemas.context import Context +from backend.tests.unit.model_deployments.mock_deployments.mock_base import ( + MockDeployment, +) + + +class MockBedrockDeployment(MockDeployment): + """Bedrock Deployment""" + + DEFAULT_MODELS = ["cohere.command-r-plus-v1:0"] + + def __init__(self, **kwargs: Any): + pass + + @staticmethod + def name() -> str: + return "Bedrock" + + @staticmethod + def env_vars() -> list[str]: + return [] + + @staticmethod + def rerank_enabled() -> bool: + return False + + @classmethod + def list_models(cls) -> list[str]: + return cls.DEFAULT_MODELS + + @staticmethod + def is_available() -> bool: + return False + + async def invoke_chat( + self, chat_request: CohereChatRequest, **kwargs: Any + ) -> Generator[StreamedChatResponse, None, None]: + event = { + "text": "Hi! Hello there! How's it going?", + "generation_id": "ca0f398e-f8c8-48f0-b093-12d1754d00ed", + "citations": None, + "documents": None, + "is_search_required": None, + "search_queries": None, + "search_results": None, + "finish_reason": "MAX_TOKENS", + "tool_calls": None, + "chat_history": [ + {"role": "USER", "message": "Hello"}, + {"role": "CHATBOT", "message": "Hi! Hello there! How's it going?"}, + ], + "response_id": "7f2c0ab4-e0d0-4808-891e-d5c6362e407a", + "meta": { + "api_version": {"version": "1"}, + "billed_units": {"input_tokens": 1, "output_tokens": 10}, + "tokens": {"input_tokens": 67, "output_tokens": 10}, + }, + } + yield event + + async def invoke_chat_stream( + self, chat_request: CohereChatRequest, ctx: Context, **kwargs: Any + ) -> Generator[StreamedChatResponse, None, None]: + for event in self.event_stream: + yield event + + async def invoke_rerank( + self, query: str, documents: list[str], ctx: Context, **kwargs: Any + + ) -> Any: + return None + +# Overriding the name so that the proper deployment is selected +MockBedrockDeployment.__name__ = "BedrockDeployment" diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_cohere_platform.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_cohere_platform.py new file mode 100644 index 0000000000000000000000000000000000000000..499148ab82ecadae977cd4d5804e392ea844e0b5 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_cohere_platform.py @@ -0,0 +1,100 @@ +import random +from typing import Any, Generator + +from cohere.types import StreamedChatResponse + +from backend.schemas.cohere_chat import CohereChatRequest +from backend.schemas.context import Context +from backend.services.conversation import SEARCH_RELEVANCE_THRESHOLD +from backend.tests.unit.model_deployments.mock_deployments.mock_base import ( + MockDeployment, +) + + +class MockCohereDeployment(MockDeployment): + """Mocked Cohere Platform Deployment.""" + + DEFAULT_MODELS = ["command", "command-r"] + + def __init__(self, **kwargs: Any): + pass + + @staticmethod + def name() -> str: + return "Cohere Platform" + + @staticmethod + def env_vars() -> list[str]: + return ["COHERE_API_KEY"] + + @staticmethod + def rerank_enabled() -> bool: + return True + + @classmethod + def list_models(cls) -> list[str]: + return cls.DEFAULT_MODELS + + @staticmethod + def is_available() -> bool: + return True + + @classmethod + def config(cls) -> dict[str, Any]: + return {"COHERE_API_KEY": "fake-api-key"} + + + def invoke_chat( + self, chat_request: CohereChatRequest, ctx: Context, **kwargs: Any + ) -> Generator[StreamedChatResponse, None, None]: + event = { + "text": "Hi! Hello there! How's it going?", + "generation_id": "ca0f398e-f8c8-48f0-b093-12d1754d00ed", + "citations": None, + "documents": None, + "is_search_required": None, + "search_queries": None, + "search_results": None, + "finish_reason": "MAX_TOKENS", + "tool_calls": None, + "chat_history": [ + {"role": "USER", "message": "Hello"}, + {"role": "CHATBOT", "message": "Hi! Hello there! How's it going?"}, + ], + "response_id": "7f2c0ab4-e0d0-4808-891e-d5c6362e407a", + "meta": { + "api_version": {"version": "1"}, + "billed_units": {"input_tokens": 1, "output_tokens": 10}, + "tokens": {"input_tokens": 67, "output_tokens": 10}, + }, + } + yield event + + async def invoke_chat_stream( + self, chat_request: CohereChatRequest, ctx: Context, **kwargs: Any + ) -> Generator[StreamedChatResponse, None, None]: + for event in self.event_stream: + yield event + + async def invoke_rerank( + self, query: str, documents: list[str], ctx: Context, **kwargs: Any + ) -> Any: + results = [] + for idx, doc in enumerate(documents): + if query in doc: + results.append({ + "index": idx, + "relevance_score": random.uniform(SEARCH_RELEVANCE_THRESHOLD, 1), + }) + event = { + "id": "eae2b023-bf49-4139-bf15-9825022762f4", + "results": results, + "meta": { + "api_version":{"version":"1"}, + "billed_units":{"search_units":1} + } + } + return event + +# Overriding the name so that the proper deployment is selected +MockCohereDeployment.__name__ = "CohereDeployment" diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_sagemaker.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_sagemaker.py new file mode 100644 index 0000000000000000000000000000000000000000..c678421cfd3f298a05cc235f99fa9a4e184f0358 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_sagemaker.py @@ -0,0 +1,58 @@ +from typing import Any, Generator + +from cohere.types import StreamedChatResponse + +from backend.schemas.cohere_chat import CohereChatRequest +from backend.schemas.context import Context +from backend.tests.unit.model_deployments.mock_deployments.mock_base import ( + MockDeployment, +) + + +class MockSageMakerDeployment(MockDeployment): + """SageMaker Deployment""" + + DEFAULT_MODELS = ["command-r"] + + def __init__(self, **kwargs: Any): + pass + + @staticmethod + def name() -> str: + return "SageMaker" + + @staticmethod + def env_vars() -> list[str]: + return [] + + @staticmethod + def rerank_enabled() -> bool: + return False + + @classmethod + def list_models(cls) -> list[str]: + return cls.DEFAULT_MODELS + + @staticmethod + def is_available() -> bool: + return False + + async def invoke_chat( + self, chat_request: CohereChatRequest, **kwargs: Any + ) -> Generator[StreamedChatResponse, None, None]: + pass + + async def invoke_chat_stream( + self, chat_request: CohereChatRequest, ctx: Context, **kwargs: Any + ) -> Generator[StreamedChatResponse, None, None]: + for event in self.event_stream: + yield event + + async def invoke_rerank( + self, query: str, documents: list[str], ctx: Context, **kwargs: Any + + ) -> Any: + return None + +# Overriding the name so that the proper deployment is selected +MockSageMakerDeployment.__name__ = "SageMakerDeployment" diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_single_container.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_single_container.py new file mode 100644 index 0000000000000000000000000000000000000000..c2c8a9005754c54268adce715242f9095726bc01 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/mock_deployments/mock_single_container.py @@ -0,0 +1,79 @@ +from typing import Any, Generator + +from cohere.types import StreamedChatResponse + +from backend.schemas.cohere_chat import CohereChatRequest +from backend.schemas.context import Context +from backend.tests.unit.model_deployments.mock_deployments.mock_base import ( + MockDeployment, +) + + +class MockSingleContainerDeployment(MockDeployment): + """Mocked Single Container Deployment.""" + + DEFAULT_MODELS = ["command-r"] + + def __init__(self, **kwargs: Any): + pass + + @staticmethod + def name() -> str: + return "Single Container" + + @staticmethod + def env_vars() -> list[str]: + return [] + + @staticmethod + def rerank_enabled() -> bool: + return False + + @classmethod + def list_models(cls) -> list[str]: + return cls.DEFAULT_MODELS + + @staticmethod + def is_available() -> bool: + return False + + async def invoke_chat( + self, chat_request: CohereChatRequest, **kwargs: Any + ) -> Generator[StreamedChatResponse, None, None]: + event = { + "text": "Hi! Hello there! How's it going?", + "generation_id": "ca0f398e-f8c8-48f0-b093-12d1754d00ed", + "citations": None, + "documents": None, + "is_search_required": None, + "search_queries": None, + "search_results": None, + "finish_reason": "MAX_TOKENS", + "tool_calls": None, + "chat_history": [ + {"role": "USER", "message": "Hello"}, + {"role": "CHATBOT", "message": "Hi! Hello there! How's it going?"}, + ], + "response_id": "7f2c0ab4-e0d0-4808-891e-d5c6362e407a", + "meta": { + "api_version": {"version": "1"}, + "billed_units": {"input_tokens": 1, "output_tokens": 10}, + "tokens": {"input_tokens": 67, "output_tokens": 10}, + }, + } + yield event + + async def invoke_chat_stream( + self, chat_request: CohereChatRequest, ctx: Context, **kwargs: Any + ) -> Generator[StreamedChatResponse, None, None]: + for event in self.event_stream: + yield event + + async def invoke_rerank( + self, query: str, documents: list[str], ctx: Context, **kwargs: Any + ) -> Any: + # TODO: Add + pass + +# Overriding the name so that the proper deployment is selected +MockSingleContainerDeployment.__name__ = "SingleContainerDeployment" diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/test_azure.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/test_azure.py new file mode 100644 index 0000000000000000000000000000000000000000..7315d3b3af3bdb7fbc9bc9d0b317b82cbc2d03af --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/test_azure.py @@ -0,0 +1,44 @@ +from fastapi.testclient import TestClient + +from backend.database_models.user import User +from backend.model_deployments.azure import AzureDeployment +from backend.tests.unit.model_deployments.mock_deployments import MockAzureDeployment + + +def test_streamed_chat( + session_client_chat: TestClient, + user: User, + mock_azure_deployment, + mock_available_model_deployments, +): + deployment = mock_azure_deployment.return_value + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": user.id, + "Deployment-Name": AzureDeployment.name(), + }, + json={"message": "Hello", "max_tokens": 10}, + ) + + assert response.status_code == 200 + assert type(deployment) is MockAzureDeployment + + +def test_non_streamed_chat( + session_client_chat: TestClient, + user: User, + mock_azure_deployment, +): + deployment = mock_azure_deployment.return_value + response = session_client_chat.post( + "/v1/chat", + headers={ + "User-Id": user.id, + "Deployment-Name": AzureDeployment.name(), + }, + json={"message": "Hello", "max_tokens": 10}, + ) + + assert response.status_code == 200 + assert type(deployment) is MockAzureDeployment diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/test_bedrock.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/test_bedrock.py new file mode 100644 index 0000000000000000000000000000000000000000..cf6c0bcc663e3dc1f8b0d01dbaf6dc0dde2c34ae --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/test_bedrock.py @@ -0,0 +1,40 @@ +from fastapi.testclient import TestClient + +from backend.database_models.user import User +from backend.model_deployments.bedrock import BedrockDeployment +from backend.tests.unit.model_deployments.mock_deployments import MockBedrockDeployment + + +def test_streamed_chat( + session_client_chat: TestClient, + user: User, + mock_bedrock_deployment, + mock_available_model_deployments, +): + deployment = mock_bedrock_deployment.return_value + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": user.id, + "Deployment-Name": BedrockDeployment.name(), + }, + json={"message": "Hello", "max_tokens": 10}, + ) + + assert response.status_code == 200 + assert type(deployment) is MockBedrockDeployment + + +def test_non_streamed_chat( + session_client_chat: TestClient, + user: User, + mock_bedrock_deployment, +): + mock_bedrock_deployment.return_value + response = session_client_chat.post( + "/v1/chat", + headers={"User-Id": user.id, "Deployment-Name": BedrockDeployment.name(),}, + json={"message": "Hello", "max_tokens": 10}, + ) + + assert response.status_code == 200 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/test_cohere_platform.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/test_cohere_platform.py new file mode 100644 index 0000000000000000000000000000000000000000..026c76de4778b7a78c088605843b6822d86f1c58 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/test_cohere_platform.py @@ -0,0 +1,46 @@ +import pytest +from fastapi.testclient import TestClient + +from backend.database_models.user import User +from backend.model_deployments.cohere_platform import CohereDeployment +from backend.tests.unit.model_deployments.mock_deployments import MockCohereDeployment + +pytest.skip("These tests are already covered by tests in integration/routers/test_chat.py and are breaking other unit tests. They should be converted to smaller-scoped unit tests.", allow_module_level=True) + +def test_streamed_chat( + session_client_chat: TestClient, + user: User, + mock_cohere_deployment, + mock_available_model_deployments, +): + deployment = mock_cohere_deployment.return_value + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + json={"message": "Hello", "max_tokens": 10}, + ) + + assert response.status_code == 200 + assert type(deployment) is MockCohereDeployment + + +def test_non_streamed_chat( + session_client_chat: TestClient, + user: User, + mock_cohere_deployment, +): + deployment = mock_cohere_deployment.return_value + response = session_client_chat.post( + "/v1/chat", + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + json={"message": "Hello", "max_tokens": 10}, + ) + + assert response.status_code == 200 + assert type(deployment) is MockCohereDeployment diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/test_sagemaker.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/test_sagemaker.py new file mode 100644 index 0000000000000000000000000000000000000000..f472efe810a023a07bcf7f3f7cd730f59a177e5e --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/test_sagemaker.py @@ -0,0 +1,39 @@ +import pytest +from fastapi.testclient import TestClient + +from backend.database_models.user import User +from backend.model_deployments.sagemaker import SageMakerDeployment +from backend.tests.unit.model_deployments.mock_deployments import ( + MockSageMakerDeployment, +) + + +def test_streamed_chat( + session_client_chat: TestClient, + user: User, + mock_sagemaker_deployment, + mock_available_model_deployments, +): + deployment = mock_sagemaker_deployment.return_value + response = session_client_chat.post( + "/v1/chat-stream", + headers={"User-Id": user.id, "Deployment-Name": SageMakerDeployment.name()}, + json={"message": "Hello", "max_tokens": 10}, + ) + + assert response.status_code == 200 + assert type(deployment) is MockSageMakerDeployment + + +@pytest.mark.skip("Non-streamed chat is not supported for SageMaker yet") +def test_non_streamed_chat( + session_client_chat: TestClient, user: User, mock_sagemaker_deployment +): + mock_sagemaker_deployment.return_value + response = session_client_chat.post( + "/v1/chat", + headers={"User-Id": user.id, "Deployment-Name": SageMakerDeployment.name()}, + json={"message": "Hello", "max_tokens": 10}, + ) + + assert response.status_code == 200 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/test_single_container.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/test_single_container.py new file mode 100644 index 0000000000000000000000000000000000000000..5d6bef8464123d306c281b58cb7938a665f76df8 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/model_deployments/test_single_container.py @@ -0,0 +1,44 @@ +from fastapi.testclient import TestClient + +from backend.database_models.user import User +from backend.model_deployments.single_container import SingleContainerDeployment +from backend.tests.unit.model_deployments.mock_deployments import ( + MockSingleContainerDeployment, +) + + +def test_streamed_chat( + session_client_chat: TestClient, + user: User, + mock_single_container_deployment, + mock_available_model_deployments, +): + deployment = mock_single_container_deployment.return_value + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": user.id, + "Deployment-Name": SingleContainerDeployment.name(), + }, + json={"message": "Hello", "max_tokens": 10}, + ) + + assert response.status_code == 200 + assert type(deployment) is MockSingleContainerDeployment + + +def test_non_streamed_chat( + session_client_chat: TestClient, user: User, mock_single_container_deployment +): + deployment = mock_single_container_deployment.return_value + response = session_client_chat.post( + "/v1/chat", + headers={ + "User-Id": user.id, + "Deployment-Name": SingleContainerDeployment.name(), + }, + json={"message": "Hello", "max_tokens": 10}, + ) + + assert response.status_code == 200 + assert type(deployment) is MockSingleContainerDeployment diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/auth/conftest.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/auth/conftest.py new file mode 100644 index 0000000000000000000000000000000000000000..8dba56d4b8e2d48f97f085187be3b1448295168e --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/auth/conftest.py @@ -0,0 +1,40 @@ +from unittest.mock import patch + +import pytest + +from backend.config.auth import ENABLED_AUTH_STRATEGY_MAPPING + + +@pytest.fixture(autouse=True) +def mock_auth_secret_key_env(monkeypatch): + monkeypatch.setenv("AUTH_SECRET_KEY", "test") + + +@pytest.fixture(autouse=True) +def mock_google_env(monkeypatch): + monkeypatch.setenv("GOOGLE_CLIENT_ID", "test") + monkeypatch.setenv("GOOGLE_CLIENT_SECRET", "test") + monkeypatch.setenv("FRONTEND_HOSTNAME", "http://localhost:4000") + + +@pytest.fixture(autouse=True) +def mock_oidc_env(monkeypatch): + monkeypatch.setenv("OIDC_CLIENT_ID", "test") + monkeypatch.setenv("OIDC_CLIENT_SECRET", "test") + monkeypatch.setenv("OIDC_CONFIG_ENDPOINT", "test") + monkeypatch.setenv("OIDC_WELL_KNOWN_ENDPOINT", "test") + + +@pytest.fixture(autouse=True) +def mock_enabled_auth(mock_google_env, mock_oidc_env): + # Can directly use class since no external calls are made + from backend.services.auth import BasicAuthentication, GoogleOAuth, OpenIDConnect + + mocked_strategies = { + BasicAuthentication.NAME: BasicAuthentication(), + GoogleOAuth.NAME: GoogleOAuth(), + OpenIDConnect.NAME: OpenIDConnect(), + } + + with patch.dict(ENABLED_AUTH_STRATEGY_MAPPING, mocked_strategies) as mock: + yield mock diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/auth/test_auth.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/auth/test_auth.py new file mode 100644 index 0000000000000000000000000000000000000000..d0c39f85d3ae406eedf8b8fec1b028f9f2a01301 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/auth/test_auth.py @@ -0,0 +1,13 @@ +from fastapi.testclient import TestClient + + +def test_list_auth_strategies(session_client: TestClient) -> None: + response = session_client.get("/v1/auth_strategies") + + assert response.status_code == 200 + strategies = response.json() + + fields = ["strategy", "client_id", "authorization_endpoint", "pkce_enabled"] + for strategy in strategies: + for field in fields: + assert field in strategy diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/auth/test_authorization_header.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/auth/test_authorization_header.py new file mode 100644 index 0000000000000000000000000000000000000000..55f8e8fb343dca579f1cd4e7244077ef6d04a109 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/auth/test_authorization_header.py @@ -0,0 +1,89 @@ +import freezegun +import pytest +from fastapi import HTTPException +from fastapi.testclient import TestClient +from sqlalchemy.orm import Session + +from backend.services.auth.jwt import JWTService +from backend.services.auth.request_validators import validate_authorization +from backend.tests.unit.factories import get_factory + +# Weird issue with freezegun, see: https://stackoverflow.com/questions/73007409/freezeguns-freeze-time-throws-odd-transformers-error-when-used +freezegun.configure(extend_ignore_list=["transformers"]) + + +def test_validate_authorization_valid_token( + session_client: TestClient, +): + user = {"user_id": "test"} + token = JWTService().create_and_encode_jwt(user) + + # Use /logout endpoint to test request validator + response = session_client.get( + "/v1/logout", headers={"Authorization": f"Bearer {token}"} + ) + + assert response.status_code == 200 + + +def test_validate_authorization_no_authorization(session: Session): + with pytest.raises(HTTPException) as exc: + _ = validate_authorization(session, "") + + exception = exc.value + assert exception.status_code == 401 + assert ( + exception.detail == "Authorization: Bearer required in request headers." + ) + + +def test_validate_authorization_no_bearer(session: Session): + with pytest.raises(HTTPException) as exc: + _ = validate_authorization(session, "test invalid_token") + + exception = exc.value + assert exception.status_code == 401 + assert ( + exception.detail == "Authorization: Bearer required in request headers." + ) + + +def test_validate_authorization_invalid_token(session: Session): + with pytest.raises(HTTPException) as exc: + _ = validate_authorization(session, "Bearer invalid_token") + + exception = exc.value + assert exception.status_code == 401 + assert exception.detail == "Bearer token is invalid or expired." + + +def test_validate_authorization_expired_token(session: Session): + user = {"user_id": "test"} + with freezegun.freeze_time("2023-01-01 00:00:00"): + token = JWTService().create_and_encode_jwt(user) + + with freezegun.freeze_time("2024-05-01 00:00:00"): + with pytest.raises(HTTPException) as exc: + _ = validate_authorization(session, f"Bearer {token}") + + exception = exc.value + assert exception.status_code == 401 + assert exception.detail == "Bearer token is invalid or expired." + + +def test_validate_authorization_blacklisted_token( + session_client: TestClient, session: Session +): + user = {"user_id": "test"} + token = JWTService().create_and_encode_jwt(user) + decoded = JWTService().decode_jwt(token) + + # Create a Blacklist entry + _ = get_factory("Blacklist", session).create(token_id=decoded["jti"]) + + response = session_client.get( + "/v1/logout", headers={"Authorization": f"Bearer {token}"} + ) + + assert response.status_code == 401 + assert response.json() == {"detail": "Bearer token is blacklisted."} diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/auth/test_basic_auth.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/auth/test_basic_auth.py new file mode 100644 index 0000000000000000000000000000000000000000..106051496c3840bdbe1112027ec689a279c60269 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/auth/test_basic_auth.py @@ -0,0 +1,100 @@ +from fastapi.testclient import TestClient +from sqlalchemy.orm import Session + +from backend.database_models import Blacklist +from backend.services.auth.jwt import JWTService +from backend.tests.unit.factories import get_factory + + +def test_login_success(session_client: TestClient, session: Session): + _ = get_factory("User", session).create(email="test@gmail.com", password="abcd") + + response = session_client.post( + "/v1/login", + json={ + "strategy": "Basic", + "payload": {"email": "test@gmail.com", "password": "abcd"}, + }, + ) + + assert response.status_code == 200 + assert response.json().get("token") is not None + + +def test_login_invalid_password(session_client: TestClient, session: Session): + _ = get_factory("User", session).create(email="test@gmail.com", password="hello") + response = session_client.post( + "/v1/login", + json={ + "strategy": "Basic", + "payload": {"email": "test@gmail.com", "password": "test"}, + }, + ) + + assert response.status_code == 401 + assert response.json() == { + "detail": "Error performing Basic authentication with payload: {'email': 'test@gmail.com', 'password': 'test'}." + } + + +def test_login_no_user(session_client: TestClient): + response = session_client.post( + "/v1/login", + json={ + "strategy": "Basic", + "payload": {"email": "nouser@gmail.com", "password": "test"}, + }, + ) + + assert response.status_code == 401 + assert response.json() == { + "detail": "Error performing Basic authentication with payload: {'email': 'nouser@gmail.com', 'password': 'test'}." + } + + +def test_login_invalid_strategy(session_client: TestClient): + response = session_client.post( + "/v1/login", json={"strategy": "test", "payload": {}} + ) + + assert response.status_code == 422 + assert response.json() == {"detail": "Invalid Authentication strategy: test."} + + +def test_login_invalid_payload(session_client: TestClient): + response = session_client.post( + "/v1/login", json={"strategy": "Basic", "payload": {}} + ) + + assert response.status_code == 422 + assert response.json() == { + "detail": "Missing the following keys in the payload: ['email', 'password']." + } + + +def test_login_no_strategy(session_client: TestClient): + response = session_client.post("/v1/login", json={"payload": {}}) + + assert response.status_code == 422 + + +def test_login_no_payload(session_client: TestClient): + response = session_client.post("/v1/login", json={"strategy": ""}) + + assert response.status_code == 422 + + +def test_logout_success(session_client: TestClient, session: Session): + user = {"user_id": "test"} + token = JWTService().create_and_encode_jwt(user) + decoded = JWTService().decode_jwt(token) + + response = session_client.get( + "/v1/logout", headers={"Authorization": f"Bearer {token}"} + ) + assert response.status_code == 200 + # Adds a Blacklist entry + blacklist = ( + session.query(Blacklist).filter(Blacklist.token_id == decoded["jti"]).first() + ) + assert blacklist is not None diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/conftest.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/conftest.py new file mode 100644 index 0000000000000000000000000000000000000000..2d788e23e0d76710d33110ea0cd425f6b6e917fd --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/conftest.py @@ -0,0 +1,17 @@ +import pytest +from fastapi.testclient import TestClient + +from backend.main import app + + +@pytest.fixture +def error_client(): + """ + Fixture used to test global error handler + """ + + @app.get("/error") + def test_error(): + raise Exception("Test exception") + + yield TestClient(app, raise_server_exceptions=False) diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_chat.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_chat.py new file mode 100644 index 0000000000000000000000000000000000000000..a278ce9fefadf989150737a75e4a1c8248ea1f68 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_chat.py @@ -0,0 +1,1143 @@ +import json +import uuid +from typing import Any + +import pytest +from fastapi.testclient import TestClient +from sqlalchemy.orm import Session + +from backend.chat.enums import StreamEvent +from backend.database_models.conversation import Conversation +from backend.database_models.message import Message, MessageAgent +from backend.database_models.user import User +from backend.model_deployments.cohere_platform import CohereDeployment +from backend.schemas.tool import ToolCategory +from backend.tests.unit.factories import get_factory +from backend.tests.unit.model_deployments.mock_deployments.mock_cohere_platform import ( + MockCohereDeployment, +) + + +@pytest.fixture(scope="session") +def user(session_chat: Session) -> User: + return get_factory("User", session_chat).create() + + +# STREAMING CHAT TESTS +def test_streaming_new_chat( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": user.id, + "Deployment-Name": MockCohereDeployment.name(), + }, + json={"message": "Hello", "max_tokens": 10}, + ) + + assert response.status_code == 200 + validate_chat_streaming_response( + response, user, session_chat, session_client_chat, 2 + ) + + +def test_streaming_new_chat_with_agent( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + deployment = get_factory("Deployment", session_chat).create() + model = get_factory("Model", session_chat).create(deployment=deployment) + agent = get_factory("Agent", session_chat).create(user=user, tools=[], deployment_id=deployment.id, + model_id=model.id) + + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": agent.user.id, + "Deployment-Name": agent.deployment, + }, + params={"agent_id": agent.id}, + json={"message": "Hello", "max_tokens": 10, "agent_id": agent.id}, + ) + assert response.status_code == 200 + validate_chat_streaming_response( + response, agent.user, session_chat, session_client_chat, 2 + ) + + +def test_streaming_new_chat_with_agent_existing_conversation( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + deployment = get_factory("Deployment", session_chat).create() + model = get_factory("Model", session_chat).create(deployment=deployment) + agent = get_factory("Agent", session_chat).create(user=user, tools=[], deployment_id=deployment.id, + model_id=model.id) + + agent.preamble = "you are a smart assistant" + session_chat.refresh(agent) + + conversation = get_factory("Conversation", session_chat).create( + user_id=agent.user.id, agent_id=agent.id + ) + _ = get_factory("Message", session_chat).create( + conversation_id=conversation.id, + user_id=agent.user.id, + agent="USER", + text="Hello", + position=1, + is_active=True, + ) + + _ = get_factory("Message", session_chat).create( + conversation_id=conversation.id, + user_id=agent.user.id, + agent="CHATBOT", + text="Hi", + position=2, + is_active=True, + ) + + session_chat.refresh(conversation) + + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": agent.user.id, + "Deployment-Name": agent.deployment, + }, + params={"agent_id": agent.id}, + json={"message": "Hello", "max_tokens": 10, "conversation_id": conversation.id, "agent_id": agent.id}, + ) + + assert response.status_code == 200 + validate_chat_streaming_response( + response, agent.user, session_chat, session_client_chat, 4 + ) + + +def test_streaming_chat_with_existing_conversation_from_other_agent( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + agent = get_factory("Agent", session_chat).create(user=user) + _ = get_factory("Agent", session_chat).create(user=user, id="123") + conversation = get_factory("Conversation", session_chat).create( + user_id=user.id, agent_id="123" + ) + _ = get_factory("Message", session_chat).create( + conversation_id=conversation.id, + user_id=user.id, + agent="USER", + text="Hello", + position=1, + is_active=True, + ) + + _ = get_factory("Message", session_chat).create( + conversation_id=conversation.id, + user_id=user.id, + agent="CHATBOT", + text="Hi", + position=2, + is_active=True, + ) + + session_chat.refresh(conversation) + + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + params={"agent_id": agent.id}, + json={"message": "Hello", "max_tokens": 10, "conversation_id": conversation.id, "agent_id": agent.id}, + ) + + assert response.status_code == 404 + assert response.json() == { + "detail": f"Conversation ID {conversation.id} not found for specified agent." + } + +_TOOL_EVENTS = [{ + "event_type": StreamEvent.TOOL_CALLS_GENERATION, + "text": "", + "tool_calls": [ + { "name": "tavily_web_search", "parameters": {} }, + ], +}] + +@pytest.mark.parametrize("inject_events", [_TOOL_EVENTS]) +def test_streaming_chat_with_tools_not_in_agent_tools( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + deployment = get_factory("Deployment", session_chat).create() + model = get_factory("Model", session_chat).create(deployment=deployment) + agent = get_factory("Agent", session_chat).create(user=user, tools=["wikipedia"], deployment_id=deployment.id, + model_id=model.id) + + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": agent.user.id, + "Deployment-Name": agent.deployment, + }, + json={ + "message": "Who is a tallest nba player", + "tools": [{"name": "tavily_web_search"}], + "agent_id": agent.id, + }, + ) + + assert response.status_code == 200 + validate_chat_streaming_tool_cals_response(response, ["tavily_web_search"]) + + +_TOOL_EVENTS = [{ + "event_type": StreamEvent.TOOL_CALLS_GENERATION, + "text": "", + "tool_calls": [ + { "name": "tavily_web_search", "parameters": {} }, + ], +}] + +@pytest.mark.parametrize("inject_events", [_TOOL_EVENTS]) +def test_streaming_chat_with_agent_tools_and_empty_request_tools( + session_client_chat: TestClient, + session_chat: + Session, user: User, + mock_available_model_deployments: list[dict], +) -> None: + deployment = get_factory("Deployment", session_chat).create() + model = get_factory("Model", session_chat).create(deployment=deployment) + agent = get_factory("Agent", session_chat).create(user=user, tools=["tavily_web_search"], + deployment_id=deployment.id, model_id=model.id) + + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": agent.user.id, + "Deployment-Name": CohereDeployment.name(), + }, + json={ + "message": "Who is a tallest NBA player", + "tools": [], + "agent_id": agent.id, + }, + ) + + assert response.status_code == 200 + validate_chat_streaming_tool_cals_response(response, ["tavily_web_search"]) + + +def test_streaming_existing_chat( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + conversation = get_factory("Conversation", session_chat).create(user_id=user.id) + + _ = get_factory("Message", session_chat).create( + conversation_id=conversation.id, + user_id=user.id, + agent="USER", + text="Hello", + position=1, + is_active=True, + ) + + _ = get_factory("Message", session_chat).create( + conversation_id=conversation.id, + user_id=user.id, + agent="CHATBOT", + text="Hi", + position=2, + is_active=True, + ) + + session_chat.refresh(conversation) + + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + json={ + "message": "How are you doing?", + "conversation_id": conversation.id, + "max_tokens": 10, + }, + ) + + assert response.status_code == 200 + validate_chat_streaming_response( + response, user, session_chat, session_client_chat, 4 + ) + + +def test_fail_chat_missing_user_id( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + response = session_client_chat.post( + "/v1/chat", + json={"message": "Hello"}, + headers={"Deployment-Name": CohereDeployment.name()}, + ) + + assert response.status_code == 401 + assert response.json() == { "detail": "User-Id required in request headers." } + + +def test_default_chat_missing_deployment_name( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + response = session_client_chat.post( + "/v1/chat", + json={"message": "Hello"}, + headers={"User-Id": user.id}, + ) + + assert response.status_code == 200 + + +def test_streaming_fail_chat_missing_message( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + json={}, + ) + + assert response.status_code == 422 + assert response.json() == { + "detail": [ + { + "type": "missing", + "loc": ["body", "message"], + "msg": "Field required", + "input": {}, + } + ] + } + + +def test_streaming_chat_with_managed_tools( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + tools = session_client_chat.get("/v1/tools", headers={"User-Id": user.id}).json() + assert len(tools) > 0 + tool = [t for t in tools if t["is_visible"] and t["category"] != ToolCategory.Function][ + 0 + ].get("name") + + response = session_client_chat.post( + "/v1/chat-stream", + json={"message": "Hello", "tools": [{"name": tool}]}, + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + ) + + assert response.status_code == 200 + validate_chat_streaming_response( + response, user, session_chat, session_client_chat, 2 + ) + + +def test_streaming_chat_with_invalid_tool( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + response = session_client_chat.post( + "/v1/chat-stream", + json={"message": "Hello", "tools": [{"name": "invalid_tool"}]}, + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + ) + + assert response.status_code == 400 + assert response.json() == {"detail": "Custom tools must have a description"} + + +def test_streaming_chat_with_managed_and_custom_tools( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + tools = session_client_chat.get("/v1/tools", headers={"User-Id": user.id}).json() + assert len(tools) > 0 + tool = [t for t in tools if t["is_visible"] and t["category"] != ToolCategory.Function][ + 0 + ].get("name") + + response = session_client_chat.post( + "/v1/chat-stream", + json={ + "message": "Hello", + "tools": [ + {"name": tool}, + { + "name": "random_number_generator", + "description": "Generate a random number", + }, + ], + }, + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + ) + + assert response.status_code == 400 + assert response.json() == {"detail": "Cannot mix both managed and custom tools"} + + +_SEARCH_EVENTS = [{ + "event_type": StreamEvent.SEARCH_QUERIES_GENERATION, + "text": "", + "search_queries": [ + { "text": "", "generation_id": "" }, + ], +}] + +@pytest.mark.parametrize("inject_events", [_SEARCH_EVENTS]) +def test_streaming_chat_with_search_queries_only( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + response = session_client_chat.post( + "/v1/chat-stream", + json={ + "message": "What is the capital of Ontario?", + "search_queries_only": True, + }, + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + ) + + assert response.status_code == 200 + validate_chat_streaming_response( + response, + user, + session_chat, + session_client_chat, + 2, + is_search_queries_only=True, + ) + + +def test_streaming_chat_with_chat_history( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + response = session_client_chat.post( + "/v1/chat-stream", + json={ + "message": "Hello", + "chat_history": [ + {"role": "USER", "message": "Hello"}, + {"role": "CHATBOT", "message": "Hi"}, + ], + "max_tokens": 10, + }, + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + ) + + assert response.status_code == 200 + validate_chat_streaming_response( + response, + user, + session_chat, + session_client_chat, + 0, + ) + + +def test_streaming_existing_chat_with_files_attaches_to_user_message( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + conversation = get_factory("Conversation", session_chat).create(user_id=user.id) + file1 = get_factory("File", session_chat).create(user_id=user.id) + file2 = get_factory("File", session_chat).create(user_id=user.id) + session_chat.refresh(conversation) + + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + json={ + "message": "How are you doing?", + "conversation_id": conversation.id, + "file_ids": [file1.id, file2.id], + "max_tokens": 10, + }, + ) + + conversation = session_chat.get(Conversation, (conversation.id, user.id)) + assert response.status_code == 200 + assert conversation is not None + message = conversation.messages[0] + assert message is not None + assert message.agent == MessageAgent.USER + assert file1.id in message.file_ids + assert file2.id in message.file_ids + validate_chat_streaming_response( + response, user, session_chat, session_client_chat, 2 + ) + + +def test_streaming_existing_chat_with_attached_files_does_not_attach( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + file1 = get_factory("File", session_chat).create( + user_id=user.id, + ) + file2 = get_factory("File", session_chat).create( + user_id=user.id, + ) + conversation = get_factory("Conversation", session_chat).create(user_id=user.id) + existing_message = get_factory("Message", session_chat).create( + conversation_id=conversation.id, + user_id=user.id, + position=0, + is_active=True, + ) + + # Create conversation,message<>file relations + for file in [file1, file2]: + _ = get_factory("ConversationFileAssociation", session_chat).create( + conversation_id=conversation.id, user_id=user.id, file_id=file.id + ) + _ = get_factory("MessageFileAssociation", session_chat).create( + message_id=existing_message.id, user_id=user.id, file_id=file.id + ) + + session_chat.refresh(conversation) + + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + json={ + "message": "How are you doing?", + "conversation_id": conversation.id, + "file_ids": [file1.id, file2.id], + "max_tokens": 10, + }, + ) + + conversation = session_chat.get(Conversation, (conversation.id, user.id)) + + assert response.status_code == 200 + assert conversation is not None + + # Existing message has file IDs + message = session_chat.query(Message).filter_by(id=existing_message.id).first() + assert file1.id in message.file_ids + assert file2.id in message.file_ids + validate_chat_streaming_response( + response, user, session_chat, session_client_chat, 3 + ) + + +def test_streaming_chat_private_agent( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + agent = get_factory("Agent", session_chat).create( + user=user, is_private=True, tools=[] + ) + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + params={"agent_id": agent.id}, + json={"message": "Hello", "max_tokens": 10, "agent_id": agent.id}, + ) + + assert response.status_code == 200 + validate_chat_streaming_response( + response, user, session_chat, session_client_chat, 2 + ) + + +def test_streaming_chat_public_agent( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + agent = get_factory("Agent", session_chat).create( + user_id=user.id, is_private=False, tools=[] + ) + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + params={"agent_id": agent.id}, + json={"message": "Hello", "max_tokens": 10, "agent_id": agent.id}, + ) + + assert response.status_code == 200 + validate_chat_streaming_response( + response, user, session_chat, session_client_chat, 2 + ) + + +def test_streaming_chat_private_agent_by_another_user( + session_client_chat: TestClient, session_chat: Session, user: User +): + agent = get_factory("Agent", session_chat).create( + user_id=user.id, is_private=True, tools=[] + ) + other_user = get_factory("User", session_chat).create() + response = session_client_chat.post( + "/v1/chat-stream", + headers={ + "User-Id": other_user.id, + "Deployment-Name": CohereDeployment.name(), + }, + params={"agent_id": agent.id}, + json={"message": "Hello", "max_tokens": 10, "agent_id": agent.id}, + ) + + assert response.status_code == 404 + assert response.json() == {"detail": f"Agent with ID {agent.id} not found."} + + +def test_stream_regenerate_existing_chat( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + conversation = get_factory("Conversation", session_chat).create(user_id=user.id) + + _ = get_factory("Message", session_chat).create( + conversation_id=conversation.id, + user_id=user.id, + agent="USER", + text="Hello", + position=1, + is_active=True, + ) + + _ = get_factory("Message", session_chat).create( + conversation_id=conversation.id, + user_id=user.id, + agent="CHATBOT", + text="Hi", + position=1, + is_active=True, + ) + + session_chat.refresh(conversation) + + response = session_client_chat.post( + "/v1/chat-stream/regenerate", + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + json={ + "message": "", + "conversation_id": conversation.id, + "max_tokens": 10, + }, + ) + + assert response.status_code == 200 + validate_chat_streaming_response( + response, user, session_chat, session_client_chat, 2 + ) + + +def test_stream_regenerate_not_existing_chat( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + conversation_id = "test_conversation_id" + + response = session_client_chat.post( + "/v1/chat-stream/regenerate", + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + json={ + "message": "", + "conversation_id": conversation_id, + "max_tokens": 10, + }, + ) + + assert response.status_code == 404 + assert response.json() == {"detail": f"Conversation with ID: {conversation_id} not found."} + + +def test_stream_regenerate_existing_chat_not_existing_user_messages( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + conversation = get_factory("Conversation", session_chat).create(user_id=user.id) + + session_chat.refresh(conversation) + + response = session_client_chat.post( + "/v1/chat-stream/regenerate", + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + json={ + "message": "", + "conversation_id": conversation.id, + "max_tokens": 10, + }, + ) + + assert response.status_code == 404 + assert response.json() == {"detail": f"Messages for user with ID: {user.id} not found."} + + +# NON-STREAMING CHAT TESTS +def test_non_streaming_chat( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + response = session_client_chat.post( + "/v1/chat", + json={"message": "Hello", "max_tokens": 10}, + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + ) + + assert response.status_code == 200 + conversation_id = response.json()["conversation_id"] + + validate_conversation(session_chat, user, conversation_id, 2) + + +def test_non_streaming_chat_with_managed_tools( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + tools = session_client_chat.get("/v1/tools", headers={"User-Id": user.id}).json() + assert len(tools) > 0 + tool = [t for t in tools if t["is_visible"] and t["category"] != ToolCategory.Function][ + 0 + ].get("name") + + response = session_client_chat.post( + "/v1/chat", + json={"message": "Hello", "tools": [{"name": tool}]}, + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + ) + + assert response.status_code == 200 + conversation_id = response.json()["conversation_id"] + + validate_conversation(session_chat, user, conversation_id, 2) + + +def test_non_streaming_chat_with_managed_and_custom_tools( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + tools = session_client_chat.get("/v1/tools", headers={"User-Id": user.id}).json() + assert len(tools) > 0 + tool = [t for t in tools if t["is_visible"] and t["category"] != ToolCategory.Function][ + 0 + ].get("name") + + response = session_client_chat.post( + "/v1/chat", + json={ + "message": "Hello", + "tools": [ + {"name": tool}, + { + "name": "random_number_generator", + "description": "Generate a random number", + }, + ], + }, + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + ) + + assert response.status_code == 400 + assert response.json() == {"detail": "Cannot mix both managed and custom tools"} + + +def test_non_streaming_chat_with_search_queries_only( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + response = session_client_chat.post( + "/v1/chat", + json={ + "message": "What is the capital of Ontario?", + "search_queries_only": True, + }, + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + ) + + assert response.status_code == 200 + conversation_id = response.json()["conversation_id"] + + validate_conversation(session_chat, user, conversation_id, 2) + + +def test_non_streaming_chat_with_chat_history( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + response = session_client_chat.post( + "/v1/chat", + json={ + "message": "Hello", + "chat_history": [ + {"role": "USER", "message": "Hello"}, + {"role": "CHATBOT", "message": "Hi"}, + ], + "max_tokens": 10, + }, + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + ) + + assert response.status_code == 200 + conversation_id = response.json()["conversation_id"] + validate_conversation(session_chat, user, conversation_id, 0) + + +def test_non_streaming_existing_chat_with_files_attaches_to_user_message( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + conversation = get_factory("Conversation", session_chat).create(user_id=user.id) + file1 = get_factory("File", session_chat).create(user_id=user.id) + file2 = get_factory("File", session_chat).create(user_id=user.id) + + session_chat.refresh(conversation) + + response = session_client_chat.post( + "/v1/chat", + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + json={ + "message": "How are you doing?", + "conversation_id": conversation.id, + "file_ids": [file1.id, file2.id], + "max_tokens": 10, + }, + ) + + conversation = session_chat.get(Conversation, (conversation.id, user.id)) + + assert response.status_code == 200 + assert conversation is not None + # Files now linked to same user message + message = conversation.messages[0] + assert message.agent == MessageAgent.USER + assert file1.id in message.file_ids + assert file2.id in message.file_ids + + +def test_non_streaming_existing_chat_with_attached_files_does_not_attach( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + conversation = get_factory("Conversation", session_chat).create(user_id=user.id) + existing_message = get_factory("Message", session_chat).create( + conversation_id=conversation.id, user_id=user.id, position=0, is_active=True + ) + file1 = get_factory("File", session_chat).create(user_id=user.id) + file2 = get_factory("File", session_chat).create(user_id=user.id) + + # Create conversation,message<>file relations + for file in [file1, file2]: + _ = get_factory("ConversationFileAssociation", session_chat).create( + conversation_id=conversation.id, user_id=user.id, file_id=file.id + ) + _ = get_factory("MessageFileAssociation", session_chat).create( + message_id=existing_message.id, user_id=user.id, file_id=file.id + ) + + session_chat.refresh(conversation) + + response = session_client_chat.post( + "/v1/chat", + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + json={ + "message": "How are you doing?", + "conversation_id": conversation.id, + "file_ids": [file1.id, file2.id], + "max_tokens": 10, + }, + ) + + conversation = session_chat.get(Conversation, (conversation.id, user.id)) + + assert response.status_code == 200 + assert conversation is not None + # Existing message has file IDs + message = session_chat.query(Message).filter_by(id=existing_message.id).first() + assert file1.id in message.file_ids + assert file2.id in message.file_ids + + +def validate_chat_streaming_response( + response: Any, + user: User, + session: Session, + session_client_chat: TestClient, + expected_num_messages: int, + has_citations: bool = False, + is_search_queries_only: bool = False, + is_custom_tools: bool = False, +) -> None: + conversation_id = None + event_types = set() + + for line in response.iter_lines(): + if not line: + continue + + # remove the 'data' prefix to make it a valid JSON + line = line.replace("data: ", "") + response_json = json.loads(line) + assert response_json["event"] in [e.value for e in StreamEvent] + + event_types.add(response_json["event"]) + if response_json["event"] == StreamEvent.STREAM_END: + conversation_id = validate_stream_end_event( + response_json, is_search_queries_only, is_custom_tools + ) + + if has_citations: + assert StreamEvent.CITATION_GENERATION in event_types + + if is_search_queries_only: + assert StreamEvent.SEARCH_QUERIES_GENERATION in event_types + + if is_custom_tools: + assert StreamEvent.TOOL_CALLS_GENERATION in event_types + + # Check if the conversation was created correctly + validate_conversation(session, user, conversation_id, expected_num_messages) + + +def test_streaming_chat_with_files( + session_client_chat: TestClient, + session_chat: Session, + user: User, + mock_available_model_deployments: list[dict], +) -> None: + # Create convo + conversation = get_factory("Conversation", session_chat).create(user_id=user.id) + + # Upload the files + files = [ + ( + "files", + ( + "Mariana_Trench.pdf", + open("src/backend/tests/unit/test_data/Mariana_Trench.pdf", "rb"), + ), + ) + ] + + response = session_client_chat.post( + "/v1/conversations/batch_upload_file", + headers={"User-Id": conversation.user_id}, + files=files, + data={"conversation_id": conversation.id}, + ) + + assert response.status_code == 200 + file_id = response.json()[0]["id"] + + # Send the chat request + response = session_client_chat.post( + "/v1/chat", + json={ + "message": "Hello", + "max_tokens": 10, + "file_ids": [file_id], + "tools": [{"name": "search_file"}], + }, + headers={ + "User-Id": user.id, + "Deployment-Name": CohereDeployment.name(), + }, + ) + + assert response.status_code == 200 + + +def validate_conversation( + session: Session, user: User, conversation_id: str, expected_num_messages: int +) -> None: + conversation = session.query(Conversation).filter_by(id=conversation_id).first() + messages = session.query(Message).filter_by(conversation_id=conversation_id).all() + + if expected_num_messages == 0: + assert conversation is None + assert len(messages) == 0 + return + + assert conversation.id == conversation_id + assert conversation.user_id == user.id + assert len(conversation.messages) == expected_num_messages + # Also test DB object + conversation_db = session.get(Conversation, (conversation_id, user.id)) + assert conversation_db is not None + assert conversation_db.user_id == user.id + assert len(conversation_db.messages) == expected_num_messages + + +def validate_stream_end_event( + response_json: dict, is_search_queries_only: bool, is_custom_tools: bool +) -> str: + data = response_json["data"] + if is_search_queries_only: + assert len(data["search_queries"]) > 0 + elif is_custom_tools: + assert len(data["tool_calls"]) > 0 + else: + assert len(data["text"]) > 0 + + assert is_valid_uuid(data["response_id"]) + assert is_valid_uuid(data["conversation_id"]) + assert is_valid_uuid(data["generation_id"]) + assert data["finish_reason"] == "COMPLETE" or data["finish_reason"] == "MAX_TOKENS" + + return data["conversation_id"] + + +def validate_chat_streaming_tool_cals_response(response: Any, tools: list) -> None: + data = [] + for line in response.iter_lines(): + if not line or ":ping" in line or ": ping" in line: + continue + + # remove the 'data' prefix to make it a valid JSON + line = line.replace("data: ", "") + if "event" in line: + response_json = json.loads(line) + assert response_json["event"] in [e.value for e in StreamEvent] + if response_json["event"] == StreamEvent.TOOL_CALLS_GENERATION: + data.append(response_json["data"]) + + tool_calls_names = [tool['name'] for entry in data for tool in entry['tool_calls']] + + # Check if all required tools are in the tool_names + assert all(tool in tool_calls_names for tool in tools) + + +def is_valid_uuid(id: str) -> bool: + try: + uuid.UUID(id, version=4) + return True + except ValueError: + return False diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_conversation.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_conversation.py new file mode 100644 index 0000000000000000000000000000000000000000..343f37a833941863ab6b72c2f2c6b2136134a4d7 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_conversation.py @@ -0,0 +1,828 @@ +from unittest.mock import MagicMock + +from fastapi.testclient import TestClient +from sqlalchemy.orm import Session + +from backend.database_models import ( + Citation, + Conversation, + ConversationFileAssociation, + Document, + File, + Message, +) +from backend.schemas.user import User +from backend.services.file import get_file_service +from backend.tests.unit.factories import get_factory + + +# CONVERSATIONS +def test_list_conversations_empty(session_client: TestClient, user) -> None: + response = session_client.get("/v1/conversations", headers={"User-Id": user.id}) + results = response.json() + + assert response.status_code == 200 + assert len(results) == 0 + + +def test_list_conversations(session_client: TestClient, session: Session, user) -> None: + get_factory("Conversation", session).create(user_id=user.id) + response = session_client.get("/v1/conversations", headers={"User-Id": user.id}) + results = response.json() + + assert response.status_code == 200 + assert len(results) == 1 + + +def test_list_conversations_with_agent( + session_client: TestClient, session: Session, user +) -> None: + agent = get_factory("Agent", session).create( + id="agent_id", name="test agent", user=user + ) + conversation1 = get_factory("Conversation", session).create( + agent_id=agent.id, user_id=user.id + ) + _ = get_factory("Conversation", session).create(user_id=user.id) + + response = session_client.get( + "/v1/conversations", headers={"User-Id": user.id}, params={"agent_id": agent.id} + ) + results = response.json() + + assert response.status_code == 200 + assert len(results) == 1 + + conversation = results[0] + assert conversation["id"] == conversation1.id + + +def test_list_conversation_with_deleted_agent( + session_client: TestClient, session: Session, user +) -> None: + agent = get_factory("Agent", session).create( + id="agent_id", name="test agent", user=user + ) + conversation = get_factory("Conversation", session).create( + agent_id=agent.id, user_id=user.id + ) + + response = session_client.get( + "/v1/conversations", headers={"User-Id": user.id}, params={"agent_id": agent.id} + ) + results = response.json() + + assert response.status_code == 200 + assert len(results) == 1 + assert results[0]["id"] == conversation.id + + # Delete agent and check that conversation is also deleted + response = session_client.delete( + f"/v1/agents/{agent.id}", headers={"User-Id": user.id} + ) + assert response.status_code == 200 + + response = session_client.get( + "/v1/conversations", headers={"User-Id": user.id}, params={"agent_id": agent.id} + ) + results = response.json() + + assert response.status_code == 200 + assert len(results) == 0 + + +def test_list_conversations_missing_user_id( + session_client: TestClient, + session: Session, + user: User, +) -> None: + _ = get_factory("Conversation", session).create(user_id=user.id) + response = session_client.get("/v1/conversations") + + assert response.status_code == 401 + assert response.json() == { "detail": "User-Id required in request headers." } + + +def test_get_conversation( + session_client: TestClient, + session: Session, + user: User, +) -> None: + conversation = get_factory("Conversation", session).create(user_id=user.id) + response = session_client.get( + f"/v1/conversations/{conversation.id}", + headers={"User-Id": conversation.user_id}, + ) + response_conversation = response.json() + + assert response.status_code == 200 + assert response_conversation["id"] == conversation.id + assert response_conversation["title"] == conversation.title + + +def test_get_conversation_lists_message_files( + session_client: TestClient, + session: Session, + user: User, +) -> None: + user = get_factory("User", session).create() + conversation = get_factory("Conversation", session).create(user_id=user.id) + file = get_factory("File", session).create( + user_id=user.id, + ) + message = get_factory("Message", session).create( + conversation_id=conversation.id, + user_id=user.id, + position=0, + is_active=True, + text="hello", + ) + + # Create conversation,message<>file relations + _ = get_factory("ConversationFileAssociation", session).create( + conversation_id=conversation.id, user_id=user.id, file_id=file.id + ) + _ = get_factory("MessageFileAssociation", session).create( + message_id=message.id, user_id=user.id, file_id=file.id + ) + + session.refresh(conversation) + + response = session_client.get( + f"/v1/conversations/{conversation.id}", + headers={"User-Id": conversation.user_id}, + ) + response_conversation = response.json() + + assert response.status_code == 200 + assert response_conversation["id"] == conversation.id + assert response_conversation["title"] == conversation.title + assert response_conversation["messages"][0]["files"][0]["id"] == file.id + + +def test_get_organization_conversation_list( + session_client: TestClient, + session: Session, + user: User, +) -> None: + organization = get_factory("Organization", session).create(name="test org") + organization1 = get_factory("Organization", session).create(name="test org1") + for i in range(3): + get_factory("Conversation", session).create( + user_id=user.id, + organization_id=organization.id, + description=organization.id, + ) + get_factory("Conversation", session).create( + user_id=user.id, organization_id=organization1.id + ) + + response = session_client.get( + "/v1/conversations", + headers={"User-Id": user.id, "Organization-Id": organization.id}, + ) + response_conversation = response.json() + + assert response.status_code == 200 + assert len(response_conversation) == 3 + for conversation in response_conversation: + assert conversation["description"] == organization.id + + +def test_fail_get_nonexistent_conversation( + session_client: TestClient, + session: Session, + user: User, +) -> None: + response = session_client.get("/v1/conversations/123", headers={"User-Id": user.id}) + + assert response.status_code == 404 + assert response.json() == {"detail": "Conversation with ID: 123 not found."} + + +def test_update_conversation_title( + session_client: TestClient, + session: Session, + user: User, +) -> None: + conversation = get_factory("Conversation", session).create( + title="test title", user_id=user.id + ) + response = session_client.put( + f"/v1/conversations/{conversation.id}", + json={"title": "new title"}, + headers={"User-Id": user.id}, + ) + response_conversation = response.json() + + assert response.status_code == 200 + assert response_conversation["title"] == "new title" + + # Check if the conversation was updated + conversation = ( + session.query(Conversation) + .filter_by(id=conversation.id, user_id=conversation.user_id) + .first() + ) + assert conversation is not None + assert conversation.title == "new title" + + +def test_update_conversation_description( + session_client: TestClient, + session: Session, + user: User, +) -> None: + conversation = get_factory("Conversation", session).create( + description="test description", user_id=user.id + ) + response = session_client.put( + f"/v1/conversations/{conversation.id}", + json={"description": "new description"}, + headers={"User-Id": user.id}, + ) + response_conversation = response.json() + + assert response.status_code == 200 + assert response_conversation["description"] == "new description" + + # Check if the conversation was updated + conversation = ( + session.query(Conversation) + .filter_by(id=conversation.id, user_id=conversation.user_id) + .first() + ) + assert conversation is not None + assert conversation.description == "new description" + + +def test_fail_update_nonexistent_conversation( + session_client: TestClient, + session: Session, + user: User, +) -> None: + response = session_client.put( + "/v1/conversations/123", + json={"title": "new title"}, + headers={"User-Id": user.id}, + ) + + assert response.status_code == 404 + assert response.json() == {"detail": "Conversation with ID: 123 not found."} + + +def test_update_conversations_missing_user_id( + session_client: TestClient, + session: Session, + user: User, +) -> None: + get_factory("Conversation", session).create(user_id=user.id) + response = session_client.get("/v1/conversations") + + assert response.status_code == 401 + assert response.json() == { "detail": "User-Id required in request headers." } + + +def test_toggle_conversation_pin( + session_client: TestClient, + session: Session, + user: User, +) -> None: + conversation = get_factory("Conversation", session).create( + is_pinned=False, user_id=user.id + ) + response = session_client.put( + f"/v1/conversations/{conversation.id}/toggle-pin", + json={"is_pinned": True}, + headers={"User-Id": user.id}, + ) + response_conversation = response.json() + + assert response.status_code == 200 + assert response_conversation["is_pinned"] + assert response_conversation["updated_at"] == conversation.updated_at.isoformat() + + # Check if the conversation was updated + updated_conversation = ( + session.query(Conversation) + .filter_by(id=conversation.id, user_id=conversation.user_id) + .first() + ) + assert updated_conversation is not None + assert updated_conversation.is_pinned + assert updated_conversation.updated_at == conversation.updated_at + + +def test_delete_conversation( + session_client: TestClient, + session: Session, + user: User, +) -> None: + conversation = get_factory("Conversation", session).create( + title="test title", user_id=user.id, id="conversation_id" + ) + response = session_client.delete( + f"/v1/conversations/{conversation.id}", + headers={"User-Id": user.id}, + ) + + assert response.status_code == 200 + assert response.json() == {} + + # Check if the conversation was deleted + conversation = ( + session.query(Conversation) + .filter_by(id="conversation_id", user_id=user.id) + .first() + ) + assert conversation is None + + +def test_fail_delete_nonexistent_conversation( + session_client: TestClient, + session: Session, + user: User, +) -> None: + response = session_client.delete( + "/v1/conversations/123", headers={"User-Id": user.id} + ) + + assert response.status_code == 404 + assert response.json() == {"detail": "Conversation with ID: 123 not found."} + + +def test_delete_conversation_with_files( + session_client: TestClient, + session: Session, + user: User, +) -> None: + file = get_factory("File", session).create( + id="file_id", + file_name="test_file.txt", + user_id=user.id, + ) + conversation = get_factory("Conversation", session).create( + id="conversation_id", title="test title", user_id=user.id + ) + + _ = get_factory("ConversationFileAssociation", session).create( + conversation_id=conversation.id, user_id=user.id, file_id=file.id + ) + + response = session_client.delete( + f"/v1/conversations/{conversation.id}", + headers={"User-Id": conversation.user_id}, + ) + + assert response.status_code == 200 + assert response.json() == {} + + # Check if the files were deleted + file = session.query(File).filter(File.id == "file_id").first() + assert file is None + + # Check if the conversation was deleted + conversation = ( + session.query(Conversation) + .filter_by(id="conversation.id", user_id=user.id) + .first() + ) + assert conversation is None + + +def test_delete_conversation_with_messages( + session_client: TestClient, + session: Session, + user: User, +) -> None: + conversation = get_factory("Conversation", session).create( + title="test title", user_id=user.id, id="conversation_id" + ) + _ = get_factory("Message", session).create( + text="test message", + conversation_id=conversation.id, + user_id=user.id, + ) + + response = session_client.delete( + f"/v1/conversations/{conversation.id}", + headers={"User-Id": user.id}, + ) + + assert response.status_code == 200 + assert response.json() == {} + + # Check if the conversation was deleted + conversation = ( + session.query(Conversation) + .filter_by(id="conversation_id", user_id=user.id) + .first() + ) + assert conversation is None + + +def test_delete_conversation_with_children_cascades_delete( + session_client: TestClient, + session: Session, + user: User, +) -> None: + file = get_factory("File", session).create( + id="file_id", + file_name="test_file.txt", + user_id=user.id, + ) + conversation = get_factory("Conversation", session).create( + id="converastion_id", title="test title", user_id=user.id + ) + message = get_factory("Message", session).create( + text="test message", + conversation_id=conversation.id, + user_id=user.id, + ) + message_id = message.id + _ = get_factory("Citation", session).create(message_id=message.id, user_id=user.id) + _ = get_factory("Document", session).create( + conversation_id=conversation.id, + message_id=message.id, + user_id=user.id, + ) + _ = get_factory("ConversationFileAssociation", session).create( + conversation_id=conversation.id, user_id=user.id, file_id=file.id + ) + + response = session_client.delete( + f"/v1/conversations/{conversation.id}", + headers={"User-Id": user.id}, + ) + + assert response.status_code == 200 + assert response.json() == {} + + # Check if the files were deleted + db_files = session.query(File).filter(File.id == "file_id").all() + assert not db_files + + # Check all children deleted + messages = ( + session.query(Message) + .filter( + Message.conversation_id == "converastion_id", + Message.user_id == user.id, + ) + .all() + ) + citations = ( + session.query(Citation) + .filter(Citation.message_id == message_id, Citation.user_id == user.id) + .all() + ) + documents = session.query(Document).filter(Document.message_id == message_id).all() + + assert not messages + assert not citations + assert not documents + + # Check if the conversation was deleted + conversation = ( + session.query(Conversation) + .filter( + Conversation.id == "converastion_id", + Conversation.user_id == user.id, + ) + .first() + ) + assert conversation is None + + +def test_delete_conversation_missing_user_id( + session_client: TestClient, + session: Session, + user: User, +) -> None: + conversation = get_factory("Conversation", session).create( + title="test title", user_id=user.id + ) + response = session_client.delete(f"/v1/conversations/{conversation.id}") + + assert response.status_code == 401 + assert response.json() == { "detail": "User-Id required in request headers." } + + +def test_search_conversations_missing_user_id( + session_client: TestClient, + session: Session, + user: User, +) -> None: + get_factory("Conversation", session).create(title="test title", user_id=user.id) + response = session_client.get("/v1/conversations:search", params={"query": "test"}) + + assert response.status_code == 401 + assert response.json() == { "detail": "User-Id required in request headers." } + + +# FILES +def test_list_files( + session_client: TestClient, session: Session, user: User +) -> None: + conversation = get_factory("Conversation", session).create(user_id=user.id) + files = [ + ( + "files", + ( + "Mariana_Trench.pdf", + open("src/backend/tests/unit/test_data/Mariana_Trench.pdf", "rb"), + ), + ) + ] + response = session_client.post( + "/v1/conversations/batch_upload_file", + headers={"User-Id": conversation.user_id}, + files=files, + data={"conversation_id": conversation.id}, + ) + assert response.status_code == 200 + files = response.json() + uploaded_file = files[0] + + response = session_client.get( + f"/v1/conversations/{conversation.id}/files", + headers={"User-Id": conversation.user_id}, + ) + + assert response.status_code == 200 + response = response.json() + assert len(response) == 1 + response_file = response[0] + assert response_file["id"] == uploaded_file["id"] + assert response_file["file_name"] == uploaded_file["file_name"] + + +def test_list_files_no_files( + session_client: TestClient, session: Session, user: User +) -> None: + conversation = get_factory("Conversation", session).create(user_id=user.id) + response = session_client.get( + f"/v1/conversations/{conversation.id}/files", + headers={"User-Id": conversation.user_id}, + ) + + assert response.status_code == 200 + assert response.json() == [] + + +def test_list_files_missing_user_id( + session_client: TestClient, session: Session, user: User +) -> None: + conversation = get_factory("Conversation", session).create(user_id=user.id) + response = session_client.get(f"/v1/conversations/{conversation.id}/files") + + assert response.status_code == 401 + assert response.json() == { "detail": "User-Id required in request headers." } + + +def test_get_file( + session_client: TestClient, session: Session, user: User +) -> None: + conversation = get_factory("Conversation", session).create(user_id=user.id) + response = session_client.post( + "/v1/conversations/batch_upload_file", + headers={"User-Id": conversation.user_id}, + files=[ + ("files", ("Mariana_Trench.pdf", open("src/backend/tests/unit/test_data/Mariana_Trench.pdf", "rb"))) + ], + data={"conversation_id": conversation.id}, + ) + assert response.status_code == 200 + uploaded_file = response.json()[0] + + response = session_client.get( + f"/v1/conversations/{conversation.id}/files/{uploaded_file['id']}", + headers={"User-Id": conversation.user_id}, + ) + + assert response.status_code == 200 + response_file = response.json() + assert response_file["id"] == uploaded_file["id"] + assert response_file["file_name"] == uploaded_file["file_name"] + + +def test_fail_get_file_nonexistent_conversation( + session_client: TestClient, session: Session, user: User +) -> None: + response = session_client.get( + "/v1/conversations/123/files/456", + headers={"User-Id": user.id}, + ) + + assert response.status_code == 404 + assert response.json() == {"detail": "Conversation with ID: 123 not found."} + + +def test_fail_get_file_nonbelong_file( + session_client: TestClient, session: Session, user: User +) -> None: + conversation = get_factory("Conversation", session).create(user_id=user.id) + response = session_client.get( + f"/v1/conversations/{conversation.id}/files/123", + headers={"User-Id": conversation.user_id}, + ) + + assert response.status_code == 404 + assert response.json() == {"detail": f"File with ID: 123 does not belong to the conversation with ID: {conversation.id}."} + + +def test_batch_upload_file_existing_conversation( + session_client: TestClient, session: Session, user +) -> None: + file_paths = { + "Mariana_Trench.pdf": "src/backend/tests/unit/test_data/Mariana_Trench.pdf", + "Cardistry.pdf": "src/backend/tests/unit/test_data/Cardistry.pdf", + } + files = [ + ("files", (file_name, open(file_path, "rb"))) + for file_name, file_path in file_paths.items() + ] + + conversation = get_factory("Conversation", session).create(user_id=user.id) + + response = session_client.post( + "/v1/conversations/batch_upload_file", + headers={"User-Id": conversation.user_id}, + files=files, + data={"conversation_id": conversation.id}, + ) + + files = response.json() + + assert response.status_code == 200 + assert len(files) == len(file_paths) + uploaded_file_names = [file["file_name"] for file in files] + assert all(file_name in uploaded_file_names for file_name in file_paths.keys()) + for file in files: + conversation_file_association = ( + session.query(ConversationFileAssociation) + .filter_by(conversation_id=conversation.id, file_id=file.get("id")) + .first() + ) + assert conversation_file_association is not None + + files_stored = get_file_service().get_files_by_conversation_id( + session, conversation.user_id, conversation.id, MagicMock() + ) + assert len(files_stored) == len(file_paths) + + +def test_batch_upload_file_nonexistent_conversation_creates_new_conversation( + session_client: TestClient, session: Session, user +) -> None: + file_paths = { + "Mariana_Trench.pdf": "src/backend/tests/unit/test_data/Mariana_Trench.pdf", + "Cardistry.pdf": "src/backend/tests/unit/test_data/Cardistry.pdf", + "Tapas.pdf": "src/backend/tests/unit/test_data/Tapas.pdf", + "Mount_Everest.pdf": "src/backend/tests/unit/test_data/Mount_Everest.pdf", + } + files = [ + ("files", (file_name, open(file_path, "rb"))) + for file_name, file_path in file_paths.items() + ] + + response = session_client.post( + "/v1/conversations/batch_upload_file", + files=files, + headers={"User-Id": user.id}, + ) + + uploaded_files = response.json() + + conversation_file_association = ( + session.query(ConversationFileAssociation) + .filter_by(file_id=uploaded_files[0].get("id")) + .first() + ) + created_conversation = ( + session.query(Conversation) + .filter_by(id=conversation_file_association.conversation_id) + .first() + ) + + assert conversation_file_association is not None + assert response.status_code == 200 + assert created_conversation is not None + assert len(files) == len(file_paths) + uploaded_file_names = [file["file_name"] for file in uploaded_files] + assert all(file_name in uploaded_file_names for file_name in file_paths.keys()) + + for file in uploaded_files: + conversation_file_association = ( + session.query(ConversationFileAssociation) + .filter_by(file_id=file.get("id"), conversation_id=created_conversation.id) + .first() + ) + assert conversation_file_association is not None + + files_stored = get_file_service().get_files_by_conversation_id( + session, created_conversation.user_id, created_conversation.id, MagicMock() + ) + assert len(files_stored) == len(file_paths) + + +def test_batch_upload_file_nonexistent_conversation_fails_if_user_id_not_provided( + session_client: TestClient, session: Session, user: User +) -> None: + file_paths = { + "Mariana_Trench.pdf": "src/backend/tests/unit/test_data/Mariana_Trench.pdf", + "Cardistry.pdf": "src/backend/tests/unit/test_data/Cardistry.pdf", + "Tapas.pdf": "src/backend/tests/unit/test_data/Tapas.pdf", + "Mount_Everest.pdf": "src/backend/tests/unit/test_data/Mount_Everest.pdf", + } + files = [ + ("files", (file_name, open(file_path, "rb"))) + for file_name, file_path in file_paths.items() + ] + + response = session_client.post("/v1/conversations/batch_upload_file", files=files) + + assert response.status_code == 401 + assert response.json() == { "detail": "User-Id required in request headers." } + + +def test_delete_file( + session_client: TestClient, + session: Session, + user: User, +) -> None: + conversation = get_factory("Conversation", session).create(user_id=user.id) + files = [ + ( + "files", + ( + "Mariana_Trench.pdf", + open("src/backend/tests/unit/test_data/Mariana_Trench.pdf", "rb"), + ), + ) + ] + + response = session_client.post( + "/v1/conversations/batch_upload_file", + headers={"User-Id": conversation.user_id}, + files=files, + data={"conversation_id": conversation.id}, + ) + assert response.status_code == 200 + files = response.json() + uploaded_file = files[0] + + response = session_client.delete( + f"/v1/conversations/{conversation.id}/files/{uploaded_file['id']}", + headers={"User-Id": conversation.user_id}, + ) + assert response.status_code == 200 + assert response.json() == {} + + # Check if File + files = get_file_service().get_files_by_conversation_id( + session, conversation.user_id, conversation.id, MagicMock() + ) + assert files == [] + + conversation_file_association = ( + session.query(ConversationFileAssociation) + .filter(File.id == uploaded_file["id"], File.user_id == user.id) + .first() + ) + assert conversation_file_association is None + + conversation = ( + session.query(Conversation).filter(Conversation.id == conversation.id).first() + ) + assert conversation.file_ids == [] + + +def test_fail_delete_nonexistent_file( + session_client: TestClient, session: Session, user: User +) -> None: + conversation = get_factory("Conversation", session).create(user_id=user.id) + response = session_client.delete( + f"/v1/conversations/{conversation.id}/files/123", + headers={"User-Id": conversation.user_id}, + ) + + assert response.status_code == 404 + assert response.json() == {"detail": "File with ID: 123 not found."} + + +def test_fail_delete_file_missing_user_id( + session_client: TestClient, session: Session, user: User +) -> None: + conversation = get_factory("Conversation", session).create(user_id=user.id) + file = get_factory("File", session).create( + file_name="test_file.txt", + user_id=conversation.user_id, + ) + + response = session_client.delete( + f"/v1/conversations/{conversation.id}/files/{file.id}" + ) + + assert response.status_code == 401 + assert response.json() == { "detail": "User-Id required in request headers." } diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_deployment.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_deployment.py new file mode 100644 index 0000000000000000000000000000000000000000..8488faa5e57ef2ef4e194bc1b55342f98d2b9af4 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_deployment.py @@ -0,0 +1,197 @@ +from unittest.mock import patch + +import pytest +from fastapi.testclient import TestClient +from sqlalchemy.orm import Session + +from backend.config.deployments import AVAILABLE_MODEL_DEPLOYMENTS +from backend.database_models import Deployment +from backend.model_deployments.cohere_platform import CohereDeployment +from backend.tests.unit.model_deployments.mock_deployments.mock_cohere_platform import ( + MockCohereDeployment, +) + + +@pytest.fixture +def db_deployment(session): + session.query(Deployment).delete() + mock_cohere_deployment = Deployment( + name=CohereDeployment.name(), + description="A mock Cohere deployment from the DB", + deployment_class_name=CohereDeployment.__name__, + is_community=False, + default_deployment_config={"COHERE_API_KEY": "db-test-api-key"}, + id="db-mock-cohere-platform-id", + ) + session.add(mock_cohere_deployment) + session.commit() + return mock_cohere_deployment + +def test_create_deployment(session_client: TestClient) -> None: + request_json = { + "name": "TestDeployment", + "default_deployment_config": {"COHERE_API_KEY": "test-api-key"}, + "deployment_class_name": "CohereDeployment", + "description": "Test deployment", + "is_community": False, + } + response = session_client.post( + "/v1/deployments", json=request_json, headers={"User-Id": "1"} + ) + assert response.status_code == 200 + deployment = response.json() + assert deployment["name"] == request_json["name"] + assert deployment["config"] == {"COHERE_API_KEY": '*****'} + assert deployment["is_available"] + + +def test_create_deployment_unique(session_client: TestClient, db_deployment) -> None: + request_json = { + "name": MockCohereDeployment.name(), + "default_deployment_config": {"COHERE_API_KEY": "test-api-key"}, + "deployment_class_name": "CohereDeployment", + } + + response = session_client.post( + "/v1/deployments", json=request_json, headers={"User-Id": "user-id"} + ) + assert response.status_code == 400 + assert ( + f"Deployment {MockCohereDeployment.name()} already exists." + in response.json()["detail"] + ) + + +def test_create_deployment_invalid_deployment_class(session_client: TestClient) -> None: + request_json = { + "name": "TestDeployment1", + "default_deployment_config": {"COHERE_API_KEY": " "}, + "deployment_class_name": "InvalidDeployment", + "description": "Test deployment", + "is_community": False, + } + response = session_client.post( + "/v1/deployments", json=request_json, headers={"User-Id": "1"} + ) + assert response.status_code == 404 + assert ( + "Deployment class name InvalidDeployment not found." + in response.json()["detail"] + ) + + +def test_list_deployments_has_all_option( + session_client: TestClient, session: Session +) -> None: + response = session_client.get("/v1/deployments?all=1") + assert response.status_code == 200 + deployments = response.json() + assert len(deployments) == len(AVAILABLE_MODEL_DEPLOYMENTS) + + +def test_list_deployments_no_available_models_404( + session_client: TestClient, session: Session +) -> None: + session.query(Deployment).delete() + with patch("backend.services.deployment.AVAILABLE_MODEL_DEPLOYMENTS", {}): + response = session_client.get("/v1/deployments") + assert response.status_code == 404 + assert response.json() == { + "detail": [ + "No available deployments found. Please ensure that the required variables in your configuration.yaml and secrets.yaml are set up correctly. Refer to the README.md for detailed instructions." + ] + } + + +def test_list_deployments_no_available_db_models_with_all_option( + session_client: TestClient, session: Session +) -> None: + session.query(Deployment).delete() + response = session_client.get("/v1/deployments?all=1") + assert response.status_code == 200 + assert len(response.json()) == len(AVAILABLE_MODEL_DEPLOYMENTS) + + +def test_update_deployment(session_client: TestClient, db_deployment) -> None: + request_json = { + "name": "UpdatedDeployment", + "default_deployment_config": {"COHERE_API_KEY": "test-api-key"}, + "deployment_class_name": "CohereDeployment", + "description": "Updated deployment", + "is_community": False, + } + response = session_client.put("/v1/deployments/" + db_deployment.id, json=request_json) + assert response.status_code == 200 + updated_deployment = response.json() + assert updated_deployment["name"] == request_json["name"] + assert updated_deployment["config"] == {"COHERE_API_KEY": '*****'} + assert updated_deployment["is_available"] + assert updated_deployment["description"] == request_json["description"] + assert updated_deployment["is_community"] == request_json["is_community"] + + +def test_delete_deployment(session_client: TestClient, session: Session, db_deployment) -> None: + deployment = session.query(Deployment).first() + assert deployment is not None + response = session_client.delete("/v1/deployments/" + deployment.id) + deleted = session.query(Deployment).filter(Deployment.id == deployment.id).first() + assert response.status_code == 200 + assert deleted is None + assert response.json() == {} + + +def test_set_env_vars( + session_client: TestClient, db_deployment +) -> None: + response = session_client.post( + f"/v1/deployments/{db_deployment.id}/update_config", + json={ + "env_vars": { + "COHERE_API_KEY": "TestCohereValue", + }, + }, + ) + assert response.status_code == 200 + updated_deployment = response.json() + assert updated_deployment["config"] == {"COHERE_API_KEY": "*****"} + + +def test_set_env_vars_with_invalid_deployment_name( + session_client: TestClient +): + response = session_client.post("/v1/deployments/unknown/update_config", json={}) + assert response.status_code == 404 + + +def test_set_env_vars_with_var_for_other_deployment( + session_client: TestClient, db_deployment +) -> None: + response = session_client.post( + f"/v1/deployments/{db_deployment.id}/update_config", + json={ + "env_vars": { + "SAGEMAKER_VAR_1": "TestSageMakerValue", + }, + }, + ) + assert response.status_code == 400 + assert response.json() == { + "detail": "Environment variables not valid for deployment: SAGEMAKER_VAR_1" + } + + +def test_set_env_vars_with_invalid_var( + session_client: TestClient, db_deployment +) -> None: + response = session_client.post( + f"/v1/deployments/{db_deployment.id}/update_config", + json={ + "env_vars": { + "API_KEY": "12345", + }, + }, + ) + assert response.status_code == 400 + assert response.json() == { + "detail": "Environment variables not valid for deployment: API_KEY" + } diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_main.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_main.py new file mode 100644 index 0000000000000000000000000000000000000000..4761affe7c8b7ad5c103397d5587ea0a3aaa4244 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_main.py @@ -0,0 +1,17 @@ +from fastapi.testclient import TestClient + + +def test_global_exception_handler(error_client: TestClient): + response = error_client.get("/error") + + assert response.status_code == 500 + assert response.json() == { + "message": "Failed method GET at URL http://testserver/error. Exception message is Exception('Test exception')." + } + + +def test_health_ok(client: TestClient) -> None: + response = client.get("/health") + + assert response.status_code == 200 + assert response.json() == {"status": "OK"} diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_model.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_model.py new file mode 100644 index 0000000000000000000000000000000000000000..fef403c9516a37cee6dee222f8b979c4edec2271 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_model.py @@ -0,0 +1,152 @@ +from fastapi.testclient import TestClient +from sqlalchemy.orm import Session + +from backend.database_models import Model +from backend.database_models.deployment import Deployment +from backend.tests.unit.factories import get_factory + + +def test_create_model(session_client: TestClient, session: Session, deployment: Deployment) -> None: + request_json = { + "name": "sagemaker-command-created", + "cohere_name": "command", + "description": "New model", + "deployment_id": deployment.id, + } + + response = session_client.post( + "/v1/models", json=request_json, headers={"User-Id": "1"} + ) + + assert response.status_code == 200 + response_json = response.json() + + assert response_json["id"] is not None + assert ( + "deployment_id" in response_json + and response_json["deployment_id"] == deployment.id + ) + assert request_json["name"] == response_json["name"] + + +def test_create_model_non_existing_deployment( + session_client: TestClient, session: Session, +) -> None: + request_json = { + "name": "sagemaker-command-created", + "cohere_name": "command", + "description": "New model", + "deployment_id": "non-existing-deployment", + } + + response = session_client.post( + "/v1/models", json=request_json, headers={"User-Id": "1"} + ) + response_json = response.json() + + assert response.status_code == 400 + assert ( + f"Deployment {request_json['deployment_id']} not found" + in response_json["detail"] + ) + + +def test_update_model(session_client: TestClient, session: Session, deployment: Deployment) -> None: + request_json = { + "name": "sagemaker-command-updated", + "cohere_name": "command", + "description": "Updated model", + } + model = get_factory("Model", session).create(deployment=deployment) + response = session_client.put( + f"/v1/models/{model.id}", json=request_json, headers={"User-Id": "1"} + ) + + assert response.status_code == 200 + response_json = response.json() + assert response_json["id"] == model.id + assert request_json["name"] == response_json["name"] + assert request_json["description"] == response_json["description"] + assert model.deployment_id == response_json["deployment_id"] + + +def test_get_model(session_client: TestClient, session: Session, deployment: Deployment) -> None: + # Delete all models + session.query(Model).delete() + model = get_factory("Model", session).create(deployment=deployment) + + response = session_client.get(f"/v1/models/{model.id}") + assert response.status_code == 200 + response_json = response.json() + assert response_json["id"] == model.id + assert response_json["name"] == model.name + assert response_json["deployment_id"] == model.deployment_id + + +def test_get_model_non_existing(session_client: TestClient, session: Session) -> None: + response = session_client.get("/v1/models/non-existing-id") + assert response.status_code == 404 + response_json = response.json() + assert "Model not found" in response_json["detail"] + + +def test_list_models( + session_client: TestClient, + session: Session, + deployment: Deployment, +) -> None: + # Delete all models + session.query(Model).delete() + for _ in range(5): + get_factory("Model", session).create(deployment=deployment) + + response = session_client.get("/v1/models") + assert response.status_code == 200 + models = response.json() + assert len(models) == 5 + + +def test_list_models_empty( + session_client: TestClient, + session: Session, +) -> None: + session.query(Model).delete() + response = session_client.get("/v1/models") + assert response.status_code == 200 + models = response.json() + assert len(models) == 0 + + +def test_list_models_with_pagination( + session_client: TestClient, + session: Session, + deployment: Deployment, +) -> None: + # Delete all models + session.query(Model).delete() + for i in range(10): + get_factory("Model", session).create( + name=f"Test Model {i}", deployment=deployment + ) + + response = session_client.get("/v1/models?offset=5&limit=5") + assert response.status_code == 200 + models = response.json() + assert len(models) == 5 + + for i, model in enumerate(models): + assert model["name"] == f"Test Model {i + 5}" + + +def test_delete_model( + session_client: TestClient, + session: Session, + deployment: Deployment, +) -> None: + model = get_factory("Model", session).create(deployment=deployment) + response = session_client.delete(f"/v1/models/{model.id}") + assert response.status_code == 200 + response_json = response.json() + assert response_json == {} + response = session_client.get(f"/v1/models/{model.id}") + assert response.status_code == 404 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_organization.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_organization.py new file mode 100644 index 0000000000000000000000000000000000000000..13a4023b5df9c1071e4192b63c7892db9f65c3b9 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_organization.py @@ -0,0 +1,84 @@ +from fastapi.testclient import TestClient +from sqlalchemy.orm import Session + +from backend.database_models.organization import Organization as OrganizationModel +from backend.schemas.organization import CreateOrganization, UpdateOrganization +from backend.tests.unit.factories import get_factory + + +def test_create_organization(session_client: TestClient, session: Session) -> None: + organization = CreateOrganization(name="test organization") + response = session_client.post("/v1/organizations", json=organization.model_dump()) + assert response.status_code == 200 + assert response.json()["name"] == organization.name + + +def test_create_organization_with_existing_name( + session_client: TestClient, session: Session +) -> None: + get_factory("Organization", session).create(name="test organization") + new_organization = CreateOrganization(name="test organization") + response = session_client.post("/v1/organizations", json=new_organization.model_dump()) + assert response.status_code == 400 + assert response.json() == { + "detail": "Organization with name: test organization already exists." + } + + +def test_update_organization(session_client: TestClient, session: Session) -> None: + organization = get_factory("Organization", session).create(name="test organization") + new_organization = UpdateOrganization(name="new organization") + response = session_client.put( + f"/v1/organizations/{organization.id}", json=new_organization.model_dump() + ) + assert response.status_code == 200 + assert response.json()["name"] == new_organization.name + + +def test_update_not_existing_organization( + session_client: TestClient, session: Session +) -> None: + new_organization = UpdateOrganization(name="new organization") + response = session_client.put("/v1/organizations/123", json=new_organization.model_dump()) + assert response.status_code == 404 + assert response.json() == {"detail": "Organization with ID: 123 not found."} + + +def test_get_organization(session_client: TestClient, session: Session) -> None: + organization = get_factory("Organization", session).create(name="test organization") + response = session_client.get(f"/v1/organizations/{organization.id}") + assert response.status_code == 200 + assert response.json()["name"] == organization.name + + +def test_list_organizations(session_client: TestClient, session: Session) -> None: + session.query(OrganizationModel).delete() + for i in range(5): + get_factory("Organization", session).create(name=f"test organization {i}") + + response = session_client.get("/v1/organizations") + results = response.json() + assert response.status_code == 200 + assert len(results) == 5 + + +def test_delete_organization(session_client: TestClient, session: Session) -> None: + organization = get_factory("Organization", session).create(name="test organization") + response = session_client.delete(f"/v1/organizations/{organization.id}") + assert response.status_code == 200 + assert response.json() == {} + + +def test_list_organization_users(session_client: TestClient, session: Session) -> None: + organization = get_factory("Organization", session).create(name="test organization") + for i in range(5): + user = get_factory("User", session).create(fullname=f"test user {i}") + organization.users.append(user) + + response = session_client.get(f"/v1/organizations/{organization.id}/users") + results = response.json() + assert response.status_code == 200 + assert len(results) == 5 + results = sorted(results, key=lambda x: x["fullname"]) + for i, result in enumerate(results): + assert result["fullname"] == f"test user {i}" diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_scim.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_scim.py new file mode 100644 index 0000000000000000000000000000000000000000..3acaad6776b9557f0dc585ae4e8968dbdf5decfe --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_scim.py @@ -0,0 +1,474 @@ +import base64 + +from fastapi.testclient import TestClient +from sqlalchemy.orm import Session + +import backend.crud.group as group_repo +import backend.crud.user as user_repo +from backend.config import Settings + +scim = Settings().get('auth.scim') +encoded_auth = base64.b64encode( + f"{scim.username}:{scim.password}".encode("utf-8") +).decode("utf-8") +scim_auth_header = { + "Authorization": f"Basic {encoded_auth}", +} + + +def create_user_request( + session_client: TestClient, + user_name="testuser@company.com", + external_id="23123123", + email="thisispatrick@gmail.com", +): + user_data_req = { + "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"], + "userName": user_name, + "name": {"givenName": "Test", "familyName": "User"}, + "emails": [{"primary": True, "value": email, "type": "work"}], + "externalId": external_id, + "locale": "en-US", + "groups": [], + "active": True, + } + + return session_client.post( + "/scim/v2/Users", json=user_data_req, headers=scim_auth_header + ) + + +def create_group_request(session_client: TestClient): + request = { + "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"], + "displayName": "Test SCIMv2", + "members": [], + } + + return session_client.post( + "/scim/v2/Groups", json=request, headers=scim_auth_header + ) + + +def test_create_user(session_client: TestClient, session: Session) -> None: + response = create_user_request(session_client, email="tanzi66m@cohere.com") + assert response.status_code == 201 + response_user = response.json() + + db_user = user_repo.get_user(session, response_user["id"]) + assert db_user is not None + assert db_user.email == "tanzi66m@cohere.com" + + expected_user = { + "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"], + "id": db_user.id, + "userName": "testuser@company.com", + "externalId": "23123123", + "active": True, + "meta": { + "resourceType": "User", + "created": db_user.created_at.isoformat(), + "lastModified": db_user.updated_at.isoformat(), + }, + } + + assert response.status_code == 201 + assert response_user == expected_user + + +def test_get_user(session_client: TestClient, session: Session): + response = session_client.get( + "/scim/v2/Users/does-not-exist", headers=scim_auth_header + ) + error_response = response.json() + + assert response.status_code == 404 + assert error_response == { + "detail": "User not found", + "schemas": ["urn:ietf:params:scim:api:messages:2.0:Error"], + } + + response = create_user_request(session_client) + response_user = response.json() + assert response.status_code == 201 + + response = session_client.get( + f"/scim/v2/Users/{response_user['id']}", headers=scim_auth_header + ) + user_response = response.json() + + db_user = user_repo.get_user_by_external_id(session, response_user["externalId"]) + assert db_user is not None + + expected_user = { + "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"], + "id": db_user.id, + "userName": "testuser@company.com", + "externalId": "23123123", + "active": True, + "meta": { + "resourceType": "User", + "created": db_user.created_at.isoformat(), + "lastModified": db_user.updated_at.isoformat(), + }, + } + + assert user_response == expected_user + + response = session_client.get( + "/scim/v2/Users?filter=userName%20eq%20%22testuser@company.com%22&startIndex=1&count=100", + headers=scim_auth_header, + ) + assert response.status_code == 200 + list_user_response = response.json() + user_response = list_user_response["Resources"][0] + + assert user_response == expected_user + + +def test_put_user(session_client: TestClient, session: Session): + response = create_user_request(session_client, email="abcd@cc.com") + response_user = response.json() + user_id = response_user["id"] + assert response.status_code == 201 + + put_data = { + "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"], + "id": user_id, + "userName": "test.user@okta.local", + "name": {"givenName": "Another", "familyName": "User"}, + "active": True, + "emails": [{"primary": True, "value": "tanzi22m@cohere.com", "type": "work"}], + "meta": {"resourceType": "User"}, + } + + response = session_client.put( + f"/scim/v2/Users/{user_id}", json=put_data, headers=scim_auth_header + ) + response_user = response.json() + db_user = user_repo.get_user(session, user_id) + assert db_user is not None + assert db_user.email == "tanzi22m@cohere.com" + + expected_response = { + "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"], + "id": user_id, + "userName": "test.user@okta.local", + "externalId": db_user.external_id, + "active": True, + "meta": { + "resourceType": "User", + "created": db_user.created_at.isoformat(), + "lastModified": db_user.updated_at.isoformat(), + }, + } + + assert response_user == expected_response + + +def test_patch_user(session_client: TestClient, session: Session): + response = create_user_request(session_client) + response_user = response.json() + user_id = response_user["id"] + assert response.status_code == 201 + + patch_data = { + "schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"], + "Operations": [{"op": "replace", "value": {"active": False}}], + } + + response = session_client.patch( + f"/scim/v2/Users/{user_id}", json=patch_data, headers=scim_auth_header + ) + response_user = response.json() + + db_user = user_repo.get_user(session, user_id) + assert db_user is not None + assert db_user.active is False + + expected_response = { + "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"], + "id": db_user.id, + "userName": "testuser@company.com", + "externalId": "23123123", + "active": False, + "meta": { + "resourceType": "User", + "created": db_user.created_at.isoformat(), + "lastModified": db_user.updated_at.isoformat(), + }, + } + + assert response_user == expected_response + + +def test_create_group(session_client: TestClient, session: Session): + response = create_group_request(session_client) + response_group = response.json() + + group = group_repo.get_group(session, response_group["id"]) + assert group is not None + + assert response.status_code == 201 + assert response_group == { + "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"], + "id": group.id, + "displayName": "Test SCIMv2", + "members": [], + "meta": { + "resourceType": "Group", + "created": group.created_at.isoformat(), + "lastModified": group.updated_at.isoformat(), + }, + } + + +def test_get_group(session_client: TestClient, session: Session): + response = session_client.get( + "/scim/v2/Groups/does-not-exist", headers=scim_auth_header + ) + error_response = response.json() + + assert response.status_code == 404 + assert error_response == { + "detail": "Group not found", + "schemas": ["urn:ietf:params:scim:api:messages:2.0:Error"], + } + + response = create_group_request(session_client) + response_group = response.json() + assert response.status_code == 201 + + response = session_client.get( + f"/scim/v2/Groups/{response_group['id']}", headers=scim_auth_header + ) + group_response = response.json() + + db_group = group_repo.get_group(session, response_group["id"]) + assert db_group is not None + + expected_group = { + "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"], + "id": db_group.id, + "displayName": "Test SCIMv2", + "members": [], + "meta": { + "resourceType": "Group", + "created": db_group.created_at.isoformat(), + "lastModified": db_group.updated_at.isoformat(), + }, + } + + assert group_response == expected_group + + response = session_client.get( + "/scim/v2/Groups?filter=displayName%20eq%20%22Test%20SCIMv2%22&startIndex=1&count=100", + headers=scim_auth_header, + ) + assert response.status_code == 200 + list_group_response = response.json() + group_response = list_group_response["Resources"][0] + + assert group_response == expected_group + + +def test_delete_group(session_client: TestClient, session: Session): + response = create_group_request(session_client) + response_group = response.json() + group_id = response_group["id"] + assert response.status_code == 201 + + response = session_client.delete( + f"/scim/v2/Groups/{group_id}", headers=scim_auth_header + ) + assert response.status_code == 204 + + response = session_client.get( + f"/scim/v2/Groups/{group_id}", headers=scim_auth_header + ) + error_response = response.json() + + assert response.status_code == 404 + assert error_response == { + "detail": "Group not found", + "schemas": ["urn:ietf:params:scim:api:messages:2.0:Error"], + } + + +def test_update_group_name(session_client: TestClient, session: Session): + response = create_group_request(session_client) + response_group = response.json() + group_id = response_group["id"] + assert response.status_code == 201 + + patch_data = { + "schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"], + "Operations": [ + { + "op": "replace", + "value": { + "id": "abf4dd94-a4c0-4f67-89c9-76b03340cb9b", + "displayName": "New Group Name", + }, + } + ], + } + + response = session_client.patch( + f"/scim/v2/Groups/{group_id}", json=patch_data, headers=scim_auth_header + ) + response_group = response.json() + + db_group = group_repo.get_group(session, group_id) + assert db_group is not None + + expected_response = { + "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"], + "id": group_id, + "displayName": "New Group Name", + "members": [], + "meta": { + "resourceType": "Group", + "created": db_group.created_at.isoformat(), + "lastModified": db_group.updated_at.isoformat(), + }, + } + + assert response_group == expected_response + + +def test_add_users_to_group(session_client: TestClient, session: Session): + response = create_group_request(session_client) + response_group = response.json() + group_id = response_group["id"] + assert response.status_code == 201 + + response = create_user_request(session_client) + response_user = response.json() + user_id = response_user["id"] + assert response.status_code == 201 + + patch_data = { + "schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"], + "Operations": [ + { + "op": "add", + "path": "members", + "value": [ + { + "value": user_id, + "display": "test.user@okta.local", + } + ], + }, + ], + } + + response = session_client.patch( + f"/scim/v2/Groups/{group_id}", json=patch_data, headers=scim_auth_header + ) + response_group = response.json() + + db_group = group_repo.get_group(session, group_id) + assert db_group is not None + + expected_response = { + "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"], + "id": group_id, + "displayName": "Test SCIMv2", + "members": [ + { + "value": user_id, + "display": "test.user@okta.local", + } + ], + "meta": { + "resourceType": "Group", + "created": db_group.created_at.isoformat(), + "lastModified": db_group.updated_at.isoformat(), + }, + } + + assert response_group == expected_response + + +def test_replace_users_in_group(session_client: TestClient, session: Session): + test_add_users_to_group(session_client, session) + group = group_repo.get_group_by_name(session, "Test SCIMv2") + + response = create_user_request( + session_client, + user_name="test.user.new@okta.local", + external_id="1234546", + email="sponge@krusty.com", + ) + response_user = response.json() + user_id = response_user["id"] + username = response_user["userName"] + assert response.status_code == 201 + + patch_data = { + "schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"], + "Operations": [ + { + "op": "replace", + "path": "members", + "value": [ + { + "value": user_id, + "display": username, + }, + ], + } + ], + } + + response = session_client.patch( + f"/scim/v2/Groups/{group.id}", json=patch_data, headers=scim_auth_header + ) + response_group = response.json() + + expected_response = { + "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"], + "id": group.id, + "displayName": "Test SCIMv2", + "members": [ + { + "value": user_id, + "display": "test.user.new@okta.local", + } + ], + "meta": { + "resourceType": "Group", + "created": group.created_at.isoformat(), + "lastModified": group.updated_at.isoformat(), + }, + } + + assert response_group == expected_response + + +def test_scim_requests_are_authenticated(session_client: TestClient): + response = session_client.get("/scim/v2/Users") + assert response.status_code == 401 + + response = session_client.post("/scim/v2/Users") + assert response.status_code == 401 + + response = session_client.patch("/scim/v2/Users/123") + assert response.status_code == 401 + + response = session_client.put("/scim/v2/Users/123") + assert response.status_code == 401 + + response = session_client.get("/scim/v2/Groups") + assert response.status_code == 401 + + response = session_client.post("/scim/v2/Groups") + assert response.status_code == 401 + + response = session_client.patch("/scim/v2/Groups/123") + assert response.status_code == 401 + + response = session_client.delete("/scim/v2/Groups/123") + assert response.status_code == 401 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_snapshot.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_snapshot.py new file mode 100644 index 0000000000000000000000000000000000000000..99ff73dfd40f23116abdb6f11641823b3efe48ca --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_snapshot.py @@ -0,0 +1,270 @@ +import pytest +from fastapi.testclient import TestClient +from sqlalchemy.orm import Session + +from backend.tests.unit.factories import get_factory + + +@pytest.fixture(autouse=True) +def conversation(session, user): + return get_factory("Conversation", session).create(id="1", user_id=user.id) + + +@pytest.fixture(autouse=True) +def message(session, conversation, user): + return get_factory("Message", session).create( + id="1", conversation_id=conversation.id, user_id=user.id + ) + + +@pytest.fixture(autouse=True) +def organization(session): + return get_factory("Organization", session).create(id="1") + + +@pytest.fixture +def snapshot(session, conversation, organization, user): + return get_factory("Snapshot", session).create( + id="1", + user_id=user.id, + conversation_id=conversation.id, + last_message_id="1", + version=1, + organization_id=organization.id, + snapshot={ + "title": "Title", + "description": "Description", + "agent": None, + "messages": [ + { + "id": "1", + "conversation_id": "1", + "user_id": "1", + "text": "hey", + "agent": "USER", + "created_at": "2021-01-01T00:00:00", + "updated_at": "2021-01-01T00:00:00", + "generation_id": "1", + "position": 0, + "is_active": True, + "documents": [], + "citations": [], + "files": [], + "tool_calls": [], + "tool_plan": None, + } + ], + }, + ) + + +@pytest.fixture +def snapshot_link(session, snapshot, user): + return get_factory("SnapshotLink", session).create( + id="1", snapshot_id=snapshot.id, user_id=user.id + ) + + +def test_share_conversation( + session_client: TestClient, session: Session, conversation, message, user +) -> None: + conversation.text_messages.append(message) + + request_json = { + "conversation_id": "1", + } + + response = session_client.post( + "/v1/snapshots", json=request_json, headers={"User-Id": user.id} + ) + + assert response.status_code == 200 + response_json = response.json() + + assert "snapshot_id" in response_json + assert "link_id" in response_json + assert len(response_json["messages"]) == 1 + + +def test_share_conversation_twice( + session_client: TestClient, session: Session, conversation, message, user +) -> None: + conversation.text_messages.append(message) + + request_json = { + "conversation_id": "1", + } + + response = session_client.post( + "/v1/snapshots", json=request_json, headers={"User-Id": user.id} + ) + + assert response.status_code == 200 + response_json = response.json() + + assert "snapshot_id" in response_json + assert "link_id" in response_json + assert len(response_json["messages"]) == 1 + + first_snapshot_id = response_json["snapshot_id"] + first_link_id = response_json["link_id"] + + response = session_client.post( + "/v1/snapshots", json=request_json, headers={"User-Id": user.id} + ) + + assert response.status_code == 200 + response_json = response.json() + + assert "snapshot_id" in response_json + assert "link_id" in response_json + assert len(response_json["messages"]) == 1 + assert first_snapshot_id == response_json["snapshot_id"] + assert first_link_id != response_json["link_id"] + + +def test_share_conversation_no_messages(session_client: TestClient, user) -> None: + request_json = { + "conversation_id": "1", + } + + response = session_client.post( + "/v1/snapshots", json=request_json, headers={"User-Id": user.id} + ) + + assert response.status_code == 404 + response_json = response.json() + + assert response_json["detail"] == "Conversation has no messages" + + +def test_share_conversation_not_found( + session_client: TestClient, session: Session, user +) -> None: + request_json = { + "conversation_id": "123", + } + + response = session_client.post( + "/v1/snapshots", json=request_json, headers={"User-Id": user.id} + ) + + assert response.status_code == 404 + response_json = response.json() + + assert response_json["detail"] == "Conversation with ID: 123 not found." + + +def test_list_snapshots( + session_client: TestClient, + session: Session, + conversation, + message, + snapshot, + snapshot_link, + user, +) -> None: + conversation.text_messages.append(message) + + response = session_client.get("/v1/snapshots", headers={"User-Id": user.id}) + + assert response.status_code == 200 + results = response.json() + + assert len(results) == 1 + + +def test_list_snapshots_no_snapshots(session_client: TestClient, user) -> None: + response = session_client.get("/v1/snapshots", headers={"User-Id": user.id}) + + assert response.status_code == 200 + results = response.json() + + assert len(results) == 0 + + +def test_get_snapshot( + session_client: TestClient, + session: Session, + conversation, + message, + snapshot, + snapshot_link, + user, +) -> None: + conversation.text_messages.append(message) + + response = session_client.get("/v1/snapshots/link/1", headers={"User-Id": user.id}) + + assert response.status_code == 200 + result = response.json() + + assert result["last_message_id"] == "1" + assert result["conversation_id"] == "1" + assert isinstance(result["snapshot"], dict) + + +def test_get_snapshot_not_found(session_client: TestClient, user) -> None: + response = session_client.get( + "/v1/snapshots/link/123", headers={"User-Id": user.id} + ) + + assert response.status_code == 404 + result = response.json() + + assert result["detail"] == "Snapshot link not found" + + +def test_delete_snapshot( + session_client: TestClient, + session: Session, + conversation, + message, + snapshot, + snapshot_link, + user, +) -> None: + conversation.text_messages.append(message) + + response = session_client.delete("/v1/snapshots/1", headers={"User-Id": user.id}) + + assert response.status_code == 200 + + response = session_client.get("/v1/snapshots/link/1", headers={"User-Id": user.id}) + + assert response.status_code == 404 + result = response.json() + + assert result["detail"] == "Snapshot link not found" + + +def test_delete_snapshot_not_found(session_client: TestClient, user) -> None: + response = session_client.delete( + "/v1/snapshots/link123", headers={"User-Id": user.id} + ) + + assert response.status_code == 404 + + +def test_delete_snapshot_wrong_user( + session_client: TestClient, + session: Session, + conversation, + message, + snapshot, + snapshot_link, + user, +) -> None: + user2 = get_factory("User", session).create(id=f"new_{user.id}") + conversation.text_messages.append(message) + + response = session_client.delete( + "/v1/snapshots/link/1", headers={"User-Id": user2.id} + ) + + assert response.status_code == 403 + result = response.json() + + assert ( + result["detail"] == "User does not have permission to delete this snapshot link" + ) diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_tool.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_tool.py new file mode 100644 index 0000000000000000000000000000000000000000..36041dfee0b86b01fc64cc6faad02df8c03d5dc7 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_tool.py @@ -0,0 +1,127 @@ +from unittest.mock import patch + +import pytest +from fastapi.testclient import TestClient +from sqlalchemy.orm import Session + +from backend.config.tools import Tool, get_available_tools +from backend.database_models.database import DBSessionDep +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.schemas.user import User +from backend.tests.unit.factories import get_factory +from backend.tools.base import BaseTool + +TOOL_DEFINITION_KEYS = [ + "name", + "display_name", + "parameter_definitions", + "is_visible", + "is_available", + "should_return_token", + "category", + "description" +] + +@pytest.fixture +def mock_get_available_tools(): + with patch("backend.routers.tool.get_available_tools") as mock: + yield mock + +def test_list_tools(session_client: TestClient) -> None: + response = session_client.get("/v1/tools") + assert response.status_code == 200 + available_tools = get_available_tools() + for tool in response.json(): + tool_definition = available_tools.get(tool["name"]) + assert tool_definition is not None + + for key in TOOL_DEFINITION_KEYS: + assert tool[key] == getattr(tool_definition, key) + +def test_list_authed_tool_should_return_token(session_client: TestClient, mock_get_available_tools) -> None: + class MockGoogleDriveAuth(): + def is_auth_required(self, session: DBSessionDep, user_id: str) -> bool: + return False + + def get_auth_url(self, user_id: str) -> str: + return "" + + def get_token(self, session: DBSessionDep, user_id: str) -> str: + return "mock" + class MockGoogleDrive(BaseTool): + ID = "google_drive" + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Google Drive", + implementation=cls, + parameter_definitions={ + "query": { + "description": "Query to search Google Drive documents with.", + "type": "str", + "required": True, + } + }, + is_visible=True, + is_available=True, + auth_implementation=MockGoogleDriveAuth, + should_return_token=True, + error_message=cls.generate_error_message(), + category=ToolCategory.DataLoader, + description="Returns a list of relevant document snippets from the user's Google drive.", + ) + + # Patch Google Drive tool + mock_get_available_tools.return_value = {Tool.Google_Drive.value.ID: MockGoogleDrive.get_tool_definition()} + + response = session_client.get("/v1/tools") + assert response.status_code == 200 + + for tool in response.json(): + print(tool) + if tool["should_return_token"]: + assert tool["token"] == "mock" + +def test_list_authed_tool_should_not_return_token(session_client: TestClient) -> None: + response = session_client.get("/v1/tools") + + assert response.status_code == 200 + + for tool in response.json(): + if not tool["should_return_token"]: + assert tool["token"] == "" + +def test_list_tools_error_message_none_if_available(session_client: TestClient) -> None: + response = session_client.get("/v1/tools") + assert response.status_code == 200 + for tool in response.json(): + if tool["is_available"]: + assert tool["error_message"] is None + +def test_list_tools_with_agent( + session_client: TestClient, session: Session, user: User +) -> None: + agent = get_factory("Agent", session).create( + name="test agent", tools=[Tool.Wiki_Retriever_LangChain.value.ID], user=user + ) + + response = session_client.get("/v1/tools", params={"agent_id": agent.id}) + assert response.status_code == 200 + assert len(response.json()) == 1 + + tool = response.json()[0] + assert tool["name"] == Tool.Wiki_Retriever_LangChain.value.ID + + # get tool that has the same name as the tool in the response + tool_definition = get_available_tools().get(tool["name"]) + + for key in TOOL_DEFINITION_KEYS: + assert tool[key] == getattr(tool_definition, key) + +def test_list_tools_with_agent_that_doesnt_exist( + session_client: TestClient +) -> None: + response = session_client.get("/v1/tools", params={"agent_id": "fake_id"}) + assert response.status_code == 404 + assert response.json() == {"detail": "Agent with ID fake_id not found."} diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_user.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_user.py new file mode 100644 index 0000000000000000000000000000000000000000..833e533df5bf377a046a8a514b75af0639bfd15a --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/routers/test_user.py @@ -0,0 +1,167 @@ + +from fastapi.testclient import TestClient +from sqlalchemy.orm import Session + +from backend.database_models.user import User +from backend.services.auth import BasicAuthentication +from backend.tests.unit.factories import get_factory + + +def test_list_users_empty(session_client: TestClient, session: Session) -> None: + # Delete the default user + session.query(User).delete() + response = session_client.get("/v1/users") + results = response.json() + + assert response.status_code == 200 + assert len(results) == 0 + + +def test_list_users(session_client: TestClient, session: Session) -> None: + session.query(User).delete() + _ = get_factory("User", session).create(fullname="John Doe") + + response = session_client.get("/v1/users") + results = response.json() + + assert response.status_code == 200 + assert len(results) == 1 + + +def test_get_user(session_client: TestClient, session: Session) -> None: + user = get_factory("User", session).create(fullname="John Doe") + + response = session_client.get(f"/v1/users/{user.id}") + response_user = response.json() + + assert response.status_code == 200 + assert response_user["id"] == user.id + assert response_user["fullname"] == "John Doe" + + +def test_fail_get_nonexistent_user( + session_client: TestClient, session: Session +) -> None: + response = session_client.get("/v1/users/123") + + assert response.status_code == 404 + assert response.json() == {"detail": "User with ID: 123 not found."} + + +def test_create_user(session_client: TestClient, session: Session) -> None: + user_data_req = { + "fullname": "John Doe", + "email": "john@email.com", + } + + response = session_client.post("/v1/users", json=user_data_req) + response_user = response.json() + + user = session.get(User, response_user["id"]) + + assert response.status_code == 200 + assert response_user["fullname"] == user_data_req["fullname"] + assert response_user["email"] == user_data_req["email"] + assert user is not None + assert user.fullname == user_data_req["fullname"] + assert user.email == user_data_req["email"] + + +def test_create_user_with_password_saves_hashed_password( + session_client: TestClient, session: Session +) -> None: + user_data_req = { + "fullname": "John Doe", + "email": "john@email.com", + "password": "abcd", + } + + response = session_client.post("/v1/users", json=user_data_req) + response_user = response.json() + + user = session.get(User, response_user["id"]) + + assert response.status_code == 200 + assert response_user["fullname"] == user_data_req["fullname"] + assert response_user["email"] == user_data_req["email"] + assert user is not None + assert user.fullname == user_data_req["fullname"] + assert user.email == user_data_req["email"] + assert BasicAuthentication.check_password("abcd", user.hashed_password) + + +def test_fail_create_user_missing_fullname( + session_client: TestClient, session: Session +) -> None: + response = session_client.post("/v1/users", json={}) + response_user = response.json() + + assert response.status_code == 422 + assert response_user == { + "detail": [ + { + "type": "missing", + "loc": ["body", "fullname"], + "msg": "Field required", + "input": {}, + } + ] + } + + +def test_update_user(session_client: TestClient, session: Session) -> None: + user = get_factory("User", session).create(fullname="John Doe") + + response = session_client.put(f"/v1/users/{user.id}", json={"fullname": "new name"}) + response_user = response.json() + + assert response.status_code == 200 + assert response_user["fullname"] == "new name" + + # Check if user was updated + updated_user = session.get(User, user.id) + assert updated_user.fullname == "new name" + + +def test_update_user_password_saves_hashed_password( + session_client: TestClient, session: Session +) -> None: + user = get_factory("User", session).create(fullname="John Doe") + + response = session_client.put(f"/v1/users/{user.id}", json={"password": "abcd"}) + + assert response.status_code == 200 + # Check if user was updated + updated_user = session.get(User, user.id) + assert BasicAuthentication.check_password("abcd", updated_user.hashed_password) + + +def test_fail_update_nonexistent_user( + session_client: TestClient, session: Session +) -> None: + response = session_client.put("/v1/users/123", json={"fullname": "new name"}) + + assert response.status_code == 404 + assert response.json() == {"detail": "User with ID: 123 not found."} + + +def test_delete_user(session_client: TestClient, session: Session) -> None: + user = get_factory("User", session).create(fullname="John Doe") + + response = session_client.delete(f"/v1/users/{user.id}") + + assert response.status_code == 200 + assert response.json() == {} + + # Check if user was deleted + user = session.get(User, user.id) + assert user is None + + +def test_fail_delete_nonexistent_user( + session_client: TestClient, session: Session +) -> None: + response = session_client.delete("/v1/users/123") + + assert response.status_code == 404 + assert response.json() == {"detail": "User with ID: 123 not found."} diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/secrets.yaml b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/secrets.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7f6ed2bfacbde68b3adb639e920b131d27e86e2c --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/secrets.yaml @@ -0,0 +1,38 @@ +deployments: + cohere_platform: + api_key: + sagemaker: + access_key: + secret_key: + session_token: + azure: + api_key: + bedrock: + access_key: + secret_key: + session_token: +database: + # Migrate access token, used to authenticate requests to the migrate endpoint. + # You can generate it using some random string generator. + migrate_token: +tools: + web_search: + api_key: + wolfram_alpha: + app_id: + google_drive: + client_id: + client_secret: + developer_key: +auth: + secret_key: fake-secret-key + google_oauth: + client_id: + client_secret: + scim: + username: test_scim_user + password: test_scim_password + oidc: + client_id: + client_secret: + well_known_endpoint: diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/auth/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/auth/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/auth/strategies/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/auth/strategies/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/auth/strategies/test_basic.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/auth/strategies/test_basic.py new file mode 100644 index 0000000000000000000000000000000000000000..034c0a79bec32bdbcaab07ccde2c806b33f0ca1c --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/auth/strategies/test_basic.py @@ -0,0 +1,62 @@ +from unittest.mock import Mock, patch + +import bcrypt + +from backend.database_models.user import User +from backend.services.auth.strategies.basic import BasicAuthentication + + +def test_login(): + basic_auth = BasicAuthentication() + password = "test_password" + hashed_password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()) + test_user = Mock(spec=User) + test_user.id = 10 + test_user.email = "test@example.com" + test_user.fullname = "Test User" + test_user.hashed_password = hashed_password + session = Mock() + session.query.return_value.filter.return_value.first.return_value = test_user + + with patch("bcrypt.checkpw") as mock_checkpw: + result = basic_auth.login( + session, {"email": "test@example.com", "password": password} + ) + assert result == { + "email": "test@example.com", + "id": 10, + "fullname": "Test User", + } + mock_checkpw.assert_called_once_with(password.encode("utf-8"), hashed_password) + + +def test_login_user_not_found(): + basic_auth = BasicAuthentication() + session = Mock() + session.query.return_value.filter.return_value.first.return_value = None + + result = basic_auth.login( + session, {"email": "test@example.com", "password": "test_password"} + ) + assert result is None + + +def test_login_invalid_password(): + basic_auth = BasicAuthentication() + password = "test_password" + hashed_password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()) + test_user = Mock(spec=User) + test_user.id = 10 + test_user.email = "test@example.com" + test_user.fullname = "Test User" + test_user.hashed_password = hashed_password + session = Mock() + session.query.return_value.filter.return_value.first.return_value = test_user + + with patch("bcrypt.checkpw") as mock_checkpw: + mock_checkpw.return_value = False + result = basic_auth.login( + session, {"email": "test@example.com", "password": password} + ) + assert result is None + mock_checkpw.assert_called_once_with(password.encode("utf-8"), hashed_password) diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/auth/test_jwt.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/auth/test_jwt.py new file mode 100644 index 0000000000000000000000000000000000000000..6c2b7bf9bb8d5c104a3aa571010cb9fa3af0a605 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/auth/test_jwt.py @@ -0,0 +1,29 @@ +from unittest.mock import patch + +import jwt + +from backend.services.auth.jwt import JWTService + + +def test_decode_jwt(): + jwt_service = JWTService() + with patch("jwt.decode") as mock_jwt_decode: + mock_jwt_decode.return_value = {"context": "test_context", "jti": "test_jti"} + result = jwt_service.decode_jwt("fake_token") + assert result == {"context": "test_context", "jti": "test_jti"} + + +def test_decode_jwt_expired(): + jwt_service = JWTService() + with patch("jwt.decode") as mock_jwt_decode: + mock_jwt_decode.side_effect = jwt.ExpiredSignatureError + result = jwt_service.decode_jwt("fake_token") + assert result is None + + +def test_decode_jwt_invalid_token(): + jwt_service = JWTService() + with patch("jwt.decode") as mock_jwt_decode: + mock_jwt_decode.side_effect = jwt.InvalidTokenError + result = jwt_service.decode_jwt("fake_token") + assert result is None diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/auth/test_request_validators.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/auth/test_request_validators.py new file mode 100644 index 0000000000000000000000000000000000000000..c09278076d4bbfded4cecd4d305fed80ca60c1e8 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/auth/test_request_validators.py @@ -0,0 +1,60 @@ +from unittest.mock import Mock, patch + +import pytest +from fastapi import HTTPException +from sqlalchemy.orm import Session + +from backend.database_models.user import User +from backend.services.auth.request_validators import validate_authorization + + +def test_validate_authorization_with_valid_request(session: Session): + mock_user = Mock(spec=User) + mock_user.id = "user-name" + mock_user.active = True + + with patch( + "backend.services.auth.jwt.JWTService.decode_jwt", + return_value={"context": {"id": "user-name"}, "jti": "test_jti"}, + ): + with patch( + "backend.crud.user.get_user", + return_value=mock_user, + ): + result = validate_authorization(session, "Bearer fake_token") + assert isinstance(result, dict) + + +def test_validate_authorization_no_auth_header(session: Session): + with pytest.raises(HTTPException) as exc: + validate_authorization(session, None) + assert exc.value.status_code == 401 + + +def test_validate_authorization_invalid_auth_header(session: Session): + with pytest.raises(HTTPException) as exc: + validate_authorization(session, "fake_token") + assert exc.value.status_code == 401 + + +def test_validate_invalid_jwt(session: Session): + with patch( + "backend.services.auth.jwt.JWTService.decode_jwt", + return_value=None, + ): + with pytest.raises(HTTPException) as exc: + validate_authorization(session, "Bearer fake_token") + assert exc.value.status_code == 401 + + +def test_validate_blacklisted_token(): + session = Mock(spec=Session) + session.query.return_value.filter.return_value.first.return_value = Mock() + + with patch( + "backend.services.auth.jwt.JWTService.decode_jwt", + return_value={"context": "test_context", "jti": "test_jti"}, + ): + with pytest.raises(HTTPException) as exc: + validate_authorization(session, "Bearer fake_token") + assert exc.value.status_code == 401 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/test_cache.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/test_cache.py new file mode 100644 index 0000000000000000000000000000000000000000..67f004159c7a549eac8723cc863197b085f62d1b --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/test_cache.py @@ -0,0 +1,14 @@ +import pytest + +from backend.config.settings import Settings +from backend.services.cache import get_client + +# skip if redis is not available +is_redis_env_set = Settings().get('redis.url') + + +@pytest.mark.skipif(not is_redis_env_set, reason="Redis is not set") +def test_redis_client(): + redis = get_client() + + assert redis.ping() is True diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/test_chat.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/test_chat.py new file mode 100644 index 0000000000000000000000000000000000000000..121c87a0f6057d2b3c1db5dbd9ca930e95c308ee --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/test_chat.py @@ -0,0 +1,163 @@ + +import pytest + +from backend.schemas.chat import EventState +from backend.schemas.context import Context +from backend.services.chat import ( + DEATHLOOP_SIMILARITY_THRESHOLDS, + are_previous_actions_similar, + check_death_loop, + check_similarity, +) + + +def test_are_previous_actions_similar(): + distances = [ + 0.5, + 0.6, + 0.8, + 0.9, + 1.0, + ] + + assert are_previous_actions_similar(distances, 0.7, 3) + + +def test_are_previous_actions_not_similar(): + distances = [ + 0.1, + 0.1, + 0.1, + 0.1, + ] + assert not are_previous_actions_similar(distances, 0.7, 3) + + +def test_check_similarity(): + ctx = Context() + distances = [ + 0.5, + 0.6, + 0.8, + 0.9, + 1.0, + ] + + response = check_similarity(distances, ctx) + assert response + + +def test_check_similarity_no_death_loop(): + ctx = Context() + distances = [ + 0.1, + 0.1, + 0.1, + 0.1, + ] + + response = check_similarity(distances, ctx) + assert not response + + +def test_check_similarity_not_enough_data(): + ctx = Context() + distances = [ + 0.1, + 0.1, + ] + + response = check_similarity(distances, ctx) + assert not response + + +@pytest.mark.skip(reason="We are supressing the exception while experimenting") +def test_check_death_loop_raises_on_plan(): + ctx = Context() + event = { + "text": "This is also a plan", + "tool_calls": [], + } + + event_state = EventState( + distances_plans=[ + 0.1, + 0.8, + 0.9, + ], + distances_actions=[ + 0.1, + 0.1, + 0.1, + ], + previous_plan="This is a plan", + previous_action="[]", + ) + + with pytest.raises(Exception): + check_death_loop(event, event_state, ctx) + + +@pytest.mark.skip(reason="We are supressing the exception while experimenting") +def test_check_death_loop_raises_on_action(): + ctx = Context() + event = { + "text": "This is a plan", + "tool_calls": [{"tool": "tool1", "args": ["This is an argument"]}], + } + + event_state = EventState( + distances_plans=[ + 0.1, + 0.1, + 0.1, + ], + distances_actions=[ + 0.1, + 0.8, + 0.9, + ], + previous_plan="Nothing like the previous plan", + previous_action="[{'tool': 'tool1', 'args': ['This is an argument']}]", + ) + + with pytest.raises(Exception): + check_death_loop(event, event_state, ctx) + + +def test_check_no_death_loop(): + ctx = Context() + event = { + "text": "Nothing like the previous plan", + "tool_calls": [ + {"tool": "different_tool", "args": ["Nothing like the previous action"]} + ], + } + + event_state = EventState( + distances_plans=[ + 0.1, + 0.1, + 0.1, + ], + distances_actions=[ + 0.1, + 0.1, + 0.1, + ], + previous_plan="This is a plan", + previous_action='[{"tool": "tool1", "args": ["This is an argument"]}]', + ) + + new_event_state = check_death_loop(event, event_state, ctx) + assert new_event_state.previous_plan == "Nothing like the previous plan" + assert ( + new_event_state.previous_action + == '[{"tool": "different_tool", "args": ["Nothing like the previous action"]}]' + ) + + assert len(new_event_state.distances_plans) == 4 + assert len(new_event_state.distances_actions) == 4 + + assert new_event_state.distances_plans[-1] < max(DEATHLOOP_SIMILARITY_THRESHOLDS) + assert new_event_state.distances_actions[-1] < max(DEATHLOOP_SIMILARITY_THRESHOLDS) diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/test_deployment.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/test_deployment.py new file mode 100644 index 0000000000000000000000000000000000000000..183acd72099e32779f8d60877a6d14a8167220a1 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/test_deployment.py @@ -0,0 +1,134 @@ +from unittest.mock import patch + +import pytest + +import backend.services.deployment as deployment_service +from backend.config.tools import Tool +from backend.database_models import Deployment +from backend.exceptions import DeploymentNotFoundError, NoAvailableDeploymentsError +from backend.schemas.deployment import DeploymentDefinition +from backend.tests.unit.model_deployments.mock_deployments import ( + MockAzureDeployment, + MockBedrockDeployment, + MockCohereDeployment, + MockSageMakerDeployment, + MockSingleContainerDeployment, +) + + +@pytest.fixture +def clear_db_deployments(session): + session.query(Deployment).delete() + session.commit() + +@pytest.fixture +def db_deployment(session): + session.query(Deployment).delete() + mock_cohere_deployment = Deployment( + name=MockCohereDeployment.name(), + description="A mock Cohere deployment from the DB", + deployment_class_name=MockCohereDeployment.__name__, + is_community=False, + default_deployment_config={"COHERE_API_KEY": "db-test-api-key"}, + id="db-mock-cohere-platform-id", + ) + session.add(mock_cohere_deployment) + session.commit() + return mock_cohere_deployment + +def test_all_tools_have_id() -> None: + for tool in Tool: + assert tool.value.ID is not None + +def test_get_default_deployment_none_available() -> None: + with patch("backend.services.deployment.AVAILABLE_MODEL_DEPLOYMENTS", {}): + with pytest.raises(NoAvailableDeploymentsError): + deployment_service.get_default_deployment_instance() + +def test_get_default_deployment_no_settings(mock_available_model_deployments) -> None: + assert isinstance(deployment_service.get_default_deployment_instance(), MockCohereDeployment) + +def test_get_default_deployment_with_settings(mock_available_model_deployments) -> None: + with patch("backend.config.settings.Settings.get", return_value="azure") as mock_settings: + assert isinstance(deployment_service.get_default_deployment_instance(), MockAzureDeployment) + mock_settings.assert_called_once_with("deployments.default_deployment") + +def test_get_deployment(session, mock_available_model_deployments, db_deployment) -> None: + deployment = deployment_service.get_deployment_instance_by_id(session, db_deployment.id) + assert isinstance(deployment, MockCohereDeployment) + +def test_get_deployment_by_name(session, mock_available_model_deployments, clear_db_deployments) -> None: + deployment = deployment_service.get_deployment_instance_by_name(session, MockCohereDeployment.name()) + assert isinstance(deployment, MockCohereDeployment) + +def test_get_deployment_by_name_wrong_name(session, mock_available_model_deployments) -> None: + with pytest.raises(DeploymentNotFoundError): + deployment_service.get_deployment_instance_by_name(session, "wrong-name") + +def test_get_deployment_definition(session, mock_available_model_deployments, db_deployment) -> None: + definition = deployment_service.get_deployment_definition(session, "db-mock-cohere-platform-id") + assert definition == DeploymentDefinition.from_db_deployment(db_deployment) + +def test_get_deployment_definition_wrong_id(session, mock_available_model_deployments) -> None: + with pytest.raises(DeploymentNotFoundError): + deployment_service.get_deployment_definition(session, "wrong-id") + +def test_get_deployment_definition_no_db_deployments(session, mock_available_model_deployments, clear_db_deployments) -> None: + definition = deployment_service.get_deployment_definition(session, MockCohereDeployment.id()) + assert definition == MockCohereDeployment.to_deployment_definition() + +def test_get_deployment_definition_by_name(session, mock_available_model_deployments, db_deployment) -> None: + definition = deployment_service.get_deployment_definition_by_name(session, db_deployment.name) + assert definition == DeploymentDefinition.from_db_deployment(db_deployment) + +def test_get_deployment_definition_by_name_no_db_deployments(session, mock_available_model_deployments, clear_db_deployments) -> None: + definition = deployment_service.get_deployment_definition_by_name(session, MockCohereDeployment.name()) + mock = MockCohereDeployment.to_deployment_definition() + assert definition.name == mock.name + assert definition.models == mock.models + assert definition.class_name == mock.class_name + assert definition.config == mock.config + + +def test_get_deployment_definition_by_name_wrong_name(session, mock_available_model_deployments) -> None: + with pytest.raises(DeploymentNotFoundError): + deployment_service.get_deployment_definition_by_name(session, "wrong-name") + +def test_get_deployment_definitions_no_db_deployments(session, mock_available_model_deployments, clear_db_deployments) -> None: + definitions = deployment_service.get_deployment_definitions(session) + + assert len(definitions) == 5 + assert all(isinstance(d, DeploymentDefinition) for d in definitions) + assert all(d.name in [MockAzureDeployment.name(), MockCohereDeployment.name(), MockSageMakerDeployment.name(), MockBedrockDeployment.name(), MockSingleContainerDeployment.name()] for d in definitions) + +def test_get_deployment_definitions_with_db_deployments(session, mock_available_model_deployments, db_deployment) -> None: + mock_cohere_deployment = Deployment( + name=MockCohereDeployment.name(), + description="A mock Cohere deployment from the DB", + deployment_class_name=MockCohereDeployment.__name__, + is_community=False, + default_deployment_config={"COHERE_API_KEY": "db-test-api-key"}, + id="db-mock-cohere-platform-id", + ) + with patch("backend.crud.deployment.get_deployments", return_value=[mock_cohere_deployment]): + with patch( + "backend.services.deployment.AVAILABLE_MODEL_DEPLOYMENTS", + { MockCohereDeployment.name(): MockCohereDeployment, MockAzureDeployment.name(): MockAzureDeployment } + ): + definitions = deployment_service.get_deployment_definitions(session) + + assert len(definitions) == 2 + assert all(isinstance(d, DeploymentDefinition) for d in definitions) + assert all(d.name in [MockAzureDeployment.name(), MockCohereDeployment.name()] for d in definitions) + assert any(d.id == "db-mock-cohere-platform-id" for d in definitions) + +def test_update_config_db(session, db_deployment) -> None: + deployment_service.update_config(session, db_deployment.id, {"COHERE_API_KEY": "new-db-test-api-key"}) + updated_deployment = session.query(Deployment).get("db-mock-cohere-platform-id") + assert updated_deployment.default_deployment_config == {"COHERE_API_KEY": "new-db-test-api-key"} + +def test_update_config_no_db_deployments(session, mock_available_model_deployments, clear_db_deployments) -> None: + with patch("backend.services.deployment.update_env_file") as mock_update_env_file: + with patch("backend.services.deployment.get_deployment_definition", return_value=MockCohereDeployment.to_deployment_definition()): + deployment_service.update_config(session, "some-deployment-id", {"API_KEY": "new-api-key"}) + mock_update_env_file.assert_called_with({"API_KEY": "new-api-key"}) diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/test_tools.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/services/test_tools.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/26mb.pdf b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/26mb.pdf new file mode 100644 index 0000000000000000000000000000000000000000..3080ec793e01947850d07109afc6d7cf62b676e1 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/26mb.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7301587fbf47590f4671ec69998193f948820569fb5f61cb91067c65017e9ce4 +size 26091554 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Cardistry.pdf b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Cardistry.pdf new file mode 100644 index 0000000000000000000000000000000000000000..e745009f6b9454190921a6a585e52e19a51da8ed --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Cardistry.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:723a66ec6836a25a6ece77de8ab95d42040bb10f4ef4210d305b0200e3e765a7 +size 353224 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Mariana_Trench.pdf b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Mariana_Trench.pdf new file mode 100644 index 0000000000000000000000000000000000000000..deb5835abce7ccf67c8d9d56567a34d1516e686a --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Mariana_Trench.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0f855d60952c5738bc42ce3b25c5ca2d7a085a332c6acd2e3074eb2716680ed0 +size 567431 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Mount_Everest.pdf b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Mount_Everest.pdf new file mode 100644 index 0000000000000000000000000000000000000000..00a6ccb652edba3e9e5fc67ac1eacf07ec3df6d3 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Mount_Everest.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:91812e38bfc8feee02961d4c23fc435d7f3dab206176571786fc29539524f911 +size 2627740 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Tapas.pdf b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Tapas.pdf new file mode 100644 index 0000000000000000000000000000000000000000..01604fd286fce4d18c6fe7821ec020d7806c5432 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Tapas.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:024cb7e009cf3b8e2a715a74928401ccb9a6b733d9886b51ec644dfc092ec7c9 +size 438733 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/tools/test_calculator.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/tools/test_calculator.py new file mode 100644 index 0000000000000000000000000000000000000000..a223c35dd869147691a975c8ef7448a495bf3d39 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/tools/test_calculator.py @@ -0,0 +1,21 @@ +import pytest + +from backend.schemas.context import Context +from backend.tools import Calculator + + +@pytest.mark.asyncio +async def test_calculator() -> None: + ctx = Context() + calculator = Calculator() + result = await calculator.call({"code": "2+2"}, ctx) + assert result == {"text": 4} + + +@pytest.mark.asyncio +async def test_calculator_invalid_syntax() -> None: + ctx = Context() + calculator = Calculator() + result = await calculator.call({"code": "2+"}, ctx) + + assert result == [{'details': 'parse error [column 2]: parity, expression: 2+', 'success': False, 'text': 'Error calling tool toolkit_calculator.', 'type': 'other'}] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/tools/test_collate.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/tools/test_collate.py new file mode 100644 index 0000000000000000000000000000000000000000..43d894aa8002a45726769e7a50980bd544c90ab4 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/tools/test_collate.py @@ -0,0 +1,112 @@ +import pytest + +from backend.chat import collate +from backend.schemas.context import Context +from backend.tests.unit.model_deployments.mock_deployments import MockCohereDeployment + + +@pytest.mark.asyncio +async def test_rerank() -> None: + model = MockCohereDeployment(model_config={}) + outputs = [ + { + "text": "Mount Everest is Earth's highest mountain above sea level, located in the Mahalangur Himal sub-range of the Himalayas" + }, + { + "text": "There are four components of blood: red blood cells, white blood cells, plasma and platelets." + }, + { + "text": "'My Man Rocks Me (with One Steady Roll)' by Trixie Smith was issued in 1922, the first record to refer to 'rocking' and 'rolling' in a secular context" + }, + ] + tool_results = [ + { + "call": { + "parameters": {"query": "highest mountain"}, + "name": "retriever", + }, + "outputs": outputs, + }, + { + "call": { + "parameters": {"query": "four components of blood"}, + "name": "retriever", + }, + "outputs": outputs, + }, + { + "call": { + "parameters": {"query": "When was 1st Olympics in history?"}, + "name": "retriever", + }, + "outputs": outputs, + }, + ] + + expected_output = [ + { + "call": { + "parameters": {"query": "highest mountain"}, + "name": "retriever", + }, + "outputs": [outputs[0]], + }, + { + "call": { + "parameters": {"query": "four components of blood"}, + "name": "retriever", + }, + "outputs": [outputs[1]], + }, + { + "call": { + "parameters": {"query": "When was 1st Olympics in history?"}, + "name": "retriever", + }, + "outputs": [], + }, + ] + output = await collate.rerank_and_chunk(tool_results, model, Context()) + assert output == expected_output + + +def test_chunk_normal_mode() -> None: + content = "This is a test. We are testing the chunk function." + expected_output = ["This is a test.", "We are testing the chunk function."] + output = collate.chunk(content, False, 3, 10) + assert output == expected_output + + +def test_chunk_compact_mode() -> None: + content = "This is a test.\nWe are testing the chunk function." + expected_output = ["This is a test.", "We are testing the chunk function."] + output = collate.chunk(content, True, 3, 10) + assert output == expected_output + + +def test_chunk_hard_cut_off() -> None: + content = "This is a test. We are testing the chunk function. This sentence will exceed the hard cut off." + expected_output = [ + "This is a test. We are testing the chunk function.", + "This sentence will exceed the hard cut off.", + ] + output = collate.chunk(content, False, 11, 10) + assert output == expected_output + + +def test_chunk_soft_cut_off() -> None: + content = "This is a test. We are testing the chunk function. This sentence will exceed the soft cut off." + expected_output = [ + "This is a test.", + "We are testing the chunk function.", + "This sentence will exceed the soft cut off.", + ] + output = collate.chunk(content, False, 3, 10) + assert output == expected_output + + +def test_chunk_empty_content() -> None: + content = "" + expected_output = [] + output = collate.chunk(content, False, 3, 10) + assert output == expected_output diff --git a/western_ai/canada/cohere-toolkit/src/backend/tests/unit/tools/test_lang_chain.py b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/tools/test_lang_chain.py new file mode 100644 index 0000000000000000000000000000000000000000..5c2aab607984b8ddf1aa0ff105300524c721f88c --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tests/unit/tools/test_lang_chain.py @@ -0,0 +1,171 @@ +from unittest.mock import MagicMock, patch + +import pytest +from langchain_core.documents.base import Document + +from backend.services.context import Context +from backend.tools import LangChainVectorDBRetriever, LangChainWikiRetriever +from backend.tools.base import ToolError, ToolErrorCode + + +@pytest.mark.asyncio +async def test_wiki_retriever() -> None: + ctx = Context() + retriever = LangChainWikiRetriever() + query = "Python programming" + mock_docs = [ + Document( + page_content="example content", + metadata={ + "title": "example title", + "summary": "example summary", + "source": "https://example.com", + }, + ), + Document( + page_content="example content 2", + metadata={ + "title": "example title 2", + "summary": "example summary 2", + "source": "https://example.com/2", + }, + ), + ] + expected_docs = [ + { + "text": "example content", + "title": "example title", + "url": "https://example.com", + }, + { + "text": "example content 2", + "title": "example title 2", + "url": "https://example.com/2", + }, + ] + + wiki_retriever_mock = MagicMock() + wiki_retriever_mock.get_relevant_documents.return_value = mock_docs + + with patch( + "backend.tools.lang_chain.WikipediaRetriever", + return_value=wiki_retriever_mock, + ): + result = await retriever.call({"query": query}, ctx) + + assert result == expected_docs + + +@pytest.mark.asyncio +async def test_wiki_retriever_no_docs() -> None: + ctx = Context() + retriever = LangChainWikiRetriever() + query = "Python programming" + mock_docs = [] + + wiki_retriever_mock = MagicMock() + wiki_retriever_mock.get_relevant_documents.return_value = mock_docs + + with patch( + "backend.tools.lang_chain.WikipediaRetriever", + return_value=wiki_retriever_mock, + ): + result = await retriever.call({"query": query}, ctx) + + expected_error = ToolError( + type=ToolErrorCode.OTHER, + success=False, + text='No results found.', + details='No results found for the given params.' + ).model_dump() + assert result == [expected_error] + + + +@pytest.mark.asyncio +async def test_vector_db_retriever() -> None: + ctx = Context() + file_path = "src/backend/tests/unit/test_data/Mariana_Trench.pdf" + retriever = LangChainVectorDBRetriever(file_path) + query = "What is the mariana trench?" + mock_docs = [ + Document( + page_content="Location of the Mariana TrenchMariana Trench", + metadata={ + "page": 0, + "source": "src/backend/tests/unit/test_data/Mariana_Trench.pdf", + }, + ), + Document( + page_content="Like other oceanic trenches, the Mariana Trench has been propos ed as a site for nuclear waste", + metadata={ + "page": 4, + "source": "src/backend/tests/unit/test_data/Mariana_Trench.pdf", + }, + ), + Document( + page_content="The Pacific plate is subducted beneath the Mariana Plate", + metadata={ + "page": 1, + "source": "src/backend/tests/unit/test_data/Mariana_Trench.pdf", + }, + ), + Document( + page_content='37. "Vityaz-D explored Mariana Trench according to preinstalled program — developer"', + metadata={ + "page": 7, + "source": "src/backend/tests/unit/test_data/Mariana_Trench.pdf", + }, + ), + ] + expected_docs = [ + {"text": "Location of the Mariana TrenchMariana Trench"}, + { + "text": "Like other oceanic trenches, the Mariana Trench has been propos ed as a site for nuclear waste" + }, + {"text": "The Pacific plate is subducted beneath the Mariana Plate"}, + { + "text": '37. "Vityaz-D explored Mariana Trench according to preinstalled program — developer"' + }, + ] + + db_get_relevant_docs_mock = MagicMock() + db_get_relevant_docs_mock.get_relevant_docs.return_value = mock_docs + + with patch( + "langchain_community.vectorstores.Chroma.from_documents" + ) as mock_from_documents: + mock_db = MagicMock() + mock_from_documents.return_value = mock_db + mock_db.as_retriever().get_relevant_documents.return_value = mock_docs + result = await retriever.call({"query": query}, ctx) + + assert result == expected_docs + + +@pytest.mark.asyncio +async def test_vector_db_retriever_no_docs() -> None: + ctx = Context() + file_path = "src/backend/tests/unit/test_data/Mariana_Trench.pdf" + retriever = LangChainVectorDBRetriever(file_path) + query = "What is the mariana trench?" + mock_docs = [] + + db_get_relevant_docs_mock = MagicMock() + db_get_relevant_docs_mock.get_relevant_docs.return_value = mock_docs + + with patch( + "langchain_community.vectorstores.Chroma.from_documents" + ) as mock_from_documents: + mock_db = MagicMock() + mock_from_documents.return_value = mock_db + mock_db.as_retriever().get_relevant_documents.return_value = mock_docs + result = await retriever.call({"query": query}, ctx) + + expected_error = ToolError( + type=ToolErrorCode.OTHER, + success=False, + text='No results found.', + details='No results found for the given params.' + ).model_dump() + assert result == [expected_error] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tools/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..9f6281a702b7b27c813d5779090fb70b9326f27c --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/__init__.py @@ -0,0 +1,38 @@ +from backend.tools.brave_search import BraveWebSearch +from backend.tools.calculator import Calculator +from backend.tools.files import ReadFileTool, SearchFileTool +from backend.tools.github import GithubAuth, GithubTool +from backend.tools.gmail import GmailAuth, GmailTool +from backend.tools.google_drive import GoogleDrive, GoogleDriveAuth +from backend.tools.google_search import GoogleWebSearch +from backend.tools.hybrid_search import HybridWebSearch +from backend.tools.lang_chain import LangChainVectorDBRetriever, LangChainWikiRetriever +from backend.tools.python_interpreter import PythonInterpreter +from backend.tools.sharepoint import SharepointAuth, SharepointTool +from backend.tools.slack import SlackAuth, SlackTool +from backend.tools.tavily_search import TavilyWebSearch +from backend.tools.web_scrape import WebScrapeTool + +__all__ = [ + "Calculator", + "PythonInterpreter", + "LangChainVectorDBRetriever", + "LangChainWikiRetriever", + "ReadFileTool", + "SearchFileTool", + "GoogleDrive", + "GoogleDriveAuth", + "WebScrapeTool", + "TavilyWebSearch", + "BraveWebSearch", + "GoogleWebSearch", + "HybridWebSearch", + "SlackTool", + "SlackAuth", + "GmailTool", + "GmailAuth", + "SharepointTool", + "SharepointAuth", + "GithubTool", + "GithubAuth", +] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/base.py b/western_ai/canada/cohere-toolkit/src/backend/tools/base.py new file mode 100644 index 0000000000000000000000000000000000000000..21b6ab03a005bc1d59ac30839ba94b0240506669 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/base.py @@ -0,0 +1,273 @@ +import datetime +from abc import ABC, abstractmethod +from enum import StrEnum +from typing import Any + +import requests +from fastapi import Request +from pydantic import BaseModel + +from backend.config.settings import Settings +from backend.crud import tool_auth as tool_auth_crud +from backend.database_models.database import DBSessionDep +from backend.database_models.tool_auth import ToolAuth +from backend.metrics import track_tool_call_time +from backend.schemas.context import Context +from backend.schemas.tool import ToolDefinition +from backend.services.logger.utils import LoggerFactory +from backend.tools.utils.tools_checkers import check_tool_parameters + +logger = LoggerFactory().get_logger() + +class ToolErrorCode(StrEnum): + HTTP_ERROR = "http_error" + AUTH = "auth" + OTHER = "other" + + +class ToolAuthException(Exception): + def __init__(self, message, tool_id: str): + self.message = message + self.tool_id = tool_id + +class ToolError(BaseModel, extra="allow"): + type: ToolErrorCode = ToolErrorCode.OTHER + success: bool = False + text: str + details: str = "" + +class ToolArgument(StrEnum): + DOMAIN_FILTER = "domain_filter" + SITE_FILTER = "site_filter" + +class ParametersValidationMeta(type): + """ + Metaclass to decorate all tools `call` methods with the parameter checker. + """ + def __new__(cls, name, bases, class_dict): + for attr_name, attr_value in class_dict.items(): + if callable(attr_value) and attr_name == "call": + metrics_enabled = Settings().get('metrics.enabled') + if metrics_enabled: + # Decorate methods with the metrics collector and parameter checker + class_dict[attr_name] = track_tool_call_time()( + check_tool_parameters(lambda self: self.__class__.get_tool_definition())(attr_value)) + else: + # Decorate methods with the parameter checker + class_dict[attr_name] = check_tool_parameters(lambda self: self.__class__.get_tool_definition())(attr_value) + return super().__new__(cls, name, bases, class_dict) + + +class ToolDefaultPreambleRegistry: + """ + Registry to store default preamble values for tools. + Currently, it's implemented as a class with predefined values, but we can use set_preamble to set values. + """ + _default_preambles = { + "toolkit_python_interpreter": "If you decide to use toolkit_python_interpreter tool and are going to plot something, try returning result as a png. Ensure that the generated code does not include any internet connection.", + "read_file": "When using the read_file tool, always ensure that the file parameter is prepared as a tuple in the format (filename, file ID). The order of the tuple fields is critical. ", + "search_file":"When using the search_file tool, always ensure that the `files` parameter is prepared as a list of tuples in the format (filename, file ID). The order of the tuple fields is critical. " + } + + @classmethod + def set_preamble(cls, tool_id, preamble): + """ + Register a default preamble for a tool class. This method can be used to set a default preamble for a tool class. + """ + cls._default_preambles[tool_id] = preamble + + @classmethod + def get_preamble(cls, tool_class_id): + """ + Retrieve the default preamble for a given tool class. + """ + return cls._get_class_specific_preamble(tool_class_id) or cls._default_preambles.get(tool_class_id, None) + + @classmethod + def _get_class_specific_preamble(cls, tool_class_id): + """ + Retrieve the custom preamble for a given tool class. + """ + if tool_class_id == "toolkit_python_interpreter": + interpreter_url = Settings().get('tools.python_interpreter.url') + if interpreter_url: + try: + # TODO Confirm with TJ if we need to add this settings here or set predefined values on the interpreter side + forbidden_packages = Settings().get('tools.python_interpreter.forbidden_packages') + if forbidden_packages: + forbidden_response = requests.post(interpreter_url + "/forbidden-packages", json={"packages": forbidden_packages}) + if forbidden_response.status_code != 200: + return None + forbidden_preamble_packages = ", ".join(forbidden_packages) + # TODO Ask TJ do we need available packages + # available_response = requests.get(interpreter_url + "/available-packages") + # if available_response.status_code != 200: + # return None + # available_packages = available_response.json()["packages"] + # available_cleaned = [item for item in available_packages if item not in forbidden_packages] + # available_preamble_packages = ", ".join(available_cleaned) + preamble = f"If you decide to use the toolkit_python_interpreter tool and plan to plot something, try returning the result as a PNG. Ensure that the generated code does not require an internet connection. Avoid using the following packages in code generation: {forbidden_preamble_packages}. " + return preamble + except Exception as e: + logger.error(event=f"Error while retrieving the Python interpreter preamble: {str(e)}") + return None + + return None + + +class BaseTool(metaclass=ParametersValidationMeta): + """ + Abstract base class for all Tools. + + Attributes: + ID (str): The name of the tool. + """ + ID = None + TOOL_DEFAULT_PREAMBLE = None + + def __init_subclass__(cls, **kwargs): + """ + Automatically set TOOL_DEFAULT_PREAMBLE for child classes + using the registry class. + """ + super().__init_subclass__(**kwargs) + cls.TOOL_DEFAULT_PREAMBLE = ToolDefaultPreambleRegistry.get_preamble(cls.ID) + + def __init__(self, *args, **kwargs): + self._post_init_check() + + def _post_init_check(self): + if self.ID is None: + raise ValueError(f"{self.__name__} must have ID attribute defined.") + + @classmethod + @abstractmethod + def is_available(cls) -> bool: + ... + + @classmethod + @abstractmethod + def get_tool_definition(cls) -> ToolDefinition: + ... + + @classmethod + def generate_error_message(cls) -> str | None: + if cls.is_available(): + return None + + return f"{cls.__name__} is not available. Please make sure all required config variables are set." + + @classmethod + def _handle_tool_specific_errors(cls, error: Exception, **kwargs: Any) -> None: + ... + + @classmethod + def get_tool_error( + cls, details: str, text: str = "Error calling tool", error_type: ToolErrorCode = ToolErrorCode.OTHER, + ) -> list[dict[str, str]]: + tool_error = ToolError(text=f"{text} {cls.ID}.", details=details, type=error_type).model_dump() + logger.error(event=f"Error calling tool {cls.ID}", error=tool_error) + return [tool_error] + + @classmethod + def get_no_results_error(cls) -> list[dict[str, str]]: + tool_error = ToolError(text="No results found.", details="No results found for the given params.").model_dump() + return [tool_error] + + @abstractmethod + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any, + ) -> list[dict[str, Any]]: + ... + + @classmethod + def get_preamble(cls): + return cls.TOOL_DEFAULT_PREAMBLE + + @classmethod + def set_preamble(cls, preamble): + cls.TOOL_DEFAULT_PREAMBLE = preamble + +class BaseToolAuthentication(ABC): + """ + Abstract base class for Tool Authentication. + """ + + def __init__(self, *args, **kwargs): + self.BACKEND_HOST = Settings().get('auth.backend_hostname') + self.FRONTEND_HOST = Settings().get('auth.frontend_hostname') + self.AUTH_SECRET_KEY = Settings().get('auth.secret_key') + + self._post_init_check() + + def _post_init_check(self): + if any( + [ + self.BACKEND_HOST is None, + self.FRONTEND_HOST is None, + self.AUTH_SECRET_KEY is None, + ] + ): + raise ValueError( + "Tool Authentication requires auth.backend_hostname, auth.frontend_hostname in configuration.yaml, " + "and auth.secret_key in the secrets.yaml configuration files." + ) + + @abstractmethod + def get_auth_url(self, user_id: str) -> str: + ... + + def is_auth_required(self, session: DBSessionDep, user_id: str) -> bool: + auth = tool_auth_crud.get_tool_auth(session, self.TOOL_ID, user_id) + + # Check Auth DNE + if auth is None: + return True + + # Check expired + if datetime.datetime.now() > auth.expires_at: + if self.try_refresh_token(session, user_id, auth): + # Refreshed token successfully + return False + + # Refresh failed, delete existing Auth + tool_auth_crud.delete_tool_auth(session, user_id, self.TOOL_ID) + return True + + # Check access_token is retrievable + try: + auth.access_token + auth.refresh_token + except Exception(): + # Retrieval failed, delete existing Auth + tool_auth_crud.delete_tool_auth(session, user_id, self.TOOL_ID) + return True + + # ToolAuth retrieved and is not expired + return False + + @abstractmethod + def try_refresh_token( + self, session: DBSessionDep, user_id: str, tool_auth: ToolAuth + ) -> bool: + ... + + @abstractmethod + def retrieve_auth_token( + self, request: Request, session: DBSessionDep, user_id: str + ) -> str|None: + ... + + def get_token(self, session: DBSessionDep, user_id: str) -> str: + tool_auth = tool_auth_crud.get_tool_auth(session, self.TOOL_ID, user_id) + return tool_auth.access_token if tool_auth else None + + def delete_tool_auth(self, session: DBSessionDep, user_id: str) -> bool: + try: + tool_auth_crud.delete_tool_auth(session, user_id, self.TOOL_ID) + return True + except Exception as e: + logger.error( + event=f"BaseToolAuthentication: Error while deleting Tool Auth: {str(e)}" + ) + raise diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/brave_search/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tools/brave_search/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..51a6f993bad3a32f6892f653fe4f05b72eb1837a --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/brave_search/__init__.py @@ -0,0 +1,4 @@ +from backend.tools.brave_search.client import BraveClient +from backend.tools.brave_search.tool import BraveWebSearch + +__all__ = ["BraveWebSearch", "BraveClient"] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/brave_search/client.py b/western_ai/canada/cohere-toolkit/src/backend/tools/brave_search/client.py new file mode 100644 index 0000000000000000000000000000000000000000..d7e7cad044ed58a032c63439b2dea4ee2f826f9c --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/brave_search/client.py @@ -0,0 +1,304 @@ +import os +from enum import StrEnum +from typing import Dict, Optional + +import httpx +import requests +from pydantic import BaseModel +from tenacity import AsyncRetrying, stop_after_attempt, wait_fixed + + +class SafeSearchTypes(StrEnum): + OFF = "off" + MODERATE = "moderate" + STRICT = "strict" + + +class WebSearchResponse(BaseModel): + """ + Web search response model. + """ + + type: str = "search" + discussions: Dict = {} + faq: Dict = {} + infobox: Dict = {} + locations: Dict = {} + mixed: Dict = {} + news: Dict = {} + query: Dict = {} + videos: Dict = {} + web: Dict = {} + summarizer: Dict = {} + + +class BraveApiException(Exception): + """ + Custom exception for Brave API errors. + """ + + pass + + +class BraveClient: + BASE_URL = "https://api.search.brave.com/res/v1/" + WEB_SEARCH_API_ENDPOINT = "web/search" + MAX_RESULTS = 20 + MAX_OFFSET = 9 + WAIT_RETRY_SECONDS = 2 + RETRY_ATTEMPTS = 2 + + def __init__(self, api_key: Optional[str] = None) -> None: + self.api_key = api_key if api_key else os.environ.get("BRAVE_API_KEY", None) + self.web_search_endpoint = f"{self.BASE_URL}{self.WEB_SEARCH_API_ENDPOINT}" + + def _get_headers(self) -> Dict: + """ + Construct the request headers with the API key. + """ + return { + "Accept": "application/json", + "Accept-Encoding": "gzip", + "X-Subscription-Token": self.api_key, + } + + def _prepare_params( + self, + q: str, + country: Optional[str] = None, + search_lang: Optional[str] = None, + ui_lang: Optional[str] = None, + count: Optional[int] = MAX_RESULTS, + offset: Optional[int] = 0, + safesearch: Optional[str] = SafeSearchTypes.MODERATE, + freshness: Optional[str] = None, + text_decorations: Optional[bool] = False, + spellcheck: Optional[bool] = True, + goggles_id: Optional[str] = None, + extra_snippets: Optional[bool] = False, + include_domains: Optional[list[str]] = [], + ) -> Dict: + """ + Prepare the query parameters for the API request. + """ + # Validate the query parameter - limits set by the API endpoint + # see https://api.search.brave.com/app/documentation/web-search/query + if not q or len(q) > 400 or len(q.split()) > 50: + raise ValueError("Invalid query parameter 'q'") + + # Construct the query parameters - see here for more details: + # https://search.brave.com/help/operators + if include_domains: + q_domains = " OR ".join([f"site:{item}" for item in include_domains]) + q = f"{q} {q_domains}" + + params = { + "q": q, + "country": country, + "search_lang": search_lang, + "ui_lang": ui_lang, + "count": min(count, self.MAX_RESULTS), + "offset": min(offset, self.MAX_OFFSET), + "safesearch": safesearch, + "freshness": freshness, + "text_decorations": text_decorations, + "spellcheck": spellcheck, + "goggles_id": goggles_id, + "extra_snippets": extra_snippets, + } + # Filter None values + params = {k: v for k, v in params.items() if v is not None} + + return params + + def _get(self, params: Optional[Dict] = None) -> Optional[requests.Response]: + """ + GET request method placeholder. + """ + + headers = self._get_headers() + response = requests.get( + self.web_search_endpoint, headers=headers, params=params + ) + + return response + + def search( + self, + q: str, + country: Optional[str] = None, + search_lang: Optional[str] = None, + ui_lang: Optional[str] = None, + count: Optional[int] = MAX_RESULTS, + offset: Optional[int] = 0, + safesearch: Optional[str] = SafeSearchTypes.MODERATE, + freshness: Optional[str] = None, + text_decorations: Optional[bool] = False, + spellcheck: Optional[bool] = True, + goggles_id: Optional[str] = None, + extra_snippets: Optional[bool] = False, + include_domains: Optional[list[str]] = [], + raw: Optional[bool] = False, + ) -> WebSearchResponse: + """ + Perform a web search using the Brave Search API. + + Parameters: + q: str + The search query (required). + country: str + The 2-character country code. + search_lang: str + The search language preference. + ui_lang: str + The user interface language preference. + count: int + The number of results to return (default: 20, max: 20). + offset: int + The number of results to skip. + safesearch: str + Filter for adult content ('off', 'moderate', 'strict'). + freshness: str + Filter for search result freshness. + text_decorations: bool + Include decoration markers in result strings. + spellcheck: bool + Spellcheck the query (default: True). + goggles_id: str + Custom re-ranking goggles ID. + extra_snippets: bool + Include extra snippets in the search results. + include_domains: list[str] + List of domains to include in the search results. + raw: bool + Return the raw API response (default: False). + + Returns: + WebSearchApiResponse + """ + + params = self._prepare_params( + q, + country, + search_lang, + ui_lang, + count, + offset, + safesearch, + freshness, + text_decorations, + spellcheck, + goggles_id, + extra_snippets, + include_domains, + ) + + response = self._get(params=params) + + if response.status_code != 200: + raise BraveApiException( + f"Brave API Error: {response.status_code} - {response.text}" + ) + + if raw: + return response.json() + return WebSearchResponse.model_validate(response.json()) + + async def _get_async(self, params: Optional[Dict] = None) -> httpx.Response: + """ + Asynchronous GET request method. + """ + headers = self._get_headers() + + async for attempt in AsyncRetrying( + stop=stop_after_attempt(self.RETRY_ATTEMPTS), + wait=wait_fixed(self.WAIT_RETRY_SECONDS), + ): + with attempt: + async with httpx.AsyncClient() as client: + response = await client.get( + self.web_search_endpoint, headers=headers, params=params + ) + return response + + async def search_async( + self, + q: str, + country: Optional[str] = None, + search_lang: Optional[str] = None, + ui_lang: Optional[str] = None, + count: Optional[int] = MAX_RESULTS, + offset: Optional[int] = 0, + safesearch: Optional[str] = SafeSearchTypes.MODERATE, + freshness: Optional[str] = None, + text_decorations: Optional[bool] = False, + spellcheck: Optional[bool] = True, + goggles_id: Optional[str] = None, + extra_snippets: Optional[bool] = False, + include_domains: Optional[list[str]] = [], + raw: Optional[bool] = False, + ) -> WebSearchResponse: + """ + Perform a web search using the Brave Search API asynchronously. + + Parameters: + q: str + The search query (required). + country: str + The 2-character country code. + search_lang: str + The search language preference. + ui_lang: str + The user interface language preference. + count: int + The number of results to return (default: 20, max: 20). + offset: int + The number of results to skip. + safesearch: str + Filter for adult content ('off', 'moderate', 'strict'). + freshness: str + Filter for search result freshness. + text_decorations: bool + Include decoration markers in result strings. + spellcheck: bool + Spellcheck the query (default: True). + goggles_id: str + Custom re-ranking goggles ID. + extra_snippets: bool + Include extra snippets in the search results. + include_domains: list[str] + List of domains to include in the search results. + raw: bool + Return the raw API response (default: False). + + Returns: + WebSearchApiResponse + """ + + # Construct the query parameters + params = self._prepare_params( + q, + country, + search_lang, + ui_lang, + count, + offset, + safesearch, + freshness, + text_decorations, + spellcheck, + goggles_id, + extra_snippets, + include_domains, + ) + + response = await self._get_async(params=params) + + if response.status_code != 200: + raise BraveApiException( + f"Brave API Error: {response.status_code} - {response.text}" + ) + + if raw: + return response.json() + return WebSearchResponse.model_validate(response.json()) diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/brave_search/tool.py b/western_ai/canada/cohere-toolkit/src/backend/tools/brave_search/tool.py new file mode 100644 index 0000000000000000000000000000000000000000..a5c86fe3d21cebbe239a2ad35248f7074249f488 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/brave_search/tool.py @@ -0,0 +1,75 @@ +from typing import Any + +from backend.config.settings import Settings +from backend.schemas.context import Context +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.tools.base import BaseTool, ToolArgument +from backend.tools.brave_search.client import BraveClient + + +class BraveWebSearch(BaseTool): + ID = "brave_web_search" + BRAVE_API_KEY = Settings().get('tools.brave_web_search.api_key') + + def __init__(self): + self.client = BraveClient(api_key=self.BRAVE_API_KEY) + self.num_results = 20 + + @classmethod + def is_available(cls) -> bool: + return cls.BRAVE_API_KEY is not None + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Brave Web Search", + implementation=cls, + parameter_definitions={ + "query": { + "description": "Query for retrieval.", + "type": "str", + "required": True, + } + }, + is_visible=False, + is_available=cls.is_available(), + error_message=cls.generate_error_message(), + category=ToolCategory.WebSearch, + description=( + "Returns a list of relevant document snippets for a textual query retrieved " + "from the internet using Brave Search." + ), + ) # type: ignore + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any + ) -> list[dict[str, Any]]: + query = parameters.get("query", "") + + # Get domain filtering from kwargs + filtered_domains = kwargs.get(ToolArgument.DOMAIN_FILTER, []) + + try: + response = await self.client.search_async( + q=query, count=self.num_results, include_domains=filtered_domains + ) + except Exception as e: + return self.get_tool_error(details=str(e)) + + response = dict(response) + + results = response.get("web", {}).get("results", []) + + if not results: + return self.get_no_results_error() + + tool_results = [] + for result in results: + tool_results.append({ + "text": result.get("description"), + "title": result.get("title"), + "url": result.get("url"), + }) + + return tool_results diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/calculator.py b/western_ai/canada/cohere-toolkit/src/backend/tools/calculator.py new file mode 100644 index 0000000000000000000000000000000000000000..c2cdd86cbb23bc0bdf47b5765e9943d56fa491f5 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/calculator.py @@ -0,0 +1,63 @@ +from typing import Any + +from py_expression_eval import Parser + +from backend.schemas.context import Context +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.tools.base import BaseTool + + +class Calculator(BaseTool): + """ + Function Tool that evaluates mathematical expressions. + """ + + ID = "toolkit_calculator" + + @classmethod + def is_available(cls) -> bool: + return True + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Calculator", + implementation=Calculator, + parameter_definitions={ + "code": { + "description": "The expression for the calculator to evaluate, it should be a valid mathematical expression.", + "type": "str", + "required": True, + } + }, + is_visible=False, + is_available=Calculator.is_available(), + category=ToolCategory.Function, + error_message=cls.generate_error_message(), + description="A powerful multi-purpose calculator capable of a wide array of math calculations.", + ) # type: ignore + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any + ) -> list[dict[str, Any]]: + logger = ctx.get_logger() + + math_parser = Parser() + expression = parameters.get("code", "") + + # remove lines that start with # and empty lines + expression = "\n".join( + [line for line in expression.split("\n") if not line.startswith("#")] + ) + + to_evaluate = expression.replace("pi", "PI").replace("e", "E") + + try: + result = {"text": math_parser.parse(to_evaluate).evaluate({})} + except Exception as e: + logger.error(event=f"[Calculator] Error parsing expression: {e}") + return self.get_tool_error(details=str(e)) + + + return result # type: ignore diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/constants.py b/western_ai/canada/cohere-toolkit/src/backend/tools/constants.py new file mode 100644 index 0000000000000000000000000000000000000000..204ca635419237450819c74724b0808440b16906 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/constants.py @@ -0,0 +1,4 @@ +from aiohttp import ClientTimeout + +TIMEOUT_SECONDS = 15 +ASYNC_TIMEOUT = ClientTimeout(total=TIMEOUT_SECONDS) diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/files.py b/western_ai/canada/cohere-toolkit/src/backend/tools/files.py new file mode 100644 index 0000000000000000000000000000000000000000..c7c429dff0d2f17f4d11eb6afddd3082347f4000 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/files.py @@ -0,0 +1,154 @@ +from enum import StrEnum +from typing import Any + +import backend.crud.file as file_crud +from backend.schemas.context import Context +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.tools.base import BaseTool + + +class FileToolsArtifactTypes(StrEnum): + local_file = "file" + +class ReadFileTool(BaseTool): + """ + Tool to read a file from the file system. + """ + + ID = "read_file" + MAX_NUM_CHUNKS = 10 + SEARCH_LIMIT = 5 + + def __init__(self): + pass + + @classmethod + def is_available(cls) -> bool: + return True + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Read Document", + implementation=cls, + parameter_definitions={ + "file": { + "description": "A file represented as a tuple (filename, file ID) to read over", + "type": "tuple[str, str]", + "required": True, + } + }, + is_visible=True, + is_available=cls.is_available(), + error_message=cls.generate_error_message(), + category=ToolCategory.FileLoader, + description="Returns the chunked textual contents of an uploaded file.", + ) # type: ignore + + @classmethod + def get_info(cls) -> ToolDefinition: + return ToolDefinition( + display_name="Calculator", + description="A powerful multi-purpose calculator capable of a wide array of math calculations.", + error_message=cls.generate_error_message(), + ) # type: ignore + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any, + ) -> list[dict[str, Any]]: + file = parameters.get("file") + + session = kwargs.get("session") + user_id = kwargs.get("user_id") + if not file: + return self.get_tool_error(details="Files are not passed in model generated params") + + _, file_id = file + retrieved_file = file_crud.get_file(session, file_id, user_id) + if not retrieved_file: + return self.get_tool_error(details="The wrong files were passed in the tool parameters, or files were not found") + + return [ + { + "text": retrieved_file.file_content, + "title": retrieved_file.file_name, + "url": retrieved_file.file_name, + } + ] + +class SearchFileTool(BaseTool): + """ + Tool to query a list of files. + """ + + ID = "search_file" + DISPLAY_NAME = "Search Files" + MAX_NUM_CHUNKS = 10 + SEARCH_LIMIT = 5 + + def __init__(self): + pass + + @classmethod + def is_available(cls) -> bool: + return True + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Search File", + implementation=cls, + parameter_definitions={ + "search_query": { + "description": "Textual search query to search over the file's content for", + "type": "str", + "required": True, + }, + "files": { + "description": "A list of files represented as tuples of (filename, file ID) to search over", + "type": "list[tuple[str, str]]", + "required": True, + }, + }, + is_visible=True, + is_available=cls.is_available(), + error_message=cls.generate_error_message(), + category=ToolCategory.FileLoader, + description="Searches across one or more attached files based on a textual search query.", + ) # type: ignore + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any, + ) -> list[dict[str, Any]]: + query = parameters.get("search_query") + files = parameters.get("files") + + session = kwargs.get("session") + user_id = kwargs.get("user_id", "") + + if not query or not files: + return self.get_tool_error( + details="Missing query or files. The wrong files might have been passed in the tool parameters") + + file_ids = [file_id for _, file_id in files] + retrieved_files = file_crud.get_files_by_ids(session, file_ids, user_id) + + if not retrieved_files: + return self.get_tool_error( + details="Missing files. The wrong files might have been passed in the tool parameters") + + results = [] + for file in retrieved_files: + results.append( + { + "text": file.file_content, + "title": file.file_name, + "url": file.file_name, + } + ) + if not results: + return self.get_no_results_error() + + return results diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/github/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tools/github/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..6eeb283c38900624b66acc6683a062720ca6d944 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/github/__init__.py @@ -0,0 +1,11 @@ +from backend.tools.github.auth import GithubAuth +from backend.tools.github.constants import ( + GITHUB_TOOL_ID, +) +from backend.tools.github.tool import GithubTool + +__all__ = [ + "GithubAuth", + "GithubTool", + "GITHUB_TOOL_ID", +] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/github/auth.py b/western_ai/canada/cohere-toolkit/src/backend/tools/github/auth.py new file mode 100644 index 0000000000000000000000000000000000000000..7d9740761a68aaac1b29e6dbe5a4b5d473ba814f --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/github/auth.py @@ -0,0 +1,163 @@ +import datetime +import json +import urllib.parse + +import requests +from fastapi import Request + +from backend.config.settings import Settings +from backend.crud import tool_auth as tool_auth_crud +from backend.database_models import ToolAuth +from backend.database_models.database import DBSessionDep +from backend.database_models.tool_auth import ToolAuth as ToolAuthModel +from backend.schemas.tool_auth import UpdateToolAuth +from backend.services.auth.crypto import encrypt +from backend.services.logger.utils import LoggerFactory +from backend.tools.base import BaseToolAuthentication +from backend.tools.github.constants import GITHUB_TOOL_ID +from backend.tools.utils.mixins import ToolAuthenticationCacheMixin + +logger = LoggerFactory().get_logger() + + +class GithubAuth(BaseToolAuthentication, ToolAuthenticationCacheMixin): + TOOL_ID = GITHUB_TOOL_ID + AUTH_ENDPOINT = "https://github.com/login/oauth/authorize" + TOKEN_ENDPOINT = "https://github.com/login/oauth/access_token" + DEFAULT_USER_SCOPES = ['public_repo', 'read:org'] + + def __init__(self): + super().__init__() + + self.GITHUB_CLIENT_ID = Settings().get('tools.github.client_id') + self.GITHUB_CLIENT_SECRET = Settings().get('tools.github.client_secret') + self.USER_SCOPES = Settings().get('tools.github.user_scopes') or self.DEFAULT_USER_SCOPES + self.REDIRECT_URL = f"{self.BACKEND_HOST}/v1/tool/auth" + + if any([ + self.GITHUB_CLIENT_ID is None, + self.GITHUB_CLIENT_SECRET is None + ]): + raise ValueError( + "GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET must be set to use Slack Tool Auth." + ) + + def get_auth_url(self, user_id: str) -> str: + key = self.insert_tool_auth_cache(user_id, self.TOOL_ID) + state = {"key": key} + + params = { + "client_id": self.GITHUB_CLIENT_ID, + "scope": " ".join(self.USER_SCOPES or []), + "redirect_uri": self.REDIRECT_URL, + "state": json.dumps(state), + } + + return f"{self.AUTH_ENDPOINT}?{urllib.parse.urlencode(params)}" + + def retrieve_auth_token( + self, request: Request, session: DBSessionDep, user_id: str + ) -> str: + if request.query_params.get("error"): + error = request.query_params.get("error") or "Unknown error" + logger.error(event=f"[Github Tool] Auth token error: {error}.") + return error + + body = { + "code": request.query_params.get("code"), + "client_id": self.GITHUB_CLIENT_ID, + "client_secret": self.GITHUB_CLIENT_SECRET, + } + + url_encoded_body = urllib.parse.urlencode(body) + headers = { + "Content-Type": "application/x-www-form-urlencoded", + "Accept": "application/json", + } + response = requests.post(self.TOKEN_ENDPOINT, data=url_encoded_body, headers=headers) + + response_body = response.json() + + if response.status_code != 200: + logger.error( + event=f"[Github Tool] Error retrieving auth token: {response_body}" + ) + return str(response) + + token = response_body.get("access_token", None) + token_type = response_body.get("token_type", None) + refresh_token = response_body.get("refresh_token", "") + expires_in = response_body.get("expires_in", 31536000) + + if token is None: + logger.error( + event=f"[Github Tool] Error retrieving auth token: {response_body}" + ) + return str(response) + + tool_auth_crud.create_tool_auth( + session, + ToolAuthModel( + user_id=user_id, + tool_id=self.TOOL_ID, + token_type=token_type, + encrypted_access_token=encrypt(token), + encrypted_refresh_token=encrypt(refresh_token), + expires_at=datetime.datetime.now() + + datetime.timedelta(seconds=expires_in) + ), + ) + + return "" + + def try_refresh_token(self, session: DBSessionDep, user_id: str, tool_auth: ToolAuth) -> bool: + body = { + "client_id": self.GITHUB_CLIENT_ID, + "client_secret": self.GITHUB_CLIENT_SECRET, + "refresh_token": tool_auth.refresh_token, + "grant_type": "refresh_token", + } + headers = { + "Content-Type": "application/x-www-form-urlencoded", + "Accept": "application/json", + } + + url_encoded_body = urllib.parse.urlencode(body) + response = requests.post(self.TOKEN_ENDPOINT, data=url_encoded_body, headers=headers) + response_body = response.json() + + if response.status_code != 200: + logger.error( + event=f"[GITHUB Tool] Error refreshing token: {response_body}" + ) + return False + + token = response_body.get("access_token", None) + token_type = response_body.get("token_type", None) + refresh_token = response_body.get("refresh_token", "") + expires_in = response_body.get("expires_in", 31536000) + + if token is None: + logger.error( + event=f"[GITHUB Tool] Error retrieving auth token: {response_body}" + ) + return False + + existing_tool_auth = tool_auth_crud.get_tool_auth( + session, self.TOOL_ID, user_id + ) + tool_auth_crud.update_tool_auth( + session, + existing_tool_auth, + UpdateToolAuth( + user_id=user_id, + tool_id=self.TOOL_ID, + token_type=token_type, + encrypted_access_token=encrypt(token), + encrypted_refresh_token=encrypt(refresh_token), + expires_at=datetime.datetime.now() + + datetime.timedelta(seconds=expires_in), + ), + ) + + return True diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/github/client.py b/western_ai/canada/cohere-toolkit/src/backend/tools/github/client.py new file mode 100644 index 0000000000000000000000000000000000000000..8a8763c8aeca77cb46405223be9cce4c69a2a48a --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/github/client.py @@ -0,0 +1,35 @@ +from github import Auth, Github + +from backend.tools.github.constants import SEARCH_LIMIT + + +class GithubClient: + def __init__(self, auth_token, search_limit=SEARCH_LIMIT): + auth = Auth.Token(auth_token) + self.client = Github(auth=auth, per_page=search_limit) + + def search_all(self, query: str): + code_results = self.search_code(query) + all_results = {"code": code_results} + return all_results + + + def search_code(self, query: str): + return self.client.search_code(query).get_page(0) + + def search_repositories(self, query: str): + return self.client.search_repositories(query).get_page(0) + + def search_pull_requests(self, query: str): + return self.client.search_issues(f"{query} is:pr").get_page(0) + + def search_commits(self, query: str): + return self.client.search_commits(query).get_page(0) + + def get_user(self): + return self.client.get_user() + + def get_user_repositories(self, user): + return user.get_repos(type="all") + + diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/github/constants.py b/western_ai/canada/cohere-toolkit/src/backend/tools/github/constants.py new file mode 100644 index 0000000000000000000000000000000000000000..6e329c49b3786c8c43f333485947a4f33b0f53e9 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/github/constants.py @@ -0,0 +1,2 @@ +SEARCH_LIMIT = 10 +GITHUB_TOOL_ID = "github" diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/github/tool.py b/western_ai/canada/cohere-toolkit/src/backend/tools/github/tool.py new file mode 100644 index 0000000000000000000000000000000000000000..b91c5cbe62771354051f411178f80bb560c2034b --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/github/tool.py @@ -0,0 +1,83 @@ +from typing import Any + +from backend.config.settings import Settings +from backend.crud import tool_auth as tool_auth_crud +from backend.schemas.context import Context +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.services.logger.utils import LoggerFactory +from backend.tools.base import BaseTool +from backend.tools.github.auth import GithubAuth +from backend.tools.github.constants import GITHUB_TOOL_ID, SEARCH_LIMIT +from backend.tools.github.utils import get_github_service + +logger = LoggerFactory().get_logger() + + +class GithubTool(BaseTool): + """ + Tool that searches Github for repositories based on a query. + """ + ID = GITHUB_TOOL_ID + CLIENT_ID = Settings().get('tools.github.client_id') + CLIENT_SECRET = Settings().get('tools.github.client_secret') + DEFAULT_REPOS = Settings().get('tools.github.default_repos') + + @classmethod + def is_available(cls) -> bool: + return cls.CLIENT_ID is not None and cls.CLIENT_SECRET is not None + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Github", + implementation=cls, + parameter_definitions={ + "query": { + "description": "Query to search Github.", + "type": "str", + "required": True, + } + }, + is_visible=True, + is_available=cls.is_available(), + auth_implementation=GithubAuth, + should_return_token=False, + error_message=cls.generate_error_message(), + category=ToolCategory.DataLoader, + description="Returns a list of relevant document snippets from Github.", + ) # type: ignore + + @classmethod + def _handle_tool_specific_errors(cls, error: Exception, **kwargs: Any) -> None: + message = "[Github] Tool Error: {}".format(str(error)) + + if error: + session = kwargs["session"] + user_id = kwargs["user_id"] + tool_auth_crud.delete_tool_auth( + db=session, user_id=user_id, tool_id=GITHUB_TOOL_ID + ) + + logger.error( + event="[Github] Auth token error: Please refresh the page and re-authenticate." + ) + raise Exception(message) + + async def call(self, parameters: dict, ctx: Context, **kwargs: Any) -> list[dict[str, Any]]: + user_id = kwargs.get("user_id", "") + query = parameters.get("query", "") + + # Search Slack + github_service = get_github_service(user_id=user_id, default_repos=self.DEFAULT_REPOS, + search_limit=SEARCH_LIMIT) + try: + all_results = github_service.search(query=query) + results = github_service.transform_response(all_results) + except Exception as e: + return self.get_tool_error(details=str(e)) + + if not results: + return self.get_no_results_error() + + return results diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/github/utils.py b/western_ai/canada/cohere-toolkit/src/backend/tools/github/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..c9c61079aa4bceead02669eb2563dffe557b8245 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/github/utils.py @@ -0,0 +1,72 @@ +from backend.database_models.database import get_session +from backend.services.logger.utils import LoggerFactory +from backend.tools.base import ToolAuthException +from backend.tools.github.auth import GithubAuth +from backend.tools.github.client import GithubClient +from backend.tools.github.constants import GITHUB_TOOL_ID, SEARCH_LIMIT + +logger = LoggerFactory().get_logger() + + +class GithubService: + def __init__(self, user_id: str, auth_token: str, default_repos: list[str], search_limit=SEARCH_LIMIT): + self.user_id = user_id + self.auth_token = auth_token + self.client = GithubClient(auth_token=auth_token, search_limit=search_limit) + self.github_user = self.client.get_user() + self.repositories = self.client.get_user_repositories(self.github_user) + if default_repos: + self.repositories = [repo for repo in self.repositories if repo.full_name in default_repos] + + + @staticmethod + def prepare_repo_query(query: str, repo): + if repo.fork: + query += " fork:true" + return f"{query} repo:{repo.full_name}" + + @staticmethod + def _extract_code_data(code): + return { + "title": code.path, + "text": code.decoded_content.decode("utf-8"), + "url": code.html_url, + "type": "code", + } + + def search(self, query: str): + results = {"code": []} + for repo in self.repositories: + prepared_query = self.prepare_repo_query(query, repo) + repo_results = self.client.search_all(query=prepared_query) + results["code"].extend(repo_results["code"]) + return results + + def transform_response(self, response): + results = [] + for code in response["code"]: + results.append(self._extract_code_data(code)) + return results + + +def get_github_service(user_id: str, default_repos: list[str], search_limit=SEARCH_LIMIT) -> GithubService: + github_auth = GithubAuth() + session = next(get_session()) + if github_auth.is_auth_required(session, user_id=user_id): + session.close() + raise ToolAuthException( + "Github Tool auth Error: Agent creator credentials need to re-authenticate", + GITHUB_TOOL_ID, + ) + + auth_token = github_auth.get_token(session=session, user_id=user_id) + if auth_token is None: + session.close() + raise Exception("Github Tool Error: No credentials found") + + service = GithubService(user_id=user_id, auth_token=auth_token, default_repos=default_repos, search_limit=search_limit) + session.close() + return service + + + diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..23898d97a653b6ede6c16640147de7671d8ae18f --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/__init__.py @@ -0,0 +1,11 @@ +from backend.tools.gmail.auth import GmailAuth +from backend.tools.gmail.constants import ( + GMAIL_TOOL_ID, +) +from backend.tools.gmail.tool import GmailTool + +__all__ = [ + "GmailAuth", + "GmailTool", + "GMAIL_TOOL_ID", +] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/auth.py b/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/auth.py new file mode 100644 index 0000000000000000000000000000000000000000..334ef65541ecfe8ad13df8a64eebc245418871e3 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/auth.py @@ -0,0 +1,137 @@ +import datetime +import json +import urllib.parse + +import requests +from fastapi import Request + +from backend.config.settings import Settings +from backend.crud import tool_auth as tool_auth_crud +from backend.database_models.database import DBSessionDep +from backend.database_models.tool_auth import ToolAuth as ToolAuthModel +from backend.schemas.tool_auth import UpdateToolAuth +from backend.services.auth.crypto import encrypt +from backend.services.logger.utils import LoggerFactory +from backend.tools.base import BaseToolAuthentication +from backend.tools.gmail.constants import GMAIL_TOOL_ID +from backend.tools.utils.mixins import ToolAuthenticationCacheMixin + +logger = LoggerFactory().get_logger() + + +class GmailAuth(BaseToolAuthentication, ToolAuthenticationCacheMixin): + TOOL_ID = GMAIL_TOOL_ID + AUTH_ENDPOINT = "https://accounts.google.com/o/oauth2/v2/auth" + TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token" + DEFAULT_USER_SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'] + + def __init__(self): + super().__init__() + + self.GMAIL_CLIENT_ID = Settings().get("tools.gmail.client_id") + self.GMAIL_CLIENT_SECRET = Settings().get("tools.gmail.client_secret") + self.REDIRECT_URL = f"{self.BACKEND_HOST}/v1/tool/auth" + self.USER_SCOPES = Settings().get("tools.gmail.user_scopes") or self.DEFAULT_USER_SCOPES + + if any([ + self.GMAIL_CLIENT_ID is None, + self.GMAIL_CLIENT_SECRET is None + ]): + raise ValueError( + "GMAIL_CLIENT_ID and GMAIL_CLIENT_SECRET must be set to use Gmail Tool Auth." + ) + + def get_auth_url(self, user_id: str) -> str: + key = self.insert_tool_auth_cache(user_id, self.TOOL_ID) + state = {"key": key} + + params = { + "response_type": "code", + "client_id": self.GMAIL_CLIENT_ID, + "scope": " ".join(self.USER_SCOPES or []), + "redirect_uri": self.REDIRECT_URL, + "prompt": "select_account consent", + "state": json.dumps(state), + "access_type": "offline", + "include_granted_scopes": "true", + } + + return f"{self.AUTH_ENDPOINT}?{urllib.parse.urlencode(params)}" + + def retrieve_auth_token( + self, request: Request, session: DBSessionDep, user_id: str + ) -> str: + if request.query_params.get("error"): + error = request.query_params.get("error") or "Unknown error" + logger.error(event=f"[Gmail Tool] Auth token error: {error}.") + return error + + body = { + "code": request.query_params.get("code"), + "client_id": self.GMAIL_CLIENT_ID, + "client_secret": self.GMAIL_CLIENT_SECRET, + "redirect_uri": self.REDIRECT_URL, + "grant_type": "authorization_code", + } + + response = requests.post(self.TOKEN_ENDPOINT, json=body) + response_body = response.json() + + if response.status_code != 200: + logger.error( + event=f"[Gmail] Error retrieving auth token: {response_body}" + ) + return "" + + tool_auth_crud.create_tool_auth( + session, + ToolAuthModel( + user_id=user_id, + tool_id=self.TOOL_ID, + token_type=response_body["token_type"], + encrypted_access_token=encrypt(response_body["access_token"]), + encrypted_refresh_token=encrypt(response_body["refresh_token"]), + expires_at=datetime.datetime.now() + + datetime.timedelta(seconds=response_body["expires_in"]), + ), + ) + + return "" + + def try_refresh_token( + self, session: DBSessionDep, user_id: str, tool_auth: ToolAuthModel + ) -> bool: + body = { + "client_id": self.GMAIL_CLIENT_ID, + "client_secret": self.GMAIL_CLIENT_SECRET, + "refresh_token": tool_auth.refresh_token, + "grant_type": "refresh_token", + } + + response = requests.post(self.TOKEN_ENDPOINT, json=body) + response_body = response.json() + + if response.status_code != 200: + logger.error( + event=f"[Gmail] Error refreshing token: {response_body}" + ) + return False + + existing_tool_auth = tool_auth_crud.get_tool_auth( + session, self.TOOL_ID, user_id + ) + tool_auth_crud.update_tool_auth( + session, + existing_tool_auth, + UpdateToolAuth( + user_id=user_id, + tool_id=self.TOOL_ID, + token_type=response_body["token_type"], + encrypted_access_token=encrypt(response_body["access_token"]), + encrypted_refresh_token=tool_auth.encrypted_refresh_token, + expires_at=datetime.datetime.now() + + datetime.timedelta(seconds=response_body["expires_in"]), + ), + ) + + return True diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/client.py b/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/client.py new file mode 100644 index 0000000000000000000000000000000000000000..c3321ece1e07ac8d9cf151f6ed0c55296a331367 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/client.py @@ -0,0 +1,31 @@ +from google.oauth2.credentials import Credentials +from googleapiclient.discovery import build + + +class GmailClient: + def __init__(self, auth_token, search_limit=20): + creds = Credentials(auth_token) + self.service = build("gmail", "v1", credentials=creds, cache_discovery=False) + self.search_limit = search_limit + + def search_all(self, query): + return ( + self.service.users() + .messages() + .list(userId="me", q=query, maxResults=self.search_limit) + .execute() + ) + + def retrieve_messages(self, message_ids): + messages = [] + + for message_id in message_ids: + message = ( + self.service.users() + .messages() + .get(userId="me", id=message_id) + .execute() + ) + messages.append(message) + + return messages diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/constants.py b/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/constants.py new file mode 100644 index 0000000000000000000000000000000000000000..a2cc103cb1faf360fae34ef0bf16563826cfbe4c --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/constants.py @@ -0,0 +1,2 @@ +SEARCH_LIMIT = 10 +GMAIL_TOOL_ID = "gmail" diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/tool.py b/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/tool.py new file mode 100644 index 0000000000000000000000000000000000000000..34c073ed2f6827b7852ae256e9ceb1f884d755b6 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/tool.py @@ -0,0 +1,80 @@ +from typing import Any + +from backend.config.settings import Settings +from backend.crud import tool_auth as tool_auth_crud +from backend.schemas.context import Context +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.services.logger.utils import LoggerFactory +from backend.tools.base import BaseTool +from backend.tools.gmail.auth import GmailAuth +from backend.tools.gmail.constants import GMAIL_TOOL_ID, SEARCH_LIMIT +from backend.tools.gmail.utils import get_gmail_service + +logger = LoggerFactory().get_logger() + + +class GmailTool(BaseTool): + """ + Tool that searches Gmail for messages and files based on a query. + """ + + ID = GMAIL_TOOL_ID + CLIENT_ID = Settings().get("tools.gmail.client_id") + CLIENT_SECRET = Settings().get("tools.gmail.client_secret") + + @classmethod + def is_available(cls) -> bool: + return cls.CLIENT_ID is not None and cls.CLIENT_SECRET is not None + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Gmail", + implementation=cls, + parameter_definitions={ + "query": { + "description": "Query to search Gmail.", + "type": "str", + "required": True, + } + }, + is_visible=True, + is_available=cls.is_available(), + auth_implementation=GmailAuth, + error_message=cls.generate_error_message(), + category=ToolCategory.DataLoader, + description="Returns a list of relevant email snippets from Gmail.", + ) # type: ignore + + @classmethod + def _handle_tool_specific_errors(cls, error: Exception, **kwargs: Any) -> None: + message = "[Gmail] Tool Error: {}".format(str(error)) + + if error: + session = kwargs["session"] + user_id = kwargs["user_id"] + tool_auth_crud.delete_tool_auth( + db=session, user_id=user_id, tool_id=GMAIL_TOOL_ID + ) + + logger.error( + event="[Gmail] Auth token error: Please refresh the page and re-authenticate." + ) + raise Exception(message) + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any, + ) -> list[dict[str, Any]]: + user_id = kwargs.get("user_id", "") + query = parameters.get("query", "") + + gmail_service = get_gmail_service(user_id=user_id, search_limit=SEARCH_LIMIT) + results = gmail_service.search_all(query=query) + message_ids = [message["id"] for message in results["messages"]] if "messages" in results else [] + messages = gmail_service.retrieve_messages(message_ids) + + if not messages: + return self.get_no_results_error() + + return gmail_service.serialize_results(messages) diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/utils.py b/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..a973485eb41842459300e1966c05732fecd1d138 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/gmail/utils.py @@ -0,0 +1,124 @@ +import base64 + +from backend.database_models.database import get_session +from backend.services.logger.utils import LoggerFactory +from backend.tools.base import ToolAuthException +from backend.tools.gmail import GmailAuth +from backend.tools.gmail.client import GmailClient +from backend.tools.gmail.constants import GMAIL_TOOL_ID, SEARCH_LIMIT + +logger = LoggerFactory().get_logger() + + +def decode_base64_raw(raw): + return base64.urlsafe_b64decode(raw).decode("utf-8") + + +class GmailService: + def __init__(self, user_id: str, auth_token: str, search_limit=SEARCH_LIMIT): + self.user_id = user_id + self.auth_token = auth_token + self.client = GmailClient(auth_token=auth_token, search_limit=search_limit) + + def search_all(self, query: str): + return self.client.search_all(query=query) + + def retrieve_messages(self, message_ids): + return self.client.retrieve_messages(message_ids) + + def serialize_results(self, messages): + results = [] + + for message in messages: + results.append(self.serialize_result(message)) + + return results + + def serialize_result(self, message): + data = { + "title": "", + "text": "", + "type": "message", + "url": "https://mail.google.com/mail/u/0/#inbox/%s" % message.get('threadId', '') + } + + payload = message["payload"] + + # Find subject title + if (headers := payload.get("headers")) is not None: + # Get miscellaneous header values in K-V format + headers_dict = {header["name"]: header["value"] for header in headers} + stripped_headers = { + key: str(value) + for key, value in headers_dict.items() + if isinstance(value, (str, int, bool)) + } + + data.update(stripped_headers) + + if "Subject" in data: + data["title"] = data.pop("Subject") + + # Build text + if (part := payload.get("parts")) is not None: + if (body := part[0].get("body", {}).get("data")) is not None: + data["text"] = decode_base64_raw(body) + + if not data["text"]: + part_parts = part[0].get("parts") + + if part_parts: + for part_part in part_parts: + content_type = self.get_header_value( + part_part["headers"], + 'Content-Type', + ) + + if content_type.startswith('text/plain;') and part_part["body"]["size"]: + if data["text"]: + data["text"] += ' ' + + data["text"] += decode_base64_raw(part_part["body"].get("data", "")) + + # Remove metadata fields + metadata_prefixes = ["ARC", "DKIM", "X"] + + data = { + key: value + for key, value in data.items() + if key.split("-")[0] not in metadata_prefixes + } + + data["text"] = data["text"].replace("\r\n", " ") + + return data + + def get_header_value(self, headers, name): + value = '' + + for header in headers: + if header.get('name') == name: + value = header.get('value', '') + + return value + +def get_gmail_service(user_id: str, search_limit=SEARCH_LIMIT) -> GmailService: + gmail_auth = GmailAuth() + session = next(get_session()) + + if gmail_auth.is_auth_required(session, user_id=user_id): + session.close() + raise ToolAuthException( + "[Gmail] Auth Error: Agent creator credentials need to re-authenticate", + GMAIL_TOOL_ID, + ) + + auth_token = gmail_auth.get_token(session=session, user_id=user_id) + if auth_token is None: + session.close() + raise Exception("[Gmail] Auth Error: No credentials found") + + service = GmailService(user_id=user_id, auth_token=auth_token, search_limit=search_limit) + session.close() + + return service diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/google_drive/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tools/google_drive/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e9a3a167cd973f86605ea1bbe164b4acaa5b5275 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/google_drive/__init__.py @@ -0,0 +1,11 @@ +from backend.tools.google_drive.auth import GoogleDriveAuth +from backend.tools.google_drive.constants import ( + GOOGLE_DRIVE_TOOL_ID, +) +from backend.tools.google_drive.tool import GoogleDrive + +__all__ = [ + "GoogleDriveAuth", + "GoogleDrive", + "GOOGLE_DRIVE_TOOL_ID", +] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/google_drive/auth.py b/western_ai/canada/cohere-toolkit/src/backend/tools/google_drive/auth.py new file mode 100644 index 0000000000000000000000000000000000000000..22e05fd2f777f821ef6fdf87b03bcd680436d47a --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/google_drive/auth.py @@ -0,0 +1,134 @@ +import datetime +import json +import urllib.parse + +import requests +from fastapi import Request + +from backend.config.settings import Settings +from backend.crud import tool_auth as tool_auth_crud +from backend.database_models.database import DBSessionDep +from backend.database_models.tool_auth import ToolAuth as ToolAuthModel +from backend.schemas.tool_auth import UpdateToolAuth +from backend.services.auth.crypto import encrypt +from backend.services.logger.utils import LoggerFactory +from backend.tools.base import BaseToolAuthentication +from backend.tools.google_drive.constants import GOOGLE_DRIVE_TOOL_ID +from backend.tools.utils.mixins import ToolAuthenticationCacheMixin + +from .constants import SCOPES + +logger = LoggerFactory().get_logger() + + +class GoogleDriveAuth(BaseToolAuthentication, ToolAuthenticationCacheMixin): + TOOL_ID = GOOGLE_DRIVE_TOOL_ID + AUTH_ENDPOINT = "https://accounts.google.com/o/oauth2/v2/auth" + TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token" + + def __init__(self): + super().__init__() + self.GOOGLE_DRIVE_CLIENT_ID = Settings().get('tools.google_drive.client_id') + self.GOOGLE_DRIVE_CLIENT_SECRET = Settings().get('tools.google_drive.client_secret') + self.REDIRECT_URL = f"{self.BACKEND_HOST}/v1/tool/auth" + + if ( + self.GOOGLE_DRIVE_CLIENT_ID is None + or self.GOOGLE_DRIVE_CLIENT_SECRET is None + ): + raise ValueError( + "GOOGLE_DRIVE_CLIENT_ID and GOOGLE_DRIVE_CLIENT_SECRET must be set to use Google Drive Tool Auth." + ) + + def get_auth_url(self, user_id: str) -> str: + key = self.insert_tool_auth_cache(user_id, self.TOOL_ID) + state = {"key": key} + + params = { + "response_type": "code", + "client_id": self.GOOGLE_DRIVE_CLIENT_ID, + "scope": " ".join(SCOPES), + "redirect_uri": self.REDIRECT_URL, + "prompt": "select_account consent", + "state": json.dumps(state), + "access_type": "offline", + "include_granted_scopes": "true", + } + + return f"{self.AUTH_ENDPOINT}?{urllib.parse.urlencode(params)}" + + def try_refresh_token( + self, session: DBSessionDep, user_id: str, tool_auth: ToolAuthModel + ) -> bool: + body = { + "client_id": self.GOOGLE_DRIVE_CLIENT_ID, + "client_secret": self.GOOGLE_DRIVE_CLIENT_SECRET, + "refresh_token": tool_auth.refresh_token, + "grant_type": "refresh_token", + } + + response = requests.post(self.TOKEN_ENDPOINT, json=body) + response_body = response.json() + + if response.status_code != 200: + logger.error( + event=f"[Google Drive] Error refreshing token: {response_body}" + ) + return False + + existing_tool_auth = tool_auth_crud.get_tool_auth( + session, self.TOOL_ID, user_id + ) + tool_auth_crud.update_tool_auth( + session, + existing_tool_auth, + UpdateToolAuth( + user_id=user_id, + tool_id=self.TOOL_ID, + token_type=response_body["token_type"], + encrypted_access_token=encrypt(response_body["access_token"]), + encrypted_refresh_token=tool_auth.encrypted_refresh_token, + expires_at=datetime.datetime.now() + + datetime.timedelta(seconds=response_body["expires_in"]), + ), + ) + + return True + + def retrieve_auth_token( + self, request: Request, session: DBSessionDep, user_id: str + ) -> str: + if request.query_params.get("error"): + error = request.query_params.get("error") + logger.error(event=f"[Google Drive] Auth token error: {error}.") + return error + + body = { + "code": request.query_params.get("code"), + "client_id": self.GOOGLE_DRIVE_CLIENT_ID, + "client_secret": self.GOOGLE_DRIVE_CLIENT_SECRET, + "redirect_uri": self.REDIRECT_URL, + "grant_type": "authorization_code", + } + + response = requests.post(self.TOKEN_ENDPOINT, json=body) + response_body = response.json() + + if response.status_code != 200: + logger.error( + event=f"[Google Drive] Error retrieving auth token: {response_body}" + ) + return response + + tool_auth_crud.create_tool_auth( + session, + ToolAuthModel( + user_id=user_id, + tool_id=self.TOOL_ID, + token_type=response_body["token_type"], + encrypted_access_token=encrypt(response_body["access_token"]), + encrypted_refresh_token=encrypt(response_body["refresh_token"]), + expires_at=datetime.datetime.now() + + datetime.timedelta(seconds=response_body["expires_in"]), + ), + ) diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/google_drive/constants.py b/western_ai/canada/cohere-toolkit/src/backend/tools/google_drive/constants.py new file mode 100644 index 0000000000000000000000000000000000000000..6ea585868da08438eb454bba2472297b8506654c --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/google_drive/constants.py @@ -0,0 +1,32 @@ +CSV_MIMETYPE = "text/csv" +TEXT_MIMETYPE = "text/plain" +SEARCH_LIMIT = 10 +# 1 hour +ACTIVITY_TRACKING_WINDOW = 86400 / 24 +SCOPES = [ + "https://www.googleapis.com/auth/drive.metadata.readonly", + "https://www.googleapis.com/auth/drive.readonly", + "https://www.googleapis.com/auth/drive.activity.readonly", +] +FOLDER_MIME_TYPE = "application/vnd.google-apps.folder" +NATIVE_SEARCH_MIME_TYPES = [ + "application/vnd.google-apps.document", + "application/vnd.google-apps.spreadsheet", + "application/vnd.google-apps.presentation", +] +NON_NATIVE_SEARCH_MIME_TYPES = [ + "application/vnd.google-apps.shortcut", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "application/pdf", + "text/plain", + "text/markdown", +] +NATIVE_EXTENSION_MAPPINGS = { + "application/vnd.google-apps.document": "txt", + "application/vnd.google-apps.spreadsheet": "csv", + "application/vnd.google-apps.presentation": "pptx", +} +SEARCH_MIME_TYPES = NATIVE_SEARCH_MIME_TYPES + NON_NATIVE_SEARCH_MIME_TYPES +DOC_FIELDS = "id, name, mimeType, webViewLink, exportLinks, shortcutDetails, trashed, parents, fileExtension, permissions" + +GOOGLE_DRIVE_TOOL_ID = "google_drive" diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/google_drive/tool.py b/western_ai/canada/cohere-toolkit/src/backend/tools/google_drive/tool.py new file mode 100644 index 0000000000000000000000000000000000000000..95594631a0688542d2842e0e610c3aa88461e499 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/google_drive/tool.py @@ -0,0 +1,200 @@ +from typing import Any + +from google.auth.exceptions import RefreshError + +from backend.config.settings import Settings +from backend.crud import tool_auth as tool_auth_crud +from backend.schemas.context import Context +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.services.logger.utils import LoggerFactory +from backend.tools.base import BaseTool +from backend.tools.google_drive.auth import GoogleDriveAuth +from backend.tools.google_drive.constants import GOOGLE_DRIVE_TOOL_ID, SEARCH_LIMIT +from backend.tools.google_drive.utils import ( + extract_export_link, + extract_title, + extract_web_view_link, + get_service, + perform_get_batch, + process_shortcut_file, +) + +logger = LoggerFactory().get_logger() + + +class GoogleDrive(BaseTool): + """ + Tool that searches Google Drive + """ + ID = GOOGLE_DRIVE_TOOL_ID + CLIENT_ID = Settings().get('tools.google_drive.client_id') + CLIENT_SECRET = Settings().get('tools.google_drive.client_secret') + + @classmethod + def is_available(cls) -> bool: + return cls.CLIENT_ID is not None and cls.CLIENT_SECRET is not None + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Google Drive", + implementation=cls, + parameter_definitions={ + "query": { + "description": "Query to search Google Drive documents with.", + "type": "str", + "required": True, + } + }, + is_visible=True, + is_available=GoogleDrive.is_available(), + auth_implementation=GoogleDriveAuth, + should_return_token=True, + error_message=cls.generate_error_message(), + category=ToolCategory.DataLoader, + description="Returns a list of relevant document snippets from the user's Google drive.", + ) # type: ignore + + def _handle_tool_specific_errors(self, error: Exception, **kwargs: Any): + message = "[Google Drive] Tool Error: {}".format(str(error)) + + if isinstance(error, RefreshError): + session = kwargs["session"] + user_id = kwargs["user_id"] + tool_auth_crud.delete_tool_auth( + db=session, user_id=user_id, tool_id=GOOGLE_DRIVE_TOOL_ID + ) + + logger.error( + event="[Google Drive] Auth token error: Please refresh the page and re-authenticate." + ) + raise Exception(message) + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any, + ) -> list[dict[str, Any]]: + user_id = kwargs.get("user_id", "") + query = parameters.get("query", "").replace("'", "\\'") + + # Search Google Drive + logger.info(event="[Google Drive] Defaulting to raw Google Drive search.") + agent_tool_metadata = kwargs["agent_tool_metadata"] + try: + documents = await _default_gdrive_list_files( + user_id=user_id, query=query, agent_tool_metadata=agent_tool_metadata + ) + except Exception as e: + return self.get_tool_error(details=str(e)) + + if not documents: + logger.info(event="[Google Drive] No documents found.") + return self.get_no_results_error() + + return documents + + +async def _default_gdrive_list_files( + user_id: str, query: str, agent_tool_metadata: dict[str, str] +): + from backend.tools.google_drive.constants import ( + DOC_FIELDS, + NATIVE_SEARCH_MIME_TYPES, + SEARCH_MIME_TYPES, + ) + from backend.tools.utils.async_download import async_perform + + (service, creds) = ( + get_service(api="drive", user_id=user_id)[key] for key in ("service", "creds") + ) + conditions = [ + "(" + + " or ".join([f"mimeType = '{mime_type}'" for mime_type in SEARCH_MIME_TYPES]) + + ")", + "(" + + " or ".join([f"fullText contains '{word}'" for word in [query]]) + + " or " + + " or ".join([f"name contains '{word}'" for word in [query]]) + + ")", + ] + + file_ids = [] + folder_ids = [] + if agent_tool_metadata: + for metadata in agent_tool_metadata: + if metadata.tool_name == GOOGLE_DRIVE_TOOL_ID: + artifacts = metadata.artifacts + for artifact in artifacts: + if artifact["type"] == "folder": + folder_ids.append(artifact["id"]) + else: + file_ids.append(artifact["id"]) + + # Condition on files if exist + files = [] + if file_ids: + files = perform_get_batch(file_ids=file_ids, user_id=user_id) + else: + # Condition on folders if exist + if folder_ids: + # Explanation: https://stackoverflow.com/questions/78063848/google-drive-api-full-text-search-doesnt-work-when-using-in-parents-option#comment138803743_78063848 + if len(folder_ids) == 1: + folder_ids += folder_ids + conditions.append( + "(" + + " or ".join( + ["'{}' in parents".format(folder_id) for folder_id in folder_ids] + ) + + ")" + ) + + q = " and ".join(conditions) + fields = f"nextPageToken, files({DOC_FIELDS})" + + search_results = [] + search_results = ( + service.files() + .list( + pageSize=SEARCH_LIMIT, + q=q, + includeItemsFromAllDrives=True, + supportsAllDrives=True, + fields=fields, + ) + .execute() + ) + + files = search_results.get("files", []) + if not files: + logger.info(event="[Google Drive] No files found.") + + if not files: + return [] + + # post process files + processed_files = {x["id"]: process_shortcut_file(service, x) for x in files} + web_view_links = {x["id"]: extract_web_view_link(x) for x in files} + titles = {x["id"]: extract_title(x) for x in files} + + id_to_texts = {} + + # native files + native_files = { + file_id: x + for file_id, x in processed_files.items() + if x["mimeType"] in NATIVE_SEARCH_MIME_TYPES + } + id_to_urls = { + file_id: extract_export_link(x) for file_id, x in native_files.items() + } + if id_to_urls: + id_to_texts = await async_perform(id_to_urls, creds.token) + + return [ + { + "text": id_to_texts[idd], + "url": web_view_links.get(idd, ""), + "title": titles.get(idd, ""), + } + for idd in id_to_texts + ] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/google_drive/utils.py b/western_ai/canada/cohere-toolkit/src/backend/tools/google_drive/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..aab1766d618732bec00ba542a034bfb69755b00b --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/google_drive/utils.py @@ -0,0 +1,124 @@ +from concurrent import futures +from typing import Any, Dict, List, TypedDict + +from google.oauth2.credentials import Credentials +from googleapiclient.discovery import build + +from backend.database_models.database import get_session +from backend.services.logger.utils import LoggerFactory +from backend.tools.base import ToolAuthException +from backend.tools.google_drive.auth import GoogleDriveAuth +from backend.tools.google_drive.constants import ( + CSV_MIMETYPE, + DOC_FIELDS, + GOOGLE_DRIVE_TOOL_ID, + TEXT_MIMETYPE, +) + +logger = LoggerFactory().get_logger() + + +class Service(TypedDict): + service: Any + creds: Credentials + + +def get_service(api: str, user_id: str, version: str = "v3") -> Service: + # Get google credentials + gdrive_auth = GoogleDriveAuth() + agent_creator_auth_token = None + + session = next(get_session()) + if gdrive_auth.is_auth_required(session, user_id=user_id): + session.close() + raise ToolAuthException( + "Sync GDrive Error: Agent creator credentials need to re-authenticate", + GOOGLE_DRIVE_TOOL_ID, + ) + + agent_creator_auth_token = gdrive_auth.get_token(session=session, user_id=user_id) + if agent_creator_auth_token is None: + session.close() + raise Exception("Sync GDrive Error: No agent creator credentials found") + + creds = Credentials(agent_creator_auth_token) + service = build(api, version, credentials=creds, cache_discovery=False) + session.close() + return {"service": service, "creds": creds} + + +""" +GDrive GET file +""" + + +def perform_get_batch(file_ids: List[str], user_id: str) -> List[Dict[str, str]]: + results = [] + + with futures.ThreadPoolExecutor(max_workers=10) as executor: + futures_list = [ + executor.submit(_get_file, file_id, user_id) for file_id in file_ids + ] + for future in futures.as_completed(futures_list): + try: + results.append(future.result()) + except Exception as e: + raise e + return results + + +def _get_file(file_id: str, user_id: str): + (service,) = ( + get_service(api="drive", user_id=user_id)[key] for key in ("service",) + ) + return ( + service.files() + .get( + fileId=file_id, + fields=DOC_FIELDS, + supportsAllDrives=True, + ) + .execute() + ) + +def process_shortcut_file(service: Any, file: Dict[str, str]) -> Dict[str, str]: + if file["mimeType"] == "application/vnd.google-apps.shortcut": + try: + targetId = file["shortcutDetails"]["targetId"] + targetFile = ( + service.files() + .get( + fileId=targetId, + fields=DOC_FIELDS, + supportsAllDrives=True, + ) + .execute() + ) + return targetFile + except Exception as error: + file_id = file["id"] + logger.error( + event="An error occurred processing a shortcut file with id", + file_id=file_id, + type=type(error), + error=error, + ) + return {} + else: + return file + + +def extract_web_view_link(file: Dict[str, str]) -> str: + return file.pop("webViewLink", "") + +def extract_title(file: Dict[str, str]) -> str: + return file.pop("name", "") + + +def extract_export_link(file: Dict[str, str]) -> str: + export_links = file.pop("exportLinks", {}) + if TEXT_MIMETYPE in export_links: + return export_links[TEXT_MIMETYPE] + elif CSV_MIMETYPE in export_links: + return export_links[CSV_MIMETYPE] + return "" diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/google_search.py b/western_ai/canada/cohere-toolkit/src/backend/tools/google_search.py new file mode 100644 index 0000000000000000000000000000000000000000..1e1afeddb2c07b40115977d529e965fbceae04dc --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/google_search.py @@ -0,0 +1,72 @@ +from typing import Any + +from googleapiclient.discovery import build + +from backend.config.settings import Settings +from backend.schemas.context import Context +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.tools.base import BaseTool, ToolArgument + + +class GoogleWebSearch(BaseTool): + ID = "google_web_search" + API_KEY = Settings().get('tools.google_web_search.api_key') + CSE_ID = Settings().get('tools.google_web_search.cse_id') + + def __init__(self): + self.client = build("customsearch", "v1", developerKey=self.API_KEY) + + @classmethod + def is_available(cls) -> bool: + return bool(cls.API_KEY) and bool(cls.CSE_ID) + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Google Web Search", + implementation=cls, + parameter_definitions={ + "query": { + "description": "A search query for the Google search engine.", + "type": "str", + "required": True, + } + }, + is_visible=False, + is_available=cls.is_available(), + error_message=cls.generate_error_message(), + category=ToolCategory.WebSearch, + description="Returns relevant results by performing a Google web search.", + ) # type: ignore + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any, + ) -> list[dict[str, Any]]: + query = parameters.get("query", "") + cse = self.client.cse() + + # Get domain filtering from kwargs + filtered_domains = kwargs.get(ToolArgument.DOMAIN_FILTER, []) + domain_filters = [f"site:{domain}" for domain in filtered_domains] + try: + response = cse.list(q=query, cx=self.CSE_ID, orTerms=domain_filters).execute() + search_results = response.get("items", []) + except Exception as e: + return self.get_tool_error(details=str(e)) + + if not search_results: + return self.get_no_results_error() + + tool_results = [] + for result in search_results: + if "snippet" not in result: + continue + + tool_results.append({ + "text": result.get("snippet"), + "title": result.get("title"), + "url": result.get("url") + }) + + return tool_results diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/hybrid_search.py b/western_ai/canada/cohere-toolkit/src/backend/tools/hybrid_search.py new file mode 100644 index 0000000000000000000000000000000000000000..e82f73e16a9e2012cb2abb61d7aa3d80a7976438 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/hybrid_search.py @@ -0,0 +1,166 @@ +import asyncio +import itertools +from typing import Any, Callable, Dict, List + +from backend.config.settings import Settings +from backend.model_deployments.base import BaseDeployment +from backend.schemas.context import Context +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.tools.base import BaseTool, ToolArgument +from backend.tools.brave_search.tool import BraveWebSearch +from backend.tools.google_search import GoogleWebSearch +from backend.tools.tavily_search import TavilyWebSearch +from backend.tools.web_scrape import WebScrapeTool + + +class HybridWebSearch(BaseTool): + ID = "hybrid_web_search" + POST_RERANK_MAX_RESULTS = 5 + AVAILABLE_WEB_SEARCH_TOOLS = [TavilyWebSearch, GoogleWebSearch, BraveWebSearch] + WEB_SCRAPE_TOOL = WebScrapeTool + ENABLED_WEB_SEARCH_TOOLS = Settings().get('tools.hybrid_web_search.enabled_web_searches') + DOMAIN_FILTER = Settings().get('tools.hybrid_web_search.domain_filters') or [] + SITE_FILTER = Settings().get('tools.hybrid_web_search.site_filters') or [] + + def __init__(self): + available_search_tools = self.get_available_search_tools() + + # Instantiate Search tools and Web scraping tool + self.search_tools = [search_tool() for search_tool in available_search_tools] + self.web_scrape_tool = self.WEB_SCRAPE_TOOL() + + @classmethod + def is_available(cls) -> bool: + if not cls.ENABLED_WEB_SEARCH_TOOLS: + return False + + available_searches = cls.get_available_search_tools() + + # False if empty, True otherwise + return bool(available_searches) + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Hybrid Web Search", + implementation=cls, + parameter_definitions={ + "query": { + "description": "Query for retrieval.", + "type": "str", + "required": True, + } + }, + is_visible=True, + is_available=cls.is_available(), + error_message=cls.generate_error_message(), + category=ToolCategory.WebSearch, + description=( + "Returns a list of relevant document snippets for a textual query " + "retrieved from the internet using a mix of any existing Web Search tools." + ) + ) # type: ignore + + @classmethod + def get_available_search_tools(cls): + available_search_tools = [] + + for search_name in cls.ENABLED_WEB_SEARCH_TOOLS: + for search_tool in cls.AVAILABLE_WEB_SEARCH_TOOLS: + if search_name == search_tool.ID and search_tool.is_available(): + available_search_tools.append(search_tool) + + return available_search_tools + + def _gather_search_tasks( + self, parameters: dict, ctx: Any, **kwargs: Any + ) -> List[Callable]: + tasks = [] + + # Add search tool calls + for search_tool in self.search_tools: + tasks.append(search_tool.call(parameters, ctx, **kwargs)) + + # Add web scrape tool calls + filtered_sites = kwargs.get(ToolArgument.SITE_FILTER, []) + for site in filtered_sites: + tasks.append(self.web_scrape_tool.call({"url": site}, ctx, **kwargs)) + + return tasks + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any + ) -> list[dict[str, Any]]: + # Retrieve query for reranking + query = parameters.get("query", "") + + # Handle domain filtering -> filter in search APIs + kwargs[ToolArgument.DOMAIN_FILTER] = self.DOMAIN_FILTER + # Handle site filtering -> perform web scraping on sites + kwargs[ToolArgument.SITE_FILTER] = self.SITE_FILTER + + tasks = self._gather_search_tasks(parameters, ctx, **kwargs) + + # Gather and run searches + results = await asyncio.gather(*tasks) + + flattened_results = list(itertools.chain.from_iterable(results)) + + reranked_results = await self.rerank_results( + query, + flattened_results, + model=kwargs.get("model_deployment"), + ctx=ctx, + **kwargs, + ) + + if not reranked_results: + return self.get_no_results_error() + + return reranked_results + + async def rerank_results( + self, + query: str, + results: List[Dict[str, Any]], + model: BaseDeployment, + ctx: Any, + **kwargs: Any, + ) -> List[Dict[str, Any]]: + if not results: + return [] + + rerank_batch_size = 500 + relevance_scores = [None for _ in range(len(results))] + for batch_start in range(0, len(results), rerank_batch_size): + results_batch = results[batch_start : batch_start + rerank_batch_size] + + documents = [ + f"{result.get('title', '')} {result.get('text')}" + for result in results_batch + if isinstance(result, dict) + and "text" in result + ] + + batch_output = await model.invoke_rerank( + query=query, + documents=documents, + ctx=ctx, + ) + for b in batch_output.get("results", []): + index = b.get("index", None) + relevance_score = b.get("relevance_score", None) + if index is not None: + relevance_scores[batch_start + index] = relevance_score + + reranked, seen_urls = [], [] + for _, result in sorted( + zip(relevance_scores, results), key=lambda x: x[0], reverse=True + ): + url = result.get("url") + if url not in seen_urls: + seen_urls.append(url) + reranked.append(result) + + return reranked[:self.POST_RERANK_MAX_RESULTS] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/lang_chain.py b/western_ai/canada/cohere-toolkit/src/backend/tools/lang_chain.py new file mode 100644 index 0000000000000000000000000000000000000000..ec7f4467506c79580aaaef921439bf5a357344a5 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/lang_chain.py @@ -0,0 +1,139 @@ +from typing import Any + +from langchain.text_splitter import CharacterTextSplitter +from langchain_cohere import CohereEmbeddings +from langchain_community.document_loaders import PyPDFLoader +from langchain_community.retrievers import WikipediaRetriever +from langchain_community.vectorstores import Chroma + +from backend.config.settings import Settings +from backend.schemas.context import Context +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.tools.base import BaseTool + +""" +Plug in your lang chain retrieval implementation here. +We have an example flows with wikipedia and vector DBs. + +More details: https://python.langchain.com/docs/integrations/retrievers +""" + + +class LangChainWikiRetriever(BaseTool): + """ + This class retrieves documents from Wikipedia using the langchain package. + This requires wikipedia package to be installed. + """ + ID = "wikipedia" + + def __init__(self, chunk_size: int = 300, chunk_overlap: int = 0): + self.chunk_size = chunk_size + self.chunk_overlap = chunk_overlap + + @classmethod + def is_available(cls) -> bool: + return True + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Wikipedia", + implementation=cls, + parameter_definitions={ + "query": { + "description": "Query for retrieval.", + "type": "str", + "required": True, + } + }, + kwargs={"chunk_size": 300, "chunk_overlap": 0}, + is_visible=True, + is_available=cls.is_available(), + error_message=cls.generate_error_message(), + category=ToolCategory.DataLoader, + description="Retrieves documents from Wikipedia.", + ) # type: ignore + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any, + ) -> list[dict[str, Any]]: + wiki_retriever = WikipediaRetriever() + query = parameters.get("query", "") + try: + docs = wiki_retriever.get_relevant_documents(query) + text_splitter = CharacterTextSplitter( + chunk_size=self.chunk_size, chunk_overlap=self.chunk_overlap + ) + documents = text_splitter.split_documents(docs) + except Exception as e: + return self.get_tool_error(details=str(e)) + + if not documents: + return self.get_no_results_error() + + return [ + { + "text": doc.page_content, + "title": doc.metadata.get("title", None), + "url": doc.metadata.get("source", None), + } + for doc in documents + ] + + +class LangChainVectorDBRetriever(BaseTool): + """ + This class retrieves documents from a vector database using the langchain package. + """ + ID = "vector_retriever" + COHERE_API_KEY = Settings().get('deployments.cohere_platform.api_key') + + def __init__(self, filepath: str): + self.filepath = filepath + + @classmethod + def is_available(cls) -> bool: + return cls.COHERE_API_KEY is not None + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Vector DB Retriever", + implementation=cls, + parameter_definitions={ + "query": { + "description": "Query for retrieval.", + "type": "str", + "required": True, + } + }, + is_visible=False, + is_available=cls.is_available(), + error_message=cls.generate_error_message(), + category=ToolCategory.DataLoader, + description="Retrieves documents from Wikipedia.", + ) # type: ignore + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any, + ) -> list[dict[str, Any]]: + cohere_embeddings = CohereEmbeddings(cohere_api_key=self.COHERE_API_KEY) + + # Load text files and split into chunks + try: + loader = PyPDFLoader(self.filepath) + text_splitter = CharacterTextSplitter(chunk_size=300, chunk_overlap=0) + pages = loader.load_and_split(text_splitter) + + # Create a vector store from the documents + db = Chroma.from_documents(documents=pages, embedding=cohere_embeddings) + query = parameters.get("query", "") + input_docs = db.as_retriever().get_relevant_documents(query) + except Exception as e: + return self.get_tool_error(details=str(e)) + if not input_docs: + return self.get_no_results_error() + + return [{"text": doc.page_content} for doc in input_docs] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/python_interpreter.py b/western_ai/canada/cohere-toolkit/src/backend/tools/python_interpreter.py new file mode 100644 index 0000000000000000000000000000000000000000..8d891772e238f41165b115693c3d9f717349b014 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/python_interpreter.py @@ -0,0 +1,109 @@ +import json +from typing import Any, Mapping + +import requests +from dotenv import load_dotenv + +from backend.config.settings import Settings +from backend.schemas.context import Context +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.tools.base import BaseTool + +load_dotenv() + + +class PythonInterpreter(BaseTool): + """ + This class calls arbitrary code against a Python interpreter. + It requires a URL at which the interpreter lives + """ + + ID = "toolkit_python_interpreter" + INTERPRETER_URL = Settings().get('tools.python_interpreter.url') + + @classmethod + def is_available(cls) -> bool: + return cls.INTERPRETER_URL is not None + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Python Interpreter", + implementation=cls, + parameter_definitions={ + "code": { + "description": ( + "Python code to execute using the Python interpreter with no internet access. " + "Do not generate code that tries to open files directly, instead use file contents passed to the interpreter, " + "then print output or save output to a file." + ), + "type": "str", + "required": True, + } + }, + is_visible=True, + is_available=cls.is_available(), + error_message=cls.generate_error_message(), + category=ToolCategory.Function, + description=( + "Executes python code and returns the result. The code runs " + "in a static sandbox without internet access and without interactive mode, " + "so print output or save output to a file." + ), + ) # type: ignore + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any, + ) -> list[dict[str, Any]]: + if not self.INTERPRETER_URL: + raise Exception("Python Interpreter tool called while URL not set") + + code = parameters.get("code", "") + try: + res = requests.post(self.INTERPRETER_URL, json={"code": code}) + clean_res = self._clean_response(res.json()) + except Exception as e: + return self.get_tool_error(details=str(e)) + + if not clean_res: + return self.get_no_results_error() + + return clean_res + + def _clean_response(self, result: Any) -> list[dict[str, str]]: + if "final_expression" in result: + result["final_expression"] = str(result["final_expression"]) + + # split up output files into separate result items, so that we may cite them individually + result_list = [result] + + output_files = result.pop("output_files", []) + for f in output_files: + result_list.append({"output_file": f}) + + for r in result_list: + if r.get("sucess") is not None: + r.update({"success": r.get("sucess")}) + del r["sucess"] + + if r.get("success") is True: + r.setdefault("text", r.get("std_out")) + elif r.get("success") is False: + error_message = r.get("error", {}).get("message", "") + # r.setdefault("text", error_message) + return self.get_tool_error(details=error_message) + elif r.get("output_file") and r.get("output_file").get("filename"): + if r["output_file"]["filename"] != "": + r.setdefault( + "text", f"Created output file {r['output_file']['filename']}" + ) + + # cast all values to strings, if it's a json object use double quotes + for key, value in r.items(): + if isinstance(value, Mapping): + r[key] = json.dumps(value) + else: + r[key] = str(value) + + return result_list diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/sharepoint/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tools/sharepoint/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..211d5e189b9fcf98e2263d7af0f1616276dd550b --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/sharepoint/__init__.py @@ -0,0 +1,11 @@ +from backend.tools.sharepoint.auth import SharepointAuth +from backend.tools.sharepoint.constants import ( + SHAREPOINT_TOOL_ID, +) +from backend.tools.sharepoint.tool import SharepointTool + +__all__ = [ + "SharepointAuth", + "SharepointTool", + "SHAREPOINT_TOOL_ID", +] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/sharepoint/auth.py b/western_ai/canada/cohere-toolkit/src/backend/tools/sharepoint/auth.py new file mode 100644 index 0000000000000000000000000000000000000000..2e06f9b15498506203144dafe7f0c9ebc0bce069 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/sharepoint/auth.py @@ -0,0 +1,147 @@ +import datetime +import json +import urllib.parse + +import requests +from fastapi import Request + +from backend.config.settings import Settings +from backend.crud import tool_auth as tool_auth_crud +from backend.database_models.database import DBSessionDep +from backend.database_models.tool_auth import ToolAuth as ToolAuthModel +from backend.schemas.tool_auth import UpdateToolAuth +from backend.services.auth.crypto import encrypt +from backend.services.logger.utils import LoggerFactory +from backend.tools.base import BaseToolAuthentication +from backend.tools.sharepoint.constants import SHAREPOINT_TOOL_ID +from backend.tools.utils.mixins import ToolAuthenticationCacheMixin + +logger = LoggerFactory().get_logger() + + +class SharepointAuth(BaseToolAuthentication, ToolAuthenticationCacheMixin): + TOOL_ID = SHAREPOINT_TOOL_ID + AUTH_ENDPOINT = "https://login.microsoftonline.com" + SCOPES = [ + "offline_access", + "https://graph.microsoft.com/.default", + ] + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.REDIRECT_URL = f"{self.BACKEND_HOST}/v1/tool/auth" + + self.SHAREPOINT_TENANT_ID = Settings().get('tools.sharepoint.tenant_id') + self.SHAREPOINT_CLIENT_ID = Settings().get('tools.sharepoint.client_id') + self.SHAREPOINT_CLIENT_SECRET = Settings().get('tools.sharepoint.client_secret') + + if not any([self.SHAREPOINT_TENANT_ID, self.SHAREPOINT_CLIENT_ID, self.SHAREPOINT_CLIENT_SECRET]): + raise ValueError( + "SHAREPOINT_TENANT_ID, SHAREPOINT_CLIENT_ID and SHAREPOINT_CLIENT_SECRET must be set to use Sharepoint Tool Auth." + ) + + def get_auth_url(self, user_id: str) -> str: + key = self.insert_tool_auth_cache(user_id, self.TOOL_ID) + state = {"key": key} + + params = { + "response_type": "code", + "client_id": self.SHAREPOINT_CLIENT_ID, + "scope": " ".join(self.SCOPES or []), + "redirect_uri": self.REDIRECT_URL, + "prompt": "select_account", + "state": json.dumps(state), + } + + return f"{self.AUTH_ENDPOINT}/{self.SHAREPOINT_TENANT_ID}/oauth2/v2.0/authorize?{urllib.parse.urlencode(params)}" + + def retrieve_auth_token( + self, request: Request, session: DBSessionDep, user_id: str + ) -> str|None: + url = f"{self.AUTH_ENDPOINT}/{self.SHAREPOINT_TENANT_ID}/oauth2/v2.0/token" + headers = { + "Content-Type": "application/x-www-form-urlencoded", + } + code = request.query_params.get("code", "") + payload = { + "grant_type": "authorization_code", + "code": code, + "scope": " ".join(self.SCOPES), + "redirect_uri": self.REDIRECT_URL, + "client_id": self.SHAREPOINT_CLIENT_ID, + "client_secret": self.SHAREPOINT_CLIENT_SECRET, + } + + error_message = "Error retrieving access token from Sharepoint Tool" + try: + response = requests.post(url, data=payload, headers=headers) + body = response.json() + except Exception as exc: + logger.error(event=f"[Sharepoint] Auth token error: {exc}") + return error_message + if not response.ok: + error = body.get("error") + error_description = body.get("error_description") + error_message = f"{error_message}: {error}. {error_description}" + logger.error(event=f"[Sharepoint] Auth token error: {error_message}") + return error_message + + tool_auth_crud.create_tool_auth( + session, + ToolAuthModel( + user_id=user_id, + tool_id=self.TOOL_ID, + token_type=body["token_type"], + encrypted_access_token=encrypt(body["access_token"]), + encrypted_refresh_token=encrypt(body["refresh_token"]), + expires_at=datetime.datetime.now() + + datetime.timedelta(seconds=int(body["expires_in"])), + ), + ) + + def try_refresh_token( + self, session: DBSessionDep, user_id: str, tool_auth: ToolAuthModel + ) -> bool: + url = f"{self.AUTH_ENDPOINT}/{self.SHAREPOINT_TENANT_ID}/oauth2/v2.0/token" + headers = { + "Content-Type": "application/x-www-form-urlencoded", + } + payload = { + "grant_type": "refresh_token", + "refresh_token": tool_auth.refresh_token, + "scope": " ".join(self.SCOPES), + "client_id": self.SHAREPOINT_CLIENT_ID, + "client_secret": self.SHAREPOINT_CLIENT_SECRET, + } + + error_message = "Error retrieving refreshing token from Sharepoint Tool" + try: + response = requests.post(url, data=payload, headers=headers) + body = response.json() + except Exception as exc: + logger.error(event=f"[Sharepoint] Auth token error: {exc}") + return False + if not response.ok: + error = body.get("error") + error_description = body.get("error_description") + error_message = f"{error_message}: {error}. {error_description}" + logger.error(event=f"[Sharepoint] Auth token error: {error_message}") + return False + + existing_tool_auth = tool_auth_crud.get_tool_auth( + session, self.TOOL_ID, user_id + ) + tool_auth_crud.update_tool_auth( + session, + existing_tool_auth, + UpdateToolAuth( + user_id=user_id, + tool_id=self.TOOL_ID, + token_type=body["token_type"], + encrypted_access_token=encrypt(body["access_token"]), + encrypted_refresh_token=encrypt(body["refresh_token"]), + expires_at=datetime.datetime.now() + + datetime.timedelta(seconds=body["expires_in"]), + ), + ) + return True diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/sharepoint/constants.py b/western_ai/canada/cohere-toolkit/src/backend/tools/sharepoint/constants.py new file mode 100644 index 0000000000000000000000000000000000000000..5a74ccf98aa8a851282702511896f17f766528af --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/sharepoint/constants.py @@ -0,0 +1,6 @@ +""" +Constants for Sharepoint Tool +""" + +SHAREPOINT_TOOL_ID = "sharepoint" +SEARCH_LIMIT = 10 diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/sharepoint/tool.py b/western_ai/canada/cohere-toolkit/src/backend/tools/sharepoint/tool.py new file mode 100644 index 0000000000000000000000000000000000000000..f0f5038fd2b856f00ea40a8267c7d4ebb02cab98 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/sharepoint/tool.py @@ -0,0 +1,167 @@ +from typing import Any + +import requests + +from backend.config.settings import Settings +from backend.database_models.database import get_session +from backend.schemas.context import Context +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.services.logger.utils import LoggerFactory +from backend.tools.base import BaseTool, ToolAuthException +from backend.tools.sharepoint.auth import SharepointAuth +from backend.tools.sharepoint.constants import SEARCH_LIMIT, SHAREPOINT_TOOL_ID +from backend.tools.sharepoint.utils import serialize_file_contents, serialize_metadata + +logger = LoggerFactory().get_logger() + + +class SharepointTool(BaseTool): + ID = SHAREPOINT_TOOL_ID + SHAREPOINT_TENANT_ID = Settings().get('tools.sharepoint.tenant_id') + SHAREPOINT_CLIENT_ID = Settings().get('tools.sharepoint.client_id') + SHAREPOINT_CLIENT_SECRET = Settings().get('tools.sharepoint.client_secret') + + BASE_URL = "https://graph.microsoft.com/v1.0" + SEARCH_ENTITY_TYPES = ["driveItem"] + DRIVE_ITEM_DATA_TYPE = "#microsoft.graph.driveItem" + + @classmethod + def is_available(cls) -> bool: + return all([ + cls.SHAREPOINT_TENANT_ID, + cls.SHAREPOINT_CLIENT_ID, + cls.SHAREPOINT_CLIENT_SECRET, + ]) + + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Sharepoint", + implementation=cls, + parameter_definitions={ + "query": { + "description": "Query to search Sharepoint documents with.", + "type": "str", + "required": True, + } + }, + is_visible=True, + is_available=SharepointTool.is_available(), + auth_implementation=SharepointAuth, + should_return_token=True, + error_message=cls.generate_error_message(), + category=ToolCategory.DataLoader, + description="Returns a list of relevant document snippets from the user's Sharepoint.", + ) # type: ignore + + def _prepare_auth(self, user_id: str) -> None: + sharepoint_auth = SharepointAuth() + session = next(get_session()) + if sharepoint_auth.is_auth_required(session, user_id=user_id): + session.close() + raise ToolAuthException( + "Sharepoint Tool auth Error: Agent creator credentials need to re-authenticate", + SHAREPOINT_TOOL_ID, + ) + + access_token = sharepoint_auth.get_token(session, user_id) + self.headers = { + "Authorization": f"Bearer {access_token}" + } + + def search(self, query: str) -> list[dict]: + request = { + "entityTypes": self.SEARCH_ENTITY_TYPES, + "query": { + "queryString": query, + "size": SEARCH_LIMIT, + }, + } + + error_message = "Error while searching with Sharepoint Tool" + try: + response = requests.post( + f"{self.BASE_URL}/search/query", + headers=self.headers, + json={"requests": [request]}, + ) + body = response.json() + except Exception as exc: + logger.error(event=f"[Sharepoint] Search error: {exc}") + raise Exception(error_message) from exc + if not response.ok: + error = body.get("error", {}) + error_code = error.get("code") + error_description = error.get("message") + error_message = f"{error_message}: {error_code}. {error_description}" + logger.error(event=f"[Sharepoint] Search error: {error_message}") + raise Exception(error_message) + + if not body.get("value"): + return [] + + return body["value"][0].get("hitsContainers", []) + + def get_drive_item_content(self, parent_drive_id: str, resource_id: str) -> bytes|None: + response = requests.get( + f"{self.BASE_URL}/drives/{parent_drive_id}/items/{resource_id}/content", + headers=self.headers, + ) + + # Fail gracefully when retrieving content + if not response.ok: + return None + + return response.content + + def collect_items(self, hits: list[dict]) -> list: + # Gather data + drive_items = [] + for hit in hits: + if hit["resource"]["@odata.type"] == self.DRIVE_ITEM_DATA_TYPE: + parent_drive_id = hit["resource"]["parentReference"]["driveId"] + resource_id = hit["resource"]["id"] + drive_item = self.get_drive_item_content( + parent_drive_id, resource_id + ) + + if drive_item: + drive_items.append((hit, drive_item)) + + return drive_items + + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any, + ) -> list[dict[str, Any]]: + user_id = str(kwargs.get("user_id", "")) + self._prepare_auth(user_id) + query = parameters.get("query", "").replace("'", "\\'") + search_response = self.search(query) + + hits = [] + for hit_container in search_response: + hits.extend(hit_container.get("hits", [])) + + drive_items = self.collect_items(hits) + + # Serialize results + results = [] + for hit, content in drive_items: + result = {} + if (resource := hit.get("resource")) is not None: + result.update(**serialize_metadata(resource)) + + content = serialize_file_contents(content, result.get("name", "")) + result.update({ + "text": content, + }) + results.append(result) + + if not results: + logger.info(event="[Sharepoint] No documents found.") + return self.get_no_results_error() + + return results diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/sharepoint/utils.py b/western_ai/canada/cohere-toolkit/src/backend/tools/sharepoint/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..0c372cfe418443f6aab6752af10acb2c00433631 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/sharepoint/utils.py @@ -0,0 +1,75 @@ +from backend.services.file import ( + CALENDAR_EXTENSION, + CSV_EXTENSION, + DOCX_EXTENSION, + EXCEL_EXTENSION, + EXCEL_OLD_EXTENSION, + JSON_EXTENSION, + MARKDOWN_EXTENSION, + PARQUET_EXTENSION, + PDF_EXTENSION, + TEXT_EXTENSION, + TSV_EXTENSION, + get_file_extension, + read_docx, + read_excel, + read_parquet, +) +from backend.services.utils import read_pdf + + +def serialize_metadata(resource: dict) -> dict: + data = {} + + # Only return primitive types, Coral cannot parse arrays/sub-dictionaries + stripped_resource = { + key: str(value) + for key, value in resource.items() + if isinstance(value, (str, int, bool)) + } + data.update({**stripped_resource}) + + if "name" in resource: + data["title"] = resource["name"] + + if "webUrl" in resource: + data["url"] = resource["webUrl"] + + return data + + +def serialize_file_contents(file_contents: bytes, filename: str) -> str: + """ + Reads the file contents based on the file extension + + Args: + file_contents(bytes): Contents of the file + filename (str): Name of the file + + Returns: + str: The file contents + + Raises: + ValueError: If the file extension is not supported + """ + file_extension = get_file_extension(filename) + + if file_extension == PDF_EXTENSION: + return read_pdf(file_contents) + elif file_extension == DOCX_EXTENSION: + return read_docx(file_contents) + elif file_extension == PARQUET_EXTENSION: + return read_parquet(file_contents) + elif file_extension in [ + TEXT_EXTENSION, + MARKDOWN_EXTENSION, + CSV_EXTENSION, + TSV_EXTENSION, + JSON_EXTENSION, + CALENDAR_EXTENSION + ]: + return file_contents.decode("utf-8") + elif file_extension in [EXCEL_EXTENSION, EXCEL_OLD_EXTENSION]: + return read_excel(file_contents) + + raise ValueError(f"File extension {file_extension} is not supported") diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/slack/__init__.py b/western_ai/canada/cohere-toolkit/src/backend/tools/slack/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..5e750975dda17db993e73b22ad4cf8faf04de62e --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/slack/__init__.py @@ -0,0 +1,11 @@ +from backend.tools.slack.auth import SlackAuth +from backend.tools.slack.constants import ( + SLACK_TOOL_ID, +) +from backend.tools.slack.tool import SlackTool + +__all__ = [ + "SlackAuth", + "SlackTool", + "SLACK_TOOL_ID", +] diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/slack/auth.py b/western_ai/canada/cohere-toolkit/src/backend/tools/slack/auth.py new file mode 100644 index 0000000000000000000000000000000000000000..baa8fa2d9ed75690fff24898241b5c3b5df3bc97 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/slack/auth.py @@ -0,0 +1,156 @@ +import datetime +import json +import urllib.parse + +import requests +from fastapi import Request + +from backend.config.settings import Settings +from backend.crud import tool_auth as tool_auth_crud +from backend.database_models import ToolAuth +from backend.database_models.database import DBSessionDep +from backend.database_models.tool_auth import ToolAuth as ToolAuthModel +from backend.schemas.tool_auth import UpdateToolAuth +from backend.services.auth.crypto import encrypt +from backend.services.logger.utils import LoggerFactory +from backend.tools.base import BaseToolAuthentication +from backend.tools.slack.constants import SLACK_TOOL_ID +from backend.tools.utils.mixins import ToolAuthenticationCacheMixin + +logger = LoggerFactory().get_logger() + + +class SlackAuth(BaseToolAuthentication, ToolAuthenticationCacheMixin): + TOOL_ID = SLACK_TOOL_ID + AUTH_ENDPOINT = "https://slack.com/oauth/v2/authorize" + TOKEN_ENDPOINT = "https://slack.com/api/oauth.v2.access" + EXCHANGE_ENDPOINT = "https://slack.com/api/oauth.v2.exchange" + DEFAULT_BOT_SCOPES = ['search:read.public'] + DEFAULT_USER_SCOPES = ['search:read'] + + def __init__(self): + super().__init__() + + self.SLACK_CLIENT_ID = Settings().get('tools.slack.client_id') + self.SLACK_CLIENT_SECRET = Settings().get('tools.slack.client_secret') + self.USER_SCOPES = Settings().get('tools.slack.user_scopes') or self.DEFAULT_USER_SCOPES + self.REDIRECT_URL = f"{self.BACKEND_HOST}/v1/tool/auth" + + if any([ + self.SLACK_CLIENT_ID is None, + self.SLACK_CLIENT_SECRET is None + ]): + raise ValueError( + "SLACK_CLIENT_ID and SLACK_CLIENT_SECRET must be set to use Slack Tool Auth." + ) + + def get_auth_url(self, user_id: str) -> str: + key = self.insert_tool_auth_cache(user_id, self.TOOL_ID) + state = {"key": key} + + params = { + "client_id": self.SLACK_CLIENT_ID, + "user_scope": " ".join(self.USER_SCOPES or []), + "redirect_uri": self.REDIRECT_URL, + "state": json.dumps(state), + } + + return f"{self.AUTH_ENDPOINT}?{urllib.parse.urlencode(params)}" + + def retrieve_auth_token( + self, request: Request, session: DBSessionDep, user_id: str + ) -> str: + if request.query_params.get("error"): + error = request.query_params.get("error") or "Unknown error" + logger.error(event=f"[Slack Tool] Auth token error: {error}.") + return error + + body = { + "code": request.query_params.get("code"), + "client_id": self.SLACK_CLIENT_ID, + "client_secret": self.SLACK_CLIENT_SECRET, + } + + url_encoded_body = urllib.parse.urlencode(body) + headers = { + "Content-Type": 'application/x-www-form-urlencoded', + } + response = requests.post(self.TOKEN_ENDPOINT, data=url_encoded_body, headers=headers) + + response_body = response.json() + + if response.status_code != 200 or response_body.get("ok") is False: + logger.error( + event=f"[Slack Tool] Error retrieving auth token: {response_body}" + ) + return str(response) + + token_data = response_body.get("authed_user", None) + + if token_data is None: + logger.error( + event=f"[Slack Tool] Error retrieving auth token: {response_body}" + ) + return str(response) + + tool_auth_crud.create_tool_auth( + session, + ToolAuthModel( + user_id=user_id, + tool_id=self.TOOL_ID, + token_type=token_data.get("token_type"), + encrypted_access_token=encrypt(token_data.get("access_token")), + encrypted_refresh_token=encrypt(token_data.get("refresh_token")), + expires_at=datetime.datetime.now() + + datetime.timedelta(seconds=token_data.get("expires_in")), + ), + ) + + return "" + + def try_refresh_token(self, session: DBSessionDep, user_id: str, tool_auth: ToolAuth) -> bool: + body = { + "client_id": self.SLACK_CLIENT_ID, + "client_secret": self.SLACK_CLIENT_SECRET, + "refresh_token": tool_auth.refresh_token, + "grant_type": "refresh_token", + } + headers = { + "Content-Type": 'application/x-www-form-urlencoded', + } + url_encoded_body = urllib.parse.urlencode(body) + + response = requests.post(self.TOKEN_ENDPOINT, data=url_encoded_body, headers=headers) + response_body = response.json() + + if response.status_code != 200 or response_body.get("ok") is False: + logger.error( + event=f"[Slack Tool] Error refreshing token: {response_body}" + ) + return False + + token_data = response_body.get("authed_user", None) + if token_data is None: + logger.error( + event=f"[Slack Tool] Error retrieving auth token: {response_body}" + ) + return False + + existing_tool_auth = tool_auth_crud.get_tool_auth( + session, self.TOOL_ID, user_id + ) + tool_auth_crud.update_tool_auth( + session, + existing_tool_auth, + UpdateToolAuth( + user_id=user_id, + tool_id=self.TOOL_ID, + token_type=token_data.get("token_type"), + encrypted_access_token=encrypt(token_data.get("access_token")), + encrypted_refresh_token=encrypt(token_data.get("refresh_token")), + expires_at=datetime.datetime.now() + + datetime.timedelta(seconds=token_data.get("expires_in")), + ), + ) + + return True diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/slack/client.py b/western_ai/canada/cohere-toolkit/src/backend/tools/slack/client.py new file mode 100644 index 0000000000000000000000000000000000000000..06f4e726207252703b6b1245293ea202ddd836ed --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/slack/client.py @@ -0,0 +1,10 @@ +from slack_sdk import WebClient + + +class SlackClient: + def __init__(self, auth_token, search_limit=20): + self.client = WebClient(token=auth_token) + self.search_limit = search_limit + + def search_all(self, query): + return self.client.search_all(query=query, count=self.search_limit) diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/slack/constants.py b/western_ai/canada/cohere-toolkit/src/backend/tools/slack/constants.py new file mode 100644 index 0000000000000000000000000000000000000000..9070ecce4816af40043e1728aff2ec193ffd0516 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/slack/constants.py @@ -0,0 +1,2 @@ +SEARCH_LIMIT = 10 +SLACK_TOOL_ID = "slack" diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/slack/tool.py b/western_ai/canada/cohere-toolkit/src/backend/tools/slack/tool.py new file mode 100644 index 0000000000000000000000000000000000000000..7ba9a15576eb5e9d1d807600277ae2b49eaf7d9c --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/slack/tool.py @@ -0,0 +1,85 @@ +from typing import Any + +from backend.config.settings import Settings +from backend.crud import tool_auth as tool_auth_crud +from backend.schemas.context import Context +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.services.logger.utils import LoggerFactory +from backend.tools.base import BaseTool +from backend.tools.slack.auth import SlackAuth +from backend.tools.slack.constants import SEARCH_LIMIT, SLACK_TOOL_ID +from backend.tools.slack.utils import get_slack_service + +logger = LoggerFactory().get_logger() + + +class SlackTool(BaseTool): + """ + Tool that searches Slack for messages and files based on a query. + """ + + ID = SLACK_TOOL_ID + CLIENT_ID = Settings().get('tools.slack.client_id') + CLIENT_SECRET = Settings().get('tools.slack.client_secret') + + @classmethod + def is_available(cls) -> bool: + return cls.CLIENT_ID is not None and cls.CLIENT_SECRET is not None + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Slack", + implementation=cls, + parameter_definitions={ + "query": { + "description": "Query to search slack.", + "type": "str", + "required": True, + } + }, + is_visible=True, + is_available=cls.is_available(), + auth_implementation=SlackAuth, + should_return_token=False, + error_message=cls.generate_error_message(), + category=ToolCategory.DataLoader, + description="Returns a list of relevant document snippets from slack.", + ) # type: ignore + + @classmethod + def _handle_tool_specific_errors(cls, error: Exception, **kwargs: Any) -> None: + message = "[Slack] Tool Error: {}".format(str(error)) + + if error: + session = kwargs["session"] + user_id = kwargs["user_id"] + tool_auth_crud.delete_tool_auth( + db=session, user_id=user_id, tool_id=SLACK_TOOL_ID + ) + + logger.error( + event="[Slack] Auth token error: Please refresh the page and re-authenticate." + ) + raise Exception(message) + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any, + ) -> list[dict[str, Any]]: + user_id = kwargs.get("user_id", "") + query = parameters.get("query", "") + + # Search Slack + slack_service = get_slack_service(user_id=user_id, search_limit=SEARCH_LIMIT) + try: + all_results = slack_service.search_all(query=query) + results = slack_service.serialize_results(all_results) + except Exception as e: + return self.get_tool_error(details=str(e)) + + if not results: + return self.get_no_results_error() + + return results + diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/slack/utils.py b/western_ai/canada/cohere-toolkit/src/backend/tools/slack/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..de6bb8ad220395337bb55587f497d00e16890901 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/slack/utils.py @@ -0,0 +1,78 @@ + +from backend.database_models.database import get_session +from backend.services.logger.utils import LoggerFactory +from backend.tools.base import ToolAuthException +from backend.tools.slack import SlackAuth +from backend.tools.slack.client import SlackClient +from backend.tools.slack.constants import SEARCH_LIMIT, SLACK_TOOL_ID + +logger = LoggerFactory().get_logger() + + +class SlackService: + def __init__(self, user_id: str, auth_token: str, search_limit=SEARCH_LIMIT): + self.user_id = user_id + self.auth_token = auth_token + self.client = SlackClient(auth_token=auth_token, search_limit=search_limit) + + def search_all(self, query: str): + return self.client.search_all(query=query) + + def serialize_results(self, response): + results = [] + for match in response["messages"]["matches"]: + document = self.extract_message_data(match) + results.append(document) + for match in response["files"]["matches"]: + document = self.extract_files_data(match, response["query"]) + results.append(document) + + return results + + @staticmethod + def extract_message_data(message_json): + document = {} + document["type"] = "message" + if "text" in message_json: + document["text"] = str(message_json.pop("text")) + if "permalink" in message_json: + document["url"] = str(message_json.pop("permalink")) + if "channel" in message_json and "name" in message_json["channel"]: + document["title"] = str(message_json["channel"]["name"]) + + return document + + @staticmethod + def extract_files_data(message_json, query=""): + document = {} + document["type"] = "file" + if "permalink" in message_json: + document["url"] = str(message_json.pop("permalink")) + if "title" in message_json: + document["title"] = str(message_json["title"]) + document["text"] = f"{query} in {str(message_json['title'])}" + + return document + + +def get_slack_service(user_id: str, search_limit=SEARCH_LIMIT) -> SlackService: + slack_auth = SlackAuth() + auth_token = None + session = next(get_session()) + if slack_auth.is_auth_required(session, user_id=user_id): + session.close() + raise ToolAuthException( + "Slack Tool auth Error: Agent creator credentials need to re-authenticate", + SLACK_TOOL_ID, + ) + + auth_token = slack_auth.get_token(session=session, user_id=user_id) + if auth_token is None: + session.close() + raise Exception("Slack Tool Error: No credentials found") + + service = SlackService(user_id=user_id, auth_token=auth_token, search_limit=search_limit) + session.close() + return service + + diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/tavily_search.py b/western_ai/canada/cohere-toolkit/src/backend/tools/tavily_search.py new file mode 100644 index 0000000000000000000000000000000000000000..40058dc3cd979ac6c0b26ea00e15699dbf252c9d --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/tavily_search.py @@ -0,0 +1,86 @@ +from typing import Any + +from tavily import TavilyClient + +from backend.config.settings import Settings +from backend.schemas.context import Context +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.tools.base import BaseTool, ToolArgument + + +class TavilyWebSearch(BaseTool): + ID = "tavily_web_search" + TAVILY_API_KEY = Settings().get('tools.tavily_web_search.api_key') + + def __init__(self): + self.client = TavilyClient(api_key=self.TAVILY_API_KEY) + + @classmethod + def is_available(cls) -> bool: + return cls.TAVILY_API_KEY is not None + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Web Search", + implementation=cls, + parameter_definitions={ + "query": { + "description": "Query to search the internet with", + "type": "str", + "required": True, + } + }, + is_visible=False, + is_available=cls.is_available(), + error_message=cls.generate_error_message(), + category=ToolCategory.WebSearch, + description="Returns a list of relevant document snippets for a textual query retrieved from the internet.", + ) # type: ignore + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any, + ) -> list[dict[str, Any]]: + logger = ctx.get_logger() + # Gather search parameters + query = parameters.get("query", "") + + # Get domain filtering from kwargs + filtered_domains = kwargs.get(ToolArgument.DOMAIN_FILTER, []) + + try: + result = self.client.search( + query=query, + search_depth="advanced", + include_raw_content=True, + include_domains=filtered_domains, + ) + except Exception as e: + logger.error(f"Failed to perform Tavily web search: {str(e)}") + return self.get_tool_error(details=str(e)) + + results = result.get("results", []) + + if not results: + return self.get_no_results_error() + + tool_results = [] + for result in results: + # Retrieve snippets from raw content if exists + raw_content = result.get("raw_content") + if raw_content: + # Get other snippets + snippets = raw_content.split("\n") + for snippet in snippets: + if result.get("content") != snippet: + if len(snippet.split()) <= 10: + continue # Skip snippets with less than 10 words + + tool_results.append({ + "text": snippet.strip(), + "title": result.get("title"), + "url": result.get("url"), + }) + + return tool_results diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/utils/async_download.py b/western_ai/canada/cohere-toolkit/src/backend/tools/utils/async_download.py new file mode 100644 index 0000000000000000000000000000000000000000..80a9c976c241958baf0fa8fa606f1d503c5ab19b --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/utils/async_download.py @@ -0,0 +1,47 @@ +import asyncio +import functools +from typing import List + +import aiohttp + +from backend.services.logger.utils import LoggerFactory + +TIMEOUT = aiohttp.ClientTimeout(total=120) + +logger = LoggerFactory().get_logger() + + +def sync_perform(id_to_urls: List[str], access_token: str) -> dict[str, str]: + return asyncio.run(_download_files(id_to_urls, access_token)) + + +async def async_perform(id_to_urls: List[str], access_token: str) -> dict[str, str]: + return await _download_files(id_to_urls, access_token) + + +async def _download_files( + id_to_urls: dict[str, str], access_token: str +) -> dict[str, str]: + async with aiohttp.ClientSession() as session: + tasks = [ + _download(session, id, url, access_token) + for (id, url) in id_to_urls.items() + ] + id_to_texts = await asyncio.gather(*tasks) + return functools.reduce(lambda x, y: x | y, id_to_texts) + + +async def _download( + session: aiohttp.ClientSession, id: str, url: str, access_token: str +): + headers = {"Authorization": f"Bearer {access_token}"} + try: + async with session.get(url, headers=headers, timeout=TIMEOUT) as response: + return {id: await response.text()} + except Exception as error: + logger.error( + event="[Async Download]: Error fetching url", + url=url, + error=error, + ) + return {} diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/utils/download.py b/western_ai/canada/cohere-toolkit/src/backend/tools/utils/download.py new file mode 100644 index 0000000000000000000000000000000000000000..31489b1ffa586345ac531833fc3c377f957964ed --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/utils/download.py @@ -0,0 +1,40 @@ +from concurrent import futures + +import httpx + +from backend.services.logger.utils import LoggerFactory + +logger = LoggerFactory().get_logger() + + +def perform_batch( + httpx_client: httpx.Client, id_to_urls: dict[str, str], access_token: str +) -> dict[str, str]: + texts = [] + with futures.ThreadPoolExecutor(max_workers=10) as executor: + futures_list = [ + executor.submit(_download, httpx_client, id, url, access_token) + for (id, url) in id_to_urls.items() + ] + for future in futures.as_completed(futures_list): + try: + texts.append(future.result()) + except Exception as e: + raise e + + return texts + + +def perform_single(httpx_client: httpx.Client, url: str, access_token: str) -> str: + return _download(httpx_client=httpx_client, url=url, access_token=access_token) + + +def _download(httpx_client: httpx.Client, url: str, access_token: str): + headers = {"Authorization": f"Bearer {access_token}"} + try: + response = httpx_client.get(url, headers=headers, follow_redirects=True) + response = response.text + return response + except Exception as error: + logger.error("Error fetching", url=url, error=error) + return "" diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/utils/mixins.py b/western_ai/canada/cohere-toolkit/src/backend/tools/utils/mixins.py new file mode 100644 index 0000000000000000000000000000000000000000..14f25d6bd0a4f6aea9e404b90cd0b23a3351860c --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/utils/mixins.py @@ -0,0 +1,22 @@ +from backend.services.auth.crypto import encrypt +from backend.services.cache import cache_get_dict, cache_put + + +class ToolAuthenticationCacheMixin: + def insert_tool_auth_cache(self, user_id: str, tool_id: str) -> str: + """ + Generates a token from a composite string formed by user_id + tool_id, and stores it in + cache. + """ + value = user_id + tool_id + # Encrypt value with Fernet and convert to string + key = encrypt(value).decode() + + # Existing cache entry + if cache_get_dict(key): + return key + + payload = {"user_id": user_id, "tool_id": tool_id} + cache_put(key, payload) + + return key diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/utils/tools_checkers.py b/western_ai/canada/cohere-toolkit/src/backend/tools/utils/tools_checkers.py new file mode 100644 index 0000000000000000000000000000000000000000..5d11c328a1733519022dba3dd08939d248e358b6 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/utils/tools_checkers.py @@ -0,0 +1,126 @@ +from typing import get_args, get_origin + +from backend.schemas.tool import ToolCategory, ToolDefinition + + +def tool_has_category(tool: ToolDefinition, category: ToolCategory) -> bool: + """ + Check if a tool has a specific category. + + Args: + tool (ToolDefinition): The tool to check. + category (ToolCategory): The category to check for. + + Returns: + bool: True if the tool has the category, False otherwise. + """ + return tool.category == category + + +def check_type(param_value, type_description: str) -> bool: + """ + Check if the parameter value is of the expected type. + type description is a string representation of the type, + e.g. "str", "int", "List[str]", "List[Dict[str, int]]", etc. + For the complex types, we need to parse the string and check the type recursively. + + Args: + param_value: The value to check. + type_description: The expected type in string representation. + + Returns: + bool: True if the value is of the expected type, False otherwise. + """ + try: + # Convert the type string into a type object, type description is set by the tool author so no security risk + expected_type = eval(type_description) + return _check_type_recursive(param_value, expected_type) + except Exception as e: + print(f"Error during type checking: {e}") + return False + + +def _check_type_recursive(value, expected_type) -> bool: + """ + Recursively check the type of the value. + Types can be base (int, str, ...) or complex (List[str], Dict[str, int], ...). + So for complex types, we need to check the type recursively. + + Args: + value: The value to check. + expected_type: The expected type. + + Returns: + bool: True if the value is of the expected type, False otherwise. + """ + origin = get_origin(expected_type) + + if origin is None: # Base types (int, str, ...) + return isinstance(value, expected_type) + + if origin is list: # Check if the value is a list + if not isinstance(value, list): + return False + element_type = get_args(expected_type)[0] + return all(_check_type_recursive(item, element_type) for item in value) + + if origin is tuple: # Tuples + # trying to help to model with tuple type by converting lists to tuples, Cohere model passed tuples as list + converted_value = tuple(value) if isinstance(value, list) else value + # Check if the value is a tuple and has the same length as the expected type + if not isinstance(converted_value, tuple) or len(converted_value) != len(get_args(expected_type)): + return False + return all( + _check_type_recursive(item, arg_type) + for item, arg_type in zip(value, get_args(expected_type)) + ) + + if origin is dict: # Dictionaries + if not isinstance(value, dict): + return False + key_type, value_type = get_args(expected_type) + return all( + _check_type_recursive(k, key_type) and _check_type_recursive(v, value_type) + for k, v in value.items() + ) + + # NOTE: Maybe we need to handle more types in the future, depends on the use in tools and models + return False + + +def check_tool_parameters(tool_definition: ToolDefinition) -> None: + """ + Decorator to check the parameters of a tool that was passed to a method by the model. + + Args: + tool_definition (ToolDefinition): The tool definition to check the parameters against. + + Raises: + ValueError: If a required parameter is missing. + TypeError: If a parameter has an invalid type. + """ + + def decorator(func): + def wrapper(self, *args, **kwargs): + parameter_definitions = tool_definition(self).parameter_definitions + passed_method_params = kwargs.get("parameters", {}) or args[0] + # Validate parameters + for param, rules in parameter_definitions.items(): + is_required = rules.get("required", False) + if param not in passed_method_params: + if is_required: + raise ValueError(f"Model didn't pass required parameter: {param}") + else: + value = passed_method_params[param] + if not value and is_required: + raise ValueError(f"Model passed empty value for required parameter: {param}") + if not check_type(value, rules["type"]): + raise TypeError( + f"Model passed invalid parameter. Parameter '{param}' must be of type {rules['type']}, but got {type(value).__name__}" + ) + + return func(self, *args, **kwargs) + + return wrapper + + return decorator diff --git a/western_ai/canada/cohere-toolkit/src/backend/tools/web_scrape.py b/western_ai/canada/cohere-toolkit/src/backend/tools/web_scrape.py new file mode 100644 index 0000000000000000000000000000000000000000..549439120a3cde9085cc4a8f23a1a0e87b8b64c0 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/backend/tools/web_scrape.py @@ -0,0 +1,107 @@ +from typing import Any + +import aiohttp +from bs4 import BeautifulSoup + +from backend.schemas.context import Context +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.services.logger.utils import LoggerFactory +from backend.services.utils import read_pdf +from backend.tools.base import BaseTool +from backend.tools.constants import ASYNC_TIMEOUT + +logger = LoggerFactory().get_logger() + + +class WebScrapeTool(BaseTool): + ID = "web_scrape" + + @classmethod + def is_available(cls) -> bool: + return True + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Web Scrape", + implementation=cls, + parameter_definitions={ + "url": { + "description": "The url to scrape.", + "type": "str", + "required": True, + }, + "query": { + "description": "The query to use to select the most relevant passages to return. Using an empty string will return the passages in the order they appear on the webpage", + "type": "str", + "required": False, + }, + }, + is_visible=False, + is_available=cls.is_available(), + error_message=cls.generate_error_message(), + category=ToolCategory.DataLoader, + description="Scrape and returns the textual contents of a webpage as a list of passages for a given url.", + ) # type: ignore + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any + ) -> list[dict[str, Any]]: + url = parameters.get("url", "") + + async with aiohttp.ClientSession(timeout=ASYNC_TIMEOUT) as session: + try: + async with session.get(url) as response: + if response.status != 200: + error_message = f"HTTP {response.status} {response.reason}" + return [ + { + "text": f"Cannot open and scrape URL {url}, Error: {error_message}", + "url": url, + } + ] + + return await self.handle_response(response, url) + + except aiohttp.ClientError as e: + return [{ + "text": f"Client error using web scrape: {str(e)}", + "url": url, + }] + except Exception as e: + return [{ + "text": f"Request failed using web scrape: {str(e)}", + "url": url, + }] + + async def handle_response(self, response: aiohttp.ClientResponse, url: str): + content_type = response.headers.get("content-type", "") + results = [] + + # If URL is a PDF, read contents using helper function + if "application/pdf" in content_type: + results.append({ + "text": read_pdf(response.content), + "url": url, + }) + elif "text/html" in content_type: + content = await response.text() + soup = BeautifulSoup(content, "html.parser") + + text = soup.get_text().replace("\n", "") + title = next((tag.text for tag in soup.find_all('h1')), None) + + data = { + "text": text, + "url": url, + } + + if title: + data["title"] = title + + results.append(data) + else: + raise ValueError(f"Unsupported Content Type using web scrape: {content_type}") + + return results diff --git a/western_ai/canada/cohere-toolkit/src/community/__init__.py b/western_ai/canada/cohere-toolkit/src/community/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/community/config/__init__.py b/western_ai/canada/cohere-toolkit/src/community/config/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/canada/cohere-toolkit/src/community/config/deployments.py b/western_ai/canada/cohere-toolkit/src/community/config/deployments.py new file mode 100644 index 0000000000000000000000000000000000000000..30c12a2a076fc0613a95da3748c9caf5808d5e76 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/config/deployments.py @@ -0,0 +1,6 @@ +from community.model_deployments.community_deployment import CommunityDeployment + +# Add the below for local model deployments +# from community.model_deployments.local_model import LocalModelDeployment + +AVAILABLE_MODEL_DEPLOYMENTS = { d.name(): d for d in CommunityDeployment.__subclasses__() } diff --git a/western_ai/canada/cohere-toolkit/src/community/config/tools.py b/western_ai/canada/cohere-toolkit/src/community/config/tools.py new file mode 100644 index 0000000000000000000000000000000000000000..69ae4792c35614e9dd861f4119a710d517c2cada --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/config/tools.py @@ -0,0 +1,31 @@ +from enum import Enum + +from backend.schemas.tool import ToolDefinition +from community.tools import ( + ArxivRetriever, + ClinicalTrials, + ConnectorRetriever, + LlamaIndexUploadPDFRetriever, + PubMedRetriever, + WolframAlpha, +) + + +class CommunityTool(Enum): + Arxiv = ArxivRetriever + Connector = ConnectorRetriever + Pub_Med = PubMedRetriever + File_Upload_LlamaIndex = LlamaIndexUploadPDFRetriever + Wolfram_Alpha = WolframAlpha + ClinicalTrials = ClinicalTrials + + +def get_community_tools() -> dict[str, ToolDefinition]: + # Get list of implementations from Tool Enum + tool_classes = [tool.value for tool in CommunityTool] + # Generate dictionary of ToolDefinitions keyed by Tool ID + community_tools = { + tool.ID: tool.get_tool_definition() for tool in tool_classes + } + + return community_tools diff --git a/western_ai/canada/cohere-toolkit/src/community/model_deployments/__init__.py b/western_ai/canada/cohere-toolkit/src/community/model_deployments/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..4f15c77a6d499aba39d83f77495f1e4bb271269d --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/model_deployments/__init__.py @@ -0,0 +1,5 @@ +from community.model_deployments.hugging_face import HuggingFaceDeployment + +__all__ = [ + "HuggingFaceDeployment", +] diff --git a/western_ai/canada/cohere-toolkit/src/community/model_deployments/community_deployment.py b/western_ai/canada/cohere-toolkit/src/community/model_deployments/community_deployment.py new file mode 100644 index 0000000000000000000000000000000000000000..98e2785b96b03117b43e9ea6f52a287c3f8b5530 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/model_deployments/community_deployment.py @@ -0,0 +1,7 @@ +from backend.model_deployments.base import BaseDeployment + + +class CommunityDeployment(BaseDeployment): + @classmethod + def is_community(cls): + return True diff --git a/western_ai/canada/cohere-toolkit/src/community/model_deployments/hugging_face.py b/western_ai/canada/cohere-toolkit/src/community/model_deployments/hugging_face.py new file mode 100644 index 0000000000000000000000000000000000000000..915da9562a72426372db551dc15192ffa347d215 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/model_deployments/hugging_face.py @@ -0,0 +1,147 @@ +import re +from typing import Any, Dict, List + +from transformers import AutoModelForCausalLM, AutoTokenizer + +from backend.chat.enums import StreamEvent +from backend.schemas.chat import ChatMessage +from backend.schemas.cohere_chat import CohereChatRequest +from backend.schemas.context import Context +from community.model_deployments.community_deployment import CommunityDeployment + + +class HuggingFaceDeployment(CommunityDeployment): + """ + The first time you run this code, it will download all the shards of the model from the Hugging Face model hub. + This usually takes a while, so you might want to run this code separately and not as part of the toolkit. + For that, you can run the following command: + poetry run python3 src/community/model_deployments/hugging_face.py + """ + + DEFAULT_MODELS = [ + "CohereForAI/c4ai-command-r-v01", + "CohereForAI/c4ai-command-r-plus", + ] + + def __init__(self, **kwargs: Any): + self.ctx = kwargs.get("ctx", None) + + @classmethod + def name(cls) -> str: + return "Hugging Face" + + @classmethod + def env_vars(cls) -> List[str]: + return [] + + @classmethod + def rerank_enabled(self) -> bool: + return False + + @classmethod + def list_models(cls) -> List[str]: + if not HuggingFaceDeployment.is_available(): + return [] + + return cls.DEFAULT_MODELS + + @classmethod + def is_available(cls) -> bool: + return True + + async def invoke_chat(self, chat_request: CohereChatRequest, **kwargs: Any) -> Any: + model_id = chat_request.model + if model_id == "command-r": + model_id = self.DEFAULT_MODELS[0] + + tokenizer = AutoTokenizer.from_pretrained(model_id) + model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto") + + # Format message with the command-r-plus chat template + messages = self._build_chat_history( + chat_request.chat_history, chat_request.message + ) + input_ids = tokenizer.apply_chat_template( + messages, tokenize=True, add_generation_prompt=True, return_tensors="pt" + ) + + gen_tokens = model.generate( + input_ids, + max_new_tokens=100, + do_sample=True, + temperature=0.3, + ) + + gen_text = self.clean_text(tokenizer.decode(gen_tokens[0])) + + return {"text": gen_text} + + async def invoke_chat_stream( + self, chat_request: CohereChatRequest, ctx: Context, **kwargs: Any + ) -> Any: + """ + Built in streamming is not supported, so this function wraps the invoke_chat function + to return a single response. + + Args: + chat_request: Chat request + ctx: Context + **kwargs: Additional arguments + """ + yield { + "event_type": StreamEvent.STREAM_START, + "generation_id": "", + } + + gen_text = await self.invoke_chat(chat_request) + + yield { + "event_type": StreamEvent.TEXT_GENERATION, + "text": gen_text.get("text", ""), + } + + yield { + "event_type": StreamEvent.STREAM_END, + "finish_reason": "COMPLETE", + } + + async def invoke_rerank( + self, query: str, documents: List[Dict[str, Any]], ctx: Context, **kwargs: Any + ) -> Any: + return None + + def _build_chat_history( + self, chat_history: List[ChatMessage], message: str + ) -> List[Dict[str, Any]]: + """ + Build chat history for the model. + + Args: + chat_history: Chat history + message: User message + + Returns: + List[Dict[str, Any]]: Chat history + """ + messages = [] + + for message in chat_history: + messages.append({"role": message["role"], "content": message["message"]}) + + messages.append({"role": "user", "content": message}) + + return messages + + def _clean_text(self, text: str) -> str: + """ + Clean text by removing all text between <| and |> tags. + + Args: + text: Text to clean + + Returns: + str: Cleaned text + """ + cleaned_text = re.sub(r'<\|USER_TOKEN\|>.*?<\|END_OF_TURN_TOKEN\|>', '', text) + cleaned_text = re.sub(r'<[^>]+>', '', cleaned_text) + return cleaned_text.strip() diff --git a/western_ai/canada/cohere-toolkit/src/community/model_deployments/local_model.py b/western_ai/canada/cohere-toolkit/src/community/model_deployments/local_model.py new file mode 100644 index 0000000000000000000000000000000000000000..a8e38e1374b26065684460a3a7a8383d99878e7f --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/model_deployments/local_model.py @@ -0,0 +1,272 @@ +import asyncio +from typing import Any, Dict, List + +from llama_cpp import Llama + +from backend.schemas.cohere_chat import CohereChatRequest + +# To use local models install poetry with: poetry install --with setup,community,local-model --verbose +from backend.schemas.context import Context +from community.model_deployments.community_deployment import CommunityDeployment + + +class LocalModelDeployment(CommunityDeployment): + def __init__(self, model_path: str, template: str = None): + self.prompt_template = PromptTemplate() + self.model_path = model_path + self.template = template + + @classmethod + def name(cls) -> str: + return "Local Model" + + @classmethod + def env_vars(cls) -> List[str]: + return [] + + @classmethod + def rerank_enabled(cls) -> bool: + return False + + @classmethod + def list_models(cls) -> List[str]: + return [] + + @classmethod + def is_available(cls) -> bool: + return True + + async def invoke_chat_stream( + self, chat_request: CohereChatRequest, **kwargs: Any + ) -> Any: + model = self._get_model() + + if chat_request.max_tokens is None: + chat_request.max_tokens = 200 + + if len(chat_request.documents) == 0: + prompt = self.prompt_template.dummy_chat_template( + chat_request.message, chat_request.chat_history + ) + else: + prompt = self.prompt_template.dummy_rag_template( + chat_request.message, chat_request.chat_history, chat_request.documents + ) + + stream = model( + prompt, + stream=True, + max_tokens=chat_request.max_tokens, + temperature=chat_request.temperature, + ) + + yield { + "event_type": "stream-start", + "generation_id": "", + } + + for item in stream: + yield { + "event_type": "text-generation", + "text": item["choices"][0]["text"], + } + + yield { + "event_type": "stream-end", + "finish_reason": "COMPLETE", + } + + async def invoke_chat( + self, chat_request: CohereChatRequest, ctx: Context, **kwargs: Any + ) -> Any: + model = self._get_model() + + if chat_request.max_tokens is None: + chat_request.max_tokens = 200 + + response = model( + chat_request.message, + stream=False, + max_tokens=chat_request.max_tokens, + temperature=chat_request.temperature, + ) + + return {"text": response["choices"][0]["text"]} + + def _get_model(self): + model = Llama( + model_path=self.model_path, + verbose=False, + ) + + return model + + async def invoke_rerank( + self, query: str, documents: List[Dict[str, Any]], ctx: Context, **kwargs: Any + ) -> Any: + return None + + +class PromptTemplate: + """ + Template for generating prompts for different types of requests. + """ + + def dummy_chat_template( + self, message: str, chat_history: List[Dict[str, str]] + ) -> str: + prompt = "System: You are an AI assistant whose goal is to help users by consuming and using the output of various tools. You will be able to see the conversation history between yourself and user and will follow instructions on how to respond." + prompt += "\n\n" + prompt += "Conversation:\n" + for chat in chat_history: + if chat["role"].lower() == "user": + prompt += f"User: {chat['message']}\n" + else: + prompt += f"Chatbot: {chat['message']}\n" + + prompt += f"User: {message}\n" + prompt += "Chatbot: " + + return prompt + + def dummy_rag_template( + self, + message: str, + chat_history: List[Dict[str, str]], + documents: List[Dict[str, str]], + max_docs: int = 5, + ) -> str: + max_docs = min(max_docs, len(documents)) + prompt = "System: You are an AI assistant whose goal is to help users by consuming and using the output of various tools. You will be able to see the conversation history between yourself and user and will follow instructions on how to respond." + + doc_str_list = [] + for doc_idx, doc in enumerate(documents[:max_docs]): + if doc_idx > 0: + doc_str_list.append("") + + # only use first 200 words of the document to avoid exceeding context window + text = doc["text"] + if len(text.split()) > 200: + text = " ".join(text.split()[:200]) + + doc_str_list.extend([f"Document: {doc_idx}", doc["title"], text]) + + doc_str = "\n".join(doc_str_list) + + chat_history.append({"role": "system", "message": doc_str}) + chat_history.append({"role": "user", "message": message}) + + chat_hist_str = "" + for turn in chat_history: + if turn["role"].lower() == "user": + chat_hist_str += "User: " + elif turn["role"].lower() == "chatbot": + chat_hist_str += "Chatbot: " + else: # role == system + chat_hist_str += "System: " + + chat_hist_str += turn["message"] + "\n" + + prompt += "\n\n" + prompt += "Conversation:\n" + prompt += chat_hist_str + prompt += "Chatbot: " + + return prompt + + # https://docs.cohere.com/docs/prompting-command-r#formatting-chat-history-and-tool-outputs + def cohere_rag_template( + self, + message: str, + chat_history: List[Dict[str, str]], + documents: List[Dict[str, str]], + preamble: str = None, + max_docs: int = 5, + ) -> str: + max_docs = min(max_docs, len(documents)) + chat_history.append({"role": "user", "message": message}) + SAFETY_PREAMBLE = "The instructions in this section override those in the task description and style guide sections. Don't answer questions that are harmful or immoral." + BASIC_RULES = "You are a powerful conversational AI trained by Cohere to help people. You are augmented by a number of tools, and your job is to use and consume the output of these tools to best help the user. You will see a conversation history between yourself and a user, ending with an utterance from the user. You will then see a specific instruction instructing you what kind of response to generate. When you answer the user's requests, you cite your sources in your answers, according to those instructions." + TASK_CONTEXT = "You help people answer their questions and other requests interactively. You will be asked a very wide array of requests on all kinds of topics. You will be equipped with a wide range of search engines or similar tools to help you, which you use to research your answer. You should focus on serving the user's needs as best you can, which will be wide-ranging." + STYLE_GUIDE = "Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling." + documents = self._get_cohere_documents_template(documents, max_docs) + chat_history = self._get_cohere_chat_history_template(chat_history) + INSTRUCTIONS = """Carefully perform the following instructions, in order, starting each with a new line. +Firstly, Decide which of the retrieved documents are relevant to the user's last input by writing 'Relevant Documents:' followed by comma-separated list of document numbers. If none are relevant, you should instead write 'None'. +Secondly, Decide which of the retrieved documents contain facts that should be cited in a good answer to the user's last input by writing 'Cited Documents:' followed a comma-separated list of document numbers. If you dont want to cite any of them, you should instead write 'None'. +Thirdly, Write 'Answer:' followed by a response to the user's last input in high quality natural english. Use the retrieved documents to help you. Do not insert any citations or grounding markup. +Finally, Write 'Grounded answer:' followed by a response to the user's last input in high quality natural english. Use the symbols and to indicate when a fact comes from a document in the search result, e.g my fact for a fact from document 0.""" + + tool_prompt_template = f"""<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|> # Safety Preamble +{SAFETY_PREAMBLE} + +# System Preamble +## Basic Rules +{BASIC_RULES} + +# User Preamble +""" + if preamble: + tool_prompt_template += f"""{preamble}\n\n""" + + tool_prompt_template += f"""## Task and Context +{TASK_CONTEXT} + +## Style Guide +{STYLE_GUIDE}<|END_OF_TURN_TOKEN|>{chat_history}""" + + if documents: + tool_prompt_template += f"""<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{documents}<|END_OF_TURN_TOKEN|>""" + + tool_prompt_template += f"""<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{INSTRUCTIONS}<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>""" + + return tool_prompt_template + + def _get_cohere_documents_template( + self, documents: List[Dict[str, str]], max_docs: int + ) -> str: + max_docs = min(max_docs, len(documents)) + doc_str_list = [""] + for doc_idx, doc in enumerate(documents[:max_docs]): + if doc_idx > 0: + doc_str_list.append("") + doc_str_list.extend([f"Document: {doc_idx}", doc["title"], doc["text"]]) + doc_str_list.append("") + return "\n".join(doc_str_list) + + def _get_cohere_chat_history_template( + self, chat_history: List[Dict[str, str]] + ) -> str: + chat_hist_str = "" + for turn in chat_history: + chat_hist_str += "<|START_OF_TURN_TOKEN|>" + if turn["role"] == "user": + chat_hist_str += "<|USER_TOKEN|>" + elif turn["role"] == "chatbot": + chat_hist_str += "<|CHATBOT_TOKEN|>" + else: # role == system + chat_hist_str += "<|SYSTEM_TOKEN|>" + chat_hist_str += turn["message"] + chat_hist_str += "<|END_OF_TURN_TOKEN|>" + return chat_hist_str + + +async def main(): + model = LocalModelDeployment(model_path="path/to/model") + + print("--- Chat Stream ---") + response = await model.invoke_chat_stream( + CohereChatRequest(message="hello world", temperature=0.3) + ) + for item in response: + print(item) + + print("\n--- Chat ---") + response = model.invoke_chat( + CohereChatRequest(message="hello world", temperature=0.3) + ) + print(response) + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/western_ai/canada/cohere-toolkit/src/community/tests/model_deployments/test_local_model.py b/western_ai/canada/cohere-toolkit/src/community/tests/model_deployments/test_local_model.py new file mode 100644 index 0000000000000000000000000000000000000000..f00f35b207c3d434b4807f8a92933fbeb90db10e --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/tests/model_deployments/test_local_model.py @@ -0,0 +1,184 @@ +from community.model_deployments.local_model import PromptTemplate + + +def test_dummy_chat_template(): + prompt_template = PromptTemplate() + message = "How are you?" + chat_history = [ + {"role": "user", "message": "Hello"}, + {"role": "chatbot", "message": "Hi"}, + ] + expected = """System: You are an AI assistant whose goal is to help users by consuming and using the output of various tools. You will be able to see the conversation history between yourself and user and will follow instructions on how to respond. + +Conversation: +User: Hello +Chatbot: Hi +User: How are you? +Chatbot: """ + assert prompt_template.dummy_chat_template(message, chat_history) == expected + + +def test_dummy_chat_template_no_chat_history(): + prompt_template = PromptTemplate() + message = "Hello" + chat_history = [] + expected = """System: You are an AI assistant whose goal is to help users by consuming and using the output of various tools. You will be able to see the conversation history between yourself and user and will follow instructions on how to respond. + +Conversation: +User: Hello +Chatbot: """ + assert prompt_template.dummy_chat_template(message, chat_history) == expected + + +def test_dummy_rag_template(): + prompt_template = PromptTemplate() + message = "How are you?" + chat_history = [ + {"role": "user", "message": "Hello"}, + {"role": "chatbot", "message": "Hi"}, + ] + documents = [ + {"title": "First Document", "text": "This is the first document."}, + {"title": "Second Document", "text": "This is the second document."}, + ] + expected = """System: You are an AI assistant whose goal is to help users by consuming and using the output of various tools. You will be able to see the conversation history between yourself and user and will follow instructions on how to respond. + +Conversation: +User: Hello +Chatbot: Hi +System: Document: 0 +First Document +This is the first document. + +Document: 1 +Second Document +This is the second document. +User: How are you? +Chatbot: """ + assert ( + prompt_template.dummy_rag_template(message, chat_history, documents) == expected + ) + + +def test_dummy_rag_template_max_docs(): + prompt_template = PromptTemplate() + message = "How are you?" + chat_history = [ + {"role": "user", "message": "Hello"}, + {"role": "chatbot", "message": "Hi"}, + ] + documents = [ + {"title": "First Document", "text": "This is the first document."}, + {"title": "Second Document", "text": "This is the second document."}, + {"title": "Third Document", "text": "This is the third document."}, + ] + expected = """System: You are an AI assistant whose goal is to help users by consuming and using the output of various tools. You will be able to see the conversation history between yourself and user and will follow instructions on how to respond. + +Conversation: +User: Hello +Chatbot: Hi +System: Document: 0 +First Document +This is the first document. + +Document: 1 +Second Document +This is the second document. +User: How are you? +Chatbot: """ + + assert ( + prompt_template.dummy_rag_template(message, chat_history, documents, max_docs=2) + == expected + ) + + +def test_cohere_rag_template(): + prompt_template = PromptTemplate() + message = "How are you?" + chat_history = [ + {"role": "user", "message": "Hello"}, + {"role": "chatbot", "message": "Hi"}, + ] + documents = [ + {"title": "First Document", "text": "This is the first document."}, + {"title": "Second Document", "text": "This is the second document."}, + ] + preamble = "My custom preamble" + expected = """<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|> # Safety Preamble +The instructions in this section override those in the task description and style guide sections. Don't answer questions that are harmful or immoral. + +# System Preamble +## Basic Rules +You are a powerful conversational AI trained by Cohere to help people. You are augmented by a number of tools, and your job is to use and consume the output of these tools to best help the user. You will see a conversation history between yourself and a user, ending with an utterance from the user. You will then see a specific instruction instructing you what kind of response to generate. When you answer the user's requests, you cite your sources in your answers, according to those instructions. + +# User Preamble +My custom preamble + +## Task and Context +You help people answer their questions and other requests interactively. You will be asked a very wide array of requests on all kinds of topics. You will be equipped with a wide range of search engines or similar tools to help you, which you use to research your answer. You should focus on serving the user's needs as best you can, which will be wide-ranging. + +## Style Guide +Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling.<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|>Hello<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>Hi<|START_OF_TURN_TOKEN|><|USER_TOKEN|>How are you?<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|> +Document: 0 +First Document +This is the first document. + +Document: 1 +Second Document +This is the second document. +<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>Carefully perform the following instructions, in order, starting each with a new line. +Firstly, Decide which of the retrieved documents are relevant to the user's last input by writing 'Relevant Documents:' followed by comma-separated list of document numbers. If none are relevant, you should instead write 'None'. +Secondly, Decide which of the retrieved documents contain facts that should be cited in a good answer to the user's last input by writing 'Cited Documents:' followed a comma-separated list of document numbers. If you dont want to cite any of them, you should instead write 'None'. +Thirdly, Write 'Answer:' followed by a response to the user's last input in high quality natural english. Use the retrieved documents to help you. Do not insert any citations or grounding markup. +Finally, Write 'Grounded answer:' followed by a response to the user's last input in high quality natural english. Use the symbols and to indicate when a fact comes from a document in the search result, e.g my fact for a fact from document 0.<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>""" + assert ( + prompt_template.cohere_rag_template(message, chat_history, documents, preamble) + == expected + ) + + +def test_cohere_rag_template_max_docs(): + prompt_template = PromptTemplate() + message = "How are you?" + chat_history = [ + {"role": "user", "message": "Hello"}, + {"role": "chatbot", "message": "Hi"}, + ] + documents = [ + {"title": "First Document", "text": "This is the first document."}, + {"title": "Second Document", "text": "This is the second document."}, + {"title": "Third Document", "text": "This is the third document."}, + ] + expected = """<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|> # Safety Preamble +The instructions in this section override those in the task description and style guide sections. Don't answer questions that are harmful or immoral. + +# System Preamble +## Basic Rules +You are a powerful conversational AI trained by Cohere to help people. You are augmented by a number of tools, and your job is to use and consume the output of these tools to best help the user. You will see a conversation history between yourself and a user, ending with an utterance from the user. You will then see a specific instruction instructing you what kind of response to generate. When you answer the user's requests, you cite your sources in your answers, according to those instructions. + +# User Preamble +## Task and Context +You help people answer their questions and other requests interactively. You will be asked a very wide array of requests on all kinds of topics. You will be equipped with a wide range of search engines or similar tools to help you, which you use to research your answer. You should focus on serving the user's needs as best you can, which will be wide-ranging. + +## Style Guide +Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling.<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|>Hello<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>Hi<|START_OF_TURN_TOKEN|><|USER_TOKEN|>How are you?<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|> +Document: 0 +First Document +This is the first document. + +Document: 1 +Second Document +This is the second document. +<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>Carefully perform the following instructions, in order, starting each with a new line. +Firstly, Decide which of the retrieved documents are relevant to the user's last input by writing 'Relevant Documents:' followed by comma-separated list of document numbers. If none are relevant, you should instead write 'None'. +Secondly, Decide which of the retrieved documents contain facts that should be cited in a good answer to the user's last input by writing 'Cited Documents:' followed a comma-separated list of document numbers. If you dont want to cite any of them, you should instead write 'None'. +Thirdly, Write 'Answer:' followed by a response to the user's last input in high quality natural english. Use the retrieved documents to help you. Do not insert any citations or grounding markup. +Finally, Write 'Grounded answer:' followed by a response to the user's last input in high quality natural english. Use the symbols and to indicate when a fact comes from a document in the search result, e.g my fact for a fact from document 0.<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>""" + + assert ( + prompt_template.cohere_rag_template( + message, chat_history, documents, max_docs=2 + ) + == expected + ) diff --git a/western_ai/canada/cohere-toolkit/src/community/tests/tools/test_arxiv.py b/western_ai/canada/cohere-toolkit/src/community/tests/tools/test_arxiv.py new file mode 100644 index 0000000000000000000000000000000000000000..f714bfed37393e3686527752773f553163ed86bd --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/tests/tools/test_arxiv.py @@ -0,0 +1,12 @@ +import pytest + +from community.tools import ArxivRetriever + + +@pytest.mark.asyncio +async def test_arxiv_retriever(): + retriever = ArxivRetriever() + result = await retriever.call({"query": "quantum"}) + assert len(result) > 0 + assert "text" in result[0] + assert "quantum" in result[0]["text"].lower() diff --git a/western_ai/canada/cohere-toolkit/src/community/tests/tools/test_clinicaltrials.py b/western_ai/canada/cohere-toolkit/src/community/tests/tools/test_clinicaltrials.py new file mode 100644 index 0000000000000000000000000000000000000000..28002cc451150a0e85e0388d7db471e2f6a12158 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/tests/tools/test_clinicaltrials.py @@ -0,0 +1,17 @@ +import pytest + +from community.tools import ClinicalTrials + + +@pytest.mark.asyncio +async def test_clinicaltrials_tool(): + retriever = ClinicalTrials() + result = await retriever.call( + parameters={ + "condition": "lung cancer", + "location": "Canada", + "is_recruiting": True, + } + ) + assert len(result) > 0 + assert "id" in result[0] diff --git a/western_ai/canada/cohere-toolkit/src/community/tests/tools/test_llama_index.py b/western_ai/canada/cohere-toolkit/src/community/tests/tools/test_llama_index.py new file mode 100644 index 0000000000000000000000000000000000000000..ee38e78647b55962202bdc51bbc937b006e5b7ab --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/tests/tools/test_llama_index.py @@ -0,0 +1,42 @@ +import pytest + +from community.tools import LlamaIndexUploadPDFRetriever + + +@pytest.mark.asyncio +async def test_pdf_retriever() -> None: + file_path = "src/backend/tests/unit/test_data/Mariana_Trench.pdf" + retriever = LlamaIndexUploadPDFRetriever(file_path) + query = "What is the mariana trench?" + expected_docs = [ + { + "text": "Location of the Mariana TrenchMariana Trench\nThe Mariana Trench is an oceanic trench located in the\nwestern Pacific Ocean, about 200 kilometres (124 mi)\neast of the Mariana Islands; it is the deepest oceanic\ntrench on Earth. It is crescent-shaped and measures\nabout 2,550 km (1,580 mi) in length and 69 km (43 mi)\nin width. The maximum know n depth is 10,984 ± 25\nmetres (36,037 ± 82 ft; 6,006 ± 14 fathoms;\n6.825 ± 0.016 mi) at the southern end of a small slot-\nshaped valley in its floor know n as the Challenger\nDeep.[1] The deepest point of the trench is more than\n2 km (1.2 mi) farther from sea level than the peak of\nMount Everest.[a]\nAt the bottom of the trench, the water column above\nexerts a pressure of 1,086 bar (15,750 psi), more than\n1,071 times the standard atmospheric pressure at sea\nlevel. At this pressure, the density of water is increased\nby 4.96% . The temperature at the bottom is 1 to 4 °C (34\nto 39 ° F).[4]\nIn 2009, the Mariana Trench was established as a US\nNational Monum ent.[5]\nOne-celled organisms called monothalamea have been\nfound in the trench at a record depth of 10.6 km (35,000 ft; 6.6 mi) below the sea surface by researchers\nfrom Scripps Institution of Oceanography. [6] Data has also sugge sted that microbial life forms thrive within\nthe trench.[7][8]\nThe Mariana Trench is named after the nearby Mariana Islands, which are named Las Marianas in hono r of\nSpanish Queen Mariana of Austria. The islands are part of the island arc that is formed on an over-riding\nplate, called the Mariana Plate (also named for the islands), on t he western side of the trench.\nThe Mariana Trench is part of the Izu–Bonin–M ariana subduc tion system that forms the bounda ry between\ntwo tectonic plates. In this system, the western edge of one plate, the Pacific Plate, is subduc ted (i.e., thrust)\nbeneath the smaller Mariana Plate that lies to the west. Crustal material at the western edge of the Pacific\nPlate is some of the oldest oceanic crust on Earth (up to 170 million years old), and is, therefore, cooler and\ndenser; hence its great height difference relative to the higher-riding (and younge r) Mariana Plate. The\ndeepest area at the plate bounda ry is the Mariana Trench proper.Coordinates: 11°21′N 142°12′E\nEtymology\nGeology" + }, + { + "text": 'The Pacific plate is subducted beneath the Mariana Plate,\ncreating the Mariana trench, and (further on) the arc of the\nMariana Islands, as water trapped in the plate is released\nand explodes upward to form island volcanoes and\nearthquakes.\nOcean trenches in the western\nPacificThe movement of the Pacific and Mariana\nplates is also indirectly respons ible for the\nformation of the Mariana Islands. These\nvolcanic islands are caused by flux melting of\nthe uppe r mantle due to the release of water\nthat is trapped in minerals of the subducted\nportion of the Pacific Plate.\nThe trench was first sounde d during the\nChallenger expedition in 1875 using a\nweighted rope, which recorded a depth of\n4,475 fathoms (8,184 metres; 26,850\nfeet).[9][10] In 1877, a map was published\ncalled Tiefenkarte des Grossen Ozeans ("Depth\nmap of the Great Ocean") by Petermann,\nwhich showed a Challenger Tief ("Challenger deep") at the location\nof that soundi ng. In 1899, USS Nero, a conve rted collier, recorded\na depth of 5,269 f athoms (9,636 m etres; 31,614 f eet).[11]\nIn 1951, unde r Chief Scientist Thomas Gaskell, Challenger II\nsurveyed the trench using echo sounding, a much more precise and\nvastly easier way to measure depth than the soundi ng equipment\nand drag lines used in the original expedition. During this survey,\nthe deepest part of the trench was recorded when the Challenger II\nmeasured a depth of 5,960 fathoms (10,900 metres; 35,760 feet) at\n11°19′N 142° 15′E,[12] know n as the Challenger Deep.[13]\nIn 1957, the Soviet vessel Vityaz reported a depth of 11,034 m\n(36,201 ft; 6,033 fathoms) at a location dubbe d the Mariana\nHollow.[14]\nIn 1962, the surface ship M.V. Spencer F. Baird recorded a\nmaximum depth of 10,915 m (35,810 ft; 5,968 fathoms) using\nprecision depth gauges.\nIn 1984, the Japanese survey vessel Takuyō (拓洋 ) collected data\nfrom the Mariana Trench using a narrow, multi-beam echo sounde r;\nit reported a maximum depth of 10,924 metres (35,840 ft), also\nreported as 10,920 ± 10 m (35,827 ± 33 ft; 5,971.1 ± 5.5 fathoms).[15] Remotely Operated Vehicle KAIKO\nreached the deepest area of the Mariana Trench and made the deepest diving record of 10,91 1 m (35,797 ft;\n5,966 f athoms) on 24 M arch 1995.[16]\nDuring surveys carried out between 1997 and 2001, a spot was found along the Mariana Trench that had a\ndepth similar to the Challenger Deep, possibly even deeper. It was discovered while scientists from the\nHawaii Institute of Geophys ics and Planetology were completing a survey around Guam; they used a sonar\nmapping system towed behind the research ship to conduc t the survey. This new spot was named the\nHMRG (Hawaii Mapping R esearch Group) Deep, after the group of scientists who di scovered it.[17]Research history' + }, + { + "text": "The bathyscaphe Trieste (designed\nby Auguste Piccard), the first crewed\nvehicle to reach the bottom of the\nMariana Trench[22]On 1 June 2009, mapping aboard the RV Kilo Moana (mothership of the Nereus vehicle), indicated a spot\nwith a depth of 10,971 m (35,994 ft; 5,999 fathoms). The sonar mapping of the Challenger Deep was\npossible by its Simrad EM120 sonar multibeam bathymetry system for deep water. The sonar system uses\nphase and amplitude bottom detection, with an accuracy of better than 0.2% of water depth across the entire\nswath (implying that the depth figure is accurate to ± 22 m etres (72 ft; 12 fathoms)).[18][19]\nIn 2011, it was announc ed at the American Geophys ical Union Fall Meeting that a US Navy hydrographic\nship equipped with a multibeam echosounde r conduc ted a survey which mapped the entire trench to 100 m\n(330 ft; 55 fathoms) resolution.[2] The mapping revealed the existence of four rocky outcrops thought to be\nformer seamounts.[20]\nThe Mariana Trench is a site chosen by researchers at Washington University in St. Louis and the Woods\nHole Oceanographic Institution in 2012 for a seismic survey to investigate the subsurface water cycle.\nUsing both ocean-bottom seismometers and hydrophone s, the scientists are able to map structures as deep\nas 97 ki lometres (318,000 f t; 53,000 f athoms; 60 m i) beneath the surface.[21]\nAs of 2022, 22 crewed descents and seven uncrewed descents have\nbeen achieved. The first was the crewed descent by Swiss-\ndesigned, Italian-built, United States Navy-owned bathyscaphe\nTrieste, which reached the bottom at 1:06 pm on 23 January 1960,\nwith Don Walsh and Jacques Piccard on board.[13][23] Iron shot\nwas used for ballast, with gasoline for buoya ncy.[13] The onboa rd\nsystems indicated a depth of 37,800 feet (11,521 m; 6,300\nfathoms),[24] but this was later revised to 35,814 feet (10,916 m;\n5,969 fathoms).[25] The depth was estimated from a conve rsion of\npressure measured and calculations based on the water density from\nsea surface to seabed.[23]\nThis was followed by the uncrewed ROVs Kaikō in 1996 and\nNereus in 2009. The first three expeditions directly measured very\nsimilar depths of 10,902 to 10,916 m (35,768 to 35,814 ft; 5,961 to\n5,969 fathoms).[26][27] The fourth was made by Canadian film director James Cameron on 26 March 2012.\nHe reached the bottom of the Mariana Trench in the submersible vessel Deepsea Challenger, diving to a\ndepth of 10,908 m (35,787 f t; 5,965 f athoms).[28][29][30]\nIn July 2015, members of the National Oceanic and Atmospheric Administration, Oregon State University,\nand the Coast Guard submerged a hydrophone into the deepest part of the Mariana Trench, the Challenger\nDeep, never having previously deployed one past a mile. The titanium-shelled hydrophone was designed to\nwithstand the immense pressure 7 mi (37,000 ft; 6,200 fathoms; 11,000 m) unde r.[31] Although researchers\nwere unable to retrieve the hydrophone until November, the data capacity was full within the first 23 days.\nAfter months of analyzing the sounds , the experts were surprised to pick up natural sounds like\nearthqua kes, typhoons , baleen whales, and machine-made sounds such as boats.[32] Due to the mission's\nsuccess, the researchers announc ed plans to deploy a second hydrophone in 2017 for an extended period of\ntime.\nVictor Vescovo achieved a new record descent to 10,92 8 m (35,853 ft; 5,976 fathoms) on 28 April 2019\nusing the DSV Limiting Factor, a Triton 36000/ 2 model manufactured by Florida-based Triton Submarines.\nHe dived four times between 28 A pril and 5 May 2019, becoming the first person to dive into Challenger\nDeep more than onc e.[33][34][35]D e s c e n t s" + }, + { + "text": 'On 8 May 2020, a joint project between the Russian shipbuilders, scientific teams of the Russian Academy\nof Sciences with the suppor t of the Russian Founda tion for Advanced Research Projects and the Pacific\nFleet submerged the autonom ous unde rwater vehicle Vityaz-D to the bottom of the Mariana Trench at a\ndepth of 10,028 m (32,900 ft; 5,483 fathoms). Vityaz-D is the first unde rwater vehicle to operate\nautonom ously at the extreme depths of the Mariana Trench. The duration of the mission, excluding diving\nand surfacing, w as more than 3 hour s.[36][37]\nOn 10 November 2020, the Chinese submersible Fendouz he reached the bottom of the Mariana Trench at a\ndepth of 10,909 m (35,791 f t; 5,965 f athoms).[38][39]\nThe expedition conduc ted in 1960 claimed to have observed, with great surprise because of the high\npressure, large creatures living at the bottom, such as a flatfish about 30 cm (12 in) long,[24] and shrimp.[40]\nAccording to Piccard, "The bottom appeared light and clear, a waste of firm diatomaceous ooze".[24] Many\nmarine biologists are now skeptical of the suppos ed sighting of the flatfish, and it is sugge sted that the\ncreature may instead have been a sea cucumber.[41][42] During the second expedition, the uncrewed vehicle\nKaikō collected mud samples from the seabed.[43] Tiny or ganisms were found t o be living in those samples.\nIn July 2011, a research expedition deployed untethered landers, called drop cams, equipped with digital\nvideo cameras and lights to explore this deep-sea region. Among many other living organisms, some\ngigantic single-celled foraminiferans with a size of more than 10 cm (4 in), belonging to the class of\nmonothalamea, were observed.[44] Monothalamea are noteworthy for their size, their extreme abunda nce on\nthe seafloor, and their role as hosts for a variety of organisms.\nIn December 2014, a new species of snailfish was discovered at a depth of 8,145 m (26,722 ft; 4,454\nfathoms), breaking the previous record for the deepest living fish seen on vi deo.[45]\nDuring the 2014 expedition, several new species were filmed, including huge amphipods know n as\nsupergiants. Deep-sea gigantism is the process where species grow larger than their shallow-water\nrelatives.[45]\nIn May 2017, a n uni dentified type of snailfish was filmed at a depth of 8,178 m etres (26,800 f t).[46]\nIn 2016, a research expedition looke d at the chemical makeup of crustacean scavengers collected from the\nrange of 7,841–10,250 m (25,725–33 ,629 ft; 4,288–5,605 fathoms) within the trench. Within these\norganisms, the researchers found extremely elevated concentrations of PCBs, a chemical toxin banned in\nthe 1970s for its environmental harm, concentrated at all depths within the sediment of the trench.[47]\nFurther research has found that amphipods also ingest microplastics, with 100% of amphipods having at\nleast one piece of synthetic material in their stomachs.[48][49]\nIn 2019, Victor Vescovo reported finding a plastic bag and candy wrappers at the bottom of the trench.[50]\nThat year, Scientific American also reported that carbon-14 from nuclear bomb testing has been found in the\nbodies of aquatic animals found i n the trench.[51]Life\nP o l l u t i o n\nPossible nuclear waste disposal site' + }, + { + "text": 'Like other oceanic trenches, the Mariana Trench has been propos ed as a site for nuclear waste\ndisposal[52][53] in the hope that tectonic plate subduc tion occurring at the site might eventually push the\nnuclear waste deep into the Earth\'s mantle, the second layer of the Earth. However, ocean dumping of\nnuclear waste is prohibited by international law.[52][53][54] Furthermore, plate subduc tion zones are\nassociated with very large megathrust earthqua kes, the effects of which are unpredictable for the safety of\nlong-term disposal of nuclear wastes within the hadope lagic ecosystem.[53]\nOceania portal\nMarianas Trench Marine National Monument, United States national monument at the\ntrench. This National Monument protects 246,610 square kilometres (95,216 sq mi) of\nsubmerged lands and waters of the Mariana Archipelago. It includes some of the Mariana\nTrench, but not the deepest part, the Challenger Deep, which lies just outside the monument\narea.\nChallenger Deep\nList of people who descended to Challenger Deep\nLitke Deep, closest point to Earth\'s center\na. Mariana Trench is 10,994 m (36,070 ft; 6.831 mi) deep,[2] while Mount Everest is 8,848 m\n(29,029 ft; 5.498 mi) tall.[3] The difference is 2,146 m (7,041 ft; 1.333 mi), or at least no less\nthan 2,104 m (6,903 ft; 1.307 mi), accounting for the combined 42 m (138 ft; 0.026 mi)\nuncertainty in the measurements.\n1. Gardner, James V.; Armstrong, Andrew A.; Calder, Brian R.; Beaudoin, Jonathan (2 January\n2014). "So, How Deep Is the Mariana Trench?" (http://ccom.unh.edu/sites/default/files/public\nations/Gardner-et-al-2014-Challenger-Deep.pdf) (PDF). Marine Geodesy. Informa UK\nLimited. 3 7 (1): 1–13. Bibcode:2014MarGe..37....1G (https://ui.adsabs.harvard.edu/abs/2014\nMarGe..37....1G). doi:10.1080/01490419.2013.837849 (https://doi.org/10.1080%2F0149041\n9.2013.837849). ISSN 0149-0419 (https://www.worldcat.org/issn/0149-0419).\nS2CID 128668687 (https://api.semanticscholar.org/CorpusID:128668687).\n2. "Scientists map Mariana Trench, deepest known section of ocean in the world" (https://www.t\nelegraph.co.uk/news/earth/environment/8940571/Scientists-map-Mariana-Trench-deepest-k\nnown-section-of-ocean-in-the-world.html). The Telegraph. 7 December 2011. Archived (http\ns://ghostarchive.org/archive/20220110/https://www.telegraph.co.uk/news/earth/environment/\n8940571/Scientists-map-Mariana-Trench-deepest-known-section-of-ocean-in-the-world.htm\nl) from the original on 10 January 2022. Retrieved 23 June 2018.\n3. "Official height for Everest set" (http://news.bbc.co.uk/2/hi/south_asia/8608913.stm). BBC\nNews. 8 April 2010. Retrieved 24 June 2018.\n4. "The Temperature in the Mariana Trench" (http://www.infoplease.com/askeds/temperature-m\nariana-trench.html). Infoplease. 28 February 2017.\n5. "About the Monument – Mariana Trench" (https://www.fws.gov/refuge/Mariana_Trench_Marin\ne_National_Monument/about/About_the_Monument.html). U.S. Fish and Wildlife Service.See also\nNotes\nReferences' + }, + { + "text": '6. "Giant amoeba found in Mariana Trench – 6.6 miles beneath the sea" (http://latimesblogs.lati\nmes.com/nationnow/2011/10/giant-amoeba-found-mariana-trench-beneath-the-sea.html).\nLos Angeles Times. 26 October 2011. Retrieved 23 March 2012.\n7. Choi, Charles Q. (17 March 2013). "Microbes Thrive in Deepest Spot on Earth" (http://www.li\nvescience.com/27954-microbes-mariana-trench.html). LiveScience. Retrieved 17 March\n2013.\n8. Glud, Ronnie; Wenzhöfer, Frank; Middleboe, Mathias; Oguri, Kazumasa; Turnewitsch,\nRobert; Canfield, Donald E.; Kitazato, Hiroshi (17 March 2013). "High rates of microbial\ncarbon turnover in sediments in the deepest oceanic trench on Earth". Nature Geoscience. 6\n(4): 284–288. Bibcode:2013NatGe...6..284G (https://ui.adsabs.harvard.edu/abs/2013NatGe...\n6..284G). doi:10.1038/ngeo1773 (https://doi.org/10.1038%2Fngeo1773).\n9. "About the Mariana Trench – Deepsea Challenge Expedition" (https://web.archive.org/web/2\n0130628083954/http://deepseachallenge.com/the-expedition/mariana-trench/).\nDeepseachallenge.com. 26 March 2012. Archived from the original (http://deepseachalleng\ne.com/the-expedition/mariana-trench/) on 28 June 2013. Retrieved 8 July 2013.\n10. Aitken, Frédéric; Foulc, Jean-Numa (2019). "Chapter 4". From deep sea to laboratory. 1 : the\nfirst explorations of the deep sea by H.M.S. Challenger (1872–1876) (http://www.iste.co.uk/b\nook.php?id=1474). London. ISBN 9781786303745.\n11. Theberge, A. (24 March 2009). "Thirty Years of Discovering the Mariana Trench" (https://ww\nw.hydro-international.com/content/article/thirty-years-of-discovering-the-mariana-trench).\nHydro International. Retrieved 31 July 2010.\n12. Gaskell, Thomas F. (1960). Under The Deep Oceans: Twentieth Century Voyages of\nDiscovery (1st ed.). Eyre & Spottiswood. p. 121.\n13. "The Mariana Trench – Exploration" (http://www.marianatrench.com/mariana_trench-explorat\nion_001.htm). marianatrench.com.\n14. "Mariana Trench" (https://www.britannica.com/place/Pacific-Ocean). Encyclopædia\nBritannica. 18 July 2023.\n15. Tani, S. "Continental shelf survey of Japan" (https://web.archive.org/web/20110309013717/h\nttp://www.gmat.unsw.edu.au/ablos/ABLOS08Folder/Session4-Paper4-Tani.pdf) (PDF).\nArchived from the original (http://www.gmat.unsw.edu.au/ablos/ABLOS08Folder/Session4-P\naper4-Tani.pdf) (PDF) on 9 March 2011. Retrieved 24 December 2010.\n16. Development and Construction of Launcher System of 10000m‐Class Remotely Operated\nVehicle KAIKO (http://www.mhi.co.jp/technology/review/abstractj-32-6-427.html) Archived (htt\nps://web.archive.org/web/20150402104327/http://www.mhi.co.jp/technology/review/abstractj-\n32-6-427.html) 2 April 2015 at the Wayback Machine Mitsubishi Heavy Industry\n17. Whitehouse, David (16 July 2003). "Sea floor survey reveals deep hole" (http://news.bbc.co.\nuk/2/hi/science/nature/3071749.stm). BBC News. Retrieved 17 December 2011.\n18. "Daily Reports for R/V KILO MOANA June and July 2009" (https://web.archive.org/web/2010\n0121061243/http://www.soest.hawaii.edu/UMC/Reports/Archives/KMreportJuneJuly2009.ht\nml). University of Hawaii Marine Center. Archived from the original (http://www.soest.hawaii.e\ndu/UMC/Reports/Archives/KMreportJuneJuly2009.html) on 21 January 2010. Retrieved\n4 January 2010.\n19. "Inventory of Scientific Equipment aboard the R/V KILO MOANA" (https://web.archive.org/we\nb/20100613143513/http://www.soest.hawaii.edu/UMC/KM/scienceequipment.htm).\nUniversity of Hawaii Marine Center. Archived from the original (http://www.soest.hawaii.edu/\nUMC/KM/scienceequipment.htm) on 13 June 2010.\n20. Duncan Geere (7 February 2012). "Four \'bridges\' span the Mariana Trench" (https://web.arch\nive.org/web/20120311150303/http://www.wired.co.uk/news/archive/2012-02/07/bridges-mari\nana-trench). Wired. Archived from the original (https://www.wired.co.uk/news/archive/2012-0\n2/07/bridges-mariana-trench) on 11 March 2012. Retrieved 23 March 2012.' + }, + { + "text": '21. "Seismic Survey at the Mariana Trench Will Follow Water Dragged Down into the Earth\'s\nMantle" (https://www.sciencedaily.com/releases/2012/03/120322142201.htm). ScienceDaily.\n22 March 2012. Retrieved 23 March 2012.\n22. Strickland, Eliza (29 February 2012). "Don Walsh Describes the Trip to the Bottom of the\nMariana Trench" (https://spectrum.ieee.org/geek-life/profiles/don-walsh-describes-the-trip-to-t\nhe-bottom-of-the-mariana-trench). IEEE Spectrum. Retrieved 8 July 2013.\n23. "Mariana Trench" (https://web.archive.org/web/20120318064310/http://earthquake.usgs.gov/\nearthquakes/world/guam/mariana_trench.php). Earthquake Hazards Program. United States\nGeological Survey. 21 October 2009. Archived from the original (https://earthquake.usgs.gov/\nearthquakes/world/guam/mariana_trench.php) on 18 March 2012. Retrieved 23 March 2012.\n24. "NOAA Ocean Explorer: History: Quotations: Soundings, Sea-Bottom, and Geophysics" (htt\np://oceanexplorer.noaa.gov/history/quotes/soundings/soundings.html). NOAA Ocean\nExploration and Research.\n25. "Bathyscaphe" (https://www.britannica.com/technology/bathyscaphe). Encyclopædia\nBritannica. 18 April 2020. Retrieved 11 November 2020.\n26. "7,000 m Class Remotely Operated Vehicle: KAIKO 7000" (https://web.archive.org/web/2020\n0410211118/http://www.jamstec.go.jp/e/about/equipment/ships/kaiko7000.html). Japan\nAgency for Marine-Earth Science and Technology. Archived from the original (http://www.jam\nstec.go.jp/e/about/equipment/ships/kaiko7000.html) on 10 April 2020. Retrieved\n11 November 2020.\n27. "Robot sub reaches deepest ocean" (http://news.bbc.co.uk/2/hi/science/nature/8080324.st\nm). BBC. 3 June 2009. Retrieved 11 November 2020.\n28. "A man took a submarine to the deepest place on Earth – and found trash" (https://www.cbc.c\na/news/world/trash-littering-ocean-floor-deepest-submarine-dive-1.5134717). CBC.ca.\nThomson Reuters. 13 May 2019. Retrieved 11 November 2020.\n29. "James Cameron has reached deepest spot on Earth" (https://www.nbcnews.com/id/wbna46\n850002). NBC News. 25 March 2012. Retrieved 25 March 2012.\n30. Broad, William J. (25 March 2012). "Filmmaker in Submarine Voyages to Bottom of Sea" (htt\nps://www.nytimes.com/2012/03/26/science/james-camerons-submarine-trip-to-challenger-de\nep.html). The New York Times. Retrieved 25 March 2012.\n31. Schneider, Kate (7 March 2016). "Eerie sounds from the bottom of the Earth" (https://www.ne\nws.com.au/travel/destinations/eerie-sounds-from-the-bottom-of-the-earth/news-story/53fffdfcb\n549bfcb80c4ea8833b7968b). News.com.au. Retrieved 11 November 2020.\n32. Chappell, Bill (4 March 2016). "Deep-Sea Audio Recordings Reveal A Noisy Mariana\nTrench, Surprising Scientists" (https://www.npr.org/sections/thetwo-way/2016/03/04/4692135\n80/unique-audio-recordings-find-a-noisy-mariana-trench-and-surprise-scientists). NPR.\nRetrieved 1 May 2016.\n33. Fitzherbert, Stephanie (13 May 2019). "Deepest Submarine Dive in history, Five Deeps\nExpedition Conquers Challenger Deep" (https://fivedeeps.com/wp-content/uploads/2019/05/\nFDE-Challenger-Release-FINAL-5132019.pdf) (PDF). The Five Deeps. Retrieved\n11 November 2020.\n34. Loomis, Ilima (3 July 2019). "Limiting Factor Was a Science Opportunity for a Deep-Sea\nGeologist" (https://eos.org/articles/limiting-factor-was-a-science-opportunity-for-a-deep-sea-g\neologist). Eos. Retrieved 11 November 2020.\n35. Blain, Loz (15 May 2019). "Victor Vescovo and the DSV Limiting Factor have found new\ndepths in the Mariana Trench" (https://newatlas.com/five-deeps-dsv-limiting-factor-mariana-tr\nench/59690/). New Atlas. Retrieved 11 November 2020.\n36. "Russian Submarine "Vityaz" Reached the Bottom of the Mariana Trench" (https://www.rgo.r\nu/en/article/russian-submarine-vityaz-reached-bottom-mariana-trench). Russian\nGeographical Society. 13 May 2020. Retrieved 11 November 2020.' + }, + { + "text": '37. "Vityaz-D explored Mariana Trench according to preinstalled program — developer" (https://t\nass.com/defense/1166477). TASS. 10 June 2020. Retrieved 11 November 2020.\n38. Westcott, Ben (11 November 2020). "China breaks national record for Mariana Trench\nmanned-dive amid race for deep sea resources" (https://web.archive.org/web/202011111848\n11/https://edition.cnn.com/2020/11/11/asia/china-record-dive-mariana-trench-intl-hnk/). CNN.\nArchived from the original (https://edition.cnn.com/2020/11/11/asia/china-record-dive-marian\na-trench-intl-hnk/) on 11 November 2020. Retrieved 11 November 2020.\n39. Cheng, Xiang; Liu, Liang (10 November 2020). ""奋⽃者 " 号载⼈潜⽔器突破万⽶海深 潜⼊全\n球最深海域 " (https://news.cctv.com/2020/11/10/ARTIQc1R6lp4IlMJT8G3SNqY201110.shtm\nl) [The "Striver" manned submersible breaks through 10,000 meters and dives into the\nworld\'s deepest waters]. China Central Television. Retrieved 11 November 2020.\n40. "Bathyscaphe Trieste | Mariana Trench | Challenger Deep" (http://geology.com/records/bathy\nscaphe-trieste.shtml). Geology.com. Retrieved 1 March 2012.\n41. "James Cameron dives deep for Avatar" (https://www.theguardian.com/environment/2011/ja\nn/18/james-cameron-dives-deep-avatar) Archived (https://web.archive.org/web/2017011806\n4643/https://www.theguardian.com/environment/2011/jan/18/james-cameron-dives-deep-ava\ntar) 18 January 2017 at the Wayback Machine, Guardian, 18 January 2011\n42. "James Cameron heads into the abyss" (http://www.nature.com/news/james-cameron-heads\n-into-the-abyss-1.10246) Archived (https://web.archive.org/web/20120901025830/http://www.\nnature.com/news/james-cameron-heads-into-the-abyss-1.10246) 1 September 2012 at the\nWayback Machine, Nature, 19 March 2012\n43. Woods, Michael; Mary B. Woods (2009). Seven Natural Wonders of the Arctic, Antarctica,\nand the Oceans (https://archive.org/details/sevennaturalwond0000wood_u5g0). Twenty-First\nCentury Books. p. 13 (https://archive.org/details/sevennaturalwond0000wood_u5g0/page/1\n3). ISBN 978-0-8225-9075-0. Retrieved 23 March 2012.\n44. "Giant amoebas discovered in the deepest ocean trench" (http://www.livescience.com/16678\n-giant-amoebas-discovered-deepest-ocean-trench.html). Live Science. 21 October 2011.\nRetrieved 26 March 2012.\n45. Morelle, Rebecca (9 December 2014). "New record for deepest fish" (https://www.bbc.co.uk/\nnews/science-environment-30541065). BBC News. Retrieved 26 August 2017.\n46. "Ghostly fish in Mariana Trench in the Pacific is deepest ever recorded" (https://www.cbc.ca/\nnews/technology/deepest-fish-1.4263003). CBC News. 25 August 2017. Retrieved\n26 August 2017.\n47. Jamieson, Alan J.; Malkocs, Tamas; Piertney, Stuart B.; Fujii, Toyonobu; Zhang, Zulin (13\nFebruary 2017). "Bioaccumulation of persistent organic pollutants in the deepest ocean\nfauna". Nature Ecology & Evolution. 1 (3): 51. doi:10.1038/s41559-016-0051 (https://doi.org/\n10.1038%2Fs41559-016-0051). hdl:2164/9142 (https://hdl.handle.net/2164%2F9142).\nISSN 2397-334X (https://www.worldcat.org/issn/2397-334X). PMID 28812719 (https://pubme\nd.ncbi.nlm.nih.gov/28812719). S2CID 9192602 (https://api.semanticscholar.org/CorpusID:91\n92602).\n48. Jamieson, A. J.; Brooks, L. S. R.; Reid, W. D. K.; Piertney, S. B.; Narayanaswamy, B. E.;\nLinley, T. D. (28 February 2019). "Microplastics and synthetic particles ingested by deep-sea\namphipods in six of the deepest marine ecosystems on Earth" (https://www.ncbi.nlm.nih.gov/\npmc/articles/PMC6408374). Royal Society Open Science. 6 (2): 180667.\nBibcode:2019RSOS....680667J (https://ui.adsabs.harvard.edu/abs/2019RSOS....680667J).\ndoi:10.1098/rsos.180667 (https://doi.org/10.1098%2Frsos.180667). ISSN 2054-5703 (https://\nwww.worldcat.org/issn/2054-5703). PMC 6408374 (https://www.ncbi.nlm.nih.gov/pmc/article\ns/PMC6408374). PMID 30891254 (https://pubmed.ncbi.nlm.nih.gov/30891254).' + }, + { + "text": '49. Robbins, Gary (5 September 2019). "UCSD discovers surge in plastics pollution off Santa\nBarbara" (https://www.latimes.com/california/story/2019-09-04/uc-san-diego-discovers-explo\nsion-in-plastics-products-in-seafloor-off-santa-barbara). Los Angeles Times. Retrieved\n5 September 2019.\n50. Street, Francesca (13 May 2019). "Deepest ocean dive recorded: How Victor Vescovo did it"\n(https://www.cnn.com/travel/article/victor-vescovo-deepest-dive-pacific/index.html). CNN\nTravel. CNN. Retrieved 13 May 2019.\n51. Levy, Adam (15 May 2019). ""Bomb Carbon" Has Been Found in Deep-Ocean Creatures" (h\nttps://www.scientificamerican.com/article/bomb-carbon-has-been-found-in-deep-ocean-creat\nures/). Scientific American.\n52. Hafemeister, David W. (2007). Physics of societal issues: calculations on national security,\nenvironment, and energy (https://books.google.com/books?id=LT4MSqv9QUIC&pg=PA187).\nBerlin: Springer. p. 187. ISBN 978-0-387-95560-5.\n53. Kingsley, Marvin G.; Rogers, Kenneth H. (2007). Calculated risks: highly radioactive waste\nand homeland security (https://books.google.com/books?id=bOP4-BpYXrEC&pg=PA75).\nAldershot, Hants, England: Ashgate. pp. 75–76. ISBN 978-0-7546-7133-6.\n54. "Dumping and Loss overview" (https://web.archive.org/web/20110605190619/http://www.la\nw.berkeley.edu/centers/ilr/ona/pages/dumping2.htm). Oceans in the Nuclear Age. Archived\nfrom the original (http://www.law.berkeley.edu/centers/ilr/ona/pages/dumping2.htm) on 5\nJune 2011. Retrieved 18 September 2010.\nMariana Trench Dive (25 March 2012) (https://web.archive.org/web/20140625050833/http://d\neepseachallenge.com/) – Deepsea Challenger\nMariana Trench Dive (23 January 1960) (http://www.britishpathe.com/video/they-dived-7-mil\nes/query/mariana+trench) – Trieste (Newsreel)\nMariana Trench Dive (50th Anniv) (http://www.vvdailypress.com/articles/walsh-18116-regret-\nmiles.html) Archived (https://web.archive.org/web/20130603064615/http://www.vvdailypress.\ncom/articles/walsh-18116-regret-miles.html) 3 June 2013 at the Wayback Machine – Trieste\n– Capt Don Walsh\nMariana Trench – Maps (Google) (https://maps.google.com/maps?q=11.317,+142.25(Marian\na+Trench)&z=6)\nNOAA – Ocean Explorer (http://oceanexplorer.noaa.gov) (Ofc Ocean Exploration & Rsch)\nNOAA – Ocean Explorer – Multimedia (http://oceanexplorer.noaa.gov/explorations/06fire/bac\nkground/marianaarc/marianaarc.html) – Mariana Arc (podcast (http://oceanexplorer.noaa.go\nv/explorations/podcast/oceanexplorer_podcast.xml))\nNOAA – Ocean Explorer – Video Playlist (https://www.youtube.com/view_play_list?p=94B79\n5FD631011E0) – Ring of Fire (2004–2006)\nRetrieved from "https://en.wikipedia.org/w/index.php?title=Mariana_Trench&oldid=1187694887"External links\n' + }, + ] + result = await retriever.call({"query": query}) + + assert expected_docs == result diff --git a/western_ai/canada/cohere-toolkit/src/community/tests/tools/test_pub_med.py b/western_ai/canada/cohere-toolkit/src/community/tests/tools/test_pub_med.py new file mode 100644 index 0000000000000000000000000000000000000000..deb0f279b528ce7b83dbf1d5fc41b03ff11a6a8a --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/tests/tools/test_pub_med.py @@ -0,0 +1,11 @@ +import pytest + +from community.tools import PubMedRetriever + + +@pytest.mark.asyncio +async def test_pub_med_retriever(): + retriever = PubMedRetriever() + result = await retriever.call({"query": "What causes lung cancer?"}) + assert len(result) > 0 + assert "text" in result[0] diff --git a/western_ai/canada/cohere-toolkit/src/community/tools/__init__.py b/western_ai/canada/cohere-toolkit/src/community/tools/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..2777cc9bb2a553ef7d7cd8e7932cf68f22a737f0 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/tools/__init__.py @@ -0,0 +1,15 @@ +from community.tools.arxiv import ArxivRetriever +from community.tools.clinicaltrials import ClinicalTrials +from community.tools.connector import ConnectorRetriever +from community.tools.llama_index import LlamaIndexUploadPDFRetriever +from community.tools.pub_med import PubMedRetriever +from community.tools.wolfram import WolframAlpha + +__all__ = [ + "WolframAlpha", + "ClinicalTrials", + "ArxivRetriever", + "ConnectorRetriever", + "LlamaIndexUploadPDFRetriever", + "PubMedRetriever", +] diff --git a/western_ai/canada/cohere-toolkit/src/community/tools/arxiv.py b/western_ai/canada/cohere-toolkit/src/community/tools/arxiv.py new file mode 100644 index 0000000000000000000000000000000000000000..f1f8b908d7f0dac5874d153bd7750eb24af18256 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/tools/arxiv.py @@ -0,0 +1,49 @@ +from typing import Any, Dict, List + +from langchain_community.utilities import ArxivAPIWrapper + +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.tools.base import BaseTool + + +class ArxivRetriever(BaseTool): + ID = "arxiv" + + def __init__(self): + self.client = ArxivAPIWrapper() + + @classmethod + def is_available(cls) -> bool: + return True + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Arxiv", + implementation=cls, + parameter_definitions={ + "query": { + "description": "Query for retrieval.", + "type": "str", + "required": True, + } + }, + is_visible=False, + is_available=cls.is_available(), + error_message=cls.generate_error_message(), + category=ToolCategory.DataLoader, + description="Retrieves documents from Arxiv.", + ) + + async def call(self, parameters: dict, **kwargs: Any) -> List[Dict[str, Any]]: + query = parameters.get("query", "") + try: + result = self.client.run(query) + except Exception as e: + return self.get_tool_error(details=str(e)) + + if not result: + return self.get_no_results_error() + + return [{"text": result}] diff --git a/western_ai/canada/cohere-toolkit/src/community/tools/clinicaltrials.py b/western_ai/canada/cohere-toolkit/src/community/tools/clinicaltrials.py new file mode 100644 index 0000000000000000000000000000000000000000..981492bfbb40ce25cefb13d153b13eb8c06b91f3 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/tools/clinicaltrials.py @@ -0,0 +1,139 @@ +from typing import Any + +import requests + +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.tools.base import BaseTool + + +class ClinicalTrials(BaseTool): + """ + Retrieves clinical studies from ClinicalTrials.gov. + + See: https://clinicaltrials.gov/data-api/api + """ + + ID = "clinical_trials" + + def __init__(self, url="https://clinicaltrials.gov/api/v2/studies"): + self._url = url + + @classmethod + def is_available(cls) -> bool: + return True + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Clinical Trials", + implementation=cls, + is_visible=False, + is_available=cls.is_available(), + error_message=cls.generate_error_message(), + category=ToolCategory.Function, + description="Retrieves clinical studies from ClinicalTrials.gov.", + parameter_definitions={ + "condition": { + "description": "Filters clinical studies to a specified disease or condition", + "type": "str", + "required": False, + }, + "location": { + "description": "Filters clinical studies to a specified city, state, or country.", + "type": "str", + "required": False, + }, + "intervention": { + "description": "Filters clinical studies to a specified drug or treatment.", + "type": "str", + "required": False, + }, + "is_recruiting": { + "description": "Filters clinical studies to those that are actively recruiting.", + "type": "bool", + "required": False, + }, + }, + ) + + async def call( + self, parameters: dict[str, Any], **kwargs, + ) -> list[dict[str, Any]]: + query_params = {"sort": "LastUpdatePostDate"} + if condition := parameters.get("condition", ""): + query_params["query.cond"] = condition + if location := parameters.get("location", ""): + query_params["query.locn"] = location + if intervention := parameters.get("intervention", ""): + query_params["query.intr"] = intervention + if parameters.get("is_recruiting"): + query_params["filter.overallStatus"] = "RECRUITING" + query_params["pageSize"] = kwargs.get("n_max_studies", 10) + + try: + response = requests.get(self._url, params=query_params) + response.raise_for_status() + except requests.exceptions.RequestException as e: + return self.get_tool_error(details=str(e)) + + results = self._parse_response(response, location, intervention) + if not results: + return self.get_no_results_error() + + return results + + def _parse_response( + self, response: requests.Response, location: str, intervention: str + ) -> list[dict[str, Any]]: + data = response.json() + return [ + self._parse_study(study, location, intervention) + for study in data.get("studies", []) + ] + + def _parse_study( + self, study: dict[str, Any], location: str, intervention: str + ) -> dict[str, Any]: + """Parse individual study data.""" + id_module = study["protocolSection"].get("identificationModule", {}) + description_module = study["protocolSection"].get("descriptionModule", {}) + status_module = study["protocolSection"].get("statusModule", {}) + eligibility_module = study["protocolSection"].get("eligibilityModule", {}) + conditions_module = study["protocolSection"].get("conditionsModule", {}) + locations_module = study["protocolSection"].get("contactsLocationsModule", {}) + intervents_module = study["protocolSection"].get("armsInterventionsModule", {}) + + return { + "id": id_module.get("nctId"), + "title": id_module["briefTitle"], + "trial_summary": description_module.get("briefSummary"), + "trial_status": status_module.get("overallStatus"), + "eligibility_criteria": eligibility_module.get("eligibilityCriteria"), + "last_updated": status_module.get("lastUpdateSubmitDate"), + "url": f"https://www.clinicaltrials.gov/study/{id_module.get('nctId')}", + "conditions": conditions_module.get("conditions", []), + "locations": self._filter_results( + locations_module.get("locations", []), + location, + ["city", "state", "country"], + ), + "interventions": self._filter_results( + intervents_module.get("interventions", []), + intervention, + ["name", "type", "description"], + ), + } + + def _filter_results( + self, results: list[dict[str, Any]], target: str, fields: list[str] + ) -> list[dict[str, Any]]: + """Keeps a result if any of the specified fields contain the target value. Only + the specified fields are returned in the result. If the query does not specify a + target value, all results are retained. + """ + return [ + {k: v for k, v in res.items() if k in fields} + for res in results + if any(target in res.get(field, "") for field in fields) + ] diff --git a/western_ai/canada/cohere-toolkit/src/community/tools/connector.py b/western_ai/canada/cohere-toolkit/src/community/tools/connector.py new file mode 100644 index 0000000000000000000000000000000000000000..3f35818e605b0ba8f470653ab889f41b64a84416 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/tools/connector.py @@ -0,0 +1,59 @@ +from typing import Any, Dict, List + +import requests + +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.tools.base import BaseTool + +""" +Plug in your Connector configuration here. For example: + +Url: http://example_connector.com/search +Auth: Bearer token for the connector + +To see SSO examples, check out our Google Drive or Slack tool implementations + +More details: https://docs.cohere.com/docs/connectors +""" + + +class ConnectorRetriever(BaseTool): + ID = "example_connector" + + def __init__(self, url: str, api_key: str): + self.url = url + self.api_key = api_key + + @classmethod + def is_available(cls) -> bool: + return False + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Example Connector Template - Do not use", + implementation=ConnectorRetriever, + is_visible=False, + is_available=cls.is_available(), + error_message=cls.generate_error_message(), + category=ToolCategory.DataLoader, + description="Example connector for a data source using a basic API.", + ) + + async def call(self, parameters: dict, **kwargs: Any) -> List[Dict[str, Any]]: + body = {"query": parameters} + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {self.api_key}", + } + try: + response = requests.get(self.url, json=body, headers=headers) + results = response.json()["results"] + except Exception as e: + return self.get_tool_error(details=str(e)) + + if not results: + return self.get_no_results_error() + + return results diff --git a/western_ai/canada/cohere-toolkit/src/community/tools/llama_index.py b/western_ai/canada/cohere-toolkit/src/community/tools/llama_index.py new file mode 100644 index 0000000000000000000000000000000000000000..7e51226e9e8e9a192b11d5f02b3e28d6e72e94ed --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/tools/llama_index.py @@ -0,0 +1,123 @@ +from typing import Any, Dict, List + +from llama_index.core import VectorStoreIndex +from llama_index.core.node_parser import SentenceSplitter +from llama_index.core.readers import StringIterableReader +from llama_index.embeddings.cohere import CohereEmbedding + +import backend.crud.file as file_crud +from backend.config import Settings +from backend.schemas.context import Context +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.tools.base import BaseTool + +""" +Plug in your llama index retrieval implementation here. +We have an example flow with PDF upload. + + +More details: +https://docs.llamaindex.ai/en/stable/module_guides/querying/retriever/root.html +""" + + +class LlamaIndexUploadPDFRetriever(BaseTool): + """ + This class retrieves documents from a PDF using the llama_index package. + This requires llama_index package to be installed. + """ + + ID = "file_reader_llamaindex" + CHUNK_SIZE = 512 + + def __init__(self): + self.COHERE_API_KEY = Settings().get('deployments.cohere_platform.api_key') + + + def _get_embedding(self, embed_type): + return CohereEmbedding( + api_key=self.COHERE_API_KEY, + model_name="embed-english-v3.0", + input_type=embed_type, + ) + + @classmethod + def is_available(cls) -> bool: + return True + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Llama File Reader", + implementation=cls, + parameter_definitions={ + "query": { + "description": "Query for retrieval.", + "type": "str", + "required": True, + }, + "files": { + "description": "A list of files represented as tuples of (filename, file ID) to search over", + "type": "list[tuple[str, str]]", + "required": True, + }, + + }, + is_visible=False, + is_available=cls.is_available(), + error_message=cls.generate_error_message(), + category=ToolCategory.FileLoader, + description=( + "Retrieves the most relevant documents from the uploaded " + "files based on the query using Llama Index." + ) + ) + + async def call( + self, parameters: dict, ctx: Context, **kwargs: Any + ) -> List[Dict[str, Any]]: + query = parameters.get("query") + files = parameters.get("files") + session = kwargs.get("session") + user_id = kwargs.get("user_id") + + if not query or not files: + return [] + + file_ids = [file_id for _, file_id in files] + retrieved_files = file_crud.get_files_by_ids(session, file_ids, user_id) + if not retrieved_files: + return self.get_no_results_error() + + file_str_list = [] + for file in retrieved_files: + file_str_list.append(file.file_content) + # LLamaIndex get documents from parsed PDFs, split it into sentences, embed, index and retrieve + try: + docs = StringIterableReader().load_data(file_str_list) + node_parser = SentenceSplitter(chunk_size=LlamaIndexUploadPDFRetriever.CHUNK_SIZE) + nodes = node_parser.get_nodes_from_documents(docs) + embed_model = self._get_embedding("search_document") + vector_index = VectorStoreIndex( + nodes, + embed_model=embed_model, + ) + embed_model_query = self._get_embedding("search_query") + retriever = vector_index.as_retriever( + similarity_top_k=10, + embed_model=embed_model_query, + ) + results = retriever.retrieve(query) + llama_results = [{"text": doc.text} for doc in results] + except Exception as e: + return self.get_tool_error(details=str(e)) + + if not llama_results and not docs: + return self.get_no_results_error() + + # If llama results are found, return them + if llama_results: + return llama_results + # If no llama results are found, return the original file content + return [{"text": doc.text} for doc in docs] diff --git a/western_ai/canada/cohere-toolkit/src/community/tools/pub_med.py b/western_ai/canada/cohere-toolkit/src/community/tools/pub_med.py new file mode 100644 index 0000000000000000000000000000000000000000..6fa5618c43d81386b2ae20a8fd802df312fafc46 --- /dev/null +++ b/western_ai/canada/cohere-toolkit/src/community/tools/pub_med.py @@ -0,0 +1,48 @@ +from typing import Any, Dict, List + +from langchain_community.tools.pubmed.tool import PubmedQueryRun + +from backend.schemas.tool import ToolCategory, ToolDefinition +from backend.tools.base import BaseTool + + +class PubMedRetriever(BaseTool): + ID = "pub_med" + + def __init__(self): + self.client = PubmedQueryRun() + + @classmethod + def is_available(cls) -> bool: + return True + + @classmethod + def get_tool_definition(cls) -> ToolDefinition: + return ToolDefinition( + name=cls.ID, + display_name="Pub Med", + implementation=cls, + parameter_definitions={ + "query": { + "description": "Query for retrieval.", + "type": "str", + "required": True, + } + }, + is_visible=False, + is_available=cls.is_available(), + error_message=cls.generate_error_message(), + category=ToolCategory.DataLoader, + description="Retrieves documents from Pub Med.", + ) + + async def call(self, parameters: dict, **kwargs: Any) -> List[Dict[str, Any]]: + query = parameters.get("query", "") + try: + result = self.client.invoke(query) + except Exception as e: + return self.get_tool_error(details=str(e)) + if not result: + return self.get_no_results_error() + + return [{"text": result}]