Technology sharing

LLM_Getting Coepi Guide (Aedificium magnae Model a Nulla Basics)

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Articulus hic maxime suggerit prompta ad exempla magna et ad paedagogia practica dat. Etiam nulla cum fundamento magna exempla aedificari possunt.

Contentus: Mentis rudimenta initialis adiuvat ad condensandam et augendam internas vires et fundamentum ad subsequentem institutionem ponit.

1、promptum

1.1 Meaning and function

promptum significat impulsum rei. In magna lingua exempla, "promptum" refertur ad textum initus seu instructiones ab usuario ad exemplar ut exemplar dirigat ad output generandum respondentem. Promptus munus vitale agit, cum exemplar inter se implicat, quod ad intellectum exemplar, accurate respondere et ad creativum contentum afficit. Praecipuae significationes et functiones promptae sunt in magnis exemplaribus;

a.exemplar generationis dux : Promptus praebet initiales informationes et directiones ab exemplari requirendas ad responsionem vel output generandam. Quaestiones, instructiones, keywords aut notitia contextualis continere potest ut exemplar adiuvet ad intentiones et necessitates utentis cognoscendas, ac generatim informationes debitas in hac fundata.

b.contextual intellectus : Exemplar, per suggerit, potest intellegere contextum hodiernae conversationis vel muneris. Hoc criticum est curare ut exemplar responsiones gignat quae ad exspectationes usuin pertinentes et cohaerentes sunt, praesertim in diuturno interactiones vel multiplices circumscriptiones colloquiorum.

c : Dilucida et prompta ad accuratiorem respondere consuevit. Si promptus quaestionem usoris describit vel evidenter egere potest, exemplum facilius informationes vel solutiones expectatas praebere potest.

d : Etsi promptum regimen ad exemplar praebet, creativity non circumscribat exemplar ac diversitas output. Apta prompta exempla inspirare possunt ad solutiones novas vel non-obvias producendas, servato accuratione.

e.negotium naturae : In aliqua applicatione missionum promptum potest specificare opera quae exemplar indiget ad praestandum vel genus generationis, ut interrogationibus respondens, suggestiones praebens, missiones describens, etc. Haec moderatio adiuvat ut exemplar output aequet necessitates cuiusdam operis vel applicationis.

f.Lingua stylo et forma : Per promptum, utentes linguae desideratae rationem reddere possunt, structuram respondere vel formas certas informationes. Hoc criticum est ut altiorem qualitatem exemplaris output et experientiam usoris curet.

In summa, promptus est pons et ductor pro commercio inter usorum et exemplorum in magna lingua exempla, et munus praecipuum agit in affectu subtilitatis, congruentiae et diversitatis exemplarium. Efficaciter aedificatio et usus suggerentes signanter emendare possunt utilitatem et alacritatem exemplaris tui.

1.2 Principia

Cum exempla in magna lingua promptorum tractamus, plura principia sunt quae considerari et sequi solent;

a: promptum debet clare exprimere necessitates vel quaestiones usoris, ac vagas vel ambiguas vitare descriptiones ad curare ut exemplar percipere ac providere possit congruentia et accurata responsa.

b.contextual ductu: Si quaestio vel necessitas involvit peculiarem condicionem vel contextum, promptum debet continere necessarias notitias ad exemplar intellegendum adiuvandum, sicut ad informationes pertinentes keywords vel relatas.

c.propria instructiones : Promptum debet continere instructiones seu interrogationes specificas ut exemplar cognoscat genus et formas de output ab usore expectatum. Exempli gratia, cum de attractionibus urbis in urbe quaerimus, perspicue exprimere potes te nominibus attractionis, actionis suggestionibus, informationibus translationis indigere.

d: Si utile est aliquem contextum et moderamen praebere, ne nimis instruat vel nimis subtiliter provideat, ne creatrix exemplaris ac responsionum diversitate circumscribatur.

e.Lingua brevis et clara est: Scribere prompte prompte, presse, ac naturali sermone ut exemplar facile intelligat ac procedat.

f: In applicationibus realibus mundi, interest probare et aptare suadet ut exemplar opera quae exspectantur et varias initibus efficienter tractare possit.

Haec principia adiuvant ut magna exemplaria linguae efficaces sint, accuratae et efficaces in eorum interactionibus cum usoribus.

In summa: promptum est principium, ac admonitus, moderatio et norma ad magnum exemplar praebens.

1.3 Ritus artes

a

