第三期书生大模型实战营 基础岛第四关 InternLM + LlamaIndex RAG 实践

112 阅读26分钟

任务要求:基于 LlamaIndex 构建自己的 RAG 知识库,寻找一个问题 A 在使用 LlamaIndex 之前InternLM2-Chat-1.8B模型不会回答,借助 LlamaIndex 后 InternLM2-Chat-1.8B 模型具备回答 A 的能力

前言

在第二关的时候,部署了InternLM2-Chat-1.8B,让它讲述某个具体的故事时,并不能正确的讲述出来。给模型注入新知识的方式,可以简单分为两种方式,一种是内部的,即更新模型的权重,另一个就是外部的方式,给模型注入格外的上下文或者说外部信息,不改变它的的权重。

LlamaIndex 是上下文增强 LLM 应用程序的数据框架,本次任务将通过LlamaIndex实现提取、解析、索引和处理新增数据,并实现新增数据的快速访问以及 LLM与 提示工程相结合的查询工作流。

环境准备

开发机创建

首先在Intern Studio 创建开发机,本次需要创建30% A100的资源配置。

VSCODE连接开发机

打开vscode远程终端,使用conda创建一个虚拟python环境。

conda create -n llamaindex python=3.10

切换环境并安装依赖

conda activate llamaindex
conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch-cuda=11.7 -c pytorch -c nvidiapip install einops
pip install  protobuf nltk einops
pip install llama-index-embeddings-huggingface llama-index-embeddings-instructor -i https://pypi.org/simple

创建工作目录

mkdir ~/llamaindex_demo
cd ~/llamaindex_demo

 

下载大模型

创建一个dl_hf.py文件,下载模型文件到目录:~/model

cd ~
vim dl_hf.py

粘贴以下代码

import os# 设置环境变量​
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'# 下载模型
​os.system('huggingface-cli download --resume-download sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 --local-dir /root/model/sentence-transformer')

执行文件,等待模型下载完成

python dl_hf.py

测试模型

进入llamaindex_demo文件夹,创建llamaindex_internlm.py

cd ~/llamaindex_demo
vim llamaindex_internlm.py

粘贴以下代码,提问两个问题,测试默认输出

from llama_index.llms.huggingface import HuggingFaceLLM
from llama_index.core.llms import ChatMessage
llm = HuggingFaceLLM(
    model_name="/root/model/internlm2-chat-1_8b",
    tokenizer_name="/root/model/internlm2-chat-1_8b",
    model_kwargs={"trust_remote_code":True},
    tokenizer_kwargs={"trust_remote_code":True}
    )
rsp = llm.chat(messages=[ChatMessage(content="xtuner是什么?")])
print(rsp)

print("=======================================================")
rsp = llm.chat(messages=[ChatMessage(content="讲讲Beauty and Beast的故事")])
print(rsp)

运行结果如下:

2024-08-14_20-33.png

可以看到,模型本身并没有相关的内容。

数据准备

llamaindex_demo目录下创建一个data文件夹,用于保存新的数据文件,。LlamaIndex 支持PDF\TXT\MD等文本数据。

加载xtuner相关资料

mkdir data
cd data 
git clone https://github.com/InternLM/xtuner.git
mv xtuner/README_zh-CN.md ./

创建一个文本文件touch beauty_and_beast.txt,将故事写下:

