![]() |
VOOZH | about |
dotnet add package NewLife.AI --version 1.3.2026.608
NuGet\Install-Package NewLife.AI -Version 1.3.2026.608
<PackageReference Include="NewLife.AI" Version="1.3.2026.608" />
<PackageVersion Include="NewLife.AI" Version="1.3.2026.608" />Directory.Packages.props
<PackageReference Include="NewLife.AI" />Project file
paket add NewLife.AI --version 1.3.2026.608
#r "nuget: NewLife.AI, 1.3.2026.608"
#:package NewLife.AI@1.3.2026.608
#addin nuget:?package=NewLife.AI&version=1.3.2026.608Install as a Cake Addin
#tool nuget:?package=NewLife.AI&version=1.3.2026.608Install as a Cake Tool
<p align="center"> <a href="https://www.nuget.org/packages/NewLife.AI"><img src="https://img.shields.io/nuget/v/NewLife.AI.svg" alt="NuGet"></a> <a href="https://www.nuget.org/packages/NewLife.AI"><img src="https://img.shields.io/nuget/dt/NewLife.AI.svg" alt="Downloads"></a> <img src="https://img.shields.io/badge/.NET-netstandard2.1%20%7C%20net8.0%20%7C%20net10.0-blue" alt=".NET"> <a href="https://github.com/NewLifeX/NewLife.AI/blob/main/LICENSE"><img src="https://img.shields.io/github/license/NewLifeX/NewLife.AI.svg" alt="License"></a> </p>
NewLife.AI 是面向 .NET 生态的开源 AI 基础库,通过统一的 IChatClient 接口封装 46 个主流大模型服务商,内置函数调用、MCP 协议、流式输出、多模态、多智能体等能力,可作为 NuGet 包嵌入任意 .NET 项目(net45 / netstandard2.1)。
NewLife.ChatAI 是构建于 NewLife.AI 之上的完整 Web 对话应用(ASP.NET Core),提供即开即用的多模型对话前端、统一 AI 网关与自动记忆进化,既可独立部署,也可通过 NuGet 嵌入已有 ASP.NET Core 项目。
IChatClient 接口:对齐 MEAI 规范,单轮、流式、函数调用、多模态全部统一 API[ToolDescription] 特性自动生成 JSON Schema,ToolChatClient 多轮循环,内置搜索 / 天气 / 翻译 / 网页抓取 / IP 定位等工具IChatHandler 三段式处理链(OnBefore → Execute → OnAfter),内置 Handler(技能激活 / 记忆注入 / 持久化 / 用量统计 / 标题生成),可插拔 IChatFilter@ 递归引用,触发词自动激活ConversableAgent / GroupChat / ParallelGroupChat / FunctionCallingPlanner46 家服务商,9 个独立协议客户端 + 37 个 OpenAI 兼容适配。
| 服务商 | 协议 | 特性 |
|---|---|---|
| OpenAI | ChatCompletions / Responses | 视觉 / 函数调用 / 图像生成 / o3 推理 |
| DeepSeek | DeepSeek API | reasoning_content / DeepSeek v4 专属参数 |
| Anthropic | Messages | Claude 3.5 / Claude 4 |
| Gemini | Gemini 1.5 / 2.0 / 2.5 | |
| 阿里云 | DashScope | qwen-plus / qwen-max / Omni 全模态 / Files API |
| Azure AI | Azure OpenAI | 部署名称 URL + api-key |
| Ollama | Ollama API | 本地 llama / deepseek / qwen |
| AWS Bedrock | SigV4 签名 | Claude / Llama / Titan / Mistral |
| NewLifeAI | 级联代理 | 聚合多服务商 |
| (其他) | OpenAI 兼容 | 37 个兼容平台 |
豆包(火山引擎)、智谱清言(GLM)、文心一言、月之暗面(Kimi)、MiniMax、阶跃星辰(StepFun)、百川、讯飞星火、零一万物、Moonshot、Mistral、Perplexity、Cohere、Together AI、Fireworks、OpenRouter、SiliconCloud、DeepInfra、Groq、Cerebras、Hyperbolic、Nebius、Novita、Lepton、302.AI、xAI(Grok)……以及其他 OpenAI 兼容平台。
所有服务商通过 [AiClient] 特性声明,AiClientRegistry 启动时自动扫描注册,新增服务商零配置。
dotnet add package NewLife.AI
using NewLife.AI.Clients;
// 单轮问答
using var client = new DashScopeChatClient("your-api-key", "qwen-plus");
var reply = await client.ChatAsync("用三句话介绍一下大语言模型");
Console.WriteLine(reply);
// 多角色消息(元组数组,无需手动构造 ChatMessage)
var reply2 = await client.ChatAsync([
("system", "你是一名专业的 C# 开发助手"),
("user", "解释一下 ValueTask 和 Task 的区别"),
]);
// 流式输出
await foreach (var chunk in client.GetStreamingResponseAsync([
new ChatMessage { Role = "user", Content = "写一首关于代码的短诗" }
], new ChatOptions()))
{
Console.Write(chunk.Text);
}
dotnet add package NewLife.AI.Extensions
// 注册服务
builder.Services.AddDashScope("your-api-key", "qwen-plus");
// Keyed 多服务商并存
builder.Services.AddOpenAI("openai-key", serviceKey: "openai");
builder.Services.AddAnthropic("anthropic-key", serviceKey: "anthropic");
// 注入使用
public class MyService(IChatClient chatClient)
{
public Task<String> ChatAsync(String question)
=> chatClient.ChatAsync(question);
}
public class MyTools
{
/// <summary>获取指定城市的天气</summary>
[ToolDescription("get_weather")]
public async Task<String> GetWeatherAsync(
[Description("城市名")] String city)
=> $"{city} 今天晴,22°C";
}
// 注册工具
var registry = new ToolRegistry();
registry.AddTools<MyTools>(new MyTools());
// 挂入管道,自动多轮循环工具调用
var client = rawClient.AsBuilder()
.UseTools(registry)
.Build();
// 模型自动调用 get_weather("北京"),返回最终文本答案
var reply = await client.ChatAsync("北京今天天气怎么样?");
当 AI 对复杂问题容易猜测或走弯路时,在 System Prompt 中使用 ReAct 格式强制逐步推理。
ToolChatClient 已内置 while 大循环(MaxIterations = 10),无需手写 Agent;
搭配 ReAct System Prompt 可进一步约束每步的推理方向,防止跳步猜测。
// UseTools 管道装配:ToolChatClient 自动循环至模型不再发起工具调用为止
var client = rawClient.AsBuilder()
.UseTools(registry)
.Build();
// ReAct System Prompt:Thought → Action → Observation → Answer 强制逐步推理
var reply = await client.ChatAsync([
("system",
"你必须按如下格式逐步推理,不允许跳过步骤或直接猜测:\n" +
"Thought: <分析当前需要做什么>\n" +
"Action: <调用哪个工具及参数>\n" +
"Observation: <分析工具返回结果>\n" +
"(重复 Thought/Action/Observation 直到信息充分)\n" +
"Answer: <基于以上观测得出最终结论>\n\n" +
"规则:禁止在未调用工具的情况下直接给出 Answer。"),
("user", "北京今天天气适合户外运动吗?"),
]);
git clone https://github.com/NewLifeX/NewLife.AI.git
cd NewLife.AI
# 构建前端(需要 Node.js + pnpm)
cd Web && pnpm install && pnpm build && cd ..
# 启动
cd NewLife.ChatAI
dotnet run --framework net8.0
浏览器访问 http://localhost:5000,默认 SQLite,开箱即用。首次启动通过 /Admin 配置服务商 API Key。
也可将 NewLife.ChatAI 通过 NuGet 嵌入已有项目:
dotnet add package NewLife.ChatAI
using NewLife.ChatAI;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddChatAI();
var app = builder.Build();
app.UseChatAI(redirectToChat: true);
app.Run();
NewLife.ChatAI 内置多协议 AI 网关,第三方系统无需改造即可接入,全路径经过记忆注入与技能增强。
| 协议 | 路由 | 说明 |
|---|---|---|
| OpenAI ChatCompletions | POST /v1/chat/completions |
流式 / 非流式 / 函数调用 / 视觉 |
| OpenAI Responses | POST /v1/responses |
o3 / gpt-5 推理模型 |
| Anthropic Messages | POST /v1/messages |
Claude 系列 |
| Google Gemini | POST /v1/gemini/... |
Gemini 系列 |
| 图像生成 | POST /v1/images/generations |
Text-to-Image |
| 图像编辑 | POST /v1/images/edits |
Inpainting(multipart/form-data) |
| 模型发现 | GET /v1/models |
可用模型列表 |
认证:Authorization: Bearer sk-xxxx(AppKey)
特性:上游 429 指数退避重试(随机抖动,最多 5 次)、Token 用量自动记录、按 AppKey + 用户双维度统计
继承 OpenAIChatClient,添加 [AiClient] 特性,AiClientRegistry 启动自动扫描注册:
[AiClient("MyAI", "我的服务", "https://api.myai.com/v1",
Description = "自定义 AI 服务")]
[AiClientModel("myai-latest", "MyAI Latest", Code = "MyAI",
FunctionCalling = true, Vision = true)]
public class MyAiChatClient : OpenAIChatClient
{
public MyAiChatClient() { }
public MyAiChatClient(String apiKey, String? model = null, String? endpoint = null)
: base(apiKey, model, endpoint) { }
}
public class MyTools
{
/// <summary>查询当前时间</summary>
[ToolDescription("get_current_time")]
public String GetCurrentTime()
=> DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
var registry = new ToolRegistry();
registry.AddTools<MyTools>(new MyTools());
// DI 场景
services.AddSingleton<IToolProvider>(_ =>
{
var r = new ToolRegistry();
r.AddTools<MyTools>(new MyTools());
return r;
});
在对话事前(OnBefore)或事后(OnAfter)注入自定义逻辑(如上下文注入、审计、记录):
[ChatHandlerOrder(150)]
public class CurrentTimeHandler : ChatHandlerBase
{
public override Task OnBefore(IChatContext context, CancellationToken cancellationToken)
{
context.ContextMessages.Insert(0,
new ChatMessage { Role = "system", Content = $"当前时间:{DateTime.Now:yyyy-MM-dd HH:mm}" });
return Task.CompletedTask;
}
}
// DI 注册,MessageFlow 自动按 ChatHandlerOrderAttribute 排序调用
services.AddSingleton<IChatHandler, CurrentTimeHandler>();
洋葱圈模型,可在对话前后插入日志、审计、内容审核等逻辑:
public class AuditFilter : IChatFilter
{
public async Task OnChatAsync(
ChatFilterContext ctx,
Func<ChatFilterContext, CancellationToken, Task> next,
CancellationToken ct)
{
// before:记录输入 / 敏感词过滤
await next(ctx, ct);
// after:记录输出 / 写审计日志
}
public Task OnStreamCompletedAsync(ChatFilterContext ctx, CancellationToken ct)
=> Task.CompletedTask;
}
| 文档 | 说明 |
|---|---|
| 产品目标、功能清单、非功能需求 | |
| 四层架构、各模块设计细节 | |
| 工具 / 智能体 / 规划器设计 | |
| 网关协议适配详解 | |
| MCP 客户端与服务端设计 | |
| 技能系统详细设计 | |
| 对话分析 + 记忆提取 | |
| MessageFlow 详细流程 | |
| 完整功能清单 |
欢迎提交 Issue 与 Pull Request。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net45 net45 is compatible. net451 net451 was computed. net452 net452 was computed. net46 net46 was computed. net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 4 NuGet packages that depend on NewLife.AI:
| Package | Downloads |
|---|---|
|
NewLife.Cube.Core
魔方MVC/Razor兼容层,支持ASP.NET Core MVC传统开发模式。提供用户权限管理、Razor视图模板、SSO登录、OAuth服务端、数据导出等功能,单表100亿级数据验证。 |
|
|
NewLife.Cube
魔方WebAPI核心库,提供实体控制器、权限管理、菜单管理、字段元数据、OAuth服务端、JWT认证等功能。支持前后端分离架构,对接Vue/Blazor/Antd等前端框架,单表100亿级数据验证。 |
|
|
NewLife.ChatAI
集对话前端、统一 AI 网关、Agent 平台于一身的完整 AI 应用。支持多轮对话、流式输出、函数调用、图像生成、会话管理与使用量统计 |
|
|
NewLife.StarChat
集对话前端、统一 AI 网关、Agent 平台于一身的完整 AI 应用。支持多轮对话、流式输出、函数调用、图像生成、会话管理与使用量统计 |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.3.2026.616-beta1104 | 61 | 6/16/2026 |
| 1.3.2026.615-beta0214 | 90 | 6/15/2026 |
| 1.3.2026.613-beta0140 | 56 | 6/13/2026 |
| 1.3.2026.611-beta0857 | 130 | 6/11/2026 |
| 1.3.2026.611-beta0320 | 103 | 6/11/2026 |
| 1.3.2026.610-beta0815 | 96 | 6/10/2026 |
| 1.3.2026.609-beta1557 | 85 | 6/9/2026 |
| 1.3.2026.608 | 143 | 6/8/2026 |
| 1.3.2026.608-beta0921 | 108 | 6/8/2026 |
| 1.2.2026.531-beta1644 | 108 | 5/31/2026 |
| 1.2.2026.529-beta0051 | 127 | 5/29/2026 |
| 1.2.2026.528-beta1609 | 115 | 5/28/2026 |
| 1.2.2026.528-beta0359 | 105 | 5/28/2026 |
| 1.2.2026.527-beta0103 | 85 | 5/27/2026 |
| 1.2.2026.526-beta0043 | 100 | 5/26/2026 |
| 1.2.2026.522-beta0457 | 111 | 5/22/2026 |
| 1.2.2026.521-beta1541 | 102 | 5/21/2026 |
| 1.2.2026.521-beta1221 | 107 | 5/21/2026 |
| 1.2.2026.521-beta0418 | 129 | 5/21/2026 |
| 1.2.2026.520-beta0906 | 108 | 5/20/2026 |
知识进化层功能落地,支持知识库自动构建与向量语义检索;嵌入/向量检索能力内置,HashTextEmbedder v2;TTS 语音合成支持 DashScope/CosyVoice V3.5;新增反思代理与评审代理,复杂任务拆分与并行聚合;人机决策检查点功能,支持 AI 多路径人工选择;工具调用增强:ToolCallContext 透传、Provider 熔断器、三档权限体系;兼容 net45/net462