eg: Da paragraphum et pete GPT ad compendiose.

  1. from tool import get_completion
  2. text = """您应该提供尽可能清晰、具体的指示,以表达您希望模型执行的任务。这将引导模型朝向所
  3. 需的输出,并降低收到无关或不正确响应的可能性。不要将写清晰的提示词与写简短的提示词混淆。在
  4. 许多情况下,更长的提示词可以为模型提供更多的清晰度和上下文信息,从而导致更详细和相关的输出。
  5. """ # 需要总结的文本内容
  6. prompt = f"""把用三个反引号括起来的文本总结成一句话。```

            本文主要介绍大模型的prompt,并且给出实战教程。即使零基础也可以实现大模型的搭建。

    内容:初级阶段的修炼心法,帮助凝聚和提升内力,为后续修炼打下基础。

    1、prompt

    1.1含义和作用

            prompt就是提示工程的意思。在大型语言模型中,"prompt"(提示)指的是用户提供给模型的输入文本或指令,用来指导模型生成相应的输出。Prompt在与模型交互时起着至关重要的作用,它影响着模型的理解、回答的准确性和内容的创造性。以下是prompt在大模型中的主要含义和作用:

            a. 指导模型生成:Prompt提供了模型生成回复或输出所需的初始信息和方向。它可以包含问题、指令、关键词或上下文信息,帮助模型理解用户的意图和需要,并基于此进行相应的生成。

            b. 上下文理解:通过prompt,模型能够了解当前对话或任务的上下文。这对于确保模型生成与用户期望相关和连贯的回复至关重要,特别是在长期交互或多轮对话中。

            c.回答准确性:一个清晰和详细的prompt通常会导致更准确的回答。如果prompt描述清楚用户的问题或需求,模型就能更容易地提供符合预期的信息或解决方案。

            d.生成多样性:尽管prompt为模型提供了指导,但它并不限制模型的创造性和输出的多样性。合适的prompt可以在保持准确性的同时,激发模型产生新颖或非显而易见的解答。

            e. 任务定向:在一些应用场景中,prompt可以具体指定模型需要执行的任务或生成的类型,如回答问题、提供建议、描述情景等。这种指导有助于确保模型输出与特定任务或应用的需求相符。

            f. 语言风格和格式:通过prompt,用户可以设定期望的语言风格、回答的结构或特定的信息格式。这对于确保模型输出的整体质量和用户体验至关重要。

            总之,prompt在大型语言模型中充当了用户与模型之间交互的桥梁和指南,对于影响模型输出的准确性、相关性和多样性起着关键作用。有效地构建和使用prompt可以显著提高模型的实用性和响应能力。

    1.2原则

            在处理大型语言模型中的prompt时,有几个原则是通常被考虑和遵循的:

            a.清晰和简洁的指导:prompt应该明确表达用户的需求或者问题,避免模糊或多义的描述,以确保模型能够理解并提供相关和准确的回答。

            b. 上下文的引导:如果问题或需求涉及到特定的背景或上下文,prompt应该包含必要的信息来帮助模型理解,比如相关的关键词或相关信息。

            c. 具体的指令:prompt中应包含具体的指令或问题,以便模型知道用户期望的输出类型和格式。例如,询问一个城市的旅游景点时,可以明确指定需要景点名称、活动建议或交通信息等。

            d.避免过度指导:虽然提供一些上下文和指导是有益的,但避免过度指导或提供过多细节,以免限制模型的创造性和回答的多样性。

            e. 语言简洁明了:使用清晰、简洁和自然的语言编写prompt,以便模型能够轻松理解和处理。

            f.测试和调整:在实际应用中,对prompt进行测试和调整是很重要的,以确保模型能够按预期工作,并且能够有效地处理各种输入。

            这些原则有助于确保大型语言模型能够在与用户的交互中表现出高效、准确和有创意的特性。

            总之:prompt就是一个起点,给大模型一个提示、引导和规范的作用。

    1.3使用技巧

            a.分隔符

            eg:给出一段话并要求 GPT 进行总结,在该示例中我们使用 ``` 来作为分隔符。

    1. from tool import get_completion
    2. text = """您应该提供尽可能清晰、具体的指示,以表达您希望模型执行的任务。这将引导模型朝向所
    3. 需的输出,并降低收到无关或不正确响应的可能性。不要将写清晰的提示词与写简短的提示词混淆。在
    4. 许多情况下,更长的提示词可以为模型提供更多的清晰度和上下文信息,从而导致更详细和相关的输出。
    5. """ # 需要总结的文本内容
    6. prompt = f"""把用三个反引号括起来的文本总结成一句话。```{text}```""" # 指令内容,使用 ``` 来分隔指令和待总结的内容
    7. response = get_completion(prompt)
    8. print(response)
    9. # 为了获得所需的输出,您应该提供清晰、具体的指示,避免与简短的提示词混淆,并使用更长的提示
    10. 词来提供更多的清晰度和上下文信息。

              b.结构化输出

            eg:生成三本书的标题、作者和类别,并以 JSON 的格式返回,为便于解析,我们指定了 Json 的键: book_id、title、author、genre。

    1. prompt = f"""请生成包括书名、作者和类别的虚构的、非真实存在的中文书籍清单,并以 JSON 格式
    2. 提供,其中包含以下键:book_id、title、author、genre。"""
    3. response = get_completion(prompt)
    4. print(response)
    5. [
    6. { "book_id": 1, "title": "幻境之夜", "author": "李梦飞", "genre": "奇幻小说"
    7. },
    8. ... ]

            c.参考示例

             Zero-Shot提示:模型只根据任务的描述生成响应,不需要任何示例。

             One-Shot提示:只提供一个例子。

            Few-Shot提示:提供几个例子。在提示中的作用是通过少量样本引导模型对特定任务进行学习和执行,例如通过提供少量风格或主题示例,引导模型产出具有相似风格或主题的创作。

            d.让模型充当角色

            示例:请以莎士比亚戏剧中的哈姆雷特的身份解释“生存还是毁灭,这是一个问题”。这个示例要求模型以莎士比亚戏剧《哈姆雷特》中主人公的角色来解释著名的“生存还是毁灭”的问题,以展示模型在不同角色中的表现能力和语境理解能力。

    2、模型实战

            项目任务(三大业务场景):

            1.文本分类

            2.文本信息抽取

            3.文本匹配

            大模型选择:ChatGLM-6B

            采用方法:基于Few-Shot+Zero-Shot以及Instrunction的思想,设计prompt, 进而应用ChatGLM-6B模型完成相应的任务

    2.1 准备工作

            a.我们运用python环境来执行大模型,所以首先需要下载python,(建议用anaconda)

            b.下载ChatGLM-6B模型模型,链接如下:

                    https://github.com/THUDM/ChatGLM-6B?tab=readme-ov-file

                    README中介绍了ChatGLM-6B模型和硬件需求。

    量化等级最低 GPU 显存(推理)最低 GPU 显存(高效参数微调)
    FP16(无量化)13 GB14 GB
    INT88 GB9 GB
    INT46 GB7 GB

            c.在你的anaconda中安装需要的依赖。在前面github中下载的包中,它其实把所有需要的依赖都放在了requirements.txt中,直接输入:

            pip install -r requirements.txt

            如果下载速度很慢,加上清华镜像

            pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

            其中 transformers 库版本推荐为 4.27.1,但理论上不低于 4.23.1 即可。

            此外,如果需要在 cpu 上运行量化后的模型,还需要安装 gcc 与 openmp。多数 Linux 发行版默认已安装。Windows 测试环境 gcc 版本为 TDM-GCC 10.3.0, Linux 为 gcc 11.3.0

            d.从本地加载模型:

            以上代码会由 transformers 自动下载模型实现和参数。完整的模型实现可以在 Hugging Face Hub。如果你的网络环境较差,下载模型参数可能会花费较长时间甚至失败。此时可以先将模型下载到本地,然后从本地加载。

            从 Hugging Face Hub 下载模型需要先安装Git LFS,然后运行

    git clone https://huggingface.co/THUDM/chatglm-6b

    如果你从 Hugging Face Hub 上下载 checkpoint 的速度较慢,可以只下载模型实现

    GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/THUDM/chatglm-6b

            然后从这里手动下载模型参数文件,并将下载的文件替换到本地的 chatglm-6b 目录下。

            将模型下载到本地之后,将以上代码中的 THUDM/chatglm-6b 替换为你本地的 chatglm-6b 文件夹的路径,即可从本地加载模型。

            MAC注意

            Mac直接加载量化后的模型出现提示 `clang: error: unsupported option '-fopenmp'

            这是由于Mac由于本身缺乏omp导致的,此时可运行但是单核。需要单独安装 openmp 依赖,即可在Mac下使用OMP:

    1. # 参考`https://mac.r-project.org/openmp/`
    2. ## 假设: gcc(clang)是14.x版本,其他版本见R-Project提供的表格
    3. curl -O https://mac.r-project.org/openmp/openmp-14.0.6-darwin20-Release.tar.gz
    4. sudo tar fvxz openmp-14.0.6-darwin20-Release.tar.gz -C /

    此时会安装下面几个文件:/usr/local/lib/libomp.dylib/usr/local/include/ompt.h/usr/local/include/omp.h/usr/local/include/omp-tools.h

    注意:如果你之前运行ChatGLM项目失败过,最好清一下Huggingface的缓存,i.e. 默认下是 rm -rf ${HOME}/.cache/huggingface/modules/transformers_modules/chatglm-6b-int4。由于使用了rm命令,请明确知道自己在删除什么。

            这是官方给的解决方案,我没成功。我是在代码上加入如下:

    1. import os
    2. os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

            防止了mac执行报错。

    2.2 文本分类

            我们的目的是期望模型能够帮助我们识别出这4段话中,每一句话描述的是一个什么类型的报告。

    1. sentences = [
    2.         "今天,央行决定通过降低利率来刺激经济增长。这一决策预计会影响到贷款利率,并在接下来的几个季度对金融市场产生深远影响。",
    3.         "ABC公司今日宣布,他们已成功收购了XYZ公司的股权。这一重要的收购交易有助于ABC公司扩展业务范围,增强市场竞争力。据悉,这次收购将进一步巩固ABC公司在行业中的地位,并为未来的业务发展提供更广阔的空间。详细信息请参阅公司官方网站公告栏。",
    4.         "公司资产负债表显示,公司偿债能力强劲,现金流充足,为未来投资和扩张提供了坚实的财务基础。",

            对于大模型来讲,prompt 的设计非常重要,一个 明确 的 prompt 能够帮助我们更好从大模型中获得我们想要的结果。

    在该任务的 prompt 设计中,我们主要考虑 2 点:

            1、需要向模型解释什么叫作「文本分类任务」

            2、需要让模型按照我们指定的格式输出

            2.2.1导入包
    1. """
    2. 利用 LLM 进行文本分类任务。
    3. """
    4. from rich import print
    5. from rich.console import Console
    6. from transformers import AutoTokenizer, AutoModel
    7. import os
    8. os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
            2.2.2 定义init_prompts函数
    1. # 提供所有类别以及每个类别下的样例
    2. class_examples ={
    3. '新闻报道':'今日,股市经历了一轮震荡,受到宏观经济数据和全球贸易紧张局势的影响。投资者密切关注美联储可能的政策调整,以适应市场的不确定性。',
    4. '财务报告':'本公司年度财务报告显示,去年公司实现了稳步增长的盈利,同时资产负债表呈现强劲的状况。经济环境的稳定和管理层的有效战略执行为公司的健康发展奠定了基础。',
    5. '公司公告':'本公司高兴地宣布成功完成最新一轮并购交易,收购了一家在人工智能领域领先的公司。这一战略举措将有助于扩大我们的业务领域,提高市场竞争力',
    6. '分析师报告':'最新的行业分析报告指出,科技公司的创新将成为未来增长的主要推动力。云计算、人工智能和数字化转型被认为是引领行业发展的关键因素,投资者应关注这些趋势'}
    7. # 定义init_prompts函数
    8. definit_prompts():
    9. '''
    10.     这里是对函数的功能进行注释,方便他人理解:该函数的目的是初始化前置prompt,便于模型做Few-shot
    11.     :return: dict字典
    12.     '''
    13.     class_list =list(class_examples.keys())
    14. print(f'分类的类别数:{class_list}')
    15.     pre_history =[
    16. (f'现在你是一个文本分类器,你需要按照要求将我给你的句子分类到:{class_list}类别中。',
    17. f'好的。')
    18. ]
    19. # 遍历给的示例样本
    20. for _type, example in class_examples.items():
    21. # print(f'键--》{_type}')
    22. # print(f'值--》{example}')
    23.         pre_history.append((f'"{example}"是{class_list}里的什么类别', _type))
    24. # print(f'pre_history--》{pre_history}')
    25. return{"class_list":class_list,"pre_history":pre_history}

            a.提供一个Few-shot的样例,标注每个类别对应的话

            b.将类别存储到class_list中

            c.在样本提供之前给予一个prompt。即告诉模型。你是个文本分类器,要具体做什么?并给予一个答案(以逗号分隔)。(相当于一个有监督的训练)

            d.遍历给的示例样本,添加到pre_history

            e.返回一个字典。

            打印的结果如下:

    1. 分类的类别数:['新闻报道','财务报告','公司公告','分析师报告']
    2. 键--》新闻报道
    3. 值--》今日,股市经历了一轮震荡,受到宏观经济数据和全球贸易紧张局势的影响。投资者密切关注美联储可能的政策调整,以适应市场的不确定性。
    4. 键--》财务报告
    5. 值--》本公司年度财务报告显示,去年公司实现了稳步增长的盈利,同时资产负债表呈现强劲的状况。经济环境的稳定和管理层的有效战略执行为公司的健康发展奠定了基础。
    6. 键--》公司公告
    7. 值--》本公司高兴地宣布成功完成最新一轮并购交易,收购了一家在人工智能领域领先的公司。这一战略举措将有助于扩大我们的业务领域,提高市场竞争力
    8. 键--》分析师报告
    9. 值--》最新的行业分析报告指出,科技公司的创新将成为未来增长的主要推动力。云计算、人工智能和数字化转型被认为是引领行业发展的关键因素,投资者应关注这些趋势
    10. pre_history--》[("现在你是一个文本分类器,你需要按照要求将我给你的句子分类到:['新闻报道', '财务报告', '公司公告', '分析师报告']类别中。",'好的。'),('"今日,股市经历了一轮震荡,受到宏观经济数据和全球贸易紧张局势的影响。投资者密切关注美联储可能的政策调整,以适应市场的不确定性。"是['新闻报道', '财务报告', '公司公告', '分析师报告']里的什么类别','新闻报道'),('"本公司年度财务报告显示,去年公司实现了稳步增长的盈利,同时资产负债表呈现强劲的状况。经济环境的稳定和管理层的有效战略执行为公司的健康发展奠定了基础。"是['新闻报道', '财务报告', '公司公告', '分析师报告']里的什么类别','财务报告'),('"本公司高兴地宣布成功完成最新一轮并购交易,收购了一家在人工智能领域领先的公司。这一战略举措将有助于扩大我们的业务领域,提高市场竞争力"是['新闻报道', '财务报告', '公司公告', '分析师报告']里的什么类别','公司公告'),('"最新的行业分析报告指出,科技公司的创新将成为未来增长的主要推动力。云计算、人工智能和数字化转型被认为是引领行业发展的关键因素,投资者应关注这些趋势"是['新闻报道', '财务报告', '公司公告', '分析师报告']里的什么类别','分析师报告')]
    2.2.3 定义inference函数
    1.     sentences = [
    2. "今天,央行决定通过降低利率来刺激经济增长。这一决策预计会影响到贷款利率,并在接下来的几个季度对金融市场产生深远影响。",
    3. "ABC公司今日宣布,他们已成功收购了XYZ公司的股权。这一重要的收购交易有助于ABC公司扩展业务范围,增强市场竞争力。据悉,这次收购将进一步巩固ABC公司在行业中的地位,并为未来的业务发展提供更广阔的空间。详细信息请参阅公司官方网站公告栏。",
    4. "公司资产负债表显示,公司偿债能力强劲,现金流充足,为未来投资和扩张提供了坚实的财务基础。",
    5. "最新的分析报告指出,可再生能源行业预计将在未来几年经历持续增长,投资者应该关注这一领域的投资机会",
    6. ]
    7. definference(sentences: list,
    8.               custom_settings: dict):
    9. """
    10.     推理函数。
    11.     Args:
    12.         sentences (List[str]): 待推理的句子。
    13.         custom_settings (dict): 初始设定,包含人为给定的 few-shot example。
    14.     """
    15. for sentence in sentences:
    16. #没啥含义,就是改变打印的颜色。
    17. with console.status("[bold bright_green] Model Inference..."):
    18. #
    19.             sentence_prompt =f'"{sentence}"是{custom_settings["class_list"]}里的什么类别?'
    20.             response, history = model.chat(tokenizer, sentence_prompt, history=custom_settings['pre_history'])
    21. print(f'>>>[bold bright_red]sentence:{sentence}')
    22. print(f'>>>[bold bright_green]inference answer:{response}')
    23. print(f'history-->{history}')
    24. print("*"*80)

    输入

    1、sentences :待推理的句子。

    2、custom_settings :init_prompts准备好的 提示工程。

    流程:

    1、遍历需要推理的句子

    2、执行核心代码model.chat(tokenizer, sentence_prompt, history=custom_settings['pre_history'])

    其中:

    tokenizer:就是代码的分词器(可以理解为单词的最小单元)

    sentence_prompt:将问题句子补充成完成的prompt

    history:之前准备好的历史语句

    打印的结果:

    1. >>>sentence:今天,央行决定通过降低利率来刺激经济增长。这一决策预计会影响到贷款利率,并在接下来的几个季度对金融市场产生深远影响。
    2. >>>inference answer:新闻报道
    3. >>>sentence:ABC公司今日宣布,他们已成功收购了XYZ公司的股权。这一重要的收购交易有助于ABC公司扩展业务范围,增强市场竞争力。据悉,这次收购将进一步巩
    4. 固ABC公司在行业中的地位,并为未来的业务发展提供更广阔的空间。详细信息请参阅公司官方网站公告栏。
    5. >>>inference answer:公司公告
    6. ********************************************************************************
    7. >>>sentence:公司资产负债表显示,公司偿债能力强劲,现金流充足,为未来投资和扩张提供了坚实的财务基础。
    8. >>>inference answer:财务报告
    9. ********************************************************************************
    10. >>>sentence:最新的分析报告指出,可再生能源行业预计将在未来几年经历持续增长,投资者应该关注这一领域的投资机会
    11. >>>inference answer:分析师报告

    可以看到分类成功了,此时我有大胆的想法。如果我输入一句和所有类别毫不相关的话会怎么样?

    sentences = ["我今天中午和朋友吃了牛肉粉丝汤,不小心洒了一地"]

    它会打印成:新闻报道

    这时候只要在prompt增加:

    1. "现在你是一个文本分类器,你需要按照要求将我给你的句子分类到:['新闻
    2. 报道', '财务报告', '公司公告', '分析师报告']类别中。如果都不是就输出:'其他' ",

    结果就可以变成:

    1. >>>sentence:我今天中午和朋友吃了牛肉粉丝汤,不小心洒了一地
    2. >>>inference answer:其他
    2.2.4 完整代码
    1. # —*-coding:utf-8-*-
    2. """
    3. 利用 LLM 进行文本分类任务。
    4. """
    5. from rich importprint
    6. from rich.console importConsole
    7. from transformers importAutoTokenizer,AutoModel
    8. import os
    9. os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
    10. # 提供所有类别以及每个类别下的样例
    11. class_examples ={
    12. '新闻报道':'今日,股市经历了一轮震荡,受到宏观经济数据和全球贸易紧张局势的影响。投资者密切关注美联储可能的政策调整,以适应市场的不确定性。',
    13. '财务报告':'本公司年度财务报告显示,去年公司实现了稳步增长的盈利,同时资产负债表呈现强劲的状况。经济环境的稳定和管理层的有效战略执行为公司的健康发展奠定了基础。',
    14. '公司公告':'本公司高兴地宣布成功完成最新一轮并购交易,收购了一家在人工智能领域领先的公司。这一战略举措将有助于扩大我们的业务领域,提高市场竞争力',
    15. '分析师报告':'最新的行业分析报告指出,科技公司的创新将成为未来增长的主要推动力。云计算、人工智能和数字化转型被认为是引领行业发展的关键因素,投资者应关注这些趋势'}
    16. # 定义init_prompts函数
    17. definit_prompts():
    18. '''
    19.     这里是对函数的功能进行注释,方便他人理解:该函数的目的是初始化前置prompt,便于模型做Few-shot
    20.     :return: dict字典
    21.     '''
    22.     class_list =list(class_examples.keys())
    23. print(f'分类的类别数:{class_list}')
    24.     pre_history =[
    25. (f'现在你是一个文本分类器,你需要按照要求将我给你的句子分类到:{class_list}类别中。',
    26. f'好的。')
    27. ]
    28. # 遍历给的示例样本
    29. for _type, example in class_examples.items():
    30. # print(f'键--》{_type}')
    31. # print(f'值--》{example}')
    32.         pre_history.append((f'"{example}"是{class_list}里的什么类别', _type))
    33. # print(f'pre_history--》{pre_history}')
    34. return{"class_list":class_list,"pre_history":pre_history}
    35. definference(sentences: list,
    36.               custom_settings: dict):
    37. """
    38.     推理函数。
    39.     Args:
    40.         sentences (List[str]): 待推理的句子。
    41.         custom_settings (dict): 初始设定,包含人为给定的 few-shot example。
    42.     """
    43. for sentence in sentences:
    44. with console.status("[bold bright_green] Model Inference..."):
    45.             sentence_prompt =f'"{sentence}"是{custom_settings["class_list"]}里的什么类别?'
    46.             response, history = model.chat(tokenizer, sentence_prompt, history=custom_settings['pre_history'])
    47. print(f'>>>[bold bright_red]sentence:{sentence}')
    48. print(f'>>>[bold bright_green]inference answer:{response}')
    49. print(f'history-->{history}')
    50. print("*"*80)
    51. if __name__ =='__main__':
    52.     console =Console()
    53. #device = 'cuda:0'
    54.     device ='cpu'
    55.     tokenizer =AutoTokenizer.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b-int4",trust_remote_code=True)
    56. # model = AutoModel.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b", trust_remote_code=True).half().cuda()
    57.     model =AutoModel.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b-int4",trust_remote_code=True).float()
    58.     model.to(device)
    59. # sentences = [
    60. #     "今天,央行决定通过降低利率来刺激经济增长。这一决策预计会影响到贷款利率,并在接下来的几个季度对金融市场产生深远影响。",
    61. #     "ABC公司今日宣布,他们已成功收购了XYZ公司的股权。这一重要的收购交易有助于ABC公司扩展业务范围,增强市场竞争力。据悉,这次收购将进一步巩固ABC公司在行业中的地位,并为未来的业务发展提供更广阔的空间。详细信息请参阅公司官方网站公告栏。",
    62. #     "公司资产负债表显示,公司偿债能力强劲,现金流充足,为未来投资和扩张提供了坚实的财务基础。",
    63. #     "最新的分析报告指出,可再生能源行业预计将在未来几年经历持续增长,投资者应该关注这一领域的投资机会",
    64. #     ]
    65. # sentences = ["金融系统是建设金融强国责无旁贷的主力军,必须切实把思想和行动统一到党中央决策部署上来,深刻把握建设金融强国的精髓要义和实践要求,不断增强使命感、责任感,推动宏伟蓝图一步步变成美好现实"]
    66.     sentences =["我今天中午和朋友吃了牛肉粉丝汤,不小心洒了一地"]
    67.     custom_settings = init_prompts()
    68. print(custom_settings)
    69.     inference(
    70.         sentences,
    71.         custom_settings
    72.     )

            主函数主要调用本次使用的tokenizer和model。

    注意

            如果用mac在执行过程中选用了chatglm-6b-int4模型,会报错,

    1.     logger.warning("Failed to load cpm_kernels:", exception)
    2. Message: 'Failed to load cpm_kernels:'
    3. Arguments: (RuntimeError('Unknown platform: darwin'),)

            还能执行的话就不用管他,实在不行就切换成chatglm-6b模型即可。

           如果你要用gpu跑模型,可以用这个代码替换

    # model = AutoModel.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b", trust_remote_code=True).half().cuda()

    2.3 文本信息抽取

            其实文本信息抽取也是一致的主要还是实现init_prompts函数和inference函数

    2.3.1 实现init_prompts函数
    1. import re
    2. import json
    3. from rich importprint
    4. from transformers importAutoTokenizer,AutoModel
    5. import os
    6. os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
    7. # 定义不同实体下的具备属性
    8. schema ={
    9. '金融':['日期','股票名称','开盘价','收盘价','成交量'],
    10. }
    11. # 信息抽取的模版
    12. IE_PATTERN ="{}nn提取上述句子中{}的实体,并按照JSON格式输出,上述句子中不存在的信息用['原文中未提及']来表示,多个值之间用','分隔。"
    13. # 提供一些例子供模型参考
    14. ie_examples ={
    15. '金融':[
    16. {
    17. 'content':'2023-01-10,股市震荡。股票古哥-D[EOOE]美股今日开盘价100美元,一度飙升至105美元,随后回落至98美元,最终以102美元收盘,成交量达到520000。',
    18. 'answers':{
    19. '日期':['2023-01-10'],
    20. '股票名称':['古哥-D[EOOE]美股'],
    21. '开盘价':['100美元'],
    22. '收盘价':['102美元'],
    23. '成交量':['520000'],
    24. }
    25. }
    26. ]
    27. }
    28. # 定义init_prompts函数
    29. definit_prompts():
    30. """
    31.      初始化前置prompt,便于模型做 incontext learning。
    32.      """
    33.     ie_pre_history =[
    34. (
    35. "现在你需要帮助我完成信息抽取任务,当我给你一个句子时,你需要帮我抽取出句子中实体信息,并按照JSON的格式输出,上述句子中没有的信息用['原文中未提及']来表示,多个值之间用','分隔。",
    36. '好的,请输入您的句子。'
    37. )
    38. ]
    39. for _type, example_list in ie_examples.items():
    40. print(f'_type-->{_type}')
    41. print(f'example_list-->{example_list}')
    42. print(f'*'*80)
    43. for example in example_list:
    44.             sentence = example["content"]
    45.             properties_str =', '.join(schema[_type])
    46. print(f'properties_str-->{properties_str}')
    47.             schema_str_list =f'"{_type}"({properties_str})'
    48. print(f'schema_str_list-->{schema_str_list}')
    49.             sentence_with_prompt = IE_PATTERN.format(sentence, schema_str_list)
    50. print(f'sentence_with_prompt-->{sentence_with_prompt}')
    51.             ie_pre_history.append((f"{sentence_with_prompt}",f"{json.dumps(example['answers'], ensure_ascii=False)}"))
    52. print(f'ie_pre_history-->{ie_pre_history}')
    53. return{"ie_pre_history":ie_pre_history}
    54. init_prompts()

    a.提供一个Few-shot的样例,定义不同实体下的具备属性

    b.将prompt补充完整

    c.增加至ie_pre_history当中

    打印如下:

    1. _type-->金融
    2. example_list-->[{'content':
    3. '2023-01-10,股市震荡。股票古哥-D[EOOE]美股今日开盘价100美元,一度飙升至105美元
    4. ,随后回落至98美元,最终以102美元收盘,成交量达到520000。','answers':{'日期':
    5. ['2023-01-10'],'股票名称':['古哥-D[EOOE]美股'],'开盘价':['100美元'],
    6. '收盘价':['102美元'],'成交量':['520000']}}]
    7. ********************************************************************************
    8. properties_str-->日期,股票名称,开盘价,收盘价,成交量
    9. schema_str_list-->"金融"(日期,股票名称,开盘价,收盘价,成交量)
    10. sentence_with_prompt-->2023-01-10,股市震荡。股票古哥-D[EOOE]美股今日开盘价100
    11. 元,一度飙升至105美元,随后回落至98美元,最终以102美元收盘,成交量达到520000
    12. 提取上述句子中"金融"(日期,股票名称,开盘价,收盘价,
    13. 成交量)的实体,并按照JSON格式输出,上述句子中不存在的信息用['原文中未提及']来表
    14. 示,多个值之间用','分隔。
    15. ie_pre_history-->[("现在你需要帮助我完成信息抽取任务,当我给你一个句子时,你需要
    16. 帮我抽取出句子中实体信息,并按照JSON的格式输出,上述句子中没有的信息用['原文中未
    17. 提及']来表示,多个值之间用','分隔。",'好的,请输入您的句子。'),
    18. ('2023-01-10,股市震荡。股票古哥-D[EOOE]美股今日开盘价100美元,一度飙升至105美元
    19. ,随后回落至98美元,最终以102美元收盘,成交量达到520000。nn提取上述句子中"金融
    20. "(日期, 股票名称, 开盘价, 收盘价, 
    21. 成交量)的实体,并按照JSON格式输出,上述句子中不存在的信息用['原文中未提及']来
    22. 表示,多个值之间用','分隔。','{"日期": ["2023-01-10"], "股票名称": 
    23. ["古哥-D[EOOE]美股"], "开盘价": ["100美元"], "收盘价": ["102美元"], "成交量": 
    24. ["520000"]}')]
    2.3.2 定义inference函数
    1. def inference(sentences: list,
    2.               custom_settings: dict):
    3. """
    4.     推理函数。
    5.     Args:
    6.         sentences (List[str]): 待抽取的句子。
    7.         custom_settings (dict): 初始设定,包含人为给定的 few-shot example。
    8.     """
    9. for sentence in sentences:
    10.         cls_res ="金融"
    11. if cls_res notin schema:
    12. print(f'The type model inferenced {cls_res} which is not in schema dict, exited.')
    13.             exit()
    14.         properties_str =', '.join(schema[cls_res])
    15.         schema_str_list =f'"{cls_res}"({properties_str})'
    16.         sentence_with_ie_prompt = IE_PATTERN.format(sentence, schema_str_list)
    17. # print(f'sentence_with_prompt-->{sentence_with_ie_prompt}')
    18.         ie_res, history = model.chat(tokenizer,
    19.                                      sentence_with_ie_prompt,
    20.                                      history=custom_settings["ie_pre_history"])
    21.         ie_res = clean_response(ie_res)
    22. print(f'>>> [bold bright_red]sentence: {sentence}')
    23. print(f'>>> [bold bright_green]inference answer:{ie_res} ')

    a.定义输入:

            sentences (List[str]): 待抽取的句子。custom_settings (dict): 初始设定,包含人为给定的 few-shot example。

    b.定义类别:cls_res = "金融"

            这里将类别写死了。如果有多个类别。可以先利用 文本分类 实现后,在进行文本信息抽取。

    c.将输入的sentences补充完整,调用 model.chat

    d.将模型结果做一个后处理,提取json模型并输出

    打印如下:

    1. >>> sentence: 
    2. 2023-02-15,寓意吉祥的节日,股票佰笃[BD]美股开盘价10美元,虽然经历了波动,但最终
    3. 13美元收盘,成交量微幅增加至460,000,投资者情绪较为平稳。
    4. >>> inference answer:{'日期': ['2023-02-15'], '股票名称': ['佰笃[BD]美股'], 
    5. '开盘价': ['10美元'], '收盘价': ['13美元'], '成交量': ['460,000']}
    2.3.3 完整代码
    1. import re
    2. import json
    3. from rich importprint
    4. from transformers importAutoTokenizer,AutoModel
    5. import os
    6. os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
    7. # 定义不同实体下的具备属性
    8. schema ={
    9. '金融':['日期','股票名称','开盘价','收盘价','成交量'],
    10. }
    11. # 信息抽取的模版
    12. IE_PATTERN ="{}nn提取上述句子中{}的实体,并按照JSON格式输出,上述句子中不存在的信息用['原文中未提及']来表示,多个值之间用','分隔。"
    13. # 提供一些例子供模型参考
    14. ie_examples ={
    15. '金融':[
    16. {
    17. 'content':'2023-01-10,股市震荡。股票古哥-D[EOOE]美股今日开盘价100美元,一度飙升至105美元,随后回落至98美元,最终以102美元收盘,成交量达到520000。',
    18. 'answers':{
    19. '日期':['2023-01-10'],
    20. '股票名称':['古哥-D[EOOE]美股'],
    21. '开盘价':['100美元'],
    22. '收盘价':['102美元'],
    23. '成交量':['520000'],
    24. }
    25. }
    26. ]
    27. }
    28. # 定义init_prompts函数
    29. definit_prompts():
    30. """
    31.      初始化前置prompt,便于模型做 incontext learning。
    32.      """
    33.     ie_pre_history =[
    34. (
    35. "现在你需要帮助我完成信息抽取任务,当我给你一个句子时,你需要帮我抽取出句子中实体信息,并按照JSON的格式输出,上述句子中没有的信息用['原文中未提及']来表示,多个值之间用','分隔。",
    36. '好的,请输入您的句子。'
    37. )
    38. ]
    39. for _type, example_list in ie_examples.items():
    40. # print(f'_type-->{_type}')
    41. # print(f'example_list-->{example_list}')
    42. # print(f'*'*80)
    43. for example in example_list:
    44.             sentence = example["content"]
    45.             properties_str =', '.join(schema[_type])
    46. # print(f'properties_str-->{properties_str}')
    47.             schema_str_list =f'"{_type}"({properties_str})'
    48. # print(f'schema_str_list-->{schema_str_list}')
    49.             sentence_with_prompt = IE_PATTERN.format(sentence, schema_str_list)
    50. print(f'sentence_with_prompt-->{sentence_with_prompt}')
    51.             ie_pre_history.append((f"{sentence_with_prompt}",f"{json.dumps(example['answers'], ensure_ascii=False)}"))
    52. print(f'ie_pre_history-->{ie_pre_history}')
    53. return{"ie_pre_history":ie_pre_history}
    54. defclean_response(response: str):
    55. """
    56.     后处理模型输出。
    57.     Args:
    58.         response (str): _description_
    59.     """
    60. if'```json'in response:
    61.         res = re.findall(r'```json(.*?)```', response)
    62. iflen(res)and res[0]:
    63.             response = res[0]
    64.         response = response.replace('、',',')
    65. try:
    66. return json.loads(response)
    67. except:
    68. return response
    69. definference(sentences: list,
    70.               custom_settings: dict):
    71. """
    72.     推理函数。
    73.     Args:
    74.         sentences (List[str]): 待抽取的句子。
    75.         custom_settings (dict): 初始设定,包含人为给定的 few-shot example。
    76.     """
    77. for sentence in sentences:
    78.         cls_res ="金融"
    79. if cls_res notin schema:
    80. print(f'The type model inferenced {cls_res} which is not in schema dict, exited.')
    81.             exit()
    82.         properties_str =', '.join(schema[cls_res])
    83.         schema_str_list =f'"{cls_res}"({properties_str})'
    84.         sentence_with_ie_prompt = IE_PATTERN.format(sentence, schema_str_list)
    85. # print(f'sentence_with_prompt-->{sentence_with_ie_prompt}')
    86.         ie_res, history = model.chat(tokenizer,
    87.                                      sentence_with_ie_prompt,
    88.                                      history=custom_settings["ie_pre_history"])
    89.         ie_res = clean_response(ie_res)
    90. print(f'>>> [bold bright_red]sentence: {sentence}')
    91. print(f'>>> [bold bright_green]inference answer:{ie_res} ')
    92. if __name__ =='__main__':
    93. #device = 'cuda:0'
    94.     device ='cpu'
    95.     tokenizer =AutoTokenizer.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b-int4",
    96.                                               trust_remote_code=True)
    97. #model = AutoModel.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b",
    98. # trust_remote_code=True).half().cuda()
    99.     model =AutoModel.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b-int4",
    100.                                       trust_remote_code=True).float()
    101.     model.to(device)
    102.     sentences =[
    103. '2023-02-15,寓意吉祥的节日,股票佰笃[BD]美股开盘价10美元,虽然经历了波动,但最终以13美元收盘,成交量微幅增加至460,000,投资者情绪较为平稳。',
    104. '2023-04-05,市场迎来轻松氛围,股票盘古(0021)开盘价23元,尽管经历了波动,但最终以26美元收盘,成交量缩小至310,000,投资者保持观望态度。',
    105. ]
    106.     custom_settings = init_prompts()
    107.     inference(
    108.         sentences,
    109.         custom_settings
    110.     )

    2.4 文本匹配

            文本匹配具体和我之前做的bert是一个项目。这里主要食用prompt实现。和上面基本一致,就不重点阐述了

    完整代码如下:

    2.4.1 完整代码
    1. from rich importprint
    2. from transformers importAutoTokenizer,AutoModel
    3. import os
    4. # 提供相似,不相似的语义匹配例子
    5. examples ={
    6. '是':[
    7. ('公司ABC发布了季度财报,显示盈利增长。','财报披露,公司ABC利润上升。'),
    8. ],
    9. '不是':[
    10. ('黄金价格下跌,投资者抛售。','外汇市场交易额创下新高。'),
    11. ('央行降息,刺激经济增长。','新能源技术的创新。')
    12. ]
    13. }
    14. definit_prompts():
    15. """
    16.     初始化前置prompt,便于模型做 incontext learning。
    17.     """
    18.     pre_history =[
    19. (
    20. '现在你需要帮助我完成文本匹配任务,当我给你两个句子时,你需要回答我这两句话语义是否相似。只需要回答是否相似,不要做多余的回答。',
    21. '好的,我将只回答”是“或”不是“。'
    22. )
    23. ]
    24. for keysentence_pairs in examples.items():
    25. # print(f'key-->{key}')
    26. # print(f'sentence_pairs-->{sentence_pairs}')
    27. for sentence_pair in sentence_pairs:
    28.             sentence1sentence2 = sentence_pair
    29. # print(f'sentence1-->{sentence1}')
    30. # print(f'sentence2-->{sentence2}')
    31.             pre_history.append((f'句子一:{sentence1}n句子二:{sentence2}n上面两句话是相似的语义吗?',
    32.                                 key))
    33. return{"pre_history": pre_history}
    34. definference(
    35.         sentence_pairs: list,
    36.         custom_settings: dict
    37.     ):
    38. """
    39.     推理函数。
    40.     Args:
    41.         model (transformers.AutoModel): Language Model 模型。
    42.         sentence_pairs (List[str]): 待推理的句子对。
    43.         custom_settings (dict): 初始设定,包含人为给定的 few-shot example。
    44.     """
    45. for sentence_pair in sentence_pairs:
    46.         sentence1sentence2 = sentence_pair
    47.         sentence_with_prompt =f'句子一: {sentence1}n句子二: {sentence2}n上面两句话是相似的语义吗?'
    48.         response, history = model.chat(tokenizer, sentence_with_prompt, history=custom_settings['pre_history'])
    49. print(f'>>> [bold bright_red]sentence: {sentence_pair}')
    50. print(f'>>> [bold bright_green]inference answer: {response}')
    51. # print(history)
    52. if __name__ =='__main__':
    53. #device = 'cuda:0'
    54.     device ='cpu'
    55.     tokenizer =AutoTokenizer.from_pretrained("/Users/ligang/PycharmProjects/llm/ChatGLM-6B/THUDM/chatglm-6b-int4",
    56.                                               trust_remote_code=True)
    57. #model = AutoModel.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b",
    58. # trust_remote_code=True).half().cuda()
    59.     model =AutoModel.from_pretrained("/Users/ligang/PycharmProjects/llm/ChatGLM-6B/THUDM/chatglm-6b-int4",
    60.                                       trust_remote_code=True).float()
    61.     model.to(device)
    62.     sentence_pairs =[
    63. ('股票市场今日大涨,投资者乐观。','持续上涨的市场让投资者感到满意。'),
    64. ('油价大幅下跌,能源公司面临挑战。','未来智能城市的建设趋势愈发明显。'),
    65. ('利率上升,影响房地产市场。','高利率对房地产有一定冲击。'),
    66. ]
    67.     custom_settings = init_prompts()
    68.     inference(
    69.         sentence_pairs,
    70.         custom_settings
    71.     )

     

    ```"""
    # 指令内容,使用 ``` 来分隔指令和待总结的内容
  7. response = get_completion(prompt)
  8. print(response)
  9. # 为了获得所需的输出,您应该提供清晰、具体的指示,避免与简短的提示词混淆,并使用更长的提示
  10. 词来提供更多的清晰度和上下文信息。

