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

前言:LLAMA是一种神经网络模型,全称为Language Model with an Average Attention Mechanism(具有平均注意机制的语言模型)。它是一种用于自然语言处理任务的模型,特别适用于生成文本和回答问题。LLAMA模型结合了注意力机制和平均池化,以提高模型对输入文本的理解和生成能力。它在多项基准测试中取得了很好的性能,是一种强大的语言模型。
此文章以基于OpenAI聊天模型训练而来的openchat_3.5.Q3_K_L模型为例进行实现。

1.准备工作:(注意打不开的链接需要科学上网

  • 下载必备软件:MicroSoft VSCMAKEGit(这一步就不详写,自行安装
  • 下载本例子的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_LLAMAHUD类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聊天功能,响应及时。但目前还有部分问题如:回答中文时部分文字呈现为?号,可能根据不同模型有不同的问题,可以自行测试该网站中的其他语言模型。

希望这篇文章能帮到你!!

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值