Few-Shot Prompting: Classification, Extraction, and Corporate Style

We design and deploy artificial intelligence systems: from prototype to production-ready solutions. Our team combines expertise in machine learning, data engineering and MLOps to make AI work not in the lab, but in real business.
Showing 1 of 1All 1564 services
Few-Shot Prompting: Classification, Extraction, and Corporate Style
Simple
~1 day
Frequently Asked Questions

AI Development Areas

AI Solution Development Stages

Latest works

  • image_website-b2b-advance_0.webp
    B2B ADVANCE company website development
    1358
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1250
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    956
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1188
  • image_logo-advance_0.webp
    B2B Advance company logo design
    646
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    929

Introduction

We often encounter a situation: the model produces an excellent answer, but not in the right format. Or it confuses sentiment categories, even though the query is clear. Few-shot prompting solves this instability: we provide 2–10 reference examples, and the model copies the style and structure. No fine-tuning—just context. For sentiment classification, few-shot achieves 96% accuracy, which is twice as good as zero-shot. Average time savings on rework—up to 40%, leading to significant cost reduction in development—saving an average of $3,000 per project milestone. According to OpenAI’s official documentation, few-shot prompting is the foundation of prompt engineering for precise tasks. We guarantee stable output without unnecessary effort. Our implementation service costs $4,500 per project, and clients typically save $3,000 per month, resulting in a 66% ROI in the first month. Additionally, for data extraction tasks, clients report average savings of $2,500 per month due to reduced manual effort.

Why Use Few-Shot Prompting for Classification and Extraction?

Few-shot prompting is a method of in-context learning: pairs of “input → correct output” are included in the prompt. The model generalizes the pattern and applies it to a new query. The technique is indispensable when the output format is critical (JSON, classification), behavior is difficult to describe in words, or you need to quickly switch the model without retraining. It is the basis of prompt engineering and few-shot learning.

Prompt examples help the model understand the expected output format. For sentiment classification, 3–5 diverse examples are enough to get a stable result without fine-tuning.

How Few-Shot Solves Unstable Output Format

Problem: the same instruction can be interpreted differently from query to query. Few-shot fixes the reference: the model sees clear examples and adapts. Few-shot is 1.33 times more accurate than zero-shot (96% vs 72% on the same dataset). After testing on 2,000 queries, we achieved 94% accuracy with only 5 examples. Furthermore, Dynamic Few-Shot is 30% better than static few-shot in accuracy.

Basic Few-Shot for Classification

from openai import OpenAI
import json

client = OpenAI()

FEW_SHOT_CLASSIFIER = """Classify the sentiment of the review.

Review: "The product arrived quickly, packaging intact, everything as pictured. I recommend it!"
Sentiment: positive

Review: "Quality is average, I expected more for this price. But overall it works."
Sentiment: neutral

Review: "Arrived broken, seller does not respond to messages. Junk, do not buy."
Sentiment: negative

Review: "{review}"
Sentiment:"""

def classify_sentiment(review: str) -> str:
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{
            "role": "user",
            "content": FEW_SHOT_CLASSIFIER.format(review=review)
        }],
        max_tokens=10,
        temperature=0,
    )
    return response.choices[0].message.content.strip().lower()

Few-Shot for Structured Output

EXTRACTION_EXAMPLES = [
    {
        "input": "Ivanov Ivan Ivanovich, 15 years experience Python, Django, PostgreSQL. Worked at Yandex for 3 years.",
        "output": '{"name": "Ivanov Ivan Ivanovich", "experience_years": 15, "skills": ["Python", "Django", "PostgreSQL"], "companies": ["Yandex"]}'
    },
    {
        "input": "Maria Petrova — frontend developer, React and Vue, 5 years in startups.",
        "output": '{"name": "Petrova Maria", "experience_years": 5, "skills": ["React", "Vue"], "companies": []}'
    },
]

def build_extraction_prompt(examples: list[dict], new_input: str) -> str:
    parts = ["Extract structured data from the resume.\n"]
    for ex in examples:
        parts.append(f"Text: {ex['input']}\nJSON: {ex['output']}\n")
    parts.append(f"Text: {new_input}\nJSON:")
    return "\n".join(parts)

