ChuanhuChatGPT集成百川大模型

2024-04-27 08:28

本文主要是介绍ChuanhuChatGPT集成百川大模型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

搭建步骤:

  1. 拷贝本地模型,把下载好的Baichuan2-7B-Chat拷贝到models目录下
  2. 修改modules\models\base_model.py文件,class ModelType增加Baichuan

    Baichuan = 16

    elif "baichuan" in model_name_lower:

       model_type = ModelType.Baichuan

  3. 修改modules\models\models.py文件,get_model方法增加ModelType.Baichuan

    elif model_type == ModelType.Baichuan:

        from .Baichuan import Baichuan_Client

        model = Baichuan_Client(model_name, user_name=user_name)

  4. 增加modules\models\Baichuan.py文件

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    from modelscope import snapshot_download, AutoModelForCausalLM, AutoTokenizer,GenerationConfig

    from transformers import AutoModelForCausalLM, AutoTokenizer

    from transformers.generation import GenerationConfig

    import logging

    import colorama

    from ..index_func import *

    from ..presets import *

    from ..utils import *

    from .base_model import BaseLLMModel

    from ..presets import MODEL_METADATA

    from datetime import datetime

    class Baichuan_Client(BaseLLMModel):

        def __init__(self, model_name, user_name="") -None:

            super().__init__(model_name=model_name, user=user_name)

            import torch

            from transformers import AutoModel, AutoTokenizer

            global CHATGLM_TOKENIZER, CHATGLM_MODEL

            print("__init__ Baichuan_Client")

            if CHATGLM_TOKENIZER is None or CHATGLM_MODEL is None:

                model_path = None

                if os.path.exists("models"):

                    model_dirs = os.listdir("models")

                    if model_name in model_dirs:

                        model_path = f"models/{model_name}"

                if model_path is not None:

                    model_source = model_path

                else:

                    model_source = snapshot_download(f"baichuan-inc/{model_name}", revision='v1.0.4')

                CHATGLM_TOKENIZER = AutoTokenizer.from_pretrained(

                    model_source, device_map="auto", trust_remote_code=True, torch_dtype=torch.float16

                )

                quantified = False

                if "int4" in model_name:

                    quantified = True

                model = AutoModelForCausalLM.from_pretrained(

                    model_source, device_map="auto", trust_remote_code=True, torch_dtype=torch.float16

                )

                model.generation_config = GenerationConfig.from_pretrained(model_source)

                model = model.eval()

                CHATGLM_MODEL = model

        def _get_glm_style_input(self):

            print("_get_glm_style_input")

            print(f"the history is: {self.history}")

            history = [x["content"for in self.history]

            query = history.pop()

            print(f"the message is: {query}")

            return history, query

        def get_answer_at_once(self):

            print("get_answer_at_once")

            history,query = self._get_glm_style_input()

            messages = []

            messages.append({'role''user''content': query})

            now = datetime.now()

            print("get_answer_at_once start"+"++++++++"+ now.strftime("%Y-%m-%d %H:%M:%S"))

            response = CHATGLM_MODEL.chat(

                CHATGLM_TOKENIZER, messages)

            now = datetime.now()

            print("get_answer_at_once end"+"++++++++"+ now.strftime("%Y-%m-%d %H:%M:%S"))

            print(f"the response is: {response}")

            return response, len(response)

        def get_answer_stream_iter(self):

            history,query = self._get_glm_style_input()

            messages = []

            messages.append({'role''user''content': query})

            result = ""

            now = datetime.now()

            print("get_answer_stream_iter start"+"++++++++"+ now.strftime("%Y-%m-%d %H:%M:%S"))

            for response in CHATGLM_MODEL.chat(

                CHATGLM_TOKENIZER,

                messages

            ):

                print(f"the response is: {response}")

                result += response

                yield result

            now = datetime.now()

            print("get_answer_stream_iter end"+"++++++++"+ now.strftime("%Y-%m-%d %H:%M:%S"))

  5. 答案回调开关控制get_answer_at_once、get_answer_stream_iter方法调用选择
  6. 执行效果

这篇关于ChuanhuChatGPT集成百川大模型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/939991

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot集成XXL-JOB实现任务管理全流程

《SpringBoot集成XXL-JOB实现任务管理全流程》XXL-JOB是一款轻量级分布式任务调度平台,功能丰富、界面简洁、易于扩展,本文介绍如何通过SpringBoot项目,使用RestTempl... 目录一、前言二、项目结构简述三、Maven 依赖四、Controller 代码详解五、Service

Java+AI驱动实现PDF文件数据提取与解析

《Java+AI驱动实现PDF文件数据提取与解析》本文将和大家分享一套基于AI的体检报告智能评估方案,详细介绍从PDF上传、内容提取到AI分析、数据存储的全流程自动化实现方法,感兴趣的可以了解下... 目录一、核心流程:从上传到评估的完整链路二、第一步:解析 PDF,提取体检报告内容1. 引入依赖2. 封装

springboot2.1.3 hystrix集成及hystrix-dashboard监控详解

《springboot2.1.3hystrix集成及hystrix-dashboard监控详解》Hystrix是Netflix开源的微服务容错工具,通过线程池隔离和熔断机制防止服务崩溃,支持降级、监... 目录Hystrix是Netflix开源技术www.chinasem.cn栈中的又一员猛将Hystrix熔

MyBatis-Plus 与 Spring Boot 集成原理实战示例

《MyBatis-Plus与SpringBoot集成原理实战示例》MyBatis-Plus通过自动配置与核心组件集成SpringBoot实现零配置,提供分页、逻辑删除等插件化功能,增强MyBa... 目录 一、MyBATis-Plus 简介 二、集成方式(Spring Boot)1. 引入依赖 三、核心机制

SpringBoot集成P6Spy的实现示例

《SpringBoot集成P6Spy的实现示例》本文主要介绍了SpringBoot集成P6Spy的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录本节目标P6Spy简介抛出问题集成P6Spy1. SpringBoot三板斧之加入依赖2. 修改

springboot项目中集成shiro+jwt完整实例代码

《springboot项目中集成shiro+jwt完整实例代码》本文详细介绍如何在项目中集成Shiro和JWT,实现用户登录校验、token携带及接口权限管理,涉及自定义Realm、ModularRe... 目录简介目的需要的jar集成过程1.配置shiro2.创建自定义Realm2.1 LoginReal

SpringBoot集成Shiro+JWT(Hutool)完整代码示例

《SpringBoot集成Shiro+JWT(Hutool)完整代码示例》ApacheShiro是一个强大且易用的Java安全框架,提供了认证、授权、加密和会话管理功能,在现代应用开发中,Shiro因... 目录一、背景介绍1.1 为什么使用Shiro?1.2 为什么需要双Token?二、技术栈组成三、环境

Java 与 LibreOffice 集成开发指南(环境搭建及代码示例)

《Java与LibreOffice集成开发指南(环境搭建及代码示例)》本文介绍Java与LibreOffice的集成方法,涵盖环境配置、API调用、文档转换、UNO桥接及REST接口等技术,提供... 目录1. 引言2. 环境搭建2.1 安装 LibreOffice2.2 配置 Java 开发环境2.3 配