├── .nvmrc ├── .dockerignore ├── .github ├── CODEOWNERS └── workflows │ └── deployment.yml ├── .env.test.example ├── .eslintignore ├── .husky └── pre-commit ├── .prettierrc ├── workers ├── python │ ├── pyproject.toml │ ├── Dockerfile │ └── python │ │ ├── __init__.py │ │ ├── extraction │ │ └── __init__.py │ │ ├── enums.py │ │ ├── logger.py │ │ └── database.py └── node │ └── main.ts ├── src ├── ui │ ├── jwt.key │ ├── jwks.json │ ├── dtos │ │ └── last_assistant.dto.ts │ └── ui.module.ts ├── files │ ├── utils │ │ └── s3.ts │ ├── extraction │ │ ├── types.ts │ │ └── constants.ts │ ├── entities │ │ ├── files-container.entity.ts │ │ └── extractions │ │ │ ├── wdu-extraction.entity.ts │ │ │ └── unstructured-api-extraction.entity.ts │ └── dtos │ │ ├── file-read.ts │ │ ├── file-content-read.ts │ │ └── file-delete.ts ├── observe │ ├── dtos │ │ ├── shared.ts │ │ └── span-read.ts │ ├── entities │ │ └── trace.entity.ts │ └── api │ │ └── client.ts ├── utils │ ├── id.ts │ ├── crypto │ │ ├── config.ts │ │ ├── encrypt.ts │ │ └── decrypt.ts │ ├── delete.ts │ ├── uri.ts │ ├── update.ts │ ├── networking.ts │ ├── strings.ts │ └── streams.ts ├── users │ ├── dtos │ │ ├── user-read.ts │ │ ├── user-create.ts │ │ ├── user-update.ts │ │ └── user.ts │ ├── helpers.ts │ └── entities │ │ └── user.entity.ts ├── embedding │ ├── constants.ts │ └── adapters │ │ ├── caikit │ │ └── grpc │ │ │ ├── types │ │ │ ├── caikit_data_model │ │ │ │ ├── common │ │ │ │ │ ├── runtime │ │ │ │ │ │ ├── RuntimeInfoRequest.ts │ │ │ │ │ │ ├── ModelInfoRequest.ts │ │ │ │ │ │ ├── RuntimeInfoResponse.ts │ │ │ │ │ │ └── ModelInfoResponse.ts │ │ │ │ │ ├── StrSequence.ts │ │ │ │ │ ├── BoolSequence.ts │ │ │ │ │ ├── FileReference.ts │ │ │ │ │ ├── FloatSequence.ts │ │ │ │ │ ├── PyFloatSequence.ts │ │ │ │ │ ├── ListOfFileReferences.ts │ │ │ │ │ ├── NpFloat32Sequence.ts │ │ │ │ │ ├── NpFloat64Sequence.ts │ │ │ │ │ ├── ProducerId.ts │ │ │ │ │ ├── Directory.ts │ │ │ │ │ ├── IntSequence.ts │ │ │ │ │ ├── File.ts │ │ │ │ │ ├── ListOfVector1D.ts │ │ │ │ │ ├── ProducerPriority.ts │ │ │ │ │ ├── S3Base.ts │ │ │ │ │ ├── S3Path.ts │ │ │ │ │ ├── S3Files.ts │ │ │ │ │ └── TrainingStatus.ts │ │ │ │ ├── runtime │ │ │ │ │ ├── ModelPointer.ts │ │ │ │ │ ├── TrainingInfoRequest.ts │ │ │ │ │ └── TrainingJob.ts │ │ │ │ ├── caikit_nlp │ │ │ │ │ ├── SentenceSimilarityScores.ts │ │ │ │ │ ├── GenerationTrainRecord.ts │ │ │ │ │ ├── ExponentialDecayLengthPenalty.ts │ │ │ │ │ ├── RerankScores.ts │ │ │ │ │ ├── RerankScore.ts │ │ │ │ │ └── TuningConfig.ts │ │ │ │ └── nlp │ │ │ │ │ ├── GeneratedToken.ts │ │ │ │ │ ├── ClassificationResult.ts │ │ │ │ │ ├── ClassificationTrainRecord.ts │ │ │ │ │ ├── InputWarningReason.ts │ │ │ │ │ ├── Token.ts │ │ │ │ │ ├── InputWarning.ts │ │ │ │ │ ├── ClassificationResults.ts │ │ │ │ │ ├── TokenizationResults.ts │ │ │ │ │ ├── TokenClassificationResults.ts │ │ │ │ │ ├── TokenClassificationResult.ts │ │ │ │ │ └── TokenizationStreamResult.ts │ │ │ ├── caikit │ │ │ │ └── runtime │ │ │ │ │ └── Nlp │ │ │ │ │ ├── TokenizationTaskRequest.ts │ │ │ │ │ ├── TextClassificationTaskRequest.ts │ │ │ │ │ ├── TokenClassificationTaskRequest.ts │ │ │ │ │ ├── BidiStreamingTokenClassificationTaskRequest.ts │ │ │ │ │ ├── EmbeddingTaskRequest.ts │ │ │ │ │ ├── EmbeddingTasksRequest.ts │ │ │ │ │ ├── SentenceSimilarityTaskRequest.ts │ │ │ │ │ ├── SentenceSimilarityTasksRequest.ts │ │ │ │ │ └── DataStreamSourceGenerationTrainRecordJsonData.ts │ │ │ └── google │ │ │ │ └── protobuf │ │ │ │ ├── NullValue.ts │ │ │ │ ├── Timestamp.ts │ │ │ │ ├── ListValue.ts │ │ │ │ └── Struct.ts │ │ │ ├── utils │ │ │ ├── url.ts │ │ │ └── metadata.ts │ │ │ ├── constants.ts │ │ │ └── protos │ │ │ └── caikit_runtime_info.proto │ │ ├── ollama-embedding.ts │ │ └── openai-embedding.ts ├── vector-stores │ ├── constants.ts │ ├── entities │ │ └── vector-store-container.entity.ts │ └── dtos │ │ ├── vector-store-delete.ts │ │ └── vector-store-read.ts ├── runs │ ├── dtos │ │ ├── trace-read.ts │ │ ├── run-read.ts │ │ ├── run-cancel.ts │ │ └── run-update.ts │ ├── execution │ │ ├── types.ts │ │ └── constants.ts │ ├── entities │ │ ├── toolApproval.entity.ts │ │ └── requiredAction.entity.ts │ └── jobs │ │ └── runs-cleanup.queue.ts ├── errors │ └── dtos │ │ ├── error-response.ts │ │ └── error.ts ├── tools │ ├── entities │ │ ├── tool-usages │ │ │ ├── code-interpreter-usage.entity.ts │ │ │ ├── user-usage.entity.ts │ │ │ ├── file-search-usage.entity.ts │ │ │ ├── system-usage.entity.ts │ │ │ └── tool-usage.entity.ts │ │ ├── tool │ │ │ ├── function-tool.entity.ts │ │ │ └── api-tool.entity.ts │ │ ├── tool-calls │ │ │ ├── function-call.entity.ts │ │ │ └── user-call.entity.ts │ │ └── tool-resources │ │ │ └── tool-resource.entity.ts │ └── dtos │ │ ├── tool-delete.ts │ │ └── tool-read.ts ├── vector-store-files │ ├── entities │ │ └── chunking-strategy │ │ │ ├── auto-chunking.strategy.entity.ts │ │ │ └── chunking-strategy.entity.ts │ ├── execution │ │ ├── vector_db │ │ │ └── types.ts │ │ └── client.ts │ ├── queues │ │ └── cleanup-vector-store.queue.ts │ └── dtos │ │ ├── vector-store-file-delete.ts │ │ └── vector-store-file-read.ts ├── administration │ ├── entities │ │ ├── constants.ts │ │ ├── principals │ │ │ ├── service-account-principal.entity.ts │ │ │ ├── user-principal.entity.ts │ │ │ └── principal.entity.ts │ │ ├── service-account.entity.ts │ │ └── organization.entity.ts │ └── dtos │ │ ├── project-archive.ts │ │ ├── api-key-delete.ts │ │ ├── project-users-delete.ts │ │ ├── project-read.ts │ │ ├── api-key-read.ts │ │ ├── project-user.ts │ │ ├── project-user-read.ts │ │ ├── project-users-read.ts │ │ ├── organization-user.ts │ │ ├── api-key-update.ts │ │ └── projects-list.ts ├── jobs │ └── constants.ts ├── threads │ └── dtos │ │ ├── thread-delete.ts │ │ ├── threads-list.ts │ │ ├── thread-read.ts │ │ └── thread.ts ├── messages │ └── dtos │ │ ├── message-delete.ts │ │ └── message-read.ts ├── assistants │ └── dtos │ │ ├── assistant-delete.ts │ │ └── assistant-read.ts ├── ajv.ts ├── run-steps │ ├── entities │ │ ├── details │ │ │ ├── run-step-thought.entity.ts │ │ │ └── run-step-message-creation.entity.ts │ │ └── emitter-event.entity.ts │ └── dtos │ │ ├── run-step-delta.ts │ │ └── run-step-read.ts ├── database.ts └── streaming │ └── sse.ts ├── .release-it.json ├── .gitignore ├── tests ├── setup.ts └── e2e │ └── utils.ts ├── tsconfig.json ├── migrations └── Migration20241106150011.ts ├── .eslintrc.cjs └── vite.config.ts /.nvmrc: -------------------------------------------------------------------------------- 1 | v22.2.0 2 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @i-am-bee/api-developers -------------------------------------------------------------------------------- /.env.test.example: -------------------------------------------------------------------------------- 1 | BEE_URL=http://localhost:4000 2 | BEE_TOKEN= -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | src/**/grpc/types/** 2 | dist/ 3 | playground/ 4 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | pnpm exec lint-staged 2 | CI=true pnpm copyright 3 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "printWidth": 100, 3 | "singleQuote": true, 4 | "trailingComma": "none" 5 | } 6 | -------------------------------------------------------------------------------- /workers/python/pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.poetry] 2 | package-mode = false 3 | 4 | [tool.poetry.dependencies] 5 | python = "3.11.10" 6 | bullmq = "2.9.4" 7 | redis = "^5.2.0" 8 | unstructured-ingest = {extras = ["s3"], version = "^0.2.1"} 9 | unstructured = {extras = ["all-docs"], version = "^0.16.4"} 10 | pymongo = "^4.10.1" 11 | pydantic-settings = "^2.6.1" 12 | aiohttp = "^3.10.10" 13 | 14 | 15 | [build-system] 16 | requires = ["poetry-core"] 17 | build-backend = "poetry.core.masonry.api" 18 | -------------------------------------------------------------------------------- /src/ui/jwt.key: -------------------------------------------------------------------------------- 1 | eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0IiwicHJlZmVycmVkX3VzZXJuYW1lIjoiVGVzdCBVc2VyIiwiZW1haWwiOiJ0ZXN0QGVtYWlsLmNvbSIsImlhdCI6MTUxNjIzOTAyMiwiaXNzIjoiaHR0cHM6Ly9sb2NhbGhvc3QiLCJhdWQiOiJiZWUtdGVzdCJ9.vwkGnl7lBbzJYk6BtoW3VoA3mnNJVI-nDQU8aK7zOH-rkf2pn5cn6CKwpq7enDInIXro8WtBLNZP8Nr8GQIZKahICuP3YrPRmzv7YIW8LuXKnx1hycg5OAtj0OtQi5FYwwCxTYW9pBF2it7XwQSBcW7yYsOrvgs7jVhThCOsavX0YiAROxZIhk1idZT4Pl3egfUI_dy9iBxcn7xocTnos-94wqJNt8oCVgB8ynj75yJFHJbiQ-9Tym_V3LcMHoEyv67Jzie8KugCgdpuF6EbQqcyfYJ83q5jJpR2LiuWMuGsNSbjjDY-f1vCSMo9L9-R8KFrDylT_BzLvRBswOzW7A -------------------------------------------------------------------------------- /workers/python/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.11-slim 2 | 3 | RUN apt-get update && apt-get install libmagic-dev libpoppler-cpp-dev libreoffice pandoc tesseract-ocr -y 4 | 5 | RUN useradd -ms /bin/bash runner 6 | USER runner 7 | 8 | # Configure Poetry 9 | ENV POETRY_VERSION=1.8.3 10 | 11 | RUN pip3.11 install poetry==${POETRY_VERSION} 12 | ENV PATH="${PATH}:/home/runner/.local/bin" 13 | 14 | WORKDIR /app 15 | 16 | # Install dependencies 17 | COPY workers/python/poetry.lock workers/python/pyproject.toml ./ 18 | RUN poetry install 19 | 20 | COPY workers/python/python/ ./python/ 21 | 22 | CMD ["poetry", "run", "python", "python/main.py"] -------------------------------------------------------------------------------- /src/ui/jwks.json: -------------------------------------------------------------------------------- 1 | { 2 | "keys": [ 3 | { 4 | "kty": "RSA", 5 | "n": "wg6CJ90ELKikVpE9kfduCL-7g8GxMGISzIRgJ3fZ1lVClhMRLKixCxmm_gkZEfr9O24D16xjB6f1GZYAeIviIfZWIRT-tACrfJQq5fEGtyIPNbfmP6Abwe4weeRXHCNl6Z7m5ee2By8vfeRW9ebOEv81m8tkTvQ5nfzqJpe_ndHgonZ2rO-wDWLYF59l4IsyhERyD6T2Zjel7jkwAoit-dMyCg4qaBzTlIbIbLEF2Xpp2TMJrarT5P61ZJr-en9ceT0Ih4xCEU27qZmvdiMQa9H1ykU4orARjhYG-6kd4xJ9HJbY1JgQYQCtUONEvUmajIxIbPp3DtJ6IkB1cg74fw", 6 | "e": "AQAB", 7 | "ext": true, 8 | "kid": "99810cf0f72de0891faaabf67b", 9 | "alg": "RS256", 10 | "use": "sig" 11 | } 12 | ] 13 | } -------------------------------------------------------------------------------- /workers/python/python/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 IBM Corp. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | -------------------------------------------------------------------------------- /.release-it.json: -------------------------------------------------------------------------------- 1 | { 2 | "git": { 3 | "commitMessage": "chore: release ${version}", 4 | "tagName": "v${version}", 5 | "commitArgs": ["-s"] 6 | }, 7 | "github": { 8 | "release": true, 9 | "releaseName": "v${version}" 10 | }, 11 | "npm": { 12 | "publish": false 13 | }, 14 | "plugins": { 15 | "@release-it/conventional-changelog": { 16 | "preset": { 17 | "name": "conventionalcommits" 18 | }, 19 | "header": "# Changelog", 20 | "infile": "CHANGELOG.md" 21 | } 22 | }, 23 | "hooks": { 24 | "before:init": ["pnpm lint", "pnpm ts:check"], 25 | "after:bump": ["pnpm build"] 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /workers/python/python/extraction/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 IBM Corp. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | .yarn/install-state.gz 8 | 9 | # testing 10 | /coverage 11 | 12 | # production 13 | /dist 14 | 15 | # playground 16 | /playground 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # env files 28 | .env 29 | .env.test 30 | 31 | # typescript 32 | *.tsbuildinfo 33 | 34 | # vscode 35 | .vscode 36 | 37 | # mikro-orm ts metadata cache 38 | /temp 39 | 40 | # npm 41 | .npmrc 42 | 43 | # other 44 | /tmp 45 | 46 | 47 | # WebStorm 48 | .idea 49 | 50 | # Python 51 | .venv 52 | __pycache__ -------------------------------------------------------------------------------- /src/files/utils/s3.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | export const isS3Error = (e: any) => 'code' in e && 'message' in e && 'time' in e; 18 | -------------------------------------------------------------------------------- /tests/setup.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import dotenv from 'dotenv'; 18 | dotenv.config(); 19 | dotenv.config({ 20 | path: '.env.test', 21 | override: true 22 | }); 23 | -------------------------------------------------------------------------------- /.github/workflows/deployment.yml: -------------------------------------------------------------------------------- 1 | name: Deployment 2 | 3 | on: 4 | push: 5 | branches: ['main'] 6 | 7 | jobs: 8 | deploy: 9 | name: Deploy via Travis 10 | runs-on: ubuntu-latest 11 | environment: development 12 | steps: 13 | - uses: actions/checkout@v4 14 | - uses: ruby/setup-ruby@v1 15 | with: 16 | ruby-version: '3.3' # Not needed with a `.ruby-version` or `.tool-versions` 17 | bundler-cache: true # runs 'bundle install' and caches installed gems automatically 18 | - name: 'Install Travis CLI' 19 | run: gem install travis --no-document 20 | - name: 'Trigger rebuild' 21 | run: travis restart --api-endpoint ${{ vars.TRAVIS_ENDPOINT }} --token ${{ secrets.TRAVIS_ACCESS_TOKEN }} --repo ${{ vars.TRAVIS_REPO }} ${{ vars.TRAVIS_BUILD }} 22 | -------------------------------------------------------------------------------- /src/files/extraction/types.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | type UnstructuredExtractionElement = { type: string; text: string }; 18 | export type UnstructuredExtractionDocument = UnstructuredExtractionElement[]; 19 | -------------------------------------------------------------------------------- /src/observe/dtos/shared.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { JSONSchema } from 'json-schema-to-ts'; 18 | 19 | export const includeProperty = { type: 'boolean', default: false } as const satisfies JSONSchema; 20 | -------------------------------------------------------------------------------- /src/utils/id.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { ObjectId } from '@mikro-orm/mongodb'; 18 | 19 | export function generatePrefixedObjectId(prefix: string) { 20 | return `${prefix}_${new ObjectId().toHexString()}`; 21 | } 22 | -------------------------------------------------------------------------------- /workers/python/python/enums.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 IBM Corp. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from enum import StrEnum 16 | 17 | 18 | class ExtractionBackend(StrEnum): 19 | UNSTRUCTURED_OPENSOURCE = 'unstructured-opensource' 20 | UNSTRUCTURED_API = 'unstructured-api' 21 | WDU = 'wdu' 22 | -------------------------------------------------------------------------------- /src/utils/crypto/config.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | export const CRYPTO_ALGORITHM = 'aes-256-cbc'; 18 | export const CRYPTO_INPUT_ENCODING = 'utf8'; 19 | export const CRYPTO_OUTPUT_ENCODING = 'hex'; 20 | export const CRYPTO_IV_LENGTH = 16; 21 | -------------------------------------------------------------------------------- /src/utils/delete.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | export function createDeleteResponse(id: string, object: T) { 18 | return { 19 | id, 20 | object: `${object}.deleted` as const, 21 | deleted: true 22 | }; 23 | } 24 | -------------------------------------------------------------------------------- /src/users/dtos/user-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema } from 'json-schema-to-ts'; 18 | 19 | import { userSchema } from './user.js'; 20 | 21 | export const userReadResponseSchema = userSchema; 22 | export type UserReadResponse = FromSchema; 23 | -------------------------------------------------------------------------------- /tests/e2e/utils.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import OpenAI from 'openai'; 18 | 19 | export function createClient() { 20 | return new OpenAI({ 21 | baseURL: new URL('/v1', process.env.BEE_URL ?? 'http://localhost').toString(), 22 | apiKey: process.env.BEE_TOKEN 23 | }); 24 | } 25 | -------------------------------------------------------------------------------- /workers/python/python/logger.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 IBM Corp. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import logging 16 | 17 | from config import config 18 | 19 | logging.basicConfig(level=logging.DEBUG if config.log_level == 20 | 'trace' else config.log_level.upper()) 21 | logger = logging.getLogger('unstructured_ingest.v2') # TODO fix this bypass 22 | -------------------------------------------------------------------------------- /src/embedding/constants.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | export const EmbeddingBackend = { 18 | CAIKIT: 'caikit', 19 | OLLAMA: 'ollama', 20 | OPENAI: 'openai', 21 | BAM: 'bam', 22 | WATSONX: 'watsonx' 23 | } as const; 24 | export type EmbeddingBackend = (typeof EmbeddingBackend)[keyof typeof EmbeddingBackend]; 25 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/runtime/RuntimeInfoRequest.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common_runtime.proto 18 | 19 | 20 | export interface RuntimeInfoRequest { 21 | } 22 | 23 | export interface RuntimeInfoRequest__Output { 24 | } 25 | -------------------------------------------------------------------------------- /src/files/extraction/constants.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | export const ExtractionBackend = { 18 | UNSTRUCTURED_OPENSOURCE: 'unstructured-opensource', 19 | UNSTRUCTURED_API: 'unstructured-api', 20 | WDU: 'wdu' 21 | } as const; 22 | export type ExtractionBackend = (typeof ExtractionBackend)[keyof typeof ExtractionBackend]; 23 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ESNext", 4 | "module": "ESNext", 5 | "moduleResolution": "Bundler", 6 | "types": [ 7 | "node", 8 | "vitest/globals" 9 | ], 10 | "declarationMap": false, 11 | "declaration": false, 12 | "resolveJsonModule": true, 13 | "sourceMap": false, 14 | "outDir": "./dist", 15 | "removeComments": true, 16 | "esModuleInterop": true, 17 | "forceConsistentCasingInFileNames": true, 18 | "experimentalDecorators": true, 19 | "emitDecoratorMetadata": true, 20 | "strict": true, 21 | "skipLibCheck": true, 22 | "paths": { 23 | "@/*": [ 24 | "./src/*" 25 | ], 26 | "@tests/*": [ 27 | "./tests/*" 28 | ], 29 | }, 30 | "useUnknownInCatchVariables": false, 31 | } 32 | } -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/utils/url.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { IBM_VLLM_URL } from '@/config'; 18 | 19 | export function createGrpcURLFromModel(modelId: string) { 20 | if (!IBM_VLLM_URL) throw new Error('Missing IBM_VLLM_URL'); 21 | return IBM_VLLM_URL.replace(/{model_id}/, modelId.replaceAll('/', '--')); 22 | } 23 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/StrSequence.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | 20 | export interface StrSequence { 21 | 'values'?: (string)[]; 22 | } 23 | 24 | export interface StrSequence__Output { 25 | 'values': (string)[]; 26 | } 27 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/BoolSequence.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | 20 | export interface BoolSequence { 21 | 'values'?: (boolean)[]; 22 | } 23 | 24 | export interface BoolSequence__Output { 25 | 'values': (boolean)[]; 26 | } 27 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/FileReference.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | 20 | export interface FileReference { 21 | 'filename'?: (string); 22 | } 23 | 24 | export interface FileReference__Output { 25 | 'filename': (string); 26 | } 27 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/runtime/ModelPointer.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_runtime.proto 18 | 19 | 20 | export interface ModelPointer { 21 | 'model_id'?: (string); 22 | } 23 | 24 | export interface ModelPointer__Output { 25 | 'model_id': (string); 26 | } 27 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit/runtime/Nlp/TokenizationTaskRequest.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_runtime_Nlp.proto 18 | 19 | 20 | export interface TokenizationTaskRequest { 21 | 'text'?: (string); 22 | } 23 | 24 | export interface TokenizationTaskRequest__Output { 25 | 'text': (string); 26 | } 27 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/FloatSequence.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | 20 | export interface FloatSequence { 21 | 'values'?: (number | string)[]; 22 | } 23 | 24 | export interface FloatSequence__Output { 25 | 'values': (number)[]; 26 | } 27 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/google/protobuf/NullValue.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: null 18 | 19 | export const NullValue = { 20 | NULL_VALUE: 'NULL_VALUE', 21 | } as const; 22 | 23 | export type NullValue = 24 | | 'NULL_VALUE' 25 | | 0 26 | 27 | export type NullValue__Output = typeof NullValue[keyof typeof NullValue] 28 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/PyFloatSequence.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | 20 | export interface PyFloatSequence { 21 | 'values'?: (number | string)[]; 22 | } 23 | 24 | export interface PyFloatSequence__Output { 25 | 'values': (number)[]; 26 | } 27 | -------------------------------------------------------------------------------- /src/observe/entities/trace.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, Property } from '@mikro-orm/core'; 18 | 19 | @Embeddable() 20 | export class Trace { 21 | @Property() 22 | id!: string; 23 | 24 | constructor(input: TraceInput) { 25 | Object.assign(this, input); 26 | } 27 | } 28 | 29 | export type TraceInput = Pick; 30 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/ListOfFileReferences.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | 20 | export interface ListOfFileReferences { 21 | 'files'?: (string)[]; 22 | } 23 | 24 | export interface ListOfFileReferences__Output { 25 | 'files': (string)[]; 26 | } 27 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/NpFloat32Sequence.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | 20 | export interface NpFloat32Sequence { 21 | 'values'?: (number | string)[]; 22 | } 23 | 24 | export interface NpFloat32Sequence__Output { 25 | 'values': (number)[]; 26 | } 27 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/NpFloat64Sequence.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | 20 | export interface NpFloat64Sequence { 21 | 'values'?: (number | string)[]; 22 | } 23 | 24 | export interface NpFloat64Sequence__Output { 25 | 'values': (number)[]; 26 | } 27 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit/runtime/Nlp/TextClassificationTaskRequest.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_runtime_Nlp.proto 18 | 19 | 20 | export interface TextClassificationTaskRequest { 21 | 'text'?: (string); 22 | } 23 | 24 | export interface TextClassificationTaskRequest__Output { 25 | 'text': (string); 26 | } 27 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/runtime/TrainingInfoRequest.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_runtime.proto 18 | 19 | 20 | export interface TrainingInfoRequest { 21 | 'training_id'?: (string); 22 | } 23 | 24 | export interface TrainingInfoRequest__Output { 25 | 'training_id': (string); 26 | } 27 | -------------------------------------------------------------------------------- /src/utils/uri.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | const URN_PREFIX = `urn:peri`; 18 | 19 | export const Resource = { 20 | FILE: 'file' 21 | } as const; 22 | export type Resource = (typeof Resource)[keyof typeof Resource]; 23 | 24 | export function getResourceURN(resource: Resource, resourceId: string) { 25 | return `${URN_PREFIX}:${resource}:${resourceId}` as const; 26 | } 27 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/runtime/ModelInfoRequest.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common_runtime.proto 18 | 19 | 20 | export interface ModelInfoRequest { 21 | 'model_ids'?: (string)[]; 22 | } 23 | 24 | export interface ModelInfoRequest__Output { 25 | 'model_ids': (string)[]; 26 | } 27 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/ProducerId.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | 20 | export interface ProducerId { 21 | 'name'?: (string); 22 | 'version'?: (string); 23 | } 24 | 25 | export interface ProducerId__Output { 26 | 'name': (string); 27 | 'version': (string); 28 | } 29 | -------------------------------------------------------------------------------- /src/users/helpers.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { requestContext } from '@fastify/request-context'; 18 | 19 | import { APIError, APIErrorCode } from '@/errors/error.entity'; 20 | 21 | export function getUser() { 22 | const user = requestContext.get('user'); 23 | if (!user) throw new APIError({ message: 'User not found', code: APIErrorCode.NOT_FOUND }); 24 | return user; 25 | } 26 | -------------------------------------------------------------------------------- /src/utils/update.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { isDefined } from 'remeda'; 18 | 19 | export function getUpdatedValue(value: T | null | undefined, currentValue: T): T; 20 | export function getUpdatedValue( 21 | value: T | null | undefined, 22 | currentValue: T | undefined 23 | ): T | undefined { 24 | return isDefined(value) ? (value ?? undefined) : currentValue; 25 | } 26 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/caikit_nlp/SentenceSimilarityScores.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_caikit_nlp.proto 18 | 19 | 20 | export interface SentenceSimilarityScores { 21 | 'scores'?: (number | string)[]; 22 | } 23 | 24 | export interface SentenceSimilarityScores__Output { 25 | 'scores': (number)[]; 26 | } 27 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/Directory.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | 20 | export interface Directory { 21 | 'dirname'?: (string); 22 | 'extension'?: (string); 23 | } 24 | 25 | export interface Directory__Output { 26 | 'dirname': (string); 27 | 'extension': (string); 28 | } 29 | -------------------------------------------------------------------------------- /src/utils/networking.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Socket } from 'node:net'; 18 | 19 | export function listenToSocketClose(socket: Socket, onClose: () => void) { 20 | if (socket.destroyed) { 21 | throw new Error('Socket already closed!'); 22 | } 23 | 24 | socket.once('close', onClose); 25 | return () => { 26 | socket.removeListener('close', onClose); 27 | }; 28 | } 29 | -------------------------------------------------------------------------------- /src/vector-stores/constants.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { getDefaultEmbeddingModel } from '@/embedding/factory'; 18 | 19 | export const VECTOR_STORE_EMBEDDING_MODEL = getDefaultEmbeddingModel(); 20 | export const VECTOR_STORE_DEFAULT_MAX_CHUNK_SIZE_TOKENS = 400; 21 | export const VECTOR_STORE_DEFAULT_CHUNK_OVERLAP_TOKENS = 200; 22 | export const VECTOR_STORE_DEFAULT_MAX_NUM_RESULTS = 5; 23 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/nlp/GeneratedToken.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_nlp.proto 18 | 19 | 20 | export interface GeneratedToken { 21 | 'text'?: (string); 22 | 'logprob'?: (number | string); 23 | } 24 | 25 | export interface GeneratedToken__Output { 26 | 'text': (string); 27 | 'logprob': (number); 28 | } 29 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/utils/metadata.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Metadata } from '@grpc/grpc-js'; 18 | 19 | const GRPC_MODEL_ID_HEADER = 'mm-model-id'; 20 | 21 | export function createGrpcMetadata({ modelId }: { modelId?: string } = {}) { 22 | const metadata = new Metadata({}); 23 | if (modelId) metadata.add(GRPC_MODEL_ID_HEADER, modelId); 24 | return metadata; 25 | } 26 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/google/protobuf/Timestamp.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: null 18 | 19 | import type { Long } from '@grpc/proto-loader'; 20 | 21 | export interface Timestamp { 22 | 'seconds'?: (number | string | Long); 23 | 'nanos'?: (number); 24 | } 25 | 26 | export interface Timestamp__Output { 27 | 'seconds': (number); 28 | 'nanos': (number); 29 | } 30 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/IntSequence.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | import type { Long } from '@grpc/proto-loader'; 20 | 21 | export interface IntSequence { 22 | 'values'?: (number | string | Long)[]; 23 | } 24 | 25 | export interface IntSequence__Output { 26 | 'values': (number)[]; 27 | } 28 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/runtime/TrainingJob.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_runtime.proto 18 | 19 | 20 | export interface TrainingJob { 21 | 'training_id'?: (string); 22 | 'model_name'?: (string); 23 | } 24 | 25 | export interface TrainingJob__Output { 26 | 'training_id': (string); 27 | 'model_name': (string); 28 | } 29 | -------------------------------------------------------------------------------- /src/ui/dtos/last_assistant.dto.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { assistantSchema } from '@/assistants/dtos/assistant.js'; 20 | 21 | export const lastAssistantsSchema = { 22 | type: 'array', 23 | items: assistantSchema 24 | } as const satisfies JSONSchema; 25 | export type LastAssistants = FromSchema; 26 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/constants.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | export const GRPC_CLIENT_TTL = 15 * 60 * 1000; 18 | export const GRPC_CLIENT_SHUTDOWN_TIMEOUT = GRPC_CLIENT_TTL + 5 * 60 * 1000; 19 | 20 | export const GrpcEmbeddingModel = { 21 | BGE_LARGE_EN_V_1_5: 'baai/bge-large-en-v1.5' 22 | } as const; 23 | export type GrpcEmbeddingModel = (typeof GrpcEmbeddingModel)[keyof typeof GrpcEmbeddingModel]; 24 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/nlp/ClassificationResult.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_nlp.proto 18 | 19 | 20 | export interface ClassificationResult { 21 | 'label'?: (string); 22 | 'score'?: (number | string); 23 | } 24 | 25 | export interface ClassificationResult__Output { 26 | 'label': (string); 27 | 'score': (number); 28 | } 29 | -------------------------------------------------------------------------------- /src/runs/dtos/trace-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema } from 'json-schema-to-ts'; 18 | 19 | import { runParamsSchema } from './run'; 20 | 21 | import { traceSchema } from '@/observe/dtos/trace.js'; 22 | 23 | export const traceReadParamsSchema = runParamsSchema; 24 | export const traceReadResponseSchema = traceSchema; 25 | 26 | export type TraceReadResponse = FromSchema; 27 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/caikit_nlp/GenerationTrainRecord.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_caikit_nlp.proto 18 | 19 | 20 | export interface GenerationTrainRecord { 21 | 'input'?: (string); 22 | 'output'?: (string); 23 | } 24 | 25 | export interface GenerationTrainRecord__Output { 26 | 'input': (string); 27 | 'output': (string); 28 | } 29 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/nlp/ClassificationTrainRecord.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_nlp.proto 18 | 19 | 20 | export interface ClassificationTrainRecord { 21 | 'text'?: (string); 22 | 'labels'?: (string)[]; 23 | } 24 | 25 | export interface ClassificationTrainRecord__Output { 26 | 'text': (string); 27 | 'labels': (string)[]; 28 | } 29 | -------------------------------------------------------------------------------- /src/runs/dtos/run-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema } from 'json-schema-to-ts'; 18 | 19 | import { runParamsSchema, runSchema } from './run.js'; 20 | 21 | export const runReadParamsSchema = runParamsSchema; 22 | export type RunReadParams = FromSchema; 23 | 24 | export const runReadResponseSchema = runSchema; 25 | export type RunReadResponse = FromSchema; 26 | -------------------------------------------------------------------------------- /src/errors/dtos/error-response.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { errorSchema } from './error.js'; 20 | 21 | export const errorResponseSchema = { 22 | type: 'object', 23 | required: ['error'], 24 | properties: { 25 | error: errorSchema 26 | } 27 | } as const satisfies JSONSchema; 28 | export type ErrorResponse = FromSchema; 29 | -------------------------------------------------------------------------------- /src/tools/entities/tool-usages/code-interpreter-usage.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable } from '@mikro-orm/core'; 18 | 19 | import { ToolType } from '../tool/tool.entity.js'; 20 | 21 | import { ToolUsage } from './tool-usage.entity.js'; 22 | 23 | @Embeddable({ discriminatorValue: ToolType.CODE_INTERPRETER }) 24 | export class CodeInterpreterUsage extends ToolUsage { 25 | type = ToolType.CODE_INTERPRETER; 26 | } 27 | -------------------------------------------------------------------------------- /src/vector-store-files/entities/chunking-strategy/auto-chunking.strategy.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable } from '@mikro-orm/core'; 18 | 19 | import { ChunkingStrategy, ChunkingStrategyType } from './chunking-strategy.entity.js'; 20 | 21 | @Embeddable({ discriminatorValue: ChunkingStrategyType.AUTO }) 22 | export class AutoChunkingStrategy extends ChunkingStrategy { 23 | type = ChunkingStrategyType.AUTO; 24 | } 25 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/File.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | 20 | export interface File { 21 | 'data'?: (Buffer | Uint8Array | string); 22 | 'filename'?: (string); 23 | 'type'?: (string); 24 | } 25 | 26 | export interface File__Output { 27 | 'data': (Buffer); 28 | 'filename': (string); 29 | 'type': (string); 30 | } 31 | -------------------------------------------------------------------------------- /src/runs/dtos/run-cancel.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema } from 'json-schema-to-ts'; 18 | 19 | import { runParamsSchema, runSchema } from './run.js'; 20 | 21 | export const runCancelParamsSchema = runParamsSchema; 22 | export type RunCancelParams = FromSchema; 23 | 24 | export const runCancelResponseSchema = runSchema; 25 | export type RunCancelResponse = FromSchema; 26 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/google/protobuf/ListValue.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: null 18 | 19 | import type { 20 | Value as _google_protobuf_Value, 21 | Value__Output as _google_protobuf_Value__Output 22 | } from './Value'; 23 | 24 | export interface ListValue { 25 | values?: _google_protobuf_Value[]; 26 | } 27 | 28 | export interface ListValue__Output { 29 | values: _google_protobuf_Value__Output[]; 30 | } 31 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/nlp/InputWarningReason.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_nlp.proto 18 | 19 | export const InputWarningReason = { 20 | UNSUITABLE_INPUT: 'UNSUITABLE_INPUT', 21 | } as const; 22 | 23 | export type InputWarningReason = 24 | | 'UNSUITABLE_INPUT' 25 | | 0 26 | 27 | export type InputWarningReason__Output = typeof InputWarningReason[keyof typeof InputWarningReason] 28 | -------------------------------------------------------------------------------- /src/administration/entities/constants.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | export const ProjectRole = { 18 | ADMIN: 'admin', 19 | WRITER: 'writer', 20 | READER: 'reader' 21 | } as const; 22 | export type ProjectRole = (typeof ProjectRole)[keyof typeof ProjectRole]; 23 | 24 | export const OrganizationUserRole = { 25 | OWNER: 'owner', 26 | MEMBER: 'member' 27 | } as const; 28 | export type OrganizationUserRole = (typeof OrganizationUserRole)[keyof typeof OrganizationUserRole]; 29 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/google/protobuf/Struct.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: null 18 | 19 | import type { 20 | Value as _google_protobuf_Value, 21 | Value__Output as _google_protobuf_Value__Output 22 | } from './Value'; 23 | 24 | export interface Struct { 25 | fields?: { [key: string]: _google_protobuf_Value }; 26 | } 27 | 28 | export interface Struct__Output { 29 | fields: { [key: string]: _google_protobuf_Value__Output }; 30 | } 31 | -------------------------------------------------------------------------------- /src/files/entities/files-container.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, ManyToOne, Ref, ref } from '@mikro-orm/core'; 18 | 19 | import { File } from '@/files/entities/file.entity.js'; 20 | 21 | @Embeddable() 22 | export class FileContainer { 23 | @ManyToOne() 24 | file!: Ref; 25 | 26 | constructor({ file }: FileContainerInput) { 27 | this.file = ref(file); 28 | } 29 | } 30 | 31 | export type FileContainerInput = { file: File | Ref }; 32 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/nlp/Token.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_nlp.proto 18 | 19 | import type { Long } from '@grpc/proto-loader'; 20 | 21 | export interface Token { 22 | 'start'?: (number | string | Long); 23 | 'end'?: (number | string | Long); 24 | 'text'?: (string); 25 | } 26 | 27 | export interface Token__Output { 28 | 'start': (number); 29 | 'end': (number); 30 | 'text': (string); 31 | } 32 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/runtime/RuntimeInfoResponse.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common_runtime.proto 18 | 19 | 20 | export interface RuntimeInfoResponse { 21 | 'runtime_version'?: (string); 22 | 'python_packages'?: ({[key: string]: string}); 23 | } 24 | 25 | export interface RuntimeInfoResponse__Output { 26 | 'runtime_version': (string); 27 | 'python_packages': ({[key: string]: string}); 28 | } 29 | -------------------------------------------------------------------------------- /src/errors/dtos/error.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { APIErrorCode } from '../error.entity.js'; 20 | 21 | export const errorSchema = { 22 | type: 'object', 23 | required: ['code', 'message'], 24 | properties: { 25 | message: { type: 'string' }, 26 | code: { type: 'string', enum: Object.values(APIErrorCode) } 27 | } 28 | } as const satisfies JSONSchema; 29 | export type Error = FromSchema; 30 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit/runtime/Nlp/TokenClassificationTaskRequest.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_runtime_Nlp.proto 18 | 19 | 20 | export interface TokenClassificationTaskRequest { 21 | 'text'?: (string); 22 | 'threshold'?: (number | string); 23 | '_threshold'?: "threshold"; 24 | } 25 | 26 | export interface TokenClassificationTaskRequest__Output { 27 | 'text': (string); 28 | 'threshold'?: (number); 29 | '_threshold': "threshold"; 30 | } 31 | -------------------------------------------------------------------------------- /src/jobs/constants.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | export const QueueName = { 18 | RUNS: 'runs', 19 | RUNS_CLEANUP: 'runs:cleanup', 20 | THREADS_CLEANUP: 'threads:cleanup', 21 | VECTOR_STORES_CLEANUP: 'vectorStores:cleanup', 22 | VECTOR_STORES_FILE_PROCESSOR: 'vectorStores:fileProcessor', 23 | FILES_EXTRACTION_NODE: 'files:extraction:node', 24 | FILES_EXTRACTION_PYTHON: 'files:extraction:python', 25 | FILES_CLEANUP: 'files:cleanup' 26 | } as const; 27 | export type QueueName = (typeof QueueName)[keyof typeof QueueName]; 28 | -------------------------------------------------------------------------------- /src/tools/dtos/tool-delete.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema } from 'json-schema-to-ts'; 18 | 19 | import { toolReadParamsSchema } from './tool-read.js'; 20 | 21 | import { createDeleteSchema } from '@/schema.js'; 22 | 23 | export const toolDeleteParamsSchema = toolReadParamsSchema; 24 | export type ToolDeleteParams = FromSchema; 25 | 26 | export const toolDeleteResponseSchema = createDeleteSchema('tool'); 27 | export type ToolDeleteResponse = FromSchema; 28 | -------------------------------------------------------------------------------- /src/observe/dtos/span-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { includeProperty } from './shared.js'; 20 | 21 | export const spanReadQuerySchema = { 22 | type: 'object', 23 | required: ['trace_id'], 24 | additionalProperties: false, 25 | properties: { 26 | trace_id: { type: 'string' }, 27 | include_attributes_ctx: includeProperty 28 | } 29 | } as const satisfies JSONSchema; 30 | 31 | export type SpanReadQuery = FromSchema; 32 | -------------------------------------------------------------------------------- /workers/python/python/database.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 IBM Corp. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import tempfile 16 | 17 | from pymongo import AsyncMongoClient 18 | 19 | from config import config 20 | 21 | cert = None 22 | if config.mongodb_ca_cert is not None: 23 | cert = tempfile.NamedTemporaryFile(delete=False, mode='w') 24 | cert.write(config.mongodb_ca_cert) 25 | cert.close() 26 | 27 | client = AsyncMongoClient( 28 | config.mongodb_url, tlsCAFile=cert.name) if cert is not None else AsyncMongoClient(config.mongodb_url) 29 | database = client.get_database(config.mongodb_database_name) 30 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/caikit_nlp/ExponentialDecayLengthPenalty.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_caikit_nlp.proto 18 | 19 | import type { Long } from '@grpc/proto-loader'; 20 | 21 | export interface ExponentialDecayLengthPenalty { 22 | 'start_index'?: (number | string | Long); 23 | 'decay_factor'?: (number | string); 24 | } 25 | 26 | export interface ExponentialDecayLengthPenalty__Output { 27 | 'start_index': (number); 28 | 'decay_factor': (number); 29 | } 30 | -------------------------------------------------------------------------------- /src/administration/dtos/project-archive.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema } from 'json-schema-to-ts'; 18 | 19 | import { projectSchema } from './project'; 20 | import { projectReadParamsSchema } from './project-read'; 21 | 22 | export const projectArchiveParamsSchema = projectReadParamsSchema; 23 | export type ProjectArchiveParams = FromSchema; 24 | 25 | export const projectArchiveResponseSchema = projectSchema; 26 | export type ProjectArchiveResponse = FromSchema; 27 | -------------------------------------------------------------------------------- /src/embedding/adapters/ollama-embedding.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Ollama } from 'ollama'; 18 | 19 | import { Embedding } from './embedding'; 20 | 21 | export class OllamaEmbedding extends Embedding { 22 | constructor( 23 | public readonly model: string, 24 | public readonly client: Ollama 25 | ) { 26 | super(model); 27 | } 28 | 29 | async embedMany(texts: string[]): Promise { 30 | const response = await this.client.embed({ model: this.model, input: texts }); 31 | return response.embeddings; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/threads/dtos/thread-delete.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema } from 'json-schema-to-ts'; 18 | 19 | import { threadReadParamsSchema } from './thread-read.js'; 20 | 21 | import { createDeleteSchema } from '@/schema.js'; 22 | 23 | export const threadDeleteParamsSchema = threadReadParamsSchema; 24 | export type ThreadDeleteParams = FromSchema; 25 | 26 | export const threadDeleteResponseSchema = createDeleteSchema('thread'); 27 | export type ThreadDeleteResponse = FromSchema; 28 | -------------------------------------------------------------------------------- /src/utils/strings.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | export type SnakeToCamelCase = 18 | Key extends `${infer FirstPart}_${infer FirstLetter}${infer LastPart}` 19 | ? `${FirstPart}${Uppercase}${SnakeToCamelCase}` 20 | : Key; 21 | 22 | export function snakeToCamel(str: S) { 23 | return str 24 | .replace(/[^a-zA-Z0-9]/g, '_') 25 | .replace(/(_)+/g, '_') 26 | .replace(/([_][a-zA-Z])/g, (group) => 27 | group.toUpperCase().replace('_', '') 28 | ) as SnakeToCamelCase; 29 | } 30 | -------------------------------------------------------------------------------- /src/administration/entities/principals/service-account-principal.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, ManyToOne, Ref } from '@mikro-orm/core'; 18 | 19 | import { ServiceAccount } from '../service-account.entity.js'; 20 | 21 | import { Principal, PrincipalType } from './principal.entity.js'; 22 | 23 | @Embeddable({ discriminatorValue: PrincipalType.SERVICE_ACCOUNT }) 24 | export class ServiceAccountPrincipal extends Principal { 25 | type = PrincipalType.SERVICE_ACCOUNT; 26 | 27 | @ManyToOne() 28 | account!: Ref; 29 | } 30 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit/runtime/Nlp/BidiStreamingTokenClassificationTaskRequest.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_runtime_Nlp.proto 18 | 19 | 20 | export interface BidiStreamingTokenClassificationTaskRequest { 21 | 'text_stream'?: (string); 22 | 'threshold'?: (number | string); 23 | '_threshold'?: "threshold"; 24 | } 25 | 26 | export interface BidiStreamingTokenClassificationTaskRequest__Output { 27 | 'text_stream': (string); 28 | 'threshold'?: (number); 29 | '_threshold': "threshold"; 30 | } 31 | -------------------------------------------------------------------------------- /src/threads/dtos/threads-list.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema } from 'json-schema-to-ts'; 18 | 19 | import { threadSchema } from './thread.js'; 20 | 21 | import { createPaginationQuerySchema, withPagination } from '@/schema.js'; 22 | 23 | export const threadsListQuerySchema = createPaginationQuerySchema(); 24 | export type ThreadsListQuery = FromSchema; 25 | 26 | export const threadsListResponseSchema = withPagination(threadSchema); 27 | export type ThreadsListResponse = FromSchema; 28 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/ListOfVector1D.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | import type { 20 | Vector1D as _caikit_data_model_common_Vector1D, 21 | Vector1D__Output as _caikit_data_model_common_Vector1D__Output 22 | } from './Vector1D'; 23 | 24 | export interface ListOfVector1D { 25 | vectors?: _caikit_data_model_common_Vector1D[]; 26 | } 27 | 28 | export interface ListOfVector1D__Output { 29 | vectors: _caikit_data_model_common_Vector1D__Output[]; 30 | } 31 | -------------------------------------------------------------------------------- /src/vector-stores/entities/vector-store-container.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, ManyToOne, Ref, ref } from '@mikro-orm/core'; 18 | 19 | import { VectorStore } from '@/vector-stores/entities/vector-store.entity.js'; 20 | 21 | @Embeddable() 22 | export class VectorStoreContainer { 23 | @ManyToOne() 24 | vectorStore!: Ref; 25 | 26 | constructor({ vectorStore }: VectorStoreContainerInput) { 27 | this.vectorStore = ref(vectorStore); 28 | } 29 | } 30 | 31 | export type VectorStoreContainerInput = { vectorStore: VectorStore }; 32 | -------------------------------------------------------------------------------- /src/messages/dtos/message-delete.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema } from 'json-schema-to-ts'; 18 | 19 | import { messageReadParamsSchema } from './message-read.js'; 20 | 21 | import { createDeleteSchema } from '@/schema.js'; 22 | 23 | export const messageDeleteParamsSchema = messageReadParamsSchema; 24 | export type MessageDeleteParams = FromSchema; 25 | 26 | export const messageDeleteResponseSchema = createDeleteSchema('thread.message'); 27 | export type MessageDeleteResponse = FromSchema; 28 | -------------------------------------------------------------------------------- /src/runs/execution/types.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Loaded } from '@mikro-orm/core'; 18 | 19 | import { Run } from '@/runs/entities/run.entity.js'; 20 | 21 | export type LoadedRun = Loaded< 22 | Run, 23 | | 'thread.*' 24 | | 'assistant.*' 25 | | 'tools.tool' 26 | | 'thread.messages.attachments.file' 27 | | 'thread.toolResources.fileContainers.file' 28 | | 'thread.toolResources.vectorStoreContainers.vectorStore.files' 29 | | 'assistant.toolResources.fileContainers.file' 30 | | 'assistant.toolResources.vectorStoreContainers.vectorStore.files' 31 | >; 32 | -------------------------------------------------------------------------------- /migrations/Migration20241106150011.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Migration } from '@mikro-orm/migrations-mongodb'; 18 | 19 | import { File } from '@/files/entities/file.entity'; 20 | import { ExtractionBackend } from '@/files/extraction/constants'; 21 | 22 | export class Migration20241106150011 extends Migration { 23 | async up(): Promise { 24 | await this.getCollection(File).updateMany( 25 | { extraction: { $exists: true } }, 26 | { $set: { 'extraction.backend': ExtractionBackend.WDU } }, 27 | { session: this.ctx } 28 | ); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/administration/dtos/api-key-delete.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema } from 'json-schema-to-ts'; 18 | 19 | import { apiKeyReadParamsSchema } from './api-key-read'; 20 | 21 | import { createDeleteSchema } from '@/schema'; 22 | 23 | export const apiKeyDeleteParamsSchema = apiKeyReadParamsSchema; 24 | export type ApiKeyDeleteParams = FromSchema; 25 | 26 | export const apiKeyDeleteResponseSchema = createDeleteSchema('organization.project.api_key'); 27 | export type ApiKeyDeleteResponse = FromSchema; 28 | -------------------------------------------------------------------------------- /src/assistants/dtos/assistant-delete.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema } from 'json-schema-to-ts'; 18 | 19 | import { assistantReadParamsSchema } from './assistant-read.js'; 20 | 21 | import { createDeleteSchema } from '@/schema.js'; 22 | 23 | export const assistantDeleteParamsSchema = assistantReadParamsSchema; 24 | export type AssistantDeleteParams = FromSchema; 25 | 26 | export const assistantDeleteResponseSchema = createDeleteSchema('assistant'); 27 | export type AssistantDeleteResponse = FromSchema; 28 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/ProducerPriority.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | import type { 20 | ProducerId as _caikit_data_model_common_ProducerId, 21 | ProducerId__Output as _caikit_data_model_common_ProducerId__Output 22 | } from './ProducerId'; 23 | 24 | export interface ProducerPriority { 25 | producers?: _caikit_data_model_common_ProducerId[]; 26 | } 27 | 28 | export interface ProducerPriority__Output { 29 | producers: _caikit_data_model_common_ProducerId__Output[]; 30 | } 31 | -------------------------------------------------------------------------------- /src/users/dtos/user-create.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { userSchema } from './user.js'; 20 | 21 | import { metadataSchema } from '@/schema.js'; 22 | 23 | export const userCreateBodySchema = { 24 | type: 'object', 25 | properties: { metadata: metadataSchema } 26 | } as const satisfies JSONSchema; 27 | export type UserCreateBody = FromSchema; 28 | 29 | export const userCreateResponseSchema = userSchema; 30 | export type UserCreateResponse = FromSchema; 31 | -------------------------------------------------------------------------------- /src/users/dtos/user-update.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { userSchema } from './user.js'; 20 | 21 | import { metadataSchema } from '@/schema.js'; 22 | 23 | export const userUpdateBodySchema = { 24 | type: 'object', 25 | properties: { metadata: metadataSchema } 26 | } as const satisfies JSONSchema; 27 | export type UserUpdateBody = FromSchema; 28 | 29 | export const userUpdateResponseSchema = userSchema; 30 | export type UserUpdateResponse = FromSchema; 31 | -------------------------------------------------------------------------------- /src/users/dtos/user.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { metadataSchema } from '@/schema.js'; 20 | 21 | export const userSchema = { 22 | type: 'object', 23 | required: ['id', 'object', 'name', 'email'], 24 | properties: { 25 | id: { type: 'string' }, 26 | object: { const: 'user' }, 27 | name: { type: 'string', nullable: true }, 28 | email: { type: 'string', nullable: true }, 29 | metadata: metadataSchema 30 | } 31 | } as const satisfies JSONSchema; 32 | export type User = FromSchema; 33 | -------------------------------------------------------------------------------- /.eslintrc.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | parser: '@typescript-eslint/parser', 4 | extends: [ 5 | 'eslint:recommended', 6 | 'plugin:@typescript-eslint/recommended', 7 | 'plugin:import/recommended', 8 | 'plugin:import/typescript', 9 | 'plugin:prettier/recommended' 10 | ], 11 | parserOptions: { 12 | project: ['tsconfig.json'], 13 | tsconfigRootDir: __dirname 14 | }, 15 | plugins: ['@typescript-eslint', 'import'], 16 | settings: { 17 | 'import/resolver': { 18 | typescript: true, 19 | node: true 20 | } 21 | }, 22 | rules: { 23 | 'import/order': [ 24 | 'error', 25 | { 26 | 'newlines-between': 'always' 27 | } 28 | ], 29 | 'import/default': 'off', 30 | '@typescript-eslint/no-explicit-any': 'off', 31 | 'import/no-named-as-default-member': 'off', 32 | 'import/no-named-as-default': 'off', 33 | '@typescript-eslint/no-unused-vars': [ 34 | 'error', 35 | { 36 | argsIgnorePattern: '^_', 37 | varsIgnorePattern: '^_', 38 | caughtErrorsIgnorePattern: '^_', 39 | }, 40 | ], 41 | '@typescript-eslint/no-empty-function': 'off', 42 | 'no-console': 'error' 43 | } 44 | }; 45 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit/runtime/Nlp/EmbeddingTaskRequest.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_runtime_Nlp.proto 18 | 19 | import type { Long } from '@grpc/proto-loader'; 20 | 21 | export interface EmbeddingTaskRequest { 22 | 'text'?: (string); 23 | 'truncate_input_tokens'?: (number | string | Long); 24 | '_truncate_input_tokens'?: "truncate_input_tokens"; 25 | } 26 | 27 | export interface EmbeddingTaskRequest__Output { 28 | 'text': (string); 29 | 'truncate_input_tokens'?: (number); 30 | '_truncate_input_tokens': "truncate_input_tokens"; 31 | } 32 | -------------------------------------------------------------------------------- /src/files/dtos/file-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { fileSchema } from './file.js'; 20 | 21 | export const fileReadParamsSchema = { 22 | type: 'object', 23 | required: ['file_id'], 24 | additionalProperties: false, 25 | properties: { 26 | file_id: { type: 'string' } 27 | } 28 | } as const satisfies JSONSchema; 29 | export type FileReadParams = FromSchema; 30 | 31 | export const fileReadResponseSchema = fileSchema; 32 | export type FileReadResponse = FromSchema; 33 | -------------------------------------------------------------------------------- /src/administration/dtos/project-users-delete.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema } from 'json-schema-to-ts'; 18 | 19 | import { projectUsersReadParamsSchema } from './project-users-read'; 20 | 21 | import { createDeleteSchema } from '@/schema'; 22 | 23 | export const projectUserDeleteParamsSchema = projectUsersReadParamsSchema; 24 | export type ProjectUserDeleteParams = FromSchema; 25 | 26 | export const projectUserDeleteResponseSchema = createDeleteSchema('project-user'); 27 | export type ProjectUserDeleteResponse = FromSchema; 28 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit/runtime/Nlp/EmbeddingTasksRequest.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_runtime_Nlp.proto 18 | 19 | import type { Long } from '@grpc/proto-loader'; 20 | 21 | export interface EmbeddingTasksRequest { 22 | 'texts'?: (string)[]; 23 | 'truncate_input_tokens'?: (number | string | Long); 24 | '_truncate_input_tokens'?: "truncate_input_tokens"; 25 | } 26 | 27 | export interface EmbeddingTasksRequest__Output { 28 | 'texts': (string)[]; 29 | 'truncate_input_tokens'?: (number); 30 | '_truncate_input_tokens': "truncate_input_tokens"; 31 | } 32 | -------------------------------------------------------------------------------- /src/vector-stores/dtos/vector-store-delete.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema } from 'json-schema-to-ts'; 18 | 19 | import { vectorStoreReadParamsSchema } from './vector-store-read.js'; 20 | 21 | import { createDeleteSchema } from '@/schema.js'; 22 | 23 | export const vectorStoreDeleteParamsSchema = vectorStoreReadParamsSchema; 24 | export type VectorStoreDeleteParams = FromSchema; 25 | 26 | export const vectorStoreDeleteResponseSchema = createDeleteSchema('vectorStore'); 27 | export type VectorStoreDeleteResponse = FromSchema; 28 | -------------------------------------------------------------------------------- /src/files/dtos/file-content-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | export const fileContentReadParamsSchema = { 20 | type: 'object', 21 | required: ['file_id'], 22 | additionalProperties: false, 23 | properties: { 24 | file_id: { type: 'string' } 25 | } 26 | } as const satisfies JSONSchema; 27 | export type FileContentReadParams = FromSchema; 28 | 29 | export const fileContentReadResponseSchema = {} as const as JSONSchema; 30 | export type FileContentReadResponse = FromSchema; 31 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/runtime/ModelInfoResponse.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common_runtime.proto 18 | 19 | import type { 20 | ModelInfo as _caikit_data_model_common_runtime_ModelInfo, 21 | ModelInfo__Output as _caikit_data_model_common_runtime_ModelInfo__Output 22 | } from './ModelInfo'; 23 | 24 | export interface ModelInfoResponse { 25 | models?: _caikit_data_model_common_runtime_ModelInfo[]; 26 | } 27 | 28 | export interface ModelInfoResponse__Output { 29 | models: _caikit_data_model_common_runtime_ModelInfo__Output[]; 30 | } 31 | -------------------------------------------------------------------------------- /src/tools/dtos/tool-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { toolSchema } from './tool.js'; 20 | 21 | export const toolReadParamsSchema = { 22 | type: 'object', 23 | additionalProperties: false, 24 | required: ['tool_id'], 25 | properties: { 26 | tool_id: { 27 | type: 'string' 28 | } 29 | } 30 | } as const satisfies JSONSchema; 31 | export type ToolReadParams = FromSchema; 32 | 33 | export const toolReadResponseSchema = toolSchema; 34 | export type ToolReadResponse = FromSchema; 35 | -------------------------------------------------------------------------------- /src/tools/entities/tool-usages/user-usage.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, ManyToOne, Ref } from '@mikro-orm/core'; 18 | 19 | import { Tool, ToolType } from '../tool/tool.entity.js'; 20 | 21 | import { ToolUsage } from './tool-usage.entity.js'; 22 | 23 | @Embeddable({ discriminatorValue: ToolType.USER }) 24 | export class UserUsage extends ToolUsage { 25 | type = ToolType.USER; 26 | 27 | @ManyToOne() 28 | tool!: Ref; 29 | 30 | constructor({ tool }: UserUsageInput) { 31 | super(); 32 | this.tool = tool; 33 | } 34 | } 35 | 36 | export type UserUsageInput = Pick; 37 | -------------------------------------------------------------------------------- /src/vector-store-files/execution/vector_db/types.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { z } from 'zod'; 18 | 19 | export const VectorDbDocumentSchema = z.object({ 20 | vector: z.number().array(), 21 | text: z.string(), 22 | vectorStoreFileId: z.string(), 23 | source: z 24 | .object({ 25 | file: z.object({ id: z.string(), name: z.string() }).optional(), 26 | url: z.string().optional() 27 | }) 28 | .optional() 29 | }); 30 | 31 | export type VectorDbDocument = z.infer; 32 | 33 | export interface DocumentStats { 34 | byteUsage: number; 35 | totalCreated: number; 36 | } 37 | -------------------------------------------------------------------------------- /src/threads/dtos/thread-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { threadSchema } from './thread.js'; 20 | 21 | export const threadReadParamsSchema = { 22 | type: 'object', 23 | required: ['thread_id'], 24 | additionalProperties: false, 25 | properties: { 26 | thread_id: { type: 'string' } 27 | } 28 | } as const satisfies JSONSchema; 29 | export type ThreadReadParams = FromSchema; 30 | 31 | export const threadReadResponseSchema = threadSchema; 32 | export type ThreadReadResponse = FromSchema; 33 | -------------------------------------------------------------------------------- /src/tools/entities/tool/function-tool.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Entity, Property } from '@mikro-orm/core'; 18 | 19 | import { Tool, ToolExecutor, ToolInput } from './tool.entity.js'; 20 | 21 | @Entity({ discriminatorValue: ToolExecutor.FUNCTION }) 22 | export class FunctionTool extends Tool { 23 | executor = ToolExecutor.FUNCTION; 24 | 25 | @Property() 26 | parameters?: Record; 27 | 28 | constructor({ parameters, ...rest }: FunctionInput) { 29 | super(rest); 30 | this.parameters = parameters; 31 | } 32 | } 33 | 34 | export type FunctionInput = ToolInput & Pick; 35 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/caikit_nlp/RerankScores.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_caikit_nlp.proto 18 | 19 | import type { 20 | RerankScore as _caikit_data_model_caikit_nlp_RerankScore, 21 | RerankScore__Output as _caikit_data_model_caikit_nlp_RerankScore__Output 22 | } from './RerankScore'; 23 | 24 | export interface RerankScores { 25 | query?: string; 26 | scores?: _caikit_data_model_caikit_nlp_RerankScore[]; 27 | } 28 | 29 | export interface RerankScores__Output { 30 | query: string; 31 | scores: _caikit_data_model_caikit_nlp_RerankScore__Output[]; 32 | } 33 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/nlp/InputWarning.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_nlp.proto 18 | 19 | import type { 20 | InputWarningReason as _caikit_data_model_nlp_InputWarningReason, 21 | InputWarningReason__Output as _caikit_data_model_nlp_InputWarningReason__Output 22 | } from './InputWarningReason'; 23 | 24 | export interface InputWarning { 25 | id?: _caikit_data_model_nlp_InputWarningReason; 26 | message?: string; 27 | } 28 | 29 | export interface InputWarning__Output { 30 | id: _caikit_data_model_nlp_InputWarningReason__Output; 31 | message: string; 32 | } 33 | -------------------------------------------------------------------------------- /src/files/dtos/file-delete.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { createDeleteSchema } from '@/schema.js'; 20 | 21 | export const fileDeleteParamsSchema = { 22 | type: 'object', 23 | required: ['file_id'], 24 | additionalProperties: false, 25 | properties: { 26 | file_id: { type: 'string' } 27 | } 28 | } as const satisfies JSONSchema; 29 | export type FileDeleteParams = FromSchema; 30 | 31 | export const fileDeleteResponseSchema = createDeleteSchema('file'); 32 | export type FileDeleteResponse = FromSchema; 33 | -------------------------------------------------------------------------------- /src/administration/dtos/project-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { projectSchema } from './project'; 20 | 21 | export const projectReadParamsSchema = { 22 | type: 'object', 23 | required: ['project_id'], 24 | additionalProperties: false, 25 | properties: { 26 | project_id: { type: 'string' } 27 | } 28 | } as const satisfies JSONSchema; 29 | export type ProjectReadParams = FromSchema; 30 | 31 | export const projectReadResponseSchema = projectSchema; 32 | export type ProjectReadResponse = FromSchema; 33 | -------------------------------------------------------------------------------- /src/ajv.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { ajvFilePlugin as multipartAjvPlugin } from '@fastify/multipart'; 18 | import { ExtendedJSONSchema, FromExtendedSchema } from 'json-schema-to-ts'; 19 | 20 | export const ajv = { 21 | plugins: [multipartAjvPlugin], 22 | customOptions: { 23 | removeAdditional: false, 24 | coerceTypes: 'array' as const, 25 | useDefaults: true, 26 | strict: false 27 | } 28 | }; 29 | 30 | type ExtensionProps = { 31 | isFile: boolean; 32 | }; 33 | 34 | export type JSONSchema = ExtendedJSONSchema; 35 | export type FromSchema = FromExtendedSchema; 36 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/nlp/ClassificationResults.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_nlp.proto 18 | 19 | import type { 20 | ClassificationResult as _caikit_data_model_nlp_ClassificationResult, 21 | ClassificationResult__Output as _caikit_data_model_nlp_ClassificationResult__Output 22 | } from './ClassificationResult'; 23 | 24 | export interface ClassificationResults { 25 | results?: _caikit_data_model_nlp_ClassificationResult[]; 26 | } 27 | 28 | export interface ClassificationResults__Output { 29 | results: _caikit_data_model_nlp_ClassificationResult__Output[]; 30 | } 31 | -------------------------------------------------------------------------------- /src/run-steps/entities/details/run-step-thought.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, Property } from '@mikro-orm/core'; 18 | 19 | import { RunStepDetails, RunStepDetailsType } from './run-step-details.entity.js'; 20 | 21 | @Embeddable({ discriminatorValue: RunStepDetailsType.THOUGHT }) 22 | export class RunStepThought extends RunStepDetails { 23 | type = RunStepDetailsType.THOUGHT; 24 | 25 | @Property() 26 | content?: string; 27 | 28 | constructor(input: RunStepThoughtInput) { 29 | super(); 30 | this.content = input.content; 31 | } 32 | } 33 | 34 | export type RunStepThoughtInput = Pick; 35 | -------------------------------------------------------------------------------- /src/run-steps/entities/emitter-event.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, Property } from '@mikro-orm/core'; 18 | 19 | @Embeddable() 20 | export class EmitterEvent { 21 | @Property() 22 | public readonly id!: string; 23 | 24 | @Property() 25 | public readonly groupId!: string; 26 | 27 | @Property() 28 | public readonly runId?: string; 29 | 30 | @Property() 31 | public readonly parentRunId?: string; 32 | 33 | @Property() 34 | public readonly name!: string; 35 | 36 | @Property() 37 | public readonly path!: string; 38 | 39 | constructor(input: EmitterEvent) { 40 | Object.assign(this, input); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/runs/entities/toolApproval.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, Enum, Property } from '@mikro-orm/core'; 18 | 19 | export enum ToolApprovalType { 20 | ALWAYS = 'always', 21 | NEVER = 'never' 22 | } 23 | 24 | @Embeddable() 25 | export class ToolApproval { 26 | @Property() 27 | toolId?: string; 28 | 29 | @Enum(() => ToolApprovalType) 30 | requireApproval?: ToolApprovalType; 31 | 32 | constructor(input: ToolApprovalInput) { 33 | this.toolId = input.toolId; 34 | this.requireApproval = input.requireApproval; 35 | } 36 | } 37 | 38 | export type ToolApprovalInput = Pick; 39 | -------------------------------------------------------------------------------- /src/vector-store-files/queues/cleanup-vector-store.queue.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { RequestContext } from '@mikro-orm/core'; 18 | 19 | import { ORM } from '@/database.js'; 20 | import { createQueue } from '@/jobs/bullmq.js'; 21 | import { cleanupVectorStores } from '@/vector-store-files/execution/clean-up-vector-db.js'; 22 | import { QueueName } from '@/jobs/constants.js'; 23 | 24 | async function jobHandler() { 25 | return RequestContext.create(ORM.em, async () => cleanupVectorStores()); 26 | } 27 | 28 | export const { queue } = createQueue({ 29 | name: QueueName.VECTOR_STORES_CLEANUP, 30 | jobHandler, 31 | jobsOptions: { attempts: 1 } 32 | }); 33 | -------------------------------------------------------------------------------- /workers/node/main.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import * as http from 'node:http'; 18 | 19 | import { runWorkers } from '@/jobs/bullmq.js'; 20 | import { PORT, RUN_BULLMQ_WORKERS } from '@/config.js'; 21 | import { getLogger } from '@/logger.js'; 22 | import { createTerminus } from '@/terminus.js'; 23 | 24 | const logger = getLogger(); 25 | 26 | try { 27 | await runWorkers(RUN_BULLMQ_WORKERS); 28 | const server = http.createServer((_, res) => res.writeHead(404).end()); 29 | createTerminus(server); 30 | server.listen({ port: PORT, host: '0.0.0.0' }); 31 | } catch (err) { 32 | logger.fatal({ err }, 'Failed to start worker!'); 33 | process.exit(1); 34 | } 35 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/S3Base.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | 20 | export interface S3Base { 21 | 'endpoint'?: (string); 22 | 'region'?: (string); 23 | 'bucket'?: (string); 24 | 'accessKey'?: (string); 25 | 'secretKey'?: (string); 26 | 'IAM_id'?: (string); 27 | 'IAM_api_key'?: (string); 28 | } 29 | 30 | export interface S3Base__Output { 31 | 'endpoint': (string); 32 | 'region': (string); 33 | 'bucket': (string); 34 | 'accessKey': (string); 35 | 'secretKey': (string); 36 | 'IAM_id': (string); 37 | 'IAM_api_key': (string); 38 | } 39 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/nlp/TokenizationResults.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_nlp.proto 18 | 19 | import type { 20 | Token as _caikit_data_model_nlp_Token, 21 | Token__Output as _caikit_data_model_nlp_Token__Output 22 | } from './Token'; 23 | import type { Long } from '@grpc/proto-loader'; 24 | 25 | export interface TokenizationResults { 26 | results?: _caikit_data_model_nlp_Token[]; 27 | token_count?: number | string | Long; 28 | } 29 | 30 | export interface TokenizationResults__Output { 31 | results: _caikit_data_model_nlp_Token__Output[]; 32 | token_count: number; 33 | } 34 | -------------------------------------------------------------------------------- /src/vector-store-files/dtos/vector-store-file-delete.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema } from 'json-schema-to-ts'; 18 | 19 | import { vectorStoreFileReadParamsSchema } from './vector-store-file-read.js'; 20 | 21 | import { createDeleteSchema } from '@/schema.js'; 22 | 23 | export const vectorStoreFileDeleteParamsSchema = vectorStoreFileReadParamsSchema; 24 | export type VectorStoreFileDeleteParams = FromSchema; 25 | 26 | export const vectorStoreFileDeleteResponseSchema = createDeleteSchema('vectorStoreFile'); 27 | export type VectorStoreFileDeleteResponse = FromSchema; 28 | -------------------------------------------------------------------------------- /src/assistants/dtos/assistant-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { assistantSchema } from './assistant.js'; 20 | 21 | export const assistantReadParamsSchema = { 22 | type: 'object', 23 | required: ['assistant_id'], 24 | additionalProperties: false, 25 | properties: { 26 | assistant_id: { type: 'string' } 27 | } 28 | } as const satisfies JSONSchema; 29 | export type AssistantReadParams = FromSchema; 30 | 31 | export const assistantReadResponseSchema = assistantSchema; 32 | export type AssistantReadResponse = FromSchema; 33 | -------------------------------------------------------------------------------- /src/threads/dtos/thread.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { metadataSchema } from '@/schema.js'; 20 | import { toolResourcesSchema } from '@/tools/dtos/tool-resources.js'; 21 | 22 | export const threadSchema = { 23 | type: 'object', 24 | required: ['id', 'metadata', 'created_at', 'object'], 25 | properties: { 26 | id: { type: 'string' }, 27 | object: { const: 'thread' }, 28 | metadata: metadataSchema, 29 | created_at: { type: 'number' }, 30 | tool_resources: toolResourcesSchema 31 | } 32 | } as const satisfies JSONSchema; 33 | export type Thread = FromSchema; 34 | -------------------------------------------------------------------------------- /src/embedding/adapters/openai-embedding.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import OpenAI from 'openai'; 18 | 19 | import { Embedding, EmbeddingOptions } from './embedding'; 20 | 21 | export class OpenAIEmbedding extends Embedding { 22 | constructor( 23 | public readonly model: string, 24 | public readonly client: OpenAI 25 | ) { 26 | super(model); 27 | } 28 | 29 | async embedMany(texts: string[], options?: EmbeddingOptions): Promise { 30 | const response = await this.client.embeddings.create( 31 | { model: this.model, input: texts }, 32 | { signal: options?.signal } 33 | ); 34 | return response.data.map((data) => data.embedding); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/vector-stores/dtos/vector-store-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { vectorStoreSchema } from './vector-store.js'; 20 | 21 | export const vectorStoreReadParamsSchema = { 22 | type: 'object', 23 | additionalProperties: false, 24 | required: ['vector_store_id'], 25 | properties: { vector_store_id: { type: 'string' } } 26 | } as const satisfies JSONSchema; 27 | 28 | export type VectorStoreReadParams = FromSchema; 29 | 30 | export const vectorStoreReadResponseSchema = vectorStoreSchema; 31 | export type VectorStoreReadResponse = FromSchema; 32 | -------------------------------------------------------------------------------- /src/administration/dtos/api-key-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { apiKeySchema } from './api-key'; 20 | 21 | export const apiKeyReadParamsSchema = { 22 | type: 'object', 23 | required: ['project_id', 'api_key_id'], 24 | additionalProperties: false, 25 | properties: { 26 | project_id: { type: 'string' }, 27 | api_key_id: { type: 'string' } 28 | } 29 | } as const satisfies JSONSchema; 30 | export type ApiKeyReadParams = FromSchema; 31 | 32 | export const apiKeyReadResponseSchema = apiKeySchema; 33 | export type ApiKeyReadResponse = FromSchema; 34 | -------------------------------------------------------------------------------- /src/administration/entities/principals/user-principal.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, ManyToOne, Ref } from '@mikro-orm/core'; 18 | 19 | import { OrganizationUser } from '../organization-user.entity.js'; 20 | 21 | import { Principal, PrincipalType } from './principal.entity.js'; 22 | 23 | @Embeddable({ discriminatorValue: PrincipalType.USER }) 24 | export class UserPrincipal extends Principal { 25 | type = PrincipalType.USER; 26 | 27 | @ManyToOne() 28 | user: Ref; 29 | 30 | constructor({ user }: UserPrincipalInput) { 31 | super(); 32 | this.user = user; 33 | } 34 | } 35 | 36 | export type UserPrincipalInput = Pick; 37 | -------------------------------------------------------------------------------- /src/tools/entities/tool-usages/file-search-usage.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, Property } from '@mikro-orm/core'; 18 | 19 | import { ToolType } from '../tool/tool.entity.js'; 20 | 21 | import { ToolUsage } from './tool-usage.entity.js'; 22 | 23 | @Embeddable({ discriminatorValue: ToolType.FILE_SEARCH }) 24 | export class FileSearchUsage extends ToolUsage { 25 | type = ToolType.FILE_SEARCH; 26 | 27 | @Property() 28 | maxNumResults!: number; 29 | 30 | constructor({ maxNumResults }: FileSearchUsageInput) { 31 | super(); 32 | this.maxNumResults = maxNumResults; 33 | } 34 | } 35 | 36 | export type FileSearchUsageInput = Pick; 37 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/nlp/TokenClassificationResults.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_nlp.proto 18 | 19 | import type { 20 | TokenClassificationResult as _caikit_data_model_nlp_TokenClassificationResult, 21 | TokenClassificationResult__Output as _caikit_data_model_nlp_TokenClassificationResult__Output 22 | } from './TokenClassificationResult'; 23 | 24 | export interface TokenClassificationResults { 25 | results?: _caikit_data_model_nlp_TokenClassificationResult[]; 26 | } 27 | 28 | export interface TokenClassificationResults__Output { 29 | results: _caikit_data_model_nlp_TokenClassificationResult__Output[]; 30 | } 31 | -------------------------------------------------------------------------------- /src/messages/dtos/message-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { messageSchema } from './message.js'; 20 | 21 | export const messageReadParamsSchema = { 22 | type: 'object', 23 | required: ['thread_id', 'message_id'], 24 | additionalProperties: false, 25 | properties: { 26 | thread_id: { type: 'string' }, 27 | message_id: { type: 'string' } 28 | } 29 | } as const satisfies JSONSchema; 30 | export type MessageReadParams = FromSchema; 31 | 32 | export const messageReadResponseSchema = messageSchema; 33 | export type MessageReadResponse = FromSchema; 34 | -------------------------------------------------------------------------------- /src/files/entities/extractions/wdu-extraction.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, Property } from '@mikro-orm/core'; 18 | 19 | import { Extraction } from './extraction.entity'; 20 | 21 | import { ExtractionBackend } from '@/files/extraction/constants'; 22 | 23 | @Embeddable({ discriminatorValue: ExtractionBackend.WDU }) 24 | export class WDUExtraction extends Extraction { 25 | backend = ExtractionBackend.WDU; 26 | 27 | @Property() 28 | storageId?: string; 29 | 30 | constructor({ storageId, ...rest }: WDUExtractionInput) { 31 | super(rest); 32 | this.storageId = storageId; 33 | } 34 | } 35 | 36 | export type WDUExtractionInput = Pick; 37 | -------------------------------------------------------------------------------- /src/observe/api/client.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import createClient from 'openapi-fetch'; 18 | 19 | import { paths } from './schema.js'; 20 | 21 | import { BEE_OBSERVE_API_AUTH_KEY, BEE_OBSERVE_API_URL } from '@/config.js'; 22 | 23 | export type Span = 24 | paths['/trace']['post']['requestBody']['content']['application/json']['spans'][0]; 25 | 26 | export type Client = ReturnType>; 27 | 28 | export const client = BEE_OBSERVE_API_URL 29 | ? createClient({ 30 | baseUrl: BEE_OBSERVE_API_URL, 31 | headers: { 32 | ['x-bee-authorization']: BEE_OBSERVE_API_AUTH_KEY, 33 | 'Content-Type': 'application/json' 34 | } 35 | }) 36 | : undefined; 37 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/S3Path.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | 20 | export interface S3Path { 21 | 'path'?: (string); 22 | 'endpoint'?: (string); 23 | 'region'?: (string); 24 | 'bucket'?: (string); 25 | 'accessKey'?: (string); 26 | 'secretKey'?: (string); 27 | 'IAM_id'?: (string); 28 | 'IAM_api_key'?: (string); 29 | } 30 | 31 | export interface S3Path__Output { 32 | 'path': (string); 33 | 'endpoint': (string); 34 | 'region': (string); 35 | 'bucket': (string); 36 | 'accessKey': (string); 37 | 'secretKey': (string); 38 | 'IAM_id': (string); 39 | 'IAM_api_key': (string); 40 | } 41 | -------------------------------------------------------------------------------- /src/run-steps/dtos/run-step-delta.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { runStepDetailsDeltaSchema } from './run-step-details-delta.js'; 20 | 21 | export const runStepDeltaSchema = { 22 | type: 'object', 23 | required: ['id', 'object', 'delta'], 24 | properties: { 25 | id: { type: 'string' }, 26 | object: { const: 'thread.run.step.delta' }, 27 | delta: { 28 | type: 'object', 29 | required: ['step_details'], 30 | properties: { 31 | step_details: runStepDetailsDeltaSchema 32 | } 33 | } 34 | } 35 | } as const satisfies JSONSchema; 36 | export type RunStepDelta = FromSchema; 37 | -------------------------------------------------------------------------------- /src/administration/dtos/project-user.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { ProjectRole } from '../entities/constants'; 20 | 21 | export const projectUserSchema = { 22 | type: 'object', 23 | required: ['object', 'id', 'name', 'email', 'role', 'added_at'], 24 | properties: { 25 | object: { const: 'organization.project.user' }, 26 | id: { type: 'string' }, 27 | name: { type: 'string' }, 28 | email: { type: 'string' }, 29 | role: { type: 'string', enum: Object.values(ProjectRole) }, 30 | added_at: { type: 'number' } 31 | } 32 | } as const satisfies JSONSchema; 33 | export type ProjectUser = FromSchema; 34 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit/runtime/Nlp/SentenceSimilarityTaskRequest.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_runtime_Nlp.proto 18 | 19 | import type { Long } from '@grpc/proto-loader'; 20 | 21 | export interface SentenceSimilarityTaskRequest { 22 | 'source_sentence'?: (string); 23 | 'sentences'?: (string)[]; 24 | 'truncate_input_tokens'?: (number | string | Long); 25 | '_truncate_input_tokens'?: "truncate_input_tokens"; 26 | } 27 | 28 | export interface SentenceSimilarityTaskRequest__Output { 29 | 'source_sentence': (string); 30 | 'sentences': (string)[]; 31 | 'truncate_input_tokens'?: (number); 32 | '_truncate_input_tokens': "truncate_input_tokens"; 33 | } 34 | -------------------------------------------------------------------------------- /src/database.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { MongoDriver, MikroORM } from '@mikro-orm/mongodb'; 18 | import { RequestContext } from '@mikro-orm/core'; 19 | import { FastifyPluginAsync } from 'fastify'; 20 | import fp from 'fastify-plugin'; 21 | 22 | import config from './mikro-orm.config.js'; 23 | import { getLogger } from './logger.js'; 24 | 25 | export const ORM = await MikroORM.init({ 26 | ...config, 27 | logger: (msg) => getLogger().trace(msg) 28 | }); 29 | await ORM.schema.createSchema(); 30 | 31 | export const databasePlugin: FastifyPluginAsync = fp.default(async (app) => { 32 | app.addHook('preHandler', (request, reply, done) => { 33 | RequestContext.create(ORM.em, done); 34 | }); 35 | }); 36 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/S3Files.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | 20 | export interface S3Files { 21 | 'files'?: (string)[]; 22 | 'endpoint'?: (string); 23 | 'region'?: (string); 24 | 'bucket'?: (string); 25 | 'accessKey'?: (string); 26 | 'secretKey'?: (string); 27 | 'IAM_id'?: (string); 28 | 'IAM_api_key'?: (string); 29 | } 30 | 31 | export interface S3Files__Output { 32 | 'files': (string)[]; 33 | 'endpoint': (string); 34 | 'region': (string); 35 | 'bucket': (string); 36 | 'accessKey': (string); 37 | 'secretKey': (string); 38 | 'IAM_id': (string); 39 | 'IAM_api_key': (string); 40 | } 41 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit/runtime/Nlp/SentenceSimilarityTasksRequest.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_runtime_Nlp.proto 18 | 19 | import type { Long } from '@grpc/proto-loader'; 20 | 21 | export interface SentenceSimilarityTasksRequest { 22 | 'source_sentences'?: (string)[]; 23 | 'sentences'?: (string)[]; 24 | 'truncate_input_tokens'?: (number | string | Long); 25 | '_truncate_input_tokens'?: "truncate_input_tokens"; 26 | } 27 | 28 | export interface SentenceSimilarityTasksRequest__Output { 29 | 'source_sentences': (string)[]; 30 | 'sentences': (string)[]; 31 | 'truncate_input_tokens'?: (number); 32 | '_truncate_input_tokens': "truncate_input_tokens"; 33 | } 34 | -------------------------------------------------------------------------------- /src/utils/crypto/encrypt.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { createCipheriv, randomBytes } from 'crypto'; 18 | 19 | import { 20 | CRYPTO_ALGORITHM, 21 | CRYPTO_INPUT_ENCODING, 22 | CRYPTO_IV_LENGTH, 23 | CRYPTO_OUTPUT_ENCODING 24 | } from './config.js'; 25 | 26 | import { CRYPTO_CIPHER_KEY } from '@/config.js'; 27 | 28 | const encrypt = (input: string) => { 29 | const iv = randomBytes(CRYPTO_IV_LENGTH); 30 | const cipher = createCipheriv(CRYPTO_ALGORITHM, CRYPTO_CIPHER_KEY, iv); 31 | const encrypted = Buffer.concat([ 32 | iv, 33 | cipher.update(input, CRYPTO_INPUT_ENCODING), 34 | cipher.final() 35 | ]); 36 | return encrypted.toString(CRYPTO_OUTPUT_ENCODING); 37 | }; 38 | 39 | export default encrypt; 40 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/common/TrainingStatus.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_common.proto 18 | 19 | export const TrainingStatus = { 20 | PLACEHOLDER_UNSET: 'PLACEHOLDER_UNSET', 21 | QUEUED: 'QUEUED', 22 | RUNNING: 'RUNNING', 23 | COMPLETED: 'COMPLETED', 24 | CANCELED: 'CANCELED', 25 | ERRORED: 'ERRORED', 26 | } as const; 27 | 28 | export type TrainingStatus = 29 | | 'PLACEHOLDER_UNSET' 30 | | 0 31 | | 'QUEUED' 32 | | 1 33 | | 'RUNNING' 34 | | 2 35 | | 'COMPLETED' 36 | | 3 37 | | 'CANCELED' 38 | | 4 39 | | 'ERRORED' 40 | | 5 41 | 42 | export type TrainingStatus__Output = typeof TrainingStatus[keyof typeof TrainingStatus] 43 | -------------------------------------------------------------------------------- /src/administration/dtos/project-user-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { projectUserSchema } from './project-user'; 20 | 21 | export const projectUserReadParamsSchema = { 22 | type: 'object', 23 | required: ['project_id', 'user_id'], 24 | additionalProperties: false, 25 | properties: { 26 | project_id: { type: 'string' }, 27 | user_id: { type: 'string' } 28 | } 29 | } as const satisfies JSONSchema; 30 | export type ProjectUserReadParams = FromSchema; 31 | 32 | export const projectUserReadResponseSchema = projectUserSchema; 33 | export type ProjectUserReadResponse = FromSchema; 34 | -------------------------------------------------------------------------------- /src/run-steps/dtos/run-step-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { runStepSchema } from './run-step.js'; 20 | 21 | export const runStepReadParamsSchema = { 22 | type: 'object', 23 | required: ['thread_id', 'run_id', 'step_id'], 24 | properties: { 25 | thread_id: { type: 'string' }, 26 | run_id: { type: 'string' }, 27 | step_id: { type: 'string' } 28 | }, 29 | additionalProperties: false 30 | } as const satisfies JSONSchema; 31 | export type RunStepReadParams = FromSchema; 32 | 33 | export const runStepReadResponseSchema = runStepSchema; 34 | export type RunStepReadResponse = FromSchema; 35 | -------------------------------------------------------------------------------- /src/administration/dtos/project-users-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { projectUserSchema } from './project-user'; 20 | 21 | export const projectUsersReadParamsSchema = { 22 | type: 'object', 23 | required: ['project_id', 'user_id'], 24 | additionalProperties: false, 25 | properties: { 26 | project_id: { type: 'string' }, 27 | user_id: { type: 'string' } 28 | } 29 | } as const satisfies JSONSchema; 30 | export type ProjectsUserReadParams = FromSchema; 31 | 32 | export const projectUsersReadResponseSchema = projectUserSchema; 33 | export type ProjectUsersReadResponse = FromSchema; 34 | -------------------------------------------------------------------------------- /src/administration/entities/principals/principal.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, Enum } from '@mikro-orm/core'; 18 | 19 | import { UserPrincipal } from './user-principal.entity.js'; 20 | import { ServiceAccountPrincipal } from './service-account-principal.entity.js'; 21 | 22 | export const PrincipalType = { 23 | USER: 'user', 24 | SERVICE_ACCOUNT: 'service_account' 25 | } as const; 26 | export type PrincipalType = (typeof PrincipalType)[keyof typeof PrincipalType]; 27 | 28 | @Embeddable({ abstract: true, discriminatorColumn: 'type' }) 29 | export abstract class Principal { 30 | @Enum(() => PrincipalType) 31 | type!: PrincipalType; 32 | } 33 | 34 | export type AnyPrincipal = ServiceAccountPrincipal | UserPrincipal; 35 | -------------------------------------------------------------------------------- /src/administration/entities/service-account.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Entity, Property } from '@mikro-orm/core'; 18 | 19 | import { 20 | ProjectAdministrationScopedEntity, 21 | ProjectAdministrationScopedEntityInput 22 | } from './project-administration-scoped.entity'; 23 | 24 | @Entity() 25 | export class ServiceAccount extends ProjectAdministrationScopedEntity { 26 | getIdPrefix(): string { 27 | return 'svc_acct'; 28 | } 29 | 30 | @Property() 31 | name: string; 32 | 33 | constructor({ name, ...rest }: ServiceAccountInput) { 34 | super(rest); 35 | this.name = name; 36 | } 37 | } 38 | 39 | export type ServiceAccountInput = ProjectAdministrationScopedEntityInput & 40 | Pick; 41 | -------------------------------------------------------------------------------- /src/tools/entities/tool/api-tool.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Entity, Property } from '@mikro-orm/core'; 18 | 19 | import { Tool, ToolExecutor, ToolInput } from './tool.entity.js'; 20 | 21 | @Entity({ discriminatorValue: ToolExecutor.API }) 22 | export class ApiTool extends Tool { 23 | executor = ToolExecutor.API; 24 | 25 | @Property() 26 | openApiSchema!: string; 27 | 28 | @Property() 29 | apiKey?: string; 30 | 31 | constructor({ openApiSchema, apiKey, ...rest }: ApiToolInput) { 32 | super(rest); 33 | this.openApiSchema = openApiSchema; 34 | this.apiKey = apiKey; 35 | } 36 | } 37 | 38 | export type ApiToolInput = ToolInput & 39 | Pick & 40 | Partial>; 41 | -------------------------------------------------------------------------------- /src/administration/dtos/organization-user.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { OrganizationUserRole } from '../entities/constants'; 20 | 21 | export const organizationUserSchema = { 22 | type: 'object', 23 | required: ['object', 'id', 'name', 'email', 'role', 'added_at'], 24 | properties: { 25 | object: { const: 'organization.user' }, 26 | id: { type: 'string' }, 27 | name: { type: 'string' }, 28 | email: { type: 'string' }, 29 | role: { type: 'string', enum: Object.values(OrganizationUserRole) }, 30 | added_at: { type: 'number' } 31 | } 32 | } as const satisfies JSONSchema; 33 | export type OrganizationUser = FromSchema; 34 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit/runtime/Nlp/DataStreamSourceGenerationTrainRecordJsonData.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_runtime_Nlp.proto 18 | 19 | import type { GenerationTrainRecord as _caikit_data_model_caikit_nlp_GenerationTrainRecord, GenerationTrainRecord__Output as _caikit_data_model_caikit_nlp_GenerationTrainRecord__Output } from '../../../caikit_data_model/caikit_nlp/GenerationTrainRecord'; 20 | 21 | export interface DataStreamSourceGenerationTrainRecordJsonData { 22 | 'data'?: (_caikit_data_model_caikit_nlp_GenerationTrainRecord)[]; 23 | } 24 | 25 | export interface DataStreamSourceGenerationTrainRecordJsonData__Output { 26 | 'data': (_caikit_data_model_caikit_nlp_GenerationTrainRecord__Output)[]; 27 | } 28 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/caikit_nlp/RerankScore.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_caikit_nlp.proto 18 | 19 | import type { Struct as _google_protobuf_Struct, Struct__Output as _google_protobuf_Struct__Output } from '../../google/protobuf/Struct'; 20 | import type { Long } from '@grpc/proto-loader'; 21 | 22 | export interface RerankScore { 23 | 'document'?: (_google_protobuf_Struct | null); 24 | 'index'?: (number | string | Long); 25 | 'score'?: (number | string); 26 | 'text'?: (string); 27 | } 28 | 29 | export interface RerankScore__Output { 30 | 'document': (_google_protobuf_Struct__Output | null); 31 | 'index': (number); 32 | 'score': (number); 33 | 'text': (string); 34 | } 35 | -------------------------------------------------------------------------------- /src/ui/ui.module.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FastifyPluginAsyncJsonSchemaToTs } from '@fastify/type-provider-json-schema-to-ts'; 18 | import { StatusCodes } from 'http-status-codes'; 19 | 20 | import { lastAssistants } from './ui.service.js'; 21 | import { lastAssistantsSchema } from './dtos/last_assistant.dto.js'; 22 | 23 | import { Tag } from '@/swagger.js'; 24 | 25 | export const uiModule: FastifyPluginAsyncJsonSchemaToTs = async (app) => { 26 | app.get( 27 | '/ui/last_assistants', 28 | { 29 | schema: { 30 | response: { [StatusCodes.OK]: lastAssistantsSchema }, 31 | tags: [Tag.BEE_API] 32 | }, 33 | preHandler: app.auth() 34 | }, 35 | async () => { 36 | return lastAssistants(); 37 | } 38 | ); 39 | }; 40 | -------------------------------------------------------------------------------- /src/vector-store-files/dtos/vector-store-file-read.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { vectorStoreFileSchema } from './vector-store-file.js'; 20 | 21 | export const vectorStoreFileReadParamsSchema = { 22 | type: 'object', 23 | additionalProperties: false, 24 | required: ['vector_store_id', 'file_id'], 25 | properties: { vector_store_id: { type: 'string' }, file_id: { type: 'string' } } 26 | } as const satisfies JSONSchema; 27 | 28 | export type VectorStoreFileReadParams = FromSchema; 29 | 30 | export const vectorStoreFileReadResponseSchema = vectorStoreFileSchema; 31 | export type VectorStoreFileReadResponse = FromSchema; 32 | -------------------------------------------------------------------------------- /src/administration/entities/organization.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Entity, ManyToOne, Property, Ref } from '@mikro-orm/core'; 18 | 19 | import { BaseEntity, BaseEntityInput } from '@/common/base.entity.js'; 20 | import { User } from '@/users/entities/user.entity'; 21 | 22 | @Entity() 23 | export class Organization extends BaseEntity { 24 | getIdPrefix(): string { 25 | return 'org'; 26 | } 27 | 28 | @Property() 29 | name: string; 30 | 31 | @ManyToOne() 32 | createdBy: Ref; 33 | 34 | constructor({ name, createdBy, ...rest }: OrganizationInput) { 35 | super(rest); 36 | this.name = name; 37 | this.createdBy = createdBy; 38 | } 39 | } 40 | 41 | export type OrganizationInput = BaseEntityInput & Pick; 42 | -------------------------------------------------------------------------------- /src/run-steps/entities/details/run-step-message-creation.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, ManyToOne, Ref } from '@mikro-orm/core'; 18 | 19 | import { RunStepDetails, RunStepDetailsType } from './run-step-details.entity.js'; 20 | 21 | import { Message } from '@/messages/message.entity.js'; 22 | 23 | @Embeddable({ discriminatorValue: RunStepDetailsType.MESSAGE_CREATION }) 24 | export class RunStepMessageCreation extends RunStepDetails { 25 | type = RunStepDetailsType.MESSAGE_CREATION; 26 | 27 | @ManyToOne() 28 | message!: Ref; 29 | 30 | constructor(input: RunStepMessageCreationInput) { 31 | super(); 32 | this.message = input.message; 33 | } 34 | } 35 | 36 | export type RunStepMessageCreationInput = Pick; 37 | -------------------------------------------------------------------------------- /src/runs/dtos/run-update.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { runParamsSchema, runSchema } from './run.js'; 20 | 21 | import { metadataSchema } from '@/schema.js'; 22 | 23 | export const runUpdateParamsSchema = runParamsSchema; 24 | export type RunUpdateParams = FromSchema; 25 | 26 | export const runUpdateBodySchema = { 27 | type: 'object', 28 | additionalProperties: false, 29 | properties: { 30 | metadata: metadataSchema 31 | } 32 | } as const satisfies JSONSchema; 33 | export type RunUpdateBody = FromSchema; 34 | 35 | export const runUpdateResponseSchema = runSchema; 36 | export type RunUpdateResponse = FromSchema; 37 | -------------------------------------------------------------------------------- /src/tools/entities/tool-calls/function-call.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, Property } from '@mikro-orm/core'; 18 | 19 | import { ToolType } from '../tool/tool.entity.js'; 20 | 21 | import { ToolCall } from './tool-call.entity.js'; 22 | 23 | @Embeddable({ discriminatorValue: ToolType.FUNCTION }) 24 | export class FunctionCall extends ToolCall { 25 | type = ToolType.FUNCTION; 26 | 27 | @Property() 28 | name!: string; 29 | 30 | @Property() 31 | arguments!: string; 32 | 33 | @Property() 34 | output?: string; 35 | 36 | constructor(input: FunctionCallInput) { 37 | super(); 38 | this.name = input.name; 39 | this.arguments = input.arguments; 40 | } 41 | } 42 | 43 | export type FunctionCallInput = Pick; 44 | -------------------------------------------------------------------------------- /vite.config.ts: -------------------------------------------------------------------------------- 1 | import { resolve } from 'node:path'; 2 | 3 | import tsConfigPaths from 'vite-tsconfig-paths'; 4 | import { externalizeDeps } from 'vite-plugin-externalize-deps'; 5 | import dtsPlugin from 'vite-plugin-dts'; 6 | import { glob } from 'glob'; 7 | 8 | const rootDir = 'src'; 9 | 10 | /** @type {import('vite').UserConfig} */ 11 | export default { 12 | test: { 13 | globals: true, 14 | passWithNoTests: true, 15 | testTimeout: 15_000, 16 | setupFiles: ['./tests/setup.ts'], 17 | deps: { 18 | interopDefault: false 19 | } 20 | }, 21 | build: { 22 | minify: false, 23 | target: 'node22.2.0', 24 | outDir: './dist', 25 | lib: { 26 | entry: [ 27 | ...glob.sync(resolve(__dirname, 'src/**/*.ts')), 28 | ...glob.sync(resolve(__dirname, 'workers/node/**/*.ts')), 29 | ...glob.sync(resolve(__dirname, 'seeders/**/*.ts')) 30 | ], 31 | formats: ['es'] 32 | }, 33 | rollupOptions: { 34 | output: { 35 | preserveModules: true, 36 | preserveModulesRoot: rootDir 37 | } 38 | } 39 | }, 40 | resolve: { 41 | alias: { 42 | '@/': resolve(resolve(__dirname), rootDir), 43 | '@tests/': resolve(resolve(__dirname), 'tests') 44 | } 45 | }, 46 | plugins: [ 47 | tsConfigPaths(), 48 | dtsPlugin({ entryRoot: rootDir, pathsToAliases: true }), 49 | externalizeDeps() 50 | ] 51 | }; 52 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/caikit_nlp/TuningConfig.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_caikit_nlp.proto 18 | 19 | import type { Long } from '@grpc/proto-loader'; 20 | 21 | export interface TuningConfig { 22 | 'num_virtual_tokens'?: (number | string | Long); 23 | 'prompt_tuning_init_text'?: (string); 24 | 'prompt_tuning_init_method'?: (string); 25 | 'prompt_tuning_init_source_model'?: (string); 26 | 'output_model_types'?: (string)[]; 27 | } 28 | 29 | export interface TuningConfig__Output { 30 | 'num_virtual_tokens': (number); 31 | 'prompt_tuning_init_text': (string); 32 | 'prompt_tuning_init_method': (string); 33 | 'prompt_tuning_init_source_model': (string); 34 | 'output_model_types': (string)[]; 35 | } 36 | -------------------------------------------------------------------------------- /src/files/entities/extractions/unstructured-api-extraction.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, Property } from '@mikro-orm/core'; 18 | 19 | import { Extraction } from './extraction.entity'; 20 | 21 | import { ExtractionBackend } from '@/files/extraction/constants'; 22 | 23 | @Embeddable({ discriminatorValue: ExtractionBackend.UNSTRUCTURED_API }) 24 | export class UnstructuredAPIExtraction extends Extraction { 25 | backend = ExtractionBackend.UNSTRUCTURED_API; 26 | 27 | @Property() 28 | storageId?: string; 29 | 30 | constructor({ storageId, ...rest }: UnstructuredAPIExtractionInput) { 31 | super(rest); 32 | this.storageId = storageId; 33 | } 34 | } 35 | 36 | export type UnstructuredAPIExtractionInput = Pick; 37 | -------------------------------------------------------------------------------- /src/utils/streams.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Transform, TransformCallback, TransformOptions, Readable } from 'node:stream'; 18 | import { Hash } from 'node:crypto'; 19 | 20 | export class PassthroughHash extends Transform { 21 | public readonly hash: Hash; 22 | 23 | constructor({ hash, ...opts }: { hash: Hash } & TransformOptions) { 24 | super(opts); 25 | this.hash = hash; 26 | } 27 | 28 | _transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback) { 29 | this.hash.update(chunk); 30 | callback(null, chunk); 31 | } 32 | } 33 | 34 | export async function toBuffer(readable: Buffer | Readable) { 35 | const chunks: any[] = []; 36 | for await (const chunk of readable) { 37 | chunks.push(chunk); 38 | } 39 | return Buffer.concat(chunks); 40 | } 41 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/nlp/TokenClassificationResult.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_nlp.proto 18 | 19 | import type { Long } from '@grpc/proto-loader'; 20 | 21 | export interface TokenClassificationResult { 22 | 'start'?: (number | string | Long); 23 | 'end'?: (number | string | Long); 24 | 'word'?: (string); 25 | 'entity'?: (string); 26 | 'entity_group'?: (string); 27 | 'score'?: (number | string); 28 | 'token_count'?: (number | string | Long); 29 | } 30 | 31 | export interface TokenClassificationResult__Output { 32 | 'start': (number); 33 | 'end': (number); 34 | 'word': (string); 35 | 'entity': (string); 36 | 'entity_group': (string); 37 | 'score': (number); 38 | 'token_count': (number); 39 | } 40 | -------------------------------------------------------------------------------- /src/tools/entities/tool-resources/tool-resource.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, Enum } from '@mikro-orm/core'; 18 | 19 | import { ToolType } from '../tool/tool.entity.js'; 20 | 21 | import { CodeInterpreterResource } from './code-interpreter-resource.entity.js'; 22 | import { FileSearchResource } from './file-search-resources.entity.js'; 23 | import { UserResource } from './user-resource.entity.js'; 24 | import { SystemResource } from './system-resource.entity.js'; 25 | 26 | @Embeddable({ abstract: true, discriminatorColumn: 'type' }) 27 | export abstract class ToolResource { 28 | @Enum(() => ToolType) 29 | type!: ToolType; 30 | } 31 | 32 | export type AnyToolResource = 33 | | CodeInterpreterResource 34 | | FileSearchResource 35 | | SystemResource 36 | | UserResource; 37 | -------------------------------------------------------------------------------- /src/utils/crypto/decrypt.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { createDecipheriv } from 'crypto'; 18 | 19 | import { 20 | CRYPTO_ALGORITHM, 21 | CRYPTO_INPUT_ENCODING, 22 | CRYPTO_IV_LENGTH, 23 | CRYPTO_OUTPUT_ENCODING 24 | } from './config.js'; 25 | 26 | import { CRYPTO_CIPHER_KEY } from '@/config.js'; 27 | 28 | const decrypt = (input: string) => { 29 | const encrypted = Buffer.from(input, CRYPTO_OUTPUT_ENCODING); 30 | const iv = encrypted.subarray(0, CRYPTO_IV_LENGTH); 31 | const text = encrypted.subarray(CRYPTO_IV_LENGTH); 32 | 33 | const decipher = createDecipheriv(CRYPTO_ALGORITHM, CRYPTO_CIPHER_KEY, iv); 34 | const decrypted = Buffer.concat([decipher.update(text), decipher.final()]); 35 | return decrypted.toString(CRYPTO_INPUT_ENCODING); 36 | }; 37 | 38 | export default decrypt; 39 | -------------------------------------------------------------------------------- /src/runs/entities/requiredAction.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, Enum, Property } from '@mikro-orm/core'; 18 | 19 | import { RequiredToolApprove } from './requiredToolApprove.entity'; 20 | import { RequiredToolOutput } from './requiredToolOutput.entity'; 21 | 22 | import { generatePrefixedObjectId } from '@/utils/id'; 23 | 24 | export enum RequiredActionType { 25 | OUTPUT = 'output', 26 | APPROVE = 'approve' 27 | } 28 | 29 | @Embeddable({ abstract: true, discriminatorColumn: 'type' }) 30 | export abstract class RequiredAction { 31 | @Property({ fieldName: '_id' }) 32 | id = generatePrefixedObjectId('action'); 33 | 34 | @Enum(() => RequiredActionType) 35 | type!: RequiredActionType; 36 | } 37 | 38 | export type AnyRequiredAction = RequiredToolApprove | RequiredToolOutput; 39 | -------------------------------------------------------------------------------- /src/runs/jobs/runs-cleanup.queue.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { RequestContext } from '@mikro-orm/core'; 18 | 19 | import { Run, RunStatus } from '../entities/run.entity.js'; 20 | 21 | import { ORM } from '@/database.js'; 22 | import { createQueue } from '@/jobs/bullmq.js'; 23 | import { QueueName } from '@/jobs/constants.js'; 24 | 25 | async function jobHandler() { 26 | return RequestContext.create(ORM.em, async () => { 27 | await ORM.em.getRepository(Run).nativeUpdate( 28 | { 29 | expiresAt: { $lt: new Date() } 30 | }, 31 | { status: RunStatus.EXPIRED } 32 | ); 33 | }); 34 | } 35 | 36 | export const { queue } = createQueue({ 37 | name: QueueName.RUNS_CLEANUP, 38 | jobHandler, 39 | jobsOptions: { attempts: 1 }, 40 | workerOptions: { concurrency: 1 } 41 | }); 42 | -------------------------------------------------------------------------------- /src/vector-store-files/execution/client.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { z } from 'zod'; 18 | 19 | import { MilvusVectorStore } from './vector_db/adapters/milvus.js'; 20 | import { VectorDbDocumentSchema } from './vector_db/types.js'; 21 | import { VectorStoreClient } from './vector_db/adapters/interface.js'; 22 | 23 | import { VECTOR_STORE_EMBEDDING_MODEL } from '@/vector-stores/constants.js'; 24 | 25 | export const DocumentSchema = VectorDbDocumentSchema.extend({}); 26 | export type Document = z.infer; 27 | export type DocumentType = z.ZodType; 28 | 29 | export function getVectorStoreClient(): VectorStoreClient { 30 | return new MilvusVectorStore({ 31 | modelName: VECTOR_STORE_EMBEDDING_MODEL, 32 | documentSchema: DocumentSchema 33 | }); 34 | } 35 | -------------------------------------------------------------------------------- /src/tools/entities/tool-usages/system-usage.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, Enum, Property } from '@mikro-orm/core'; 18 | 19 | import { ToolType } from '../tool/tool.entity.js'; 20 | import { SystemTools } from '../tool-calls/system-call.entity.js'; 21 | 22 | import { ToolUsage } from './tool-usage.entity.js'; 23 | 24 | @Embeddable({ discriminatorValue: ToolType.SYSTEM }) 25 | export class SystemUsage extends ToolUsage { 26 | type = ToolType.SYSTEM; 27 | 28 | @Enum() 29 | toolId!: SystemTools; 30 | 31 | @Property({ type: 'json' }) 32 | config?: any; 33 | 34 | constructor({ toolId, config }: SystemUsageInput) { 35 | super(); 36 | this.toolId = toolId; 37 | this.config = config; 38 | } 39 | } 40 | 41 | export type SystemUsageInput = Pick; 42 | -------------------------------------------------------------------------------- /src/vector-store-files/entities/chunking-strategy/chunking-strategy.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, Enum } from '@mikro-orm/core'; 18 | 19 | import { StaticChunkingStrategy } from './static-chunking-strategy.entity.js'; 20 | import { AutoChunkingStrategy } from './auto-chunking.strategy.entity.js'; 21 | 22 | export const ChunkingStrategyType = { 23 | STATIC: 'static', 24 | AUTO: 'auto' 25 | } as const; 26 | export type ChunkingStrategyType = (typeof ChunkingStrategyType)[keyof typeof ChunkingStrategyType]; 27 | 28 | @Embeddable({ abstract: true, discriminatorColumn: 'type' }) 29 | export abstract class ChunkingStrategy { 30 | @Enum(() => ChunkingStrategyType) 31 | type!: ChunkingStrategyType; 32 | } 33 | 34 | export type AnyChunkingStrategy = AutoChunkingStrategy | StaticChunkingStrategy; 35 | -------------------------------------------------------------------------------- /src/administration/dtos/api-key-update.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { apiKeyReadParamsSchema } from './api-key-read'; 20 | import { apiKeySchema } from './api-key'; 21 | 22 | export const apiKeyUpdateParamsSchema = apiKeyReadParamsSchema; 23 | export type ApiKeyUpdateParams = FromSchema; 24 | 25 | export const apiKeyUpdateBodySchema = { 26 | type: 'object', 27 | additionalProperties: false, 28 | properties: { 29 | name: { type: 'string' } 30 | } 31 | } as const satisfies JSONSchema; 32 | export type ApiKeyUpdateBody = FromSchema; 33 | 34 | export const apiKeyUpdateResponseSchema = apiKeySchema; 35 | export type ApiKeyUpdateResponse = FromSchema; 36 | -------------------------------------------------------------------------------- /src/administration/dtos/projects-list.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FromSchema, JSONSchema } from 'json-schema-to-ts'; 18 | 19 | import { projectSchema } from './project'; 20 | 21 | import { createPaginationQuerySchema, withPagination } from '@/schema.js'; 22 | 23 | export const projectsListQuerySchema = { 24 | type: 'object', 25 | additionalProperties: false, 26 | properties: { 27 | ...createPaginationQuerySchema().properties, 28 | include_archived: { 29 | type: 'boolean', 30 | default: false 31 | } 32 | } 33 | } as const satisfies JSONSchema; 34 | export type ProjectsListQuery = FromSchema; 35 | 36 | export const projectsListResponseSchema = withPagination(projectSchema); 37 | export type ProjectsListResponse = FromSchema; 38 | -------------------------------------------------------------------------------- /src/users/entities/user.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Entity, Property } from '@mikro-orm/core'; 18 | 19 | import { BaseEntity, BaseEntityInput } from '@/common/base.entity.js'; 20 | 21 | @Entity() 22 | export class User extends BaseEntity { 23 | getIdPrefix(): string { 24 | return 'user'; 25 | } 26 | 27 | @Property({ unique: true }) 28 | externalId: string; // Remove after all users have migrated 29 | 30 | @Property({ unique: true }) 31 | email?: string; 32 | 33 | @Property() 34 | name?: string; 35 | 36 | constructor({ externalId, email, name, ...rest }: UserInput) { 37 | super(rest); 38 | this.externalId = externalId; 39 | this.email = email; 40 | this.name = name; 41 | } 42 | } 43 | 44 | export type UserInput = BaseEntityInput & Pick; 45 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/types/caikit_data_model/nlp/TokenizationStreamResult.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Original file: src/grpc/protos/caikit_data_model_nlp.proto 18 | 19 | import type { 20 | Token as _caikit_data_model_nlp_Token, 21 | Token__Output as _caikit_data_model_nlp_Token__Output 22 | } from './Token'; 23 | import type { Long } from '@grpc/proto-loader'; 24 | 25 | export interface TokenizationStreamResult { 26 | results?: _caikit_data_model_nlp_Token[]; 27 | processed_index?: number | string | Long; 28 | start_index?: number | string | Long; 29 | token_count?: number | string | Long; 30 | } 31 | 32 | export interface TokenizationStreamResult__Output { 33 | results: _caikit_data_model_nlp_Token__Output[]; 34 | processed_index: number; 35 | start_index: number; 36 | token_count: number; 37 | } 38 | -------------------------------------------------------------------------------- /src/embedding/adapters/caikit/grpc/protos/caikit_runtime_info.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2024 IBM Corp. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | 16 | /*------------------------------------------------------------------------------ 17 | * AUTO GENERATED 18 | *----------------------------------------------------------------------------*/ 19 | 20 | syntax = "proto3"; 21 | package caikit.runtime.info; 22 | import "caikit_data_model_common.proto"; 23 | import "caikit_data_model_common_runtime.proto"; 24 | 25 | 26 | /*-- SERVICES ----------------------------------------------------------------*/ 27 | 28 | service InfoService { 29 | rpc GetRuntimeInfo(caikit_data_model.common.runtime.RuntimeInfoRequest) returns (caikit_data_model.common.runtime.RuntimeInfoResponse); 30 | rpc GetModelsInfo(caikit_data_model.common.runtime.ModelInfoRequest) returns (caikit_data_model.common.runtime.ModelInfoResponse); 31 | } 32 | -------------------------------------------------------------------------------- /src/tools/entities/tool-calls/user-call.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, ManyToOne, Property, Ref } from '@mikro-orm/core'; 18 | 19 | import { Tool, ToolType } from '../tool/tool.entity.js'; 20 | 21 | import { ToolCall } from './tool-call.entity.js'; 22 | 23 | @Embeddable({ discriminatorValue: ToolType.USER }) 24 | export class UserCall extends ToolCall { 25 | type = ToolType.USER; 26 | 27 | @ManyToOne() 28 | tool!: Ref; 29 | 30 | @Property() 31 | arguments?: string; 32 | 33 | @Property() 34 | output?: string; 35 | 36 | constructor({ tool, arguments: args, output }: UserCallInput) { 37 | super(); 38 | this.tool = tool; 39 | this.arguments = args; 40 | this.output = output; 41 | } 42 | } 43 | 44 | export type UserCallInput = Pick; 45 | -------------------------------------------------------------------------------- /src/tools/entities/tool-usages/tool-usage.entity.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { Embeddable, Enum } from '@mikro-orm/core'; 18 | 19 | import { ToolType } from '../tool/tool.entity.js'; 20 | 21 | import { FunctionUsage } from './function-usage.entity.js'; 22 | import { FileSearchUsage } from './file-search-usage.entity.js'; 23 | import { CodeInterpreterUsage } from './code-interpreter-usage.entity.js'; 24 | import { UserUsage } from './user-usage.entity.js'; 25 | import { SystemUsage } from './system-usage.entity.js'; 26 | 27 | @Embeddable({ abstract: true, discriminatorColumn: 'type' }) 28 | export abstract class ToolUsage { 29 | @Enum(() => ToolType) 30 | type!: ToolType; 31 | } 32 | 33 | export type AnyToolUsage = 34 | | CodeInterpreterUsage 35 | | FileSearchUsage 36 | | FunctionUsage 37 | | SystemUsage 38 | | UserUsage; 39 | -------------------------------------------------------------------------------- /src/runs/execution/constants.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | export const RUN_EXPIRATION_MILLISECONDS = 10 * 60 * 1000; 18 | export const STATUS_POLL_INTERVAL = 5 * 1000; 19 | 20 | export const LLMBackend = { 21 | OLLAMA: 'ollama', 22 | IBM_VLLM: 'ibm-vllm', 23 | OPENAI: 'openai', 24 | BAM: 'bam', 25 | WATSONX: 'watsonx' 26 | } as const; 27 | export type LLMBackend = (typeof LLMBackend)[keyof typeof LLMBackend]; 28 | 29 | export const CodeInterpreterStorageBackend = { 30 | S3: 's3', 31 | FILESYSTEM: 'filesystem' 32 | } as const; 33 | export type CodeInterpreterStorageBackend = (typeof LLMBackend)[keyof typeof LLMBackend]; 34 | 35 | export const SearchToolBackend = { 36 | GOOGLE: 'google', 37 | DUCK_DUCK_GO: 'duck-duck-go' 38 | } as const; 39 | export type SearchToolBackend = (typeof SearchToolBackend)[keyof typeof SearchToolBackend]; 40 | -------------------------------------------------------------------------------- /src/streaming/sse.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2024 IBM Corp. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { FastifyReply } from 'fastify'; 18 | 19 | import { Event } from './dtos/event.js'; 20 | 21 | export const init = (res: FastifyReply) => { 22 | res.hijack(); 23 | if (!res.raw.headersSent) { 24 | res.raw.setHeader('Content-Type', 'text/event-stream'); 25 | res.raw.setHeader('Connection', 'keep-alive'); 26 | res.raw.setHeader('Cache-Control', 'no-cache,no-transform'); 27 | res.raw.setHeader('x-no-compression', 1); 28 | } 29 | }; 30 | 31 | export const send = (res: FastifyReply, event: Event) => { 32 | res.raw.write(createMessage(event)); 33 | }; 34 | 35 | export const end = (res: FastifyReply) => { 36 | res.raw.end(); 37 | }; 38 | 39 | function createMessage(event: Event): string { 40 | return `event: ${event.event}\ndata: ${JSON.stringify(event.data)}\n\n`; 41 | } 42 | --------------------------------------------------------------------------------