-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLMStudioClient.cs
More file actions
162 lines (135 loc) · 4.65 KB
/
LMStudioClient.cs
File metadata and controls
162 lines (135 loc) · 4.65 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using UnityEngine;
using UnityEngine.Networking;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// By default, this script will attempt to send a test prompt to the LM Studio on Start().
/// It will log the processed response in the Unity console.
/// Spin up the LM Studio server before running the game.
/// </summary>
public class LMStudioClient : MonoBehaviour
{
// Replace with your LM Studio endpoint
[SerializeField]
private string url = "http://localhost:1234/v1/chat/completions";
[SerializeField]
private string model = "";
[SerializeField]
private int maxTokens = 128;
[SerializeField]
[TextArea(15, 20)]
private string systemPrompt = "Always answer in rhymes.";
[SerializeField]
[TextArea(15, 20)]
private string userPrompt = "Introduce yourself.";
[SerializeField]
private bool runOnStart = true;
/// <summary>
/// Resets every time SendRequest is called.
/// </summary>
private StringBuilder output = new StringBuilder("");
void Start()
{
if (runOnStart)
StartCoroutine(SendRequest(() => HandleOutput()));
}
/// <summary>
/// Replace with your own output handling.
/// </summary>
void HandleOutput()
{
Debug.Log(output.ToString());
}
/// <summary>
/// Sends a request to the LM and invokes a callback if successful.
/// Currently processes all received data at the same time.
/// </summary>
/// <returns></returns>
IEnumerator SendRequest(Action onComplete)
{
if (string.IsNullOrWhiteSpace (url)) {
Debug.LogError("Please input a URL into the inspector; i.e.: \"http://localhost:1234/v1/chat/completions\"");
yield break;
}
output.Clear();
string jsonData = $@"
{{
""model"": ""{model}"",
""messages"": [
{{ ""role"": ""system"", ""content"": ""{systemPrompt}"" }},
{{ ""role"": ""user"", ""content"": ""{userPrompt}"" }}
],
""temperature"": 0.7,
""max_tokens"": {maxTokens},
""stream"": true
}}";
UnityWebRequest request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
Debug.Log("Sending request...");
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
string rawResponse = request.downloadHandler.text;
// Process each `data:` line
string[] dataLines = rawResponse.Split(new[] { "data: " }, System.StringSplitOptions.RemoveEmptyEntries);
foreach (string dataLine in dataLines)
{
try
{
// Trim and ignore the `data: [DONE]` case (end-of-output)
string trimmedLine = dataLine.Trim();
if (string.IsNullOrEmpty(trimmedLine) || trimmedLine == "[DONE]") continue;
// Parse the JSON object
var parsedJson = JsonUtility.FromJson<StreamedChunk>(trimmedLine);
if (parsedJson != null && parsedJson.choices != null)
{
foreach (var choice in parsedJson.choices)
{
if (choice.delta != null && !string.IsNullOrEmpty(choice.delta.content))
{
output.Append(choice.delta.content);
}
}
}
}
catch (System.Exception ex)
{
Debug.LogError("Failed to parse line: " + dataLine + " Error: " + ex.Message);
}
}
onComplete.Invoke();
}
else
{
Debug.LogError($"Error: {request.responseCode} - {request.error}");
}
}
}
// Classes to match the streamed JSON structure
[System.Serializable]
public class Delta
{
public string role;
public string content;
}
[System.Serializable]
public class Choice
{
public int index;
public Delta delta;
public string finish_reason;
}
[System.Serializable]
public class StreamedChunk
{
public string id;
public string @object;
public long created;
public string model;
public List<Choice> choices;
}