INTEGRITY Dokumentace

Začínáme

Tento návod vás provede nastavením a nasazením prvního projektu Workers AI se vestavěným voláním funkcí. Použijete Workers, vazbu Workers AI, ai-utils package, a velký jazykový model (LLM) k nasazení vaší první aplikace využívající AI v globální síti Cloudflare, s vestavěným voláním funkcí.

1. Vytvořte projekt Worker s Workers AI

Postupujte podle Úvodní příručka pro Workers AI až do kroku 2.

2. Nainstalujte další npm balíček

Dále spusťte následující příkaz v repozitáři svého projektu a nainstalujte balíček nástrojů Worker AI.

npm i @cloudflare/ai-utils

3. Přidejte vestavěné volání funkcí Workers AI

Aktualizujte index.ts soubor v adresáři vaší aplikace s následujícím kódem:

index.js
import { runWithTools } from "@cloudflare/ai-utils";

export default {
	async fetch(request, env, ctx) {
		// Define function
		const sum = (args) => {
			const { a, b } = args;
			return Promise.resolve((a + b).toString());
		};
		// Run AI inference with function calling
		const response = await runWithTools(
			env.AI,
			// Model with function calling support
			"@hf/nousresearch/hermes-2-pro-mistral-7b",
			{
				// Messages
				messages: [
					{
						role: "user",
						content: "What the result of 123123123 + 10343030?",
					},
				],
				// Definition of available tools the AI model can leverage
				tools: [
					{
						name: "sum",
						description: "Sum up two numbers and returns the result",
						parameters: {
							type: "object",
							properties: {
								a: { type: "number", description: "the first number" },
								b: { type: "number", description: "the second number" },
							},
							required: ["a", "b"],
						},
						// reference to previously defined function
						function: sum,
					},
				],
			},
		);
		return new Response(JSON.stringify(response));
	},
};
index.ts
import { runWithTools } from "@cloudflare/ai-utils";

type Env = {
	AI: Ai;
};

export default {
	async fetch(request, env, ctx) {
		// Define function
		const sum = (args: { a: number; b: number }): Promise<string> => {
			const { a, b } = args;
			return Promise.resolve((a + b).toString());
		};
		// Run AI inference with function calling
		const response = await runWithTools(
			env.AI,
			// Model with function calling support
			"@hf/nousresearch/hermes-2-pro-mistral-7b",
			{
				// Messages
				messages: [
					{
						role: "user",
						content: "What the result of 123123123 + 10343030?",
					},
				],
				// Definition of available tools the AI model can leverage
				tools: [
					{
						name: "sum",
						description: "Sum up two numbers and returns the result",
						parameters: {
							type: "object",
							properties: {
								a: { type: "number", description: "the first number" },
								b: { type: "number", description: "the second number" },
							},
							required: ["a", "b"],
						},
						// reference to previously defined function
						function: sum,
					},
				],
			},
		);
		return new Response(JSON.stringify(response));
	},
} satisfies ExportedHandler<Env>;

Tento příklad importuje utils pomocí import { runWithTools} from "@cloudflare/ai-utils" a řídí se referencí API níže.

V tomto příkladu navíc definujeme a popisujeme seznam nástrojů, které může LLM využít k odpovědi na dotaz uživatele. Seznam v tomto případě obsahuje jen jeden nástroj, sum .

Abstrahováno pomocí runWithTools funkce proběhnou následující kroky:

sequenceDiagram
    participant Worker as Worker
    participant WorkersAI as Workers AI

    Worker->>+WorkersAI: Send messages, function calling prompt, and available tools
    WorkersAI->>+Worker: Select tools and arguments for function calling
    Worker-->>-Worker: Execute function
    Worker-->>+WorkersAI: Send messages, function calling prompt and function result
    WorkersAI-->>-Worker: Send response incorporating function output

ai-utils package je také open source na Github.

4. Lokální vývoj a nasazení

Postupujte podle kroků 4 a 5 v Úvodní příručka pro Workers AI pro místní vývoj a nasazení.

Referenční dokumentace API

Podrobnosti uvádí Referenční dokumentace API.