Skip to Content
企业级Java开发中的应用人工智能 (Chinese Edition)
book

企业级Java开发中的应用人工智能 (Chinese Edition)

by Alex Soto Bueno, Markus Eisele, Natale Vinto
November 2025
Beginner to intermediate
430 pages
5h 12m
Chinese
O'Reilly Media, Inc.
Content preview from 企业级Java开发中的应用人工智能 (Chinese Edition)

第三章 开发者指南 :prompt为何在人工智能应用中至关重要

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

prompt 是与LLMs交互的核心机制。它们定义了AI系统的响应方式,直接影响生成内容的质量、相关性和可靠性。对于构建AI融合应用的Java开发者而言,掌握prompt设计是至关重要的技能。精心设计的prompt能减少幻觉现象、提升响应一致性并优化性能,且无需对模型进行微调。关于有效prompt编写及最佳实践技巧已有诸多建议,例如OpenAI的《prompt工程指南》或James Phoenix与Mike Taylor合著的《生成式AI prompt工程》(O'Reilly出版)。本章旨在提供简要概述,作为您学习之旅的起点。

prompt类型

的prompt根据来源和引导模型的方式有所不同。主要类型包括用户prompt、系统prompt和上下文prompt。下面我们逐一探讨。

用户prompt:来自用户的直接输入

用户prompt即 终端用户提供的原始输入。这类输入通常缺乏结构化特征,需经预处理或上下文增强才能确保响应准确性。示例如下:

String userPrompt = "What is the capital of France?";

有效处理用户 prompt 需要输入净化、意图识别和上下文增强。我们将在第四章中对此进行更详细的探讨。

系统 prompt:引导模型行为的指令

系统 prompt定义了模型在会话中的行为模式( )。这类 prompt 通常在交互开始时设置,对用户不可见。它们可用于设定语气、强制约束或引导模型响应。例如:

String systemPrompt = "You are a helpful AI assistant\n
                       that provides concise and factual responses.";

系统prompt在应用场景中界定了LLM的边界,既可强制特定输出,也可包含工具调用指令。

上下文 prompt:预填充或动态生成的输入

上下文prompt包含 背景信息、历史交互记录或领域知识,通过添加至prompt来优化响应。这些内容可基于用户历史或外部数据动态生成,也是向对话注入记忆的有效方式。第四章将深入探讨其架构实现。以下为上下文prompt示例:

String context = "User previously asked about European capitals.";
String fullPrompt = context + " " + userPrompt;

上下文prompt能增强响应的相关性,尤其在多轮对话中,其作为事实上的记忆机制,帮助LLMs保持对话的连贯性。

高效 prompt 编写原则

具体性与结构性是有效prompt工程的核心要素。通过精准表述和逻辑化组织prompt,可显著提升LLMs的响应质量与相关性。精心设计结构严谨、目标明确的prompt,是充分发挥这些强大工具效能的关键步骤。以下分别展示过于具体与过于模糊的prompt示例:

String vaguePrompt = "Tell me about Java."; // Too broad
String specificPrompt = "Explain Java's garbage collection \n
                         mechanisms in one paragraph."; // Much better

vaguePrompt ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

产品思维工程师 (Chinese Edition)

产品思维工程师 (Chinese Edition)

Drew Hoskins

Publisher Resources

ISBN: 0642572279905