当前位置:   article > 正文

如何拥有自己的本地大语言模型_bge-large-zh-v1.5

bge-large-zh-v1.5

一、环境需求

        本次demo是在win 11上进行搭建,模型选择为ChatGLM3-6B,python 版本为3.11,java 版本为jdk 17,node 版本为 node18.x。由于本人不擅长python,所以用java进行项目开发。

        项目思路是用ChatGLM3-6B开源大模型进行本地搭建,以api_server方式启动。此时搭配开源的ui框架就已经可以实现一个基本的GLM模型。进行模型微调和定制开发则采用spring ai + langchain4j。

        注意想在本地运行chatglm3-6b模型需要12G显存,否则以cpu方式运行程序会非常慢,内存要求也会要32g。

二、环境搭建

        conda安装直接跳过,直接进入正题

1、安装pytorch环境

        1.1官网 PyTorch

        1.2 pip安装

        注意安装时选择对应自己cuda版本的torch版本

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

2、下载ChatGLM3项目

        2.1仓库地址GitHub - THUDM/ChatGLM3: ChatGLM3 series: Open Bilingual Chat LLMs | 开源双语对话语言模型

3、下载chatglm3-6b模型

        3.1模型地址 https://huggingface.co/THUDM/chatglm3-6b

        3.2模型下载命令 

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

4、下载bge-large-zh-v1.5模型

        4.1模型地址https://huggingface.co/BAAI/bge-large-zh-v1.5

        4.2模型下载命令 

git clone https://huggingface.co/BAAI/bge-large-zh-v1.5
        上面这两个模型下载需要科学上网,后面补充网盘资源

        4.3把下载好的两个模型文件夹复制到ChatGLM3项目根目录下,复制好后文件目录如下        

        4.4修改openai_api_demo/api_server.py文件中设置模型路径的位置为以下路径

  1. # set LLM path
  2. MODEL_PATH = os.environ.get('MODEL_PATH', 'chatglm3-6b')
  3. TOKENIZER_PATH = os.environ.get("TOKENIZER_PATH", MODEL_PATH)
  4. # set Embedding Model path
  5. EMBEDDING_PATH = os.environ.get('EMBEDDING_PATH', 'bge-large-zh-v1.5')

        4.5执行 下面命令以openai-api模式启动

 python ./openai_api_demo/api_server.py     

         出现以下日志时启动成功        

        此时可以通过api形式进行访问,编写自己的程序了,如果想直接使用本地模型,官方提供了一个带ui的启动方式,启动前修改basic_demo/web_demo_streamlit.py文件中设置模型路径的位置如下

  1. MODEL_PATH = os.environ.get('MODEL_PATH', './chatglm3-6b')
  2. TOKENIZER_PATH = os.environ.get("TOKENIZER_PATH", MODEL_PATH)

        然后执行下面命令即可弹出一个webui界面

streamlit run basic_demo/web_demo_streamlit.py

5、下载ChatGPT-Next-Web项目(使用开源web-ui)

        5.1github地址 GitHub - ChatGPTNextWeb/ChatGPT-Next-Web: A cross-platform ChatGPT/Gemini UI (Web / PWA / Linux / Win / MacOS). 一键拥有你自己的跨平台 ChatGPT/Gemini 应用。

        5.2在项目目录下新建一个.env.local文件,指定大模型地址为本地大模型,内容如下

  1. OPENAI_API_KEY=none
  2. BASE_URL=http://0.0.0.0:8000

        5.3执行 yarn run dev即可启动ui项目

