INTEGRITY Dokumentace

Volání funkcí

Volání funkcí umožňuje využít odpovědi velkých jazykových modelů (LLM) ke spouštění funkcí nebo ke komunikaci s externími API. Vývojář obvykle definuje sadu funkcí a požadované vstupní schéma pro každou z nich, čemuž říkáme tools. Model pak inteligentně rozpozná, kdy potřebuje provést volání nástroje, a vrátí výstup JSON, který uživatel musí předat jiné funkci nebo API.

Volání funkcí v podstatě umožňuje provádět akce pomocí LLM, a to spuštěním kódu nebo prostřednictvím dalších volání API.

Jak mohu použít volání funkcí?

Workers AI má vestavěné volání funkcí která vám umožňuje spouštět kód funkcí společně s voláními inference. Máme k dispozici balíček s názvem @cloudflare/ai-utils která to usnadňuje a kterou jsme zveřejnili jako open source na Github.

Dokumentaci k function calling podle průmyslového standardu najdete na Tradiční volání funkcí.

Přínos embedded function calling ukazuje příklad níže, který porovnává tradiční volání funkcí s embedded function calling. Díky embedded function calling jsme dokázali zkrátit kód ze 77 na 31 řádků.

# The ai-utils package enables embedded function calling
npm i @cloudflare/ai-utils
Příklad embedded function calling
import {
	createToolsFromOpenAPISpec,
	runWithTools,
	autoTrimTools,
} from "@cloudflare/ai-utils";

export default {
	async fetch(request, env, ctx) {
		const response = await runWithTools(
			env.AI,
			"@hf/nousresearch/hermes-2-pro-mistral-7b",
			{
				messages: [{ role: "user", content: "Who is Cloudflare on github?" }],
				tools: [
					// You can pass the OpenAPI spec link or contents directly
					...(await createToolsFromOpenAPISpec(
						"https://gist.githubusercontent.com/mchenco/fd8f20c8f06d50af40b94b0671273dc1/raw/f9d4b5cd5944cc32d6b34cad0406d96fd3acaca6/partial_api.github.com.json",
						{
							overrides: [
								{
									// for all requests on *.github.com, we'll need to add a User-Agent.
									matcher: ({ url, method }) => {
										return url.hostname === "api.github.com";
									},
									values: {
										headers: {
											"User-Agent":
												"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",
										},
									},
								},
							],
						},
					)),
				],
			},
		).then((response) => {
			return response;
		});

		return new Response(JSON.stringify(response));
	},
};
Příklad tradičního volání funkcí
export default {
	async fetch(request, env, ctx) {
		const response = await env.AI.run(
			"@hf/nousresearch/hermes-2-pro-mistral-7b",
			{
				messages: [{ role: "user", content: "Who is Cloudflare on GitHub?" }],
				tools: [
					{
						name: "getGithubUser",
						description:
							"Provides publicly available information about someone with a GitHub account.",
						parameters: {
							type: "object",
							properties: {
								username: {
									type: "string",
									description: "The handle for the GitHub user account.",
								},
							},
							required: ["username"],
						},
					},
				],
			},
		);

		const selected_tool = response.tool_calls[0];
		let res;

		if (selected_tool.name == "getGithubUser") {
			try {
				const username = selected_tool.arguments.username;
				const url = `https://api.github.com/users/${username}`;
				res = await fetch(url, {
					headers: {
						// Github API requires a User-Agent header
						"User-Agent":
							"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",
					},
				}).then((res) => res.json());
			} catch (error) {
				return error;
			}
		}

		const finalResponse = await env.AI.run(
			"@hf/nousresearch/hermes-2-pro-mistral-7b",
			{
				messages: [
					{
						role: "user",
						content: "Who is Cloudflare on GitHub?",
					},
					{
						role: "assistant",
						content: JSON.stringify(selected_tool),
					},
					{
						role: "tool",
						content: JSON.stringify(res),
					},
				],
				tools: [
					{
						name: "getGithubUser",
						description:
							"Provides publicly available information about someone with a GitHub account.",
						parameters: {
							type: "object",
							properties: {
								username: {
									type: "string",
									description: "The handle for the GitHub user account.",
								},
							},
							required: ["username"],
						},
					},
				],
			},
		);
		return new Response(JSON.stringify(finalResponse));
	},
};

Které modely podporují volání funkcí?

Existují open source modely doladěné pro volání funkcí. Při procházení naší katalog modelů, hledejte modely, které mají vedle sebe vlastnost function calling. Například @hf/nousresearch/hermes-2-pro-mistral-7b je doladěná varianta modelu Mistral 7B, kterou můžete použít pro volání funkcí.