【python】AI Navigator的使用及搭建本机大模型_anaconda ai navigator-CSDN博客
【Python】AI Navigator对话流式输出_python ai流式返回-CSDN博客
前两章节我们讲解了使用AI Navigator软件搭建本机大模型,并使用python对大模型api进行调用,使其流式返回数据。这一章节我们使用C#实现python同样的流式返回数据并流式输出。
结果
代码
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using System.Security.Cryptography;
namespace ConsoleApp49
{
internal class Program
{
public static string base_url = "http://localhost:8070";
static void Main(string[] args)
{
Run();
Console.ReadLine();
}
static async void Run()
{
string context = "你是一个友好的人工智能助手,旨在提供有用、简洁和准确的信息。";
//获取服务器状态
string getHealth = get_server_health();
JObject jsonHealth = JObject.Parse(getHealth);
string HealthStatus = jsonHealth["status"].ToString();
if (HealthStatus == "ok")
{
while (true)
{
Console.WriteLine("用户(输入对话或键入“exit”退出):");
string user_input = Console.ReadLine();
if (user_input.ToLower() == "exit")
{
return;
}
Console.WriteLine("AI助理:");
//人工智能回复信息
string assistant_response = await post_completion(context, user_input);
//组合我们之前的聊天内容
context = update_context(context, user_input, assistant_response);
}
}
else
{
Console.WriteLine("服务器尚未准备好接受请求。");
}
}
public static string update_context(string context, string user_input, string assistant_response)
{
return $"{context}\nUser: {user_input}\nAssistant: {assistant_response}";
}
public static string Get(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.Timeout = 3000;
if (req == null || req.GetResponse() == null)
return string.Empty;
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
if (resp == null)
return string.Empty;
using (Stream stream = resp.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
public static string get_server_health() {
string getResult = Get($"{base_url}/health");
return getResult;
}
static async Task<string> post_completion(string context, string user_input)
{
string allStr = "";
HttpClient httpClient = new HttpClient();
string url = "http://localhost:8070/completion";
string prompt = $"{context}\nUser: {user_input}\nAssistant:";
StringContent jsonContent = new StringContent(
JsonConvert.SerializeObject(new
{
prompt = prompt,
temperature = 0.8,
top_k = 35,
top_p = 0.95,
n_predict = 400,
stop = new string[] { "</s>", "Assistant:", "User:" },
stream = true
}), Encoding.UTF8,"application/json");
var httpreq = new HttpRequestMessage(HttpMethod.Post, url) { Content = jsonContent };
var response = await httpClient.SendAsync(httpreq, HttpCompletionOption.ResponseHeadersRead);
// 确保请求成功
response.EnsureSuccessStatusCode();
// 读取响应内容的异步流
using (var responseStream = await response.Content.ReadAsStreamAsync())
{
// 逐块读取并处理响应内容
var buffer = new byte[5000];
int bytesRead;
while ((bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
// 处理响应内容
string content = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead).Trim();
if (content != null)
{
JObject jsonObject = JObject.Parse(content.Replace("data:", ""));
if (jsonObject["content"].ToString().Trim() != "")
{
allStr = allStr + jsonObject["content"].ToString().Trim();
Console.Write(jsonObject["content"].ToString().Trim(), "");
}
}
}
}
Console.Write("\n");
return allStr;
}
}
}