Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to set a LLM base prompt for all LLMCharacter objects #192

Merged
merged 5 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions Editor/LLMCharacterEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ protected override Type[] GetPropertyTypes()
return new Type[] { typeof(LLMCharacter) };
}

public void AddClientSettings(SerializedObject llmScriptSO)
{
List<Type> attributeClasses = new List<Type>(){typeof(LocalRemoteAttribute)};
attributeClasses.Add(llmScriptSO.FindProperty("remote").boolValue ? typeof(RemoteAttribute) : typeof(LocalAttribute));
attributeClasses.Add(typeof(LLMAttribute));
if (llmScriptSO.FindProperty("advancedOptions").boolValue)
{
attributeClasses.Add(typeof(LLMAdvancedAttribute));
}
ShowPropertiesOfClass("Setup Settings", llmScriptSO, attributeClasses, true);
}

public void AddModelSettings(SerializedObject llmScriptSO, LLMCharacter llmCharacterScript)
{
EditorGUILayout.LabelField("Model Settings", EditorStyles.boldLabel);
Expand All @@ -51,11 +39,6 @@ public void AddModelSettings(SerializedObject llmScriptSO, LLMCharacter llmChara
}
}

public void AddChatSettings(SerializedObject llmScriptSO)
{
ShowPropertiesOfClass("Chat Settings", llmScriptSO, new List<Type> { typeof(ChatAttribute) }, true);
}

public override void OnInspectorGUI()
{
LLMCharacter llmScript = (LLMCharacter)target;
Expand All @@ -64,8 +47,9 @@ public override void OnInspectorGUI()
OnInspectorGUIStart(llmScriptSO);
AddOptionsToggles(llmScriptSO);

AddClientSettings(llmScriptSO);
AddSetupSettings(llmScriptSO);
AddChatSettings(llmScriptSO);
Space();
AddModelSettings(llmScriptSO, llmScript);

OnInspectorGUIEnd(llmScriptSO);
Expand Down
26 changes: 1 addition & 25 deletions Editor/LLMEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ protected override Type[] GetPropertyTypes()
return new Type[] { typeof(LLM) };
}

public void AddServerLoadersSettings(SerializedObject llmScriptSO, LLM llmScript)
{
EditorGUILayout.LabelField("Setup Settings", EditorStyles.boldLabel);
AddServerSettings(llmScriptSO);
}

public void AddModelLoadersSettings(SerializedObject llmScriptSO, LLM llmScript)
{
EditorGUILayout.LabelField("Model Settings", EditorStyles.boldLabel);
Expand Down Expand Up @@ -100,24 +94,6 @@ public void AddModelSettings(SerializedObject llmScriptSO)
Space();
}

public void AddServerSettings(SerializedObject llmScriptSO)
{
List<Type> attributeClasses = new List<Type>(){typeof(LocalRemoteAttribute)};
attributeClasses.Add(llmScriptSO.FindProperty("remote").boolValue ? typeof(RemoteAttribute) : typeof(LocalAttribute));
attributeClasses.Add(typeof(LLMAttribute));
if (llmScriptSO.FindProperty("advancedOptions").boolValue)
{
attributeClasses.Add(typeof(LLMAdvancedAttribute));
}
ShowPropertiesOfClass("", llmScriptSO, attributeClasses, true);
Space();
}

public void AddChatSettings(SerializedObject llmScriptSO)
{
ShowPropertiesOfClass("Chat Settings", llmScriptSO, new List<Type> { typeof(ChatAttribute) }, false);
}

void ShowProgress(float progress, string progressText)
{
if (progress != 1) EditorGUI.ProgressBar(EditorGUILayout.GetControlRect(), progress, progressText);
Expand All @@ -136,7 +112,7 @@ public override void OnInspectorGUI()

GUI.enabled = LLMUnitySetup.libraryProgress == 1 && llmScript.modelProgress == 1 && llmScript.modelCopyProgress == 1;
AddOptionsToggles(llmScriptSO);
AddServerLoadersSettings(llmScriptSO, llmScript);
AddSetupSettings(llmScriptSO);
AddModelLoadersSettings(llmScriptSO, llmScript);
GUI.enabled = true;
AddChatSettings(llmScriptSO);
Expand Down
22 changes: 22 additions & 0 deletions Editor/PropertyEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ public void AddOptionsToggle(SerializedObject llmScriptSO, string propertyName,
}
}