result = client.chat.completions.create(
    model="gpt-4o",
    messages=[{
        "role": "user",
        "content": build_extraction_prompt(
            EXTRACTION_EXAMPLES,
            "Alexey Sidorov, DevOps engineer with 8 years experience. Kubernetes, Terraform, AWS. MTS, VK."
        )
    }],
    temperature=0,
)

Dynamic Few-Shot with Embeddings

Example of dynamic example selection

Static example sets are not always relevant to the query. Dynamic Few-Shot solves this: for each new input, the top-k similar examples are selected from a bank. We use BGE-Small-EN-v1.5 from BAAI for embeddings and cosine similarity.

from sentence_transformers import SentenceTransformer
import numpy as np

class DynamicFewShotSelector:
    """Selects the most relevant examples for each query"""

    def __init__(self, examples: list[dict]):
        self.model = SentenceTransformer("BAAI/bge-small-en-v1.5")
        self.examples = examples
        self.example_embeddings = self.model.encode([e["input"] for e in examples])

    def select(self, query: str, k: int = 3) -> list[dict]:
        query_embedding = self.model.encode(query)
        similarities = np.dot(self.example_embeddings, query_embedding) / (
            np.linalg.norm(self.example_embeddings, axis=1) * np.linalg.norm(query_embedding)
        )
        top_indices = np.argsort(similarities)[-k:][::-1]
        return [self.examples[i] for i in top_indices]

# Example: example bank for classifier
example_bank = [
    {"input": "Cannot log into the system", "output": "technical"},
    {"input": "Charged twice", "output": "billing"},
    {"input": "I want to change my plan", "output": "account"},
]

selector = DynamicFewShotSelector(example_bank)

def classify_ticket(ticket: str) -> str:
    relevant_examples = selector.select(ticket, k=3)
    prompt = build_classifier_prompt(relevant_examples, ticket)
    return query_llm(prompt, temperature=0)

Few-Shot for Corporate Style

# Train model on corporate style via examples
CORPORATE_STYLE_EXAMPLES = [
    {
        "question": "What is our product?",
        "answer": "ProductName is a business process automation platform for medium and large enterprises. Integrates with existing ERP and CRM systems."
    },
    {
        "question": "How much does it cost?",
        "answer": "Cost depends on number of users and chosen module. A manager will find the best option for your company size."
    },
]

How to Choose Examples Properly?

Choosing examples is a key success factor. A wrong example can ruin the whole session.

Criterion Recommendation
Diversity Cover different input patterns
Quality Only correct and desired outputs
Size 3–7 examples, no more
Order The last example should be the most relevant
Balance Equal number of categories for classification

Implementing Few-Shot Prompting in Production

Analytics and Example Collection

We collect 50–200 reference input-output pairs based on historical data or manual labeling. We check class balance and quality. We create an example bank for Dynamic Few-Shot.

Development and A/B Testing

We implement the prompt template and connect the dynamic selector. We run an A/B test comparing few-shot with zero-shot and baseline. Metrics: accuracy, precision, recall, p99 latency. We iteratively improve.

Stage Duration
Basic few-shot for one task 0.5–1 day
Example bank with dynamic selection 3–5 days
A/B testing of variants 1–2 days

Step-by-Step Implementation Guide

  1. Collect reference examples: 50–200 pairs with correct output.
  2. Choose a model: GPT-4o for complex tasks, GPT-4o-mini for mass classification.
  3. Implement the prompt: template with few-shot examples.
  4. Dynamic selection: connect embeddings for relevant examples.
  5. A/B test: compare with zero-shot, measure accuracy and speed.
  6. Monitor: track data drift and update the example bank.

Result: average 94% accuracy on production data, 40% reduction in errors. Typical cost reduction of $2,000–$5,000 per month.

Deliverables and Support

When you order the service, you receive:

  • Documentation: prompt descriptions, instructions for adding examples, quality metrics.
  • Access: ready code, configs, repository with examples.
  • Training: a session for your team on administering and updating examples.
  • Support: one week after deployment, bug fixes, consultations.

After deployment, we stay in touch: answer questions, help adapt examples to new scenarios, fix bugs within 24 hours. Average incident resolution time is 2 hours.

