当前位置:   article > 正文

基于GPT4All本地安装LLM训练_gpt4all 如何训练

gpt4all 如何训练

面向读者:软件工程师、架构师、IT人士、设计人员等

文章目标:接触GPT4ALL,本地安装训练LLM模型,在自己计算机上尝试离线方案

所属专栏:人工智能工具实践

概述

什么是 GPT4All?

如何获取 GPT4All

如何在Python中使用GPT4All

大型语言模型最近变得流行。ChatGPT 很时尚。尝试使用 ChatGPT 来了解 LLM 的内容很容易,但有时,您可能需要一个可以在您的计算机上运行的离线替代方案。在这篇文章中,您将了解 GPT4All 作为可以安装在计算机上的 LLM。特别是,你将学到

  • 什么是 GPT4All
  • 如何安装 GPT4All 桌面客户端
  • 如何在 Python 中运行 GPT4All

概述

这篇文章分为三个部分;他们是:

  • 什么是 GPT4All?
  • 如何获取 GPT4All
  • 如何在Python中使用GPT4All

什么是 GPT4All?

“GPT”一词源自 Radford 等人 2018 年发表的论文《通过生成预训练提高语言理解》的标题。本文描述了如何证明 Transformer 模型能够理解人类语言。

从那时起,许多人尝试使用 Transformer 架构来开发语言模型,并且发现足够大的模型可以给出出色的结果。然而,许多开发的模型都是专有的。它们要么作为付费订阅的服务提供,要么根据带有某些限制性条款的许可证提供。由于尺寸原因,有些甚至无法在商用硬件上运行。

GPT4All 项目试图让公众可以在通用硬件上获得法学硕士学位。它允许您训练和部署模型。预训练模型也可用,其尺寸较小,可以合理地在 CPU 上运行。

如何获取 GPT4All

让我们只关注使用预先训练的模型。

在撰写本文时,GPT4All 可从GPT4All获取,您可以将其作为桌面应用程序运行或使用 Python 库运行。您可以下载适合您操作系统的安装程序来运行桌面客户端。客户端只有几百MB。您应该看到如下安装屏幕:

安装客户端后,第一次启动会提示您安装一个模型,该模型可以大到GB。首先,您可以选择“ gpt4all-j-v1.3-groovy”(GPT4All-J 型号)。这是一种相对较小但很受欢迎的型号。

客户端和模型准备就绪后,您可以在输入框中键入消息。该模型可能期望特定形式的输入,例如特定的语言或风格。该模型需要一种对话风格(如 ChatGPT)并且通常可以很好地处理英语。例如,下面是它如何响应输入“给我 10 种颜色及其 RGB 代码的列表”:

如何在Python中使用GPT4All

GPT4All 的关键组件是模型。桌面客户端只是它的一个界面。除了客户端之外,您还可以通过Python库调用模型。

不出所料,该库被命名为“ gpt4all,”,您可以使用pip命令安装它:

pip install gpt4all
之后,您只需几行代码即可在 Python 中使用它:
  1. import gpt4all
  2. gptj = gpt4all.GPT4All("ggml-gpt4all-j-v1.3-groovy")
  3. messages = [{"role": "user", "content": "Give me a list of 10 colors and their RGB code"}]
  4. ret = gptj.chat_completion(messages)
  5. print(ret)

如果您还没有下载模型文件,运行上面的代码将会下载。然后,加载模型,提供输入,并将响应作为 Python 字典返回,如下所示:
  1. {'model': 'ggml-gpt4all-j-v1.3-groovy',
  2. 'usage': {'prompt_tokens': 272,
  3. 'completion_tokens': 301,
  4. 'total_tokens': 573},
  5. 'choices': [
  6. {'message':
  7. {'role': 'assistant',
  8. 'content': ' Here is a list of 10 colors and their RGB code:Red (255, 0, 0) Green (0, 255, 0) Blue (0, 0, 255) Yellow (255, 255, 0) Orange (255, 127, 0) Purple (0, 128, 255) Pink (255, 192, 203) Blue-Green (0, 0, 255) Green-Blue (0, 0, 255) Blue-Purple (0, 0, 255) Blue-Green (0, 0, 255) Blue-Purple (0, 0'
  9. }
  10. }
  11. ]
  12. }

