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

feat: aiproxy service #5209

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
strategy:
matrix:
## TODO: add more modules
module: [database, pay, account, minio, launchpad, exceptionmonitor]
module: [database, pay, account, minio, launchpad, exceptionmonitor, aiproxy]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -182,7 +182,7 @@ jobs:
strategy:
matrix:
## TODO: add more modules
module: [database, pay, account, minio, launchpad, exceptionmonitor]
module: [database, pay, account, minio, launchpad, exceptionmonitor, aiproxy]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down
7 changes: 7 additions & 0 deletions service/aiproxy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM gcr.io/distroless/static:nonroot
ARG TARGETARCH
COPY bin/service-aiproxy-$TARGETARCH /manager
EXPOSE 3000
USER 65532:65532

ENTRYPOINT ["/manager"]
53 changes: 53 additions & 0 deletions service/aiproxy/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
IMG ?= ghcr.io/labring/sealos-aiproxy-service:latest

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

# only support linux, non cgo
PLATFORMS ?= linux_arm64 linux_amd64
GOOS=linux
GOARCH=$(shell go env GOARCH)

.PHONY: all
all: build

##@ General

# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by '##@' and the
# target descriptions by '##'. The awk commands is responsible for reading the
# entire set of makefiles included in this invocation, looking for lines of the
# file as xyz: ## something, and then pretty-format the target and help. Then,
# if there's a line with ##@ something, that gets pretty-printed as a category.
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php

.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Build

.PHONY: clean
clean:
rm -f $(SERVICE_NAME)

.PHONY: build
build: ## Build service-hub binary.
LD_FLAGS="-s -w -extldflags '-static'"; \
CGO_ENABLED=0 GOOS=linux go build -tags "jsoniter" -ldflags "$${LD_FLAGS}" -trimpath -o bin/manager main.go

.PHONY: docker-build
docker-build: build
mv bin/manager bin/service-aiproxy-${TARGETARCH}
docker build -t $(IMG) .

.PHONY: docker-push
docker-push:
docker push $(IMG)
14 changes: 14 additions & 0 deletions service/aiproxy/common/balance/balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package balance

import "context"

type GroupBalance interface {
GetGroupRemainBalance(ctx context.Context, group string) (float64, PostGroupConsumer, error)
}

type PostGroupConsumer interface {
PostGroupConsume(ctx context.Context, tokenName string, usage float64) (float64, error)
GetBalance(ctx context.Context) (float64, error)
}

var Default GroupBalance = NewMockGroupBalance()
27 changes: 27 additions & 0 deletions service/aiproxy/common/balance/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package balance

import "context"

var _ GroupBalance = (*MockGroupBalance)(nil)

const (
mockBalance = 10000000
)

type MockGroupBalance struct{}

func NewMockGroupBalance() *MockGroupBalance {
return &MockGroupBalance{}
}

func (q *MockGroupBalance) GetGroupRemainBalance(_ context.Context, _ string) (float64, PostGroupConsumer, error) {
return mockBalance, q, nil
}

func (q *MockGroupBalance) PostGroupConsume(_ context.Context, _ string, usage float64) (float64, error) {
return usage, nil
}

func (q *MockGroupBalance) GetBalance(_ context.Context) (float64, error) {
return mockBalance, nil
}
Loading
Loading