APIQIKAPIQIK
Docs
Cookbook

Knowledge-Base Q&A with RAG

Add retrieved source passages to model context and return traceable citations.

Retrieval-augmented generation finds relevant passages in your knowledge base, then asks the model to answer from those materials. It suits product support, company policies, and internal knowledge search.

Processing flow

User question and query rewriting → knowledge retrieval, ranking, and trimming → APIQIK answers using the context.

Minimal generation example

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["APIQIK_API_KEY"],
    base_url="https://api.apiqik.com/v1",
)

# 假设这些片段已由你的检索系统返回。
documents = [
    {"id": "doc-17", "text": "The APIQIK Base URL is https://api.apiqik.com/v1."},
    {"id": "doc-23", "text": "The chat endpoint path is /chat/completions and uses Bearer tokens."},
]

context = "\n\n".join(
    f"[{item['id']}] {item['text']}" for item in documents
)

response = client.chat.completions.create(
    model="qwen-turbo",
    messages=[
        {
            "role": "system",
            "content": (
                "Answer only from the supplied materials. If insufficient, say 'The materials do not specify this.' "
                "Cite [document-ID] after each conclusion."
            ),
        },
        {
            "role": "user",
            "content": f"Materials:\n{context}\n\nQuestion: What is the APIQIK chat endpoint URL?",
        },
    ],
    temperature=0.2,
)

print(response.choices[0].message.content)

Retrieval quality matters more than prompting

  • Preserve titles, section paths, and update times in chunk metadata.
  • Rerank retrieved passages and remove duplicates or conflicting material.
  • Sort context by relevance and trim it to the token budget.
  • Clearly separate user questions from source passages to reduce prompt-injection risk.
  • Validate and render citations in application code to prevent invented links.
  • Apply tenant and user permissions before retrieving documents.

Evaluation metrics

MetricMeaning
RecallWhether passages needed for the correct answer enter the candidate set.
Context precisionHow much of the supplied context is actually relevant.
Answer faithfulnessWhether supplied materials support the answer rather than guesswork.
Citation accuracyWhether each citation points to a passage supporting the claim.
Correct abstentionWhether the model declines to answer when materials are insufficient.

Treat retrieved content as untrusted input. External documents may contain prompt injections. System instructions should tell the model to ignore operational instructions within materials and use them only as source content.