6、搭建spring ai环境

        注意,该项目必须使用jdk>=17。

       6.1 创建一个springboot 3.x项目,pom如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>3.2.3</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.iai</groupId>
  12. <artifactId>iask</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>iask</name>
  15. <description>iask</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. <spring-ai.version>0.8.0</spring-ai.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. <exclusions>
  25. <exclusion>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-tomcat</artifactId>
  28. </exclusion>
  29. </exclusions>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-undertow</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.ai</groupId>
  37. <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-devtools</artifactId>
  42. <scope>runtime</scope>
  43. <optional>true</optional>
  44. </dependency>
  45. <dependency>
  46. <groupId>com.mysql</groupId>
  47. <artifactId>mysql-connector-j</artifactId>
  48. <scope>runtime</scope>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.projectlombok</groupId>
  52. <artifactId>lombok</artifactId>
  53. <optional>true</optional>
  54. </dependency>
  55. <dependency>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-starter-test</artifactId>
  58. <scope>test</scope>
  59. </dependency>
  60. <dependency>
  61. <groupId>dev.langchain4j</groupId>
  62. <artifactId>langchain4j</artifactId>
  63. <version>0.27.1</version>
  64. </dependency>
  65. <dependency>
  66. <groupId>dev.langchain4j</groupId>
  67. <artifactId>langchain4j-spring-boot-starter</artifactId>
  68. <version>0.24.0</version>
  69. </dependency>
  70. </dependencies>
  71. <dependencyManagement>
  72. <dependencies>
  73. <dependency>
  74. <groupId>org.springframework.ai</groupId>
  75. <artifactId>spring-ai-bom</artifactId>
  76. <version>${spring-ai.version}</version>
  77. <type>pom</type>
  78. <scope>import</scope>
  79. </dependency>
  80. </dependencies>
  81. </dependencyManagement>
  82. <build>
  83. <plugins>
  84. <plugin>
  85. <groupId>org.springframework.boot</groupId>
  86. <artifactId>spring-boot-maven-plugin</artifactId>
  87. <configuration>
  88. <excludes>
  89. <exclude>
  90. <groupId>org.projectlombok</groupId>
  91. <artifactId>lombok</artifactId>
  92. </exclude>
  93. </excludes>
  94. </configuration>
  95. </plugin>
  96. </plugins>
  97. </build>
  98. <repositories>
  99. <repository>
  100. <id>spring-milestones</id>
  101. <name>Spring Milestones</name>
  102. <url>https://repo.spring.io/milestone</url>
  103. <snapshots>
  104. <enabled>false</enabled>
  105. </snapshots>
  106. </repository>
  107. </repositories>
  108. </project>

        6.2配置application.yaml

  1. server:
  2. port: 8886
  3. # undertow 配置
  4. undertow:
  5. # HTTP post内容的最大大小。当值为-1时,默认值为大小是无限的
  6. max-http-post-size: -1
  7. # 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理
  8. # 每块buffer的空间大小,越小的空间被利用越充分
  9. buffer-size: 512
  10. # 是否分配的直接内存
  11. direct-buffers: true
  12. threads:
  13. # 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程
  14. io: 8
  15. # 阻塞任务线程池, 当执行类似servlet请求阻塞操作, undertow会从这个线程池中取得线程,它的值设置取决于系统的负载
  16. worker: 256
  17. spring:
  18. ai:
  19. openai:
  20. api-key: none
  21. base-url: http://0.0.0.0:8000
  22. chat:
  23. options:
  24. model: chatglm3-6b

        6.3解决undertow使用默认websocket缓冲池警告(非必须)

        创建一个配置类UndertowConfig.java

  1. package com.iai.iask.config;
  2. import io.undertow.server.DefaultByteBufferPool;
  3. import io.undertow.websockets.jsr.WebSocketDeploymentInfo;
  4. import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
  5. import org.springframework.boot.web.server.WebServerFactoryCustomizer;
  6. import org.springframework.stereotype.Component;
  7. /**
  8. * @author: wongcai
  9. * @date: 2024-03-12 15:42
  10. * @description: 解决启动io.undertow.websockets.jsr UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used的警告
  11. */
  12. @Component
  13. public class UndertowConfig implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
  14. /**
  15. * 设置 Undertow 的 websocket 缓冲池
  16. */
  17. @Override
  18. public void customize(UndertowServletWebServerFactory factory) {
  19. // 默认不直接分配内存 如果项目中使用了 websocket 建议直接分配
  20. factory.addDeploymentInfoCustomizers(deploymentInfo -> {
  21. WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo();
  22. webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(false, 512));
  23. deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo", webSocketDeploymentInfo);
  24. });
  25. }
  26. }

7、测试代码

        7.1编写测试AiController.java

        

  1. package com.iai.iask.aicontroller;
  2. import org.springframework.ai.openai.OpenAiChatClient;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. @RequestMapping("/api/v1")
  9. public class AiController {
  10. private final OpenAiChatClient chatClient;
  11. @Autowired
  12. public AiController(OpenAiChatClient chatClient) {
  13. this.chatClient = chatClient;
  14. }
  15. @RequestMapping("/chat")
  16. public String chat(@RequestParam(value = "message",defaultValue = "Hi") String message){
  17. return chatClient.call(message);
  18. }
  19. }

        7.2浏览器访问测试

        

三、相关文件

        缺少部分逐渐补全

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/347350
推荐阅读
相关标签
  

闽ICP备14008679号