langchain4j 学习系列(5)-RAG
继续我们的langchain4j之旅,今天来看看RAG如何实现,“RAG萌宠新手盆友们”建议先看看B站大佬的视频RAG 工作机制详解—哔哩哔哩_bilibili,核心步骤就是下面这3张图:



最简单的RAG hello-world
1、分片/索引
这里embedding模型,我们用ollamal加载“nomic-embed-text:latest”, 项目的yaml配置参考下面:
# Ollama配置 ollama: base-url: http://localhost:11434 model: deepseek-v3.1:671b-cloud embedding-model: nomic-embed-text:latest timeout: 60
示例代码:
EmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
@Autowired
@Qualifier("ollamaEmbeddingModel")
OllamaEmbeddingModel ollamaEmbeddingModel;
@GetMapping(value = "/embed/memory", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> embedMemory() {
try {
TextSegment segment1 = TextSegment.from("我喜欢打乒乓球");
Embedding embedding1 = ollamaEmbeddingModel.embed(segment1).content();
embeddingStore.add(embedding1, segment1);
TextSegment segment2 = TextSegment.from("张三是个程序员");
Embedding embedding2 = ollamaEmbeddingModel.embed(segment2).content();
embeddingStore.add(embedding2, segment2);
return ResponseEntity.ok("{\"code\":\"success\"}");
} catch (Exception e) {
log.error("embed-in-memory", e);
return ResponseEntity.ok("{\"error\":\"embed in-memory error: " + e.getMessage() + "\"}");
}
}
略做解释:
- 为了简单起见, 这里分片我们略过,直接手动用二个句子,当成2个分片
- 使用langchang4j自带的“内存”向量存储(可以很容易替换成其它向量数据库存储)
2、召回验证
@GetMapping(value = "/query/memory", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> queryInMemory(@RequestParam(required = false) String query) {
try {
if (!StringUtils.hasText(query)) {
query = "我最喜欢的运动是什么?";
}
Embedding queryEmbedding = ollamaEmbeddingModel.embed(query).content();
EmbeddingSearchRequest embeddingSearchRequest = EmbeddingSearchRequest.builder()
.queryEmbedding(queryEmbedding)
.maxResults(1)
.build();
List<EmbeddingMatch<TextSegment>> matches = embeddingStore.search(embeddingSearchRequest).matches();
EmbeddingMatch<TextSegment> embeddingMatch = matches.get(0);
return ResponseEntity.ok("{\"score\":" + embeddingMatch.score() + "\",\"text\":\"" + embeddingMatch.embedded().text() + "\"}");
} catch (Exception e) {
log.error("query-in-memory", e);
return ResponseEntity.ok("{\"error\":\"" + e.getMessage() + "\"}");
}
}
验证方法:
先访问 /embed/memory

日志里能看到类似输出:
2025-12-03T20:49:25.134+08:00 INFO 2004 --- [langchain4j-study] [nio-8080-exec-2] d.l.http.client.log.LoggingHttpClient : HTTP request:
- method: POST
- url: http://localhost:11434/api/embed
- headers: [Content-Type: application/json]
- body: {
"model" : "nomic-embed-text:latest",
"input" : [ "我喜欢打乒乓球" ]
}
2025-12-03T20:49:25.998+08:00 INFO 2004 --- [langchain4j-study] [nio-8080-exec-2] d.l.http.client.log.LoggingHttpClient : HTTP response:
- status code: 200
- headers: [content-type: application/json; charset=utf-8], [date: Wed, 03 Dec 2025 12:49:25 GMT], [transfer-encoding: chunked]
- body: {"model":"nomic-embed-text:latest","embeddings":[[-0.012458213,0.0039172457,-0.12891848,0.017662216,0.025300438,0.024325758,-0.0036070538,-0.033644814,-0.0381311,-0.03634163,-0.05963595,0.054057397,-0.03499457,0.018935377,-0.008374583,-0.034164727,-0.0049227136,-0.06220498,-0.08091822,0.09595101,-0.05103614,-0.0136290835,-0.021773232,-0.013128546,0.15322413,0.012732275,0.015309959,0.040424943,-0.00033224112,0.01658926,-0.012258051,-0.013984016,-0.0032665338,0.0071487525,-0.06581794,-0.0094845425,-0.020760503,-0.015683724,0.077724874,-0.0058224904,0.009427068,0.044845946,0.02461985,-0.024601495,0.09705044,-0.0011949927,0.0046018264,0.024254085,0.081773125,-0.01725117,-0.015873862,0.02042173,0.011301929,-0.039436378,0.057020035,0.017206708,-0.020540748,-0.049172606,-0.023007197,0.018489506,0.05498273,0.037677817,0.004085807,0.024920072,0.038203474,-0.02519353,-0.008932274,0.033442993,0.037548456,-0.0081279045,0.05669283,-0.022172932,0.0041421456,0.05737762,-0.022708746,-0.03461972,0.015064465,0.043800466,0.012183848,-0.004566944,-0.0036377572,-0.013947094,0.08149233,-0.0016764933,0.0054556034,0.0058031534,0.015527312,-0.0035933307,-0.08838505,0.029139869,-0.006777496,0.0077084485,0.019112136,0.033412933,-0.0480321,-0.012102348,-0.004239011,0.061032787,-0.0579935,-0.009473412,-0.03933967,-0.0043621548,0.023765758,0.0032002365,0.0425869,0.026631407,0.021958495,0.012563806,-0.028455937,0.012312696,-0.002447975,0.051577017,-0.019399943,0.03240589,0.005346467,-0.037924852,0.025513943,-0.03641099,-0.01771762,0.030995507,-0.045378692,-0.0072853183,0.01496905,0.033930972,0.047220357,0.001891088,-0.043199636,0.025126396,0.0082712,-0.014336107,0.02299608,0.03695494,-0.031902455,-0.014891659,-0.04119217,-0.0126992455,-0.033002827,-0.04176258,0.01696001,0.012815302,-0.010139981,0.02541227,0.026224406,-0.08467736,-0.01036433,-0.021439519,0.034442775,-0.07415152,-0.07627749,0.015752453,0.007223294,0.01375651,0.0011124315,0.021325773,0.02891942,-0.018423207,-0.008344011,0.020883271,0.009631505,0.024712183,0.017345998,0.0029280796,-0.03804128,0.0068171904,0.0008997442,-0.06083421,-0.007373738,0.07257523,0.02059963,0.02534663,-0.04393039,-0.02154865,0.013769385,-0.004244279,-0.04271552,0.0022474776,-0.018932758,-0.046988882,-0.028639391,0.052725717,0.04184288,-0.032908622,0.013641719,-0.024019161,-0.06525277,-0.008747925,0.002658261,0.0054255393,-0.009330522,-0.030206269,0.010751471,-0.0045538982,-0.02103397,-0.008394027,-0.0061083557,-0.06730141,-0.016043691,0.016352192,-0.003351765,-0.042771764,-0.016877415,-0.014862734,-0.02238862,-0.044956047,-0.016563887,-0.005472391,0.025998248,0.030923538,0.0114779575,-0.008590849,0.055101596,-0.045934986,-0.016732357,0.0017127717,-0.01398391,0.009828012,0.0013537256,-0.04638824,-0.09776861,0.032502655,0.03829003,0.021019036,-0.022623325,-0.0063213673,0.06002845,-0.0033687477,0.0025381974,-0.008064681,0.025479037,-0.031904727,-0.020291261,-0.040521063,0.106739186,0.06693403,-0.07955313,0.061153132,0.0010594398,0.098829836,-0.021282706,0.035282467,-0.0055606607,-0.006739511,0.010749998,-0.025116479,-0.030600592,-0.012364566,0.014683787,-0.027038267,0.007232086,0.022181682,0.024009116,-0.0074885017,-0.001418513,0.057900473,0.021429744,-0.052669637,-0.021409081,0.04249021,-0.00037090518,-0.004339607,0.005603592,-0.07505218,-0.009280494,-0.038106687,-0.0122500025,-0.01169743,-0.012411175,0.01995531,-0.009089314,0.010376189,0.033866882,0.05661189,0.004106048,0.055462204,-0.04531201,-0.006846949,0.022010498,-0.031004658,-0.061774857,0.012150931,0.030378802,-0.013861524,0.033796314,0.026777264,-0.0005726319,0.009243347,-0.049827427,-0.019435763,0.0015726548,0.0010323931,0.022491656,0.0013827641,-0.006789208,0.0655327,0.024350733,0.013471634,0.048357487,0.005508278,0.003927951,0.024350954,0.057273958,0.029133383,0.030226987,-0.022165632,0.015017607,0.016747091,0.08473727,-0.004313997,0.009891569,-0.031435654,-0.06068177,-0.0071860636,-0.015852293,0.04086709,-0.09022679,0.028333658,0.03241014,-0.013437933,0.026222765,0.0017604269,0.00016218411,-0.053620227,0.03844302,-0.041051988,-0.04597637,0.056273703,0.026004747,0.037739772,-0.001115858,-0.038443428,-0.03239269,0.03821732,0.044401653,-0.023431325,-0.0035764996,0.0440406,0.017453695,-0.010676289,-0.04152656,0.07972034,0.016837802,0.0046362695,0.036325917,-0.025703505,-0.023982164,-0.014884179,-0.06366106,0.02458128,0.02455616,-0.014446463,-0.020697251,0.01716094,-0.03154391,-0.02452164,0.010915117,0.014459898,0.016768359,0.036897838,0.0031146673,-0.009332912,0.043560322,0.01723151,-0.015456339,0.08636779,-0.0039273244,0.075721465,0.01884202,-0.011837652,0.016794553,0.020799648,-0.05813417,-0.037899256,0.005361145,0.0045944178,-0.018389683,0.00974621,-0.033685226,-0.0018594693,-0.044432137,0.022150835,-0.02758174,-0.021186957,-0.03605525,0.005528283,0.026348246,0.0138723,0.04552078,-0.018413119,-0.016553754,0.018572886,-0.0235327,-0.05108994,-0.024805017,-0.0030415633,0.045106664,-0.06671464,0.039470863,0.021384079,0.03922674,-0.024490539,-0.04223655,-0.034145657,0.019078305,0.005128247,-0.03674582,0.03879852,-0.027496213,-0.03526551,0.10097044,0.023621734,-0.012370205,0.019312168,-0.0070257518,-0.01720142,0.017612759,0.059788615,0.054412846,-0.03406933,-0.00048177666,-0.025571667,0.062171713,0.05565147,-0.0050585764,-0.015036547,0.013989498,0.06309631,0.016413543,0.022786321,0.026776407,-0.06795984,-0.030496506,0.050110687,-0.005006996,-0.03155094,0.012153403,0.026498858,0.062840454,0.026858954,-0.016728176,0.017119631,0.024262348,-0.03273679,-0.05917213,0.002792444,0.015854167,0.057342466,0.099889815,-0.06583564,-0.04930119,0.011296933,0.050080363,0.0015024358,0.017423032,-0.010358372,0.11626378,-0.012852813,-0.024912443,-0.011838098,-0.011310713,0.053885236,0.042620707,0.038809292,-0.043121047,0.01727294,0.0013542941,-0.008939246,0.041300874,-0.008536322,0.06488855,0.0259599,-0.025463998,0.02332791,0.037637122,0.0071708746,0.055198174,0.03735143,-0.00835376,-0.045780007,-0.04519846,0.043139964,0.028848058,-0.0015656946,-0.04863766,-0.057991054,0.0111059975,0.0315183,0.018076116,-0.0020678712,-0.018948572,-0.0073253014,0.036832582,0.001824912,-0.01599255,-0.014958996,-0.028188951,-0.0021525715,-0.033312913,0.021741705,0.015192562,0.02709798,-0.03291035,-0.051742394,-0.021989953,0.020064257,0.044471465,0.031046756,0.004689247,-0.041490443,-0.05656114,-0.010617029,-0.009052025,-0.023404304,-0.007283982,0.009609758,0.008982582,-0.0218624,0.06646375,-0.044257894,-0.076200694,-0.028197767,-0.0075237863,-0.07606948,-0.034412477,0.013161237,-0.07200784,-0.00095287035,-0.0252759,-0.07993159,0.082565226,0.002167284,0.031716116,-0.011083042,-0.01808169,-0.033488777,-0.0037027437,0.013494024,0.024234366,-0.0130011225,-0.015972845,0.044646297,-0.008054673,0.004797913,0.046461098,0.0052690976,0.015779227,0.025032466,-0.04696594,0.055714898,-0.001834367,-0.01702806,0.016205909,-0.027968574,-0.0126328645,-0.01624707,-0.014808208,-0.016704042,-0.018611336,0.023497444,-0.06933934,-0.046954677,0.0050912835,0.0065386826,-0.01825594,0.003465947,0.06754617,-0.06800963,0.009375227,0.02533909,0.0013262947,-0.02603026,-0.012035954,-0.02935515,-0.021697167,-0.02091626,-0.021321377,-0.0031186286,0.020498483,-0.018332722,-0.07044214,-0.028674757,0.039714664,-0.01988052,-0.058011863,-0.017483708,0.0023328592,0.00042041278,0.03611158,-0.087743565,0.058773745,-0.023885911,-0.025961636,0.018797195,0.031063864,0.006790029,0.0029309585,-0.00043652096,-0.036464747,-0.051174376,-0.02380469,-0.024914507,0.060109347,0.028371701,0.051849198,-0.08653098,0.03195379,0.06653195,-0.021380406,0.03630037,-0.00034705765,0.0041278903,0.028880866,0.01904683,0.011841076,-0.026176458,0.041941054,-0.03530494,0.041139983,0.024389593,-0.011736747,-0.04638311,-0.028486596,-0.06357138,0.052638907,-0.022152139,0.04588967,-0.020598596,-0.013723542,-0.03878669,-0.07934631,-0.0021311329,-0.01793114,0.011704245,-0.0572209,0.031903088,-0.033560295,0.018312301,0.029658461,0.01788086,-0.008984239,0.031808227,0.046142854,0.038308267,-0.0190473,0.058701087,0.0005623742,-0.0182426,-0.034167368,-0.015364325,0.049755238,-0.02068832,0.09164519,0.046271507,0.04853209,0.03251203,0.003244886,-0.011197777,0.015368255,-0.06699817,-0.04379445,-0.017225763,0.018064016,0.025777014,-0.012755728,-0.035268813,0.012360913,0.0113154845,-0.023607794,-0.040491693,-0.0756118,0.0008602258,0.017135924,-0.005171128,-0.03563974,-0.04375687,0.0054057883,0.027626062,0.008358948,0.026954327,-0.0030281355,0.045946553,-0.0648394,-0.045807637,0.014000194,-0.016671158,-0.0091330465,-0.039655656,0.08367981,-0.05149778,-0.036708914,-0.040108822,-0.037377384,-0.056931857,-0.047816403,-0.017870458,-0.008247641,-0.0050499053,-0.048646502,0.010743838,-0.046634644,0.05545778,0.043695174,0.016826479,0.054618385,0.033485923,-0.010118012,-0.04240243,0.022392223,0.020765718,-0.012698481,-0.022561735,-0.012372884,0.04026567,-0.03613239,0.055380546,0.035113573,0.026327785,-0.055418078,0.028260835,0.00087006803,0.06464325,0.0013291327,0.00142253,-0.060858183,-0.03018637,-0.05509179,-0.02376558,0.024877256,-0.0004114037,0.04209435,0.00080005894,0.0031264652,0.02518459,-0.012287199,-0.0001839128,0.012721054,0.047496233,0.01263706,-0.009802294,-0.090985246,-0.03363164,-0.028485274,-0.00029657417,0.019560788,0.0650089,-0.00832356,-0.022838222,-0.0042198263,0.06196195,0.016242018,-0.01520719,0.0086057475,0.013810887,-0.039997358,0.0014321145,-0.01297491,-0.015002875,-0.023154167,0.069985226,0.085558236,0.017133577,0.030067587,-0.020229042,-0.029827058,0.010278732,0.017431095,-0.0021934267,-0.050127365,-0.033852506]],"total_duration":795955600,"load_duration":780065000,"prompt_eval_count":7}
2025-12-03T20:49:26.046+08:00 INFO 2004 --- [langchain4j-study] [nio-8080-exec-2] d.l.http.client.log.LoggingHttpClient : HTTP request:
- method: POST
- url: http://localhost:11434/api/embed
- headers: [Content-Type: application/json]
- body: {
"model" : "nomic-embed-text:latest",
"input" : [ "张三是个程序员" ]
}
2025-12-03T20:49:26.078+08:00 INFO 2004 --- [langchain4j-study] [nio-8080-exec-2] d.l.http.client.log.LoggingHttpClient : HTTP response:
- status code: 200
- headers: [content-type: application/json; charset=utf-8], [date: Wed, 03 Dec 2025 12:49:26 GMT], [transfer-encoding: chunked]
- body: {"model":"nomic-embed-text:latest","embeddings":[[0.008590562,0.031015625,-0.16650501,0.007470685,0.019849382,0.014403546,-0.010300263,-0.05160567,0.0035875158,0.0013182258,-0.033842687,0.036495715,-7.4348833e-7,-0.06829574,-0.046190348,-0.017976506,-0.0046319333,-0.03619337,-0.049828514,0.053493585,-0.03710347,0.06048352,-0.03672529,-0.06601009,0.11165622,-0.013223444,0.02168524,-0.0040255073,-0.013327427,0.032252006,0.056895804,0.018110206,-0.04244362,0.0073742718,-0.02057949,-0.031122291,0.012021057,0.09302927,0.02364641,0.010348169,0.015764242,-0.0090054395,0.0037802742,-0.032016225,0.031793654,0.010246178,0.0022156888,0.03622668,0.068126604,-0.007162246,-0.05145329,-0.0146910725,0.02610642,-0.0497348,-0.020128349,0.03257015,-0.001811818,-0.08629027,-0.0003080031,0.0063849147,-0.0045739794,0.018806497,0.009025892,0.018843805,0.061153397,-0.0060491776,-0.051379837,0.0899103,0.029820317,-0.020355722,0.07973665,0.007201161,0.052205145,0.011411029,-0.05240771,-0.043174885,-0.033917926,-0.016335737,0.020242654,0.030693358,-0.00081439066,-0.049479954,0.08692303,0.012701794,0.058882803,-0.0027290238,0.036982156,-0.01059147,-0.02762344,0.052463498,-0.050198853,0.039518088,0.017831603,0.0063255006,-0.06483641,0.029218016,0.009462119,0.093950525,0.0043410533,-0.012705846,-0.03731307,-0.041111734,0.032821205,-0.030068547,-0.013130003,0.021054126,0.033766095,0.05583977,0.0028652041,0.008174571,0.012991008,0.059271127,-0.019151585,-0.018025493,0.072042905,0.020171048,0.0040657944,-0.063513406,0.018441916,-0.007857493,-0.03409809,-0.008043158,-0.015247218,0.06873721,0.033048317,0.014219673,-0.05385796,0.056045223,0.026153313,-0.07902372,-0.017130896,0.014195139,-0.040471792,-0.02724227,-0.00017226701,0.015670897,-0.042542093,-0.014322978,-0.0011318738,0.052081622,0.012548531,-0.0023206465,0.016707009,-0.044201232,0.04035552,-0.016559558,0.016625147,-0.021837177,-0.074494585,-0.015067043,-0.0036149584,0.022983935,0.017676054,-0.012576821,-0.0036504744,-0.048099276,0.012722795,0.015365593,0.0012339418,0.04260723,0.008560363,-0.012536454,-0.058387417,0.013829387,0.015311579,-0.040455323,0.016614893,0.036967978,0.017426984,-0.012358328,-0.055293113,-0.0021101348,-0.007432121,-0.02651007,0.000087812456,-0.017641917,-0.035303194,-0.051334735,0.002530298,0.018606832,-0.019407725,-0.022313327,-0.005976882,0.024999946,-0.071344964,0.0041563064,-0.018115958,-0.013323988,-0.027802218,-0.010522106,-0.0015264363,0.045287803,0.012309738,0.0050979857,-0.036705814,-0.022299252,0.025558786,0.0251468,0.04019238,-0.0153363235,0.004247454,-0.015198666,-0.038954727,-0.02285649,-0.011278251,0.03485875,0.016511755,0.017208992,-0.0033281355,0.057290893,-0.01621834,-0.022690194,-0.0063593304,-0.019345928,0.0005613422,0.025556583,0.034102917,-0.050624423,-0.05377216,0.02577523,-0.00792278,-0.0058003943,-0.051275812,-0.065554366,0.016858708,-0.019309198,-0.006651939,-0.020783858,0.046286926,-0.017890804,-0.03046092,-0.06843685,0.06636347,0.020455407,0.006887038,0.044306304,0.0044104615,0.046723817,0.02065035,0.063303374,0.0056700124,0.058412824,0.034533042,-0.046333242,0.0034523385,0.020299222,-0.0025172215,-0.027143173,0.009705527,0.028661605,-0.0058045196,0.015537313,0.041698664,0.019581895,0.035892844,-0.017247008,-0.053584237,-0.010606862,0.020993149,-0.013470348,0.014917697,-0.024929732,0.028946279,-0.049482383,0.03139993,-0.022876615,-0.009451597,-0.01561755,-0.034764048,0.021868624,-0.0060067144,-0.0052583427,0.01605561,0.017362257,-0.060779516,0.0117240185,-0.006892056,-0.0052455063,-0.044337045,-0.0074947765,-0.004488904,0.007824931,-0.017396608,0.014307885,0.0019230598,0.051407803,0.0076049096,0.0045396904,-0.0018676841,0.05687475,-0.006556594,-0.015711226,0.013694917,0.013742172,0.0013737023,-0.033116076,0.016797341,-0.04943451,0.04507906,0.028682401,0.083399266,0.02108589,0.006868516,-0.02237691,0.030966954,0.00083201495,0.09340374,0.018196171,0.052590642,0.022043753,-0.07153434,-0.022450263,-0.018907806,0.038323488,-0.09544609,-0.0067460905,0.009314503,0.021421093,0.015388795,0.054287445,-0.036231864,0.004741054,-0.01019616,-0.059320092,-0.03508661,0.07541711,-0.01844508,0.09696373,0.01537255,-0.04591825,-0.03757316,0.06300658,0.02442396,-0.010680613,-0.04759838,0.045394093,0.027063416,0.002585389,-0.009735654,-0.017820505,0.075695895,0.0060885916,-0.016787855,-0.09357182,-0.000877391,0.019030167,-0.06287424,-0.0063872267,0.015603507,0.006003304,-0.0009679022,0.0034523648,-0.07250516,-0.018124463,-0.02605944,0.012973602,-0.0075678993,0.030512815,0.054636326,-0.01753772,0.0144184325,0.010600104,0.00437912,0.014677567,-0.0105082495,0.052433904,-0.004473732,-0.0030103053,0.0749955,0.06373782,-0.008105547,-0.040547702,0.0029279294,-0.013561181,-0.019525409,-0.012301959,-0.045189213,-0.006929278,-0.045076497,0.036633965,-0.0031806005,-0.026705857,-0.001666452,0.011524989,-0.0034941803,0.058078304,0.015344257,0.009469278,-0.09988575,0.022507071,-0.053511605,-0.045080703,-0.053717654,0.0056730453,0.010272321,-0.075675964,0.056281023,0.02868782,-0.00029655875,0.03922947,-0.06003071,-0.07330226,-0.023338381,0.00976207,-0.0686264,0.03627796,0.028168183,-0.049119193,0.10578816,-0.0033787766,0.012876725,0.016017372,-0.023767948,-0.052687462,0.03748561,0.01783566,0.038659725,0.027012859,-0.04447574,0.017601615,0.060764194,0.027124906,-0.039095905,-0.015393358,0.0113273235,0.037486456,0.043161225,0.0052888324,-0.0051092575,-0.009712759,0.01688616,0.05448898,-0.018232625,0.010844586,0.01262339,-0.048822556,0.022942038,0.0028246099,-0.03582101,0.043071076,0.018292341,0.009527496,-0.048251864,-0.03739528,0.034853425,0.0071248314,0.03709441,-0.087941915,0.004660677,0.015172789,0.041209135,-0.008159491,-0.05122877,0.03412225,0.05942744,0.0044897506,-0.007634141,-0.04404325,-0.03835943,0.039753404,0.041605562,0.0018283474,-0.044757113,-0.015651224,0.016357379,-0.03520186,0.0050640935,0.013015743,0.05960018,0.039904438,0.00030555413,0.035785627,0.0537588,0.014380054,0.021094417,-0.060753368,-0.0043271105,0.00063462753,-0.0024251728,0.06903713,0.06053826,-0.008958702,-0.03275732,-0.036294136,0.005286753,0.009060593,-0.006180468,-0.06860374,-0.010899158,-0.0011590931,0.023314267,-0.0018345176,-0.0275583,-0.0032993765,-0.015836263,0.012940074,-0.03831956,0.028931163,0.005330536,0.020248445,-0.007310754,-0.018168412,-0.0025023138,-0.008937886,0.02714692,-0.025806474,-0.029021347,-0.03495193,0.006244348,-0.000052224816,-0.043418616,-0.04027487,0.024697397,-0.01670041,-0.006405406,-0.03344401,0.08096624,-0.01673568,-0.030612225,0.025451224,0.036975738,-0.012661105,-0.032142825,-0.047408853,-0.05308202,-0.03703192,0.026182353,-0.024437902,0.053533416,-0.028965438,-0.03750813,0.00872211,-0.027232518,-0.025171468,-0.0044281366,-0.01873027,0.027332023,0.061342433,0.029374463,0.04893919,-0.014993691,-0.010504114,0.015161416,0.03540359,-0.0037676343,0.025356341,-0.041378442,-0.009041768,-0.008523679,-0.011273603,0.08246525,-0.07105511,-0.074741304,-0.04462433,0.022010632,-0.034813788,-0.018248407,0.0034918706,0.015101339,-0.06989776,-0.039119683,-0.010964537,0.015794812,0.0004916373,0.05546412,-0.09903014,-0.014818889,0.037392687,-0.00584791,-0.025484394,-0.024998834,-0.062217396,-0.03829924,0.034674995,-0.018653693,0.017741129,0.008504499,-0.01519927,-0.06862197,0.022577012,0.026841344,-0.0027886578,0.006432464,0.0034500938,-0.018223252,0.040249184,0.028976696,-0.021473493,0.033897858,-0.06799461,-0.014736201,0.03721534,-0.028939042,-0.015783217,0.008796445,-0.011302674,0.023690592,-0.03415775,-0.021574695,-0.01956304,0.032121222,0.049584586,0.06549984,-0.06028639,-0.0095222825,0.06628373,0.0024587917,-0.012616075,-0.010949471,0.011424885,0.023394806,0.02689124,-0.045895327,-0.002623215,0.060656346,-0.023087507,0.068677776,0.02523266,0.0058408435,-0.06505133,-0.019935044,-0.042986006,0.05470673,-0.049787395,0.02234649,-0.040309574,0.02163993,-0.004273782,-0.05797902,0.066293634,-0.0018739136,-0.013293968,-0.05044481,-0.020874467,-0.014905202,0.022408893,0.029362202,-0.049261443,0.027829139,0.038575713,0.03318018,0.036310162,0.04540779,0.04815854,0.023842087,-0.013268249,0.024096213,0.023741703,0.053433698,-0.031386048,0.053772595,0.014243401,0.015288994,0.03139649,-0.003420198,-0.048755616,0.032757588,-0.032204706,-0.050331343,-0.031434022,-0.004453705,0.019752976,0.0068550543,0.0065903887,0.022211375,0.033160064,0.00088454684,-0.024909154,-0.08423674,0.002339054,0.034160897,0.013382925,-0.04162849,-0.033120625,-0.0045666303,0.048306737,0.07156336,0.024346244,0.024216983,0.02445087,-0.014345396,-0.037348326,0.026476141,-0.06858837,-0.012303432,-0.03489846,0.06408541,-0.058163036,-0.018453123,-0.065192185,-0.008539487,-0.04655166,0.00042454328,-0.011788158,-0.063578196,0.018247813,-0.012388181,0.027751073,-0.028262425,0.0574435,0.0073557277,-0.009461153,0.025204694,-0.012254223,-0.034520205,-0.03502168,-0.006660032,0.0036886663,-0.011498965,0.045002043,0.006224707,-0.003794949,-0.0090487115,-0.0011528758,0.032117415,0.03219082,-0.03267421,-0.024073236,-0.024450669,0.038304657,0.028566768,0.012223739,-0.05863526,-0.035832483,-0.052684344,-0.0141777,0.00504447,-0.014406067,0.118135646,-0.00024360162,0.027165784,0.030325316,-0.03463776,0.05458861,0.03785111,0.02914164,-0.027391203,0.014545223,-0.027452081,-0.046843007,0.015639732,0.066088416,-0.011578402,0.0015018014,0.022046408,0.016381744,-0.0064455066,0.05273965,0.006859068,-0.015097319,-0.0061694505,0.019579172,-0.0034062087,0.026545914,0.06916123,0.022936445,-0.02655567,0.034123596,0.06268199,-0.007642043,-0.008394144,-0.013892761,-0.015325021,0.01428205,0.015795916,-0.04655224,-0.038126703,-0.031497564]],"total_duration":28549700,"load_duration":13033600,"prompt_eval_count":7}
也就是每个分片,都会调用1次ollama的embedding模型,然后返回1个N维向量化的数值列表。
再访问 /query/memory

在运行完下面这行代码后
EmbeddingSearchRequest embeddingSearchRequest = EmbeddingSearchRequest.builder()
.queryEmbedding(queryEmbedding)
.maxResults(1)
.build();
日志里能看出类似下面的输出:
2025-12-03T20:55:50.338+08:00 INFO 2004 --- [langchain4j-study] [nio-8080-exec-3] d.l.http.client.log.LoggingHttpClient : HTTP request:
- method: POST
- url: http://localhost:11434/api/embed
- headers: [Content-Type: application/json]
- body: {
"model" : "nomic-embed-text:latest",
"input" : [ "我最喜欢的运动是什么?" ]
}
2025-12-03T20:55:50.373+08:00 INFO 2004 --- [langchain4j-study] [nio-8080-exec-3] d.l.http.client.log.LoggingHttpClient : HTTP response:
- status code: 200
- headers: [content-type: application/json; charset=utf-8], [date: Wed, 03 Dec 2025 12:55:50 GMT], [transfer-encoding: chunked]
- body: {"model":"nomic-embed-text:latest","embeddings":[[-0.008115742,-0.015585497,-0.13333036,0.014075963,0.015065597,0.041018385,0.0093338275,-0.029693706,-0.060415648,-0.027698671,-0.052847344,0.030690305,0.010376536,0.006411707,-0.0028337257,-0.051441237,-0.016112154,-0.05086288,-0.064174384,0.09260154,-0.04556787,-0.011901141,-0.051579148,-0.03880057,0.14123084,0.005697247,0.044796832,0.029680252,-0.008968429,0.02070351,0.0005384779,-0.00739899,0.0067503783,0.02072552,-0.04889415,-0.015790965,-0.02292258,0.015328896,0.08163653,-0.00048329745,0.011841128,0.050682325,-0.0027264701,-0.06918234,0.093836255,0.014679576,0.007782651,0.024759298,0.06410606,-0.0200973,-0.005375509,-0.004897554,0.018823352,-0.033870872,0.054296874,0.02213565,-0.037226338,-0.052412357,-0.015238384,-0.007883027,0.03199989,0.03468052,-0.008106541,0.012445603,0.042109136,-0.047825668,-0.0034301947,0.043615565,0.0440101,-0.009739389,0.057788894,-0.023860384,0.018072136,0.06061713,-0.007466897,-0.02671022,-0.005062547,0.03723064,0.042074934,0.0030844717,-0.0145332925,-0.006205023,0.08416619,-0.004070509,0.0008449276,0.04076994,0.018784352,0.010214048,-0.07535662,0.03430938,0.02815943,-0.006273019,0.015156151,0.014893555,-0.040834658,-0.035543747,-0.0034450423,0.077888705,-0.059903845,0.0017995096,-0.04611276,-0.015257642,0.0069801696,-0.01233614,0.032612786,0.05331779,0.006282434,0.02068734,-0.042655632,0.021370713,-0.012770967,0.052754514,-0.0062254043,0.014820399,0.011350412,-0.03702375,0.04073735,-0.02250403,-0.030020835,0.032361265,-0.048771087,0.017013451,0.002949944,0.04379019,0.037737772,0.0025416142,-0.02822043,0.031174898,-0.0008814349,-0.016885675,0.018667608,0.03837068,-0.010391661,-0.023683624,-0.045387805,0.003798634,-0.027902782,-0.04566038,-0.011995797,0.014768694,-0.012910256,0.0031263528,0.021616146,-0.07669826,-0.00034426866,0.0021399322,0.029707586,-0.074164,-0.084653124,0.008929458,0.009953579,0.00929179,0.008245573,-0.004845136,0.038104005,-0.01710641,-0.022373583,0.032140963,0.0010637263,0.008772573,0.022202952,0.016757442,-0.01304422,0.00023633665,0.0001642406,-0.05737902,-0.0036307937,0.084965,0.023454588,0.034155447,-0.043068808,-0.031163499,0.010587485,-0.009281333,-0.03196874,-0.0108324755,-0.0021923278,-0.036802996,0.00087092404,0.06296409,0.011602653,-0.032950934,0.025423832,-0.022851044,-0.06139013,-0.0070615783,0.009101179,-0.011478052,-0.01878926,-0.037191972,0.0014877256,0.0007775391,-0.033233833,0.009093755,-0.016524913,-0.055446133,-0.025737965,-0.012461609,-0.005133042,-0.00398971,-0.026171435,-0.0016164841,-0.013170093,-0.019892735,-0.025664736,-0.008967844,-0.010380286,0.062129214,0.0162898,-0.0115862675,0.061520923,-0.03338186,-0.01254469,0.0075701894,-0.018509671,0.0032092815,-0.013146711,-0.029882805,-0.099842764,0.007912248,-0.0059437808,0.011386356,-0.022434039,-0.01308352,0.05604825,-0.0039252755,-0.0028751004,-0.02107682,0.042494092,-0.03210877,-0.016672453,-0.073347315,0.0883686,0.059930574,-0.055812076,0.07707391,0.001712748,0.090796515,-0.01927482,0.03358539,0.0039575156,-0.0057691755,0.013589839,-0.030882977,-0.030422674,-0.026976114,0.0040917294,-0.012387564,0.0066033606,-0.004724346,0.025853438,-0.022949548,0.012765916,0.06289916,0.028682806,-0.025512276,-0.02269097,0.009805497,0.016847452,0.0025103365,-0.004040384,-0.04413349,0.01340134,-0.03268585,-0.02427441,-0.031419516,-0.038697284,0.017902683,-0.001541238,0.021164333,0.04036404,0.059666324,-0.008868298,0.05450973,-0.04792435,-0.03334107,0.021895131,-0.007546498,-0.05882375,0.018312508,0.041004516,-0.000666968,0.019353122,0.032961134,-0.00082671875,0.02837444,-0.0471661,-0.010013583,0.024346149,0.01422616,0.012211974,-0.029859373,-0.0019391068,0.07868876,0.008026108,0.027926043,0.0398688,0.003160286,-0.016946834,0.018473957,0.041405037,0.022756994,0.051411245,-0.012842393,0.042399954,-0.0035269877,0.0822787,0.0018001649,0.022062154,-0.04171039,-0.04335265,0.0020458684,-0.0077533056,0.04580981,-0.089055315,0.0372908,0.034763575,-0.01915443,0.04666737,-0.010026614,0.00420045,-0.0268113,0.023279548,-0.035705607,-0.03659528,0.07135262,0.020292541,0.040815864,0.00910824,-0.041103702,-0.028106777,0.026332615,0.04183927,-0.041592494,-0.019030934,0.035726525,0.014816425,-0.011934556,-0.04071483,0.08871609,0.02954997,0.026761355,0.066348545,-0.04480436,0.00036604036,-0.025199125,-0.07083473,0.04527632,0.019795392,-0.034232587,-0.016409243,0.033908073,-0.03486652,-0.010048105,-0.00496681,0.0007306037,0.008923997,0.034070753,0.011769994,-0.0098197665,0.055801883,0.022011312,-0.032751173,0.06937673,-0.009899164,0.07221998,0.016788442,-0.018445518,0.015535376,0.009546978,-0.07979852,-0.018999133,-0.004450009,0.006625163,-0.011275887,-0.002693962,-0.034339577,-0.000025643763,-0.04789614,0.027982617,-0.02358338,-0.030491589,-0.013846871,0.007060635,0.03738191,0.011572845,0.027781395,-0.007893956,-0.021810682,0.023727927,-0.025399655,-0.07374523,-0.055070467,-0.002877967,0.037805356,-0.06079738,0.030284297,0.0531466,0.033288077,-0.021598645,-0.028856734,-0.029001212,0.02724322,-0.0014798277,-0.020576032,0.045418043,-0.01593172,-0.052276548,0.10947757,0.036169063,0.009748931,0.021588888,-0.011341725,-0.02702904,-0.0015633478,0.05059406,0.06023162,-0.0147287855,-0.02833043,-0.009267264,0.06519959,0.010081677,-0.027306113,-0.005990853,0.018372448,0.05867316,0.020249818,0.009503379,0.031673934,-0.065324046,-0.03578315,0.06490272,0.021771029,-0.0123683475,0.009822485,0.04522174,0.07775963,0.046681315,-0.022082463,0.02771851,0.01869938,-0.049579468,-0.045697734,-0.015279012,0.016752264,0.015125946,0.09349564,-0.08036948,-0.064772576,-0.0033323816,0.02474867,0.03491571,0.005695979,-0.013034251,0.12365097,-0.03236941,-0.0410409,-0.02853897,-0.033536483,0.036535084,0.02953421,0.018964991,-0.043603886,0.020572193,0.026156766,-0.008886562,0.027775602,-0.015827546,0.06151183,0.015997749,-0.006523213,0.042643726,0.05186382,0.010915669,0.06863525,0.0358132,-0.023492016,-0.04795516,-0.02786007,0.021940107,0.025288798,-0.035293136,-0.05662155,-0.06437612,0.005913431,0.01305711,0.0045761093,0.007888366,-0.012019398,-0.025043717,0.046696566,-0.0031181087,-0.018143885,-0.03147513,-0.015051214,0.0021655853,-0.019577026,0.002296133,0.009979383,0.022035947,-0.033517957,-0.021207662,-0.009086088,0.044459015,0.06848301,0.033261746,0.0130590545,-0.023270579,-0.03845769,-0.010440919,-0.04050453,-0.012967926,-0.0149792535,0.030992843,0.00678055,-0.006880013,0.037172455,-0.020247728,-0.060844947,-0.043875363,-0.019488225,-0.06488449,-0.030761205,0.043167047,-0.052886073,-0.012507291,-0.0010001627,-0.08169139,0.06768102,0.0201341,0.02781023,-0.003916916,-0.011803959,-0.028655719,-0.015347605,0.013413334,0.018498387,-0.00034818306,-0.019966338,0.041559428,0.008130601,0.0013016722,0.040024176,0.021986326,0.009723751,0.023152934,-0.03311178,0.025477832,0.014715807,-0.019772537,0.023504416,-0.024493547,-0.029253123,-0.036824778,-0.00033862857,-0.004920964,-0.023366148,0.016927306,-0.06421647,-0.032642335,0.013927714,0.0060826577,-0.020252207,0.006964334,0.0697905,-0.0648599,0.046724826,0.052941564,0.0018347019,0.0125221405,0.0025142601,-0.016252909,-0.017113369,-0.018424492,-0.020608893,0.009626813,0.0005812131,-0.001089929,-0.070007734,-0.050719883,0.014778829,-0.027579054,-0.06540208,-0.036946554,-0.016515205,0.011790516,0.0078643495,-0.085410744,0.055054575,-0.024332732,-0.045337938,0.014176501,0.02664769,0.0055836253,0.019611299,0.0019017619,-0.0466582,-0.0496996,-0.014661929,-0.025317712,0.08826201,0.051592737,0.03234753,-0.062027734,0.018372335,0.08740607,-0.013051332,0.03621722,-0.014771845,-0.018579198,0.03969096,0.033640202,-0.002073451,-0.05179525,0.044089876,-0.024287477,0.0282181,0.048065037,-0.025321037,-0.025103245,0.003009924,-0.0406875,0.026016891,-0.030809969,0.044824786,-0.011064573,-0.031673983,-0.040815715,-0.047715474,-0.00006370696,-0.018475268,0.02093795,-0.06360585,0.020086234,-0.03930048,0.020298067,0.008839129,0.009075908,0.00925528,0.017089458,0.037834603,0.027752021,-0.018815871,0.057482976,0.01703942,-0.027874121,-0.013361988,-0.010856889,0.052335404,-0.031846356,0.06991038,0.048569877,0.0663401,0.02571991,0.019868027,-0.033919293,0.025195478,-0.07152865,-0.08591871,-0.052609075,0.0248162,0.023398038,-0.025106415,-0.025987174,0.018537428,0.0035505227,-0.00753813,-0.024998391,-0.0795355,-0.006484952,0.026805002,-0.01943846,-0.042584565,-0.020271726,0.015568395,0.038097035,-0.01087966,0.033430714,-0.0004264459,0.04119051,-0.07264177,-0.021113876,0.03167816,-0.023426246,-0.023975592,-0.007034573,0.06895538,-0.057525635,-0.026526554,-0.01783456,-0.058120593,-0.0472508,-0.048533157,0.0017255767,-0.02114539,-0.03147397,-0.048717614,0.014413072,-0.06587363,0.06240838,0.045198083,0.009743508,0.021514531,0.02697135,-0.004341519,-0.015265082,0.023395324,0.017472507,-0.013622462,0.0019736853,-0.026773533,0.03651884,-0.008615806,0.0734604,0.010918776,0.012674031,-0.043588556,0.0006317654,-0.030423554,0.05500461,0.0074791363,-0.0016071416,-0.07096685,-0.040689144,-0.05325465,-0.021476716,0.0035326187,-0.0011494444,0.061648067,-0.008181069,-0.0041762907,0.014587891,-0.030434487,-0.0041058203,0.008717493,0.03546327,0.028929433,-0.0014569175,-0.04828812,-0.032865316,-0.0512884,0.010617321,0.02133418,0.041363817,0.012069813,-0.024778321,-0.0053670583,0.034263976,0.01412419,-0.0068221544,0.008293809,0.01379106,-0.045575395,0.026151646,0.015173649,-0.039394826,-0.02495427,0.070570946,0.073696576,0.014283659,0.012210249,-0.02519546,-0.019005196,-0.0022043423,0.009383856,-0.007286069,-0.04551789,-0.024567215]],"total_duration":31426800,"load_duration":15239000,"prompt_eval_count":11}
3、重排/生成
private interface Assistant {
String chat(String userMessage);
}
/**
* 基于RAG的AI聊天
*
* @param query
* @return
*/
@GetMapping(value = "/query/bot", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> bot(@RequestParam(required = false) String query) {
try {
if (!StringUtils.hasText(query)) {
query = "张三的职业是什么?";
}
ContentRetriever retriever = EmbeddingStoreContentRetriever.builder()
.embeddingStore(embeddingStore)
.embeddingModel(ollamaEmbeddingModel)
.maxResults(3)
.minScore(0.6)
.build();
Assistant assistant = AiServices.builder(Assistant.class)
.chatModel(ollamaChatModel)
.chatMemory(MessageWindowChatMemory.withMaxMessages(10))
.contentRetriever(retriever)
.build();
return ResponseEntity.ok(assistant.chat(query));
} catch (Exception e) {
log.error("bot", e);
return ResponseEntity.ok("{\"error\":\"" + e.getMessage() + "\"}");
}
}

日志输出:
2025-12-03T21:06:00.218+08:00 INFO 16956 --- [langchain4j-study] [nio-8080-exec-6] d.l.http.client.log.LoggingHttpClient : HTTP request:
- method: POST
- url: http://localhost:11434/api/embed
- headers: [Content-Type: application/json]
- body: {
"model" : "nomic-embed-text:latest",
"input" : [ "张三的职业是什么?" ]
}
2025-12-03T21:06:00.255+08:00 INFO 16956 --- [langchain4j-study] [nio-8080-exec-6] d.l.http.client.log.LoggingHttpClient : HTTP response:
- status code: 200
- headers: [content-type: application/json; charset=utf-8], [date: Wed, 03 Dec 2025 13:06:00 GMT], [transfer-encoding: chunked]
- body: {"model":"nomic-embed-text:latest","embeddings":[[0.004239314,0.0076822806,-0.15271105,0.020557597,0.017610246,0.027028874,0.008144241,-0.05415975,-0.009586946,0.011460197,-0.030693114,0.016304646,0.026536094,-0.05875219,-0.05067128,-0.035113554,-0.019440431,-0.03776121,-0.02378428,0.04519144,-0.050143618,0.058961812,-0.07612095,-0.056118697,0.10937852,-0.02857785,0.03000752,0.01154821,-0.009974659,0.020804431,0.054440968,0.014955637,-0.03105021,0.012834936,-0.025086157,-0.038320024,0.004240673,0.083755895,0.023091014,0.0068098526,0.025083207,0.0047945147,-0.017740598,-0.06915115,0.04255228,0.030164212,0.0010529847,0.028383082,0.06147136,-0.015637815,-0.048482683,-0.029060137,0.027621185,-0.043885596,-0.02241433,0.04847376,-0.008942968,-0.07875518,-0.002326856,-0.000876321,-0.033622157,0.017305302,0.006997458,0.0134410625,0.061712984,-0.037069496,-0.03415777,0.07897632,0.03367711,-0.024371814,0.07567722,-0.008275173,0.075571656,0.011161394,-0.055894863,-0.033794764,-0.0398987,-0.012965509,0.046365112,0.030377273,-0.007176594,-0.035462674,0.09366785,0.012488825,0.025095569,0.036211118,0.04821028,0.010534683,-0.009972741,0.06394646,-0.016000072,0.00644526,0.02687148,-0.011847599,-0.06238765,0.009690668,0.009904766,0.081197165,0.006833967,-0.0038695005,-0.04186199,-0.05436312,0.018488813,-0.038451444,-0.024730546,0.027535748,0.021780146,0.059848938,-0.0002500832,0.008661577,-0.0077257636,0.06457395,-0.0027686341,-0.009195185,0.069868706,0.010799128,0.012559299,-0.040336374,0.011811284,0.00063853635,-0.045540534,0.017777976,-0.022031223,0.075947724,0.0115830125,0.03329062,-0.043727126,0.05534762,0.011966479,-0.07175714,-0.021497155,0.015485216,-0.024018852,-0.049549583,0.008524587,0.0049498314,-0.035109345,-0.023410548,-0.027239466,0.03842147,0.01448168,-0.014102318,0.0032033215,-0.054772083,0.021659981,0.0067783417,0.02170977,-0.011267286,-0.08510153,-0.013214338,-0.00348202,0.02383748,0.029579483,-0.04043524,0.00044404608,-0.044277325,-0.014728323,0.023417234,0.007308794,0.020965401,0.017066762,-0.0005711157,-0.019155668,0.012148955,-0.0017873651,-0.044090237,0.024649762,0.035161387,0.0035890387,0.003900446,-0.049732774,-0.008480614,0.0074587865,-0.029605603,0.0019114861,-0.031284958,-0.021024121,-0.04693143,0.023029728,0.024880763,-0.046670627,0.0043787607,0.012573593,0.021554986,-0.059274822,-0.009824468,-0.011827892,-0.021197192,-0.032784812,-0.009025754,-0.020741744,0.04332855,-0.0015510862,-0.0045260345,-0.025825664,-0.009923332,0.008637219,0.004847918,0.03409407,0.019117644,-0.025345683,0.0014867083,-0.03858049,-0.0075780028,-0.024258662,0.024665292,-0.0027994271,0.03188669,0.0020731047,0.023816857,0.009806663,-0.025651302,-0.008033845,-0.019956153,-0.014523345,0.017414723,0.027297214,-0.040654045,-0.060853194,0.018099638,-0.047840822,-0.009596795,-0.046515644,-0.049647443,0.023643402,-0.024819456,-0.017526656,-0.038194455,0.048414312,-0.00926003,-0.031959366,-0.10014377,0.048893444,0.016025485,0.024572294,0.056181535,0.007686751,0.04741462,0.004992303,0.06475729,0.01850571,0.027393414,0.04121269,-0.049672104,0.0055567184,0.017761739,-0.0013584531,-0.030926447,0.01058877,0.004911465,-0.012616818,-0.020507948,0.042212673,0.024521543,0.02743672,-0.0003124863,-0.040724095,-0.02091249,0.03027006,0.009871204,0.014010726,0.0020538687,0.03791008,-0.043991327,0.00816236,-0.027717594,-0.023350587,-0.008899039,-0.029842105,0.026171569,0.011561457,-0.0060322625,0.012637834,0.01210109,-0.051430922,-0.006517182,-0.010841997,-0.0038439855,-0.025716938,-0.006127447,-0.017805882,0.02975385,-0.022800546,0.025821596,-0.004564489,0.06804078,0.01069556,0.0024808545,0.015150475,0.058398042,-0.0073075136,-0.040593546,0.0083828885,0.025062455,-0.019359473,-0.01640101,0.023051668,-0.051863715,0.017485382,0.02869504,0.072855994,0.019572867,0.015174361,-0.012517081,0.05302477,-0.0044919676,0.09097072,-0.004353169,0.061164353,0.009341156,-0.056262925,0.0008793209,-0.020595431,0.038634345,-0.09336777,0.011940502,0.011527484,0.0065326956,0.020256134,0.034039382,-0.0339366,0.022494942,-0.021347981,-0.057749186,-0.013683414,0.084091954,-0.019109922,0.10854067,0.02052652,-0.054907523,-0.03633764,0.04415649,0.013936457,-0.04068045,-0.034107056,0.052095514,0.016485762,0.012878618,0.0025548527,0.0059093726,0.05836622,0.025727693,0.03317276,-0.09940213,0.018877828,0.031769868,-0.06272839,-0.0048316387,-0.0013718973,0.00816232,0.007471591,0.023453824,-0.07561334,-0.0032386584,-0.01692012,-0.0031250906,-0.021765087,0.037825674,0.05001515,-0.025596138,0.016760351,0.021452932,-0.015958514,0.015510471,-0.010207154,0.04849632,-0.022791835,-0.008830724,0.063183576,0.04663268,-0.023999449,-0.017784538,-0.012060538,-0.0011768463,-0.010786378,-0.008324438,-0.047535736,-0.001738403,-0.056259096,0.04014468,-0.005272602,-0.026317384,0.012514213,0.015059558,0.004496179,0.06955241,-0.004637489,0.00017667936,-0.08513875,0.016370857,-0.055048607,-0.07140911,-0.081966415,0.00030733185,0.010621059,-0.07454518,0.028682731,0.04973373,0.015434044,0.032244183,-0.040782902,-0.06103318,-0.018811677,-0.011944774,-0.05340002,0.051050365,0.028209431,-0.07048763,0.1292563,0.015788727,0.021853482,0.028857075,-0.019521916,-0.060547307,0.02191412,0.023292338,0.04369128,0.029931394,-0.07537579,0.02379132,0.059457928,-0.0054533686,-0.04838991,-0.010382239,0.013790543,0.04031787,0.04593453,0.0035965103,0.003830042,-0.01895226,0.02133826,0.058973763,0.005854475,0.025542043,0.009063986,-0.038048066,0.032067522,0.02204293,-0.04642957,0.047500666,0.012578929,-0.011539611,-0.038362917,-0.050482046,0.04113387,-0.02782944,0.038907357,-0.10160827,-0.0015507967,0.016757162,0.021490112,0.027127154,-0.050923735,0.025501352,0.056381334,-0.0071632527,-0.0071845283,-0.053656835,-0.058691893,0.027289815,0.026149955,-0.017202834,-0.04020127,-0.0027388576,0.03484433,-0.044742953,0.018350178,0.0027425652,0.051960062,0.035645038,0.0053780437,0.035967674,0.06540823,0.03577455,0.01854643,-0.06525295,-0.007879242,-0.002113245,-0.004005781,0.029152326,0.0584502,-0.016381431,-0.034783315,-0.039640322,-0.0014952852,-0.0095473975,-0.005678321,-0.057393085,-0.013394316,-0.0077183773,0.029069172,-0.012357107,-0.03578763,-0.015306675,0.0032748596,0.009398851,-0.01311399,0.026989317,-0.000836687,0.015069301,-0.0036676894,-0.00455568,0.0068664216,0.013578071,0.04251677,-0.020329874,-0.01887932,-0.028071124,0.025700448,-0.005058962,-0.057915613,-0.029488306,0.011417365,-0.0029739402,-0.013637518,-0.028992068,0.051570166,-0.0149316825,-0.019501744,0.008539193,0.026438462,-0.012016562,-0.0314256,0.0015972179,-0.01954337,-0.05373485,0.033556696,-0.035999633,0.0531771,-0.018250877,-0.03466959,0.009479784,-0.023361837,-0.017463325,0.0025408724,-0.006980441,0.031768385,0.04420795,0.03179117,0.058933582,0.0037400569,-0.013232174,0.013965171,0.045889672,-0.01018382,0.02284273,-0.021698652,-0.016798163,-0.0005869417,-0.017905826,0.070325024,-0.05684624,-0.087579355,-0.03542601,0.02266523,-0.033230636,-0.022272518,-2.7461e-7,0.032914832,-0.06935193,-0.017655943,-0.011459261,0.009935156,-0.007662621,0.058246523,-0.088759445,0.013634456,0.046107832,-0.00062332774,-0.001124956,-0.022926036,-0.06386429,-0.00788879,0.029425634,-0.00062139984,0.020791754,0.0033109956,-0.0007674863,-0.09831547,0.015583692,0.02097934,-0.011392306,-0.0022881734,-0.011716278,-0.01594401,0.064240225,0.0205384,-0.03174797,0.024243874,-0.057022937,-0.030922778,0.033032637,-0.025811221,-0.0056205257,0.008472512,0.012337248,0.0011733003,-0.010635884,-0.020362062,-0.007157757,0.056365535,0.06611271,0.057620756,-0.05054261,-0.018119399,0.08212738,0.024661517,0.0037607318,-0.016132,-0.014730193,0.02405035,0.03656392,-0.054034926,-0.026423624,0.07040349,-0.02323852,0.05476059,0.029168636,-0.015232205,-0.031917393,-0.0015120265,-0.027342107,0.014827417,-0.05661694,0.029209934,-0.034035865,-0.0043230555,-0.006688305,-0.026893342,0.0749632,0.00403138,-0.0063194423,-0.04880561,-0.023989432,-0.025482556,0.03446206,0.020196505,-0.047460396,0.029858857,0.025801543,0.020115932,0.039588965,0.029865136,0.05552343,0.033649344,-0.02212388,0.032553803,0.0124924155,0.058882274,-0.04683214,0.026294542,0.016393008,0.009326513,0.012182453,0.010453827,-0.06815359,0.03354419,-0.052273188,-0.08314763,-0.0526515,-0.0068970337,0.027125243,0.0042001,0.01680315,0.025246829,0.01940458,0.008729578,-0.028120253,-0.08059583,-0.00015949234,0.038790293,-0.0024494377,-0.03693281,-0.0035142284,0.017876936,0.049747378,0.038981076,0.028778218,0.017998405,0.022339223,-0.016271245,-0.009814025,0.05427773,-0.06674238,-0.029299235,0.004611899,0.060835272,-0.06467459,-0.020397063,-0.0546159,-0.030502323,-0.04963557,0.00028014425,0.0034612329,-0.056374557,-0.010244094,-0.021868994,0.03847016,-0.041613195,0.058092404,0.010680698,0.0049237283,0.0076524247,-0.010727673,-0.015634516,-0.013103446,0.00004253777,0.0034415699,-0.015937073,0.050427258,-0.008913672,0.0035406454,0.016454168,0.028332196,0.023553139,0.026810646,-0.03237243,-0.051344108,-0.0408263,0.016672155,0.029943697,0.008967387,-0.06845903,-0.025524158,-0.05670307,-0.013660232,-0.010677435,-0.005277578,0.120395266,0.0020981333,0.020239038,0.023900429,-0.0364865,0.062224284,0.036208972,0.01892785,0.0051098405,0.007617975,-0.0003034432,-0.04963794,-0.0105263265,0.07343204,0.007900394,-0.017053545,0.023035154,0.026797958,-0.0116560655,0.041811824,0.0077687483,-0.01553096,-0.0052456115,0.016330473,0.0019632638,0.054174215,0.08227454,-0.0013025316,-0.02069202,0.04110507,0.052067257,-0.0045271832,-0.019649476,-0.013414835,-0.017467491,-0.0018297071,0.008386364,-0.028313095,-0.039351642,-0.021777628]],"total_duration":33676500,"load_duration":18415500,"prompt_eval_count":9}
2025-12-03T21:06:00.306+08:00 INFO 16956 --- [langchain4j-study] [nio-8080-exec-6] d.l.http.client.log.LoggingHttpClient : HTTP request:
- method: POST
- url: http://localhost:11434/api/chat
- headers: [Content-Type: application/json]
- body: {
"model" : "deepseek-v3.1:671b-cloud",
"messages" : [ {
"role" : "user",
"content" : "张三的职业是什么?\n\nAnswer using the following information:\n张三是个程序员\n\n我喜欢打乒乓球"
} ],
"options" : {
"stop" : [ ]
},
"stream" : false,
"tools" : [ ]
}
2025-12-03T21:06:02.708+08:00 INFO 16956 --- [langchain4j-study] [nio-8080-exec-6] d.l.http.client.log.LoggingHttpClient : HTTP response:
- status code: 200
- headers: [content-type: text/plain; charset=utf-8], [date: Wed, 03 Dec 2025 13:06:02 GMT], [transfer-encoding: chunked]
- body: {"model":"deepseek-v3.1:671b-cloud","remote_model":"deepseek-v3.1:671b","remote_host":"https://ollama.com:443","created_at":"2025-12-03T13:06:02.704211517Z","message":{"role":"assistant","content":"根据提供的信息,张三的职业是程序员。 \n(信息源:第一句话“张三是个程序员”)"},"done":true,"done_reason":"stop","total_duration":1059949995,"prompt_eval_count":21,"eval_count":22}
从日志上看,先做了1次RAG检索(即:召回),然后将返回的结果进行了排序,将得分最高的"张三是个程序员",排到了第1位(即:重排),然后再把用户问题+重排后的结果,拼在一起发给LLM,最终得出了答案(即:增强生成)
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
浙公网安备 33010602011771号