AI & ML Developer Guide
Every capability on this page is part of the standard library — no pip, no npm, no third-party packages. Import the bundle, compile with the flag, and go. See the API reference for full class documentation.
Contents
Authentication
Cloud APIs use API keys stored in plain text files in your working directory.
OpenAI
Create openai_api_key.dat containing your key, then read it at startup:
use API.OpenAI, System.IO.Filesystem;
token := FileReader->ReadFile("openai_api_key.dat")->Trim();
Gemini
Create gemini_api_key.dat and use the built-in helper:
use API.Google.Gemini;
token := EndPoint->GetApiKey(); # reads gemini_api_key.dat
Ollama
No key required. Start the daemon and pull a model:
ollama serve ollama pull llama3.2
OpenAI
Chat & Text
use API.OpenAI, System.IO.Filesystem;
class ChatExample {
function : Main(args : String[]) ~ Nil {
token := FileReader->ReadFile("openai_api_key.dat")->Trim();
# single-turn
response := Response->Respond("gpt-4o-mini",
Pair->New("user", "Explain JIT compilation in one sentence."),
token);
response->GetText()->PrintLine();
# multi-turn conversation
messages := Vector->New()>;
messages->AddBack(Pair->New("system", "You are a concise coding assistant."));
messages->AddBack(Pair->New("user", "What is a closure?"));
messages->AddBack(Pair->New("assistant", "A closure captures its enclosing scope."));
messages->AddBack(Pair->New("user", "Show me one in Objeck."));
response := Response->Respond("gpt-4o-mini", messages, token);
response->GetText()->PrintLine();
}
}
> obc -src chat.obs -lib net,net_server,json,cipher,misc,openai > obr chat
Vision
use API.OpenAI, System.IO.Filesystem;
bytes := FileReader->ReadBinaryFile("photo.jpg");
image := ImageQuery->New("What is in this image?", bytes, ImageQuery->MimeType->JPEG);
query := Pair->New("user", image);
response := Response->Respond("gpt-4o", query, token);
response->GetText()->PrintLine();
Embeddings
use API.OpenAI;
values := Embedding->Create("Objeck is a JIT-compiled language",
"text-embedding-3-small", token);
if(values <> Nil) {
"Dimensions: {$values->Size()}"->PrintLine(); # 1536
};
Moderation
Per-category safety flags and confidence scores:
use API.OpenAI;
result := Moderation->Check("I want to hurt someone.", token);
if(result <> Nil) {
"Flagged: {$result->IsFlagged()}"->PrintLine();
if(result->IsFlagged()) {
"violence score: {$result->GetScore("violence")}"->PrintLine();
};
};
Categories: harassment, hate, self-harm, sexual, violence (and /threatening, /graphic, /instructions, /intent variants).
Batch Processing
Up to 50,000 requests at 50% cost. Results within 24 hours.
use API.OpenAI, System.IO.Filesystem;
# 1. upload request file
data := FileReader->ReadBinaryFile("requests.jsonl");
file := File->Create("requests.jsonl", "batch", data, token);
# 2. submit
job := Batch->Create(file->GetId(), "/v1/chat/completions", token);
"Batch ID: {$job->GetId()}"->PrintLine();
# 3. poll for completion
job := Batch->Get(job->GetId(), token);
if(job->IsComplete()) {
"Output file: {$job->GetOutputFileId()}"->PrintLine();
};
Request format — one JSON object per line in requests.jsonl:
{"custom_id":"r1","method":"POST","url":"/v1/chat/completions","body":{"model":"gpt-4o-mini","messages":[{"role":"user","content":"Hello"}]}}
Realtime Audio
Text or audio over WebSocket; returns transcript and PCM audio. Requires -lib sdl2 for playback.
use API.OpenAI, Game.SDL2;
response := Realtime->Respond("What time is it in Tokyo?",
"gpt-4o-realtime-preview", token);
if(response <> Nil) {
response->GetFirst()->PrintLine(); # transcript
audio := response->GetSecond();
Mixer->PlayPcm(audio->Get(), 24000,
AudioFormat->SDL_AUDIO_S16LSB, 1); # 24 kHz mono PCM
};
Gemini
Chat & Vision
use API.Google.Gemini;
class GeminiChat {
function : Main(args : String[]) ~ Nil {
token := EndPoint->GetApiKey();
# single-turn text
content := Content->New("user")
->AddPart(TextPart->New("Why is the sky blue?"));
candidates := Model->GenerateContent("models/gemini-2.0-flash", content, token);
if(candidates <> Nil & <>candidates->IsEmpty()) {
candidates->First()->GetAllText()->PrintLine();
};
# image + text
bytes := System.IO.Filesystem.FileReader->ReadBinaryFile("chart.png");
content := Content->New("user")
->AddPart(TextPart->New("Summarize this chart."))
->AddPart(BinaryPart->New(bytes, "image/png"));
candidates := Model->GenerateContent("models/gemini-2.0-flash", content, token);
if(candidates <> Nil & <>candidates->IsEmpty()) {
candidates->First()->GetAllText()->PrintLine();
};
# multi-turn with system instruction
chat := Chat->New("models/gemini-2.0-flash", token);
chat->SetSystemInstruction(
Content->New("system")->AddPart(TextPart->New("You are a coding assistant.")));
chat->SendPart(TextPart->New("What is a closure?"), "user")->GetAllText()->PrintLine();
chat->SendPart(TextPart->New("Show me one in Objeck."), "user")->GetAllText()->PrintLine();
}
}
Search Grounding
Anchor responses in live Google Search results for up-to-date answers:
use API.Google.Gemini;
content := Content->New("user")
->AddPart(TextPart->New("What major AI models were released this month?"));
candidates := Model->GenerateContentWithGrounding(
"models/gemini-2.0-flash", content, token);
if(candidates <> Nil & <>candidates->IsEmpty()) {
candidates->First()->GetAllText()->PrintLine();
};
Files API
Upload once, reference across many requests without re-uploading:
use API.Google.Gemini, System.IO.Filesystem;
# upload
data := FileReader->ReadBinaryFile("report.pdf");
file := FileManager->Upload("Q1 Report", data, "application/pdf", token);
if(file <> Nil & file->IsActive()) {
"URI: {$file->GetUri()}"->PrintLine();
};
# list active files
files := FileManager->List(token);
each(f in files) {
"{$f->GetName()}: {$f->GetState()}"->PrintLine();
};
# delete
FileManager->Delete("files/abc123", token);
Context Caching
Cache large reused content server-side to avoid re-tokenization cost on every request:
use API.Google.Gemini, System.IO.Filesystem;
large_context := FileReader->ReadFile("legal_document.txt");
content := Content->New("user")->AddPart(TextPart->New(large_context));
# cache for 5 minutes (300 seconds)
item := CachedContent->Create(
"models/gemini-1.5-pro-001", content, 300, "legal-doc-cache", token);
if(item <> Nil) {
"Tokens cached: {$item->GetTokenCount()}"->PrintLine();
"Expires: {$item->GetExpireTime()}"->PrintLine();
};
CachedContent->Delete("cachedContents/abc123", token);
Embeddings
use API.Google.Gemini;
# single embedding (768 dimensions)
content := Content->New("user")->AddPart(TextPart->New("machine learning"));
values := Model->EmbedContent(content, token);
"Dimensions: {$values->Size()}"->PrintLine();
# batch — multiple texts in one round-trip
texts := Vector->New();
texts->AddBack("Objeck is JIT-compiled");
texts->AddBack("Python uses an interpreter");
texts->AddBack("Rust is memory-safe");
embeddings := Model->BatchEmbedContent("models/text-embedding-004", texts, token);
each(i : embeddings) {
" [{$i}] dim={$embeddings->Get(i)->Size()}"->PrintLine();
};
Ollama (Local LLMs)
Run open-source models locally. No API key, no data leaves your machine. Install Ollama, start the daemon, then pull a model:
ollama serve && ollama pull llama3.2
use API.Ollama;
class LocalChat {
function : Main(args : String[]) ~ Nil {
# one-shot generation
Completion->Generate("llama3.2", "What is 2 + 2?")->PrintLine();
# with temperature control
opts := Options->New()->SetTemperature(0.2);
Completion->Generate("llama3.2", "List 3 capitals of Europe.", opts)->PrintLine();
# multi-turn — context maintained automatically
chat := Chat->New("llama3.2");
chat->Send("My name is Alice.");
chat->Send("What is my name?")->PrintLine(); # "Your name is Alice."
# vision (multimodal models)
image := System.IO.Filesystem.File->New("photo.jpg");
Completion->Generate("llava", "Describe this image.", image)->PrintLine();
# local embeddings
values := Model->Embeddings("nomic-embed-text", "machine learning");
"Dimensions: {$values->Size()}"->PrintLine();
}
}
> obc -src local_chat.obs -lib net,json,cipher,misc,ollama > obr local_chat
Machine Learning
Pure Objeck ML routines backed by the Eigen C++ library — no Python, no external frameworks. Covers linear algebra, regression, neural networks, clustering, and model evaluation.
Matrix Operations
Matrix2D provides the foundational matrix math used by all other ML classes, plus activation functions used in neural architectures:
use System.ML;
class MatrixExample {
function : Main(args : String[]) ~ Nil {
a := [[1.0, 2.0], [3.0, 4.0]];
b := [[5.0, 6.0], [7.0, 8.0]];
# matrix dot product: [[19,22],[43,50]]
c := Matrix2D->DotProduct(a, b);
rows := c->Size();
each(r : rows) {
each(col : c[r]->Size()) {
"{$c[r,col]} "->Print();
};
'\n'->Print();
};
# activation functions (element-wise)
x := [[0.5, -0.3], [1.2, -0.8]];
Matrix2D->ReLU(x); # max(0, x): [[0.5,0.0],[1.2,0.0]]
Matrix2D->Sigmoid(x); # 1/(1+e^-x)
Matrix2D->Tanh(x); # hyperbolic tangent
Matrix2D->Softmax(x); # probability distribution over rows
# statistical helpers
"Row 0 avg: {$Matrix2D->AverageRow(0, a)}"->PrintLine(); # 1.5
"Col 1 sum: {$Matrix2D->SumColumn(1, a)}"->PrintLine(); # 6.0
"Std dev: {$Matrix2D->StdDevColumn(0, a)}"->PrintLine();
# random matrices for weight initialization
weights := Matrix2D->Random(3, 4); # uniform [0,1]
weights := Matrix2D->RandomNormal(0.0, 0.1, 3, 4); # Gaussian
}
}
Regression
Linear Regression — least-squares fit for continuous target prediction:
use System.ML;
class RegressionExample {
function : Main(args : String[]) ~ Nil {
# features: [size_sqft, rooms] → price
X := [[800.0, 2.0], [1200.0, 3.0], [1600.0, 4.0], [2000.0, 5.0]];
y := [[150000.0], [220000.0], [300000.0], [380000.0]];
model := LinearRegression->New();
if(model->Fit(X, y)) {
preds := model->Predict([[1400.0, 3.0]]);
"Predicted price: {$preds[0,0]}"->PrintLine();
"R²: {$model->GetRSquared()}"->PrintLine();
coefficients := model->GetCoefficients();
each(i : coefficients->Size()) {
"w[{$i}] = {$coefficients[i]}"->PrintLine();
};
};
}
}
Logistic Regression — gradient descent binary classifier with sigmoid activation:
use System.ML;
class ClassifierExample {
function : Main(args : String[]) ~ Nil {
# features: [hours_studied, prev_score] → pass/fail
X := [[2.0, 55.0], [3.0, 60.0], [7.0, 80.0], [9.0, 90.0]];
y := [[0.0], [0.0], [1.0], [1.0]];
model := LogisticRegression->New(0.01, 2000);
if(model->Fit(X, y)) {
probs := model->Predict([[5.0, 72.0]]);
classes := model->PredictClass([[5.0, 72.0]]);
"Probability: {$probs[0]}"->PrintLine();
"Predicted pass: {$classes[0]}"->PrintLine();
};
}
}
Neural Networks
Configurable multi-layer network trained with backpropagation. Models can be saved and reloaded:
use System.ML, Collection;
class XorNet {
function : Main(args : String[]) ~ Nil {
# XOR training data
inputs := Vector->New();
targets := Vector->New();
inputs->AddBack(FloatMatrixRef->New([[0.0, 0.0]])); targets->AddBack(FloatMatrixRef->New([[0.0]]));
inputs->AddBack(FloatMatrixRef->New([[0.0, 1.0]])); targets->AddBack(FloatMatrixRef->New([[1.0]]));
inputs->AddBack(FloatMatrixRef->New([[1.0, 0.0]])); targets->AddBack(FloatMatrixRef->New([[1.0]]));
inputs->AddBack(FloatMatrixRef->New([[1.0, 1.0]])); targets->AddBack(FloatMatrixRef->New([[0.0]]));
# Train: 2 inputs → 3 hidden (factor) → 1 output
net := NeuralNetwork->Train(2, inputs, 3, 1, targets, 0.3, 10000);
if(net <> Nil) {
query := FloatMatrixRef->New([[1.0, 0.0]]);
"1 XOR 0 — confidence: {$net->Confidence(query)}"->PrintLine(); # ~1.0
"1 XOR 0 — activated: {$net->Query(query)}"->PrintLine(); # true
net->Store("xor.dat");
};
}
}
Load CSV training data with the built-in reader — it splits inputs and targets automatically:
use System.ML, Collection;
# LoadSplitMatrices: filename, target column offset, train%, line ending
splits := MatrixReader->LoadSplitMatrices("iris.csv", 4, 0.8, "\n");
# splits[0]=train_inputs splits[1]=train_targets
# splits[2]=test_inputs splits[3]=test_targets
train_inputs := splits[0];
train_targets := splits[1];
net := NeuralNetwork->Train(4, train_inputs, 6, 3, train_targets, 0.05, 5000);
> obc -src xor_net.obs -lib ml > obr xor_net
Clustering & Classification
K-Nearest Neighbors — instance-based classification over float feature vectors:
use System.ML;
class KNNExample {
function : Main(args : String[]) ~ Nil {
# training: [height_cm, weight_kg] with size labels
data := [[162.0, 55.0], [175.0, 75.0], [185.0, 90.0], [155.0, 48.0]];
labels := ["S", "M", "L", "XS"];
knn := KNearestNeighbors->New(data, labels);
neighbors := knn->Query(3, [170.0, 68.0]);
each(n in neighbors) {
"{$n->GetCategory()}: distance={$n->GetDistance()}"->PrintLine();
};
}
}
K-Means Clustering — unsupervised grouping with Dunn index quality score:
use System.ML, Collection;
class ClusterExample {
function : Main(args : String[]) ~ Nil {
records := Vector->New();
records->AddBack(FloatArrayRef->New([1.0, 1.5]));
records->AddBack(FloatArrayRef->New([1.2, 1.3]));
records->AddBack(FloatArrayRef->New([8.0, 8.5]));
records->AddBack(FloatArrayRef->New([7.8, 8.2]));
records->AddBack(FloatArrayRef->New([4.5, 4.0]));
groups := ["low", "high"];
grouping := KMeans->Group(groups, records, 2, 0.0, 10.0);
"Dunn Index: {$grouping->GetDunnIndex()}"->PrintLine();
each(i : grouping->Size()) {
g := grouping->Get(i);
"Group '{$g->GetName()}': {$g->Size()} members"->PrintLine();
};
}
}
Naive Bayes — probabilistic text classification (spam detection, sentiment, etc.):
use System.ML;
class SpamFilter {
function : Main(args : String[]) ~ Nil {
# "ham" group (index 0)
ham := BayesGroup->New(0.6);
ham->AddEntry(BayesEntry->New("meeting", 15));
ham->AddEntry(BayesEntry->New("report", 12));
ham->AddEntry(BayesEntry->New("project", 18));
# "spam" group (index 1)
spam := BayesGroup->New(0.4);
spam->AddEntry(BayesEntry->New("buy", 30));
spam->AddEntry(BayesEntry->New("free", 25));
spam->AddEntry(BayesEntry->New("click", 20));
classifier := NaiveBayes->New(ham, spam);
result := classifier->Query(["buy", "free"]);
if(result = 1) {
"Spam"->PrintLine();
} else {
"Ham"->PrintLine();
};
}
}
Evaluation & Preprocessing
FeatureScaler normalizes inputs before training; Metrics evaluates classifier performance:
use System.ML;
class EvalExample {
function : Main(args : String[]) ~ Nil {
# ── Feature scaling ──────────────────────────────
raw := [[10.0, 200.0], [20.0, 400.0], [30.0, 600.0]];
scaled := FeatureScaler->MinMaxScaler(raw); # [0, 1]
z_score := FeatureScaler->StandardScaler(raw); # mean=0, std=1
l2_norm := FeatureScaler->Normalize(raw); # L2 row normalization
# ── Metrics ──────────────────────────────────────
predictions := [true, false, true, true, false];
actuals := [true, true, false, true, false];
"Accuracy: {$Metrics->Accuracy(predictions, actuals)}"->PrintLine();
"Precision: {$Metrics->Precision(predictions, actuals)}"->PrintLine();
"Recall: {$Metrics->Recall(predictions, actuals)}"->PrintLine();
"F1 Score: {$Metrics->F1Score(predictions, actuals)}"->PrintLine();
"MCC: {$Metrics->MCC(predictions, actuals)}"->PrintLine();
cm := Metrics->ConfusionMatrix(predictions, actuals);
Metrics->PrintConfusionMatrix(cm);
# ── Train/test split ─────────────────────────────
data := [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0], [7.0, 8.0], [9.0, 10.0]];
splits := CrossValidation->TrainTestSplit(data, 0.2); # 80/20
train := splits[0]->Get();
test := splits[1]->Get();
"Train rows: {$train->Size()}, Test rows: {$test->Size()}"->PrintLine();
}
}
> obc -src eval.obs -lib ml > obr eval
ONNX Local Inference
Run ONNX models locally with hardware acceleration: DirectML (Windows), CUDA (Linux), CoreML (macOS). CPU inference requires no GPU. Models load from local files — no internet at runtime.
Face Recognition
Uses InsightFace buffalo_l: SCRFD 10G-KPS detector + ArcFace R50 512-dim embeddings.
curl -L -o buffalo_l.zip https://github.com/deepinsight/insightface/releases/download/v0.7/buffalo_l.zip unzip buffalo_l.zip # det_10g.onnx and w600k_r50.onnx
use API.Onnx, System.IO.Filesystem;
session := FaceSession->New("det_10g.onnx", "w600k_r50.onnx");
img1 := FileReader->ReadBinaryFile("person_a.jpg");
img2 := FileReader->ReadBinaryFile("person_b.jpg");
r1 := session->Recognize(img1, 0.5);
r2 := session->Recognize(img2, 0.5);
if(r1->GetSize() > 0 & r2->GetSize() > 0) {
emb1 := r1->GetResults()[0]->GetEmbedding();
emb2 := r2->GetResults()[0]->GetEmbedding();
sim := FaceSession->Compare(emb1, emb2);
"Similarity: {$sim}"->PrintLine();
"Same person: {$(sim > 0.35)}"->PrintLine();
};
session->Close();
Object Detection (YOLO)
use API.Onnx, System.IO.Filesystem;
labels := String->New[80]; # COCO class labels
labels[0] := "person"; labels[1] := "bicycle"; # ... fill all 80
session := YoloSession->New("yolov11n.onnx");
img := FileReader->ReadBinaryFile("street.jpg");
result := session->Inference(img, 640, 640, 0.5, labels);
each(d in result->GetClassifications()) {
"{$d->GetLabel()}: {$d->GetConfidence()}"->PrintLine();
};
session->Close();
Phi-3 Mini (Local SLM)
Run Microsoft's Phi-3 Mini locally — no internet at inference time. (~2 GB model)
huggingface-cli download microsoft/Phi-3-mini-4k-instruct-onnx \ --include "directml/directml-int4-awq-block-128/*"
use API.Onnx, System.IO.Filesystem;
tokenizer := Phi3Tokenizer->New("tokenizer.json");
session := Phi3Session->New("phi3-mini-4k-instruct.onnx");
prompt := "<|user|>\nWhat is the capital of France?<|end|>\n<|assistant|>\n";
token_ids := tokenizer->Encode(prompt);
eos := Int->New[1]; eos[0] := 32007;
result := session->Generate(token_ids, 200, 0.7, eos);
tokenizer->Decode(result->GetTokenIds())->PrintLine();
session->Close();
Computer Vision (OpenCV)
use API.OpenCV;
class VisionExample {
function : Main(args : String[]) ~ Nil {
# load, process, save
image := Image->New("photo.jpg");
gray := image->ToGray();
blurred := gray->GaussianBlur(5, 5);
edges := blurred->Canny(50, 150);
edges->Save("edges.jpg");
# face detection via Haar cascade
detector := FaceDetector->New("haarcascade_frontalface_default.xml");
faces := detector->Detect(image);
"Faces detected: {$faces->Size()}"->PrintLine();
# resize and color space conversion
resized := image->Resize(320, 240);
resized->ToHsv()->Save("hsv_output.jpg");
}
}
Natural Language Processing
Built-in NLP primitives — no model download required. Pure Objeck bytecode.
use API.ML.NLP;
class NLPExample {
function : Main(args : String[]) ~ Nil {
# sentiment
SentimentAnalyzer->Classify("This product is absolutely wonderful!")->PrintLine(); # "positive"
# TF-IDF vectorization
docs := String->New[3];
docs[0] := "cats are pets";
docs[1] := "dogs are pets";
docs[2] := "birds can fly";
tfidf := TF_IDF->New();
tfidf->Fit(docs);
vector := tfidf->Transform("cats and dogs");
each(v in vector) { v->PrintLine(); };
# cosine similarity
sim := TextSimilarity->Cosine("hello world", "hello there");
"Similarity: {$sim}"->PrintLine();
# tokenization
tokens := Tokenizer->Tokenize("The quick brown fox");
each(t in tokens) { t->PrintLine(); };
}
}
> obc -src nlp.obs -lib gen_collect,nlp > obr nlp
Quick Reference
| Capability | Class | Library flag | Key |
|---|---|---|---|
| Chat / text | Response | openai | OpenAI |
| Vision (image input) | Response + ImageQuery | openai | OpenAI |
| Realtime audio | Realtime | openai,sdl2 | OpenAI |
| Text embeddings | Embedding | openai | OpenAI |
| Content moderation | Moderation | openai | OpenAI |
| Batch processing | Batch | openai | OpenAI |
| Gemini chat / vision | Model::GenerateContent | gemini | Gemini |
| Search grounding | Model::GenerateContentWithGrounding | gemini | Gemini |
| File upload | FileManager | gemini | Gemini |
| Context caching | CachedContent | gemini | Gemini |
| Gemini embeddings | Model::EmbedContent / BatchEmbedContent | gemini | Gemini |
| Local chat / generation | Completion, Chat | ollama | None |
| Local embeddings | Model::Embeddings | ollama | None |
| Matrix algebra | Matrix2D | ml | None |
| Linear regression | LinearRegression | ml | None |
| Logistic regression | LogisticRegression | ml | None |
| Neural network | NeuralNetwork | ml | None |
| K-nearest neighbors | KNearestNeighbors | ml | None |
| K-means clustering | KMeans | ml | None |
| Naive Bayes | NaiveBayes | ml | None |
| Feature scaling | FeatureScaler | ml | None |
| Model evaluation | Metrics, CrossValidation | ml | None |
| Face recognition | FaceSession | onnx | None |
| Object detection | YoloSession | onnx | None |
| Local SLM | Phi3Session | onnx | None |
| Computer vision | Image, FaceDetector | opencv | None |
| Sentiment / TF-IDF | SentimentAnalyzer, TF_IDF | nlp | None |
Model Recommendations
| Task | OpenAI | Gemini | Ollama (local) |
|---|---|---|---|
| General chat | gpt-4o-mini | gemini-2.0-flash | llama3.2 |
| Reasoning | o3-mini | gemini-2.5-pro | qwen2.5 |
| Vision | gpt-4o | gemini-2.0-flash | llava |
| Realtime audio | gpt-4o-realtime-preview | — | — |
| Text embeddings | text-embedding-3-small | text-embedding-004 | nomic-embed-text |
| Fast & cheap | gpt-4o-mini | gemini-2.5-flash | phi3 |
Example Programs
Full working examples in programs/frameworks/:
openai_batch.obs openai_tune.obs openai_responses.obs
gemini/ gemini_image.obs gemini_audio.obs gemini_files.obs
gemini_cache.obs gemini_ground.obs gemini_embed.obs
ollama/ ollama_chat.obs ollama_vision.obs
opencv_onnx/ face_recog.obs yolo_detect.obs phi3_chat.obs