慢吞云雾缓吐愁 发表于 2024-8-8 16:38:54

Llama 3增强中文明白能力,并生成内网穿透API后Spring Boot请求

大家都知道Llama3是用英文训练的,以是它的中文明白力不抱负
前面已经教大家怎样本地部署llama 3.1(原理差不多)
原版部署地址:本地部署llama 3.1生成API而且内网穿透后Spring Boot调用-CSDN博客
这期主要是将原版的模子换成中文的模子(换成其他的也都一样)
 01、部署本地环境

在window搜索“环境”
https://i-blog.csdnimg.cn/direct/6ab4b4464c1a4577bd43b5ea40ae7e15.png
https://i-blog.csdnimg.cn/direct/fc8179ee1b86490fa09070fd1e8d3d4c.png
https://i-blog.csdnimg.cn/direct/0ca2ab1de86b480ca6632a7cd80f2323.png
新建我这三个变量
https://i-blog.csdnimg.cn/direct/297c97f42822472da5d27bcb5fdfa8a7.png
    OLLAMA_HOST   0.0.0.0:11434
OLLAMA_ORIGINS  *

OLLAMA_MODELS  [自定义模子存的位置]
配置好我们测试一下
键盘win+r打开cmd
 https://i-blog.csdnimg.cn/direct/7a7c495fca934150a8e58751672dbe22.png
输入上期我们配置好的模子,你的不一定是ollama run llama3.1
   ollama run llama3.1

ollama run [你的模子]
出现对话输入就没题目接着往下走(如果没下过就等他下载下)
https://i-blog.csdnimg.cn/direct/c02288175d444f08b6e299a01ad37b07.png

https://i-blog.csdnimg.cn/direct/3fb682ae7cea4ce59db4e3433079b3c5.png

 02、利用 Ollama 本地部署 Llama3 中文微调版

起首我们先去Hugging Face下载我们要的模子,下载地址:
   https://huggingface.co/hfl/llama-3-chinese-8b-instruct-gguf/tree/main
 https://i-blog.csdnimg.cn/direct/b9606d7f872a4411826765f7abe5506b.png
就下载 ggml-model-q8_0.gguf就行https://i-blog.csdnimg.cn/direct/1528b079e2e248fea5340947bb944b36.png
经过测试q8_0对家用电脑性价比最高 ,体现也比力均衡(想下载其他的玩玩,配置方式也是一样,背面配置的名字改下就行)
给他移动到环境配置中,模子安装的地方
https://i-blog.csdnimg.cn/direct/3011fbd26ce9412d80edd04e9ccb10dc.png
然后右键新建一个txt文档
 https://i-blog.csdnimg.cn/direct/6f6c34722fb645c5a94c38fc9c71ac86.png
改个名字而且把背面的".txt"给删掉,酿成我如许
https://i-blog.csdnimg.cn/direct/f8a7e60083fa46c785b9e5c37d36826f.png
 用记事本打开它,输入
   FROM [你自己的模子地址]
TEMPLATE """{{ if .System }}<|start_header_id|>system<|end_header_id|>
{{ .System }}<|eot_id|>{{ end }}{{ if .Prompt }}<|start_header_id|>user<|end_header_id|>
{{ .Prompt }}<|eot_id|>{{ end }}<|start_header_id|>assistant<|end_header_id|>
{{ .Response }}<|eot_id|>"""
SYSTEM """You are a helpful assistant. 你是一个乐于助人的助手。"""
PARAMETER temperature 0.2
PARAMETER num_keep 24
PARAMETER stop <|start_header_id|>
PARAMETER stop <|end_header_id|>
PARAMETER stop <|eot_id|>
https://i-blog.csdnimg.cn/direct/fd2608126ccd4ef1aafc0744b384d6e5.png
 然后在你的文件位置的地址栏中输入cmd
https://i-blog.csdnimg.cn/direct/e656bee6191044c1890c7127e56966a4.png
创建一个模子(名字自定义) 
   ollama create llama3-zh-inst -f Modelfile
 https://i-blog.csdnimg.cn/direct/cfa0672dd62f4f70bcc632b0551668d3.png
 https://i-blog.csdnimg.cn/direct/570dd0d124cf4e92803746a4e1d56513.png
然后执行命令 
   ollama serve
https://i-blog.csdnimg.cn/direct/f4b16bf45d644f9185e510558252869d.png
 然后打开你的postman,这里我的是Apifox都是一样的,输入我的这些测试下
   http://localhost:11434/api/generate
    {
  "model": "llama3-zh-inst",
  "prompt": "天空是什么颜色的?",
  "stream": false
}
 https://i-blog.csdnimg.cn/direct/70ce946f1ad546dcb7ed48deb41603c6.png
如果出现我的这个就阐明成功了

03、内网穿透更改外网可访问的API地址

起首下载花生壳客户端
下载地址:花生壳客户端官方下载 - 贝锐花生壳官网
一样获得.exe的安装文件,双击一直下一步安装完成(可以自定义安装路径)
https://i-blog.csdnimg.cn/direct/a51aac59183249509513ede66741571b.png
 点击添加映射,去网页填上自己的IPV4的地址
https://i-blog.csdnimg.cn/direct/172fe8af85494cc2b7351b55b2d33cbb.png
 末了获得了映射的公网IP
 https://i-blog.csdnimg.cn/direct/d35a6e3528cc41b6a66f4e1314c2d4f6.png
将你的内网穿透的地址复制到欣赏器如果出现这个就成功了
 https://i-blog.csdnimg.cn/direct/743ea480ad2c42cf84d864d4d52a28aa.png
也可以用postman测试玩一下

 04、内网穿透更改外网可访问的API地址

打开你的springboot项目
https://i-blog.csdnimg.cn/direct/dee460e87c824fac8ef430e8446791be.png
public String talkToLlama(String text) throws IOException {
      String url = "https://你的内网穿透地址/api/chat";//请求地址

      OkHttpClient client = new OkHttpClient()
                .newBuilder()
                .connectTimeout(60, TimeUnit.SECONDS) // 设置连接超时时间
                .writeTimeout(60, TimeUnit.SECONDS) // 设置写入超时时间
                .readTimeout(60, TimeUnit.SECONDS) // 设置读取超时时间
                .build();

      // 定义请求体的媒体类型为JSON
      MediaType JSON = MediaType.get("application/json; charset=utf-8");

      String json = "{\n" +
                "\"model\": \"llama3-zh-inst\"," +
                "\"messages\": [{\"role\": \"user\", \"content\": \""+text+"\"}],"+
                "\"stream\": "+false+"}";
      RequestBody body = RequestBody.create(json, JSON);

      //发送POST请求并获取响应:
      Request request = new Request.Builder().url(url)
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json") // 显式设置Content-Type头
                .post(body) // 使用POST方法并传递请求体
                .build();
      // 发送请求并获取响应
      Response response = client.newCall(request).execute();
      String result = response.body().string();
      System.out.println(result);

      JSONObject returnJsonObject = new JSONObject(result);
      JSONObject messages = returnJsonObject.getJSONObject("message");
      // 提取 content 的值
      String content = messages.getString("content").replace("“", "").replace("\"", "").replace("\r", "").replace("\n", "");
      // 输出 content 的值
      System.out.println("回答的: " + content);
      returncontent;
    } 需要使用就直接调用这个方法就行
String resultText = talkToLlama("自我介绍一下");
免责声名:

一切均严格参照ollama开源策略,没有任何侵权意图,本文章全由自己编写,如出现任何题目请联系我,我将删除。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Llama 3增强中文明白能力,并生成内网穿透API后Spring Boot请求