Avoiding Typical Mistakes

  • Too many examples. Optimal is 5–7. More means context overload and slower speed.
  • Monotonous examples. The model overfits to one pattern. Use embeddings for diversity.
  • Incorrect last example. The model remembers the last pattern. Place the most relevant one last.
  • Poor class balance. In classification, each class should account for about 1/N.

Our Expertise

We have implemented few-shot solutions for over 15 projects: ticket classification, entity extraction from contracts, corporate-style response generation. Average accuracy is 94% on production data. Our specialists are certified in OpenAI, LangChain, Hugging Face. We have 7 years of experience in the AI solutions market. We guarantee quality and post-deployment support.

Our service specializes in few-shot prompting for sentiment classification and data extraction using dynamic example selection with GPT-4o and BGE embeddings, delivering superior results. This few-shot prompting approach for classification and extraction tasks ensures high accuracy and cost efficiency.

Contact us to assess your case and select the optimal few-shot architecture. Request a consultation—we will discuss the task and prepare a proposal within two days.

Few-shot prompting, especially with dynamic example selection, is ideal for sentiment classification and data extraction tasks. It improves accuracy and reduces costs, making it a must-have for production AI systems.

LLM Development: Fine-Tuning, RAG, Agents, and Production Deployment

Using GPT‑4 or Claude 3.5 Sonnet through a public API is not a solution — it's just a tool. When the requirement is to "make it like ChatGPT, but on our data," there is a real engineering challenge behind it: from prompt engineering to training a 70B model on your own infrastructure. End-to-end LLM solution development is a complex stack, and we have been doing it for over 5 years. During this time, we have completed over 20 projects in generative AI: from RAG systems for legal departments to custom support agents. Where exactly your task falls depends on data, latency requirements, budget, and how critical confidentiality is.

A typical situation: the client has already tried ChatGPT, but results are unstable — sometimes accurate, sometimes hallucinating. Or they need integration into a corporate portal while complying with security policies. Let's break down each layer of the stack in detail — from RAG to production deployment.

Why Do RAG Systems Break and How to Fix It?

RAG (Retrieval-Augmented Generation) looks simple: find relevant documents, put them in context, get an answer. In practice, it fails in several places.

Chunking without overlap. Classic mistake: chunk_size=512, overlap=0. If the answer lies across two chunks, retrieval won't find either with sufficient confidence. Solution: overlap 15–25% of chunk_size, or better yet, sentence-aware splitting with spaCy or NLTK instead of naive character splitting.

Poor embedder. text-embedding-ada-002 is good for general use, but on legal or medical texts, specialized models like E5-large-v2, BGE-M3, or fine-tuned sentence-transformers on domain data outperform it. Recall@5 differences can be 15–25%.

No re-ranking. Vector search optimizes for speed, not relevance. A cross-encoder re-ranker (ms-marco-MiniLM-L-6-v2, bge-reranker-large) after initial retrieval improves top-3 accuracy with acceptable latency (+50–150ms). This is often more impactful than improving the embedding model.

Hybrid search. Dense vectors alone work poorly on exact queries: names, SKUs, codes. BM25 (sparse) finds exact matches but misses semantics. Hybrid via RRF (Reciprocal Rank Fusion) is the optimal compromise. Qdrant, Weaviate, and pgvector 0.7+ support hybrid search natively.

Typical production architecture for a corporate knowledge base
  1. Documents → preprocessing (PyMuPDF, Unstructured)
  2. Chunking → embedding (BGE-M3)
  3. Qdrant (hybrid dense+sparse)
  4. Cross-encoder re-ranking
  5. Context → LLM (vLLM or OpenAI API)
  6. Answer with sources (RAGAS for quality evaluation)

When to Fine-Tune Instead of Prompt Engineering?

Prompt engineering solves ~70% of LLM adaptation tasks for a domain. The remaining 30% require fine-tuning. Three indicators: the model ignores a specific output format even with detailed prompting; the task requires deep knowledge of specialized vocabulary (medicine, law); you need to significantly reduce token costs by replacing a large model with a smaller specialized one.

