返回顶部
热门问答 更多热门问答
技术文章 更多技术文章

一文搞懂大模型的提示工程(Text2SQL、Text2API)

[复制链接]
链载Ai 显示全部楼层 发表于 昨天 13:39 |阅读模式 打印 上一主题 下一主题

ingFang SC", system-ui, -apple-system, "system-ui", "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.578px;-webkit-tap-highlight-color: transparent;outline: 0px;visibility: visible;text-wrap: wrap;background-color: rgb(255, 255, 255);text-align: left;margin-bottom: 16px;">ingFang SC", system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;">大模型的提示工程(Prompt Engineering) 是通过精心设计输入文本(Prompt),引导大语言模型(LLM)生成符合预期输出的技术。在Text2SQL(自然语言转SQL)和Text2API(自然语言调接口)场景中,提示工程的核心目标是将自然语言问题转化为准确的 SQL 查询和具体的 API 调用参数。

ingFang SC", system-ui, -apple-system, "system-ui", "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.578px;-webkit-tap-highlight-color: transparent;outline: 0px;visibility: visible;text-wrap: wrap;background-color: rgb(255, 255, 255);text-align: left;margin-bottom: 16px;">ingFang SC", system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;">Text2SQL和Text2API的提示工程本质是是ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", "Source Han Sans CN", sans-serif, "Apple Color Emoji", "Segoe UI Emoji";list-style: none;margin-right: 0px;margin-left: 0px;scrollbar-width: none;font-weight: 600;color: rgb(6, 7, 31);font-size: 15px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(253, 253, 254);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">将领域知识显式化,通过角色定义和业务知识注入(如数据库Schema、API文档),让模型“理解”自然语言背后的真实意图,并ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", "Source Han Sans CN", sans-serif, "Apple Color Emoji", "Segoe UI Emoji";list-style: none;margin-right: 0px;margin-left: 0px;scrollbar-width: none;font-weight: 600;color: rgb(6, 7, 31);font-size: 15px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(253, 253, 254);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">将其转化为可执行的结构化指令

ingFang SC", system-ui, -apple-system, "system-ui", "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.578px;-webkit-tap-highlight-color: transparent;outline: 0px;visibility: visible;text-wrap: wrap;background-color: rgb(255, 255, 255);text-align: left;margin-bottom: 16px;">ingFang SC", system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: var(--articleFontsize);letter-spacing: 0.034em;">WTH is Prompt Engineering? - DEV Community

一、Text2SQL

ingFang SC", system-ui, -apple-system, "system-ui", "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.578px;text-wrap: wrap;background-color: rgb(255, 255, 255);-webkit-tap-highlight-color: transparent;outline: 0px;">ingFang SC", system-ui, -apple-system, "system-ui", "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: var(--articleFontsize);letter-spacing: 0.578px;text-align: left;color: rgb(172, 57, 255);">Text2SQL(文本转SQL)是什么Text2SQL是一种将自然语言描述的查询需求,自动转换为结构化查询语言(SQL)的技术。
Understanding the Ways for Text-to-SQL with LLMs | by Elmo | Generative AI
LangChain提供SQLDatabaseChain,支持将数据库Schema动态注入提示词,实现端到端的SQL生成与执行。LangChain可以自动提取数据库表结构(Schema)作为上下文。支持多轮对话修正SQL语句。
SQL | ?️? LangChain
fromlangchain.utilitiesimportSQLDatabasefromlangchain.chainsimportSQLDatabaseChainfromlangchain_community.llmsimportOpenAIdb=SQLDatabase.from_uri("sqlite:///sales.db")llm=OpenAI(temperature=0)chain=SQLDatabaseChain.from_llm(llm,db,verbose=True)response=chain.run("2024年销售额超过100万的产品是什么?")
如何实现Text2SQL?通过自然语言处理技术进行语义解析(包括实体识别、关系抽取、意图理解),结合预加载的数据库Schema信息,利用大语言模型(LLM)生成符合语法规范的SQL语句。
1. 输入解析:用户提问 → 提取关键实体(表名、字段、条件)。
例如:“统计2024年销售额超过100万的产品” → 提取“销售额(sales)”、“产品(product)”、“年份(year=2024)”、“条件(>1,000,000)”。
2. Schema绑定:结合数据库表结构(Schema),明确字段和表关系。
关键:在Prompt中提供Schema,例如:表orders: id (int), product_id (int), sales (float), date (date) 表products: id (int), name (str), category (str)
3. SQL生成:模型根据Schema和用户意图生成查询语句。
    SELECTp.name,SUM(o.sales)AStotal_salesFROMordersoJOINproductspONo.product_id=p.idWHEREYEAR(o.date)=2023GROUPBYp.idHAVINGtotal_sales>1000000;
    4. 结果验证:执行SQL并返回数据,若失败则优化Prompt或追问用户。
    No More Text2SQL, It's Now RAG2SQL! | by Angelina Yang | Medium

    二、Text2API

    Text2API(文本转API调用)是什么Text2API(文本转API调用)是一种将自然语言描述的用户需求自动转换为对应用程序接口(API)的调用请求的技术。
    Pseudo Function Calling for Gemini API Through Prompt Engineering | by  Kanshi Tanaike | Google Cloud - Community | Medium
    LangChain提供APIChain,支持将API文档(如OpenAPI/Swagger)嵌入提示词,引导大模型生成请求参数,可以支持多步骤调用(如先查询用户ID,再调用订单API)。
    fromlangchain.chainsimportAPIChainfromlangchain_community.llmsimportOpenAIllm=OpenAI(temperature=0)api_docs="""API文档:-发送邮件:POST/send_email参数:to(收件人),subject(主题),content(内容)-查询天气:GET/weather参数:city(城市),date(日期)"""chain=APIChain.from_llm_and_api_docs(llm,api_docs,verbose=True)response=chain.run("给Allen发邮件,主题是会议提醒,内容为明天下午2点开会。")
    如何实现Text2API?通过自然语言处理技术进行语义解析(含意图识别、实体抽取、上下文理解),结合预加载的API文档信息,利用大语言模型(LLM)生成符合语法规范的API调用请求。
    1. API目录管理:维护API文档(端点、参数、权限)。
    例如:邮件API文档:POST /send_email 参数:to (str), subject (str), content (str) 权限:需用户OAuth令牌
    2. 意图识别:模型解析用户指令,匹配目标API。
    例如:“给Allen发邮件,主题是项目开发进度,内容为‘本周完成80%’” → 调用/send_email
    3. 参数填充:提取并验证参数(如邮箱、内容)。
    {"endpoint":"/send_email","params":{"to":"zhangsan@company.com","subject":"项目进度","content":"本周完成80%"}}
    4. 执行与反馈:调用API并返回结果(成功/失败原因)。
        NL2SQL技术方案系列(1):NL2API、NL2SQL技术路径选择;LLM选型与Prompt工程技巧,揭秘项目落地优化之道- 汀、人工智能- 博客园

          回复

          使用道具 举报

          您需要登录后才可以回帖 登录 | 立即注册

          本版积分规则

          链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
          • 官方手机版

          • 微信公众号

          • 商务合作

          • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
          • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