Skip to content

Commit

Permalink
Setup movies example
Browse files Browse the repository at this point in the history
  • Loading branch information
fwang committed Jan 14, 2024
1 parent 2a471d5 commit c9d275a
Show file tree
Hide file tree
Showing 26 changed files with 1,597 additions and 398 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
88 changes: 88 additions & 0 deletions examples/playground/functions/vector-example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import fs from "fs/promises";
import { VectorClient } from "sst";
const client = VectorClient("MyVectorDB");

const tags = [
{ id: "tag1", text: "Not from Earth" },
{ id: "tag2", text: "Super rich genius" },
];
const movies = [
{
id: "movie1",
title: "Iron Man",
poster: "./iron-man.jpg",
summary:
"A billionaire industrialist and genius inventor, Tony Stark (Robert Downey Jr.), is conducting weapons tests overseas, but terrorists kidnap him to force him to build a devastating weapon. Instead, he builds an armored suit and upends his captors. Returning to America, Stark refines the suit and uses it to combat crime and terrorism.",
},
{
id: "movie2",
title: "Thor",
poster: "./thor.jpg",
summary:
"As the son of Odin (Anthony Hopkins), king of the Norse gods, Thor (Chris Hemsworth) will soon inherit the throne of Asgard from his aging father. However, on the day that he is to be crowned, Thor reacts with brutality when the gods' enemies, the Frost Giants, enter the palace in violation of their treaty. As punishment, Odin banishes Thor to Earth. While Loki (Tom Hiddleston), Thor's brother, plots mischief in Asgard, Thor, now stripped of his powers, faces his greatest threat.",
},
{
id: "movie3",
title: "Spider-Man: Homecoming",
poster: "./spider-man.jpg",
summary:
"Thrilled by his experience with the Avengers, young Peter Parker returns home to live with his Aunt May. Under the watchful eye of mentor Tony Stark, Parker starts to embrace his newfound identity as Spider-Man. He also tries to return to his normal daily routine -- distracted by thoughts of proving himself to be more than just a friendly neighborhood superhero. Peter must soon put his powers to the test when the evil Vulture emerges to threaten everything that he holds dear.",
},
{
id: "movie4",
title: "Captain America: The First Avenger",
poster: "./captain-america.jpg",
summary:
"It is 1941 and the world is in the throes of war. Steve Rogers (Chris Evans) wants to do his part and join America's armed forces, but the military rejects him because of his small stature. Finally, Steve gets his chance when he is accepted into an experimental program that turns him into a supersoldier called Captain America. Joining forces with Bucky Barnes (Sebastian Stan) and Peggy Carter (Hayley Atwell), Captain America leads the fight against the Nazi-backed HYDRA organization.",
},
{
id: "movie5",
title: "Black Widow",
poster: "./black-widow.jpg",
summary:
"Natasha Romanoff, aka Black Widow, confronts the darker parts of her ledger when a dangerous conspiracy with ties to her past arises. Pursued by a force that will stop at nothing to bring her down, Natasha must deal with her history as a spy, and the broken relationships left in her wake long before she became an Avenger.",
},
];

export const seeder = async () => {
// Ingest tags
for (const tag of tags) {
console.log("ingesting tag", tag.id);
await client.ingest({
text: tag.text,
metadata: { type: "tag", id: tag.id },
});
}

// Ingest movies
for (const movie of movies) {
console.log("ingesting movie", movie.id);
const imageBuffer = await fs.readFile(movie.poster);
const image = imageBuffer.toString("base64");

await client.ingest({
text: movie.summary,
image,
metadata: { type: "movie", id: movie.id },
});
}

return {
statusCode: 200,
body: "done",
};
};

export const app = async (event) => {
const prompt = event.queryStringParameters?.prompt;

const ret = await client.retrieve({
prompt,
metadata: { type: "movie" },
});

return {
statusCode: 200,
body: JSON.stringify(ret, null, 2),
};
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions examples/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "playground",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "index.js",
"scripts": {
"deploy": "go run ../../cmd/sst deploy",
Expand All @@ -11,7 +12,7 @@
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"sst": "^2.36.8"
"devDependencies": {
"sst": "3.0.1-7"
}
}
10 changes: 10 additions & 0 deletions examples/playground/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 31 additions & 4 deletions examples/playground/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,38 @@ export default $config({
};
},
async run() {
const worker = new sst.Worker("Page", {
handler: "worker/index.ts",
devUrl: true,
const vector = new sst.Vector("MyVectorDB", {
model: "amazon.titan-embed-image-v1",
});

return { url: worker.devUrl };
const seeder = new sst.Function("Seeder", {
handler: "functions/vector-example/index.seeder",
link: [vector],
copyFiles: [
{ from: "functions/vector-example/iron-man.jpg", to: "iron-man.jpg" },
{
from: "functions/vector-example/black-widow.jpg",
to: "black-widow.jpg",
},
{
from: "functions/vector-example/spider-man.jpg",
to: "spider-man.jpg",
},
{ from: "functions/vector-example/thor.jpg", to: "thor.jpg" },
{
from: "functions/vector-example/captain-america.jpg",
to: "captain-america.jpg",
},
],
url: true,
});

const app = new sst.Function("MyApp", {
handler: "functions/vector-example/index.app",
link: [vector],
url: true,
});

return { seeder: seeder.url, app: app.url };
},
});
25 changes: 15 additions & 10 deletions internal/components/src/components/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import {
output,
Output,
all,
interpolate,
} from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import { RandomId } from "@pulumi/random";
import { prefixName, hashNumberToString } from "./helpers/naming";
import { Component } from "./component";
import { AWSLinkable, Link, Linkable } from "./link";
import { FunctionPermissionArgs } from ".";

/**
* Properties to create a DNS validated certificate managed by AWS Certificate Manager.
Expand All @@ -31,7 +33,7 @@ export class Bucket extends Component implements Linkable, AWSLinkable {
constructor(
name: string,
args?: BucketArgs,
opts?: ComponentResourceOptions,
opts?: ComponentResourceOptions
) {
super("sst:sst:Bucket", name, args, opts);

Expand All @@ -46,15 +48,15 @@ export class Bucket extends Component implements Linkable, AWSLinkable {
bucket: randomId.dec.apply((dec) =>
prefixName(
name.toLowerCase(),
`-${hashNumberToString(parseInt(dec), 8)}`,
),
`-${hashNumberToString(parseInt(dec), 8)}`
)
),
forceDestroy: true,
...args?.nodes?.bucket,
},
{
parent,
},
}
);

output(blockPublicAccess).apply((blockPublicAccess) => {
Expand All @@ -69,7 +71,7 @@ export class Bucket extends Component implements Linkable, AWSLinkable {
ignorePublicAcls: true,
restrictPublicBuckets: true,
},
{ parent },
{ parent }
);
});

Expand Down Expand Up @@ -97,13 +99,16 @@ export class Bucket extends Component implements Linkable, AWSLinkable {
public getSSTLink(): Link {
return {
type: `{ bucketName: string }`,
value: all([this.name]).apply(([bucketName]) => ({
bucketName,
})),
value: {
bucketName: this.name,
},
};
}

public getSSTAWSPermissions(): string[] {
return ["s3:*"];
public getSSTAWSPermissions(): FunctionPermissionArgs {
return {
actions: ["s3:*"],
resources: [this.bucket.arn, interpolate`${this.bucket.arn}/*`],
};
}
}
Loading

0 comments on commit c9d275a

Please sign in to comment.