Skip to content

Commit

Permalink
Merge pull request #31 from undreamai/feature/autoformatting_precommit
Browse files Browse the repository at this point in the history
Setup auto-formatting precommit
  • Loading branch information
amakropoulos authored Jan 16, 2024
2 parents f068ad4 + 3cfc810 commit bc72492
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
Samples
Samples.meta
obj/
6 changes: 3 additions & 3 deletions Editor/LLMClientEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace LLMUnity
public class LLMClientEditor : Editor
{
protected int buttonWidth = 150;
Type[] orderedTypes = new Type[] {typeof(LLM), typeof(LLMClient)};
Type[] orderedTypes = new Type[] { typeof(LLM), typeof(LLMClient) };

public void AddScript(SerializedObject llmScriptSO)
{
Expand All @@ -26,7 +26,7 @@ public void AddAdvancedOptionsToggle(SerializedObject llmScriptSO)
string toggleText = (advancedOptionsProp.boolValue ? "Hide" : "Show") + " Advanced Options";
GUIStyle style = new GUIStyle("Button");
if (advancedOptionsProp.boolValue)
style.normal = new GUIStyleState(){ background = Texture2D.grayTexture };
style.normal = new GUIStyleState() { background = Texture2D.grayTexture };
if (GUILayout.Button(toggleText, style, GUILayout.Width(buttonWidth)))
{
advancedOptionsProp.boolValue = !advancedOptionsProp.boolValue;
Expand Down Expand Up @@ -74,7 +74,7 @@ public List<SerializedProperty> GetPropertiesOfClass(SerializedObject so, Type[]
{
// display a property if it belongs to a certain class and/or has a specific attribute class
List<SerializedProperty> properties = new List<SerializedProperty>();
List<Type> attributeClasses = new List<Type> {attributeClass};
List<Type> attributeClasses = new List<Type> { attributeClass };
if (so.FindProperty("advancedOptions").boolValue)
attributeClasses.Add(attributeAdvancedClass);
foreach (Type attrClass in attributeClasses)
Expand Down
12 changes: 12 additions & 0 deletions LLMUnity.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="**/*.cs" />
</ItemGroup>

</Project>
8 changes: 4 additions & 4 deletions Runtime/LLM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static string GetAssetPath(string relPath = "")
return Path.Combine(Application.streamingAssetsPath, relPath);
}

#if UNITY_EDITOR
#if UNITY_EDITOR
[InitializeOnLoadMethod]
private static async void InitializeOnLoad()
{
Expand Down Expand Up @@ -104,7 +104,7 @@ public async void SetLora(string path)
modelCopyProgress = 1;
}

#endif
#endif

new public void OnEnable()
{
Expand Down Expand Up @@ -161,7 +161,7 @@ private void CheckIfListening(string message)
serverListening = true;
}
}
catch {}
catch { }
}

private void StartLLMServer()
Expand Down Expand Up @@ -194,7 +194,7 @@ private void StartLLMServer()
if (numGPULayers <= 0)
{
// prevent nvcc building if not using GPU
environment = new List<(string, string)> {("PATH", ""), ("CUDA_PATH", "")};
environment = new List<(string, string)> { ("PATH", ""), ("CUDA_PATH", "") };
}
}
Debug.Log($"Server command: {binary} {arguments}");
Expand Down
22 changes: 11 additions & 11 deletions Runtime/LLMClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

namespace LLMUnity
{
public class ClientAttribute : PropertyAttribute {}
public class ServerAttribute : PropertyAttribute {}
public class ModelAttribute : PropertyAttribute {}
public class ChatAttribute : PropertyAttribute {}
public class ClientAdvancedAttribute : PropertyAttribute {}
public class ServerAdvancedAttribute : PropertyAttribute {}
public class ModelAdvancedAttribute : PropertyAttribute {}
public class ClientAttribute : PropertyAttribute { }
public class ServerAttribute : PropertyAttribute { }
public class ModelAttribute : PropertyAttribute { }
public class ChatAttribute : PropertyAttribute { }
public class ClientAdvancedAttribute : PropertyAttribute { }
public class ServerAdvancedAttribute : PropertyAttribute { }
public class ModelAdvancedAttribute : PropertyAttribute { }

[DefaultExecutionOrder(-1)]
public class LLMClient : MonoBehaviour
Expand Down Expand Up @@ -42,9 +42,9 @@ public class LLMClient : MonoBehaviour
public LLMClient()
{
// initialise headers and chat lists
requestHeaders = new List<(string, string)> {("Content-Type", "application/json")};
requestHeaders = new List<(string, string)> { ("Content-Type", "application/json") };
chat = new List<ChatMessage>();
chat.Add(new ChatMessage {role = "system", content = prompt});
chat.Add(new ChatMessage { role = "system", content = prompt });
}

public async void OnEnable()
Expand Down Expand Up @@ -89,14 +89,14 @@ public ChatRequest GenerateRequest(string message, bool openAIFormat = false)
{
chatRequest.seed = seed;
}
chatRequest.stop = new List<string> {RoleString(playerName), playerName + ":"};
chatRequest.stop = new List<string> { RoleString(playerName), playerName + ":" };
return chatRequest;
}

private void AddMessage(string role, string content)
{
// add the question / answer to the chat list, update prompt
chat.Add(new ChatMessage {role = role, content = content});
chat.Add(new ChatMessage { role = role, content = content });
currentPrompt += RoleMessageString(role, content);
}

Expand Down
4 changes: 2 additions & 2 deletions Runtime/LLMUnitySetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static string RunProcess(string command, string commandArgs = "", Callbac
return output;
}

#if UNITY_EDITOR
#if UNITY_EDITOR
public static async Task DownloadFile(
string fileUrl, string savePath, bool executable = false,
Callback<string> callback = null, Callback<float> progresscallback = null,
Expand Down Expand Up @@ -153,6 +153,6 @@ await Task.Run(() =>
return fullPath.Substring(basePath.Length + 1);
}

#endif
#endif
}
}
2 changes: 1 addition & 1 deletion Samples~/ChatBot/Bubble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Bubble(Transform parent, BubbleUI ui, string name, string message)
{
bubbleUI = ui;
bubbleObject = CreateTextObject(parent, name, message, bubbleUI.bubbleWidth == -1, bubbleUI.bubbleHeight == -1);
imageObject = CreateImageObject(bubbleObject.transform, "Image");
imageObject = CreateImageObject(bubbleObject.transform, "Image");
SetBubblePosition(bubbleObject.GetComponent<RectTransform>(), imageObject.GetComponent<RectTransform>(), bubbleUI);
SetSortingOrder(bubbleObject, imageObject);
}
Expand Down
2 changes: 1 addition & 1 deletion Samples~/ChatBot/ChatBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ChatBot : MonoBehaviour

void Start()
{
if (font == null) font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
if (font == null) font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
playerUI = new BubbleUI
{
sprite = sprite,
Expand Down
13 changes: 13 additions & 0 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
LC_ALL=C
# Select files to format
FILES=$(git diff --cached --name-only --diff-filter=ACM "*.cs" | sed 's| |\\ |g')
[ -z "$FILES" ] && exit 0

# Format all selected files
echo "$FILES" | cat | xargs | sed -e 's/ /,/g' | xargs dotnet format -v q --include

# Add back the modified files to staging
echo "$FILES" | xargs git add

exit 0
1 change: 1 addition & 0 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
git config core.hooksPath hooks

0 comments on commit bc72492

Please sign in to comment.