当前位置: 欣欣网 > 码农

Ollama如何构建自己的Llama3中文模型模型

2024-05-16码农

Ollama

Ollama 是一个开源的大型语言模型(LLM)服务工具,它允许用户在本地机器上运行和部署大型语言模型。Ollama 设计为一个框架,旨在简化在 Docker 容器中部署和管理大型语言模型的过程,使得这一过程变得简单快捷。用户可以通过简单的命令行操作,快速在本地运行如 Llama 3 这样的开源大型语言模型。

应用模型

注意:推荐下载 GGUF文件格式的模型,可以快速简洁的导入 Ollama 中

0X01 下载模型文件

下载地址: https://huggingface.co/shenzhi-wang/Llama3-8B-Chinese-Chat-GGUF-8bit/tree/main

下载 Llama3-8B-Chinese-Chat-q8_0-v2_1.gguf 模型文件

0X02 编写模型文件

Modelfile 文件内容如下:

# FROM 指定 GGUF 文件的路径
FROM D:/AI/Download/Llama3-8B-Chinese-Chat-q8_0-v2_1.gguf

0X03 创建 Ollama 模型

使用 ollama create 命令可以根据 Modelfile 创建一个新的模型

ollama create tinywan-Llama3-8B-Chinese -f ./Modelfile
transferring model data
using existing layer sha256:ea6e8d5cda0fc798898b67f6e728eb2d02877a2599aa3c8290aaa6f681c2bb9e
creating new layer sha256:e6e86a160950a92b7b32fcd6fcbc830e38634be6d616ec57452561bfe2a243fb
writing manifest
success

这个命令会读取 Modelfile 中的配置,并创建一个名为 tinywan-Llama3-8B-Chinese 的新模型。

通过命令 ollama list 查看模型列表

> ollama list
NAME ID SIZE MODIFIED
tinywan-Llama3-8B-Chinese:latest adcb30feaee5 16 GB About a minute ago
llama3:8b a6990ed6be41 4.7 GB 2 weeks ago

0X04 运行 Ollama 模型

使用 ollama run 命令来验证 tinywan-Llama3-8B-Chinese 新模型

ollama run tinywan-Llama3-8B-Chinese:latest

模型测试

>>> 写一首诗吧
"我说。
他眨了眨眼,微笑着回答道:「好啊,我试试看。」然后,他闭上眼睛,开始吟唱:
在星空之下,
月光洒银辉。
风轻轻地舞,
树叶低语。
山川河流静默,
夜晚笼罩大地。
但就在这宁静中,
我感受到生命的脉动,
它如同诗歌般跳跃,
穿梭于每一颗心中。

REST API

除了命令行界面, OLlama 还提供了 REST API ,使得您可以通过HTTP请求与模型交互。这对于在Web应用程序中集成ollama尤其有用。

请求

要生成模型的响应,您可以发送一个POST请求到 /api/generate

curl -X POST http://localhost:11434/api/generate -d '{
 "model": "llama3:8b",
 "prompt": "中文回答。你是什么大模型?",
 "stream": false
}'

响应

返回一个JSON对象流:

{
"model""llama3:8b",
"created_at""2024-05-12T09:00:01.9513668Z",
"response""I'm LLaMA, a large language model developed by Meta AI that can understand and respond to human input in a conversational manner. I've been trained on a massive dataset of text from the internet and can generate human-like responses to a wide range of topics and questions. My training data includes but is not limited to:\n\n* Web pages: articles, blogs, forums\n* Books: fiction and non-fiction\n* Research papers: academic journals, research articles\n* User-generated content: social media, comments, reviews\n\nI'm able to generate responses that are contextualized to the conversation I'm having with you. This means I can recall previous statements or questions in our conversation and respond accordingly.\n\nMy capabilities include:\n\n* Answering questions on a wide range of topics, from science and history to entertainment and culture\n* Generating text summaries of long pieces of content\n* Translating text from one language to another (in this case, Chinese to English)\n* Responding to natural language input in a conversational manner\n\nI'm constantly learning and improving my responses based on the conversations I have with users like you. So feel free to ask me anything, and I'll do my best to provide helpful and accurate information!",
"done"true,
"done_reason""stop",
"context": [
128006,
...
128009
 ],
"total_duration": 37185731000,
"load_duration": 10876300,
"prompt_eval_count": 13,
"prompt_eval_duration": 1058480000,
"eval_count": 248,
"eval_duration": 36115711000
}

更多了解:https://github.com/ollama/ollama/blob/main/docs/api.md

meta-llama

项目开源地址:https://github.com/meta-llama/llama3

模型下载直接在在 Hugging Face 上下载就是了。模型地址: https://huggingface.co/models

注意:推荐下载 GGUF 文件格式的模型,可以快速简洁的导入 Ollama 中。有了 gguf 格式的模型文件这样就不需要通过 llama.cpp 项目进行模型格式转换了。

其他

删除模型

如果需要删除一个本地的模型,可以使用 ollama rm 命令。这将从您的本地环境中删除名为 my-model 的模型。

ollama rm my-model

复制模型

您可以使用ollama cp命令复制一个模型,创建一个新的模型副本:

ollama cp original-model new-model