【UE5】离线AI聊天-接入LLAMA语言模型 教程

打印 上一主题 下一主题

主题 1737|帖子 1737|积分 5211

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x


前言: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:

    1. mkdir llama
    2. cd llama
    复制代码
  • llama文件中放入下载解压好的llama.cpp:llama.cpp兼容版本

  • 创建build文件夹进行cmake编译:
  1. cd llama.cpp
  2. mkdir build
  3. cd build/
  4. cmake .. -DBUILD_SHARED_LIBS=ON
  5. cd ..
  6. cmake --build build --config Release -j --verbose
复制代码


  • 天生成功:

  • 1.我们需要的文件是llama.dll:复制到Plugins\UELlama\Binaries\Win64文件夹中




  • 2.llama插件的Includes和Libraries文件夹中已经有了全部需要的文件,遂不需要复制。

2.项目各项设置及代码:



  • 修改UELlama.Build.cs:(修复打包后dll缺失
  1. using UnrealBuildTool;
  2. using System.IO;
  3. public class UELlama : ModuleRules
  4. {
  5.         public UELlama(ReadOnlyTargetRules Target) : base(Target)
  6.         {
  7.                 PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
  8.                 PublicIncludePaths.AddRange(
  9.                         new string[] {
  10.                                 // ... add public include paths required here ...
  11.                         }
  12.                         );
  13.                 PrivateIncludePaths.AddRange(
  14.                         new string[] {
  15.                         }
  16.                         );
  17.                 PublicDependencyModuleNames.AddRange(
  18.                         new string[]
  19.                         {
  20.                                 "Core",
  21.                                 // ... add other public dependencies that you statically link with here ...
  22.                         }
  23.                         );
  24.                 PrivateDependencyModuleNames.AddRange(
  25.                         new string[]
  26.                         {
  27.                                 "CoreUObject",
  28.                                 "Engine",
  29.                                 "Slate",
  30.                                 "SlateCore",
  31.                                 // ... add private dependencies that you statically link with here ...
  32.                         }
  33.                         );
  34.                 if (Target.bBuildEditor)
  35.                 {
  36.                         PrivateDependencyModuleNames.AddRange(
  37.                                 new string[]
  38.                                 {
  39.                                         "UnrealEd"
  40.                                 }
  41.                         );
  42.                 }
  43.                 if (Target.Platform == UnrealTargetPlatform.Win64)
  44.                 {
  45.                         string PluginBinariesDir = Path.Combine(ModuleDirectory, "..", "..", "Binaries", "Win64");
  46.                         string ProjectBinariesDir = Path.Combine(ModuleDirectory, "..", "..", "..", "..", "Binaries", "Win64");
  47.                         string DLLFilePath = Path.Combine(ProjectBinariesDir, "llama.dll");
  48.                         string DestinationDLLPath = Path.Combine(PluginBinariesDir, "llama.dll");
  49.                         RuntimeDependencies.Add(DLLFilePath, DestinationDLLPath);
  50.                 }
  51.                 DynamicallyLoadedModuleNames.AddRange(
  52.                         new string[]
  53.                         {
  54.                                 // ... add any modules that your module loads dynamically here ...
  55.                         }
  56.                         );
  57.                 if (Target.Platform == UnrealTargetPlatform.Linux)
  58.                 {
  59.                         PublicAdditionalLibraries.Add(Path.Combine(PluginDirectory, "Libraries", "libllama.so"));
  60.                         PublicIncludePaths.Add(Path.Combine(PluginDirectory, "Includes"));
  61.                 }
  62.                 else if (Target.Platform == UnrealTargetPlatform.Win64)
  63.                 {
  64.                         PublicAdditionalLibraries.Add(Path.Combine(PluginDirectory, "Libraries", "llama.lib"));
  65.                         PublicIncludePaths.Add(Path.Combine(PluginDirectory, "Includes"));
  66.                 }
  67.         }
  68. }
复制代码


  • 编译天生项目成功:

  • 打开项目:
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值;

  1. 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.指定语言模型的路径;

  1. F:\Projects\UE_Projects\5.1\UE5LLAMA\Content\Movies\Models\openchat_3.5.Q3_K_L.gguf
复制代码
(4.指定Stop Sequences:

  1. best_of;
  2. The completion can’t change the speaker.
  3. The completion won’t allow the speaker to speak twice in a row.
复制代码

(5.编辑用户控件WBP_MainLLAMA:


添加函数Add Token:

变乱图表:

  1. User:
  2. {prompt}
  3. GPT4:
复制代码

3.编译蓝图,运行测试对话成功,朗读答案成功:(打包后也能成功



后言:该项目实现了离线AI聊天功能,响应及时。但现在还有部门题目如:回答中文时部门文字呈现为?号,大概根据不同模型有不同的题目,可以自行测试该网站中的其他语言模型。
希望这篇文章能帮到你!!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

用户云卷云舒

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表