Skip to content

Commit

Permalink
feat: initial implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Matous Havlena <[email protected]>
  • Loading branch information
matoushavlena committed Oct 17, 2024
1 parent b836b70 commit acd7337
Show file tree
Hide file tree
Showing 406 changed files with 38,587 additions and 0 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
119 changes: 119 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Port used by the server
PORT=4000 # optional

# Log level, set to "trace" for detailed logs
LOG_LEVEL=info # optional

# Secret used by the server to encrypt/decrypt values
# You can use `$ openssl rand -base64 32` to generate new one
CRYPTO_CIPHER_KEY=

# MongoDB database, must be a replica set, see https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set/
MONGODB_URL=mongodb://localhost:27017?directConnection=true
MONGODB_DATABASE_NAME=bee-api
MONGODB_CA_CERT= # optional

# Redis database, used by distributed queue and as pub/sub broker
REDIS_URL=redis://127.0.0.1:6379/0
REDIS_CA_CERT= # optional

# Redis database for data caching
# In production, make sure you set up eviction policy to `volatile-lru`
# https://redis.io/docs/latest/develop/reference/eviction/#eviction-policies
REDIS_CACHE_URL=redis://127.0.0.1:6379/1
REDIS_CACHE_CA_CERT= # optional

# Object Storage used for files and their text extractions
S3_ENDPOINT=
S3_ACCESS_KEY_ID=
S3_SECRET_ACCESS_KEY=
S3_BUCKET_FILE_STORAGE=

# OAuth2 client credentials for access token validation, acting as the resource server
AUTH_WELL_KNOWN=
AUTH_CLIENT_ID=
AUTH_CLIENT_SECRET=
AUTH_AUDIENCE=

# Queue workers
RUN_BULLMQ_WORKERS=runs,runs:cleanup,vectorStores:cleanup,vectorStores:fileProcessor,files:extraction,threads:cleanup,files:cleanup


# --- BACKEND SECTION ---

# LLM backend, possible values are: ollama, openai, watsonx, bam, ibm-vllm
LLM_BACKEND=watsonx

# Embedding backend, possible values are: ollama, openai, watsonx, bam, caikit
EMBEDDING_BACKEND=watsonx

# --- Backend details (only applicable to backend(s) selected above) ---

# https://ollama.com/
OLLAMA_URL=

# https://openai.com/
OPENAI_API_KEY=

# https://www.ibm.com/products/watsonx-ai
WATSONX_API_KEY=
WATSONX_PROJECT_ID=

BAM_API_KEY=

# Must contain port, can contain {model_id} placeholder, e.g. "{model_id}.inference.example.com:443"
IBM_VLLM_URL=
IBM_VLLM_ROOT_CERT=
IBM_VLLM_CERT_CHAIN=
IBM_VLLM_PRIVATE_KEY=

CAIKIT_URL=
CAIKIT_CA_CERT=
CAIKIT_CERT=
CAIKIT_KEY=

# --- BACKEND SECTION ---

# --- TOOLS SECTION ---

# Code Interpreter, see https://github.com/i-am-bee/bee-code-interpreter
BEE_CODE_INTERPRETER_URL=http://localhost:50051
BEE_CODE_INTERPRETER_CA_CERT= # optional
BEE_CODE_INTERPRETER_CERT= # optional
BEE_CODE_INTERPRETER_KEY= # optional
# Storage for code interpreter, possible values are: s3, filesystem
BEE_CODE_INTERPRETER_STORAGE_BACKEND=s3
# applicable for s3 storage, uses S3 credential above
BEE_CODE_INTERPRETER_BUCKET_FILE_STORAGE=
# applicable for filesystem storage
BEE_CODE_INTERPRETER_FILE_STORAGE_PATH=

# Milvus is used as a vector store for file search tool
MILVUS_HOST=127.0.0.1
MILVUS_PORT=19530
MILVUS_USE_TLS=false
MILVUS_USERNAME=
MILVUS_PASSWORD=
MILVUS_DATABASE_NAME=

# Search tool backend, possible values are: google, duck-duck-go
SEARCH_TOOL_BACKEND=duck-duck-go
# applicable for google search tool
BEE_GOOGLE_SEARCH_API_KEY=
BEE_GOOGLE_SEARCH_CSE_ID=

# Forward proxy for api calling tool
HTTP_PROXY_URL= # optional

# --- TOOLS SECTION ---

# Observer server, see https://github.com/i-am-bee/bee-observe
BEE_OBSERVE_API_URL= # optional
BEE_OBSERVE_API_AUTH_KEY= # optional

# Text extraction service
WDU_URL= # optional

