langchain sql agent 案例

原文:SQL 数据库 |🦜️🔗 朗链 (langchain.com)langchainSQL 数据库 |🦜️🔗 朗链 (langchain.com)

说明:看原文,复制有问题

SQL Database

This notebook showcases an agent designed to interact with a SQL databases. The agent builds off of SQLDatabaseChain and is designed to answer more general questions about a database, as well as recover from errors.

Note that, as this agent is in active development, all answers might not be correct. Additionally, it is not guaranteed that the agent won't perform DML statements on your database given certain questions. Be careful running it on sensitive data!

This uses the example Chinook database. To set it up follow the instructions on 2 Sample Databases for SQLite, placing the .db file in a notebooks folder at the root of this repository.

Initialization​

from langchain.agents import create_sql_agent
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.sql_database import SQLDatabase
from langchain.llms.openai import OpenAI
from langchain.agents import AgentExecutor
from langchain.agents.agent_types import AgentType
from langchain.chat_models import ChatOpenAI

db = SQLDatabase.from_uri("sqlite:///../../../../../notebooks/Chinook.db")
toolkit = SQLDatabaseToolkit(db=db, llm=OpenAI(temperature=0))

Using ZERO_SHOT_REACT_DESCRIPTION

This shows how to initialize the agent using the ZERO_SHOT_REACT_DESCRIPTION agent type.

agent_executor = create_sql_agent(llm=OpenAI(temperature=0),toolkit=toolkit,verbose=True,agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
)

Using OpenAI Functions​

This shows how to initialize the agent using the OPENAI_FUNCTIONS agent type. Note that this is an alternative to the above.

# agent_executor = create_sql_agent(
#     llm=ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613"),
#     toolkit=toolkit,
#     verbose=True,
#     agent_type=AgentType.OPENAI_FUNCTIONS
# )

Disclaimer ⚠️​

The query chain may generate insert/update/delete queries. When this is not expected, use a custom prompt or create a SQL users without write permissions.

The final user might overload your SQL database by asking a simple question such as "run the biggest query possible". The generated query might look like:

SELECT * FROM "public"."users"JOIN "public"."user_permissions" ON "public"."users".id = "public"."user_permissions".user_idJOIN "public"."projects" ON "public"."users".id = "public"."projects".user_idJOIN "public"."events" ON "public"."projects".id = "public"."events".project_id;

For a transactional SQL database, if one of the table above contains millions of rows, the query might cause trouble to other applications using the same database.

Most datawarehouse oriented databases support user-level quota, for limiting resource usage.

Example: describing a table​

agent_executor.run("Describe the playlisttrack table")

    > Entering new  chain...Invoking: `list_tables_sql_db` with `{}`Album, Artist, Track, PlaylistTrack, InvoiceLine, sales_table, Playlist, Genre, Employee, Customer, Invoice, MediaTypeInvoking: `schema_sql_db` with `PlaylistTrack`CREATE TABLE "PlaylistTrack" ("PlaylistId" INTEGER NOT NULL, "TrackId" INTEGER NOT NULL, PRIMARY KEY ("PlaylistId", "TrackId"), FOREIGN KEY("TrackId") REFERENCES "Track" ("TrackId"), FOREIGN KEY("PlaylistId") REFERENCES "Playlist" ("PlaylistId"))/*3 rows from PlaylistTrack table:PlaylistId  TrackId1   34021   33891   3390*/The `PlaylistTrack` table has two columns: `PlaylistId` and `TrackId`. It is a junction table that represents the relationship between playlists and tracks. Here is the schema of the `PlaylistTrack` table:```CREATE TABLE "PlaylistTrack" ("PlaylistId" INTEGER NOT NULL, "TrackId" INTEGER NOT NULL, PRIMARY KEY ("PlaylistId", "TrackId"), FOREIGN KEY("TrackId") REFERENCES "Track" ("TrackId"), FOREIGN KEY("PlaylistId") REFERENCES "Playlist" ("PlaylistId"))```Here are three sample rows from the `PlaylistTrack` table:```PlaylistId   TrackId1            34021            33891            3390```Please let me know if there is anything else I can help you with.> Finished chain.'The `PlaylistTrack` table has two columns: `PlaylistId` and `TrackId`. It is a junction table that represents the relationship between playlists and tracks. \n\nHere is the schema of the `PlaylistTrack` table:\n\n```\nCREATE TABLE "PlaylistTrack" (\n\t"PlaylistId" INTEGER NOT NULL, \n\t"TrackId" INTEGER NOT NULL, \n\tPRIMARY KEY ("PlaylistId", "TrackId"), \n\tFOREIGN KEY("TrackId") REFERENCES "Track" ("TrackId"), \n\tFOREIGN KEY("PlaylistId") REFERENCES "Playlist" ("PlaylistId")\n)\n```\n\nHere are three sample rows from the `PlaylistTrack` table:\n\n```\nPlaylistId   TrackId\n1            3402\n1            3389\n1            3390\n```\n\nPlease let me know if there is anything else I can help you with.'

