Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

chore: Sample CAP transpilation #273

Merged
merged 32 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
769de60
fix: support ts compile
ZhongpinWang Nov 7, 2024
89c24f4
chore: remove cds type from sample-cap
ZhongpinWang Nov 7, 2024
0c1c45d
move back
ZhongpinWang Nov 7, 2024
ee84ee5
chore: rename build to compile
ZhongpinWang Nov 7, 2024
9365382
add back cds types...
ZhongpinWang Nov 7, 2024
36ae176
fix: deploy via manifest
ZhongpinWang Nov 8, 2024
c3bdb42
fix: authz issue
ZhongpinWang Nov 8, 2024
3c5ee9a
Merge branch 'main' into fix-sample-cap-build
ZhongpinWang Nov 8, 2024
0a7207d
fix: set Request from @sap/cds as any as this package many times has …
ZhongpinWang Nov 8, 2024
e3b02ef
fix: change gen to dist
ZhongpinWang Nov 8, 2024
faa77e9
chore: exclude sample cap from deps check
ZhongpinWang Nov 8, 2024
79d334c
docs: update README with remote deployment
ZhongpinWang Nov 8, 2024
844e464
fix: Changes from lint
Nov 8, 2024
71af75d
fix: dist
ZhongpinWang Nov 8, 2024
f0ab6ff
fix: Changes from lint
Nov 8, 2024
5eb7afe
Merge branch 'main' into fix-sample-cap-build
ZhongpinWang Nov 8, 2024
34e413d
fix: remove redundant reference in tsconfig
ZhongpinWang Nov 8, 2024
20233ad
chore: add @sap/cds back with pnpm rebuild
ZhongpinWang Nov 8, 2024
34868cb
chore: move rebuild
ZhongpinWang Nov 8, 2024
e154103
Merge branch 'main' into fix-sample-cap-build
ZhongpinWang Nov 8, 2024
9caf8b1
Merge branch 'main' into fix-sample-cap-build
ZhongpinWang Nov 11, 2024
47748b0
fix: flaky pipeline due to @cap-js/cds-types
ZhongpinWang Nov 11, 2024
55141fd
Merge branch 'main' into fix-sample-cap-build
ZhongpinWang Nov 12, 2024
23f9006
docs: add security warning for cds services
ZhongpinWang Nov 13, 2024
e8a2a1b
Merge branch 'main' into fix-sample-cap-build
ZhongpinWang Nov 13, 2024
aba9904
fix: Changes from lint
Nov 13, 2024
d226f96
fix: vale
ZhongpinWang Nov 13, 2024
db7e2a9
Merge branch 'main' into fix-sample-cap-build
ZhongpinWang Nov 14, 2024
9dfb9b2
Merge branch 'main' into fix-sample-cap-build
ZhongpinWang Nov 15, 2024
e8024c0
fix: add missing @sap/cds-dk as dev deps
ZhongpinWang Nov 15, 2024
5866d68
fix: Changes from lint
Nov 15, 2024
71b412c
Merge branch 'main' into fix-sample-cap-build
ZhongpinWang Nov 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"sample-code": "pnpm -F=@sap-ai-sdk/sample-code",
"sample-cap": "pnpm -F=@sap-ai-sdk/sample-cap",
"check:public-api": "pnpm -r check:public-api",
"check:deps": "pnpm -r -F !./tests/smoke-tests -F !./tests/schema-tests exec depcheck --ignores=\"nock,@jest/globals\" --quiet"
"check:deps": "pnpm -r -F !./tests/smoke-tests -F !./tests/schema-tests -F !./sample-cap exec depcheck --ignores=\"nock,@jest/globals\" --quiet"
Copy link
Contributor Author

@ZhongpinWang ZhongpinWang Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fyi, I removed deps check for this sample cap package because, e.g., xssec was added when using cds add xsuaa but our check complains that this package is not used. And also for dev deps @cap-js/cds-types.

},
"devDependencies": {
"@changesets/cli": "^2.27.9",
Expand All @@ -51,7 +51,6 @@
"@types/jsonwebtoken": "^9.0.7",
"@types/mock-fs": "^4.13.4",
"@types/node": "^20.17.6",
"@cap-js/cds-types": "^0.7.0",
"depcheck": "^1.4.7",
"eslint": "^9.14.0",
"glob": "^11.0.0",
Expand Down
651 changes: 559 additions & 92 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions sample-cap/.cdsrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"build": {
"target": "dist"
}
}
31 changes: 23 additions & 8 deletions sample-cap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
### Table of Contents