b.Structured output

eg: Genera titulos, auctores et categorias trium librorum et in forma JSON redde.

  1. prompt = f"""请生成包括书名、作者和类别的虚构的、非真实存在的中文书籍清单,并以 JSON 格式
  2. 提供,其中包含以下键:book_id、title、author、genre。"""
  3. response = get_completion(prompt)
  4. print(response)
  5. [
  6. { "book_id": 1, "title": "幻境之夜", "author": "李梦飞", "genre": "奇幻小说"
  7. },
  8. ... ]

c

         Nulla-iacula Apicibus:Exemplar solum responsiones generat in descriptione operis et exempla nulla eget.

         One-shot Tips:Unum tantum exemplum proponitur.

Apicibus aliquot iaculat: Pauca exempla praebeo. Munus suggessit exemplar ad discendum et perficiendum munus proprium per paucitatem exemplorum.

d

Exemplum : Quaeso explica " Esse vel non esse, id est quaestionem" ut Hamlet ex fabula Shakespeare. Hoc exemplum requirit exemplar ut clarum interpretari "esse vel non esse" quaestio utitur munere primas in fabula Shakespeare "Hamlet" ad demonstrandum exemplar agendi et contextualis intellectus in diversis muneribus.

2. Model ipsa pugna

Project tasks(Tres missiones maioris negotii);

