Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/litellm #1340

Draft
wants to merge 35 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
44431bb
initial commit
gecBurton Jan 28, 2025
b7ea00f
update woth correct envvars
gecBurton Jan 28, 2025
c6e6bfb
bodge
gecBurton Jan 28, 2025
ffd242e
bodge
gecBurton Jan 28, 2025
4c6a094
bug fix
gecBurton Jan 28, 2025
70e7e40
added dependency
gecBurton Jan 28, 2025
09a0cda
added dependency
gecBurton Jan 28, 2025
6fb83cb
reset condtion
gecBurton Jan 29, 2025
4c11b46
added logging
gecBurton Jan 29, 2025
d01715e
test
gecBurton Jan 29, 2025
cdc7882
fixed .env
gecBurton Jan 29, 2025
e4b145c
reduced diff
gecBurton Jan 29, 2025
6ae0397
remove echo
gecBurton Jan 29, 2025
8620b95
added infra
gecBurton Jan 29, 2025
1e54c18
added infra
gecBurton Jan 29, 2025
1cadc8c
added infra
gecBurton Jan 29, 2025
4257905
added infra
gecBurton Jan 29, 2025
8eac441
added image to litellm
gecBurton Jan 29, 2025
6474e29
added reverse proxy to litellm
gecBurton Jan 29, 2025
66e46d8
moved to config on s3
gecBurton Jan 29, 2025
900d78e
corrected litellm image
gecBurton Jan 29, 2025
eb1527a
fixed some bugs
gecBurton Jan 29, 2025
32506d8
Update django_app/redbox_app/urls.py
gecBurton Jan 29, 2025
c8fe8d1
added missing import
gecBurton Jan 29, 2025
1d10648
formatting
gecBurton Jan 29, 2025
147d9b0
Update django_app/redbox_app/urls.py
gecBurton Jan 29, 2025
7a36b11
aded import
gecBurton Jan 29, 2025
1d61dfb
updated ebvvars
gecBurton Jan 29, 2025
0ebd181
Update django_app/redbox_app/urls.py
gecBurton Jan 29, 2025
36ca183
Update django_app/redbox_app/urls.py
gecBurton Jan 29, 2025
569e11e
adding scheme and port to url
gecBurton Jan 29, 2025
e07983a
wip
gecBurton Jan 29, 2025
9d4bbdc
removed proxy
gecBurton Jan 29, 2025
1b9dccf
removed proxy
gecBurton Jan 29, 2025
cc3b7dc
Made litellm ECS service accessible via the ALB and added a DNS recor…
jamesrichards4 Jan 30, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ jobs:
- name: Build Containers
run: |
cd integration
chmod -R 777 data/
chmod -R 777 .
cp tests/.env.integration .env
echo AZURE_OPENAI_ENDPOINT=${{ secrets.AZURE_OPENAI_ENDPOINT }} >> .env
echo AZURE_OPENAI_API_KEY=${{ secrets.AZURE_OPENAI_API_KEY }} >> .env
echo EMBEDDING_AZURE_OPENAI_ENDPOINT=${{ secrets.AZURE_OPENAI_ENDPOINT }} >> .env
echo EMBEDDING_OPENAI_API_KEY=${{ secrets.AZURE_OPENAI_API_KEY }} >> .env
docker compose up -d --wait minio db
docker compose up -d --wait minio db litellm
docker compose up -d --wait django-app worker
docker ps

Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ docker_build: ## Build the docker container
docker pull $$PREV_IMAGE; \
docker compose build lit-ssr;

echo "Building liellm..."; \
PREV_IMAGE="$(ECR_REPO_URL)-litellm:$(PREV_IMAGE_TAG)"; \
echo "Pulling previous image: $$PREV_IMAGE"; \
docker pull $$PREV_IMAGE; \
docker compose build litellm;




Expand Down
21 changes: 20 additions & 1 deletion django_app/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions django_app/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ djangorestframework = "^3.15.2"
django-adminplus = "^0.6"
django-waffle = "^4.1.0"
markitdown = "^0.0.1a3"
django-revproxy = "^0.13.0"

[tool.poetry.group.dev.dependencies]
pytest = "^8.3.2"
Expand Down
1 change: 1 addition & 0 deletions django_app/redbox_app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"rest_framework",
"adminplus",
"waffle",
"revproxy.apps.RevProxyConfig",
]

if LOGIN_METHOD == "sso":
Expand Down
6 changes: 5 additions & 1 deletion django_app/redbox_app/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import os

from adminplus.sites import AdminSitePlus
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
from django.urls import include, path, re_path
from django.views.generic.base import RedirectView
from magic_link import urls as magic_link_urls
from revproxy.views import ProxyView

from .redbox_core import views
from .redbox_core.views import api_views
Expand Down Expand Up @@ -75,6 +78,7 @@

api_url_patterns = [
path("api/v0/file/", api_views.file_upload, name="file-upload"),
re_path(r"litellm/(?P<path>.*)", ProxyView.as_view(upstream=os.environ["LITELLM_URL"])),
gecBurton marked this conversation as resolved.
Show resolved Hide resolved
]

