从 GPT4All 体验 LLM

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

从 GPT4All 体验 LLM
推荐:使用NSDT场景编辑器助你快速搭建可编辑的3D应用场景

什么是 GPT4All?

术语“GPT”源自 Radford 等人 2018 年论文的标题“通过生成预训练提高语言理解”。本文描述了如何证明变压器模型能够理解人类语言。

从那时起,许多人尝试使用转换器架构开发语言模型,并且已经发现足够大的模型可以给出出色的结果。但是,开发的许多模型都是专有的。有付费订阅的服务或具有某些限制条款的许可证。由于尺寸的原因,有些甚至无法在商用硬件上运行。

GPT4All项目试图在通用硬件上向公众提供LLM。它允许你训练和部署模型。还提供预训练模型,其尺寸较小,可以在 CPU 上合理运行。

如何获取 GPT4All

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

在撰写本文时,GPT4All 可从 https://gpt4all.io/index.html 获得,您可以将其作为桌面应用程序或使用 Python 库运行。您可以下载操作系统的安装程序以运行桌面客户端。客户端只有几百MB。您应该会看到一个安装屏幕,如下所示:

安装客户端后,首次启动它将提示您安装模型,该模型可以大至数 GB。首先,您可以选择“”(GPT4All-J 型号)。这是一个相对较小但流行的模型。gpt4all-j-v1.3-groovy

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

如何在 Python 中使用 GPT4All

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

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

1
pip install gpt4all

之后,您只需几行代码即可在 Python 中使用它:

1
2
3
4
5
6
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)

运行上述代码将下载模型文件(如果尚未下载)。之后,加载模型,提供输入,并将响应作为 Python 字典返回,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
{'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'
        }
    }
]
}

上面的示例使用输入作为一个字典的列表。更复杂的输入是许多字典的列表,每个字典都包含键和 .可以是 、 或 ,而 是文本字符串。如果您使用的是 GPT4All-J 模型(如示例所示),则您的角色是在计算机 .输入应该是这两方之间的一系列对话。以下是逐步构建对话的方法:rolecontentrole"system""assistant""user"content"user""assistant"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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))

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[
    {
        "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 是一个不错的工具,您可以在计算机上使用。它允许您探索与大型语言模型的交互,并帮助您更好地了解模型的功能和限制。在这篇文章中,您了解到:

  • GPT4All 有一个桌面客户端,您可以将其安装在计算机上
  • GPT4All有一个Python接口,允许您在代码中与语言模型进行交互
  • 有多种语言模型可用

3D建模学习工作室 整理翻译,转载请注明出处!

NSDT场景编辑器 | NSDT 数字孪生 | GLTF在线编辑器 | 3D模型在线转换 | UnrealSynth虚幻合成数据生成器 | 3D模型自动纹理化工具
2023 power by nsdt©鄂ICP备2023000829号