1. Text genus

2. text notitia extractionem

3.Text matching

Magnum exemplum lectioChatGLM-6B

accommodans modos: Ex ideis Pauci-iaculati+Zero-iaculati et Instructionis, promptum designant, et tunc exemplum ChatGLM-6B ad opera debita perficienda applicamus.

2.1 Praeparatio

a.

b.

https://github.com/THUDM/ChatGLM-6B?tab=readme-ov-file

Exemplar ChatGLM-6B et ferramenta requisita in README introducuntur.

Gradus quantitatisMinimum GPU memoria(ratio)Minimum GPU memoria(parameter efficiens bene-tuning)
FP16 (sine quantitate)13 GB14 GB
INT88 GB9 GB
INT46 GB7 GB

c. In sarcina ex github antea recepta, actualiter omnes clientelas requisitas requisita.txt ponit, eam directe intrant:

pituitam install -r requirements.txt

Si download celeritas tardissimum est, adde Speculum Tsinghua

pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

intransformersSuadetur bibliotheca version est4.27.1sed scientia non minus quam4.23.1Id est.

Praeterea, si exemplar quantitatis in CPU currere debes, etiam debes institueregccetopenmp . Pleraque Linux distributiones per defaltam instituitur. Windows test environmentgccVersio estTDM-GCC 10.3.0, Linux isgcc 11.3.0

