以下有三个参数
model:模型名称
prompt:询问内容
temperature:严谨与发散程度(0-2),数字越大越发散
[C#] 纯文本查看 复制代码 private void generate(string model, string prompt, double temperature)
{
using (RestClient client = new RestClient("http://localhost:11434"))
{
var request = new RestRequest("/api/generate", Method.Post);
request.AddHeader("Content-Type", "application/json");
var body = new
{
model,
prompt,
options = new
{
temperature
},
stream = true
};
request.AddStringBody(JsonConvert.SerializeObject(body), DataFormat.None);
var responseStream = client.DownloadStream(request);
if (responseStream != null)
{
using (var reader = new StreamReader(responseStream))
{
string line;
while ((line = reader.ReadLine()) != null)
{
JObject bite = JObject.Parse(line);
bool done = (bool)bite["done"];
if (!done)
{
textBox2.Invoke((MethodInvoker)delegate
{
var response = (string)bite["response"];
textBox2.AppendText(response.Replace("\n", "\r\n"));
});
}
else
{
long total_duration = (long)bite["total_duration"]; // 生成响应所花费的时间
long load_duration = (long)bite["load_duration"]; // 加载模型所花费的时间(纳秒)
long prompt_eval_count = (long)bite["prompt_eval_count"]; // 提示中的令牌数
long prompt_eval_duration = (long)bite["prompt_eval_duration"]; // 评估提示所花费的时间(纳秒)
long eval_count = (long)bite["eval_count"]; // 响应中的令牌数
long eval_duration = (long)bite["eval_duration"]; // 生成响应所花费的时间(纳秒)
label4.Invoke((MethodInvoker)delegate
{
label4.Text = $"生成响应所花费的时间:{total_duration / 1e9}s\r\n" +
$"加载模型所花费的时间:{load_duration / 1e9}s\r\n" +
$"提示中的令牌数:{prompt_eval_count}\r\n" +
$"响应中的令牌数:{eval_count}\r\n" +
$"评估提示所花费的时间:{prompt_eval_duration / 1e9}s\r\n" +
$"生成响应所花费的时间:{eval_duration / 1e9}s\r\n";
});
}
}
}
}
}
}
不知道model怎么填?
发送请求看看你部署了哪几个模型。
[C#] 纯文本查看 复制代码 using (RestClient client = new RestClient("http://localhost:11434"))
{
var request = new RestRequest("/api/tags", Method.Get);
var response = client.Execute(request);
if (response.IsSuccessful)
{
try
{
var resp = response.Content.UnescapeUnicode();
var json = JObject.Parse(resp);
foreach (JObject array in json["models"])
{
var pair = new KeyValuePair<string, string>((string)array["name"], (string)array["size"]);
comboBox_models.Invoke((MethodInvoker)delegate
{
comboBox_models.Items.Add(pair);
});
}
}
catch (JsonException ex)
{
Console.WriteLine(ex.Message);
}
}
}
|