Example: describing a table, recovering from an error​

In this example, the agent tries to search for a table that doesn't exist, but finds the next best result

agent_executor.run("Describe the playlistsong table")

    > Entering new AgentExecutor chain...Action: list_tables_sql_dbAction Input: ""Observation: Genre, PlaylistTrack, MediaType, Invoice, InvoiceLine, Track, Playlist, Customer, Album, Employee, ArtistThought: I should look at the schema of the PlaylistSong tableAction: schema_sql_dbAction Input: "PlaylistSong"Observation: Error: table_names {'PlaylistSong'} not found in databaseThought: I should check the spelling of the tableAction: list_tables_sql_dbAction Input: ""Observation: Genre, PlaylistTrack, MediaType, Invoice, InvoiceLine, Track, Playlist, Customer, Album, Employee, ArtistThought: The table is called PlaylistTrackAction: schema_sql_dbAction Input: "PlaylistTrack"Observation: CREATE TABLE "PlaylistTrack" ("PlaylistId" INTEGER NOT NULL, "TrackId" INTEGER NOT NULL, PRIMARY KEY ("PlaylistId", "TrackId"), FOREIGN KEY("TrackId") REFERENCES "Track" ("TrackId"), FOREIGN KEY("PlaylistId") REFERENCES "Playlist" ("PlaylistId"))SELECT * FROM 'PlaylistTrack' LIMIT 3;PlaylistId TrackId1 34021 33891 3390Thought: I now know the final answerFinal Answer: The PlaylistTrack table contains two columns, PlaylistId and TrackId, which are both integers and are used to link Playlist and Track tables.> Finished chain.'The PlaylistTrack table contains two columns, PlaylistId and TrackId, which are both integers and are used to link Playlist and Track tables.'

Example: running queries​

