-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLlamaExample.cs
72 lines (63 loc) · 2.84 KB
/
LlamaExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.IO;
using System.Threading;
using UnityEngine;
namespace Abuksigun.LlamaCpp
{
[ExecuteInEditMode]
public class LlamaExample : MonoBehaviour
{
CancellationTokenSource cts;
LlamaModel model;
// Download model here: https://huggingface.co/TheBloke/speechless-mistral-dolphin-orca-platypus-samantha-7B-GGUF/blob/main/speechless-mistral-dolphin-orca-platypus-samantha-7b.Q4_K_M.gguf
[SerializeField] string modelPath = "StreamingAssets/Models/speechless-mistral-dolphin-orca-platypus-samantha-7b.Q4_K_M.gguf";
[SerializeField, TextArea(10, 10)] string systemPrompt = "You are an AI game character";
[SerializeField, TextArea(10, 10)] string userPrompt = "You are in a Tavern\nHP:40%\nWhat is your next action:";
[SerializeField, TextArea(10, 10)] string assistantPrompt = "I will";
[SerializeField] int gpuLayers = 0;
[SerializeField, Range(0, 1.5f)] float temperature = 0.8f;
[ContextMenu("Run")]
public async void RunAsync()
{
const string promptFormat = "<|im_start|>system\n{{system}}\n<|im_end|>\n<|im_start|>user\n{{user}}\n<|im_end|>\n<|im_start|>assistant\n{{assistant}}";
const string customEos = "<|im_end|>";
string fullModelPath = Path.Join(Application.streamingAssetsPath, modelPath);
model ??= await LlamaModel.LoadModel(fullModelPath, new Progress<float>(x => Debug.Log($"Progress {x}")), gpuLayers: gpuLayers);
Debug.Log($"Model context size: {model.ContextSize} tokens.");
cts = new CancellationTokenSource();
void Progress(string currentString)
{
if (currentString.EndsWith(customEos))
{
cts.Cancel();
cts = null;
}
Debug.Log(currentString);
}
string fullPrompt = FormatPrompt(promptFormat, systemPrompt, userPrompt, assistantPrompt);
Debug.Log(fullPrompt);
var parameters = new LlamaModel.SamplingParams { Temp = temperature };
string result = await model.RunAsync(fullPrompt, 100, parameters, new Progress<string>(Progress), cts.Token);
Debug.Log($"Result: {result}");
}
[ContextMenu("Stop")]
public void Stop()
{
cts?.Cancel();
}
[ContextMenu("Reset Model")]
public void ResetModel()
{
cts?.Cancel();
model?.Dispose();
model = null;
}
public static string FormatPrompt(string promptFormat, string system, string user, string assistant = "")
{
return promptFormat
.Replace("{{system}}", system)
.Replace("{{user}}", user)
.Replace("{{assistant}}", assistant);
}
}
}