1010from lmstudio .sdk_api import LMStudioOSError
1111from lmstudio .schemas import DictObject
1212from lmstudio .history import (
13+ AnyChatMessageDict ,
1314 AnyChatMessageInput ,
15+ AssistantMultiPartInput ,
1416 Chat ,
15- AnyChatMessageDict ,
1617 ChatHistoryData ,
1718 ChatHistoryDataDict ,
1819 LocalFileInput ,
2930 LlmPredictionStats ,
3031 PredictionResult ,
3132)
33+ from lmstudio ._sdk_models import (
34+ ToolCallRequestDataDict ,
35+ ToolCallResultDataDict ,
36+ )
3237
3338from .support import IMAGE_FILEPATH , check_sdk_error
3439
125130 "role" : "system" ,
126131 "content" : [{"type" : "text" , "text" : "Structured text system prompt" }],
127132 },
133+ {
134+ "role" : "assistant" ,
135+ "content" : [
136+ {"type" : "text" , "text" : "Example tool call request" },
137+ {
138+ "type" : "toolCallRequest" ,
139+ "toolCallRequest" : {
140+ "type" : "function" ,
141+ "id" : "114663647" ,
142+ "name" : "example_tool_name" ,
143+ "arguments" : {
144+ "n" : 58013 ,
145+ "t" : "value" ,
146+ },
147+ },
148+ },
149+ {
150+ "type" : "toolCallRequest" ,
151+ "toolCallRequest" : {
152+ "type" : "function" ,
153+ "id" : "114663648" ,
154+ "name" : "another_example_tool_name" ,
155+ "arguments" : {
156+ "n" : 23 ,
157+ "t" : "some other value" ,
158+ },
159+ },
160+ },
161+ ],
162+ },
163+ {
164+ "role" : "tool" ,
165+ "content" : [
166+ {
167+ "type" : "toolCallResult" ,
168+ "toolCallId" : "114663647" ,
169+ "content" : "example tool call result" ,
170+ },
171+ {
172+ "type" : "toolCallResult" ,
173+ "toolCallId" : "114663648" ,
174+ "content" : "another example tool call result" ,
175+ },
176+ ],
177+ },
128178]
129179
130180INPUT_HISTORY = {"messages" : INPUT_ENTRIES }
214264 "role" : "system" ,
215265 "content" : [{"type" : "text" , "text" : "Structured text system prompt" }],
216266 },
267+ {
268+ "role" : "assistant" ,
269+ "content" : [
270+ {"type" : "text" , "text" : "Example tool call request" },
271+ {
272+ "type" : "toolCallRequest" ,
273+ "toolCallRequest" : {
274+ "type" : "function" ,
275+ "id" : "114663647" ,
276+ "name" : "example_tool_name" ,
277+ "arguments" : {
278+ "n" : 58013 ,
279+ "t" : "value" ,
280+ },
281+ },
282+ },
283+ {
284+ "type" : "toolCallRequest" ,
285+ "toolCallRequest" : {
286+ "type" : "function" ,
287+ "id" : "114663648" ,
288+ "name" : "another_example_tool_name" ,
289+ "arguments" : {
290+ "n" : 23 ,
291+ "t" : "some other value" ,
292+ },
293+ },
294+ },
295+ ],
296+ },
297+ {
298+ "role" : "tool" ,
299+ "content" : [
300+ {
301+ "type" : "toolCallResult" ,
302+ "toolCallId" : "114663647" ,
303+ "content" : "example tool call result" ,
304+ },
305+ {
306+ "type" : "toolCallResult" ,
307+ "toolCallId" : "114663648" ,
308+ "content" : "another example tool call result" ,
309+ },
310+ ],
311+ },
217312]
218313
219314
@@ -271,6 +366,44 @@ def test_from_history_with_simple_text() -> None:
271366 "sizeBytes" : 100 ,
272367 "fileType" : "text/plain" ,
273368}
369+ INPUT_TOOL_REQUESTS : list [ToolCallRequestDataDict ] = [
370+ {
371+ "type" : "toolCallRequest" ,
372+ "toolCallRequest" : {
373+ "type" : "function" ,
374+ "id" : "114663647" ,
375+ "name" : "example_tool_name" ,
376+ "arguments" : {
377+ "n" : 58013 ,
378+ "t" : "value" ,
379+ },
380+ },
381+ },
382+ {
383+ "type" : "toolCallRequest" ,
384+ "toolCallRequest" : {
385+ "type" : "function" ,
386+ "id" : "114663648" ,
387+ "name" : "another_example_tool_name" ,
388+ "arguments" : {
389+ "n" : 23 ,
390+ "t" : "some other value" ,
391+ },
392+ },
393+ },
394+ ]
395+ INPUT_TOOL_RESULTS : list [ToolCallResultDataDict ] = [
396+ {
397+ "type" : "toolCallResult" ,
398+ "toolCallId" : "114663647" ,
399+ "content" : "example tool call result" ,
400+ },
401+ {
402+ "type" : "toolCallResult" ,
403+ "toolCallId" : "114663648" ,
404+ "content" : "another example tool call result" ,
405+ },
406+ ]
274407
275408
276409def test_get_history () -> None :
@@ -289,6 +422,8 @@ def test_get_history() -> None:
289422 chat .add_user_message ("Avoid consecutive responses" )
290423 chat .add_assistant_response (INPUT_FILE_HANDLE_DICT )
291424 chat .add_system_prompt (TextData (text = "Structured text system prompt" ))
425+ chat .add_assistant_response ("Example tool call request" , INPUT_TOOL_REQUESTS )
426+ chat .add_tool_results (INPUT_TOOL_RESULTS )
292427 assert chat ._get_history_for_prediction () == EXPECTED_HISTORY
293428
294429
@@ -307,6 +442,19 @@ def test_add_entry() -> None:
307442 chat .add_entry ("user" , "Avoid consecutive responses" )
308443 chat .add_entry ("assistant" , INPUT_FILE_HANDLE_DICT )
309444 chat .add_entry ("system" , TextData (text = "Structured text system prompt" ))
445+ tool_call_message_contents : AssistantMultiPartInput = [
446+ "Example tool call request" ,
447+ * INPUT_TOOL_REQUESTS ,
448+ ]
449+ chat .add_entry ("assistant" , tool_call_message_contents )
450+ chat .add_entry ("tool" , INPUT_TOOL_RESULTS )
451+ assert chat ._get_history_for_prediction () == EXPECTED_HISTORY
452+
453+
454+ def test_append () -> None :
455+ chat = Chat ()
456+ for message in INPUT_ENTRIES :
457+ chat .append (cast (AnyChatMessageDict , message ))
310458 assert chat ._get_history_for_prediction () == EXPECTED_HISTORY
311459
312460
0 commit comments