agent_executor.run("List the total sales per country. Which country's customers spent the most?"
)

    > Entering new AgentExecutor chain...Action: list_tables_sql_dbAction Input: ""Observation: Invoice, MediaType, Artist, InvoiceLine, Genre, Playlist, Employee, Album, PlaylistTrack, Track, CustomerThought: I should look at the schema of the relevant tables to see what columns I can use.Action: schema_sql_dbAction Input: "Invoice, Customer"Observation: CREATE TABLE "Customer" ("CustomerId" INTEGER NOT NULL, "FirstName" NVARCHAR(40) NOT NULL, "LastName" NVARCHAR(20) NOT NULL, "Company" NVARCHAR(80), "Address" NVARCHAR(70), "City" NVARCHAR(40), "State" NVARCHAR(40), "Country" NVARCHAR(40), "PostalCode" NVARCHAR(10), "Phone" NVARCHAR(24), "Fax" NVARCHAR(24), "Email" NVARCHAR(60) NOT NULL, "SupportRepId" INTEGER, PRIMARY KEY ("CustomerId"), FOREIGN KEY("SupportRepId") REFERENCES "Employee" ("EmployeeId"))SELECT * FROM 'Customer' LIMIT 3;CustomerId FirstName LastName Company Address City State Country PostalCode Phone Fax Email SupportRepId1 Luís Gonçalves Embraer - Empresa Brasileira de Aeronáutica S.A. Av. Brigadeiro Faria Lima, 2170 São José dos Campos SP Brazil 12227-000 +55 (12) 3923-5555 +55 (12) 3923-5566 luisg@embraer.com.br 32 Leonie Köhler None Theodor-Heuss-Straße 34 Stuttgart None Germany 70174 +49 0711 2842222 None leonekohler@surfeu.de 53 François Tremblay None 1498 rue Bélanger Montréal QC Canada H2G 1A7 +1 (514) 721-4711 None ftremblay@gmail.com 3CREATE TABLE "Invoice" ("InvoiceId" INTEGER NOT NULL, "CustomerId" INTEGER NOT NULL, "InvoiceDate" DATETIME NOT NULL, "BillingAddress" NVARCHAR(70), "BillingCity" NVARCHAR(40), "BillingState" NVARCHAR(40), "BillingCountry" NVARCHAR(40), "BillingPostalCode" NVARCHAR(10), "Total" NUMERIC(10, 2) NOT NULL, PRIMARY KEY ("InvoiceId"), FOREIGN KEY("CustomerId") REFERENCES "Customer" ("CustomerId"))SELECT * FROM 'Invoice' LIMIT 3;InvoiceId CustomerId InvoiceDate BillingAddress BillingCity BillingState BillingCountry BillingPostalCode Total1 2 2009-01-01 00:00:00 Theodor-Heuss-Straße 34 Stuttgart None Germany 70174 1.982 4 2009-01-02 00:00:00 Ullevålsveien 14 Oslo None Norway 0171 3.963 8 2009-01-03 00:00:00 Grétrystraat 63 Brussels None Belgium 1000 5.94Thought: I should query the Invoice and Customer tables to get the total sales per country.Action: query_sql_dbAction Input: SELECT c.Country, SUM(i.Total) AS TotalSales FROM Invoice i INNER JOIN Customer c ON i.CustomerId = c.CustomerId GROUP BY c.Country ORDER BY TotalSales DESC LIMIT 10Observation: [('USA', 523.0600000000003), ('Canada', 303.9599999999999), ('France', 195.09999999999994), ('Brazil', 190.09999999999997), ('Germany', 156.48), ('United Kingdom', 112.85999999999999), ('Czech Republic', 90.24000000000001), ('Portugal', 77.23999999999998), ('India', 75.25999999999999), ('Chile', 46.62)]Thought: I now know the final answerFinal Answer: The customers from the USA spent the most, with a total of $523.06.> Finished chain.'The customers from the USA spent the most, with a total of $523.06.'

agent_executor.run("Show the total number of tracks in each playlist. The Playlist name should be included in the result."
)

    > Entering new AgentExecutor chain...Action: list_tables_sql_dbAction Input: ""Observation: Invoice, MediaType, Artist, InvoiceLine, Genre, Playlist, Employee, Album, PlaylistTrack, Track, CustomerThought: I should look at the schema of the Playlist and PlaylistTrack tables to see what columns I can use.Action: schema_sql_dbAction Input: "Playlist, PlaylistTrack"Observation: CREATE TABLE "Playlist" ("PlaylistId" INTEGER NOT NULL, "Name" NVARCHAR(120), PRIMARY KEY ("PlaylistId"))SELECT * FROM 'Playlist' LIMIT 3;PlaylistId Name1 Music2 Movies3 TV ShowsCREATE TABLE "PlaylistTrack" ("PlaylistId" INTEGER NOT NULL, "TrackId" INTEGER NOT NULL, PRIMARY KEY ("PlaylistId", "TrackId"), FOREIGN KEY("TrackId") REFERENCES "Track" ("TrackId"), FOREIGN KEY("PlaylistId") REFERENCES "Playlist" ("PlaylistId"))SELECT * FROM 'PlaylistTrack' LIMIT 3;PlaylistId TrackId1 34021 33891 3390Thought: I can use a SELECT statement to get the total number of tracks in each playlist.Action: query_checker_sql_dbAction Input: SELECT Playlist.Name, COUNT(PlaylistTrack.TrackId) AS TotalTracks FROM Playlist INNER JOIN PlaylistTrack ON Playlist.PlaylistId = PlaylistTrack.PlaylistId GROUP BY Playlist.NameObservation: SELECT Playlist.Name, COUNT(PlaylistTrack.TrackId) AS TotalTracks FROM Playlist INNER JOIN PlaylistTrack ON Playlist.PlaylistId = PlaylistTrack.PlaylistId GROUP BY Playlist.NameThought: The query looks correct, I can now execute it.Action: query_sql_dbAction Input: SELECT Playlist.Name, COUNT(PlaylistTrack.TrackId) AS TotalTracks FROM Playlist INNER JOIN PlaylistTrack ON Playlist.PlaylistId = PlaylistTrack.PlaylistId GROUP BY Playlist.Name LIMIT 10Observation: [('90’s Music', 1477), ('Brazilian Music', 39), ('Classical', 75), ('Classical 101 - Deep Cuts', 25), ('Classical 101 - Next Steps', 25), ('Classical 101 - The Basics', 25), ('Grunge', 15), ('Heavy Metal Classic', 26), ('Music', 6580), ('Music Videos', 1)]Thought: I now know the final answer.Final Answer: The total number of tracks in each playlist are: '90’s Music' (1477), 'Brazilian Music' (39), 'Classical' (75), 'Classical 101 - Deep Cuts' (25), 'Classical 101 - Next Steps' (25), 'Classical 101 - The Basics' (25), 'Grunge' (15), 'Heavy Metal Classic' (26), 'Music' (6580), 'Music Videos' (1).> Finished chain."The total number of tracks in each playlist are: '90’s Music' (1477), 'Brazilian Music' (39), 'Classical' (75), 'Classical 101 - Deep Cuts' (25), 'Classical 101 - Next Steps' (25), 'Classical 101 - The Basics' (25), 'Grunge' (15), 'Heavy Metal Classic' (26), 'Music' (6580), 'Music Videos' (1)."

