Skip to content
Open
Changes from all commits
Commits
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
30 changes: 15 additions & 15 deletions js/testapps/express/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@ const ai = genkit({
],
});

const selectedModel = gemini15Flash;

export const jokeFlow = ai.defineFlow(
{ name: 'jokeFlow', inputSchema: z.string(), outputSchema: z.string() },
async (subject, { context, sendChunk }) => {
if (context!.auth!.username != 'Ali Baba') {
throw new UserFacingError('PERMISSION_DENIED', context!.auth!.username!);
if (context?.auth?.username && context.auth.username !== 'Ali Baba') {
throw new UserFacingError('PERMISSION_DENIED', context.auth.username);
}
return await ai.run('call-llm', async () => {
const llmResponse = await ai.generate({
prompt: `tell me long joke about ${subject}`,
model: gemini15Flash,
model: selectedModel,
config: {
temperature: 1,
},
Expand Down Expand Up @@ -96,18 +98,16 @@ const acls: Record<string, string> = {
jokeFlow: 'Ali Baba',
};

// curl http://localhost:5000/jokeFlow?stream=true -d '{"data": "banana"}' -H "content-type: application/json" -H "authorization: open sesame"
ai.flows.forEach((f) => {
app.post(
`/${f.name}`,
expressHandler(f, { contextProvider: auth(acls[f.name]) })
);
});
// curl http://localhost:8080/jokeFlow?stream=true -d '{"data": "banana"}' -H "content-type: application/json" -H "authorization: open sesame"
app.post(
'/jokeFlow',
expressHandler(jokeFlow, { contextProvider: auth(acls['jokeFlow']) })
);

// curl http://localhost:5000/jokeHandler?stream=true -d '{"data": "banana"}' -H "content-type: application/json"
// curl http://localhost:8080/jokeHandler?stream=true -d '{"data": "banana"}' -H "content-type: application/json"
app.post('/jokeHandler', expressHandler(jokeFlow));

// curl http://localhost:5000/jokeWithFlow?subject=banana
// curl http://localhost:8080/jokeWithFlow?subject=banana
app.get('/jokeWithFlow', async (req: Request, res: Response) => {
const subject = req.query['subject']?.toString();
if (!subject) {
Expand All @@ -117,7 +117,7 @@ app.get('/jokeWithFlow', async (req: Request, res: Response) => {
res.send(await jokeFlow(subject));
});

// curl http://localhost:5000/jokeStream?subject=banana
// curl http://localhost:8080/jokeStream?subject=banana
app.get('/jokeStream', async (req: Request, res: Response) => {
const subject = req.query['subject']?.toString();
if (!subject) {
Expand All @@ -131,7 +131,7 @@ app.get('/jokeStream', async (req: Request, res: Response) => {
});
await ai.generate({
prompt: `Tell me a long joke about ${subject}`,
model: gemini15Flash,
model: selectedModel,
config: {
temperature: 1,
},
Expand All @@ -143,7 +143,7 @@ app.get('/jokeStream', async (req: Request, res: Response) => {
res.end();
});

const port = process.env.PORT || 5000;
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});