How to tune agent _executor for better understanding of the database

news2024/9/20 16:39:28

题意:如何调整agent _executor以更好地理解数据库

问题背景:

I have a database in which I have connected an agent too. However, I have noticed that it sometimes gets confused between whether or not it should return a column ID or persons first name when asked "which person sold the most....?" Is there a way to tune/adjust the create_sql_agent from langchain.agents at which I can tell the agent to not return column ID but return first and last name based on questions structured like that?

我有一个数据库,并且我也已经连接了一个智能代理。然而,我注意到当被问到“哪个人卖得最多....?”时,它有时会混淆是否应该返回列ID还是人的名字。有没有办法调整或配置langchain.agents中的create_sql_agent,以便我告诉代理不要返回列ID,而是基于这样的问题结构返回名和姓?

I think the question may be related to this post but I am unsure how to include that/and structure that properly: Training SQL Agent · langchain-ai/langchain · Discussion #9591 · GitHub

我认为这个问题可能与这篇帖子有关,但我不确定如何正确地将其包括在内/以及如何组织它:

System Info langchain-openai==0.1.3 Python 3.11.7 Windows 11

Basic Model

from langchain_openai import ChatOpenAI
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.agents import create_sql_agent
from langchain.sql_database import SQLDatabase


llm = ChatOpenAI(model_name="gpt-3.5-turbo-1106", temperature=0, openai_api_key=os.environ.get('OPENAI_API_KEY'))

toolkit = SQLDatabaseToolkit(db=db, llm=llm)

agent_executor = create_sql_agent(
    llm=llm,
    toolkit=toolkit,
    verbose=False,
    agent_type="openai-tools")


print(agent_executor.invoke("What is my data about"))

Nothing, unsure how to progress as I can not find examples.

没什么进展,因为找不到例子,所以不知道如何继续。

问题解决:

You can customize the prompts given to the LLM by passing a prompt argument to create_sql_agent function.

您可以通过向create_sql_agent函数传递一个提示参数来定制给大语言模型(LLM)的提示。

Looking at create_sql_agent source code at         langchain/libs/community/langchain_community/agent_toolkits/sql/base.py at master · langchain-ai/langchain · GitHub

在查看create_sql_agent的源代码时,

you can see if prompt is None it will create a prompt using:

你可以看到,如果promptNone,它将使用以下方式创建一个提示:

messages = [
                SystemMessage(content=cast(str, prefix)),
                HumanMessagePromptTemplate.from_template("{input}"),
                AIMessage(content=suffix or SQL_FUNCTIONS_SUFFIX),
                MessagesPlaceholder(variable_name="agent_scratchpad"),
            ]
prompt = ChatPromptTemplate.from_messages(messages)

using prefix and suffix from there:        从那里使用前缀和后缀:

from langchain_community.agent_toolkits.sql.prompt import (
    SQL_FUNCTIONS_SUFFIX,
    SQL_PREFIX,
)

So you can do the same thing to create a prompt with more customized instructions.

所以你可以做同样的事情来创建一个带有更多自定义指令的提示。

And then pass it to the agent.        然后将其传递给代理。

Here is an example with the prefix and suffix can explicitly be customized, to add a line about your issue.

这里有一个例子,可以明确自定义前缀和后缀,以添加一行关于您的问题的描述。

from langchain_core.prompts.chat import (
    ChatPromptTemplate,
    HumanMessagePromptTemplate,
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate,
    MessagesPlaceholder,
)

prefix = """
You are an agent designed to interact with a SQL database.
Given an input question, create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer.
Unless the user specifies a specific number of examples they wish to obtain, always limit your query to at most {top_k} results.
You can order the results by a relevant column to return the most interesting examples in the database.
Never query for all the columns from a specific table, only ask for the relevant columns given the question.
You have access to tools for interacting with the database.
Only use the below tools. Only use the information returned by the below tools to construct your final answer.
You MUST double check your query before executing it. If you get an error while executing a query, rewrite the query and try again.

DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database.

If the question does not seem related to the database, just return "I don't know" as the answer.
"""

suffix = """I should look at the tables in the database to see what I can query.  Then I should query the schema of the most relevant tables.
"""

