LLM Tool Calling i AI Agenci
Tool Calling (Function Calling), Vercel AI SDK (generateObject, streamUI), agentic loops, Structured Output (Zod) i RAG z pgvector.
6 narzędzi AI dla TypeScript — porównanie
Vercel AI SDK, OpenAI SDK, Anthropic SDK, LangChain.js, LlamaIndex i Mastra — typ, tool calling i zastosowanie.
| Narzędzie | Typ | Tool Calling | Kiedy |
|---|---|---|---|
| Vercel AI SDK | Full-stack AI framework | Tak (Zod schema) | Next.js AI apps, streaming, generative UI |
| OpenAI SDK | Direct API client | Tak (JSON Schema) | Direct OpenAI, custom setup |
| Anthropic SDK | Direct API client | Tak (tool_use) | Claude models, tool_use pattern |
| LangChain.js | LLM framework | Tak (Tools API) | Complex agents, chains, memory |
| LlamaIndex.TS | RAG framework | Tak (QueryEngine) | RAG, document indexing, data agents |
| Mastra | AI agent framework | Tak (native tools) | TypeScript-first AI agents, workflows |
Często zadawane pytania
Co to jest LLM Tool Calling (Function Calling) i jak działa?
Tool Calling (Function Calling): LLM może wywoływać narzędzia (funkcje). Model nie wykonuje kodu — zwraca intent wywołania. Twój kod wykonuje funkcję. Wynik wraca do LLM. LLM formułuje odpowiedź na bazie wyniku. Flow: 1. Wyślij prompt + lista dostępnych tools. 2. LLM decyduje które tool wywołać (lub nie). 3. LLM zwraca: tool_calls z argumentami. 4. Twój kod wykonuje funkcję. 5. Wynik wraca do LLM jako tool message. 6. LLM finalnie odpowiada użytkownikowi. OpenAI Tool Calling: tools: [{type: 'function', function: {name: 'get_weather', description: 'Get weather for a city', parameters: {type: 'object', properties: {city: {type: 'string', description: 'City name'}}, required: ['city']}}}]. model: 'gpt-4o'. Odpowiedź: message.tool_calls[0].function.name === 'get_weather'. Anthropic Tool Use: tools: [{name: 'get_weather', description: '...', input_schema: {type: 'object', properties: {...}}}]. Gemini: functionDeclarations. Parallel tool calling: wiele tool calls na raz. Szybsze niż sekwencyjne. LLM wywołuje kilka tools równolegle. Wszystkie wyniki wracają razem. Tool choice: toolChoice: 'auto' — LLM decyduje. toolChoice: 'required' — musi wywołać tool. toolChoice: {type: 'tool', name: 'specific_tool'} — wymuszony tool.
Vercel AI SDK — tool calling w Next.js i React?
Vercel AI SDK: npm install ai @ai-sdk/openai. generateText z tools: import {generateText, tool} from 'ai'. import {openai} from '@ai-sdk/openai'. import {z} from 'zod'. const {text, toolResults} = await generateText({model: openai('gpt-4o'), prompt: 'What is the weather in Warsaw?', tools: {getWeather: tool({description: 'Get current weather', parameters: z.object({city: z.string()}), execute: async ({city}) => {return await fetchWeather(city)}})}}). Zod dla parameter validation. execute — twój kod. Stream z tools: streamText. toTextStreamResponse() dla klienta. useChat hook: obsługuje tool calls automatycznie. onToolCall callback. ToolInvocation w messages. Generative UI (AI SDK 3): streamUI. React components zamiast tekstu. return jsx{WeatherComponent city={city} /}. LLM zwraca komponenty. maxSteps dla agentów: maxSteps: 10 — wielokrokowe wywołania. LLM -> tool -> LLM -> tool -> ... Agentic loops. stepFinishReason: 'tool-calls' vs 'stop'. Tools w Route Handler: api/chat/route.ts. POST handler. AI SDK server-side. Stream do klienta. Tools bez execute (client-side tools): brak execute w tool definition. LLM zwraca tool call. Klient obsługuje (np. nawigacja, UI update). LLM SDK types: CoreTool, ToolCallResult, StepResult. TypeScript dla wszystkich tool args i results.
AI Agenci z Tool Calling — agentic loops i multi-agent?
Agentic Loop: LLM + tools w pętli. Autonomiczne wykonanie zadań. Plan -> Execute -> Observe -> Repeat. Przykład agenta: const result = await generateText({model, system: 'You are a research assistant...', prompt: userQuery, tools: {search: tool({...}), scrapeUrl: tool({...}), summarize: tool({...})}, maxSteps: 10}). LLM planuje. Wywołuje search(). Analizuje wyniki. Scrape URL po potrzeby. Summarize. Odpowiada. ReAct pattern (Reasoning + Acting): LLM: THOUGHT: I need to find... ACTION: search(query). OBSERVATION: results. THOUGHT: I found... FINAL: answer. Explicit chain of thought. Anthropic Claude z tools: claude-3-5-sonnet. anthropic.messages.create z tools. tool_use content block w odpowiedzi. tool_result message z twoim wynikiem. Wielokrotne tury. Multi-agent systems: Orchestrator agent + Sub-agents. Handoff między agentami. OpenAI Swarm (beta). LangGraph (Python, JS). Microsoft AutoGen. Tool categories: Data tools: search, DB query, calculator. Action tools: email send, calendar create, API call. Code tools: code execute (sandbox), file read. Perception tools: web scrape, image analyze. Memory tools: vector store, document retrieval. Safety w tool calling: sanitize inputs. Nie pozwól LLM na arbitrary code exec. Confirmation steps dla destructive actions. Human in the loop dla ważnych akcji. Rate limiting tool calls. Token limits: każdy tool call = tokeny. Parallel = oszczędność tokenów. Mądry dobór tools per task.
Structured Output — JSON Schema i Zod z LLM?
Structured Output (OpenAI): response_format: {type: 'json_schema', json_schema: {name: 'product', schema: {...}, strict: true}}. LLM zwraca valid JSON. 100% zgodność ze schematem. Lepsze niż prompt engineering. Vercel AI SDK structured output: const {object} = await generateObject({model, schema: z.object({name: z.string(), price: z.number(), categories: z.array(z.string())}), prompt: 'Extract product info from: Nike Air Max 90...'}). Zod schema -> JSON Schema konwersja. Type-safe result. streamObject: strumieniowanie obiektów. Partial objects podczas streaming. useObject hook: client-side streaming object. Data extraction: scrape -> structured output. Form filling (auto). Classification: z.enum(['positive', 'negative', 'neutral']). document.sentiment. Batch processing: Promise.all na wiele dokumentów. Rate limit awareness. Content generation: schema dla blog post. {title, intro, sections: [{heading, content}], conclusion}. AI SDK Output format: generateText — tekst. generateObject — structured JSON. streamText — streaming tekst. streamObject — streaming JSON. streamUI — React components. Zod z OpenAI bez AI SDK: zodResponseFormat z openai package. const parser = zodResponseFormat(schema, 'name'). response_format: parser. const data = JSON.parse(completion.choices[0].message.content). TypeScript z LLM: pełne typowanie output. Nie any. Refine z Zod — walidacja po parse. Error handling: parse error retry. LLM hallucination rescue.
Retrieval-Augmented Generation (RAG) z TypeScript?
RAG: wzbogacanie LLM o własne dane. Embed dokumenty -> vector DB. Query -> embedding -> similar docs -> LLM z kontekstem. Chunking: podziel dokumenty na chunks. Size: 512-1024 tokenów. Overlap: 10-20% między chunks. RecursiveCharacterTextSplitter. Embedding: OpenAI text-embedding-3-small. Cohere embed-multilingual. Voyage (Anthropic recommended). Batch embedding dla efficiency. Vector Databases: pgvector (Postgres extension). Pinecone (managed, serverless). Qdrant (open source). Weaviate. Chroma (local development). Upstash Vector. pgvector z Drizzle: CREATE EXTENSION vector. column: vector(1536) — OpenAI dim. drizzle-orm/pg-core. cosineDistance() function. SELECT ... ORDER BY embedding. Retrieval: const embedding = await embed(query). const similar = await db.select().where(cosineDistance(embedding, queryEmbedding).lt(0.3)).limit(5). LLM call z retrieved context: const context = similar.map(d => d.content).join('nn'). const prompt = 'Based on context: ' + context + 'nnAnswer: ' + query. Reranking: Cohere rerank API. Lepszy wybór z retrieved. Baza rankingowa po retrieval. Hybrid search: BM25 (keyword) + vector. Full-text search + semantic. Reciprocal Rank Fusion. LlamaIndex.TS: npm install llamaindex. VectorStoreIndex.fromDocuments(). queryEngine.query(). Wbudowane chunking, embedding, retrieval. Vercel AI SDK RAG: embed() function. cosineSimilarity() helper. createDataStreamResponse() dla streaming.
Powiązane artykuły
Skontaktuj się z nami
Porozmawiajmy o Twoim projekcie. Bezpłatna wycena w ciągu 24 godzin.
Wyślij zapytanie
Telefon
+48 790 814 814
Pon-Pt: 9:00 - 18:00
adam@fotz.pl
Odpowiadamy w ciągu 24h
Adres
Plac Wolności 16
61-739 Poznań
Godziny pracy
Wolisz porozmawiać?
Zadzwoń teraz i porozmawiaj z naszym specjalistą o Twoim projekcie.
Zadzwoń teraz