前言:LLAMA是一種神經(jīng)網(wǎng)絡(luò)模型,全稱為L(zhǎng)anguage Model with an Average Attention Mechanism(具有平均注意機(jī)制的語(yǔ)言模型)。它是一種用于自然語(yǔ)言處理任務(wù)的模型,特別適用于生成文本和回答問(wèn)題。LLAMA模型結(jié)合了注意力機(jī)制和平均池化,以提高模型對(duì)輸入文本的理解和生成能力。它在多項(xiàng)基準(zhǔn)測(cè)試中取得了很好的性能,是一種強(qiáng)大的語(yǔ)言模型。
此文章以基于OpenAI聊天模型訓(xùn)練而來(lái)的openchat_3.5.Q3_K_L模型為例進(jìn)行實(shí)現(xiàn)。
1.準(zhǔn)備工作:(注意打不開(kāi)的鏈接需要科學(xué)上網(wǎng)
-
下載必備軟件:MicroSoft VS,CMAKE,Git(這一步就不詳寫(xiě),自行安裝
-
下載本例子的AI模型:openchat_3.5.Q3_K_L(放入項(xiàng)目目錄/Content/Movies/Models/..中
-
下載LLAMA插件:Llama-Unreal(我的教程后面修改了部分代碼,請(qǐng)支持插件原作者M(jìn)ikaPi
-
新建空白C++項(xiàng)目后關(guān)閉引擎,并打開(kāi)項(xiàng)目文件夾:
-
項(xiàng)目文件夾中創(chuàng)建Plugins文件夾并放入TTS和LLAMA插件:(TTS在上一篇文章有分享
-
進(jìn)入LLAMA插件文件夾,右鍵空白區(qū)域打開(kāi)Git:
-
mkdir llama cd llama
-
llama文件中放入下載解壓好的llama.cpp:llama.cpp兼容版本
-
創(chuàng)建build文件夾進(jìn)行cmake編譯:
cd llama.cpp
mkdir build
cd build/
cmake .. -DBUILD_SHARED_LIBS=ON
cd ..
cmake --build build --config Release -j --verbose
-
生成成功:
-
1.我們需要的文件是llama.dll:復(fù)制到Plugins\UELlama\Binaries\Win64文件夾中
-
2.llama插件的Includes和Libraries文件夾中已經(jīng)有了所有需要的文件,遂不需要復(fù)制。
2.項(xiàng)目各項(xiàng)設(shè)置及代碼:
-
修改UELlama.Build.cs:(修復(fù)打包后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"));
}
}
}
-
編譯生成項(xiàng)目成功:
-
打開(kāi)項(xiàng)目:
1.新建Blueprints文件夾,新建空白關(guān)卡LLAMA;游戲模式GM_LLAMA(不要?jiǎng)?chuàng)建錯(cuò)成游戲模式基礎(chǔ));玩家控制器PC_LLAMA,HUD類HUD_LLAMA,用戶控件WBP_MainLLAMA。
2.項(xiàng)目設(shè)置中指定游戲默認(rèn)地圖為L(zhǎng)LAMA,世界場(chǎng)景設(shè)置中指定游戲模式為GM_LLAMA,控制器為PC_LLAMA,HUD為HUD_LLAMA。
3.編寫(xiě)HUD藍(lán)圖和用戶控件WBP_MainLLAMA:
(0.HUD藍(lán)圖與函數(shù):
(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.指定語(yǔ)言模型的路徑;
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:
添加函數(shù)Add Token:
事件圖表:
User:
{prompt}
GPT4:
3.編譯藍(lán)圖,運(yùn)行測(cè)試對(duì)話成功,朗讀答案成功:(打包后也能成功
后言:該項(xiàng)目實(shí)現(xiàn)了離線AI聊天功能,響應(yīng)及時(shí)。但目前還有部分問(wèn)題如:回答中文時(shí)部分文字呈現(xiàn)為?號(hào),可能根據(jù)不同模型有不同的問(wèn)題,可以自行測(cè)試該網(wǎng)站中的其他語(yǔ)言模型。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-842120.html
希望這篇文章能幫到你?。?span toymoban-style="hidden">文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-842120.html
到了這里,關(guān)于【UE5】離線AI聊天-接入LLAMA語(yǔ)言模型 教程的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!