messages = [
                SystemMessagePromptTemplate.from_template(prefix),
                HumanMessagePromptTemplate.from_template("{input}"),
                AIMessagePromptTemplate.from_template(suffix),
                MessagesPlaceholder(variable_name="agent_scratchpad"),
            ]
prompt = ChatPromptTemplate.from_messages(messages)

agent_executor = create_sql_agent(llm,
                                   toolkit=toolkit,
                                   agent_type="openai-tools",
                                   prompt = prompt,
                                   verbose=False)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1973599.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

5 mysql 查询语句

1.DML:对数据进行增删改查 提示:Execute执行 Execute and Suppress 执行并且抑制这个警告 person表的结构 /* DML:Data Manipulation Language 数据操作语言,对数据进行 增删改查操作,因为査询的操作太频繁和复杂,将查询的操作独立成为DQL */ use db1109;//person表在d…

智慧水务项目(二)django(drf)+angular 18 创建通用model,并对orm常用字段进行说明

一、说明 上一篇文章建立一个最简单的项目,现在我们建立一个公共模型,抽取公共字段,以便于后续模块继承,过程之中会对orm常用字段进行说明,用到的介绍一下 二、创建一个db.py 目录如下图 1、代码 from importlib im…

私域流量变迁与精细移动化趋势下的AI智能名片小程序源码应用探索

摘要:随着移动互联网技术的飞速发展,私域流量的价值日益凸显,成为企业营销战略的重要组成部分。私域流量的精细化和移动化趋势不仅改变了传统的营销格局,也为新兴技术的应用提供了广阔空间。本文深入探讨了私域流量的变迁历程&…

磁盘碎片整理工具IObit Smart Defrag PRO 绿色版

在数字化时代,电脑的性能直接影响到我们的工作效率和娱乐体验。随着使用时间的增长,硬盘上的文件碎片逐渐增多,导致电脑运行缓慢。今天,我为大家介绍一款强大的磁盘碎片整理工具——IObit Smart Defrag,它能有效解决这…

探索Python的聊天机器人世界:Errbot的魔力

文章目录 探索Python的聊天机器人世界:Errbot的魔力背景:为何选择Errbot?Errbot:Python中的聊天机器人框架安装Errbot:简单几步,即刻开始探索Errbot:五个简单函数的魔法1. 创建机器人2. 响应消息…

全球汽车用MEMS加速度计市场规划预测:未来六年CAGR为2.8%

随着汽车行业的持续发展和消费者对安全性能的需求增加,汽车用MEMS加速度计作为提升车辆安全性和稳定性的关键组件,正逐渐受到市场的广泛关注。本文旨在通过深度分析汽车用MEMS加速度计行业的各个维度,揭示行业发展趋势和潜在机会。 【市场趋…

ElementPlus 中el-select自定义指令实现触底加载请求options数据

1) 背景: 老项目翻新时,发现一个下拉框数据非常多,客户呢,希望全部数据一起展示,意思就是全部数据一起返回给前端用于展示。但这会造成明显的卡顿。~~明显的不合理! QAQ!~~ 于是压力给到前端,查询资料,各种…

论文阅读:Most Probable Densest Subgraphs

摘要 本文提出了一种在不确定图中发现最有可能稠密子图(MPDS)的新方法。不确定图中的每条边都有存在概率,使得计算稠密子图变得複杂。作者定义了稠密子图概率,并证明了计算该概率是#P难的。为了解决这个问题,设计了基…

算法通关:014_1:用栈实现队列