urlpatterns = (
Expand Down
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ services:
condition: service_healthy
minio:
condition: service_healthy
litellm:
condition: service_started
networks:
- redbox-app-network
env_file:
Expand Down Expand Up @@ -111,6 +113,18 @@ services:
retries: 24
start_period: 30s

litellm:
image: ghcr.io/berriai/litellm:main-latest
env_file: .env
networks:
- redbox-app-network
depends_on:
db:
condition: service_healthy
ports:
- "4000:4000"
command: --config /app/config.yml --detailed_debug

networks:
redbox-app-network:
driver: bridge
Expand Down
11 changes: 8 additions & 3 deletions infrastructure/aws/data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ locals {
record_prefix = terraform.workspace == "prod" ? var.project_name : "${var.project_name}-${terraform.workspace}"
django_host = "${local.record_prefix}.${var.domain_name}"
name = "${var.team_name}-${terraform.workspace}-${var.project_name}"
ssr_url = "${aws_service_discovery_service.lit_ssr_service_discovery_service.name}.${aws_service_discovery_private_dns_namespace.private_dns_namespace.name}"
ssr_url = "${aws_service_discovery_service.lit_ssr_service_discovery_service.name}.${aws_service_discovery_private_dns_namespace.private_dns_namespace.name}"
litellm_url = "${aws_service_discovery_service.litellm_service_discovery_service.name}.${aws_service_discovery_private_dns_namespace.private_dns_namespace.name}"


django_app_environment_variables = {
Expand All @@ -11,6 +12,11 @@ locals {

"LIT_SSR_URL": local.ssr_url,

"LITELLM_CONFIG_BUCKET_NAME": aws_s3_bucket.user_data.bucket,
"LITELLM_CONFIG_BUCKET_OBJECT_KEY": "litellm_proxy_config.yml",
"LITELLM_URL": local.litellm_url,
"LITELLM_MASTER_KEY": var.litellm_master_key,

"OBJECT_STORE" : "s3",
"BUCKET_NAME" : aws_s3_bucket.user_data.bucket,
"POSTGRES_DB" : module.rds.db_instance_name,
Expand Down Expand Up @@ -43,8 +49,7 @@ locals {
"AZURE_OPENAI_ENDPOINT" : var.azure_openai_endpoint,
"OPENAI_API_VERSION": var.openai_api_version,

"EMBEDDING_OPENAI_API_KEY" : var.embedding_openai_api_key,
"EMBEDDING_AZURE_OPENAI_ENDPOINT" : var.embedding_azure_openai_endpoint,


"DJANGO_SECRET_KEY" : var.django_secret_key,
"POSTGRES_PASSWORD" : module.rds.rds_instance_db_password,
Expand Down
63 changes: 63 additions & 0 deletions infrastructure/aws/ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ resource "aws_service_discovery_service" "lit_ssr_service_discovery_service" {
}
}

resource "aws_service_discovery_service" "litellm_service_discovery_service" {
name = "${local.name}-litellm"

dns_config {
namespace_id = aws_service_discovery_private_dns_namespace.private_dns_namespace.id

dns_records {
ttl = 10
type = "A"
}

routing_policy = "MULTIVALUE"
}

health_check_custom_config {
failure_threshold = 1
}
}

resource "aws_secretsmanager_secret" "django-app-secret" {
name = "${local.name}-django-app-secret"
tags = {
Expand Down Expand Up @@ -163,6 +182,41 @@ module "lit-ssr" {
wait_for_ready_state = true
}

module "litellm" {
# checkov:skip=CKV_TF_1: We're using semantic versions instead of commit hash
#source = "../../i-dot-ai-core-terraform-modules//modules/infrastructure/ecs" # For testing local changes
source = "git::https://github.com/i-dot-ai/i-dot-ai-core-terraform-modules.git//modules/infrastructure/ecs?ref=v1.0.0-ecs"
service_discovery_service_arn = aws_service_discovery_service.litellm_service_discovery_service.arn
memory = 4096
cpu = 2048
create_listener = false
create_networking = false
name = "${local.name}-litellm"
image_tag = "main-latest"
ecr_repository_uri = "ghcr.io/berriai/litellm"
ecs_cluster_id = module.cluster.ecs_cluster_id
ecs_cluster_name = module.cluster.ecs_cluster_name
autoscaling_minimum_target = 1
autoscaling_maximum_target = 1
environment_variables = local.django_app_environment_variables
health_check = {
healthy_threshold = 3
unhealthy_threshold = 3
accepted_response = "200"
path = "/test"
timeout = 5
}
state_bucket = var.state_bucket
vpc_id = data.terraform_remote_state.vpc.outputs.vpc_id
private_subnets = data.terraform_remote_state.vpc.outputs.private_subnets
container_port = 4000
load_balancer_security_group = module.load_balancer.load_balancer_security_group_id
aws_lb_arn = module.load_balancer.alb_arn
ephemeral_storage = 30
auto_scale_off_peak_times = true
wait_for_ready_state = true
}


resource "aws_security_group_rule" "ecs_ingress_django_to_lit_ssr" {
type = "ingress"
Expand All @@ -174,3 +228,12 @@ resource "aws_security_group_rule" "ecs_ingress_django_to_lit_ssr" {
security_group_id = module.lit-ssr.ecs_sg_id
}

resource "aws_security_group_rule" "ecs_django_to_litellm" {
type = "ingress"
description = "Allow all traffic from the django-app to litellm"
from_port = 0
to_port = 0
protocol = "-1"
source_security_group_id = module.django-app.ecs_sg_id
security_group_id = module.litellm.ecs_sg_id
}
1 change: 1 addition & 0 deletions infrastructure/aws/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ resource "aws_iam_role_policy_attachment" "redbox_role_policy" {
{
"worker" = module.worker.ecs_task_execution_exec_role_name,
"django" = module.django-app.ecs_task_execution_exec_role_name,
"litellm" = module.litellm.ecs_task_execution_exec_role_name,
}
)
role = each.value
Expand Down
6 changes: 6 additions & 0 deletions infrastructure/aws/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ variable "elastic_api_key" {
description = "API Key for elastic cloud instance"
}

variable "litellm_master_key" {
type = string
description = "master key for lite-llm"
default = "sk-1234"
}

variable "env" {
type = string
description = "Environment"
Expand Down
35 changes: 35 additions & 0 deletions litellm_proxy_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
model_list:
- model_name: gpt-4o-mini
litellm_params:
model: azure/gpt-4o-mini
api_base: "os.environ/AZURE_OPENAI_ENDPOINT"
api_key: "os.environ/AZURE_OPENAI_API_KEY"
api_version: "2024-07-01-preview"

- model_name: gpt-4o
litellm_params:
model: azure/gpt-4o
api_base: "os.environ/AZURE_OPENAI_ENDPOINT"
api_key: "os.environ/AZURE_OPENAI_API_KEY"
api_version: "2024-07-01-preview"

- model_name: gpt-35-turbo-16k
litellm_params:
model: azure/gpt-35-turbo-16k
api_base: "os.environ/AZURE_OPENAI_ENDPOINT"
api_key: "os.environ/AZURE_OPENAI_API_KEY"
api_version: "2024-07-01-preview"

- model_name: gpt-4o-2024-08-06
litellm_params:
model: azure/gpt-4o-2024-08-06
api_base: "os.environ/AZURE_OPENAI_ENDPOINT"
api_key: "os.environ/AZURE_OPENAI_API_KEY"
api_version: "2024-07-01-preview"

general_settings:
master_key: sk-1234
database_url: os.environ/POSTGRES_URL

litellm_settings:
ssl_verify: false
7 changes: 7 additions & 0 deletions redbox-core/redbox/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from logging import getLogger

from langchain.chat_models import init_chat_model
Expand Down Expand Up @@ -35,7 +36,13 @@ def __init__(self, debug: bool = False):
self.debug = debug

def _get_runnable(self, state: RedboxState):
azure_endpoint = os.environ["LITELLM_URL"]
api_key = os.environ["LITELLM_MASTER_KEY"]
logger.info("LITELLM_URL=%s, LITELLM_MASTER_KEY=%s", azure_endpoint, api_key)

llm = init_chat_model(
azure_endpoint=azure_endpoint,
api_key=api_key,
model=state.chat_backend.name,
model_provider=state.chat_backend.provider,
)
Expand Down
5 changes: 5 additions & 0 deletions tests/.env.integration
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ EMAIL_FILE_PATH='/app/mail'

FILE_EXPIRY_IN_DAYS=30
MAX_SECURITY_CLASSIFICATION=OFFICIAL_SENSITIVE

LITELLM_URL=http://litellm:4000
LITELLM_MASTER_KEY=sk-1234
POSTGRES_URL=postgresql://redbox-core:insecure@db:5432/litellm

12 changes: 4 additions & 8 deletions tests/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ OPENAI_API_VERSION=2024-02-01

DEV_MODE=true

# === Database ===

KIBANA_SYSTEM_PASSWORD=redboxpass
METRICBEAT_INTERNAL_PASSWORD=redboxpass
FILEBEAT_INTERNAL_PASSWORD=redboxpass
HEARTBEAT_INTERNAL_PASSWORD=redboxpass
MONITORING_INTERNAL_PASSWORD=redboxpass
BEATS_SYSTEM_PASSWORD=redboxpass


# === Object Storage ===
Expand Down Expand Up @@ -60,3 +52,7 @@ USER_EMAIL=

# OFFICIAL, OFFICIAL_SENSITIVE, SECRET or TOP_SECRET
MAX_SECURITY_CLASSIFICATION=OFFICIAL_SENSITIVE

LITELLM_URL=http://0.0.0.0:4000
LITELLM_MASTER_KEY=sk-1234
POSTGRES_URL=postgresql://redbox-core:insecure@localhost:5432/litellm
Loading