Skip to content

Commit

Permalink
wip ai generation
Browse files Browse the repository at this point in the history
  • Loading branch information
nc-andreashaller committed Jan 22, 2024
1 parent 690a3b6 commit 292a044
Show file tree
Hide file tree
Showing 17 changed files with 2,986 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ai/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
out/
node_modules/
secrets/
dist/
4 changes: 4 additions & 0 deletions ai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# solari2.0-poc

node generate-techsheets.mjs
node node_modules/@adobe/helix-md2docx/src/cli/convert2docx.js out/tech-sheet.md
2,639 changes: 2,639 additions & 0 deletions ai/package-lock.json

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions ai/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "solari2.0-poc",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"compile": "tsc",
"run": "npm run compile && ./src/main/sh/run.sh"
},
"author": "[email protected]",
"dependencies": {
"@adobe/helix-md2docx": "^2.1.33",
"@types/node": "^20.10.5",
"google-auth-library": "^9.4.1",
"googleapis": "^129.0.0",
"openai": "^4.24.1",
"typescript": "^5.3.3"
}
}
17 changes: 17 additions & 0 deletions ai/src/main/sh/build-lambda-layer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
set -x
set -e

ROOT_DIR="$(pwd)"

OUTPUT_DIR="$(pwd)/dist"

LAYER_DIR=$OUTPUT_DIR/layers/lambda

mkdir -p $LAYER_DIR

cp -LR node_modules $LAYER_DIR

cd LAYER_DIR/..

zip -r layers.zip lambda
1 change: 1 addition & 0 deletions ai/src/main/sh/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node "dist/ts/$1.js"
54 changes: 54 additions & 0 deletions ai/src/main/terraform/apiGateway.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
resource "aws_api_gateway_rest_api" "api_gateway_rest_api" {
name = "api_gateway"
description = "Api Gateway for Lambda"
}

resource "aws_api_gateway_resource" "api_gateway" {
rest_api_id = "${aws_api_gateway_rest_api.api_gateway_rest_api.id}"
parent_id = "${aws_api_gateway_rest_api.api_gateway_rest_api.root_resource_id}"
path_part = "{proxy+}"
}

resource "aws_api_gateway_method" "api_gateway_method" {
rest_api_id = "${aws_api_gateway_rest_api.api_gateway_rest_api.id}"
resource_id = "${aws_api_gateway_resource.api_gateway.id}"
http_method = "ANY"
authorization = "NONE"
}

resource "aws_api_gateway_integration" "api_gateway_integration" {
rest_api_id = "${aws_api_gateway_rest_api.api_gateway_rest_api.id}"
resource_id = "${aws_api_gateway_method.api_gateway_method.resource_id}"
http_method = "${aws_api_gateway_method.api_gateway_method.http_method}"

integration_http_method = "POST"
type = "AWS_PROXY"
uri = "${aws_lambda_function.lambda.invoke_arn}"
}

resource "aws_api_gateway_method" "api_gateway_root_method" {
rest_api_id = "${aws_api_gateway_rest_api.api_gateway_rest_api.id}"
resource_id = "${aws_api_gateway_rest_api.api_gateway_rest_api.root_resource_id}"
http_method = "ANY"
authorization = "NONE"
}

resource "aws_api_gateway_integration" "api_gateway_root_integration" {
rest_api_id = "${aws_api_gateway_rest_api.api_gateway_rest_api.id}"
resource_id = "${aws_api_gateway_method.api_gateway_root_method.resource_id}"
http_method = "${aws_api_gateway_method.api_gateway_root_method.http_method}"

integration_http_method = "POST"
type = "AWS_PROXY"
uri = "${aws_lambda_function.lambda.invoke_arn}"
}

