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简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

Java的IO模型、Netty原理解析

《Java的IO模型、Netty原理解析》Java的I/O是以流的方式进行数据输入输出的,Java的类库涉及很多领域的IO内容:标准的输入输出,文件的操作、网络上的数据传输流、字符串流、对象流等,这篇... 目录1.什么是IO2.同步与异步、阻塞与非阻塞3.三种IO模型BIO(blocking I/O)NI

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

springboot集成Deepseek4j的项目实践

《springboot集成Deepseek4j的项目实践》本文主要介绍了springboot集成Deepseek4j的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录Deepseek4j快速开始Maven 依js赖基础配置基础使用示例1. 流式返回示例2. 进阶

Spring Boot 集成 Quartz 使用Cron 表达式实现定时任务

《SpringBoot集成Quartz使用Cron表达式实现定时任务》本文介绍了如何在SpringBoot项目中集成Quartz并使用Cron表达式进行任务调度,通过添加Quartz依赖、创... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启

Spring AI ectorStore的使用流程

《SpringAIectorStore的使用流程》SpringAI中的VectorStore是一种用于存储和检索高维向量数据的数据库或存储解决方案,它在AI应用中发挥着至关重要的作用,本文给大家介... 目录一、VectorStore的基本概念二、VectorStore的核心接口三、VectorStore的

Spring AI集成DeepSeek三步搞定Java智能应用的详细过程

《SpringAI集成DeepSeek三步搞定Java智能应用的详细过程》本文介绍了如何使用SpringAI集成DeepSeek,一个国内顶尖的多模态大模型,SpringAI提供了一套统一的接口,简... 目录DeepSeek 介绍Spring AI 是什么?Spring AI 的主要功能包括1、环境准备2

Spring AI集成DeepSeek实现流式输出的操作方法

《SpringAI集成DeepSeek实现流式输出的操作方法》本文介绍了如何在SpringBoot中使用Sse(Server-SentEvents)技术实现流式输出,后端使用SpringMVC中的S... 目录一、后端代码二、前端代码三、运行项目小天有话说题外话参考资料前面一篇文章我们实现了《Spring

Spring AI与DeepSeek实战一之快速打造智能对话应用

《SpringAI与DeepSeek实战一之快速打造智能对话应用》本文详细介绍了如何通过SpringAI框架集成DeepSeek大模型,实现普通对话和流式对话功能,步骤包括申请API-KEY、项目搭... 目录一、概述二、申请DeepSeek的API-KEY三、项目搭建3.1. 开发环境要求3.2. mav

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea