Skip to content

Commit

Permalink
docs(js): Improvements and small fixes. (#1894)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisraygill authored Feb 7, 2025
1 parent dd633c7 commit 85d5e9f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 51 deletions.
87 changes: 44 additions & 43 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Genkit is a framework designed to help you build AI-powered applications and fea

This documentation covers Genkit for Node.js. If you're a Go developer, see the [Genkit Go documentation](/docs/genkit-go/get-started-go).

You can deploy and run Genkit libraries anywhere Node.js is supported. It's designed to work with any generative AI model API or vector database. While we offer integrations for Firebase and Google Cloud, you can use Genkit independently of any Google services.
You can deploy and run Genkit libraries anywhere Node.js is supported. It's designed to work with many AI model providers and vector databases. While we offer integrations for Firebase and Google Cloud, you can use Genkit independently of any Google services.

[Get started](/docs/genkit/get-started){: .button}

Expand Down Expand Up @@ -65,19 +65,19 @@ See the following code samples for a concrete idea of how to use these capabilit

```javascript
import { genkit } from 'genkit';
import { googleAI, gemini15Flash } from '@genkit-ai/googleai';
import { googleAI, gemini20Flash } from '@genkit-ai/googleai';

const ai = genkit({
plugins: [googleAI()],
model: gemini15Flash, // Set default model
model: gemini20Flash, // Set default model
});

// Simple generation
const { text } = await ai.generate('Why is AI awesome?');
console.log(text);

// Streamed generation
const { stream } = await ai.generateStream('Tell me a story');
const { stream } = ai.generateStream('Tell me a story');
for await (const chunk of stream) {
console.log(chunk.text);
}
Expand All @@ -87,11 +87,11 @@ See the following code samples for a concrete idea of how to use these capabilit

```javascript
import { genkit, z } from 'genkit';
import { googleAI, gemini15Flash } from '@genkit-ai/googleai';
import { googleAI, gemini20Flash } from '@genkit-ai/googleai';

const ai = genkit({
plugins: [googleAI()],
model: gemini15Flash,
model: gemini20Flash,
});

const { output } = await ai.generate({
Expand All @@ -110,15 +110,15 @@ See the following code samples for a concrete idea of how to use these capabilit
console.log(output);
```

- {Function calling}
- {Tool calling}

```javascript
import { genkit, z } from 'genkit';
import { googleAI, gemini15Flash } from '@genkit-ai/googleai';
import { googleAI, gemini20Flash } from '@genkit-ai/googleai';

const ai = genkit({
plugins: [googleAI()],
model: gemini15Flash,
model: gemini20Flash,
});

// Define tool to get current weather for a given location
Expand All @@ -129,7 +129,9 @@ See the following code samples for a concrete idea of how to use these capabilit
inputSchema: z.object({
location: z.string().describe('The location to get the current weather for')
}),
outputSchema: z.string(),
outputSchema: z.object({
weatherReport: z.string().describe('Weather report of a particular location')
}),
},
async (input) => {
// Here, we would typically make an API call or database query. For this
Expand All @@ -149,12 +151,12 @@ See the following code samples for a concrete idea of how to use these capabilit
- {Chat}

```javascript
import { genkit, z } from 'genkit';
import { googleAI, gemini15Flash } from '@genkit-ai/googleai';
import { genkit, z } from 'genkit/beta';
import { googleAI, gemini20Flash } from '@genkit-ai/googleai';

const ai = genkit({
plugins: [googleAI()],
model: gemini15Flash,
model: gemini20Flash,
});

const chat = ai.chat({ system: 'Talk like a pirate' });
Expand All @@ -169,52 +171,51 @@ See the following code samples for a concrete idea of how to use these capabilit
- {Agents}

```javascript
import { genkit, z } from 'genkit';
import { googleAI, gemini15Flash } from '@genkit-ai/googleai';
import { genkit, z } from 'genkit/beta';
import { googleAI, gemini20Flash } from '@genkit-ai/googleai';

const ai = genkit({
plugins: [googleAI()],
model: gemini15Flash,
model: gemini20Flash,
});

// Define prompts that represent specialist agents
const reservationAgent = ai.definePrompt(
{
name: 'reservationAgent',
description: 'Reservation Agent can help manage guest reservations',
tools: [reservationTool, reservationCancelationTool, reservationListTool],

},
`{% verbatim %}{{role "system"}}{% endverbatim %} Help guests make and manage reservations`
);
// Define tools for your agents to use
const reservationTool = ai.defineTool( ... );
const reservationCancelationTool = ai.defineTool( ... );
const reservationListTool = ai.defineTool( ... );

const menuInfoAgent = ...
const complaintAgent = ...
// Define prompts that represent specialist agents
const reservationAgent = ai.definePrompt({
name: 'reservationAgent',
description: 'Reservation Agent can help manage guest reservations',
tools: [reservationTool, reservationCancelationTool, reservationListTool],
system: `Help guests make and manage reservations`
});
const menuInfoAgent = ai.definePrompt( ... );
const complaintAgent = ai.definePrompt( ... );

// Define a triage agent that routes to the proper specialist agent
const triageAgent = ai.definePrompt(
{
name: 'triageAgent',
description: 'Triage Agent',
tools: [reservationAgent, menuInfoAgent, complaintAgent],
},
`{% verbatim %}{{role "system"}}{% endverbatim %} You are an AI customer service agent for Pavel's Cafe.
Greet the user and ask them how you can help. If appropriate, transfer to an
agent that can better handle the request. If you cannot help the customer with
the available tools, politely explain so.`
);
const triageAgent = ai.definePrompt({
name: 'triageAgent',
description: 'Triage Agent',
tools: [reservationAgent, menuInfoAgent, complaintAgent],
system: `You are an AI customer service agent for Pavel's Cafe.
Greet the user and ask them how you can help. If appropriate, transfer to an
agent that can better handle the request. If you cannot help the customer with
the available tools, politely explain so.`
});

// Create a chat to enable multi-turn agent interactions
// Create a chat to enable conversational agent interactions
const chat = ai.chat(triageAgent);

chat.send('I want a reservation at Pavel\'s Cafe for noon on Tuesday.' );
chat.send('I want a reservation at Pavel\'s Cafe for noon on Tuesday.');
```

- {Data retrieval}

```javascript
import { genkit } from 'genkit';
import { googleAI, gemini15Flash, textEmbedding004 } from '@genkit-ai/googleai';
import { googleAI, gemini20Flash, textEmbedding004 } from '@genkit-ai/googleai';
import { devLocalRetrieverRef } from '@genkit-ai/dev-local-vectorstore';

const ai = genkit({
Expand All @@ -227,7 +228,7 @@ See the following code samples for a concrete idea of how to use these capabilit
},
]),
],
model: gemini15Flash,
model: gemini20Flash,
});

// Reference to a local vector database storing Genkit documentation
Expand Down
4 changes: 2 additions & 2 deletions docs/migrating-from-0.9.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,10 @@ export const simpleFlow = ai.defineFlow(
},
async (input, { context }) => {
if (!context.auth) {
throw new Error('Authorization required.');
throw new UserFacingError("UNAUTHORIZED", "Authorization required.");
}
if (input.uid !== context.auth.uid) {
throw new Error('You may only summarize your own profile data.');
throw new UserFacingError("UNAUTHORIZED", "You may only summarize your own profile data.");
}
// Flow logic here...
}
Expand Down
9 changes: 3 additions & 6 deletions docs/nextjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ For example:
```ts
'use server';
import { gemini15Flash, googleAI } from "@genkit-ai/googleai";
import { gemini20Flash, googleAI } from "@genkit-ai/googleai";
import { genkit, z } from "genkit";
const ai = genkit({
plugins: [googleAI()],
model: gemini15Flash,
model: gemini20Flash,
});
export const menuSuggestionFlow = ai.defineFlow(
Expand All @@ -79,10 +79,7 @@ export const menuSuggestionFlow = ai.defineFlow(
outputSchema: z.string(),
},
async (restaurantTheme) => {
const { text } = await ai.generate({
model: gemini15Flash,
prompt: `Invent a menu item for a ${restaurantTheme} themed restaurant.`,
});
const { text } = await ai.generate('Invent a menu item for a ${restaurantTheme} themed restaurant.');
return text;
}
);
Expand Down

0 comments on commit 85d5e9f

Please sign in to comment.