Skip to content

Commit

Permalink
[backend] agents API (cohere-ai#186)
Browse files Browse the repository at this point in the history
* route logic

* rename

* fixes and test

* done, need to write postman examples

* squash migrations from development

* lint

* pagiantion test

* Agents postman examples

* postman

* postman export version

* db nullable fix

* unused user id

* fix validator to create only

* squash migration

* lint
  • Loading branch information
scott-cohere authored Jun 10, 2024
1 parent d3d0024 commit 7c65779
Show file tree
Hide file tree
Showing 10 changed files with 771 additions and 14 deletions.
156 changes: 154 additions & 2 deletions docs/postman/Toolkit.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"info": {
"_postman_id": "ddf8071a-3b8a-45d7-b4c4-abd54b0dccc2",
"_postman_id": "affe2235-b89c-4ea1-8b5f-568f16f24ae3",
"name": "Toolkit",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json",
"_exporter_id": "25545730"
"_exporter_id": "31264470"
},
"item": [
{
Expand Down Expand Up @@ -423,6 +423,158 @@
}
]
},
{
"name": "Agents",
"item": [
{
"name": "Create agent",
"request": {
"method": "POST",
"header": [
{
"key": "User-Id",
"value": "user-id",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"name\": \"string\",\n \"version\": 0,\n \"description\": \"string\",\n \"preamble\": \"string\",\n \"temperature\": 0,\n \"model\": \"command-r\",\n \"deployment\": \"Cohere Platform\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": "http://localhost:8000/v1/agents"
},
"response": []
},
{
"name": "List agents",
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [
{
"key": "User-Id",
"value": "user-id",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8000/v1/agents?limit=10&offset=0",
"protocol": "http",
"host": [
"localhost"
],
"port": "8000",
"path": [
"v1",
"agents"
],
"query": [
{
"key": "limit",
"value": "10"
},
{
"key": "offset",
"value": "0"
}
]
}
},
"response": []
},
{
"name": "Update agent",
"request": {
"method": "PUT",
"header": [
{
"key": "User-Id",
"value": "user-id",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"name\": \"string\",\n \"version\": 0,\n \"description\": \"string\",\n \"preamble\": \"string\",\n \"temperature\": 0,\n \"model\": \"command-r\",\n \"deployment\": \"Cohere Platform\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": "http://localhost:8000/v1/agents/{id}"
},
"response": []
},
{
"name": "Get agent",
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [
{
"key": "User-Id",
"value": "user-id",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": "http://localhost:8000/v1/agents/{id}"
},
"response": []
},
{
"name": "Delete agent",
"request": {
"method": "DELETE",
"header": [
{
"key": "User-Id",
"value": "user-id",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": "http://localhost:8000/v1/agents/{id}"
},
"response": []
}
]
},
{
"name": "Health",
"protocolProfileBehavior": {
Expand Down
32 changes: 32 additions & 0 deletions src/backend/alembic/versions/a9b07acef4e8_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""empty message
Revision ID: a9b07acef4e8
Revises: f5819b10ef2a
Create Date: 2024-06-10 15:10:58.650989
"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "a9b07acef4e8"
down_revision: Union[str, None] = "f5819b10ef2a"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column("agents", "description", existing_type=sa.TEXT(), nullable=False)
op.alter_column("agents", "preamble", existing_type=sa.TEXT(), nullable=False)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column("agents", "preamble", existing_type=sa.TEXT(), nullable=True)
op.alter_column("agents", "description", existing_type=sa.TEXT(), nullable=True)
# ### end Alembic commands ###
10 changes: 10 additions & 0 deletions src/backend/config/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class RouterName(StrEnum):
EXPERIMENTAL_FEATURES = "experimental_features"
TOOL = "tool"
USER = "user"
AGENT = "agent"


# Router dependency mappings
Expand Down Expand Up @@ -87,4 +88,13 @@ class RouterName(StrEnum):
Depends(get_session),
],
},
RouterName.AGENT: {
"default": [
Depends(get_session),
],
"auth": [
Depends(get_session),
Depends(validate_authorization),
],
},
}
2 changes: 1 addition & 1 deletion src/backend/crud/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def update_agent(db: Session, agent: Agent, new_agent: UpdateAgent) -> Agent:
Returns:
Agent: Updated agent.
"""
for attr, value in new_agent.model_dump().items():
for attr, value in new_agent.model_dump(exclude_none=True).items():
setattr(agent, attr, value)
db.commit()
db.refresh(agent)
Expand Down
8 changes: 4 additions & 4 deletions src/backend/database_models/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class Model(StrEnum):
class Agent(Base):
__tablename__ = "agents"

version: Mapped[int] = mapped_column(Integer, default=1)
version: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
name: Mapped[str] = mapped_column(Text, nullable=False)
description: Mapped[str] = mapped_column(Text, nullable=True)
preamble: Mapped[str] = mapped_column(Text, nullable=True)
temperature: Mapped[float] = mapped_column(Float, default=0.3)
description: Mapped[str] = mapped_column(Text, default="", nullable=False)
preamble: Mapped[str] = mapped_column(Text, default="", nullable=False)
temperature: Mapped[float] = mapped_column(Float, default=0.3, nullable=False)
# tool: Mapped[List["Tool"]] = relationship()

# TODO @scott-cohere: eventually switch to Fkey when new deployment tables are implemented
Expand Down
2 changes: 2 additions & 0 deletions src/backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from backend.config.auth import is_authentication_enabled
from backend.config.routers import ROUTER_DEPENDENCIES
from backend.routers.agent import router as agent_router
from backend.routers.auth import router as auth_router
from backend.routers.chat import router as chat_router
from backend.routers.conversation import router as conversation_router
Expand Down Expand Up @@ -41,6 +42,7 @@ def create_app():
tool_router,
deployment_router,
experimental_feature_router,
agent_router,
]

# Dynamically set router dependencies
Expand Down
Loading

0 comments on commit 7c65779

Please sign in to comment.