文章目录 题目总结代码运行结果 题目 用栈实现队列 leetcode :232 总结 时间复杂度 平均下来每个方式是O(1) 代码 class MyQueue {public Stack<Integer> in;public Stack<Integer> out;//初始化public MyQueue() {in new Stack<>();out new Stack<…

整体接口测试

文章目录 1.分类1.新增分类1.接口设计2.结果 2.更新分类1.接口设计2.结果 3.查询分类1.接口设计2.结果&#xff08;少了一个count字段暂时不改&#xff09; 4.查询大类下分类1.接口设计2.结果 5.删除分类1.接口设计2.结果 2.标签1.新增标签1.接口设计2.结果 2.更新标签1.接口设…

E5092A可配置的多端口测试仪

E5092A 可配置的多端口测试仪 多达 10 个端口的全交叉测量&#xff0c;或者最多 22 个端口的测量功能。 概述 E5092A 多端口测试仪可以灵活配置&#xff0c;并可与4 端口ENA 网络分析仪&#xff08;E5070B/E5071B/E5071C/E5080A&#xff09;结合使用&#xff0c;组成频率范…

信息安全管理中,文件销毁、硬盘销毁、数据销毁和物料销毁分别如何

文件销毁的方法主要有删除、覆盖、加密和物理破坏。硬盘销毁的方法主要有物理破坏、磁性破坏、化学破坏和高温破坏。数据销毁的方法主要有逻辑删除、物理破坏、磁性破坏和化学破坏。物料销毁的方法主要有回收利用、填埋处理、焚烧处理、生物降解和化学处理。 文件销毁的方法&a…

江协科技51单片机学习- p31 LCD1602液晶屏驱动

&#x1f680;write in front&#x1f680; &#x1f50e;大家好&#xff0c;我是黄桃罐头&#xff0c;希望你看完之后&#xff0c;能对你有所帮助&#xff0c;不足请指正&#xff01;共同学习交流 &#x1f381;欢迎各位→点赞&#x1f44d; 收藏⭐️ 留言&#x1f4dd;​…

Frida挂钩Java层代码与协议分析

Hook类中普通方法 在本文中&#xff0c;我们继续以某嘟牛应用为例&#xff0c;在用户点击登录按钮后&#xff0c;哪个方法会被调用。我们在上一篇文章中提到&#xff0c;搜索关键词 “Encrypt” 后&#xff0c;发现两个包含该字符串的 Java 方法。接下来&#xff0c;我们通过 …

AGI思考探究的意义、价值与乐趣Ⅳ

探究in context或Prompt对于LLM来说其根本意义&#xff0c;in context & Prompt Learning带给我们更深一层的提示是什么&#xff1f; 文章里的探索希望能够将in context或Prompt置身于一个更全局的视角来看待&#xff1a;将其视为在真实世界中时空认知流形所映射为数据流形…

路由器IP互联无线对讲系统解决方案

一、项目概况 随着信息化的全面深入发展&#xff0c;各行各业的通信需求日益增长&#xff0c;传统的通信方式无法满足跨网络、跨系统、跨媒介的通信互联互通&#xff0c;打破信息孤岛、提高协同效率&#xff0c;成为当前各行业融合通信的首要任务。尤其大型企业、学校、医院等…

第14课 Scratch少儿编程 入门篇:抓蝴蝶

开车自驾去旅行 故事背景&#xff1a; 春天到了&#xff0c;让我们一起春游&#xff01;公园的草坪上有五颜六色的花朵和翩翩起舞的蝴蝶&#xff0c;让我们拿起工具&#xff0c;去捉蝴蝶吧&#xff01;&#xff01; 程序原理: 添加绿色的草地角色和蝴蝶的角色&#xff0c;给…

【C++程序设计】——利用数组处理批量数据(一)

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;开发者-削好皮的Pineapple! &#x1f468;‍&#x1f4bb; hello 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍&#x1f4bb; 本文由 削好皮的Pineapple! 原创 &#x1f468;‍&#x1f4…

使用 Python 制作一个属于自己的 AI 搜索引擎

1. 使用到技术 OpenAI KEYSerper KEYBing Search 2. 原理解析 使用Google和Bing的搜搜结果交由OpenAI处理并给出回答。 3. 代码实现 import requests from lxml import etree import os from openai import OpenAI# 从环境变量中加载 API 密钥 os.environ["OPENAI_AP…

5G三大场景:eMBB、mMTC、uRLLC

1G,2G,3G,4G,5G有什么区别&#xff1f;5G的优势在哪&#xff1f;有什么应用&#xff1f; 1G,2G,3G,4G,5G有什么区别&#xff1f;5G的优势在哪&#xff1f;有什么应用&#xff1f;_3g4g5g的区别和作用-CSDN博客 从1G到4G&#xff0c;移动通信的核心是人与人之间的通信&#xff0…