resource "aws_api_gateway_deployment" "api_gateway_deployment" {
depends_on = [
"aws_api_gateway_integration.api_gateway_integration",
"aws_api_gateway_integration.api_gateway_root_integration",
]

rest_api_id = "${aws_api_gateway_rest_api.api_gateway_rest_api.id}"
stage_name = "test"
}
53 changes: 53 additions & 0 deletions ai/src/main/terraform/iam.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
data "aws_iam_policy_document" "lambda_assume_role_document" {
version = "2012-10-17"

statement {
actions = ["sts:AssumeRole"]

principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}

effect = "Allow"
}
}

data "aws_iam_policy_document" "lambda_document" {
version = "2012-10-17"

statement {
effect = "Allow"

actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"cloudwatch:PutMetricData",
"kms:*",
]

resources = ["*"]
}
}

resource "aws_iam_policy" "lambda_policy" {
policy = "${data.aws_iam_policy_document.lambda_document.json}"
}

resource "aws_iam_role" "lambda_role" {
name = "${local.name}-role"
assume_role_policy = "${data.aws_iam_policy_document.lambda_assume_role_document.json}"

tags = "${local.tags}"
}

resource "aws_iam_policy_attachment" "lambda_attachment" {
name = "${local.name}-attachment"

roles = [
"${aws_iam_role.lambda_role.name}",
]

policy_arn = "${aws_iam_policy.lambda_policy.arn}"
}
55 changes: 55 additions & 0 deletions ai/src/main/terraform/lambdas/create-ship-page.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
data "archive_file" "function_archive" {
type = "zip"

source {
content = "${path.module}/../dist/ts"
filename = "ts"
}
source {
content = "${path.module}/../dist/secrets"
filename = "ts"
}
output_path = "${path.module}/../dist/function.zip"
}

data "archive_file" "function_archive" {
type = "zip"
source_dir = "${path.module}/../lambda/dist"
output_path = "${path.module}/../lambda/dist/function.zip"
}

resource "aws_lambda_layer_version" "dependency_layer" {
filename = "${path.module}/../dist/layers/layers.zip"
layer_name = "dependency_layer"
compatible_runtimes = ["nodejs18.x"]
source_code_hash = "${base64sha256(file("${path.module}/../dist/layers/layers.zip"))}"
}

resource "aws_lambda_function" "lambda" {
filename = "${data.archive_file.function_archive.output_path}"
function_name = "${local.name}"
role = "${aws_iam_role.lambda_role.arn}"
handler = "index.handler"

# Lambda Runtimes can be found here: https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html
runtime = "nodejs18.x"
timeout = "30"
memory_size = "${local.lambda_memory}"

environment {
variables = {
"EXAMPLE_SECRET" = "${var.example_secret}"
}
}
}

resource "aws_lambda_permission" "lambda" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.lambda.function_name}"
principal = "apigateway.amazonaws.com"

# The "/*/*" portion grants access from any method on any resource
# within the API Gateway REST API.
source_arn = "${aws_api_gateway_rest_api.api_gateway_rest_api.execution_arn}/*/*"
}
5 changes: 5 additions & 0 deletions ai/src/main/terraform/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
provider "aws" {
profile = "${var.aws_profile}"
region = "${var.aws_region}"
max_retries = 1
}
13 changes: 13 additions & 0 deletions ai/src/main/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
variable "aws_region" {
type = "string"
default = "eu-west-1"
}

variable "aws_profile" {
type = "string"
default = "default"
}

variable "example_secret" {
type = "string"
}
32 changes: 32 additions & 0 deletions ai/src/main/ts/generate-document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import OpenAI from 'openai';
import { writeFileSync } from 'fs';

import secrets from '../secrets/chatgpt-key.json';

const openai = new OpenAI({
apiKey: secrets.key
});

