qidao123.com技术社区-IT企服评测·应用市场
标题:
【UE5】离线AI聊天-接入LLAMA语言模型 教程
[打印本页]
作者:
用户云卷云舒
时间:
2024-8-25 21:07
标题:
【UE5】离线AI聊天-接入LLAMA语言模型 教程
前言: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聊天功能,响应及时。但现在还有部门题目如:回答中文时部门文字呈现为?号,大概根据不同模型有不同的题目,可以自行测试该网站中的其他语言模型。
希望这篇文章能帮到你!!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/)
Powered by Discuz! X3.4