Beauty and the Beast

    by Anonymous

    Edition     (November     2006)

    BEAUTY AND THE BEAST.

    Once upon a time, in a very far-off country, there lived a mer-chant who had been so fortunate in all his undertakings that hewas  enormously  rich.       As  he  had,  however,  six  sons  and  sixdaughters, he found that his money was not too much to let themall have everything they fancied, as they were accustomed to do.

    But one day a most unexpected misfortune befell them. Theirhouse  caught  fire  and  was  speedily  burnt  to  the  ground,  withall the splendid furniture, the books, pictures, gold, silver, andprecious goods it contained; and this was only the beginning oftheir troubles. Their father, who had until this moment prosperedin all ways, suddenly lost every ship he had upon the sea, eitherby  dint  of  pirates,  shipwreck,  or  fire.    Then  he  heard  that  hisclerks in distant countries, whom he trusted entirely, had provedunfaithful;  and at last from great wealth he fell into the direstpoverty.

    All that he had left was a little house in a desolate place at leasta hundred leagues from the town in which he had lived, and to thishe was forced to retreat with his children, who were in despair atthe idea of leading such a different life. Indeed, the daughters atfirst hoped that their friends, who had been so numerous whilethey were rich, would insist on their staying in their houses nowthey  no  longer  possessed  one.       But  they  soon  found  that  theywere left alone, and that their former friends even attributed theirmisfortunes to their own extravagance, and showed no intentionof offering them any help.  So nothing was left for them but totake their departure to the cottage, which stood in the midst of

    a dark forest, and seemed to be the most dismal place upon theface of the earth.   As they were too poor to have any servants,the girls had to work hard, like peasants, and the sons, for theirpart, cultivated the fields to earn their living.  Roughly clothed,and living in the simplest way, the girls regretted unceasingly theluxuries and amusements of their former life; only the youngesttried to be brave and cheerful.     She had been as sad as anyonewhen misfortune first overtook her father, but, soon recoveringher natural gaiety, she set to work to make the best of things, toamuse her father and brothers as well as she could,  and to tryto persuade her sisters to join her in dancing and singing.         Butthey would do nothing of the sort, and, because she was not asdoleful as themselves, they declared that this miserable life wasall she was fit for.   But she was really far prettier and clevererthan they were; indeed, she was so lovely that she was alwayscalled Beauty. After two years, when they were all beginning toget used to their new life, something happened to disturb theirtranquillity. Their father received the news that one of his ships,which he had believed to be lost, had come safely into port witha  rich  cargo.  All  the  sons  and  daughters  at  once  thought  thattheir poverty was at an end, and wanted to set out directly for thetown;  but their father, who was more prudent, begged them towait a little, and, though it was harvest-time, and he could ill bespared, determined to go himself first, to make inquiries.  Onlythe youngest daughter had any doubt but that they would soonagain be as rich as they were before, or at least rich enough to livecomfortably  in  some  town  where  they  would  find  amusementand gay companions once more.  So they all loaded their fatherwith commissions for jewels and dresses which it would havetaken a fortune to buy; only Beauty, feeling sure that it was ofno use, did not ask for anything. Her father, noticing her silence,said: "And what shall I bring for you, Beauty?"

    "The only thing I wish for is to see you come home safely,"she answered.

    But this reply vexed her sisters, who fancied she was blamingthem for having asked for such costly things.  Her father, how-ever, was pleased, but as he thought that at her age she certainlyought to like pretty presents, he told her to choose something.

    "Well, dear father," she said, "as you insist upon it, I beg thatyou will bring me a rose.      I have not seen one since we camehere, and I love them so much."

    So the merchant set out and reached the town as quickly aspossible, but only to find that his former companions, believinghim to be dead, had divided between them the goods which theship had brought;  and after six months of trouble and expensehe found himself as poor as when he started, having been ableto recover only just enough to pay the cost of his journey.         Tomake  matters  worse,  he  was  obliged  to  leave  the  town  in  themost terrible weather, so that by the time he was within a fewleagues  of  his  home  he  was  almost  exhausted  with  cold  andfatigue. Though he knew it would take some hours to get throughthe forest, he was so anxious to be at his journey's end that heresolved to go on;  but night overtook him, and the deep snowand bitter frost made it impossible for his horse to carry him anyfurther. Not a house was to be seen; the only shelter he could getwas the hollow trunk of a great tree, and there he crouched all thenight, which seemed to him the longest he had ever known.  Inspite of his weariness the howling of the wolves kept him awake,and even when at last the day broke he was not much better off,for the falling snow had covered up every path, and he did notknow which way to turn.

    At length he made out some sort of track, and though at thebeginning it was so rough and slippery that he fell down morethan once, it presently became easier, and led him into an avenueof trees which ended in a splendid castle. It seemed to the mer-chant very strange that no snow had fallen in the avenue, whichwas entirely composed of orange trees, covered with flowers andfruit. When he reached the first court of the castle he saw before

    him a flight of agate steps, and went up them, and passed throughseveral splendidly furnished rooms. The pleasant warmth of theair revived him, and he felt very hungry; but there seemed to benobody in all this vast and splendid palace whom he could askto give him something to eat. Deep silence reigned everywhere,and at last, tired of roaming through empty rooms and galleries,he stopped in a room smaller than the rest, where a clear fire wasburning and a couch was drawn up cosily close to it.  Thinkingthat this must be prepared for someone who was expected, he satdown to wait till he should come, and very soon fell into a sweetsleep.

    When his extreme hunger wakened him after several hours, hewas still alone; but a little table, upon which was a good dinner,had been drawn up close to him, and, as he had eaten nothingfor  twenty-four  hours,  he  lost  no  time  in  beginning  his  meal,hoping that he might soon have an opportunity of thanking hisconsiderate entertainer, whoever it might be.

    But no one appeared, and even after another long sleep, fromwhich he awoke completely refreshed, there was no sign of any-body, though a fresh meal of dainty cakes and fruit was preparedupon  the  little  table  at  his  elbow.    Being  naturally  timid,  thesilence began to terrify him, and he resolved to search once morethrough all the rooms; but it was of no use.  Not even a servantwas to be seen; there was no sign of life in the palace! He beganto wonder what he should do, and to amuse himself by pretendingthat all the treasures he saw were his own, and considering howhe would divide them among his children.  Then he went downinto the garden, and though it was winter everywhere else, herethe sun shone, and the birds sang, and the flowers bloomed, andthe air was soft and sweet. The merchant, in ecstacies with all hesaw and heard, said to himself:

    "All this must be meant for me. I will go this minute and bringmy children to share all these delights."

    In spite of being so cold and weary when he reached the castle,

    he had taken his horse to the stable and fed it. Now he thought hewould saddle it for his homeward journey, and he turned downthe path which led to the stable. This path had a hedge of roseson each side of it, and the merchant thought he had never seen orsmelt such exquisite flowers. They reminded him of his promiseto Beauty, and he stopped and had just gathered one to take toher when he was startled by a strange noise behind him. Turninground, he saw a frightful Beast, which seemed to be very angryand said, in a terrible voice:

    "Who told you that you might gather my roses?           Was it notenough  that  I  allowed  you  to  be  in  my  palace  and  was  kindto you?   This is the way you show your gratitude,  by stealingmy flowers!  But your insolence shall not go unpunished." Themerchant, terrified by these furious words, dropped the fatal rose,and,  throwing himself on his knees,  cried:  "Pardon me,  noblesir. I am truly grateful to you for your hospitality, which was somagnificent that I could not imagine that you would be offendedby my taking such a little thing as a rose." But the Beast's angerwas not lessened by this speech.

    "You are very ready with excuses and flattery," he cried; "butthat will not save you from the death you deserve."

    "Alas!" thought the merchant, "if my daughter Beauty couldonly know what danger her rose has brought me into!"

    And in despair he began to tell the Beast all his misfortunes,and the reason of his journey, not forgetting to mention Beauty'srequest.

    "A king's ransom would hardly have procured all that my otherdaughters asked," he said; "but I thought that I might at least takeBeauty her rose. I beg you to forgive me, for you see I meant noharm."

    The Beast considered for a moment, and then he said, in a lessfurious tone:

    "I will forgive you on one condition--that is, that you will giveme one of your daughters."

    "Ah!" cried the merchant, "if I were cruel enough to buy myown  life  at  the  expense  of  one  of  my  children's,  what  excusecould I invent to bring her here?"

    "No excuse would be necessary," answered the Beast. "If shecomes at all she must come willingly. On no other condition willI have her.   See if any one of them is courageous enough, andloves you well enough to come and save your life. You seem tobe an honest man, so I will trust you to go home.  I give you amonth to see if either of your daughters will come back with you

    and stay here, to let you go free.     If neither of them is willing,you must come alone, after bidding them good-bye for ever, forthen you will belong to me.      And do not imagine that you canhide from me, for if you fail to keep your word I will come andfetch you!" added the Beast grimly.

    The merchant accepted this proposal, though he did not reallythink  any  of  his  daughters  would  be  persuaded  to  come.     Hepromised to return at the time appointed,  and then,  anxious toescape from the presence of the Beast, he asked permission toset off at once. But the Beast answered that he could not go untilthe next day.

    "Then you will find a horse ready for you," he said. "Now goand eat your supper, and await my orders."

    The poor merchant,  more dead than alive,  went back to hisroom, where the most delicious supper was already served on thelittle table which was drawn up before a blazing fire. But he wastoo terrified to eat, and only tasted a few of the dishes, for fearthe Beast should be angry if he did not obey his orders. When hehad finished he heard a great noise in the next room, which heknew meant that the Beast was coming. As he could do nothingto escape his visit, the only thing that remained was to seem aslittle afraid as possible; so when the Beast appeared and askedroughly if he had supped well, the merchant answered humblythat he had, thanks to his host's kindness. Then the Beast warnedhim to remember their agreement,  and to prepare his daughterexactly for what she had to expect.

    "Do not get up to-morrow," he added, "until you see the sunand hear a golden bell ring.  Then you will find your breakfastwaiting for you here, and the horse you are to ride will be readyin the courtyard.   He will also bring you back again when youcome with your daughter a month hence. Farewell. Take a roseto Beauty, and remember your promise!"

    The merchant was only too glad when the Beast went away,and  though  he  could  not  sleep  for  sadness,  he  lay  down  until

    the sun rose.    Then,  after a hasty breakfast,  he went to gatherBeauty's rose, and mounted his horse, which carried him off soswiftly that in an instant he had lost sight of the palace, and hewas still wrapped in gloomy thoughts when it stopped before thedoor of the cottage.

    His sons and daughters, who had been very uneasy at his longabsence,  rushed  to  meet  him,  eager  to  know  the  result  of  hisjourney, which, seeing him mounted upon a splendid horse andwrapped in a rich mantle, they supposed to be favorable. But hehid the truth from them at first, only saying sadly to Beauty ashe gave her the rose:

    "Here is what you asked me to bring you; you little know whatit has cost."

    But  this  excited  their  curiosity  so  greatly  that  presently  hetold them his adventures from beginning to end, and then theywere all very unhappy. The girls lamented loudly over their losthopes, and the sons declared that their father should not returnto  this  terrible  castle,  and  began  to  make  plans  for  killing  theBeast if it should come to fetch him. But he reminded them thathe  had  promised  to  go  back.     Then  the  girls  were  very  angrywith  Beauty,  and  said  it  was  all  her  fault,  and  that  if  she  hadasked for something sensible this would never have happened,and complained bitterly that they should have to suffer for herfolly.

    Poor Beauty, much distressed, said to them:

    "I have indeed caused this misfortune, but I assure you I did itinnocently. Who could have guessed that to ask for a rose in themiddle of summer would cause so much misery? But as I did themischief it is only just that I should suffer for it. I will thereforego back with my father to keep his promise."

    At first nobody would hear of this arrangement, and her fatherand brothers, who loved her dearly, declared that nothing shouldmake them let her go; but Beauty was firm. As the time drew nearshe divided all her little possessions between her sisters, and said

    good-bye to everything she loved, and when the fatal day cameshe encouraged and cheered her father as they mounted togetherthe horse which had brought him back.  It seemed to fly ratherthan  gallop,  but  so  smoothly  that  Beauty  was  not  frightened;indeed, she would have enjoyed the journey if she had not fearedwhat might happen to her at the end of it.  Her father still triedto persuade her to go back, but in vain. While they were talkingthe night fell, and then, to their great surprise, wonderful coloredlights  began  to  shine  in  all  directions,  and  splendid  fireworksblazed out before them; all the forest was illuminated by them,and even felt pleasantly warm, though it had been bitterly coldbefore. This lasted until they reached the avenue of orange trees,where were statues holding flaming torches, and when they gotnearer to the palace they saw that it was illuminated from theroof to the ground, and music sounded softly from the courtyard.

    "The Beast must be very hungry," said Beauty, trying to laugh,

    "if he makes all this rejoicing over the arrival of his prey."

    But, in spite of her anxiety, she could not help admiring allthe wonderful things she saw.

    The horse stopped at the foot of the flight of steps leading tothe terrace, and when they had dismounted her father led her tothe little room he had been in before, where they found a splendidfire burning, and the table daintily spread with a delicious supper.

    The merchant knew that this was meant for them, and Beauty,who was rather less frightened now that she had passed throughso many rooms and seen nothing of the Beast, was quite willing tobegin, for her long ride had made her very hungry. But they hadhardly finished their meal when the noise of the Beast's footstepswas heard approaching, and Beauty clung to her father in terror,which became all the greater when she saw how frightened hewas. But when the Beast really appeared, though she trembled atthe sight of him, she made a great effort to hide her horror, andsaluted him respectfully.

    This evidently pleased the Beast. After looking at her he said,

    in  a  tone  that  might  have  struck  terror  into  the  boldest  heart,though he did not seem to be angry:

    "Good-evening, old man. Good-evening, Beauty."

    The merchant was too terrified to reply, but Beauty answeredsweetly:

    "Good-evening, Beast."

    "Have you come willingly?" asked the Beast.             "Will you becontent to stay here when your father goes away?"

    Beauty answered bravely that she was quite prepared to stay.

    "I am pleased with you," said the Beast.  "As you have comeof your own accord,  you may stay.          As for you,  old man," headded, turning to the merchant, "at sunrise to-morrow you willtake your departure. When the bell rings get up quickly and eatyour breakfast, and you will find the same horse waiting to takeyou home; but remember that you must never expect to see mypalace again."

    Then turning to Beauty, he said:

    "Take your father into the next room, and help him to chooseeverything you think your brothers and sisters would like to have.You will find two traveling-trunks there; fill them as full as youcan.  It is only just that you should send them something veryprecious as a remembrance of yourself."

    Then he went away, after saying, "Good-bye, Beauty; good-bye, old man;" and though Beauty was beginning to think withgreat dismay of her father's departure, she was afraid to disobeythe Beast's orders; and they went into the next room, which hadshelves and cupboards all round it. They were greatly surprisedat the riches it contained.  There were splendid dresses fit for aqueen, with all the ornaments that were to be worn with them;and when Beauty opened the cupboards she was quite dazzledby the gorgeous jewels that lay in heaps upon every shelf. Afterchoosing a vast quantity, which she divided between her sisters--for she had made a heap of the wonderful dresses for each ofthem---she opened the last chest, which was full of gold.

    "I think, father," she said, "that, as the gold will be more usefulto you, we had better take out the other things again, and fill thetrunks with it." So they did this;  but the more they put in,  themore room there seemed to be, and at last they put back all thejewels and dresses they had taken out, and Beauty even added asmany more of the jewels as she could carry at once; and then thetrunks were not too full, but they were so heavy that an elephantcould not have carried them!

    "The Beast was mocking us," cried the merchant;  "he musthave pretended to give us all these things, knowing that I couldnot carry them away."

    "Let us wait and see," answered Beauty. "I cannot believe thathe meant to deceive us.  All we can do is to fasten them up andleave them ready."

    So they did this and returned to the little room, where, to theirastonishment, they found breakfast ready. The merchant ate hiswith a good appetite, as the Beast's generosity made him believethat he might perhaps venture to come back soon and see Beauty.But she felt sure that her father was leaving her for ever, so shewas very sad when the bell rang sharply for the second time, andwarned them that the time was come for them to part. They wentdown  into  the  courtyard,  where  two  horses  were  waiting,  oneloaded with the two trunks, the other for him to ride. They werepawing the ground in their impatience to start, and the merchantwas forced to bid Beauty a hasty farewell; and as soon as he wasmounted he went off at such a pace that she lost sight of him inan instant. Then Beauty began to cry, and wandered sadly backto her own room.  But she soon found that she was very sleepy,and as she had nothing better to do she lay down and instantlyfell asleep.    And then she dreamed that she was walking by abrook bordered with trees, and lamenting her sad fate, when ayoung  prince,  handsomer  than  anyone  she  had  ever  seen,  andwith a voice that went straight to her heart, came and said to her,"Ah, Beauty!  you are not so unfortunate as you suppose.  Here

    you will be rewarded for all you have suffered elsewhere. Yourevery wish shall be gratified. Only try to find me out, no matterhow I may be disguised, as I love you dearly, and in making mehappy you will find your own happiness.  Be as true-hearted asyou are beautiful, and we shall have nothing left to wish for."

    "What can I do, Prince, to make you happy?" said Beauty.

    "Only be grateful," he answered, "and do not trust too muchto your eyes.   And, above all, do not desert me until you havesaved me from my cruel misery."

    After  this  she  thought  she  found  herself  in  a  room  with  astately and beautiful lady, who said to her:

    "Dear Beauty, try not to regret all you have left behind you,for you are destined to a better fate. Only do not let yourself bedeceived by appearances."

    Beauty  found  her  dreams  so  interesting  that  she  was  in  nohurry to awake, but presently the clock roused her by calling hername  softly  twelve  times,  and  then  she  got  up  and  found  herdressing-table set out with everything she could possibly want;and when her toilet was finished she found dinner was waiting inthe room next to hers. But dinner does not take very long whenyou are all by yourself, and very soon she sat down cosily in thecorner of a sofa, and began to think about the charming Princeshe had seen in her dream.

    "He said I could make him happy," said Beauty to herself.

    "It seems, then, that this horrible Beast keeps him a prisoner.How can I set him free? I wonder why they both told me not totrust to appearances?  I don't understand it.  But, after all, it wasonly a dream,  so why should I trouble myself about it?          I hadbetter go and find something to do to amuse myself."

    So she got up and began to explore some of the many roomsof the palace.

    The first she entered was lined with mirrors, and Beauty sawherself reflected on every side, and thought she had never seensuch a charming room. Then a bracelet which was hanging froma  chandelier  caught  her  eye,  and  on  taking  it  down  she  wasgreatly surprised to find that it held a portrait of her unknownadmirer, just as she had seen him in her dream. With great delightshe slipped the bracelet on her arm, and went on into a gallery ofpictures, where she soon found a portrait of the same handsomePrince, as large as life, and so well painted that as she studied ithe seemed to smile kindly at her. Tearing herself away from theportrait at last, she passed through into a room which containedevery  musical  instrument  under  the  sun,  and  here  she  amusedherself  for  a  long  while  in  trying  some  of  them,  and  singinguntil she was tired.   The next room was a library, and she saweverything she had ever wanted to read,  as well as everythingshe had read, and it seemed to her that a whole lifetime wouldnot be enough even to read the names of the books, there were so

    many. By this time it was growing dusk, and wax candles in dia-mond and ruby candlesticks were beginning to light themselvesin every room.

    Beauty found her supper served just at the time she preferredto have it, but she did not see anyone or hear a sound, and, thoughher father had warned her that she would be alone, she began tofind it rather dull.

    But  presently  she  heard  the  Beast  coming,      and  wonderedtremblingly if he meant to eat her up now.

    However, as he did not seem at all ferocious, and only saidgruffly:

    "Good-evening, Beauty," she answered cheerfully and man-aged to conceal her terror.      Then the Beast asked her how shehad been amusing herself, and she told him all the rooms she hadseen.

    Then he asked if she thought she could be happy in his palace;and Beauty answered that everything was so beautiful that shewould be very hard to please if she could not be happy. And afterabout an hour's talk Beauty began to think that the Beast was notnearly so terrible as she had supposed at first. Then he got up toleave her, and said in his gruff voice:

    "Do you love me, Beauty? Will you marry me?"

    "Oh!   what shall I say?" cried Beauty, for she was afraid tomake the Beast angry by refusing.

    "Say 'yes' or 'no' without fear," he replied.

    "Oh! no, Beast," said Beauty hastily.

    "Since you will not,  good-night,  Beauty," he said.        And sheanswered:

    "Good-night, Beast," very glad to find that her refusal had notprovoked him. And after he was gone she was very soon in bedand asleep, and dreaming of her unknown Prince.  She thoughthe came and said to her:

    "Ah, Beauty! why are you so unkind to me? I fear I am fatedto be unhappy for many a long day still."

    And then her dreams changed, but the charming Prince figuredin them all; and when morning came her first thought was to lookat the portrait and see if it was really like him, and she found thatit certainly was.

    This  morning  she  decided  to  amuse  herself  in  the  garden,for the sun shone,  and all the fountains were playing;  but shewas astonished to find that every place was familiar to her, andpresently  she  came  to  the  brook  where  the  myrtle  trees  weregrowing where she had first met the Prince in her dream, and thatmade her think more than ever that he must be kept a prisonerby the Beast.  When she was tired she went back to the palace,and found a new room full of materials for every kind of work--ribbons to make into bows, and silks to work into flowers. Thenthere was an aviary full of rare birds, which were so tame thatthey flew to Beauty as soon as they saw her, and perched uponher shoulders and her head.

    "Pretty little creatures," she said, "how I wish that your cagewas nearer to my room, that I might often hear you sing!"

    So saying she opened a door, and found to her delight that itled into her own room, though she had thought it was quite theother side of the palace.

    There were more birds in a room farther on, parrots and cock-atoos that could talk, and they greeted Beauty by name; indeed,she found them so entertaining that she took one or two back toher room, and they talked to her while she was at supper; afterwhich  the  Beast  paid  her  his  usual  visit,  and  asked  the  samequestions as before, and then with a gruff "good-night" he tookhis departure, and Beauty went to bed to dream of her mysteriousPrince.   The days passed swiftly in different amusements,  andafter a while Beauty found out another strange thing in the palace,which often pleased her when she was tired of being alone. Therewas  one  room  which  she  had  not  noticed  particularly;  it  wasempty,   except  that  under  each  of  the  windows  stood  a  verycomfortable chair;  and the first time she had looked out of the

    window it had seemed to her that a black curtain prevented herfrom  seeing  anything  outside.    But  the  second  time  she  wentinto the room, happening to be tired, she sat down in one of thechairs, when instantly the curtain was rolled aside, and a mostamusing pantomime was acted before her; there were dances andcolored lights, and music, and pretty dresses, and it was all sogay that Beauty was in ecstacies.  After that she tried the otherseven windows in turn, and there was some new and surprisingentertainment to be seen from each of them, so that Beauty nevercould feel lonely any more. Every evening after supper the Beastcame to see her, and always before saying good-night asked herin his terrible voice:

    "Beauty, will you marry me?"

    And  it  seemed  to  Beauty,  now  she  understood  him  better,that when she said, "No, Beast," he went away quite sad.          Buther happy dreams of the handsome young Prince soon made herforget the poor Beast, and the only thing that at all disturbed herwas to be constantly told to distrust appearances, to let her heartguide her, and not her eyes, and many other equally perplexingthings, which, consider as she would, she could not understand.

    So everything went on for a long time, until at last, happy asshe was, Beauty began to long for the sight of her father and herbrothers and sisters; and one night, seeing her look very sad, theBeast asked her what was the matter. Beauty had quite ceased tobe afraid of him. Now she knew that he was really gentle in spiteof his ferocious looks and his dreadful voice.  So she answeredthat she was longing to see her home once more.  Upon hearingthis the Beast seemed sadly distressed, and cried miserably.

    "Ah!  Beauty, have you the heart to desert an unhappy Beastlike  this? What  more  do  you  want  to  make  you  happy?     Is  itbecause you hate me that you want to escape?"

    "No, dear Beast," answered Beauty softly, "I do not hate you,and I should be very sorry never to see you any more, but I longto see my father again.    Only let me go for two months,  and I

    promise to come back to you and stay for the rest of my life."

    The Beast, who had been sighing dolefully while she spoke,now replied:

    "I cannot refuse you anything you ask, even though it shouldcost me my life.  Take the four boxes you will find in the roomnext to your own, and fill them with everything you wish to takewith you. But remember your promise and come back when thetwo months are over, or you may have cause to repent it, for ifyou do not come in good time you will find your faithful Beastdead.   You will not need any chariot to bring you back.          Onlysay good-bye to all your brothers and sisters the night before youcome away, and when you have gone to bed turn this ring roundupon your finger and say firmly: 'I wish to go back to my palaceand  see  my  Beast  again.'   Good-night,  Beauty.      Fear  nothing,sleep peacefully, and before long you shall see your father oncemore."

    As soon as Beauty was alone she hastened to fill the boxeswith all the rare and precious things she saw about her, and onlywhen she was tired of heaping things into them did they seem tobe full.

    Then she went to bed,  but could hardly sleep for joy.          Andwhen at last she did begin to dream of her beloved Prince shewas  grieved  to  see  him  stretched  upon  a  grassy  bank  sad  andweary, and hardly like himself.

    "What is the matter?" she cried.

    But he looked at her reproachfully, and said:

    "How can you ask me, cruel one? Are you not leaving me tomy death perhaps?"

    "Ah! don't be so sorrowful," cried Beauty; "I am only going toassure my father that I am safe and happy.  I have promised theBeast faithfully that I will come back, and he would die of griefif I did not keep my word!"

    "What would that matter to you?" said the Prince. "Surely youwould not care?"

    "Indeed I should be ungrateful if I did not care for such a kindBeast," cried Beauty indignantly. "I would die to save him frompain. I assure you it is not his fault that he is so ugly."

    Just then a strange sound woke her--someone was speakingnot very far away; and opening her eyes she found herself in aroom she had never seen before, which was certainly not nearlyso splendid as those she was used to in the Beast's palace. Wherecould  she  be?   She  got  up  and  dressed  hastily,  and  then  sawthat the boxes she had packed the night before were all in theroom.  While she was wondering by what magic the Beast hadtransported them and herself to this strange place she suddenlyheard her father's voice, and rushed out and greeted him joyfully.Her brothers and sisters were all astonished at her appearance,as they had never expected to see her again, and there was noend to the questions they asked her.  She had also much to hearabout what had happened to them while she was away, and ofher father's journey home. But when they heard that she had onlycome to be with them for a short time, and then must go back tothe Beast's palace for ever, they lamented loudly.  Then Beautyasked her father what he thought could be the meaning of herstrange dreams,  and why the Prince constantly begged her notto trust to appearances.  After much consideration he answered:"You tell me yourself that the Beast, frightful as he is, loves youdearly, and deserves your love and gratitude for his gentlenessand kindness;  I think the Prince must mean you to understandthat you ought to reward him by doing as he wishes you to, inspite of his ugliness."

    Beauty could not help seeing that this seemed very probable;still, when she thought of her dear Prince who was so handsome,she did not feel at all inclined to marry the Beast.  At any rate,for  two  months  she  need  not  decide,  but  could  enjoy  herselfwith her sisters.  But though they were rich now, and lived in atown again, and had plenty of acquaintances, Beauty found thatnothing  amused  her  very  much;  and  she  often  thought  of  the

    palace, where she was so happy, especially as at home she neveronce dreamed of her dear Prince, and she felt quite sad withouthim.

    Then her sisters seemed to have got quite used to being withouther, and even found her rather in the way, so she would not havebeen  sorry  when  the  two  months  were  over  but  for  her  fatherand brothers, who begged her to stay, and seemed so grieved atthe thought of her departure that she had not the courage to saygood-bye to them. Every day when she got up she meant to sayit at night, and when night came she put it off again, until at lastshe had a dismal dream which helped her to make up her mind.She thought she was wandering in a lonely path in the palacegardens,  when  she  heard  groans  which  seemed  to  come  fromsome bushes hiding the entrance of a cave, and running quicklyto see what could be the matter, she found the Beast stretchedout upon his side, apparently dying.  He reproached her faintlywith being the cause of his distress, and at the same moment astately lady appeared, and said very gravely:

    "Ah!  Beauty, you are only just in time to save his life.  Seewhat happens when people do not keep their promises!              If youhad delayed one day more, you would have found him dead."

    Beauty was so terrified by this dream that the next morningshe announced her intention of going back at once, and that verynight she said good-bye to her father and all her brothers andsisters, and as soon as she was in bed she turned her ring roundupon her finger, and said firmly:

    "I wish to go back to my palace and see my Beast again," asshe had been told to do.

    Then she fell asleep instantly, and only woke up to hear theclock saying, "Beauty, Beauty," twelve times in its musical voice,which  told  her  at  once  that  she  was  really  in  the  palace  oncemore. Everything was just as before, and her birds were so gladto see her! but Beauty thought she had never known such a longday, for she was so anxious to see the Beast again that she felt as

    if supper-time would never come.

    But when it did come and no Beast appeared she was really

    frightened;  so,  after listening and waiting for a long time,  she

    ran down into the garden to search for him.          Up and down the

    paths and avenues ran poor Beauty, calling him in vain, for no

    one answered, and not a trace of him could she find; until at last,

    quite tired, she stopped for a minute's rest, and saw that she was

    standing opposite the shady path she had seen in her dream. She

    rushed  down  it,  and,  sure  enough,  there  was  the  cave,  and  in

    it lay the Beast--asleep, as Beauty thought.  Quite glad to have

    found him, she ran up and stroked his head, but to her horror he

    did not move or open his eyes.

    "Oh!  he is dead; and it is all my fault," said Beauty, crying

    bitterly.

    But then, looking at him again, she fancied he still breathed,

    and, hastily fetching some water from the nearest fountain, she

    sprinkled it over his face, and to her great delight he began to

    revive.

    "Oh! Beast, how you frightened me!" she cried. "I never knewhow much I loved you until just now, when I feared I was toolate to save your life."

    "Can you really love such an ugly creature as I am?" said theBeast faintly.  "Ah!  Beauty, you only came just in time.  I wasdying because I thought you had forgotten your promise. But goback now and rest, I shall see you again by-and-by."

    Beauty, who had half expected that he would be angry withher,  was  reassured  by  his  gentle  voice,  and  went  back  to  thepalace, where supper was awaiting her; and afterwards the Beastcame in as usual, and talked about the time she had spent withher father, asking if she had enjoyed herself, and if they had allbeen very glad to see her.

    Beauty answered politely,  and quite enjoyed telling him allthat had happened to her.        And when at last the time came forhim to go, and he asked, as he had so often asked before:

    "Beauty, will you marry me?" she answered softly:

    "Yes, dear Beast."

    As she spoke a blaze of light sprang up before the windowsof the palace;  fireworks crackled and guns banged, and acrossthe avenue of orange trees, in letters all made of fire-flies, waswritten: "Long live the Prince and his Bride."

    Turning to ask the Beast what it could all mean, Beauty foundthat he had disappeared,  and in his place stood her long-lovedPrince! At the same moment the wheels of a chariot were heardupon the terrace, and two ladies entered the room. One of themBeauty recognized as the stately lady she had seen in her dreams;the other was also so grand and queenly that Beauty hardly knewwhich to greet first.

    But the one she already knew said to her companion:

    "Well,  Queen,  this  is  Beauty,  who  has  had  the  courage  torescue your son from the terrible enchantment.              They love oneanother,  and only your consent to their marriage is wanting tomake them perfectly happy."

    "I consent with all my heart," cried the Queen.              "How can Iever thank you enough,  charming girl,  for having restored mydear son to his natural form?"

    And then she tenderly embraced Beauty and the Prince, whohad meanwhile been greeting the Fairy and receiving her con-gratulations.

    "Now," said the Fairy to Beauty, "I suppose you would likeme  to  send  for  all  your  brothers  and  sisters  to  dance  at  yourwedding?"

    And so she did, and the marriage was celebrated the very nextday with the utmost splendor, and Beauty and the Prince livedhappily ever after.

