Ollama provider returns Bad request
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 1.1k
- Forks
- 141
- Avg merge
- 2d 18m
- Merged PRs (30d)
- 2
Description
Describe the bug
I tried using the v0.017.1dev28 and net 8.0 but it returns and error with bad request using latest version from ollama on windows 11
Steps to reproduce the bug
using this code example it returns 400 when sending this tool in the request ```
using LangChain.Providers.OpenAI.Predefined;
using LangChain.Providers.Ollama;
using System.Text.RegularExpressions;
using Ollama;
using System.IO;
using LlmAssist.Core.DataContextEmbeddeding;
using LlmAssist.Core.Tools;
using Microsoft.Extensions.Options;
using System.Reflection;
using Tool = Ollama.Tool;
using Message = Ollama.Message;
using LlmAssist.Core.ToolExtension;
namespace LlmAssist.Core.Services;
public class LlmAssistSvc
{
const string Sys_Message = "You are a helpful coding assistant, complete the work without comments, dont explain the code. dont write the thought process. strictly do what was prompted";
OllamaApiClient ollama { get; }
Dictionary<int, Ollama.Model> Models { get; }
GenerateEmbeddingResponse Embeddings { get; set; }
public string SelectedModel { get; set; }
public List Messages { get; set; } = new List();
bool ModelSelected => !string.IsNullOrEmpty(SelectedModel);
public async Task<List<Ollama.Model>> GetModels() => Models.Values.ToList();
public async Task GetEmbeddings() => Embeddings;
static HtmlDownloadTool htmlDownloadTool = new HtmlDownloadTool();
public async Task SetModel(int index)
{
if (!Models.ContainsKey(index))
throw new Exception("Index not found");
SelectedModel = Models[index].Model1;
}
public LlmAssistSvc(string uriOllama=null)
{
ollama= string.IsNullOrEmpty(uriOllama) ?
new OllamaApiClient()
:
new OllamaApiClient(baseUri: new Uri(uriOllama));
Models = ollama.Models.ListModelsAsync().Result!.Models.Select((x,idx) =>
{
return new {
Index =idx,
Model =x
};
}).ToDictionary(x=>x.Index, x=>x.Model)!;
}
public LlmAssistSvc(string uriOllama = null,int index=1)
{
ollama = string.IsNullOrEmpty(uriOllama) ?
new OllamaApiClient()
:
new OllamaApiClient(baseUri: new Uri(uriOllama));
Models = ollama.Models.ListModelsAsync().Result!.Models.Select((x, idx) =>
{
return new
{
Index = idx,
Model = x
};
}).ToDictionary(x => x.Index, x => x.Model)!;
ollama = GetClient(Models[index].Model1).Result;
SetModel(index).Wait();
}
async Task<OllamaApiClient> GetClient(string model)
{
// set OLLAMA_HOST=...:11434
var apiClient = new OllamaApiClient(
httpClient: new HttpClient
{
Timeout = TimeSpan.FromMinutes(10),
},
baseUri: new Uri("http://127.0.0.1:11434/api")); // baseUri: new Uri("http://10.10.5.85:11434/api")
if (!string.IsNullOrEmpty(model))
{
await apiClient.Models.PullModelAsync(model).EnsureSuccessAsync();
}
return apiClient;
}
public async Task GeneratedEmbeddings(string input)
{
Embeddings = await ollama.Embeddings.GenerateEmbeddingAsync(
model: SelectedModel,
prompt: "");
}
public async Task<QueryResponseContext> QueryChat(string userInput, bool includeThought = false, bool useStream = false, DataChatContext ctx = null, List<ExtendedTool> toolSets=null)
{
Messages.Add("You are a helpful coding assistant, complete the work without comments, dont explain the code. dont write the thought process. strictly do what was prompted".AsSystemMessage()) ;
Messages.Add(userInput.AsUserMessage());
// await ollama.Chat.GenerateChatCompletionAsync(SelectedModel, ctx?.Messages, tools: toolSets.Select(x=>x.Tool).ToList());
var service = new HtmlDownloadTool();
var tools = service.AsTools().AsOllamaTools();
var calls = service.AsCalls();
var lastResponse = ollama.Chat(
model: SelectedModel,
systemMessage:Sys_Message ,
autoCallTools: true);
lastResponse.AddToolService(service.AsTools().AsOllamaTools(), service.AsCalls());
var res=await lastResponse.SendAsync(userInput);
string response = res.Content;
response = includeThought ? response : Sanitize(ref response);
return new QueryResponseContext
{
Response = response
};
}
string Sanitize(ref string input)
{
input = Regex.Replace(input, "<think>.*?</think>", "", RegexOptions.Singleline);
return input.StartsWith("\n\n") ? input.Substring(2) : input;
}
public async Task PrintModels()
{
Console.WriteLine(string.Join(Environment.NewLine,
(await GetModels()).Select((x, idx) => $"{idx} {x.Model1}")
));
}
}the tool implementation [GenerateJsonSchema]
public interface IHTMLDownloadTool
{
[Description("Downloads or fetchs data in HTML")]
Task DownloadHtmlAsync(
[Description("Url string to request to fetch HTML from")] string uri,
global::System.Threading.CancellationToken cancellationToken = default);
}
public class HtmlDownloadTool: IHTMLDownloadTool
{
private readonly HttpClient _httpClient;
public HtmlDownloadTool()
{
_httpClient = new HttpClient();
}
public async Task<string> DownloadHtmlAsync(string uri ,global::System.Threading.CancellationToken cancellationToken = default)
{
if (cancellationToken != null && cancellationToken.IsCancellationRequested)
return "";
var response = await _httpClient.GetStringAsync(new Uri(uri));
return response;
}
}```
Expected behavior
returns the response without errors
Screenshots
No response
NuGet package version
No response
Additional context
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the Ollama provider calls shown in the report, especially ollama.Chat, AddToolService, and SendAsync, and reproduce the 400 response with the supplied tool. Inspect the generated request and compare it with the Ollama API's tool-calling requirements; done means the same tool request returns a response without Bad Request.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, ollama
- Domain
- ai, api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100