Skip to content

Commit f08af8f

Browse files
Add custom Ollama URI and fix json saving
- Add support for custom Ollama URI -Fix json saving path to Resources folder
1 parent 6a4eeaf commit f08af8f

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

unity/Editor/BaseAgentController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ internal sealed class BaseAgentController : MonoBehaviour, IAgent
5757
/// </summary>
5858
private string _output;
5959

60+
// TTS
6061
[Header("TTS")]
6162
[SerializeField] private AudioSource _responseAudioSource;
6263

@@ -65,12 +66,12 @@ internal sealed class BaseAgentController : MonoBehaviour, IAgent
6566
/// Used to prevent unnecessary API requests when this script is disabled
6667
/// </summary>
6768
private bool _stopRequesting;
69+
private string _ollamaURI;
6870
private List<ChatMessage> _chatHistory = new();
6971
private IChatClient _chatClient;
7072

7173
private void Awake()
7274
{
73-
// At the moment, it's only used for proper logging. In future updates, it might become much more useful.
7475
string json;
7576
try
7677
{
@@ -83,7 +84,7 @@ private void Awake()
8384
}
8485
var data = JsonUtility.FromJson<JsonData>(json);
8586
LogUtils.logLevel = data.logLevel;
86-
87+
_ollamaURI = data.ollamaURI;
8788

8889
SafeExecutionUtils.SafeExecute("InitOllama", InitOllama, agentSettings.systemPrompt, agentSettings.modelName);
8990

@@ -100,7 +101,7 @@ private void InitOllama(string systemPrompt, string modelName)
100101
var builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings
101102
{ DisableDefaults = true });
102103

103-
builder.Services.AddChatClient(new OllamaChatClient("http://localhost:11434", modelName));
104+
builder.Services.AddChatClient(new OllamaChatClient(_ollamaURI, modelName));
104105

105106
_chatClient = builder.Build().Services.GetRequiredService<IChatClient>();
106107
_chatHistory.Add(new(ChatRole.System, systemPrompt));
@@ -183,7 +184,6 @@ private void OnButtonPressed()
183184
/// <summary>
184185
/// Once the mic stops, send the transcribed text to Ollama
185186
/// </summary>
186-
/// <param name="recordedAudio"></param>
187187
private async void OnRecordStop(AudioChunk recordedAudio)
188188
{
189189
var res = await _whisper.GetTextAsync(recordedAudio.Data, recordedAudio.Frequency, recordedAudio.Channels);

unity/Editor/CreateSettings.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ namespace UnityNeuroSpeech.Editor
1111
{
1212
internal sealed class CreateSettings : EditorWindow
1313
{
14-
private int _selectedLogIndex;
15-
private string[] _logOptions = new[] { "None", "Error", "All" };
16-
1714
// I really wouldn't want someone to do this, but I personally like to throw all tools and assets into Assets/Imports.
1815
// In this case, path logic breaks, which obviously shouldn't happen.
1916
private bool _isFrameworkInAnotherFolder;
2017
private string _anotherFolderName;
2118

22-
private ReorderableList _emotionsReorderableList;
2319

20+
private int _selectedLogIndex;
21+
private string[] _logOptions = new[] { "None", "Error", "All" };
22+
23+
private string _customOllamaURI;
24+
25+
private ReorderableList _emotionsReorderableList;
2426
private List<string> _emotions = new();
2527

2628
[MenuItem("UnityNeuroSpeech/Create Settings")]
@@ -68,6 +70,10 @@ private void OnGUI()
6870
_anotherFolderName = EditorGUILayout.TextField(new GUIContent("Directory name", "For example, if you throw this framework in Assets\\MyImports\\Frameworks, then write \"MyImports/Frameworks\""), _anotherFolderName);
6971
}
7072

73+
EditorGUILayout.LabelField("Advanced", EditorStyles.boldLabel);
74+
75+
_customOllamaURI = EditorGUILayout.TextField(new GUIContent("Custom Ollama URI", "If empty, Ollama URI will be default \"localhost:11434\""), _customOllamaURI);
76+
7177
if (GUILayout.Button("Save"))
7278
{
7379
LogUtils.logLevel = _logOptions[_selectedLogIndex] switch
@@ -133,13 +139,9 @@ private void OnGUI()
133139
File.WriteAllText(createAgentScriptPath, createAgentScriptContent);
134140

135141
// Save the settings into a JSON file.
136-
string finalSettingsPath;
137-
if (_isFrameworkInAnotherFolder) finalSettingsPath = $"Assets/{_anotherFolderName}/UnityNeuroSpeech/Resources/Settings/UnityNeuroSpeechSettings.json";
138-
else finalSettingsPath = $"Assets/UnityNeuroSpeech/Resources/Settings/UnityNeuroSpeechSettings.json";
139-
140-
var data = new JsonData(LogUtils.logLevel);
142+
var data = new JsonData(LogUtils.logLevel, string.IsNullOrEmpty(_customOllamaURI)? "http://localhost:11434" : _customOllamaURI);
141143
var json = JsonUtility.ToJson(data, true);
142-
File.WriteAllText(finalSettingsPath, json);
144+
File.WriteAllText("Assets/Resources/Settings/UnityNeuroSpeechSettings.json", json);
143145

144146
AssetDatabase.SaveAssets();
145147
AssetDatabase.Refresh();

unity/Shared/JsonData.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
namespace UnityNeuroSpeech.Shared
44
{
55

6-
// Sure, right now it only stores one logLevel field, so it might seem overkill(YAGNI moment.)
7-
// But when more settings come, this structure will be very useful
86
[System.Serializable]
97
internal struct JsonData
108
{
119
public LogLevel logLevel;
12-
public JsonData(LogLevel logLevel) => this.logLevel = logLevel;
10+
public string ollamaURI;
11+
12+
public JsonData(LogLevel logLevel, string ollamaURI)
13+
{
14+
this.logLevel = logLevel;
15+
this.ollamaURI = ollamaURI;
16+
}
1317
}
1418
}

0 commit comments

Comments
 (0)