Skip to content

Commit 699615b

Browse files
authored
Merge pull request #149 from RogerBarreto/check-invalid-function-calling
Add exception for not supported streaming tools scenario
2 parents 5137316 + 7a88754 commit 699615b

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

src/OllamaApiClient.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Net.Http;
56
using System.Runtime.CompilerServices;
67
using System.Text;
@@ -182,6 +183,12 @@ public Task<EmbedResponse> EmbedAsync(EmbedRequest request, CancellationToken ca
182183
{
183184
if (string.IsNullOrEmpty(request.Model))
184185
request.Model = SelectedModel;
186+
187+
if (request.Stream && (request.Tools?.Any() ?? false))
188+
throw new NotSupportedException("""
189+
Currently, Ollama does not support function calls in streaming mode.
190+
See Ollama docs at https://github.com/ollama/ollama/blob/main/docs/api.md#parameters-1 to see whether support has since been added.
191+
""");
185192

186193
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, "api/chat")
187194
{

test/OllamaApiClientTests.cs

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO;
12
using System.Net;
23
using System.Text;
34
using System.Text.Json;
@@ -390,8 +391,8 @@ public async Task Receives_Response_Message_With_ToolsCalls()
390391
"eval_count": 28,
391392
"eval_duration": 4602334000
392393
}
393-
""".ReplaceLineEndings(""); // the JSON stream reader reads by line, so we need to make this one single line
394-
394+
""".ReplaceLineEndings(""); // the JSON stream reader reads by line, so we need to make this one single line
395+
395396
await using var stream = new MemoryStream();
396397

397398
await using var writer = new StreamWriter(stream, leaveOpen: true);
@@ -407,7 +408,8 @@ public async Task Receives_Response_Message_With_ToolsCalls()
407408

408409
var chat = new ChatRequest
409410
{
410-
Model = "llama3.1:latest",
411+
Model = "llama3.1:latest",
412+
Stream = false,
411413
Messages = [
412414
new(ChatRole.User, "How is the weather in LA?"),
413415
],
@@ -466,6 +468,66 @@ public async Task Receives_Response_Message_With_ToolsCalls()
466468

467469
toolsFunction.Arguments.ElementAt(2).Key.Should().Be("number");
468470
toolsFunction.Arguments.ElementAt(2).Value.ToString().Should().Be("42");
471+
}
472+
473+
[Test, NonParallelizable]
474+
public async Task Response_Streaming_Message_With_ToolsCalls_Throws_Not_Supported()
475+
{
476+
_response = new HttpResponseMessage
477+
{
478+
StatusCode = HttpStatusCode.OK,
479+
Content = new StringContent(string.Empty)
480+
};
481+
482+
var request = new ChatRequest
483+
{
484+
Model = "llama3.1:latest",
485+
Messages = [
486+
new(ChatRole.User, "How is the weather in LA?"),
487+
],
488+
Tools = [
489+
new Tool
490+
{
491+
Function = new Function
492+
{
493+
Description = "Get the current weather for a location",
494+
Name = "get_current_weather",
495+
Parameters = new Parameters
496+
{
497+
Properties = new Dictionary<string, Properties>
498+
{
499+
["location"] = new()
500+
{
501+
Type = "string",
502+
Description = "The location to get the weather for, e.g. San Francisco, CA"
503+
},
504+
["format"] = new()
505+
{
506+
Type = "string",
507+
Description = "The format to return the weather in, e.g. 'celsius' or 'fahrenheit'",
508+
Enum = ["celsius", "fahrenheit"]
509+
},
510+
["number"] = new()
511+
{
512+
Type = "integer",
513+
Description = "The number of the day to get the weather for, e.g. 42"
514+
}
515+
},
516+
Required = ["location", "format"],
517+
}
518+
},
519+
Type = "function"
520+
}
521+
]
522+
};
523+
524+
var act = async () =>
525+
{
526+
var enumerator = _client.ChatAsync(request, CancellationToken.None).GetAsyncEnumerator();
527+
await enumerator.MoveNextAsync();
528+
};
529+
530+
await act.Should().ThrowAsync<NotSupportedException>();
469531
}
470532
}
471533

@@ -528,7 +590,7 @@ public async Task Throws_Known_Exception_For_Models_That_Dont_Support_Tools()
528590
Content = new StringContent("{ error: llama2 does not support tools }")
529591
};
530592

531-
var act = () => _client.ChatAsync(new ChatRequest(), CancellationToken.None).StreamToEndAsync();
593+
var act = () => _client.ChatAsync(new ChatRequest() { Stream = false }, CancellationToken.None).StreamToEndAsync();
532594
await act.Should().ThrowAsync<ModelDoesNotSupportToolsException>();
533595
}
534596

0 commit comments

Comments
 (0)