开始实现RAG

完成以上步骤,我们准备好了语言模型和自己的数据文件。

接下来使用llamaindex实现RAG。

创建llamaindex_internlm_rag.py输入我们的RAG的代码:



from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings

from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.huggingface import HuggingFaceLLM

#初始化一个HuggingFaceEmbedding对象,用于将文本转换为向量表示
embed_model = HuggingFaceEmbedding(
#指定了一个预训练的sentence-transformer模型的路径
    model_name="/root/model/sentence-transformer"
)
#将创建的嵌入模型赋值给全局设置的embed_model属性,
#这样在后续的索引构建过程中就会使用这个模型。
Settings.embed_model = embed_model

llm = HuggingFaceLLM(
    model_name="/root//model/internlm2-chat-1_8b",
    tokenizer_name="/root/model/internlm2-chat-1_8b",
    # model_name="/root/share/model_repos/internlm2-chat-7b",
    # tokenizer_name="/root/share/model_repos/internlm2-chat-7b",
    model_kwargs={"trust_remote_code":True},
    tokenizer_kwargs={"trust_remote_code":True}
)
#设置全局的llm属性,这样在索引查询时会使用这个模型。
Settings.llm = llm

#从指定目录读取所有文档,并加载数据到内存中
documents = SimpleDirectoryReader("/root/llamaindex_demo/data").load_data()
#创建一个VectorStoreIndex,并使用之前加载的文档来构建索引。
# 此索引将文档转换为向量,并存储这些向量以便于快速检索。
index = VectorStoreIndex.from_documents(documents)
# 创建一个查询引擎,这个引擎可以接收查询并返回相关文档的响应。
query_engine = index.as_query_engine()


while True:
    word = input("user>>>>")
    if word =='exit':
        break

    if word is None or word =='' or str(word).isspace():
        continue

    response = query_engine.query(word)
    print(response)

继续提问,结果如下:

image.png

得到了与资料相关的回答。 image.png

小结

至此,一个RAG基础demo完成。

根据此demo,还可以使用更加优化的embedding模型,解析后的index也可以保存到向量数据库,而不是每次启动都要解析,优化的方向的很多。