- [Local Deployment](#local-deployment)
- [Remote Deployment](#remote-deployment)
- [Usage](#usage)
- [`ai-api`](#ai-api)
- [Deployment API](#deployment-api)
- [`foundation-models`](#foundation-models)
- [Azure OpenAI Chat Completion](#azure-openai-chat-completion)
- [`orchestration`](#orchestration)
- [Chat Completions with Templating](#chat-completions-with-templating)

## Local Deployment

1. Build the application with `pnpm install`.
1. Install dependencies using `pnpm install`.

2. Login using `cf login -a API_ENDPOINT -o ORG -s SPACE`.

Expand All @@ -31,15 +29,32 @@
pnpm watch:hybrid
```

## Remote Deployment

> [!WARNING]

Check warning on line 34 in sample-cap/README.md

View workflow job for this annotation

GitHub Actions / grammar-check

[vale] reported by reviewdog 🐶 [SAP.Spacing] '!W' should have one space. Raw Output: {"message": "[SAP.Spacing] '!W' should have one space.", "location": {"path": "sample-cap/README.md", "range": {"start": {"line": 34, "column": 4}}}, "severity": "WARNING"}
> All CDS services are marked with `@requires: 'any'` and are publicly accessible in order to simplify the deployment process.
> Apply proper authentication mechanisms to avoid unauthorized access.

1. Install dependencies using `pnpm install`.
2. Transpile the CAP application using `pnpm build`.

3. Modify `services` and `routes` values in `manifest.yml`.

4. Login using `cf login -a API_ENDPOINT -o ORG -s SPACE`.

5. Deploy the application using `cf push`.

## Usage

For local deployment, set `SAMPLE_CAP_HOST` as `http://localhost:4004`. For remote deployment, set `SAMPLE_CAP_HOST` as the `route` value defined in `manifest.yaml`.

### `ai-api`

#### Deployment API

```bash
curl --request GET \
--url 'http://localhost:4004/odata/v4/ai-api/getDeployments'
curl --request POST \
--url $SAMPLE_CAP_HOST/odata/v4/ai-api/getDeployments
```

### `foundation-models`
Expand All @@ -48,7 +63,7 @@

```bash
curl --request POST \
--url 'http://localhost:4004/odata/v4/azure-openai/chatCompletion' \
--url $SAMPLE_CAP_HOST/odata/v4/azure-openai/chatCompletion \
--header 'Content-Type: application/json' \
--data '{
"messages": [
Expand All @@ -66,7 +81,7 @@

```bash
curl --request POST \
--url 'http://localhost:4004/odata/v4/orchestration/chatCompletion' \
--url $SAMPLE_CAP_HOST/odata/v4/orchestration/chatCompletion \
--header 'Content-Type: application/json' \
--data '{
"template": [
Expand Down
8 changes: 0 additions & 8 deletions sample-cap/jsconfig.json

This file was deleted.

11 changes: 11 additions & 0 deletions sample-cap/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
applications:
- name: ai-sdk-js-sample-cap
path: ./dist/srv
memory: 256M
buildpacks:
- nodejs_buildpack
services:
- default_aicore
- default_xsuaa
routes:
- route: 'ai-sdk-js-sample-cap.cfapps.eu12-001.hana.ondemand.com'
28 changes: 23 additions & 5 deletions sample-cap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,34 @@
"repository": "https://github.com/sap/ai-sdk-js",
"private": true,
"dependencies": {
"@sap-ai-sdk/ai-api": "workspace:^",
"@sap-ai-sdk/foundation-models": "workspace:^",
"@sap-ai-sdk/orchestration": "workspace:^",
"@sap/cds": "^8",
"express": "^4"
"@sap-ai-sdk/ai-api": "^1",
"@sap-ai-sdk/foundation-models": "^1",
"@sap-ai-sdk/orchestration": "^1",
"express": "^4",
"@sap/xssec": "^4",
"@sap/cds": "^8.4.1"
},
"devDependencies": {
"@sap/cds-dk": "^8.4.1"
},
"scripts": {
"prebuild": "rm -rf dist",
"build": "pnpm cds-build && pnpm compile && pnpm cleanup",
"cds-build": "cds build --production",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fyi, cds build only generates (copies) .ts file. We need to run tsc to transpile ts files into js and remove ts files afterwards.

"compile": "tsc",
"cleanup": "rm -f ./dist/srv/srv/**/*.ts ./dist/srv/package-lock.json",
"postbuild": "pushd ./dist/srv && npm i --package-lock-only && popd",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to remove the generated (copied) lock file and regenerate it in ./dist/srv using npm directly as for remote deployment, buildpack uses npm instead of pnpm. Otherwise, the lock file would only resolves into a bunch of symbolic links and a incomplete install when using buildpack.

"start": "npx cds-serve",
"watch": "cds-tsx watch",
"watch:hybrid": "cds-tsx watch --profile hybrid",
"lint": "eslint . && prettier . --config ../.prettierrc --ignore-path ../.prettierignore -c",
"lint:fix": "eslint . --fix && prettier . --config ../.prettierrc --ignore-path ../.prettierignore -w --log-level error"
},
"cds": {
"requires": {
"[production]": {
"auth": "xsuaa"
}
}
}
}
1 change: 1 addition & 0 deletions sample-cap/srv/ai-api/ai-api-service.cds
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@path: 'ai-api'
@requires: 'any'
Copy link
Contributor Author

@ZhongpinWang ZhongpinWang Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fyi, nodejs CAP by default authenticate all endpoints. Ideally, setting up an app-router to do authentication would be the right approach. For testing purpose, I just expose them all publicly.

STOP THE APPLICATION after testing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would document this in the readme, as this is related to security topics.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

service AiApiService {
action getDeployments() returns String;
}
1 change: 1 addition & 0 deletions sample-cap/srv/foundation-models/azure-openai-service.cds
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@path: 'azure-openai'
@requires: 'any'
service AzureOpenAiService {
action chatCompletion(messages : array of Message) returns String;
}
Expand Down
3 changes: 1 addition & 2 deletions sample-cap/srv/foundation-models/azure-openai-service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Request } from '@sap/cds';
import { AzureOpenAiChatClient } from '@sap-ai-sdk/foundation-models';

export default class AzureOpenAiService {
async chatCompletion(req: Request) {
async chatCompletion(req: any) {
ZhongpinWang marked this conversation as resolved.
Show resolved Hide resolved
const { messages } = req.data;
ZhongpinWang marked this conversation as resolved.
Show resolved Hide resolved
const response = await new AzureOpenAiChatClient('gpt-35-turbo').run({
messages
Expand Down
1 change: 1 addition & 0 deletions sample-cap/srv/orchestration/orchestration-service.cds
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@path: 'orchestration'
@requires: 'any'
service OrchestrationService {
action chatCompletion(template : array of Template, inputParams : array of InputParam) returns String;
}
Expand Down
3 changes: 1 addition & 2 deletions sample-cap/srv/orchestration/orchestration-service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Request } from '@sap/cds';
import { OrchestrationClient } from '@sap-ai-sdk/orchestration';

export default class OrchestrationService {
async chatCompletion(req: Request) {
async chatCompletion(req: any) {
const { template, inputParams } = req.data;
const llm = {
model_name: 'gpt-4-32k',
Expand Down
11 changes: 11 additions & 0 deletions sample-cap/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "./srv",
"outDir": "./dist/srv/srv",
"tsBuildInfoFile": "./dist/.tsbuildinfo",
"composite": true
},
"include": ["srv/**/*.ts"],
"exclude": ["dist/**/*", "**/*.test.ts", "node_modules/**/*"]
}
5 changes: 5 additions & 0 deletions sample-cap/xs-security.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scopes": [],
"attributes": [],
"role-templates": []
}
4 changes: 3 additions & 1 deletion styles/config/vocabularies/SAP/accept.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,6 @@ Istio
[Mm]egaclite

seldomly
lookups
lookups

CDS