How to Make LLM APIs Work Like the Web Chat: A Prompt Optimization Guide for Structured Information Extraction

    This guide explains why calling an LLM API directly often gives worse results on tasks such as relation extraction than the web chat interface does. It covers what the web product adds behind the scenes, why chat-style prompts fail on structured extraction, a five-part prompt structure with a worked example, and how to measure the gap with repeated runs and precision, recall, and F1. It also describes how some providers, including Baidu AI Cloud, now offer product-layer features such as web search, file parsing, and deep thinking through the API.

    1. Why Does the Web Chat Feel More Natural?

    In development and enterprise deployments, developers often see a puzzling result. When they call a large language model (LLM) API for a specific task, such as sentence-level relation extraction, the output is often much worse than what they get by asking the same question in the web chat interface. Is the problem the prompt, or has the API been stripped of capability? This article examines the technical principles, task characteristics, prompt engineering, and evaluation methods behind this gap, and offers practical steps to improve results.

    A common misconception is that the web chat interface is the model itself. In practice, the fluent, capable answers you see in the web chat result from the base model plus a layer of product logic.

    The web chat product is a highly packaged AI application. After you type a message, the product layer runs a series of preprocessing steps and builds context in the background, including:

    1. System instruction injection: Presets the model’s persona, tone, and response style.
    2. Context assembly: Organizes the conversation history to keep multi-turn dialogue coherent.
    3. External information integration: When you upload a file or enable web search, the system parses the file or retrieves web content and adds it to the prompt.
    4. Output format and safety rules: Enforces a specific output format and filters policy-violating content.
    5. Dynamic decisions: Determines when to trigger deep thinking and when to call tools. Agent rules on the backend control this logic.

    An API call, by contrast, runs only the single request you submit. The prompt you send is everything the model sees. Every missing piece of background or constraint must be supplied by the caller. So the statement “asking the model directly works better” can be true, but that does not prove the web product uses a smarter base model. It means the web product quietly handles a large amount of orchestration work for you.

    2. The Core Conflict in Information Extraction: Creativity Versus Restraint

    Why does a chat-style prompt often produce poor results for relation extraction? The answer lies in a basic conflict between chat and extraction tasks.

    In everyday conversation, a model may fill in context freely, which makes its answers read smoothly and naturally. Information extraction tasks, such as relation extraction and named entity recognition, require the opposite behavior:

    • If a relation is not explicitly stated in the text, the model must not infer it.
    • The subject and object of a relation must not be reversed.
    • Output must stay within the given schema, with no extra fields.

    If you ask the API in a conversational way, for example, “What relations are in this sentence?”, the model may treat “understanding the text” as “freely interpreting the text.” This can lead to hallucinated relations or messy output formats. To fix this, you need to restructure the prompt.

    3. A Five-Step Method for Building Relation Extraction Prompts

    For structured extraction tasks, split the prompt into the following five parts. Each part narrows the model’s behavior.

    1. Entity type and relation whitelist
    State which entity types and relations you care about. For example, in a medical context, limit entities to “drug” and “disease,” and relations to “treats” and “contraindicated.”

    2. Relation direction and decision rules
    Define the subject and object of each relation precisely. For example, the subject of “treats” must be a drug, and the object must be a disease. Add decision criteria so the model does not interpret the relation ambiguously.

    3. Uncertainty handling
    Instruct the model to output null whenever the context is ambiguous, and to never guess or infer. This is the key to curbing the model’s tendency to sound helpful and reducing false positives.

    4. Require supporting evidence
    Require the model to include the original supporting sentence with every relation it extracts. This makes later verification and review easier, and it encourages the model to be more rigorous during extraction.

    5. Fixed JSON schema and few-shot examples
    Provide a strict JSON output template and two or three boundary examples. Few-shot examples help anchor the output format and prevent the model from improvising.

    The following shows an optimized prompt structure:

    1. You are a professional relation extraction model. Extract the following relations from the given text:
    2. - Entity types: Drug, Disease
    3. - Relation whitelist: treats (Drug -> Disease), contraindicated (Drug -> Disease)
    4. - Decision rule: Extract a relation only when the text explicitly states that the drug can be used to treat the disease, or that it is contraindicated for the disease.
    5. - Evidence requirement: Each relation must include the original sentence from the text as evidence.
    6. - When uncertain: Set the corresponding field to null. Do not guess.
    7. Output format:
    8. {
    9. "relations": [
    10. {"subject": "", "relation": "", "object": "", "evidence": ""}
    11. ]
    12. }
    13. Example text: Aspirin can be used to relieve mild headaches, but patients with stomach ulcers should use it with caution.
    14. Example output:
    15. {
    16. "relations": [
    17. {"subject": "Aspirin", "relation": "treats", "object": "headache", "evidence": "Aspirin can be used to relieve mild headaches"},
    18. {"subject": "Aspirin", "relation": "contraindicated", "object": "stomach ulcer", "evidence": "patients with stomach ulcers should use it with caution"}
    19. ]
    20. }
    21. Extract from the following text: [insert your target text]

    4. How to Measure the Gap Between the API and the Web Chat Fairly

    Many developers compare one web chat response with one API response and draw a conclusion from that single pair. This approach ignores the randomness inherent in generative models. The same prompt can produce answers of different quality across repeated calls.

    To reach an objective conclusion, control the variables carefully:

    • Use the same base model version.
    • Use the same thinking mode, such as whether deep thinking is enabled.
    • Use the same set of test sentences.
    • Follow the same output protocol.

    After you control these variables, run the same dataset through repeated calls and compute the standard metrics for relation extraction: precision, recall, and F1 score. Only a quantified evaluation can show the actual performance gap between the API and the web chat on a specific task, rather than a subjective impression.

    5. Product-Layer Orchestration in LLM APIs

    In the past, developers mainly bought base model inference through an API. Context assembly and tool calling had to be implemented on the client side. Many developers find APIs harder to use because the orchestration work that the web backend handles quietly is left entirely to the bare model.

    This situation is changing. Major model service providers, such as Baidu AI Cloud, are extending product-layer orchestration capabilities into their APIs. Some advanced APIs can now enable web search, file input parsing, and deep thinking on the server side, with tool calling and context assembly handled automatically.

    Some of the web product’s capabilities can therefore be carried into API calls. These advanced orchestration features are typically disabled by default in the API, so developers must turn them on through specific request parameters. For details, see the relevant API documentation for your provider. When you call an API, you must decide not only how to write the prompt, but also which product-layer orchestration capabilities to use and enable.

    Summary

    An LLM API does not perform worse than the web chat because model capability has declined. The difference comes from how deeply the application layer wraps the model. For tasks that demand strict accuracy, such as information extraction, developers should move away from casual chat-style prompts and adopt structured prompts with clear constraints. Combined with a rigorous evaluation process and the advanced orchestration features that cloud providers now offer, developers can build AI applications that are more precise and better matched to their business needs than the web chat experience.