async function main(input: string) {
const chatCompletion = await openai.chat.completions.create({
messages: [
{ role: 'system', content: `You are a markdown generator. The documents your are generating are used as content for websites. The Content Management system which will use the markdown as content for websites is called Adobe Experience Manager, more specifically a feature within it called Edge Delivery Services.
The markdown documents can use headlines, ordered lists, unordered lists. Tables have a special semantic therefore they should not be used for typical content structuring but are reserved for layouting of the content.
Contend layouted with with tables is called a block. Each block has a name which is the first cell of the table and spans all columns forming a header row. The rest of the stucture of the blocks' table is defined for each block individually. There are the following blocks you can use to layout the content:
- "Hero": typically used at the very top of the page consisting of a background image, a headline and short teaser text to start the content presentation. Additional instances of this block might be used to introduce important sections of the document but should be used scarce. The structure of the block below the header row is one cell containing an image, a headline and a short description all to tease the following content.
- "Column": the column block can be used to show text beside an image or to show two options beside each other. Below the header row the column block has two columns each containing the content shown beside each other.
- "Cards": the cards block can be used to show 3 or more options in a gallery give the reader a good overview of the choices or attributes. Below the header row the cards block has a row with one column for each card. The content structure should be similar for each card.
` },
{ role: 'user', content: input },
],
model: 'gpt-3.5-turbo',
});
chatCompletion.choices.forEach((choice) => {
console.log(choice);
writeFileSync('out/eds-doc.md', choice.message.content || '');
});
}

main('Generate a markdown document for a space ship.');
12 changes: 12 additions & 0 deletions ai/src/main/ts/lambda/create-ship-page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { post } from '../publish';

const handler = (): LambdaResponse => {
console.log(post);
return {
statusCode: 200,
headers: {},
body: 'Hello World',
};
};

export { handler };
24 changes: 24 additions & 0 deletions ai/src/main/ts/publish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const apiBaseUrl = 'https://admin.hlx.page/'
const organisation = 'netcentric';
const repository = 'eds-solari';
const branch = 'main';

async function post(getEndpoint: (path: string) => string, path: string): Promise<void> {
const endpoint = getEndpoint(path);
const response = await fetch(endpoint, {
method: "POST",
});
if (!response.ok) {
throw new Error(`Error while calling ${endpoint}, status = ${response.status}`);
}
console.log(`${endpoint}: `, await response.json());
}

const getPreviewEndpoint = (path: string) => `${apiBaseUrl}preview/${organisation}/${repository}/${branch}/${path}`;
const getPublishEndpoint = (path: string) => `${apiBaseUrl}live/${organisation}/${repository}/${branch}/${path}`;


post(getPreviewEndpoint, 'test');
post(getPublishEndpoint, 'test');

export { post };
5 changes: 5 additions & 0 deletions ai/src/main/ts/types/LambdaResponse.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface LambdaResponse {
statusCode: number;
headers: object;
body: string;
}
32 changes: 32 additions & 0 deletions ai/src/main/ts/upload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { GoogleAuth } from 'google-auth-library';
import { google } from 'googleapis';
import { createReadStream } from 'fs';

import secrets from '../secrets/google-drive-key.json'

const driveShare = '0AJjRhcTZbIKsUk9PVA';
const driveFolder = '1OME3hUxm5zYtBsVlUfKal2O5qmdW3ehE';

async function upload(filePath: string): Promise<void> {
const auth = new GoogleAuth({credentials: secrets, scopes: ['https://www.googleapis.com/auth/drive']});
const service = google.drive({version: 'v3', auth});
const requestBody = {
name: 'photo.jpg',
parents: [driveFolder],
driveId: driveShare,
supportsAllDrives: true,
};
const media = {
mimeType: 'image/jpeg',
body: createReadStream(filePath),
};
const driveFile = await service.files.create({
requestBody,
media: media,
supportsAllDrives: true,
});
console.log('File Id:', driveFile.data.id);
console.log(driveFile.data.id);
}

upload('/Users/andreashaller/Pictures/samples/munich-1220908_1280.jpg');
16 changes: 16 additions & 0 deletions ai/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"sourceMap": true,
"allowSyntheticDefaultImports": true,
"target": "es2022",
"module": "CommonJS",
"moduleResolution": "node",
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"outDir": "dist"
},
"include": [
"src/main/ts/**/*"
]
}

0 comments on commit 292a044

Please sign in to comment.