Book
132 entries · types: chapter
- ... — 'ai_api', 'rest_framework', 'corsheaders', ] ``` Install the required packages: ```bash pip install djangorestframework django-cors-headers requests httpx ``` Configure CORS if your front-end run
- ... — 'corsheaders.middleware.CorsMiddleware',
- ... — ] CORS_ALLOWED_ORIGINS = [ "http://localhost:3000", ] ``` ## Building AI Models and Serializers Django's ORM lets you store prompts, responses, and metadata. Define a model for chat sessions and ind
- ... — ``` ## Personalization with AI Personalization tailors experiences to individual users based on preferences and behavior. Store profiles and inject context into prompts. ```python def build_persona
- ... — ``` ## Testing Use Django's test suite and the REPL environment for interactive debugging. ```python
- ... — 'ai_api', 'rest_framework', 'corsheaders', ] ``` Install the required packages: ```bash pip install djangorestframework django-cors-headers requests httpx ``` Configure CORS if your front-end run
- ... — 'corsheaders.middleware.CorsMiddleware',
- ... — ] CORS_ALLOWED_ORIGINS = [ "http://localhost:3000", ] ``` ## Building AI Models and Serializers Django's ORM lets you store prompts, responses, and metadata. Define a model for chat sessions and ind
- ... (previous validation) — user_msg = serializer.save(session=session) generate_response.delay(session_id, user_msg.content) return Response({'status': 'processing'}, status=status.HTTP_202_ACCEPTED) ``` Configure Celery in `
- ... (previous validation) — user_msg = serializer.save(session=session) generate_response.delay(session_id, user_msg.content) return Response({'status': 'processing'}, status=status.HTTP_202_ACCEPTED) ``` Configure Celery in `
- AI Workflow Automation — ## The Automation Imperative In the modern software landscape, the ability to automate repetitive, rule-based, or AI-driven tasks has become a critical differentiator for teams seeking to scale effic
- AI Workflow Automation — ## The Automation Imperative In the modern software landscape, the ability to automate repetitive, rule-based, or AI-driven tasks has become a critical differentiator for teams seeking to scale effic
- ai_api/models.py — from django.db import models class ChatSession(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateT
- ai_api/models.py — from django.db import models class ChatSession(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateT
- ai_api/serializers.py — from rest_framework import serializers from .models import ChatSession, ChatMessage class ChatMessageSerializer(serializers.ModelSerializer): class Meta: model = ChatMessage fields = ['id', 'role', 'c
- ai_api/serializers.py — from rest_framework import serializers from .models import ChatSession, ChatMessage class ChatMessageSerializer(serializers.ModelSerializer): class Meta: model = ChatMessage fields = ['id', 'role', 'c
- ai_api/services.py — import httpx import json OLLAMA_URL = "http://localhost:11434/api/generate" def ollama_generate(prompt: str) -> str: data = { "model": "llama3", "prompt": prompt, "stream": False, } with httpx.Client(
- ai_api/services.py — import httpx import json OLLAMA_URL = "http://localhost:11434/api/generate" def ollama_generate(prompt: str) -> str: data = { "model": "llama3", "prompt": prompt, "stream": False, } with httpx.Client(
- ai_api/tasks.py — from celery import shared_task from .services import ollama_generate from .models import ChatSession, ChatMessage @shared_task def generate_response(session_id: str, user_content: str): session = Chat
- ai_api/tasks.py — from celery import shared_task from .services import ollama_generate from .models import ChatSession, ChatMessage @shared_task def generate_response(session_id: str, user_content: str): session = Chat
- ai_api/tests.py — from django.test import TestCase from rest_framework.test import APIClient from .models import ChatSession, ChatMessage class ChatAPITestCase(TestCase): def setUp(self): self.client = APIClient() self
- ai_api/views.py — from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status from .serializers import ChatSessionSerializer, ChatMessageSerializer from .serv
- ai_api/views.py — from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status from .serializers import ChatSessionSerializer, ChatMessageSerializer from .serv
- ai_backend/urls.py — from django.contrib import admin from django.urls import path, include urlpatterns = [ path("admin/", admin.site.urls), path("api/", include("ai_core.urls")), ] ``` With Django running (`python mana
- ai_core/models.py — from transformers import pipeline
- ai_core/prompts.py — CLASSIFIER_SYSTEM_PROMPT = """ You are a classifier. Given a message, decide which category it belongs to. Categories: support, general, technical. Return a JSON object with a single key "category".
- ai_core/services.py — import json import re def classify_message(message: str) -> dict:
- ai_core/urls.py — from django.urls import path from . import views urlpatterns = [ path("api/classify/", views.classify_and_route, name="classify"), ] ``` ```python
- ai_core/views.py — import json from django.http import JsonResponse from django.views.decorators.csrf import csrf_exempt from django.utils.decorators import method_decorator from rest_framework.decorators import api_vie
- ai_core/views.py — import json from django.http import JsonResponse from django.views.decorators.csrf import csrf_exempt from django.utils.decorators import method_decorator from rest_framework.decorators import api_vie
- AI-Powered Study Systems — The rise of AI in education has fundamentally changed how we approach learning, and the most impactful systems are those that respect the learner's privacy and data sovereignty. In this chapter, we'll
- annotation_api.py — from fastapi import FastAPI, Depends, HTTPException from pydantic import BaseModel from jose import jwt, JWTError from datetime import datetime, timedelta import uuid import sqlite3 app = FastAPI()
- Assume a dummy document for graph hits — merged[name] = {"page_content": f"Graph node: {name}"} return list(merged.values())[:top_k * 2] ``` The hybrid retriever returns both text chunks and graph node identifiers. You can then pass these
- Automated Technical Blogging — The landscape of technical blogging has undergone a radical transformation in recent years. The traditional process of writing, editing, and publishing technical content was once a solitary endeavor,
- Automated Technical Blogging — The landscape of technical blogging has undergone a radical transformation in recent years. The traditional process of writing, editing, and publishing technical content was once a solitary endeavor,
- Automated Technical Blogging — The landscape of technical blogging has undergone a radical transformation in recent years. The traditional process of writing, editing, and publishing technical content was once a solitary endeavor,
- Automated Technical Blogging — The landscape of technical blogging has undergone a radical transformation in recent years. The traditional process of writing, editing, and publishing technical content was once a solitary endeavor,
- Automated Technical Blogging — The landscape of technical blogging has undergone a radical transformation in recent years. The traditional process of writing, editing, and publishing technical content was once a solitary endeavor,
- Automated Technical Blogging — The landscape of technical blogging has undergone a radical transformation in recent years. The traditional process of writing, editing, and publishing technical content was once a solitary endeavor,
- Automated Technical Bloging — The landscape of technical blogging has undergone a radical transformation in recent years. The traditional process of writing, editing, and publishing technical content was once a solitary endeavor,
- Build the prompt — system_prompt = persona["system_prompt"]
- Build the knowledge graph — graph = cola.Graph(dataset) graph.build()
- Call Ollama — response_content = ollama_generate(user_msg.content) assistant_msg = ChatMessage.objects.create(session=session, role='', content=response_content) return Response(ChatMessageSerializer([user_msg, ass
- Call Ollama — response_content = ollama_generate(user_msg.content) assistant_msg = ChatMessage.objects.create(session=session, role='', content=response_content) return Response(ChatMessageSerializer([user_msg, ass
- Chapter 10: Next.js AI Frontends — ## Introduction Developing an AI-powered front-end with Next.js brings together the best of server-side rendering, API routes, and modern React patterns. In this chapter, we’ll cover how to integrate
- Chapter 10: Next.js AI Frontends — ## Introduction Building interfaces that leverage artificial intelligence requires a blend of modern web frameworks, efficient data handling, and a keen eye for experience. In this chapter, we explo
- Chapter 13: Data Annotation and RLHF — ## Introduction High-quality training data is the backbone of any AI . In local-first architectures, where privacy, transparency, and control are paramount, the process of gathering, labeling, and re
- Chapter 2: The Local AI Technology Stack — ## Setting the Stage for Local AI [1] Building a local AI infrastructure requires careful selection of tools and models that balance performance, privacy, and cost. This chapter walks through the core
- Chapter 2: The Local AI Technology Stack — ## Setting the Stage for Local AI Building a local AI infrastructure requires careful selection of tools and models that balance performance, privacy, and cost. This chapter walks through the core com
- Chapter 4: Vector Databases and ChromaDB — ## Chapter Objectives - Set up ChromaDB for local vector storage - Implement efficient document chunking - Build a complete RAG pipeline In modern AI applications, the ability to retrieve relevant inf
- Chapter 9: Django for AI Applications — ## Introduction Django is a mature, full-stack web framework that excels at building robust APIs and web applications. Its batteries-included philosophy, powerful ORM, and mature ecosystem make it an
- Chapter 9: Django for AI Applications — ## Introduction Django is a mature, full-stack web framework that excels at building robust APIs and web applications. Its batteries-included philosophy, powerful ORM, and mature ecosystem make it an
- Chapter Objectives — - Understand the philosophy behind local-first AI - Learn why data sovereignty matters for developers - Explore the trade-offs between cloud and local AI ## The Philosophy of Local-First AI The philo
- Chapter Objectives — - Understand the philosophy behind local-first AI - Learn why data sovereignty matters for developers - Explore the trade-offs between cloud and local AI ## The Philosophy of Local-First AI The philo
- Chapter Objectives — - Design multi-agent collaboration patterns - Implement agent communication protocols - Use Microsoft AutoGen for agent orchestration ## Introduction to Multi-Agent Systems As local-first AI systems
- Chapter Objectives — - Design multi-agent collaboration patterns - Implement agent communication protocols - Use Microsoft AutoGen for agent orchestration ## Introduction to Multi-Agent Systems As local-first AI systems
- Combine prompt, history, and input — messages = [ {"role": "", "content": system_prompt}, {"role": "", "content": user_input} ]
- Construct a prompt that encodes persona keys — system_prompt = ( "You are an AI . " f"Tone: {persona['tone']}. " f"Expertise: {persona['expertise']}. " f"Length: {persona['length']}." ) messages = [ {"role": "", "content": system_prompt}, {"role":
- Convert Neo4j query result to a networkx graph — def neo4j_to_nx(session, cypher_query: str): rows = session.run(cypher_query) G = nx.MultiDiGraph() for r in rows: subj = r["s.name"] obj = r["o.name"] rel = r["r"] G.add_node(subj, label=subj) G.add_
- Default error — return MCPResponse( error={"code": -32601, "message": "Method not found"}, id=request.id ) ``` This example is intentionally simple. In a production server, you would add authentication, rate limiti
- Define a simple reward model (binary classification) — reward_model = AutoModelForSequenceClassification.from_pretrained( model_name, num_labels=2, ignore_mismatched_sizes=True ) training_args = TrainingArguments( output_dir="./rlhf_results", per_device_t
- Define MCP message schema — class MCPRequest(BaseModel): jsonrpc: str = "2.0" method: str params: dict = {} id: int = Field(..., gt=0) class MCPResponse(BaseModel): jsonrpc: str = "2.0" result: dict = {} error: Optional[dict] =
- Define the endpoint — ENDPOINT = "https://api.capacity.so/v1/inference"
- Define the endpoint — ENDPOINT = "https://api.capacity.so/v1/inference"
- Digital Resurrection and AI Ethics — ## The Ethics of AI-Powered Digital Resurrection ... <>user Write the chapter "Digital Resurrection and AI Ethics". Exploring the ethical frontier of AI-powered digital resurrection. No source arti
- Digital Resurrection and AI Ethics — ## The Ethics of AI-Powered Digital Resurrection The rapid advancement of artificial intelligence has unlocked unprecedented capabilities in modeling human behavior, speech, and thought. As these sys
- Digital Resurrection and AI Ethics — ## The Ethics of AI-Powered Digital Resurrection The rapid advancement of artificial intelligence has unlocked unprecedented capabilities in modeling human behavior, speech, and thought. As these sys
- Digital Resurrection and AI Ethics — ## The Ethics of AI-Powered Digital Resurrection The rapid advancement of artificial intelligence has unlocked unprecedented capabilities in modeling human behavior, speech, and thought. As these sys
- Dispatch to appropriate function — if tool == "get_blog_post": result = get_blog_post(**args) elif tool == "analyze_threat": result = analyze_threat(**args) else: raise ValueError(f"Unknown tool: {tool}") return {"success": True, "resu
- docker-compose.yml — version: "3.8" services: backend: build: context: . dockerfile: Dockerfile.backend ports: - "8000:8000" environment: - DJANGO_SETTINGS_MODULE=ai_backend.settings - DEBUG=1 volumes: - ./ai_backend:/app
- Dockerfile.backend — FROM python:3.11-slim WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . EXPOSE 8000 CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] ``` Create
- Dockerfile.frontend — FROM node:18-alpine WORKDIR /app COPY package.json package-lock.json ./ RUN npm ci COPY . . RUN npm run build EXPOSE 3000 CMD ["npm", "start"] ``` Now we need a `docker-compose.yml` to orchestrate t
- e.g., if category == "technical": invoke technical model — return JsonResponse({"category": category}) ``` To make this fully functional, we would add a model loader that initializes a local LLM (e.g., using `transformers` or `llama.cpp`) and a function tha
- Example usage — generated = generate_routes(endpoints) assert validate_routes(generated, endpoints) ``` This validation step ensures that the generated code matches the specification, catching any drift or errors e
- Example: Sequential pipeline with AutoGen — from autogen import Agent, ConversableAgent planner = ConversableAgent( name="Planner", llm_config={"model": "gpt-4"}, system_message="You are a planner. Produce a step-by-step plan." ) executor = Con
- Example: Sequential pipeline with AutoGen — from autogen import Agent, ConversableAgent planner = ConversableAgent( name="Planner", llm_config={"model": "gpt-4"}, system_message="You are a planner. Produce a step-by-step plan." ) executor = Con
- For simplicity, we assume the language model is called via a function — response = self.call_model(messages)
- Generate a blog post — post = generator.generate_post( topic="local-first AI", tone="informative" ) print(post) ``` This example shows how BlogGenerator can be integrated into your workflow. By running it locally, you ens
- Here we simulate a simple keyword‑based classifier — if "help" in user_message.lower(): category = "support" else: category = "general"
- Hypothetical capacity check — def get_capacity(model_name): resp = requests.get(f"https://api.capacity.so/v1/capacity/{model_name}") return resp.json()
- Hypothetical capacity check — def get_capacity(model_name): resp = requests.get(f"https://api.capacity.so/v1/capacity/{model_name}") return resp.json()
- Implementation omitted for brevity — return {"id": post_id, "title": "Sample Post"} ``` When the agent reasons that it needs to retrieve a post, it issues a function call: ```json { "tool": "get_blog_post", "arguments": {"post_id": 42
- In a real , store task in a database — return {"task_id": str(uuid.uuid4()), "data_id": task.data_id, "label": task.label} ``` This snippet demonstrates how to protect the annotation endpoint with JWT tokens and how to record a simple la
- In practice, this would invoke OpenAI, Anthropic, or another API — return "Response from model" ``` This implementation demonstrates the essential steps: classification, persona selection, history retrieval, and model invocation. In a production , the `call_model`
- Initialize Cola with a local LLM — client = cola.Client( model="llama3", base_url="http://localhost:8080" )
- Initialize the generator — generator = blog_generator.Generator( model="gpt-4", api_key="your_local_key" )
- Load a dataset — dataset = cola.Dataset.from_csv("data.csv")
- Load a pre-trained question answering model — qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad") def answer_question(question, context): return qa_pipeline({"question": question, "context": context})
- Load a pre-trained question answering model — qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad") def answer_question(question, context): return qa_pipeline({"question": question, "context": context})
- Load a small local model — generator = pipeline("text-generation", model="distilgpt2") def generate_response(category: str, user_message: str) -> str: if category == "support": prompt = f"Support response for: {user_message}" e
- Load tokenizer and base model — model_name = "local/llama-7b" tokenizer = AutoTokenizer.from_pretrained(model_name)
- Map categories to personas — category_to_persona = { "in-scope": "technical_writer", "out-of-scope": "general_assistant", "unsafe": "safety_bot" } return category_to_persona.get(category, "default") def generate(self, user_input:
- Merge and deduplicate — merged = {doc.page_content: doc for doc in vector_results} for name in graph_results: if name not in merged:
- Mock tools — def get_blog_post(post_id: int) -> dict: return {"id": post_id, "title": "Sample Post"} def analyze_threat(threat_vector: str) -> dict: return {"analysis": "Low risk"} def call_tool(payload: str) -> d
- Part III: Building AI Agents — Okay, I need to write a full chapter on AI Agents as part of a book about building local-first AI systems. The has given me specific guidelines: use the glossary definitions for consistency, synthesi
- Part IV: Full-Stack AI Applications — Okay, let's plan the structure of the chapter. I need to cover the three objectives: building AI backends with Django REST Framework, integrating Ollama with Django, and implementing async AI processi
- Part V: Advanced Topics — ## Chapter Objectives - Design and implement AI personas - Build persona-based content generators - Apply persona systems to real-world use cases ## BlogGenerator Wiki Page **BlogGenerator** is a pro
- Part VI: Cutting-Edge AI Development — <>assistant "Vibe Coding and AI-Assisted Development" ## The Vibe Coding Paradigm Vibe coding is a new term that's been circulating through the developer community, referring to a workflow where dev
- Part VII: Applied AI Systems — <>assistant Alright, let me start by understanding what the user is asking for. They want a technical book chapter titled "AI-Powered Study Systems" as part of the larger book "Sovereign AI: Building
- Persona-Based AI Generation — ## Why Personas Matter in AI Systems When we build AI applications that generate text, the quality of the output depends heavily on the voice behind the words. A persona captures that voice: a set of
- personalized_generation.py — import torch from transformers import AutoModelForCausalLM, AutoTokenizer model_name = "local/llama-7b" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrain
- Placeholder: replace with actual classifier call — lower = message.lower() if any(kw in lower for kw in ["help", "error", "issue"]): cat = "support" elif any(kw in lower for kw in ["code", "api", "endpoint"]): cat = "technical" else: cat = "general" r
- Prepare dataset — df = pd.read_csv("preferences.csv") # columns: chosen, rejected dataset = Dataset.from_pandas(df) def preprocess(example): chosen_input = tokenizer(example["chosen"], truncation=True, padding="max_le
- Prepare the payload — payload = { "model": "llama-3-70b", "prompt": "Explain quantum entanglement", "temperature": 0.7, "max_tokens": 1024 }
- Prepare the payload — payload = { "model": "llama-3-70b", "prompt": "Explain quantum entanglement", "temperature": 0.7, "max_tokens": 1024 }
- quality_control.py — from sklearn.metrics import cohen_kappa_score import numpy as np
- Query the graph — results = graph.query("What are the key topics in the dataset?") print(results) ``` This code snippet highlights how Cola abstracts away much of the complexity involved in setting up a local knowled
- Reciprocal rank fusion — scores = {} for i, doc in enumerate(vector_results): scores[doc["id"]] = scores.get(doc["id"], 0) + 1.0 / (i + 1) for i, doc in enumerate(keyword_results): scores[doc["id"]] = scores.get(doc["id"], 0)
- Resource handlers — @app.post("/mcp") async def mcp_endpoint(request: MCPRequest): if request.method == "list_resources": return MCPResponse( result={"resources": [{"id": "data.json", "name": "Data JSON"}]}, id=request.i
- Retrieve conversation history — history = self.conversation_history[-5:] # last 5 messages
- Return the category so the frontend can decide which model to invoke — return JsonResponse({"category": category}) ``` This endpoint is deliberately minimal; later we will replace the keyword logic with a real classifier that consumes the **CLASSIFIER_SYSTEM_PROMPT** a
- rlhf_pipeline.py — import torch from transformers import AutoModelForSequenceClassification, AutoTokenizer, Trainer, TrainingArguments from datasets import Dataset import pandas as pd
- Send the request — response = requests.post(ENDPOINT, json=payload) print(response.json()) ``` This snippet demonstrates how a simple HTTP call can trigger a sophisticated inference workflow managed by capacity.so. Th
- Send the request — response = requests.post(ENDPOINT, json=payload) print(response.json()) ``` This snippet demonstrates how a simple HTTP call can trigger a sophisticated inference workflow managed by capacity.so. Th
- Service API Specification — ## Endpoints - `GET /users` - List all users - `GET /users/{id}` - Get user by ID - `POST /users` - Create a new user ## Data Model User: id: int name: string email: string ``` #### Step 2:
- Service API Specification — ## Endpoints - `GET /users` - List all users - `GET /users/{id}` - Get user by ID - `POST /users` - Create a new user ## Data Model User: id: int name: string email: string ``` #### Step 2:
- settings.py — INSTALLED_APPS = [ ... "ai_core", "rest_framework", "corsheaders", ] MIDDLEWARE = [ ... "corsheaders.middleware.CorsMiddleware", ] CORS_ALLOWED_ORIGINS = [ "http://localhost:3000", ] ``` With the pr
- Simple check for common unsupported patterns — if '' in chunk.lower() or 'undefined' in chunk.lower(): return False return True ``` ## Implementation Considerations Building a production‑grade RAG involves more than wiring together retrieval an
- Simple JWT secret — SECRET_KEY = "local-secret-key" class AnnotationTask(BaseModel): data_id: str label: str def create_access_token(data: dict, expires_delta: timedelta = timedelta(hours=1)): to_encode = data.copy() to_
- Simple pattern‑based extraction of entity names from the query — entities = [word for word in query.split() if len(word) > 3] results = [] for ent in entities: rows = session.run( f"MATCH (n) WHERE n.name CONTAINS '{ent}' RETURN n.name LIMIT {top_k}" ) results.exte
- Simplified reasoning: decide based on keywords — if "blog" in observation.lower(): return json.dumps({"tool": "get_blog_post", "arguments": {"post_id": 1}}) elif "threat" in observation.lower(): return json.dumps({"tool": "analyze_threat", "argument
- Simulated annotations from two annotators — annotator_1 = np.array([0, 1, 1, 0, 1, 0, 1, 1, 0, 0]) annotator_2 = np.array([0, 1, 0, 0, 1, 1, 1, 1, 0, 0]) kappa = cohen_kappa_score(annotator_1, annotator_2) print(f"Cohen's Kappa: {kappa:.3f}")
- Simulated persona keys retrieved from database — persona_keys = {"tone": "friendly", "expertise": "technical", "length": "concise"} def generate_response(user_prompt: str, persona: dict):
- Start a REPL session — while True: user_input = input("Enter your query: ") if user_input.lower() == "exit": break response = ollama.generate(user_input) print(response) ``` This simple loop allows you to interact with a
- Understanding RAG Systems — Retrieval-Augmented Generation (RAG) has emerged as a cornerstone technique for building AI systems that can draw on external knowledge while retaining the flexibility of large language models. At its
- Update conversation history — self.conversation_history.append({"role": "", "content": user_input}) self.conversation_history.append({"role": "", "content": response}) return response def call_model(self, messages: List[Dict[str,
- Usage — df = pd.read_csv("employees.csv") summary = summarize_tabular_data(df, ["Name", "Age", "Salary"]) print(summary) ``` #### Model Selection and Prompt Design The choice of model matters. A 7‑billion‑
- Use capacity info to decide on concurrency — capacity = get_capacity("llama-3-70b") if capacity["gpu_available"] > 0: # Proceed with parallel processing ... ``` This pattern ensures that workflows scale gracefully, avoiding resource co
- Use capacity info to decide on concurrency — capacity = get_capacity("llama-3-70b") if capacity["gpu_available"] > 0: # Proceed with parallel processing ... ``` This pattern ensures that workflows scale gracefully, avoiding resource co
- Use the classifier to categorize the input — return self.classifier.classify(user_input) def select_persona(self, category: str) -> str:
- Using a REPL environment with a local LLM — import ollama
- Using BlogGenerator locally — import blog_generator