CS

Průvodce vývojem AI

This guide consolidates AI concepts, tools, and practical implementation techniques for developers. It covers foundational principles, generative AI, embeddings, RAG, local AI serving with Ollama, Python libraries, AI in Drupal, and machine learning fundamentals.


1. Generative AI Fundamentals

1.1 Vector Databases and Embeddings

Vector databases store high-dimensional vectors for efficient semantic searches. Unlike traditional keyword-based databases, they allow similarity searches using embeddings.

Key Concepts:

  • Embeddings: Numerical representations of text, images, or audio in vector space.
  • Semantic Search: Retrieves information based on meaning, not exact keywords.
  • Vector Similarity Metrics: Cosine similarity, Euclidean distance, Manhattan distance.

Recommended Resources:


1.2 Retrieval-Augmented Generation (RAG)

RAG combines large language models (LLMs) with external knowledge sources to provide accurate, contextually relevant responses.

Benefits:

  • Access to up-to-date information
  • Improved answer precision
  • Integration with vector databases for document retrieval

2. Ollama: Local AI Model Serving

Ollama allows running large language models locally, ensuring privacy and control.

2.1 Installation & Basic Commands

# Pull models
ollama pull llama3.2:1b
ollama run deepseek-r1:8b

# Start Ollama server
ollama serve

2.2 Embedding Models

Embeddings convert text into vectors for semantic search.

ModelSizeDescription
mxbai-embed-large334MHigh-quality general embeddings
nomic-embed-text137MBalanced performance and efficiency
all-minilm23MLightweight, low-resource model

Generating Embeddings:

REST API:

curl http://localhost:11434/api/embed -d '{
  "model": "mxbai-embed-large",
  "input": "Llamas are members of the camelid family"
}'

Python:

import ollama
result = ollama.embed(model='mxbai-embed-large', input='Llamas are members of the camelid family')

JavaScript:

import ollama from 'ollama';
const result = await ollama.embed({model: 'mxbai-embed-large', input: 'Llamas are members of the camelid family'});

2.3 Running Ollama with Docker Compose

Basic Setup:

version: '3.8'
services:
  ollama:
    image: ollama/ollama:latest
    ports:
      - "11434:11434"
    volumes:
      - ollama_data:/root/.ollama
    restart: unless-stopped
    environment:
      - OLLAMA_HOST=0.0.0.0

volumes:
  ollama_data:

GPU Support Example:

deploy:
  resources:
    reservations:
      devices:
        - driver: nvidia
          count: 1
          capabilities: [gpu]

Usage Commands:

docker-compose up -d         # Start Ollama
docker-compose exec ollama bash  # Access container
docker-compose down          # Stop Ollama

Pre-load Models:

#!/bin/bash
ollama pull llama3.2:1b
ollama pull deepseek-r1:8b
ollama pull mxbai-embed-large

3. Python AI Libraries

3.1 Transformers Library

from transformers import AutoModel, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModel.from_pretrained("bert-base-uncased")
inputs = tokenizer("Hello world", return_tensors="pt")
outputs = model(**inputs)

Transformers vs Sentence Transformers:

Featuretransformerssentence-transformers
FocusGeneral NLP tasksSentence/document embeddings
OutputToken embeddings, logitsSemantic embeddings ready for similarity search
Ease of useRequires manual pooling for sentencesencode() method for direct vectors
ModelsBERT, GPT, T5MiniLM, mpnet, etc.

3.2 Other Libraries

  • PyTorch: Neural networks
  • NumPy / Pandas: Data handling
  • Scikit-learn: ML algorithms
  • PEFT: Efficient fine-tuning of LLMs

4. AI Implementation Best Practices

  1. Choose models suited to task and hardware.
  2. Decide local (Ollama) vs cloud-based deployment based on privacy and scalability.
  3. Use efficient embeddings for semantic search.
  4. Combine retrieval systems with generative models (RAG) for knowledge-intensive tasks.
  5. Monitor resource usage and optimize performance.

5. AI in Drupal

AiAssistantApiRunner Workflow:

  1. Direct Answer: Respond in Markdown.
  2. Agent Delegation: Route complex queries to AI agents.
  3. Tool Integration: Use external services for data processing.
  4. Result Formatting: Present outputs in readable, context-preserving formats.

System Prompt Example:

$pre_prompt = $this->assistant->get('system_prompt');
"[instructions] ... You will answer directly or delegate to an agent. Format outputs in Markdown for readability."

6. AI Hunter: CV Creation Tool

Features:

  • Free CV creation
  • AI-driven optimization suggestions
  • Multiple import options: LinkedIn, PDF, Word, text, images
  • Referral program integration

Platform: Nebula.io


7. Model Context Protocol (MCP)

Session / Connection Methods: initialize, session/open, session/close, session/list
Tools Methods: tools/list, tools/call, tools/register, tools/unregister
Resource Methods: resources/list, resources/read, resources/write, resources/delete
Prompts / Messages: prompts/list, prompts/submit, prompts/feedback
Logging / Monitoring: logging/get, logging/subscribe, logging/clear
Server Info: server/info, capabilities/list


8. Machine Learning Fundamentals

Core Concepts:

  • Bias-Variance Tradeoff: Balance simplicity and flexibility.
  • Central Limit Theorem: Sample means converge to normal distribution.
  • Law of Large Numbers: Larger datasets provide more reliable estimates.

Supervised Learning:

  • Linear Regression: Predict continuous values.
  • R-squared: Model fit metric.
  • SVM: Optimal hyperplane classification.

Ensemble Methods:

  • Random Forest: Reduces overfitting, handles missing data, provides feature importance.

Unsupervised Learning:

  • Nearest Neighbors: Classifies based on similarity, sensitive to irrelevant features.

Neural Networks:

  • Layered structure, learns patterns via backpropagation, foundation for deep learning.

Data Visualization:

  • Seaborn: Advanced statistical plots, integrates with pandas.

Practical Applications:

  • Predictive modeling (e.g., elections)
  • Trend analysis from demographic or historical data