赞
踩
✍面向读者:软件工程师、架构师、IT人士、设计人员等
✍文章目标:接触GPT4ALL,本地安装训练LLM模型,在自己计算机上尝试离线方案
✍所属专栏:人工智能工具实践
大型语言模型最近变得流行。ChatGPT 很时尚。尝试使用 ChatGPT 来了解 LLM 的内容很容易,但有时,您可能需要一个可以在您的计算机上运行的离线替代方案。在这篇文章中,您将了解 GPT4All 作为可以安装在计算机上的 LLM。特别是,你将学到
这篇文章分为三个部分;他们是:
“GPT”一词源自 Radford 等人 2018 年发表的论文《通过生成预训练提高语言理解》的标题。本文描述了如何证明 Transformer 模型能够理解人类语言。
从那时起,许多人尝试使用 Transformer 架构来开发语言模型,并且发现足够大的模型可以给出出色的结果。然而,许多开发的模型都是专有的。它们要么作为付费订阅的服务提供,要么根据带有某些限制性条款的许可证提供。由于尺寸原因,有些甚至无法在商用硬件上运行。
GPT4All 项目试图让公众可以在通用硬件上获得法学硕士学位。它允许您训练和部署模型。预训练模型也可用,其尺寸较小,可以合理地在 CPU 上运行。
让我们只关注使用预先训练的模型。
在撰写本文时,GPT4All 可从GPT4All获取,您可以将其作为桌面应用程序运行或使用 Python 库运行。您可以下载适合您操作系统的安装程序来运行桌面客户端。客户端只有几百MB。您应该看到如下安装屏幕:
安装客户端后,第一次启动会提示您安装一个模型,该模型可以大到GB。首先,您可以选择“ gpt4all-j-v1.3-groovy
”(GPT4All-J 型号)。这是一种相对较小但很受欢迎的型号。
客户端和模型准备就绪后,您可以在输入框中键入消息。该模型可能期望特定形式的输入,例如特定的语言或风格。该模型需要一种对话风格(如 ChatGPT)并且通常可以很好地处理英语。例如,下面是它如何响应输入“给我 10 种颜色及其 RGB 代码的列表”:
GPT4All 的关键组件是模型。桌面客户端只是它的一个界面。除了客户端之外,您还可以通过Python库调用模型。
不出所料,该库被命名为“ gpt4all
,”,您可以使用pip
命令安装它:
pip install gpt4all
- import gpt4all
-
- gptj = gpt4all.GPT4All("ggml-gpt4all-j-v1.3-groovy")
- messages = [{"role": "user", "content": "Give me a list of 10 colors and their RGB code"}]
- ret = gptj.chat_completion(messages)
- print(ret)
-
- {'model': 'ggml-gpt4all-j-v1.3-groovy',
- 'usage': {'prompt_tokens': 272,
- 'completion_tokens': 301,
- 'total_tokens': 573},
- 'choices': [
- {'message':
- {'role': 'assistant',
- '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'
- }
- }
- ]
- }
role
和
content
。可以
role
是
"system"
、
"assistant"
或
"user"
,而
content
是文本字符串。如果您像示例一样使用 GPT4All-J 模型,则您的角色是
"user"
计算机运行时的角色
"assistant"
。输入应该是这两方之间的一系列对话。以下是如何逐步构建对话:
- import json
- import gpt4all
-
- gptj = gpt4all.GPT4All("ggml-gpt4all-j-v1.3-groovy")
- messages = [{"role": "user", "content": "Can you explain what is a large language model?"}]
- ret = gptj.chat_completion(messages)
-
- messages.append(ret["choices"][0]["message"])
- messages.append({"role": "user", "content": "Can you give some examples applications?"})
- ret = gptj.chat_completion(messages)
-
- messages.append(ret["choices"][0]["message"])
- messages.append({"role": "user", "content": "Are there any limitations?"})
- ret = gptj.chat_completion(messages)
-
- messages.append(ret["choices"][0]["message"])
- messages.append({"role": "user", "content": "Summarize the above in two sentences."})
- ret = gptj.chat_completion(messages)
-
- print(json.dumps(messages, indent=4))
- print(json.dumps(ret, indent=4))
下面是上述代码的输出示例,其中打印了模型的最后一个输入消息及其响应。您可能会从其他模型中获得更好的结果。由于模型的随机性,您也可能会得到不同的结果:
- [
- {
- "role": "user",
- "content": "Can you explain what is a large language model?"
- },
- {
- "role": "assistant",
- "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."
- },
- {
- "role": "user",
- "content": "Can you give some examples applications?"
- },
- {
- "role": "assistant",
- "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"
- },
- {
- "role": "user",
- "content": "Are there any limitations?"
- },
- {
- "role": "assistant",
- "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."
- },
- {
- "role": "user",
- "content": "Summarize the above in two sentences."
- }
- ]
- {
- "model": "ggml-gpt4all-j-v1.3-groovy",
- "usage": {
- "prompt_tokens": 2113,
- "completion_tokens": 542,
- "total_tokens": 2655
- },
- "choices": [
- {
- "message": {
- "role": "assistant",
- "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."
- }
- }
- ]
- }
GPT4All 是一个可以在计算机上使用的好工具。它允许您探索与大型语言模型的交互,并帮助您更好地理解模型的功能和局限性。在这篇文章中,您了解到:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。