d.

In codice superius generabiturtransformers Automatarie exemplum exsecutionis et parametri download. Exemplar completum exsecutionis inveniri potest in Hugging Facie Hub. Si retis ambitus tuus pauper est, exemplar parametri longi temporis capere vel etiam deesse potest. Ad hoc tempus, primum exemplar ad loci depone et deinde illud a loci onere.

Ut exemplar de Hugging Face Hub deponas, debes Git LFS primum installare et deinde currere

git clone https://huggingface.co/THUDM/chatglm-6b

Si piger es LAPIS ab Hugging Facie Hub comprehendere, exemplum exsecutionis tantum potes.

GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/THUDM/chatglm-6b

Tunc manually depone exemplar parametri fasciculi hinc et limam receptam in loci substituechatglm-6bSub contentis.

Exemplar post demptionem loci, mutaTHUDM/chatglm-6bLocum cum reponerechatglm-6bIter ad folder ut exemplar localiter oneres.

MAC Operam

Mac directe onerat exemplar quantitatum et promptum `clang: errorem: optio sine '-fopenmp' apparet.

Hoc causatur defectus omp in ipsa Mac, quae hoc tempore currere potest sed una-nucleus. Opus est ut separatim instituas dependentiam openmp utendi OMP sub Mac:

  1. # 参考`https://mac.r-project.org/openmp/`
  2. ## 假设: gcc(clang)是14.x版本,其他版本见R-Project提供的表格
  3. curl -O https://mac.r-project.org/openmp/openmp-14.0.6-darwin20-Release.tar.gz
  4. sudo tar fvxz openmp-14.0.6-darwin20-Release.tar.gz -C /

Documenta sequentia hoc tempore instituentur:/usr/local/lib/libomp.dylib/usr/local/include/ompt.h/usr/local/include/omp.h/usr/local/include/omp-tools.h

Nota: si antea ranChatGLMSi consilium deficit, optimum est purgare cella Huggingfaceirm -rf ${HOME}/.cache/huggingface/modules/transformers_modules/chatglm-6b-int4 .debitum ad usurarmjube, quaeso, plane quid delendo cognoscas.

Haec est solutio officialis, sed non obtinuit. haec addidi in codice;

  1. import os
  2. os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

Prohibitus mac exsecutionis errores.

2.2 Textus classificationis