# Identifiers to be used for seeder and to allocate new users into default org/project
ORGANIZATION_OWNER_ID_DEFAULT=org_user_670cc04869ddffe24f4fd70e
PROJECT_ADMIN_ID_DEFAULT=proj_principal_670cc04869ddffe24f4fd710
2 changes: 2 additions & 0 deletions .env.test.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BEE_URL=http://localhost:4000
BEE_TOKEN=
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/**/grpc/types/**
44 changes: 44 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:import/recommended',
'plugin:import/typescript',
'plugin:prettier/recommended'
],
parserOptions: {
project: ['tsconfig.json'],
tsconfigRootDir: __dirname
},
plugins: ['@typescript-eslint', 'import'],
settings: {
'import/resolver': {
typescript: true,
node: true
}
},
rules: {
'import/order': [
'error',
{
'newlines-between': 'always'
}
],
'import/default': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'import/no-named-as-default-member': 'off',
'import/no-named-as-default': 'off',
'@typescript-eslint/no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_',
},
],
'@typescript-eslint/no-empty-function': 'off',
'no-console': 'error'
}
};
48 changes: 48 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# production
/dist

# playground
/playground

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# env files
.env
.env.test

# typescript
*.tsbuildinfo

# vscode
.vscode

# mikro-orm ts metadata cache
/temp

# npm
.npmrc

# other
/tmp


# WebStorm
.idea
2 changes: 2 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pnpm exec lint-staged
CI=true pnpm copyright
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22.2.0
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"printWidth": 100,
"singleQuote": true,
"trailingComma": "none"
}
54 changes: 54 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
FROM node:22.2-alpine AS base

# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat

ENV APP_DIR=/app

RUN mkdir -p ${APP_DIR}
WORKDIR ${APP_DIR}

# Install dependencies only when needed
FROM base AS deps

ENV CI=1
ENV HUSKY=0

# Install dependencies based on the preferred package manager
COPY package.json pnpm-lock.yaml* ./
RUN corepack enable pnpm && pnpm i --frozen-lockfile

# Rebuild the source code only when needed
FROM base AS builder

ENV CI=1

COPY --from=deps ${APP_DIR}/node_modules ./node_modules
COPY . .

RUN corepack enable pnpm && pnpm run build;

# Production image, copy all the files and run next
FROM base AS runner

ENV NODE_ENV production

# https://github.com/nodejs/docker-node/blob/main/docs/BestPractices.md
RUN deluser --remove-home node \
&& addgroup -S node -g 1001 \
&& adduser -S -G node -u 1001 node

COPY --chown=node:node --from=deps ${APP_DIR}/package.json ./
COPY --chown=node:node --from=deps ${APP_DIR}/node_modules ./node_modules
COPY --chown=node:node --from=builder ${APP_DIR}/dist ./dist

RUN chown -R 1001:0 ${APP_DIR} &&\
chmod -R g+w ${APP_DIR}

USER node

EXPOSE 4000

ENV PORT 4000

CMD [ "node", "--enable-source-maps", "--experimental-loader=@opentelemetry/instrumentation/hook.mjs", "--import", "./dist/opentelemetry.js", "./dist/server.js" ]
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<p align="center">
<img src="./docs/assets/Bee_Dark.svg" height="128">
<h1 align="center">Bee API</h1>
</p>
OpenAI-compatible Assistants API backed by [Bee Agent Framework](https://github.com/i-am-bee/bee-agent-framework)

## Getting started

1. Create `.env` (from `.env.example`) and fill in values.
2. Run `pnpm install` to install dependencies.
3. Start the server with `pnpm start:dev`

## Technologies

- [Fastify](https://fastify.dev/) as the web framework
- [MikroORM](https://mikro-orm.io/) backed by [MongoDB](https://www.mongodb.com/) as the database layer
- [BullMQ](https://docs.bullmq.io/guide/jobs) backed by [Redis](https://redis.io/) as the job executor
- [Bee Agent Framework](https://github.com/i-am-bee/bee-agent-framework) as the agent execution engine

## Architecture overview

The Assistants API consists mostly of CRUDL endpoints for managing API resources like assistants, threads, runs and more. Furthermore, some resources are asynchronous in a sense that they contain `status` changing over time as the background execution progresses. Clients use polling or streaming to watch for status updates of such resources.

### Infrastructure

The infrastructure consists of:
- REST API server
- MongoDB
- Redis

The REST API server stores resources in MongoDB database. Redis is used by BullMQ, rate limiter and as pub/sub broker for event streaming. Agent execution is performed by the Bee Agent Framework using various adapters for inference and embeddings.

### Server

The codebase contains several types of modules:
- `*.modules.ts` containing endpoint handlers
- `*.services.ts` containing services for the handlers
- `dtos/*.ts` containing JSON schema definitions for resources
- `*.entity.ts` containing ORM definitions for database entities
- `*.queue.ts` containing BullMQ queues and workers for asynchronous execution

These modules are connected in the following manner

```
module ---> dto
---> service ---> entity
---> queue ---> entity
```

OpenAPI schema is auto-generated from the `dtos` and exposed on the `/docs` endpoint.
15 changes: 15 additions & 0 deletions docs/assets/Bee_Dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit acd7337

Please sign in to comment.