Python的构建块,以探索大型语言模型,仅为512MB RAM

该软件包使使用Python的大型语言模型尽可能简单。所有推论都是在本地执行的,以默认情况下将数据私有化。
可以使用以下命令安装此软件包:
pip install languagemodels安装后,您应该能够与Python中的软件包进行交互,如下所示:
> >> import languagemodels as lm
> >> lm . do ( "What color is the sky?" )
'The color of the sky is blue.'这将需要在第一次运行中下载大量数据(〜250MB)。模型将被缓存以供以后使用,随后的呼叫应很快。
以下是一些用法示例,例如Python repl会议。这应该在repl,笔记本或传统脚本和应用程序中起作用。
> >> import languagemodels as lm
> >> lm . do ( "Translate to English: Hola, mundo!" )
'Hello, world!'
> >> lm . do ( "What is the capital of France?" )
'Paris.'如果需要,则可以将输出限制为选择列表:
> >> lm . do ( "Is Mars larger than Saturn?" , choices = [ "Yes" , "No" ])
'No'基本模型应在任何具有512MB内存的系统上快速运行,但是可以增加此内存限制以选择更强大的模型,以消耗更多资源。这是一个例子:
> >> import languagemodels as lm
> >> lm . do ( "If I have 7 apples then eat 5, how many apples do I have?" )
'You have 8 apples.'
> >> lm . config [ "max_ram" ] = "4gb"
4.0
> >> lm . do ( "If I have 7 apples then eat 5, how many apples do I have?" )
'I have 2 apples left.'如果您有可用CUDA的NVIDIA GPU,则可以选择使用GPU进行推理:
> >> import languagemodels as lm
> >> lm . config [ "device" ] = "auto" > >> import languagemodels as lm
> >> lm . complete ( "She hid in her room until" )
'she was sure she was safe'提供了辅助功能以从外部来源检索可用于增强提示上下文的文本。
> >> import languagemodels as lm
> >> lm . get_wiki ( 'Chemistry' )
' Chemistry is the scientific study ...
>> > lm . get_weather ( 41.8 , - 87.6 )
' Partly cloudy with a chance of rain ...
>> > lm . get_date ()
'Friday, May 12, 2023 at 09:27AM'这是一个示例,显示如何使用它(与以前的聊天示例进行比较):
> >> lm . do ( f"It is { lm . get_date () } . What time is it?" )
'The time is 12:53PM.'提供语义搜索以检索可能提供文档商店中有用上下文的文档。
> >> import languagemodels as lm
> >> lm . store_doc ( lm . get_wiki ( "Python" ), "Python" )
> >> lm . store_doc ( lm . get_wiki ( "C language" ), "C" )
> >> lm . store_doc ( lm . get_wiki ( "Javascript" ), "Javascript" )
> >> lm . get_doc_context ( "What does it mean for batteries to be included in a language?" )
' From Python document : It is often described as a "batteries included" language due to its comprehensive standard library . Guido van Rossum began working on Python in the late 1980 s as a successor to the ABC programming language and first released it in 1991 as Python 0.9 .
From C document : It was designed to be compiled to provide low - level access to memory and language constructs that map efficiently to machine instructions , all with minimal runtime support .'完整的文档
由于INT8量化和Ctranslate2后端,该软件包目前的表现优于CPU推理的Face transformers表现。下表使用20个问题测试集上的最佳可用量化在相同模型上比较了CPU推理性能。
| 后端 | 推理时间 | 使用的内存 |
|---|---|---|
| 拥抱面部变压器 | 22s | 1.77GB |
| 这个包 | 11s | 0.34GB |
请注意,量化在技术上确实略微损害了产出质量,但在此级别应该可以忽略不计。
提供了明智的默认模型。随着更强大的模型可用,该软件包应随着时间的推移而改善。所使用的基本型号比当今使用的最大型号小1000倍。它们作为学习工具很有用,但远低于当前的最新状态。
以下是该软件包使用的当前默认模型,用于提供的max_ram值:
| max_ram | 模型名称 | 参数(b) |
|---|---|---|
| 0.5 | Lamini-Flan-T5-248M | 0.248 |
| 1.0 | Lamini-Flan-T5-783M | 0.783 |
| 2.0 | Lamini-Flan-T5-783M | 0.783 |
| 4.0 | Flan-Alpaca-gpt4-Xl | 3.0 |
| 8.0 | OpenChat-3.5-0106 | 7.0 |
对于代码完成,使用CODET5+系列模型。
此软件包本身已获得商业用途的许可,但是所使用的模型可能与商业用途不兼容。为了商业使用此软件包,您可以使用require_model_license函数通过许可类型过滤模型。
> >> import languagemodels as lm
> >> lm . config [ 'instruct_model' ]
'LaMini-Flan-T5-248M-ct2-int8'
> >> lm . require_model_license ( "apache|bsd|mit" )
> >> lm . config [ 'instruct_model' ]
'flan-t5-base-ct2-int8'建议确认使用的模型满足您软件的许可要求。
该软件包的目标之一是成为学习者和教育工作者的直接工具,以探索大型语言模型与现代软件开发相交的方式。它可用于为许多学习项目做繁重的工作:
examples目录中包含了几个示例程序和笔记本。