Recovering from an error​

In this example, the agent is able to recover from an error after initially trying to access an attribute (Track.ArtistId) which doesn't exist.

agent_executor.run("Who are the top 3 best selling artists?")

    > Entering new AgentExecutor chain...Action: list_tables_sql_dbAction Input: ""Observation: MediaType, Track, Invoice, Album, Playlist, Customer, Employee, InvoiceLine, PlaylistTrack, Genre, ArtistThought: I should look at the schema of the Artist, InvoiceLine, and Track tables to see what columns I can use.Action: schema_sql_dbAction Input: "Artist, InvoiceLine, Track"Observation: CREATE TABLE "Artist" ("ArtistId" INTEGER NOT NULL, "Name" NVARCHAR(120), PRIMARY KEY ("ArtistId"))SELECT * FROM 'Artist' LIMIT 3;ArtistId Name1 AC/DC2 Accept3 AerosmithCREATE TABLE "Track" ("TrackId" INTEGER NOT NULL, "Name" NVARCHAR(200) NOT NULL, "AlbumId" INTEGER, "MediaTypeId" INTEGER NOT NULL, "GenreId" INTEGER, "Composer" NVARCHAR(220), "Milliseconds" INTEGER NOT NULL, "Bytes" INTEGER, "UnitPrice" NUMERIC(10, 2) NOT NULL, PRIMARY KEY ("TrackId"), FOREIGN KEY("MediaTypeId") REFERENCES "MediaType" ("MediaTypeId"), FOREIGN KEY("GenreId") REFERENCES "Genre" ("GenreId"), FOREIGN KEY("AlbumId") REFERENCES "Album" ("AlbumId"))SELECT * FROM 'Track' LIMIT 3;TrackId Name AlbumId MediaTypeId GenreId Composer Milliseconds Bytes UnitPrice1 For Those About To Rock (We Salute You) 1 1 1 Angus Young, Malcolm Young, Brian Johnson 343719 11170334 0.992 Balls to the Wall 2 2 1 None 342562 5510424 0.993 Fast As a Shark 3 2 1 F. Baltes, S. Kaufman, U. Dirkscneider & W. Hoffman 230619 3990994 0.99CREATE TABLE "InvoiceLine" ("InvoiceLineId" INTEGER NOT NULL, "InvoiceId" INTEGER NOT NULL, "TrackId" INTEGER NOT NULL, "UnitPrice" NUMERIC(10, 2) NOT NULL, "Quantity" INTEGER NOT NULL, PRIMARY KEY ("InvoiceLineId"), FOREIGN KEY("TrackId") REFERENCES "Track" ("TrackId"), FOREIGN KEY("InvoiceId") REFERENCES "Invoice" ("InvoiceId"))SELECT * FROM 'InvoiceLine' LIMIT 3;InvoiceLineId InvoiceId TrackId UnitPrice Quantity1 1 2 0.99 12 1 4 0.99 13 2 6 0.99 1Thought: I should query the database to get the top 3 best selling artists.Action: query_sql_dbAction Input: SELECT Artist.Name, SUM(InvoiceLine.Quantity) AS TotalQuantity FROM Artist INNER JOIN Track ON Artist.ArtistId = Track.ArtistId INNER JOIN InvoiceLine ON Track.TrackId = InvoiceLine.TrackId GROUP BY Artist.Name ORDER BY TotalQuantity DESC LIMIT 3Observation: Error: (sqlite3.OperationalError) no such column: Track.ArtistId[SQL: SELECT Artist.Name, SUM(InvoiceLine.Quantity) AS TotalQuantity FROM Artist INNER JOIN Track ON Artist.ArtistId = Track.ArtistId INNER JOIN InvoiceLine ON Track.TrackId = InvoiceLine.TrackId GROUP BY Artist.Name ORDER BY TotalQuantity DESC LIMIT 3](Background on this error at: https://sqlalche.me/e/14/e3q8)Thought: I should double check my query before executing it.Action: query_checker_sql_dbAction Input: SELECT Artist.Name, SUM(InvoiceLine.Quantity) AS TotalQuantity FROM Artist INNER JOIN Track ON Artist.ArtistId = Track.ArtistId INNER JOIN InvoiceLine ON Track.TrackId = InvoiceLine.TrackId GROUP BY Artist.Name ORDER BY TotalQuantity DESC LIMIT 3Observation: SELECT Artist.Name, SUM(InvoiceLine.Quantity) AS TotalQuantity FROM Artist INNER JOIN Track ON Artist.ArtistId = Track.ArtistId INNER JOIN InvoiceLine ON Track.TrackId = InvoiceLine.TrackId GROUP BY Artist.Name ORDER BY TotalQuantity DESC LIMIT 3;Thought: I now know the final answer.Action: query_sql_dbAction Input: SELECT Artist.Name, SUM(InvoiceLine.Quantity) AS TotalQuantity FROM Artist INNER JOIN Album ON Artist.ArtistId = Album.ArtistId INNER JOIN Track ON Album.AlbumId = Track.AlbumId INNER JOIN InvoiceLine ON Track.TrackId = InvoiceLine.TrackId GROUP BY Artist.Name ORDER BY TotalQuantity DESC LIMIT 3Observation: [('Iron Maiden', 140), ('U2', 107), ('Metallica', 91)]Thought: I now know the final answer.Final Answer: The top 3 best selling artists are Iron Maiden, U2, and Metallica.> Finished chain.'The top 3 best selling artists are Iron Maiden, U2, and Metallica.'

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

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

相关文章

如何使用visual studio 2010构建SQLite3.lib文件

sqlite3官网只提供了dll,并没有lib文件。需要自己生成sqlite3.lib。因项目升级到x64,以前并没有生成64位的链接库,需要自己创建。本人电脑操作系统windows 10, 开发环境为visual studio 2010。下面是详细生成过程。 1. 从源下载源&#xff08…

Spring中静态代理设计模式

目录 一、为什么需要代理设计模式 二、代理设计模式 三、静态代理设计模式 3.1 存在的问题 一、为什么需要代理设计模式 在项目的开发过程中我们知道service层是整个项目中最重要的部分,在service中一般会有两个部分,一个是核心业务,一个是额…

力扣每日一题54:螺旋矩阵

题目描述: 给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。 示例 1: 输入:matrix [[1,2,3],[4,5,6],[7,8,9]] 输出:[1,2,3,6,9,8,7,4,5]示例 2: 输入&#…

mk语法示例

这里写自定义目录标题 欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants 创建一个自定义列表如何创建一个…

『ARM』和『x86』处理器架构解析指南

前言 如果问大家是否知道 CPU,我相信不会得到否定的答案,但是如果继续问大家是否了解 ARM 和 X86 架构,他们的区别又是什么,相信可能部分人就会哑口无言了 目前随着深度学习、高性能计算、NLP、AIGC、GLM、AGI 的技术迭代&#…

自然语言处理---RNN、LSTM、GRU模型

RNN模型 RNN模型概述 RNN(Recurrent Neural Network),中文称作循环神经网络,它一般以序列数据为输入,通过网络内部的结构设计有效捕捉序列之间的关系特征,一般也是以序列形式进行输出。RNN的循环机制使模型隐层上一时间步产生的…

MSP430F5529时钟系统配置

1、为什么要进行时钟管理?   时钟系统是一个数字器件的命脉,对于普通的51单片机来说,它的时钟来源只有外部晶振,然后每12个振荡周期完成一个基本操作,所以也叫做12T单片机,但对于当前高级一点的单片机来…

Windows系统使用powershell查找并删除重复文件

使用powershell工具,根据文件的MD5值查找多个文件夹里的重复文件,并删除重复的文件只保留文件名最长的文件。 编写运行.ps1文件 新建文本文档,输入如下内容: # 指定文件夹路径 $folderPath "D:\111"# 获取文件夹中的…

Wireshark-win32-1.8.4 给winxp和win2003用

Index of /wireshark/win32/all-versions/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror https://mirrors4.tuna.tsinghua.edu.cn/wireshark/win32/all-versions/Wireshark-win32-1.12.1.exehttps://mirrors4.tuna.tsinghua.edu.cn/wireshark/win32/all-versions/W…

Windows快速安装pandora-chatgpt

如果你没有python环境,请安装python3.7的版本,如果你安装了请安装3.7以上,包括pip这里提供下载地址3.7.2版本为例: https://www.python.org/ftp/python/3.7.2/python-3.7.2rc1-amd64.exe 有些时候用不了官网的镜像安装模块就用阿里…

Spring5学习笔记—高级注解

✅作者简介:大家好,我是Leo,热爱Java后端开发者,一个想要与大家共同进步的男人😉😉 🍎个人主页:Leo的博客 💞当前专栏: Spring专栏 ✨特色专栏: M…

Python 创建或读取 Excel 文件

Excel是一种常用的电子表格软件,广泛应用于金融、商业和教育等领域。它提供了强大的数据处理和分析功能,可进行各种计算和公式运算,并能创建各种类型的图表和可视化数据。Excel的灵活性使其成为处理和管理数据的重要工具。本文将介绍如何使用…

【微信小程序】6天精准入门(第5天:利用案例与后台的数据交互)附源码

一、什么是后台交互? 在小程序中,与后台交互指的是小程序前端与后台服务器之间的数据通信和请求处理过程。通过与后台交互,小程序能够获取服务器端的数据、上传用户数据、发送请求等。 小程序与后台交互可以实现数据的传输、用户认证、实时消…

什么是Sectigo证书?

Sectigo证书,早前被称为Comodo证书,是一种SSL(安全套接层)证书,用于保护互联网上的数据传输的安全性和隐私性。这些证书由全球领先的SSL证书颁发机构Sectigo颁发,被广泛用于网站、应用程序和服务器上。本文…

hexo发生错误 Error: Spawn failed

错误描述 仓库中有东西,运行如下命令后报错 hexo d报错提示: 原因分析: 看别人的博客是用git进行push或hexo d的时候改变了一些.deploy_git文件下的内容,这个.deploy_git的内容对于hexo来说可能是系统文件,这里挖坑 解决办法 一个个的…

分布式Trace:横跨几十个分布式组件的慢请求要如何排查?

目录 前言 一、问题的出现? 二、一体化架构中的慢请求排查如何做 三、分布式 Trace原理 四、如何来做分布式 Trace 前言 在分布式服务架构下,一个 Web 请求从网关流入,有可能会调用多个服务对请求进行处理,拿到最终结果。这个…

统计射击比赛成绩

题目描述 给定一个射击比赛成绩单,包含多个选手若干次射击的成绩分数,请对每个选手按其最高3个分数之和进行降序排名,输出降序排名后的选手ID序列。 条件如下 ① 一个选手可以有多个射击成绩的分数,且次序不固定。 ② 如果一个选手成绩少于3个,则认为选手的所有成绩无效…

Kotlin中类型转换

在 Kotlin 中,类型转换是一种常见的操作,用于将一个数据类型转换为另一个数据类型。在本篇博客中,我们将介绍 Kotlin 中的类型转换,并提供示例代码演示智能类型转换、强制类型转换以及可空类型的转换。 智能类型转换是 Kotlin 中…

自然语言处理---注意力机制

注意力概念 观察事物时,之所以能够快速判断一种事物(当然允许判断是错误的),是因为大脑能够很快把注意力放在事物最具有辨识度的部分从而作出判断,而并非是从头到尾的观察一遍事物后,才能有判断结果。正是基于这样的理论&#xf…

函数和执行上下文

一.变量提升与函数提升 变量提升:通过var关键字定义(声明)的变量,在定义语句之前就可以访问到,只不过其值是undefined 函数提升:通过function声明的函数,在之前就可以调用,值是函数…