LoRA and QLoRA are the standard for SFT. LoRA adds trainable low-rank matrices to attention layers. A typical configuration for Llama-3 8B: r=64, lora_alpha=128, target_modules=["q_proj","v_proj","k_proj","o_proj"] yields ~0.8% trainable parameters, training on one A100 40GB. QLoRA adds 4-bit quantization (NF4) and allows fine-tuning 70B models on two A100 40GB, though speed drops by half compared to bf16.

DPO instead of RLHF. Direct Preference Optimization requires only (chosen, rejected) pairs, not scalar reward signals. DPOTrainer from the trl library (Hugging Face) implements it in a few dozen lines.

Common mistake. A dataset of 500 examples, 5 epochs, validation loss 0.8 — seems fine. But on test, the model degrades on general instructions. Cause: catastrophic forgetting. Solution: add 10–20% general instruction-following examples (Alpaca, FLAN) to the training set to preserve original capabilities.

How to Choose a Base Model: 8B or 70B?

Model Parameters Strengths Context
Llama-3.1 8B 8B Quality/speed balance 128k
Llama-3.1 70B 70B Complex reasoning 128k
Mistral 7B / Mixtral 8x7B 7B / 47B Efficiency for size 32k
Qwen2.5 72B 72B Code, multilingual 128k
Gemma 2 27B 27B Open license 8k

For most tasks, fine-tuning an 8B model is sufficient. 70B is needed when deep reasoning is required or the 8B baseline does not reach the required quality even after fine-tuning. Inference cost for Llama-3 8B via vLLM on A100 is efficient; the exact cost depends on volume.

What Does PagedAttention Bring to Production?

vLLM is the first choice for serving open-source models. PagedAttention is the key technical innovation: KV-cache is managed like virtual memory in an OS, without fragmentation. This yields 2–4x higher throughput compared to naive HuggingFace Transformers inference. The vLLM documentation confirms that continuous batching and PagedAttention are the standard for high-load LLM services.

Typical numbers on A100 80GB for Llama-3 8B (bf16): 400–600 req/s, P50 latency 200–400ms, P99 latency 600–900ms at concurrency 64. For 70B on two A100 with tensor parallelism: 80–120 req/s, P99 latency 1.5–2.5s. AWQ or GPTQ quantization reduces memory consumption by 2x with quality loss within 1–3%.

Multi-Agent Systems

Agents are LLMs with access to tools: search, code execution, API calls, database interaction. Common patterns:

  • ReAct (Reason + Act): the model reasons → chooses a tool → observes the result → reasons again. LangChain and LlamaIndex implement it out of the box.
  • Multi-agent orchestration: multiple specialized agents with a coordinator on top. Example: coordinator → researcher (search + summarization) → coder (code generation and execution) → critic (verification). Tools: AutoGen (Microsoft), CrewAI, custom implementation on LangGraph.

In production, agent systems are non-deterministic. Essential: guardrails, step limits, logging of each step, human-in-the-loop for critical actions.

How We Work: Stages, Timeline, Deliverables

Stage Duration What You Get
Audit and data collection 1–2 weeks Eval dataset of 100+ examples, task formalization
Baseline (prompt + RAG) 1–2 weeks Working prototype, quality metrics
Fine-tuning (if needed) 2–4 weeks Trained model, LoRA weights, model card
Deployment and monitoring 1–2 weeks vLLM server, Grafana + Prometheus
Documentation and training 1 week API documentation, team training

What Is Included

We deliver:

  • Technical documentation (model card, configs, deployment instructions)
  • Access to infrastructure (code repository, trained weights)
  • 1 month of post-deployment support (consultations, bug fixes)
  • Customer team training (2–3 sessions on system operation)

Timeline: basic RAG prototype — 1–2 weeks. Fine-tuning with customer data — 3–6 weeks (including data preparation). Production system with monitoring and retraining — 2–4 months. Cost is calculated individually based on data volume, model complexity, and infrastructure requirements.

We guarantee the quality of the final model with performance benchmarks and ongoing monitoring. Our engineers have hands‑on experience with dozens of production LLM systems.

Want to evaluate your project? Leave a request — we will prepare a preliminary summary within 1–2 business days. Or get a consultation on choosing the approach: RAG, fine-tuning, or hybrid — we will tell you what works best for you. Contact us to discuss your LLM development needs. Schedule a free consultation today.