前言:LLAMA是一种神经网络模型,全称为Language Model with an Average Attention Mechanism(具有平均注意机制的语言模型)。它是一种用于自然语言处理任务的模型,特别适用于生成文本和回答问题。LLAMA模型结合了注意力机制和平均池化,以提高模型对输入文本的理解和生成能力。它在多项基准测试中取得了很好的性能,是一种强大的语言模型。
此文章以基于OpenAI聊天模型训练而来的openchat_3.5.Q3_K_L模型为例进行实现。
1.准备工作:(注意打不开的链接需要科学上网
下载必备软件:MicroSoft VS,CMAKE,Git(这一步就不详写,自行安装
下载本例子的AI模型:openchat_3.5.Q3_K_L(放入项目目录/Content/Movies/Models/..中
下载LLAMA插件:Llama-Unreal(我的教程后面修改了部分代码,请支持插件原作者MikaPi
新建空白C++项目后关闭引擎,并打开项目文件夹:
项目文件夹中创建Plugins文件夹并放入TTS和LLAMA插件:(TTS在上一篇文章有分享
进入LLAMA插件文件夹,右键空白区域打开Git:
mkdir llama
cd llama
llama文件中放入下载解压好的llama.cpp:llama.cpp兼容版本
创建build文件夹进行cmake编译:
cd llama.cpp
mkdir build
cd build/
cmake .. -DBUILD_SHARED_LIBS=ON
cd ..
cmake --build build --config Release -j --verbose
生成成功:
1.我们需要的文件是llama.dll:复制到Plugins\UELlama\Binaries\Win64文件夹中
2.llama插件的Includes和Libraries文件夹中已经有了所有需要的文件,遂不需要复制。
2.项目各项设置及代码:
修改UELlama.Build.cs:(修复打包后dll缺失
using UnrealBuildTool;
using System.IO;
public class UELlama : ModuleRules
{
public UELlama(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
PublicIncludePaths.AddRange(
new string[] {
// ... add public include paths required here ...
}
);
PrivateIncludePaths.AddRange(
new string[] {
}
);
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
// ... add other public dependencies that you statically link with here ...
}
);
PrivateDependencyModuleNames.AddRange(
new string[]
{
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
// ... add private dependencies that you statically link with here ...
}
);
if (Target.bBuildEditor)
{
PrivateDependencyModuleNames.AddRange(
new string[]
{
"UnrealEd"
}
);
}
if (Target.Platform == UnrealTargetPlatform.Win64)
{
string PluginBinariesDir = Path.Combine(ModuleDirectory, "..", "..", "Binaries", "Win64");
string ProjectBinariesDir = Path.Combine(ModuleDirectory, "..", "..", "..", "..", "Binaries", "Win64");
string DLLFilePath = Path.Combine(ProjectBinariesDir, "llama.dll");
string DestinationDLLPath = Path.Combine(PluginBinariesDir, "llama.dll");
RuntimeDependencies.Add(DLLFilePath, DestinationDLLPath);
}
DynamicallyLoadedModuleNames.AddRange(
new string[]
{
// ... add any modules that your module loads dynamically here ...
}
);
if (Target.Platform == UnrealTargetPlatform.Linux)
{
PublicAdditionalLibraries.Add(Path.Combine(PluginDirectory, "Libraries", "libllama.so"));
PublicIncludePaths.Add(Path.Combine(PluginDirectory, "Includes"));
}
else if (Target.Platform == UnrealTargetPlatform.Win64)
{
PublicAdditionalLibraries.Add(Path.Combine(PluginDirectory, "Libraries", "llama.lib"));
PublicIncludePaths.Add(Path.Combine(PluginDirectory, "Includes"));
}
}
}
编译生成项目成功:
打开项目:
1.新建Blueprints文件夹,新建空白关卡LLAMA;游戏模式GM_LLAMA(不要创建错成游戏模式基础);玩家控制器PC_LLAMA,HUD类HUD_LLAMA,用户控件WBP_MainLLAMA。
2.项目设置中指定游戏默认地图为LLAMA,世界场景设置中指定游戏模式为GM_LLAMA,控制器为PC_LLAMA,HUD为HUD_LLAMA。
3.编写HUD蓝图和用户控件WBP_MainLLAMA:
(0.HUD蓝图与函数:
(1.添加llama组件;
(2.指定Prompt值;
A new line, the value “Human:”, and the value “AI:”.Our goal is to generate only a single line of text that corresponds to the current speaker.
(3.指定语言模型的路径;
F:\Projects\UE_Projects\5.1\UE5LLAMA\Content\Movies\Models\openchat_3.5.Q3_K_L.gguf
(4.指定Stop Sequences:
best_of;
The completion can’t change the speaker.
The completion won’t allow the speaker to speak twice in a row.
(5.编辑用户控件WBP_MainLLAMA:
添加函数Add Token:
事件图表:
User:
{prompt}
GPT4:
3.编译蓝图,运行测试对话成功,朗读答案成功:(打包后也能成功
后言:该项目实现了离线AI聊天功能,响应及时。但目前还有部分问题如:回答中文时部分文字呈现为?号,可能根据不同模型有不同的问题,可以自行测试该网站中的其他语言模型。
希望这篇文章能帮到你!!