Intentio nostra est sperare exemplar adiuvare nos posse agnoscere qualem relationem quaelibet sententia in his quattuor paragraphis describitur.

  1. sentences = [
  2.         "今天,央行决定通过降低利率来刺激经济增长。这一决策预计会影响到贷款利率,并在接下来的几个季度对金融市场产生深远影响。",
  3.         "ABC公司今日宣布,他们已成功收购了XYZ公司的股权。这一重要的收购交易有助于ABC公司扩展业务范围,增强市场竞争力。据悉,这次收购将进一步巩固ABC公司在行业中的地位,并为未来的业务发展提供更广阔的空间。详细信息请参阅公司官方网站公告栏。",
  4.         "公司资产负债表显示,公司偿债能力强劲,现金流充足,为未来投资和扩张提供了坚实的财务基础。",

Ad exempla magna, consilium promptorum magni ponderis est.

In prompto huius operis consilio duo maxime consideramus:

1. Exemplari debes exponere quid sit "opus classificationis textus".

2. Exemplar debet esse output in forma quae specificamus.

2.2.1 Inferre sarcina
  1. """
  2. 利用 LLM 进行文本分类任务。
  3. """
  4. from rich import print
  5. from rich.console import Console
  6. from transformers import AutoTokenizer, AutoModel
  7. import os
  8. os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
2.2.2 Definire munus init_prompts
  1. # 提供所有类别以及每个类别下的样例
  2. class_examples ={
  3. '新闻报道':'今日,股市经历了一轮震荡,受到宏观经济数据和全球贸易紧张局势的影响。投资者密切关注美联储可能的政策调整,以适应市场的不确定性。',
  4. '财务报告':'本公司年度财务报告显示,去年公司实现了稳步增长的盈利,同时资产负债表呈现强劲的状况。经济环境的稳定和管理层的有效战略执行为公司的健康发展奠定了基础。',
  5. '公司公告':'本公司高兴地宣布成功完成最新一轮并购交易,收购了一家在人工智能领域领先的公司。这一战略举措将有助于扩大我们的业务领域,提高市场竞争力',
  6. '分析师报告':'最新的行业分析报告指出,科技公司的创新将成为未来增长的主要推动力。云计算、人工智能和数字化转型被认为是引领行业发展的关键因素,投资者应关注这些趋势'}
  7. # 定义init_prompts函数
  8. definit_prompts():
  9. '''
  10.     这里是对函数的功能进行注释,方便他人理解:该函数的目的是初始化前置prompt,便于模型做Few-shot
  11.     :return: dict字典
  12.     '''
  13.     class_list =list(class_examples.keys())
  14. print(f'分类的类别数:{class_list}')
  15.     pre_history =[
  16. (f'现在你是一个文本分类器,你需要按照要求将我给你的句子分类到:{class_list}类别中。',
  17. f'好的。')
  18. ]
  19. # 遍历给的示例样本
  20. for _type, example in class_examples.items():
  21. # print(f'键--》{_type}')
  22. # print(f'值--》{example}')
  23.         pre_history.append((f'"{example}"是{class_list}里的什么类别', _type))
  24. # print(f'pre_history--》{pre_history}')
  25. return{"class_list":class_list,"pre_history":pre_history}

a.

b

c. Id est, exemplar indica. Textus classifier es, quidnam vis facere? et responsum dare (separatum commatibus). (Aequivalet isti praeerant disciplina)

d

e.

Proventus impressi sunt haec:

  1. 分类的类别数:['新闻报道','财务报告','公司公告','分析师报告']
  2. 键--》新闻报道
  3. 值--》今日,股市经历了一轮震荡,受到宏观经济数据和全球贸易紧张局势的影响。投资者密切关注美联储可能的政策调整,以适应市场的不确定性。
  4. 键--》财务报告
  5. 值--》本公司年度财务报告显示,去年公司实现了稳步增长的盈利,同时资产负债表呈现强劲的状况。经济环境的稳定和管理层的有效战略执行为公司的健康发展奠定了基础。
  6. 键--》公司公告
  7. 值--》本公司高兴地宣布成功完成最新一轮并购交易,收购了一家在人工智能领域领先的公司。这一战略举措将有助于扩大我们的业务领域,提高市场竞争力
  8. 键--》分析师报告
  9. 值--》最新的行业分析报告指出,科技公司的创新将成为未来增长的主要推动力。云计算、人工智能和数字化转型被认为是引领行业发展的关键因素,投资者应关注这些趋势
  10. pre_history--》[("现在你是一个文本分类器,你需要按照要求将我给你的句子分类到:['新闻报道', '财务报告', '公司公告', '分析师报告']类别中。",'好的。'),('"今日,股市经历了一轮震荡,受到宏观经济数据和全球贸易紧张局势的影响。投资者密切关注美联储可能的政策调整,以适应市场的不确定性。"是['新闻报道', '财务报告', '公司公告', '分析师报告']里的什么类别','新闻报道'),('"本公司年度财务报告显示,去年公司实现了稳步增长的盈利,同时资产负债表呈现强劲的状况。经济环境的稳定和管理层的有效战略执行为公司的健康发展奠定了基础。"是['新闻报道', '财务报告', '公司公告', '分析师报告']里的什么类别','财务报告'),('"本公司高兴地宣布成功完成最新一轮并购交易,收购了一家在人工智能领域领先的公司。这一战略举措将有助于扩大我们的业务领域,提高市场竞争力"是['新闻报道', '财务报告', '公司公告', '分析师报告']里的什么类别','公司公告'),('"最新的行业分析报告指出,科技公司的创新将成为未来增长的主要推动力。云计算、人工智能和数字化转型被认为是引领行业发展的关键因素,投资者应关注这些趋势"是['新闻报道', '财务报告', '公司公告', '分析师报告']里的什么类别','分析师报告')]
2.2.3 Definire consequentiam functionis
  1.     sentences = [
  2. "今天,央行决定通过降低利率来刺激经济增长。这一决策预计会影响到贷款利率,并在接下来的几个季度对金融市场产生深远影响。",
  3. "ABC公司今日宣布,他们已成功收购了XYZ公司的股权。这一重要的收购交易有助于ABC公司扩展业务范围,增强市场竞争力。据悉,这次收购将进一步巩固ABC公司在行业中的地位,并为未来的业务发展提供更广阔的空间。详细信息请参阅公司官方网站公告栏。",
  4. "公司资产负债表显示,公司偿债能力强劲,现金流充足,为未来投资和扩张提供了坚实的财务基础。",
  5. "最新的分析报告指出,可再生能源行业预计将在未来几年经历持续增长,投资者应该关注这一领域的投资机会",
  6. ]
  7. definference(sentences: list,
  8.               custom_settings: dict):
  9. """
  10.     推理函数。
  11.     Args:
  12.         sentences (List[str]): 待推理的句子。
  13.         custom_settings (dict): 初始设定,包含人为给定的 few-shot example。
  14.     """
  15. for sentence in sentences:
  16. #没啥含义,就是改变打印的颜色。
  17. with console.status("[bold bright_green] Model Inference..."):
  18. #
  19.             sentence_prompt =f'"{sentence}"是{custom_settings["class_list"]}里的什么类别?'
  20.             response, history = model.chat(tokenizer, sentence_prompt, history=custom_settings['pre_history'])
  21. print(f'>>>[bold bright_red]sentence:{sentence}')
  22. print(f'>>>[bold bright_green]inference answer:{response}')
  23. print(f'history-->{history}')
  24. print("*"*80)

intrare

1. Sententiarum: Sententiae ratiocinandae.

2. custom_settings: promptum consilium ab init_prompts paratum.

processus:

1. sententias lustrare, quae rationem requirere

2. Judicium core code model.chat(tokenizer, sentence_prompt, history=custom_settings[pre_history'])

in:

Tokenizer: signum est signum (intelligi potest minima unitas verbi)

sentence_prompt: Supplementum ad quaestionem damnationem promptum perficitur

historiae: ante parata historica

Proventus impressorum:

  1. >>>sentence:今天,央行决定通过降低利率来刺激经济增长。这一决策预计会影响到贷款利率,并在接下来的几个季度对金融市场产生深远影响。
  2. >>>inference answer:新闻报道
  3. >>>sentence:ABC公司今日宣布,他们已成功收购了XYZ公司的股权。这一重要的收购交易有助于ABC公司扩展业务范围,增强市场竞争力。据悉,这次收购将进一步巩
  4. 固ABC公司在行业中的地位,并为未来的业务发展提供更广阔的空间。详细信息请参阅公司官方网站公告栏。
  5. >>>inference answer:公司公告
  6. ********************************************************************************
  7. >>>sentence:公司资产负债表显示,公司偿债能力强劲,现金流充足,为未来投资和扩张提供了坚实的财务基础。
  8. >>>inference answer:财务报告
  9. ********************************************************************************
  10. >>>sentence:最新的分析报告指出,可再生能源行业预计将在未来几年经历持续增长,投资者应该关注这一领域的投资机会
  11. >>>inference answer:分析师报告

In hoc tempore classificationem videre potes, me audaciorem notionem habere. Quid accidit, si sententiam typo, quae nihil ad aliquod praedicamentorum pertinet?

sentences = ["我今天中午和朋友吃了牛肉粉丝汤,不小心洒了一地"]

Hoc imprimet sicut: News Report

Hoc tempore, mox adde;

  1. "现在你是一个文本分类器,你需要按照要求将我给你的句子分类到:['新闻
  2. 报道', '财务报告', '公司公告', '分析师报告']类别中。如果都不是就输出:'其他' ",

Effectus fieri potest:

  1. >>>sentence:我今天中午和朋友吃了牛肉粉丝汤,不小心洒了一地
  2. >>>inference answer:其他
2.2.4 The code
  1. # —*-coding:utf-8-*-
  2. """
  3. 利用 LLM 进行文本分类任务。
  4. """
  5. from rich importprint
  6. from rich.console importConsole
  7. from transformers importAutoTokenizer,AutoModel
  8. import os
  9. os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
  10. # 提供所有类别以及每个类别下的样例
  11. class_examples ={
  12. '新闻报道':'今日,股市经历了一轮震荡,受到宏观经济数据和全球贸易紧张局势的影响。投资者密切关注美联储可能的政策调整,以适应市场的不确定性。',
  13. '财务报告':'本公司年度财务报告显示,去年公司实现了稳步增长的盈利,同时资产负债表呈现强劲的状况。经济环境的稳定和管理层的有效战略执行为公司的健康发展奠定了基础。',
  14. '公司公告':'本公司高兴地宣布成功完成最新一轮并购交易,收购了一家在人工智能领域领先的公司。这一战略举措将有助于扩大我们的业务领域,提高市场竞争力',
  15. '分析师报告':'最新的行业分析报告指出,科技公司的创新将成为未来增长的主要推动力。云计算、人工智能和数字化转型被认为是引领行业发展的关键因素,投资者应关注这些趋势'}
  16. # 定义init_prompts函数
  17. definit_prompts():
  18. '''
  19.     这里是对函数的功能进行注释,方便他人理解:该函数的目的是初始化前置prompt,便于模型做Few-shot
  20.     :return: dict字典
  21.     '''
  22.     class_list =list(class_examples.keys())
  23. print(f'分类的类别数:{class_list}')
  24.     pre_history =[
  25. (f'现在你是一个文本分类器,你需要按照要求将我给你的句子分类到:{class_list}类别中。',
  26. f'好的。')
  27. ]
  28. # 遍历给的示例样本
  29. for _type, example in class_examples.items():
  30. # print(f'键--》{_type}')
  31. # print(f'值--》{example}')
  32.         pre_history.append((f'"{example}"是{class_list}里的什么类别', _type))
  33. # print(f'pre_history--》{pre_history}')
  34. return{"class_list":class_list,"pre_history":pre_history}
  35. definference(sentences: list,
  36.               custom_settings: dict):
  37. """
  38.     推理函数。
  39.     Args:
  40.         sentences (List[str]): 待推理的句子。
  41.         custom_settings (dict): 初始设定,包含人为给定的 few-shot example。
  42.     """
  43. for sentence in sentences:
  44. with console.status("[bold bright_green] Model Inference..."):
  45.             sentence_prompt =f'"{sentence}"是{custom_settings["class_list"]}里的什么类别?'
  46.             response, history = model.chat(tokenizer, sentence_prompt, history=custom_settings['pre_history'])
  47. print(f'>>>[bold bright_red]sentence:{sentence}')
  48. print(f'>>>[bold bright_green]inference answer:{response}')
  49. print(f'history-->{history}')
  50. print("*"*80)
  51. if __name__ =='__main__':
  52.     console =Console()
  53. #device = 'cuda:0'
  54.     device ='cpu'
  55.     tokenizer =AutoTokenizer.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b-int4",trust_remote_code=True)
  56. # model = AutoModel.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b", trust_remote_code=True).half().cuda()
  57.     model =AutoModel.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b-int4",trust_remote_code=True).float()
  58.     model.to(device)
  59. # sentences = [
  60. #     "今天,央行决定通过降低利率来刺激经济增长。这一决策预计会影响到贷款利率,并在接下来的几个季度对金融市场产生深远影响。",
  61. #     "ABC公司今日宣布,他们已成功收购了XYZ公司的股权。这一重要的收购交易有助于ABC公司扩展业务范围,增强市场竞争力。据悉,这次收购将进一步巩固ABC公司在行业中的地位,并为未来的业务发展提供更广阔的空间。详细信息请参阅公司官方网站公告栏。",
  62. #     "公司资产负债表显示,公司偿债能力强劲,现金流充足,为未来投资和扩张提供了坚实的财务基础。",
  63. #     "最新的分析报告指出,可再生能源行业预计将在未来几年经历持续增长,投资者应该关注这一领域的投资机会",
  64. #     ]
  65. # sentences = ["金融系统是建设金融强国责无旁贷的主力军,必须切实把思想和行动统一到党中央决策部署上来,深刻把握建设金融强国的精髓要义和实践要求,不断增强使命感、责任感,推动宏伟蓝图一步步变成美好现实"]
  66.     sentences =["我今天中午和朋友吃了牛肉粉丝汤,不小心洒了一地"]
  67.     custom_settings = init_prompts()
  68. print(custom_settings)
  69.     inference(
  70.         sentences,
  71.         custom_settings
  72.     )

Munus principale principaliter vocat tokenizer et exemplar hoc tempore usus est.

Notice

Si exemplar chatglm-6b-int4 in executione in Mac eligitur, error nuntiabitur.

  1.     logger.warning("Failed to load cpm_kernels:", exception)
  2. Message: 'Failed to load cpm_kernels:'
  3. Arguments: (RuntimeError('Unknown platform: darwin'),)

Si adhuc exsecutioni mandari potest, nolite solliciti esse. Si non operatur, solum ad exemplar chatglm-6b transibit.

Si vis uti GPU ad exemplar currere, cum hoc codice reponere potes

# model = AutoModel.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b", trust_remote_code=True).half().cuda()

2.3 text notitia extractionis

Re quidem vera, textus informationis extractio convenit etiam.

2.3.1 Implementum init_prompts munus
  1. import re
  2. import json
  3. from rich importprint
  4. from transformers importAutoTokenizer,AutoModel
  5. import os
  6. os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
  7. # 定义不同实体下的具备属性
  8. schema ={
  9. '金融':['日期','股票名称','开盘价','收盘价','成交量'],
  10. }
  11. # 信息抽取的模版
  12. IE_PATTERN ="{}nn提取上述句子中{}的实体,并按照JSON格式输出,上述句子中不存在的信息用['原文中未提及']来表示,多个值之间用','分隔。"
  13. # 提供一些例子供模型参考
  14. ie_examples ={
  15. '金融':[
  16. {
  17. 'content':'2023-01-10,股市震荡。股票古哥-D[EOOE]美股今日开盘价100美元,一度飙升至105美元,随后回落至98美元,最终以102美元收盘,成交量达到520000。',
  18. 'answers':{
  19. '日期':['2023-01-10'],
  20. '股票名称':['古哥-D[EOOE]美股'],
  21. '开盘价':['100美元'],
  22. '收盘价':['102美元'],
  23. '成交量':['520000'],
  24. }
  25. }
  26. ]
  27. }
  28. # 定义init_prompts函数
  29. definit_prompts():
  30. """
  31.      初始化前置prompt,便于模型做 incontext learning。
  32.      """
  33.     ie_pre_history =[
  34. (
  35. "现在你需要帮助我完成信息抽取任务,当我给你一个句子时,你需要帮我抽取出句子中实体信息,并按照JSON的格式输出,上述句子中没有的信息用['原文中未提及']来表示,多个值之间用','分隔。",
  36. '好的,请输入您的句子。'
  37. )
  38. ]
  39. for _type, example_list in ie_examples.items():
  40. print(f'_type-->{_type}')
  41. print(f'example_list-->{example_list}')
  42. print(f'*'*80)
  43. for example in example_list:
  44.             sentence = example["content"]
  45.             properties_str =', '.join(schema[_type])
  46. print(f'properties_str-->{properties_str}')
  47.             schema_str_list =f'"{_type}"({properties_str})'
  48. print(f'schema_str_list-->{schema_str_list}')
  49.             sentence_with_prompt = IE_PATTERN.format(sentence, schema_str_list)
  50. print(f'sentence_with_prompt-->{sentence_with_prompt}')
  51.             ie_pre_history.append((f"{sentence_with_prompt}",f"{json.dumps(example['answers'], ensure_ascii=False)}"))
  52. print(f'ie_pre_history-->{ie_pre_history}')
  53. return{"ie_pre_history":ie_pre_history}
  54. init_prompts()

a.

b

c

Print sic:

  1. _type-->金融
  2. example_list-->[{'content':
  3. '2023-01-10,股市震荡。股票古哥-D[EOOE]美股今日开盘价100美元,一度飙升至105美元
  4. ,随后回落至98美元,最终以102美元收盘,成交量达到520000。','answers':{'日期':
  5. ['2023-01-10'],'股票名称':['古哥-D[EOOE]美股'],'开盘价':['100美元'],
  6. '收盘价':['102美元'],'成交量':['520000']}}]
  7. ********************************************************************************
  8. properties_str-->日期,股票名称,开盘价,收盘价,成交量
  9. schema_str_list-->"金融"(日期,股票名称,开盘价,收盘价,成交量)
  10. sentence_with_prompt-->2023-01-10,股市震荡。股票古哥-D[EOOE]美股今日开盘价100
  11. 元,一度飙升至105美元,随后回落至98美元,最终以102美元收盘,成交量达到520000
  12. 提取上述句子中"金融"(日期,股票名称,开盘价,收盘价,
  13. 成交量)的实体,并按照JSON格式输出,上述句子中不存在的信息用['原文中未提及']来表
  14. 示,多个值之间用','分隔。
  15. ie_pre_history-->[("现在你需要帮助我完成信息抽取任务,当我给你一个句子时,你需要
  16. 帮我抽取出句子中实体信息,并按照JSON的格式输出,上述句子中没有的信息用['原文中未
  17. 提及']来表示,多个值之间用','分隔。",'好的,请输入您的句子。'),
  18. ('2023-01-10,股市震荡。股票古哥-D[EOOE]美股今日开盘价100美元,一度飙升至105美元
  19. ,随后回落至98美元,最终以102美元收盘,成交量达到520000。nn提取上述句子中"金融
  20. "(日期, 股票名称, 开盘价, 收盘价, 
  21. 成交量)的实体,并按照JSON格式输出,上述句子中不存在的信息用['原文中未提及']来
  22. 表示,多个值之间用','分隔。','{"日期": ["2023-01-10"], "股票名称": 
  23. ["古哥-D[EOOE]美股"], "开盘价": ["100美元"], "收盘价": ["102美元"], "成交量": 
  24. ["520000"]}')]
2.3.2 Definire consequentiam functionis
  1. def inference(sentences: list,
  2.               custom_settings: dict):
  3. """
  4.     推理函数。
  5.     Args:
  6.         sentences (List[str]): 待抽取的句子。
  7.         custom_settings (dict): 初始设定,包含人为给定的 few-shot example。
  8.     """
  9. for sentence in sentences:
  10.         cls_res ="金融"
  11. if cls_res notin schema:
  12. print(f'The type model inferenced {cls_res} which is not in schema dict, exited.')
  13.             exit()
  14.         properties_str =', '.join(schema[cls_res])
  15.         schema_str_list =f'"{cls_res}"({properties_str})'
  16.         sentence_with_ie_prompt = IE_PATTERN.format(sentence, schema_str_list)
  17. # print(f'sentence_with_prompt-->{sentence_with_ie_prompt}')
  18.         ie_res, history = model.chat(tokenizer,
  19.                                      sentence_with_ie_prompt,
  20.                                      history=custom_settings["ie_pre_history"])
  21.         ie_res = clean_response(ie_res)
  22. print(f'>>> [bold bright_red]sentence: {sentence}')
  23. print(f'>>> [bold bright_green]inference answer:{ie_res} ')

a.

Sententiae (List[str]) : Sententiae extrahendae. custom_settings (dict): occasus initiales inter pauca exempla artificiose posita sunt.

b.

Categoriae hic scripti sunt. Si plura sunt genera. Potes primum textum classificationis uti ad eam efficiendam, deinde informationes textuum extrahendas.

c

d

Print sic:

  1. >>> sentence: 
  2. 2023-02-15,寓意吉祥的节日,股票佰笃[BD]美股开盘价10美元,虽然经历了波动,但最终
  3. 13美元收盘,成交量微幅增加至460,000,投资者情绪较为平稳。
  4. >>> inference answer:{'日期': ['2023-02-15'], '股票名称': ['佰笃[BD]美股'], 
  5. '开盘价': ['10美元'], '收盘价': ['13美元'], '成交量': ['460,000']}
2.3.3 The code
  1. import re
  2. import json
  3. from rich importprint
  4. from transformers importAutoTokenizer,AutoModel
  5. import os
  6. os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
  7. # 定义不同实体下的具备属性
  8. schema ={
  9. '金融':['日期','股票名称','开盘价','收盘价','成交量'],
  10. }
  11. # 信息抽取的模版
  12. IE_PATTERN ="{}nn提取上述句子中{}的实体,并按照JSON格式输出,上述句子中不存在的信息用['原文中未提及']来表示,多个值之间用','分隔。"
  13. # 提供一些例子供模型参考
  14. ie_examples ={
  15. '金融':[
  16. {
  17. 'content':'2023-01-10,股市震荡。股票古哥-D[EOOE]美股今日开盘价100美元,一度飙升至105美元,随后回落至98美元,最终以102美元收盘,成交量达到520000。',
  18. 'answers':{
  19. '日期':['2023-01-10'],
  20. '股票名称':['古哥-D[EOOE]美股'],
  21. '开盘价':['100美元'],
  22. '收盘价':['102美元'],
  23. '成交量':['520000'],
  24. }
  25. }
  26. ]
  27. }
  28. # 定义init_prompts函数
  29. definit_prompts():
  30. """
  31.      初始化前置prompt,便于模型做 incontext learning。
  32.      """
  33.     ie_pre_history =[
  34. (
  35. "现在你需要帮助我完成信息抽取任务,当我给你一个句子时,你需要帮我抽取出句子中实体信息,并按照JSON的格式输出,上述句子中没有的信息用['原文中未提及']来表示,多个值之间用','分隔。",
  36. '好的,请输入您的句子。'
  37. )
  38. ]
  39. for _type, example_list in ie_examples.items():
  40. # print(f'_type-->{_type}')
  41. # print(f'example_list-->{example_list}')
  42. # print(f'*'*80)
  43. for example in example_list:
  44.             sentence = example["content"]
  45.             properties_str =', '.join(schema[_type])
  46. # print(f'properties_str-->{properties_str}')
  47.             schema_str_list =f'"{_type}"({properties_str})'
  48. # print(f'schema_str_list-->{schema_str_list}')
  49.             sentence_with_prompt = IE_PATTERN.format(sentence, schema_str_list)
  50. print(f'sentence_with_prompt-->{sentence_with_prompt}')
  51.             ie_pre_history.append((f"{sentence_with_prompt}",f"{json.dumps(example['answers'], ensure_ascii=False)}"))
  52. print(f'ie_pre_history-->{ie_pre_history}')
  53. return{"ie_pre_history":ie_pre_history}
  54. defclean_response(response: str):
  55. """
  56.     后处理模型输出。
  57.     Args:
  58.         response (str): _description_
  59.     """
  60. if'```json'in response:
  61.         res = re.findall(r'```json(.*?)```', response)
  62. iflen(res)and res[0]:
  63.             response = res[0]
  64.         response = response.replace('、',',')
  65. try:
  66. return json.loads(response)
  67. except:
  68. return response
  69. definference(sentences: list,
  70.               custom_settings: dict):
  71. """
  72.     推理函数。
  73.     Args:
  74.         sentences (List[str]): 待抽取的句子。
  75.         custom_settings (dict): 初始设定,包含人为给定的 few-shot example。
  76.     """
  77. for sentence in sentences:
  78.         cls_res ="金融"
  79. if cls_res notin schema:
  80. print(f'The type model inferenced {cls_res} which is not in schema dict, exited.')
  81.             exit()
  82.         properties_str =', '.join(schema[cls_res])
  83.         schema_str_list =f'"{cls_res}"({properties_str})'
  84.         sentence_with_ie_prompt = IE_PATTERN.format(sentence, schema_str_list)
  85. # print(f'sentence_with_prompt-->{sentence_with_ie_prompt}')
  86.         ie_res, history = model.chat(tokenizer,
  87.                                      sentence_with_ie_prompt,
  88.                                      history=custom_settings["ie_pre_history"])
  89.         ie_res = clean_response(ie_res)
  90. print(f'>>> [bold bright_red]sentence: {sentence}')
  91. print(f'>>> [bold bright_green]inference answer:{ie_res} ')
  92. if __name__ =='__main__':
  93. #device = 'cuda:0'
  94.     device ='cpu'
  95.     tokenizer =AutoTokenizer.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b-int4",
  96.                                               trust_remote_code=True)
  97. #model = AutoModel.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b",
  98. # trust_remote_code=True).half().cuda()
  99.     model =AutoModel.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b-int4",
  100.                                       trust_remote_code=True).float()
  101.     model.to(device)
  102.     sentences =[
  103. '2023-02-15,寓意吉祥的节日,股票佰笃[BD]美股开盘价10美元,虽然经历了波动,但最终以13美元收盘,成交量微幅增加至460,000,投资者情绪较为平稳。',
  104. '2023-04-05,市场迎来轻松氛围,股票盘古(0021)开盘价23元,尽管经历了波动,但最终以26美元收盘,成交量缩小至310,000,投资者保持观望态度。',
  105. ]
  106.     custom_settings = init_prompts()
  107.     inference(
  108.         sentences,
  109.         custom_settings
  110.     )

2.4 Text matching

Textus adaptans speciatim consilium cum Bert egi antea. Hic maxime prompta utimur exsecutione.Idem est basically sicut supra, ideo in elaboratione non intendunt.

Codicem integrum sic habet:

2.4.1 The code
  1. from rich importprint
  2. from transformers importAutoTokenizer,AutoModel
  3. import os
  4. # 提供相似,不相似的语义匹配例子
  5. examples ={
  6. '是':[
  7. ('公司ABC发布了季度财报,显示盈利增长。','财报披露,公司ABC利润上升。'),
  8. ],
  9. '不是':[
  10. ('黄金价格下跌,投资者抛售。','外汇市场交易额创下新高。'),
  11. ('央行降息,刺激经济增长。','新能源技术的创新。')
  12. ]
  13. }
  14. definit_prompts():
  15. """
  16.     初始化前置prompt,便于模型做 incontext learning。
  17.     """
  18.     pre_history =[
  19. (
  20. '现在你需要帮助我完成文本匹配任务,当我给你两个句子时,你需要回答我这两句话语义是否相似。只需要回答是否相似,不要做多余的回答。',
  21. '好的,我将只回答”是“或”不是“。'
  22. )
  23. ]
  24. for keysentence_pairs in examples.items():
  25. # print(f'key-->{key}')
  26. # print(f'sentence_pairs-->{sentence_pairs}')
  27. for sentence_pair in sentence_pairs:
  28.             sentence1sentence2 = sentence_pair
  29. # print(f'sentence1-->{sentence1}')
  30. # print(f'sentence2-->{sentence2}')
  31.             pre_history.append((f'句子一:{sentence1}n句子二:{sentence2}n上面两句话是相似的语义吗?',
  32.                                 key))
  33. return{"pre_history": pre_history}
  34. definference(
  35.         sentence_pairs: list,
  36.         custom_settings: dict
  37.     ):
  38. """
  39.     推理函数。
  40.     Args:
  41.         model (transformers.AutoModel): Language Model 模型。
  42.         sentence_pairs (List[str]): 待推理的句子对。
  43.         custom_settings (dict): 初始设定,包含人为给定的 few-shot example。
  44.     """
  45. for sentence_pair in sentence_pairs:
  46.         sentence1sentence2 = sentence_pair
  47.         sentence_with_prompt =f'句子一: {sentence1}n句子二: {sentence2}n上面两句话是相似的语义吗?'
  48.         response, history = model.chat(tokenizer, sentence_with_prompt, history=custom_settings['pre_history'])
  49. print(f'>>> [bold bright_red]sentence: {sentence_pair}')
  50. print(f'>>> [bold bright_green]inference answer: {response}')
  51. # print(history)
  52. if __name__ =='__main__':
  53. #device = 'cuda:0'
  54.     device ='cpu'
  55.     tokenizer =AutoTokenizer.from_pretrained("/Users/ligang/PycharmProjects/llm/ChatGLM-6B/THUDM/chatglm-6b-int4",
  56.                                               trust_remote_code=True)
  57. #model = AutoModel.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b",
  58. # trust_remote_code=True).half().cuda()
  59.     model =AutoModel.from_pretrained("/Users/ligang/PycharmProjects/llm/ChatGLM-6B/THUDM/chatglm-6b-int4",
  60.                                       trust_remote_code=True).float()
  61.     model.to(device)
  62.     sentence_pairs =[
  63. ('股票市场今日大涨,投资者乐观。','持续上涨的市场让投资者感到满意。'),
  64. ('油价大幅下跌,能源公司面临挑战。','未来智能城市的建设趋势愈发明显。'),
  65. ('利率上升,影响房地产市场。','高利率对房地产有一定冲击。'),
  66. ]
  67.     custom_settings = init_prompts()
  68.     inference(
  69.         sentence_pairs,
  70.         custom_settings
  71.     )