.NET httpclient本地调用Ollama

1、搭建环境:先去ollama官网( https://ollama.com )下载安装,如图安装成功,

安装好后选择语言库(官方模型地址 https://ollama.com/search ),我选了Llama3.2和DeepSeek-R1:14B(安装和运行命令一样:ollama run Llama3.2,另一个同理),本地运行。以下用Llama3.2举例,deepseek同理。

2、新建winform项目,搭好界面

3、调用本地ollama接口(以下以api/generate为例)
接口详情:https://github.com/ollama/ollama/blob/main/docs/api.md
,本机地址: http://localhost:11434/ ,也可以绑定自己的ip地址(添加环境变量,OLLAMA_HOST 0.0.0.0即可用本机ip访问)。

后台代码:

private void btn_send_Click(object sender, EventArgs e)
{
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://localhost:11434/");
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");

string txtContent = this.textBox1.Text.Trim();
this.textBox1.Clear();
this.richTextBox1.AppendText("我:" + txtContent);

var requestBody = new
{
model = "Llama3.2",//(必填)选择模型 Llama3.2和DeepSeek-R1:14B 均可
prompt = txtContent,//(必填)生成文本所用提示词
//stream = true,
//options = new { temperature = 0.7,max_tokens=100}
};

var jsonContent = new StringContent(JsonSerializer.Serialize(requestBody), Encoding.UTF8, "application/json");

Task res = send(httpClient, jsonContent);

res.ContinueWith(t =>
{
if (richTextBox1.InvokeRequired)
{
richTextBox1.Invoke(new MethodInvoker(delegate
{
this.richTextBox1.AppendText("\n" + "Ollama:" + res.Result + "\n");
}));
}
else
{
richTextBox1.AppendText("\n" + "Ollama:" + res.Result + "\n");
}
});
}

private async Task send(HttpClient httpClient, StringContent requestBody)
{
var response = await httpClient.PostAsync("api/generate", requestBody);//chat/completions

if(response.StatusCode == HttpStatusCode.OK)
{
var stream = await response.Content.ReadAsStreamAsync();
var reader = new StreamReader(stream);

StringBuilder resContent = new StringBuilder();

// 循环读取响应流
while (!reader.EndOfStream)
{
var content = await reader.ReadLineAsync();
if (!string.IsNullOrEmpty(content))
{
var partialResponse = JsonSerializer.Deserialize(content);

// 输出响应内容
resContent.Append(partialResponse?.response);
}
}
return resContent.ToString();
}
return response.StatusCode.ToString();
}

public class OllamaResponse
{
///


/// 模型标识符
///

public string model { get; set; }

///


/// 创建时间戳
///

public string created_at { get; set; }

///


/// 响应内容
///

public string response { get; set; }

///


/// 是否完成
///

public bool done { get; set; }

///


/// 完成原因
///

public string done_reason { get; set; }

public int[] context { get; set; }
public long total_duration { get; set; }
public long load_duration { get; set; }
public int prompt_eval_count { get; set; }
public long prompt_eval_duration { get; set; }
public int eval_count { get; set; }
public long eval_duration { get; set; }
}

4、运行效果

posted @ 2025-03-19 11:10  jechsky1  阅读(395)  评论(0)    收藏  举报