Skip to content

Commit dc53e1c

Browse files
committed
Init Agent Api
0 parents  commit dc53e1c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2003
-0
lines changed

.dockerignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.DS_Store
2+
.git
3+
4+
# Cache
5+
.mypy_cache
6+
*__pycache__*
7+
*.egg-info
8+
*.pyc
9+
*.pytest_cache
10+
*.ruff_cache
11+
*.cache*
12+
*.config*
13+
*.local*
14+
15+
# Machine specific
16+
.idea
17+
.vscode
18+
19+
# Ignore .env files
20+
.env
21+
.envrc
22+
23+
# ignore storage dir
24+
/storage
25+
.images
26+
27+
# ignore dist dir
28+
dist
29+
30+
# ignore virtualenvs
31+
.venv*
32+
venv*

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_size = 2
5+
indent_style = space
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.py]
12+
indent_size = 4

.github/workflows/docker-images.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Build Docker Images
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
permissions:
8+
contents: read
9+
10+
env:
11+
IMAGE_NAME: agent-api
12+
DOCKERHUB_NAMESPACE: ${{ vars.DOCKERHUB_NAMESPACE }}
13+
14+
jobs:
15+
build-api-image:
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
22+
- name: Set up QEMU
23+
uses: docker/setup-qemu-action@v3
24+
25+
- name: Set up Docker Buildx
26+
uses: docker/setup-buildx-action@v3
27+
28+
- name: Docker Login
29+
uses: docker/login-action@v3
30+
with:
31+
username: ${{ secrets.DOCKERHUB_USERNAME }}
32+
password: ${{ secrets.DOCKERHUB_TOKEN }}
33+
34+
- name: Extract metadata (tags, labels) for Docker
35+
id: meta
36+
uses: docker/metadata-action@v5
37+
with:
38+
images: ${{ env.DOCKERHUB_NAMESPACE }}/${{ env.IMAGE_NAME }}
39+
tags: |
40+
type=semver,pattern={{version}}
41+
type=semver,pattern={{major}}.{{minor}}
42+
type=raw,value=latest,enable=${{ github.event_name == 'release' && github.event.release.prerelease == false }}
43+
type=raw,value=dev
44+
type=raw,value=prd
45+
46+
- name: Build and push image to Docker Hub
47+
uses: docker/build-push-action@v5
48+
with:
49+
context: .
50+
file: Dockerfile
51+
platforms: linux/amd64,linux/arm64
52+
push: true
53+
tags: ${{ steps.meta.outputs.tags }}
54+
labels: ${{ steps.meta.outputs.labels }}
55+
cache-from: type=gha
56+
cache-to: type=gha,mode=max

