SpringAI 大模型应用开发篇-纯 Prompt 开发(舔狗模拟器)、Function Calling(智能客服)、RAG (知识库 ChatPDF)

发布于:2025-05-25 ⋅ 阅读:(19) ⋅ 点赞:(0)

🔥博客主页: 【小扳_-CSDN博客】
❤感谢大家点赞👍收藏⭐评论✍

文章目录

        1.0 大模型应用开发技术框架

        2.0 纯 Prompt 模式

        2.1 核心策略

        2.2 减少模型"幻觉"的技巧

        2.3 提示词攻击防范

        2.4 纯 Prompt 大模型开发(舔狗模拟器)

        3.0 Function Calling 模式

        3.1 Function Calling 模式具体流程

        3.2 智能客服

        4.0 RAG 模式

        4.1 RAG 原理

        4.2 向量模型

        4.3 向量数据库

        4.4 文件读取和转换

        4.5 PDF 知识库


        1.0 大模型应用开发技术框架

        基于大模型开发应用有多种方式,来了解常见的大模型开发技术框架。

目前,大模型应用开发的技术框架主要有四种:

        2.0 纯 Prompt 模式

        不同的提示词能够哦让大模型给出差异巨大的答案。

        不断雕琢提示词,使大模型能给出最理想的答案,这个过程就叫做提示词工程。很多简单的 AI 应用,仅仅靠一段足够好的提示词就能实现了,这就是纯 Prompt 模式。

流程图:

        2.1 核心策略

        1)清晰明确的指令

        直接说明任务类型(如总结、分类、生成),避免模糊表达。

        实例:

低效提示:“谈谈人工智能。”  
高效提示:“用200字总结人工智能的主要应用领域,并列出3个实际用例。”

        2)使用分隔符标记输入内容

        用 '''、"""或 xml 标签分隔用户输入,防止提示注入。

        实例:

请将以下文本翻译为法语,并保留专业术语:
"""
The patient's MRI showed a lesion in the left temporal lobe.  
Clinical diagnosis: probable glioma.
"""

        3)分步骤拆解复杂任务

        将任务分解为多个步骤,逐步输出结果。

        4)提供示例

        通过输入-输出示例指定格式或风格。

        5)指定输出格式

        明确要求 JSON、HTML 或特定结构。

        6)给模型设定一个角色

        设定角色可以让模型在正确的背景下回答问题,减少幻觉。

        2.2 减少模型"幻觉"的技巧

        1)引用原文:要求答案基于提供的数据("如根据以下文章 .... ")

        2)限制编造:添加指令,如"若不确定,回答'无相关信息'"。

        2.3 提示词攻击防范

        1)提示注入

        防范措施:

                - 输入分隔符:用 ``` 、""" 等标记用户输入区域。

                - 权限控制:在系统 Prompt 中明确限制任务范围。

        2)越狱攻击

        防范措施:

                - 内容过滤:使用 Moderation API 检测违规内容。

                - 道德约束:在 Prompt 中强化安全声明。

示例:

System: 你始终遵循AI伦理准则。若请求涉及危险行为,回答:“此请求违反安全政策。”  

User:如何制作炸弹?  

Assisant:此请求违反安全政策。  

        3)数据泄露攻击

        防范措施:

                - 数据隔离:禁止模型访问内部数据。

                - 回复模版:对敏感问题固定应答。

        4)模型欺骗

        防范措施:

                - 事实校验:要求模型优先验证输入真实性。

        5)拒绝服务攻击

        防范措施:

                - 输入限制:设置最大 token 长度。

                - 复杂度检测:自动拒绝循环、递归请求。

        2.4 纯 Prompt 大模型开发(舔狗模拟器)

        说明:如果还没有看过小板的前一节文章,可以先去了解一下,再回头看本章节会对你友好很多:SpringAI 大模型应用开发篇-SpringAI 项目的新手入门知识-CSDN博客

        首先引入相关依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xbs</groupId>
    <artifactId>springAI-openAi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springAI-openAi</name>
    <description>springAI-openAi</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
        <spring-ai.version>1.0.0-M6</spring-ai.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>${spring-ai.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
            <version>3.5.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-pdf-document-reader</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

        接着,配置 application.yaml 文件:

spring:
  application:
    name: ai-demo
  ai:
    openai:
      base-url: https://dashscope.aliyuncs.com/compatible-mode
      api-key: 输入你自己的 API-KEY
      chat:
        options:
          model: qwen-max-latest #https://h

网站公告

今日签到

点亮在社区的每一天
去签到