[LLM] 开源 AI 大语言模型的本地化定制实践

发布时间 2023-08-16 09:05:20作者: ercom

 

LLM(Large Language Model,大型语言模型)是一种基于深度学习的自然语言处理模型,旨在理解和生成人类语言。

它们在大量的文本数据上进行训练,可以执行广泛的任务,包括文本总结、翻译、情感分析等等。

LLM的特点是规模庞大,包含数十亿的参数,帮助它们学习语言数据中的复杂模式。

 

本文假定你对一些类似 ChatGPT 的工具已有使用经验, 了解其中的强大和不足。

 

LLM 应用层面介绍

 

1、LLM 当前主要应用

 

通用问答类——基于OpenAI等做的套壳。(能做不限于创作、翻译、分析、摘要、预测)

领域搜索类——基于”数据库”做的垂直领域搜索。*

  

2、为什么需要开源的 LLM ?

 

隐私性

基于openai或其它第三方模型时对数据的控制性;AWS、Azure内的数据隐私性。

(随提供方政策)

 

精准性

当你不知道某件事的时候,生成的答案感觉是靠谱的,但不一定正确。

(GPT插件系统、可信数据源)

怎么验证这个正确性、提高服务的准确性,是需要提高的地方。

 

拓展性

单独使用 LLM 通常不足以创建一个真正强大的应用程序,真正的强大之处在于将它们与其他计算或知识来源结合起来。

(LLM训练成本)

后面会介绍基于语言模型的开发框架 LangChain。

 

3、LLM 应用优化方向

0.(模型架构改进)

1. 模型调参。(Fine-tuning:小规模的特定任务上继续训练)

2. 关键词拆分。(AutoGPT为代表的,用合理的架构让模型自动拆分关键词、更细使得精度提高)

 

LLM 应用实践

 

1. 单独部署使用开源 LLM 

  一种是用终端执行,另一种是用比较多的 Webui 可以直接与同一类LLM进行chat。

  比如基于 ChatGLM 的 ChatGLM-webui(https://github.com/Akegarasu/ChatGLM-webui/),操作示例:https://www.cnblogs.com/farwish/p/17289475.html

  比如基于 alpaca-7B-ggml 的 Serge(https://github.com/nsarrazin/serge),操作示例:https://www.cnblogs.com/farwish/p/17280159.html

  等等。

 

2. 开源 LLM 与私有数据配合

  LangChain 框架使从数据到 LLM 的各个部分都组件化,LangChain + LLMs 较为通用。

 

1)LangChain 与某一个 LLM 的结合。

## pip install langchain
## pip install gpt4all

from langchain import PromptTemplate, LLMChain
from langchain.llms import GPT4All# 下载语言模型
local_path = (
    "./wizardlm-13b-v1.1-superhot-8k.ggmlv3.q4_0.bin"  # 本地目标文件路径
)

import requests

from pathlib import Path
from tqdm import tqdm

Path(local_path).parent.mkdir(parents=True, exist_ok=True)

# 下载语言模型,官网 https://gpt4all.io
url = 'https://gpt4all.io/models/wizardlm-13b-v1.1-superhot-8k.ggmlv3.q4_0.bin'

# 1.
response = requests.get(url, stream=True)

# 2
with open(local_path, 'wb') as f:
    for chunk in tqdm(response.iter_content(chunk_size=8192)):
        if chunk:
            f.write(chunk)

# 设置模版
template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate(template=template, input_variables=["question"])


# Verbose is required
llm = GPT4All(model=local_path, verbose=True)

llm_chain = LLMChain(prompt=prompt, llm=llm)

question = " What is cup ?"
llm_chain.run(question)

official doc:https://python.langchain.com/docs/integrations/llms/gpt4all

 

2)对文档作为知识源的查询提问。

考虑到 LLM 对理解文本的熟练程度,在这里是一个很好的工具。

```code

```

official doc:https://python.langchain.com/docs/use_cases/question_answering.html

 

3)其它-多模态方面

想尝试其他类型 AI 的, 比如多模态的, 可以在线进行体验:https://tool.offso.com/chatai

3. 其它:多模态方面

想尝试其他多模态 AI 的, 可以在线进行体验:https://tool.offso.com/chatai

这里不展开讨论,除了文生图、图生文、创造出优质内容,随着硬件等各方面提升、未来还会有更多场景涌现。

 

Other:https://gist.github.com/psychemedia/51f45fbfe160f78605bdd0c1b404e499

Link:https://www.cnblogs.com/farwish/p/17632893.html