.github/workflows/ecr-images.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Build ECR Images
2+
3+
on: workflow_dispatch
4+
5+
permissions:
6+
# For AWS OIDC Token access as per https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services#updating-your-github-actions-workflow
7+
id-token: write # This is required for requesting the JWT
8+
contents: read # This is required for actions/checkout
9+
10+
env:
11+
IMAGE_NAME: agent-api
12+
ECR_REPO: ${{ vars.ECR_REPO }}
13+
# Create role using https://aws.amazon.com/blogs/security/use-iam-roles-to-connect-github-actions-to-actions-in-aws/
14+
AWS_ROLE: ${{ vars.AWS_ROLE }}
15+
AWS_REGION: us-east-1
16+
17+
jobs:
18+
build-api-image:
19+
runs-on: ubuntu-latest
20+
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v4
24+
25+
- name: Set up QEMU
26+
uses: docker/setup-qemu-action@v3
27+
28+
- name: Set up Docker Buildx
29+
uses: docker/setup-buildx-action@v3
30+
31+
- name: Configure AWS credentials
32+
uses: aws-actions/configure-aws-credentials@v4
33+
with:
34+
role-to-assume: ${{ env.AWS_ROLE }}
35+
aws-region: ${{ env.AWS_REGION }}
36+
37+
- name: ECR Login
38+
id: login-ecr
39+
uses: aws-actions/amazon-ecr-login@v2
40+
41+
- name: Build and push image to Amazon ECR
42+
uses: docker/build-push-action@v5
43+
with:
44+
context: .
45+
file: Dockerfile
46+
platforms: linux/amd64,linux/arm64
47+
push: true
48+
tags: |
49+
${{ env.ECR_REPO }}/${{ env.IMAGE_NAME }}:dev
50+
${{ env.ECR_REPO }}/${{ env.IMAGE_NAME }}:prd
51+
${{ env.ECR_REPO }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
52+
cache-from: type=gha
53+
cache-to: type=gha,mode=max

.github/workflows/validate.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Validate
2+
3+
on:
4+
push:
5+
branches:
6+
- "main"
7+
pull_request:
8+
types:
9+
- opened
10+
- synchronize
11+
- reopened
12+
branches:
13+
- "main"
14+
15+
jobs:
16+
validate:
17+
runs-on: ubuntu-latest
18+
strategy:
19+
matrix:
20+
python-version: ["3.11", "3.12"]
21+
fail-fast: false
22+
23+
steps:
24+
- uses: actions/checkout@v4
25+
26+
- name: Install uv
27+
uses: astral-sh/setup-uv@v3
28+
with:
29+
enable-cache: true
30+
cache-dependency-glob: "requirements**.txt"
31+
32+
- name: Set up Python ${{ matrix.python-version }}
33+
uses: actions/setup-python@v5
34+
with:
35+
python-version: ${{ matrix.python-version }}
36+
37+
- name: Create a virtual environment
38+
run: uv venv --python ${{ matrix.python-version }}
39+
40+
- name: Install dependencies
41+
run: |
42+
uv pip sync requirements.txt
43+
uv pip install ruff mypy pytest
44+
45+
- name: Format with ruff
46+
run: uv run ruff format . --check
47+
48+
- name: Lint with ruff
49+
run: uv run ruff check .
50+
51+
- name: Type-check with mypy
52+
run: uv run mypy .
53+
54+
# - name: Run tests
55+
# run: uv run pytest tests
56+
# if: ${{ !cancelled() }}

.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# See https://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
.DS_Store
4+
5+
# Cache
6+
.mypy_cache
7+
*__pycache__*
8+
*.egg-info
9+
*.pyc
10+
*.pytest_cache
11+
*.ruff_cache
12+
*.cache*
13+
*.config*
14+
15+
# Machine specific
16+
.idea
17+
.vscode
18+
19+
# Ignore .env files
20+
.env
21+
.envrc
22+
23+
# ignore storage dir
24+
/storage
25+
.images
26+
27+
# ignore dist dir
28+
dist
29+
30+
# ignore virtualenvs
31+
.venv*
32+
venv*

Dockerfile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
FROM agnohq/python:3.12
2+
3+
ARG USER=app
4+
ARG APP_DIR=/app
5+
ENV APP_DIR=${APP_DIR}
6+
7+
# Create user and home directory
8+
RUN groupadd -g 61000 ${USER} \
9+
&& useradd -g 61000 -u 61000 -ms /bin/bash -d ${APP_DIR} ${USER}
10+
11+
WORKDIR ${APP_DIR}
12+
13+
# Copy requirements.txt
14+
COPY requirements.txt ./
15+
16+
# Install requirements
17+
RUN uv pip sync requirements.txt --system
18+
19+
# Copy project files
20+
COPY . .
21+
22+
# Set permissions for the /app directory
23+
RUN chown -R ${USER}:${USER} ${APP_DIR}
24+
25+
# Switch to non-root user
26+
USER ${USER}
27+
28+
ENTRYPOINT ["/app/scripts/entrypoint.sh"]
29+
CMD ["chill"]

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
## Agent api AWS
2+
3+
This repo contains the code for a production-grade agentic system built with:
4+
5+
1. A FastAPI server
6+
2. A Postgres database with the PgVector extension.
7+
8+
You can run the agent api in 2 environments:
9+
10+
1. A development environment running locally on docker
11+
2. A production environment running on AWS ECS
12+
13+
## Setup
14+
15+
1. [Install uv](https://docs.astral.sh/uv/#getting-started) for managing the python environment.
16+
17+
```bash
18+
curl -LsSf https://astral.sh/uv/install.sh | sh
19+
```
20+
21+
2. Create a virtual environment and install dependencies:
22+
23+
```sh
24+
./scripts/dev_setup.sh
25+
```
26+
27+
3. Activate virtual environment
28+
29+
```
30+
source .venv/bin/activate
31+
```
32+
33+
## Run application locally using docker
34+
35+
1. Install [docker desktop](https://www.docker.com/products/docker-desktop)
36+
37+
2. Export API keys
38+
39+
Required: Set the `OPENAI_API_KEY` environment variable using
40+
41+
```sh
42+
export OPENAI_API_KEY=***
43+
```
44+
45+
> you may use any model provider, just need to update the agents in the /agents folder
46+
47+
Optional: Set the `EXA_API_KEY` if you'd like to use Exa search
48+
49+
```sh
50+
export EXA_API_KEY=***
51+
```
52+
53+
3. Start the workspace:
54+
55+
```sh
56+
ag ws up
57+
```
58+
59+
- This will run 3 containers:
60+
- FastAPI on [localhost:8000](http://localhost:8000/docs)
61+
- Postgres on [localhost:5432](http://localhost:5432)
62+
- Open [localhost:8000/docs](http://localhost:8000/docs) to view the FastAPI docs.
63+
64+
4. Stop the workspace using:
65+
66+
```sh
67+
ag ws down
68+
```
69+
70+
## More Information
71+
72+
Learn more about this application and how to customize it in the [Agno Workspaces](https://docs.agno.com/workspaces) documentaion

agents/__init__.py

Whitespace-only changes.

agents/operator.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from enum import Enum
2+
from typing import List, Optional
3+
4+
from agents.sage import get_sage
5+
from agents.scholar import get_scholar
6+
7+
8+
class AgentType(Enum):
9+
SAGE = "sage"
10+
SCHOLAR = "scholar"
11+
12+
13+
def get_available_agents() -> List[str]:
14+
"""Returns a list of all available agent IDs."""
15+
return [agent.value for agent in AgentType]
16+
17+
18+
def get_agent(
19+
model_id: str = "gpt-4o",
20+
agent_id: Optional[AgentType] = None,
21+
user_id: Optional[str] = None,
22+
session_id: Optional[str] = None,
23+
debug_mode: bool = True,
24+
):
25+
if agent_id == AgentType.SAGE:
26+
return get_sage(model_id=model_id, user_id=user_id, session_id=session_id, debug_mode=debug_mode)
27+
else:
28+
return get_scholar(model_id=model_id, user_id=user_id, session_id=session_id, debug_mode=debug_mode)

0 commit comments

Comments
 (0)