public void AddSetupSettings(SerializedObject llmScriptSO)
{
List<Type> attributeClasses = new List<Type>(){typeof(LocalRemoteAttribute)};
attributeClasses.Add(llmScriptSO.FindProperty("remote").boolValue ? typeof(RemoteAttribute) : typeof(LocalAttribute));
attributeClasses.Add(typeof(LLMAttribute));
if (llmScriptSO.FindProperty("advancedOptions").boolValue)
{
attributeClasses.Add(typeof(LLMAdvancedAttribute));
}
ShowPropertiesOfClass("Setup Settings", llmScriptSO, attributeClasses, true);
}

public void AddChatSettings(SerializedObject llmScriptSO)
{
List<Type> attributeClasses = new List<Type>(){typeof(ChatAttribute)};
if (llmScriptSO.FindProperty("advancedOptions").boolValue)
{
attributeClasses.Add(typeof(ChatAdvancedAttribute));
}
ShowPropertiesOfClass("Chat Settings", llmScriptSO, attributeClasses, false);
}

public void AddOptionsToggles(SerializedObject llmScriptSO)
{
LLMUnitySetup.SetDebugMode((LLMUnitySetup.DebugModeType)EditorGUILayout.EnumPopup("Log Level", LLMUnitySetup.DebugMode));
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ HuggingFace models can be converted to gguf with this [online converter](https:/
### LLM Settings

- `Show/Hide Advanced Options` Toggle to show/hide advanced options from below
- `Log Level` select how verbose the log messages are

#### 💻 Setup Settings

Expand Down Expand Up @@ -345,10 +346,17 @@ If the user's GPU is not supported, the LLM will fall back to the CPU

</details>

#### 🗨️ Chat Settings
- <details><summary>Advanced options</summary>

- `Base Prompt` a common base prompt to use across all LLMCharacter objects using the LLM

</details>

### LLMCharacter Settings

- `Show/Hide Advanced Options` Toggle to show/hide advanced options from below
- `Log Level` select how verbose the log messages are

#### 💻 Setup Settings
<div>
Expand Down
12 changes: 12 additions & 0 deletions Runtime/LLM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public class LLM : MonoBehaviour
[ModelAdvanced] public int contextSize = 0;
/// <summary> Batch size for prompt processing. </summary>
[ModelAdvanced] public int batchSize = 512;
/// <summary> a base prompt to use as a base for all LLMCharacter objects </summary>
[TextArea(5, 10), ChatAdvanced] public string basePrompt = "";

/// <summary> Boolean set to true if the server has started and is ready to receive requests, false otherwise. </summary>
public bool started { get; protected set; } = false;
/// <summary> Boolean set to true if the server has failed to start. </summary>
Expand Down Expand Up @@ -212,6 +215,7 @@ public async void Awake()
if (asynchronousStartup) await Task.Run(() => StartLLMServer());
else StartLLMServer();
if (dontDestroyOnLoad) DontDestroyOnLoad(transform.root.gameObject);
if (basePrompt != "") await SetBasePrompt(basePrompt);
}

private void SetupLogging()
Expand Down Expand Up @@ -429,6 +433,7 @@ public async Task<string> Slot(string json)
public async Task<string> Completion(string json, Callback<string> streamCallback = null)
{
AssertStarted();
if (streamCallback == null) streamCallback = (string s) => {};
StreamWrapper streamWrapper = ConstructStreamWrapper(streamCallback);
await Task.Run(() => llmlib.LLM_Completion(LLMObject, json, streamWrapper.GetStringWrapper()));
if (!started) return null;
Expand All @@ -439,6 +444,13 @@ public async Task<string> Completion(string json, Callback<string> streamCallbac
return result;
}

public async Task SetBasePrompt(string base_prompt)
{
AssertStarted();
SystemPromptRequest request = new SystemPromptRequest(){system_prompt = base_prompt, prompt = " ", n_predict = 0};
await Completion(JsonUtility.ToJson(request));
}

/// <summary>
/// Allows to cancel the requests in a specific slot of the LLM
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions Runtime/LLMInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public struct ChatRequest
public List<ChatMessage> messages;
}

[Serializable]
public struct SystemPromptRequest
{
public string prompt;
public string system_prompt;
public int n_predict;
}

[Serializable]
public struct ChatResult
{
Expand Down
1 change: 1 addition & 0 deletions Runtime/LLMUnitySetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class LocalAttribute : PropertyAttribute {}
public class ModelAttribute : PropertyAttribute {}
public class ModelAdvancedAttribute : PropertyAttribute {}
public class ChatAttribute : PropertyAttribute {}
public class ChatAdvancedAttribute : PropertyAttribute {}
public class LLMUnityAttribute : PropertyAttribute {}

public class NotImplementedException : Exception
Expand Down
Loading