上面的示例使用输入作为一个字典的列表。更复杂的输入是许多字典的列表,每个字典都包含键 role content 。可以 role "system" "assistant" "user" ,而 content 是文本字符串。如果您像示例一样使用 GPT4All-J 模型,则您的角色是 "user" 计算机运行时的角色 "assistant" 。输入应该是这两方之间的一系列对话。以下是如何逐步构建对话:
  1. import json
  2. import gpt4all
  3. gptj = gpt4all.GPT4All("ggml-gpt4all-j-v1.3-groovy")
  4. messages = [{"role": "user", "content": "Can you explain what is a large language model?"}]
  5. ret = gptj.chat_completion(messages)
  6. messages.append(ret["choices"][0]["message"])
  7. messages.append({"role": "user", "content": "Can you give some examples applications?"})
  8. ret = gptj.chat_completion(messages)
  9. messages.append(ret["choices"][0]["message"])
  10. messages.append({"role": "user", "content": "Are there any limitations?"})
  11. ret = gptj.chat_completion(messages)
  12. messages.append(ret["choices"][0]["message"])
  13. messages.append({"role": "user", "content": "Summarize the above in two sentences."})
  14. ret = gptj.chat_completion(messages)
  15. print(json.dumps(messages, indent=4))
  16. print(json.dumps(ret, indent=4))

请注意,您多次调用该模型。每次它响应时,您都会获取输出并将其附加到消息列表中,以便积累上下文。然后添加一个新对话框并再次调用该模型。您需要收集消息,因为每次调用模型时,它都会重新开始,而不知道之前的对话。因此,您的工作就是保存历史记录以提醒模型其之前的响应。

下面是上述代码的输出示例,其中打印了模型的最后一个输入消息及其响应。您可能会从其他模型中获得更好的结果。由于模型的随机性,您也可能会得到不同的结果:

  1. [
  2.     {
  3.         "role": "user",
  4.         "content": "Can you explain what is a large language model?"
  5.     },
  6.     {
  7.         "role": "assistant",
  8.         "content": " A large language model is a type of artificial intelligence that can process and understand natural language, such as speech or text. It is trained on large amounts of data and can perform tasks such as language translation, sentiment analysis or even answering questions. Large language models are becoming increasingly popular in various industries, including finance and healthcare."
  9.     },
  10.     {
  11.         "role": "user",
  12.         "content": "Can you give some examples applications?"
  13.     },
  14.     {
  15.         "role": "assistant",
  16.         "content": "  Large language models are being used in a variety of applications, such as:- Language translation and machine translation, where large language models are trained on vast amounts of data to improve accuracy and speed.- Sentiment analysis, where large language models are trained to analyze social media data and identify patterns in user opinions.- Question answering, where large language models are trained to answer questions based on a large corpus of data.- Healthcare, where large language models are being used to analyze patient data and identify patterns in health trends.- Finance, where large language models are being used to analyze financial data and identify patterns in"
  17.     },
  18.     {
  19.         "role": "user",
  20.         "content": "Are there any limitations?"
  21.     },
  22.     {
  23.         "role": "assistant",
  24.         "content": "  Yes, there are some limitations to large language models. One of the main challenges is that they require a lot of data to train, which can be expensive and time-consuming. Additionally, large language models can be computationally expensive and may require specialized hardware to run. Another limitation is that large language models may not be able to understand the nuances of human language, which can make them less effective in certain situations. Finally, large language models may not be able to understand the context of a conversation or text, which can make them less effective in certain applications."
  25.     },
  26.     {
  27.         "role": "user",
  28.         "content": "Summarize the above in two sentences."
  29.     }
  30. ]
  31. {
  32.     "model": "ggml-gpt4all-j-v1.3-groovy",
  33.     "usage": {
  34.         "prompt_tokens": 2113,
  35.         "completion_tokens": 542,
  36.         "total_tokens": 2655
  37.     },
  38.     "choices": [
  39.         {
  40.             "message": {
  41.                 "role": "assistant",
  42.                 "content": "  Large language models are a type of artificial intelligence that can process and understand natural language, such as speech or text. They are trained on large amounts of data and can perform tasks such as language translation, sentiment analysis or even answering questions. They are becoming increasingly popular in various industries, including finance and healthcare. However, there are some limitations such as expensive data and specialized hardware, computational expense, lack of understanding nuances in human language and context."
  43.             }
  44.         }
  45.     ]
  46. }

总结

GPT4All 是一个可以在计算机上使用的好工具。它允许您探索与大型语言模型的交互,并帮助您更好地理解模型的功能和局限性。在这篇文章中,您了解到:

  • GPT4All 有一个桌面客户端,您可以将其安装在计算机上
  • GPT4All 有一个 Python 接口,允许您与代码中的语言模型进行交互
  • 有多种语言模型可用
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/208514
推荐阅读