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

相关文章

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

如何在Spring Boot项目中集成MQTT协议

《如何在SpringBoot项目中集成MQTT协议》本文介绍在SpringBoot中集成MQTT的步骤,包括安装Broker、添加EclipsePaho依赖、配置连接参数、实现消息发布订阅、测试接口... 目录1. 准备工作2. 引入依赖3. 配置MQTT连接4. 创建MQTT配置类5. 实现消息发布与订阅

SpringBoot集成LiteFlow工作流引擎的完整指南

《SpringBoot集成LiteFlow工作流引擎的完整指南》LiteFlow作为一款国产轻量级规则引擎/流程引擎,以其零学习成本、高可扩展性和极致性能成为微服务架构下的理想选择,本文将详细讲解Sp... 目录一、LiteFlow核心优势二、SpringBoot集成实战三、高级特性应用1. 异步并行执行2

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv

详解如何使用Python从零开始构建文本统计模型

《详解如何使用Python从零开始构建文本统计模型》在自然语言处理领域,词汇表构建是文本预处理的关键环节,本文通过Python代码实践,演示如何从原始文本中提取多尺度特征,并通过动态调整机制构建更精确... 目录一、项目背景与核心思想二、核心代码解析1. 数据加载与预处理2. 多尺度字符统计3. 统计结果可

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

SpringBoot整合Sa-Token实现RBAC权限模型的过程解析

《SpringBoot整合Sa-Token实现RBAC权限模型的过程解析》:本文主要介绍SpringBoot整合Sa-Token实现RBAC权限模型的过程解析,本文给大家介绍的非常详细,对大家的学... 目录前言一、基础概念1.1 RBAC模型核心概念1.2 Sa-Token核心功能1.3 环境准备二、表结

Java集成Onlyoffice的示例代码及场景分析

《Java集成Onlyoffice的示例代码及场景分析》:本文主要介绍Java集成Onlyoffice的示例代码及场景分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 需求场景:实现文档的在线编辑,团队协作总结:两个接口 + 前端页面 + 配置项接口1:一个接口,将o