ai-ml-integration
AI/ML APIs, LLM integration, and intelligent application patterns
What this skill does
# AI/ML Integration
## Overview
Integrating AI and machine learning capabilities into applications, including LLM APIs, embeddings, and RAG patterns.
---
## LLM Integration
### OpenAI API
```typescript
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
// Chat completion
async function chat(messages: Array<{ role: string; content: string }>) {
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages,
temperature: 0.7,
max_tokens: 1000,
});
return response.choices[0].message.content;
}
// Streaming response
async function* streamChat(prompt: string) {
const stream = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: prompt }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) {
yield content;
}
}
}
// Function calling
async function chatWithTools(message: string) {
const tools = [
{
type: 'function' as const,
function: {
name: 'get_weather',
description: 'Get current weather for a location',
parameters: {
type: 'object',
properties: {
location: { type: 'string', description: 'City name' },
unit: { type: 'string', enum: ['celsius', 'fahrenheit'] },
},
required: ['location'],
},
},
},
];
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: message }],
tools,
tool_choice: 'auto',
});
const toolCall = response.choices[0].message.tool_calls?.[0];
if (toolCall) {
const args = JSON.parse(toolCall.function.arguments);
// Execute the function
const result = await executeFunction(toolCall.function.name, args);
// Continue conversation with function result
return openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'user', content: message },
response.choices[0].message,
{
role: 'tool',
tool_call_id: toolCall.id,
content: JSON.stringify(result),
},
],
});
}
return response;
}
```
### Anthropic Claude
```typescript
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
// Basic message
async function chat(prompt: string) {
const message = await anthropic.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: [{ role: 'user', content: prompt }],
});
return message.content[0].type === 'text' ? message.content[0].text : '';
}
// With system prompt
async function chatWithSystem(system: string, prompt: string) {
const message = await anthropic.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
system,
messages: [{ role: 'user', content: prompt }],
});
return message.content[0];
}
// Streaming
async function* streamChat(prompt: string) {
const stream = anthropic.messages.stream({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: [{ role: 'user', content: prompt }],
});
for await (const event of stream) {
if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
yield event.delta.text;
}
}
}
// Tool use
async function chatWithTools(prompt: string) {
const response = await anthropic.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
tools: [
{
name: 'search_database',
description: 'Search the database for relevant information',
input_schema: {
type: 'object',
properties: {
query: { type: 'string', description: 'Search query' },
limit: { type: 'number', description: 'Max results' },
},
required: ['query'],
},
},
],
messages: [{ role: 'user', content: prompt }],
});
// Handle tool use blocks
for (const block of response.content) {
if (block.type === 'tool_use') {
const result = await executeSearch(block.input);
// Continue with tool result...
}
}
}
```
---
## Embeddings
### Text Embeddings
```typescript
import OpenAI from 'openai';
const openai = new OpenAI();
// Generate embeddings
async function getEmbedding(text: string): Promise<number[]> {
const response = await openai.embeddings.create({
model: 'text-embedding-3-small',
input: text,
});
return response.data[0].embedding;
}
// Batch embeddings
async function getEmbeddings(texts: string[]): Promise<number[][]> {
const response = await openai.embeddings.create({
model: 'text-embedding-3-small',
input: texts,
});
return response.data.map(d => d.embedding);
}
// Cosine similarity
function cosineSimilarity(a: number[], b: number[]): number {
let dotProduct = 0;
let normA = 0;
let normB = 0;
for (let i = 0; i < a.length; i++) {
dotProduct += a[i] * b[i];
normA += a[i] * a[i];
normB += b[i] * b[i];
}
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
}
// Find similar items
async function findSimilar(query: string, items: Array<{ text: string; embedding: number[] }>, topK = 5) {
const queryEmbedding = await getEmbedding(query);
const scored = items.map(item => ({
...item,
score: cosineSimilarity(queryEmbedding, item.embedding),
}));
return scored
.sort((a, b) => b.score - a.score)
.slice(0, topK);
}
```
### Vector Database (Pinecone)
```typescript
import { Pinecone } from '@pinecone-database/pinecone';
const pinecone = new Pinecone({
apiKey: process.env.PINECONE_API_KEY,
});
const index = pinecone.index('my-index');
// Upsert vectors
async function upsertDocuments(documents: Document[]) {
const vectors = await Promise.all(
documents.map(async (doc) => ({
id: doc.id,
values: await getEmbedding(doc.content),
metadata: {
title: doc.title,
source: doc.source,
content: doc.content.slice(0, 1000), // Store truncated for retrieval
},
}))
);
await index.upsert(vectors);
}
// Query similar vectors
async function querySimilar(query: string, topK = 5, filter?: object) {
const queryEmbedding = await getEmbedding(query);
const results = await index.query({
vector: queryEmbedding,
topK,
includeMetadata: true,
filter,
});
return results.matches.map(match => ({
id: match.id,
score: match.score,
...match.metadata,
}));
}
```
---
## RAG (Retrieval-Augmented Generation)
### Basic RAG Pipeline
```typescript
class RAGPipeline {
constructor(
private vectorStore: VectorStore,
private llm: LLM,
private embeddings: EmbeddingModel
) {}
async query(question: string): Promise<string> {
// 1. Retrieve relevant documents
const relevantDocs = await this.retrieve(question);
// 2. Build context
const context = this.buildContext(relevantDocs);
// 3. Generate response with context
return this.generate(question, context);
}
private async retrieve(query: string, topK = 5) {
const queryEmbedding = await this.embeddings.embed(query);
return this.vectorStore.similaritySearch(queryEmbedding, topK);
}
private buildContext(docs: Document[]): string {
return docs
.map((doc, i) => `[Document ${i + 1}]\n${doc.content}`)
.join('\n\n');
}
private async generate(question: string, context: string): Promise<string> {
const prompt = `Answer the question based on the following context.
If the answer is not in the context, say "I don't have enough information."
Context:
${context}
Question: ${question}
Answer:`;
return this.llm.generate(prompt);
}
}
```
### Advanced RAG with Reranking
```typescript
import { CohereClient } from 'cohere-ai';
const cohere = new CohereClient({ token: procRelated in AI Agents
skill-development
IncludedComprehensive meta-skill for creating, managing, validating, auditing, and distributing Claude Code skills and slash commands (unified in v2.1.3+). Provides skill templates, creation workflows, validation patterns, audit checklists, naming conventions, YAML frontmatter guidance, progressive disclosure examples, and best practices lookup. Use when creating new skills, validating existing skills, auditing skill quality, understanding skill architecture, needing skill templates, learning about YAML frontmatter requirements, progressive disclosure patterns, tool restrictions (allowed-tools), skill composition, skill naming conventions, troubleshooting skill activation issues, creating custom slash commands, configuring command frontmatter, using command arguments ($ARGUMENTS, $1, $2), bash execution in commands, file references in commands, command namespacing, plugin commands, MCP slash commands, Skill tool configuration, or deciding between skills vs slash commands. Delegates to docs-management skill for official documentation.
reprompter
IncludedTransform messy prompts into well-structured, effective prompts — single or multi-agent. Use when: "reprompt", "reprompt this", "clean up this prompt", "structure my prompt", rough text needing XML tags and best practices, "reprompter teams", "repromptception", "run with quality", "smart run", "smart agents", multi-agent tasks, audits, parallel work, anything going to agent teams. Don't use when: simple Q&A, pure chat, immediate execution-only tasks. See "Don't Use When" section for details. Outputs: Structured XML/Markdown prompt, quality score (before/after), optional team brief + per-agent sub-prompts, agent team output files. Success criteria: Single mode quality score ≥ 7/10; Repromptception per-agent prompt quality score 8+/10; all required sections present, actionable and specific.
adaptive-compaction
IncludedAdaptive add-on policy and recovery layer that decides WHEN to compact, prune, snapshot, or fork -- replacing fixed-percent auto-compaction across Claude Code, Codex, and MCP-capable hosts. Trigger on auto-compact timing or damage: "when should I compact", "is it safe to compact now or start a fresh session", "auto-compact fires too early/mid-task", "switching to an unrelated task but the window still has space", "context rot", "answers get worse the longer the session runs", "the agent forgot the plan or my decisions after it summarized", "add a layer on top that manages context without changing the agent", raising autoCompactWindow to give the policy room, or installing/tuning a cross-tool compaction policy or PreCompact hook -- even when "compaction" is never said but the problem is context-window pressure or post-summarization memory loss. Do NOT use to summarize a conversation, build RAG, write a summarization prompt (decides WHEN not HOW), or answer max-context-length trivia.
agent-skill-creator
IncludedCreate cross-platform agent skills from workflow descriptions. Activates when users ask to create an agent, automate a repetitive workflow, create a custom skill, or need advanced agent creation. Triggers on phrases like create agent for, automate workflow, create skill for, every day I have to, daily I need to, turn process into agent, need to automate, create a cross-platform skill, validate this skill, export this skill, migrate this skill. Supports single skills, multi-agent suites, transcript processing, template-based creation, interactive configuration, cross-platform export, and spec validation.
llm-wiki
IncludedUse when building or maintaining a persistent personal knowledge base (second brain) in Obsidian where an LLM incrementally ingests sources, updates entity/concept pages, maintains cross-references, and keeps a synthesis current. Triggers include "second brain", "Obsidian wiki", "personal knowledge management", "ingest this paper/article/book", "build a research wiki", "compound knowledge", "Memex", or whenever the user wants knowledge to accumulate across sessions instead of being re-derived by RAG on every query.
skill-master
IncludedAgent Skills authoring, evaluation, and optimization. Create, edit, validate, benchmark, and improve skills following the agentskills.io specification. Use when designing SKILL.md files, structuring skill folders (references, scripts, assets), ingesting external documentation into skills, running trigger evals, benchmarking skill quality, optimizing descriptions, or performing blind A/B comparisons. Keywords: agentskills.io, SKILL.md, skill authoring, eval, benchmark, trigger optimization.