Gradio.Net 开发指南 -- 快速创建聊天机器人 目录快速创建聊天机器人简介定义聊天函数流式聊天机器人自定义聊天 UI额外输入额外输出返回复杂响应直接修改聊天机器人值通过 API 使用聊天机器人Using Your Chatbot via API聊天历史收集用户反馈总结上一篇Gradio.Net (https://github.com/feiyun0112/Gradio.Net)是一个开源的 .NET 库它是 Gradio 的 .NET 移植版本允许你为机器学习模型、API 或任何 C# 函数快速构建演示或 Web 应用程序无需任何 JavaScript、CSS 或 Web 开发经验快速创建聊天机器人简介聊天机器人是大型语言模型LLM的热门应用。使用 Gradio.Net你可以轻松构建聊天应用并分享给用户或通过直观的 UI 自行体验。本教程使用ChatInterface类这是一个高级抽象允许你快速创建聊天机器人 UI通常只需几行 C# 代码。它还可以轻松扩展以支持多模态聊天机器人或需要进一步定制的聊天机器人。定义聊天函数要使用ChatInterface创建聊天应用首先需要定义聊天函数。最简单的情况下你的聊天函数应该接受两个参数message和history参数可以任意命名但必须保持此顺序。‎message表示用户最新消息的字符串。‎history包含 ‎role和 ‎content键的 openai 格式字典列表表示之前的对话历史。history的格式如下[ {role: user, content: What is the capital of France?}, {role: assistant, content: Paris} ]你的聊天函数只需返回一个字符串值即聊天机器人基于聊天历史和最新消息的响应。示例1随机回复 Yes 或 Nousing Gradio.Net; string RandomResponse(object message, object history) { return new Random().Next(2) 0 ? Yes : No; } Funcobject, object, string fn RandomResponse; var demo new ChatInterface(fn: fn); await demo.Launch();示例2交替同意和不同意using Gradio.Net; string AlternatinglyAgree(object message, object history) { var histList history as ListDictionarystring, object ?? new(); int assistantCount histList.Count(h h.ContainsKey(role) h[role]?.ToString() assistant); if (assistantCount % 2 0) return $Yes, I do think that: {message}; else return I dont think so; } Funcobject, object, string fn AlternatinglyAgree; var demo new ChatInterface(fn: fn); await demo.Launch();流式聊天机器人在聊天函数中可以使用IAsyncEnumerableobject生成一系列部分响应每个响应替换前一个。这样就实现了流式聊天机器人using Gradio.Net; async IAsyncEnumerableobject SlowEcho(object message, object history) { var msg message?.ToString() ?? ; for (int i 0; i msg.Length; i) { await Task.Delay(300); yield return You typed: msg[..i]; } } Funcobject, object, IAsyncEnumerableobject fn SlowEcho; var demo new ChatInterface(fn: fn); await demo.Launch();响应流式传输时Submit 按钮会变成 Stop 按钮可以用来停止生成。自定义聊天 UI如果你熟悉 Gradio.Net 的Interface类ChatInterface包含许多相同的参数可用于自定义聊天机器人的外观和行为使用 ‎title和 ‎description参数在聊天机器人上方添加标题和描述使用 ‎examples参数添加预设示例并可以通过 ‎cacheExamples进行缓存自定义聊天机器人如更改高度或添加占位符或文本框如设置最大字符数添加示例可以通过examples参数为ChatInterface添加预设示例这些示例会以按钮形式显示在聊天机器人中using Gradio.Net; using Gradio.Net.Components; string YesMan(object message, object history) { var msg message?.ToString() ?? ; if (msg.EndsWith(?)) return Yes; else return Ask me anything!; } Funcobject, object, string fn YesMan; var demo new ChatInterface( fn: fn, chatbot: gr.Chatbot(height: 300), textbox: gr.Textbox(placeholder: Ask me a yes or no question, scale: 7), title: Yes Man, description: Ask Yes Man any question, examples: new Liststring { Hello, Am I cool?, Are tomatoes vegetables? }); await demo.Launch();额外输入你可能希望为聊天函数添加额外的输入并通过聊天 UI 向用户展示。例如可以添加一个系统提示文本框或一个设置响应 token 数量的滑块。ChatInterface类支持additionalInputs参数using Gradio.Net; using Gradio.Net.Components; async IAsyncEnumerableobject Echo(object message, object history, string systemPrompt, double tokens) { string response $System prompt: {systemPrompt}\nMessage: {message}.; int maxTokens (int)tokens; for (int i 0; i Math.Min(response.Length, maxTokens); i) { await Task.Delay(50); yield return response[..(i 1)]; } } Funcobject, object, string, double, IAsyncEnumerableobject fn Echo; var demo new ChatInterface( fn: fn, additionalInputs: new ListComponent { gr.Textbox(You are helpful AI., label: System Prompt), gr.Slider(minimum: 10, maximum: 100) }); await demo.Launch();额外输出同样你也可以从聊天函数返回额外的输出。只需向additionalOutputs参数传入组件列表并从聊天函数中返回这些组件的额外值using Gradio.Net; using Gradio.Net.Components; (string, Code?) Chat(object message, object history) { var msg message?.ToString()?.ToLower() ?? ; if (msg.Contains(python)) return (Heres the Python code., gr.Code(language: python, value: def fib(n): return n if n 1 else fib(n-1)fib(n-2))); else if (msg.Contains(csharp) || msg.Contains(c#)) return (Heres the C# code., gr.Code(language: csharp, value: int Fib(int n) n 1 ? n : Fib(n-1) Fib(n-2);)); else return (Please ask about Python or C#., null); } using var demo gr.Blocks(); var code gr.Code(render: false); using (gr.Row()) { using (gr.Column()) { gr.Markdown(centerh1Write Python or C#/h1/center); var chat new ChatInterface( fn: (Funcobject, object, (string, Code?))Chat, examples: new Liststring { Python, C# }, additionalOutputs: new ListComponent { code }); chat.Render(); } using (gr.Column()) { gr.Markdown(centerh1Code Artifacts/h1/center); code.Render(); } } await demo.Launch();返回复杂响应返回文件或 Gradio.Net 组件以下 Gradio.Net 组件可以在聊天界面中显示gr.Image、gr.Plot、gr.Audio、gr.HTML、gr.Video、gr.Gallery、gr.File。从聊天函数中返回这些组件即可使用它们using Gradio.Net; using Gradio.Net.Components; object Music(object message, object history) { var msg message?.ToString()?.Trim() ?? ; if (!string.IsNullOrEmpty(msg)) return gr.Audio(https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav); else return Please provide the name of an artist; } Funcobject, object, object fn Music; var demo new ChatInterface( fn: fn, textbox: gr.Textbox(placeholder: Which artists music do you want to listen to?, scale: 7)); await demo.Launch();显示中间思考过程或工具调用ChatInterface支持显示中间思考过程或工具调用。要实现这一点需要从聊天函数中返回ChatMessage对象并在metadata中设置title键using Gradio.Net; async IAsyncEnumerableobject SimulateThinking(object message, object history) { var response new ChatMessage( content: , metadata: new Dictionarystring, object { [title] _Thinking_ step-by-step, [id] 0, [status] pending }); yield return response; var thoughts new[] { First, understanding the query..., Considering broader context..., Analyzing approaches..., Structuring the response... }; string accumulated ; foreach (var thought in thoughts) { await Task.Delay(500); accumulated $- {thought}\n\n; response.Content accumulated.Trim(); yield return response; } response.Metadata[status] done; yield return response; yield return new ChatMessage(content: Based on my analysis, here is my response.); } Funcobject, object, IAsyncEnumerableobject fn SimulateThinking; var demo new ChatInterface(fn: fn, title: Thinking LLM Chat Interface ); await demo.Launch();直接修改聊天机器人值可以通过ChatInterface.chatbot_value作为事件的输入或输出来直接修改聊天机器人的值。下面的例子用一个Radio组件预填充聊天历史using Gradio.Net; using Gradio.Net.Components; ListDictionarystring, object? PrefillChatbot(string choice) { return choice switch { Greeting new ListDictionarystring, object { new() { [role] user, [content] Hi there! }, new() { [role] assistant, [content] Hello! How can I assist you today? } }, Complaint new ListDictionarystring, object { new() { [role] user, [content] Im not happy with the service. }, new() { [role] assistant, [content] Im sorry to hear that. Can you tell me more? } }, _ new ListDictionarystring, object() }; } string RandomResponse(object message, object history) { return new Random().Next(2) 0 ? Yes : No; } using var demo gr.Blocks(); var radio gr.Radio(new Liststring { Greeting, Complaint, Blank }, label: Prefill); var chat new ChatInterface( fn: (Funcobject, object, string)RandomResponse, apiName: chat); chat.Render(); radio.Change(fn: PrefillChatbot, inputs: radio, outputs: chat.ChatbotValue); await demo.Launch();通过 API 使用聊天机器人Using Your Chatbot via API一旦你构建了 Gradio.Net 聊天界面并将其托管就可以通过简单的 API 查询它。API 路由将是你传递给ChatInterface的函数名称例如gr.ChatInterface(fn: respond)则 API 路由为/respond。聊天历史通过设置saveHistory: true可以为ChatInterface启用持久化聊天历史允许用户维护多个对话并轻松切换。对话会存储在用户浏览器的本地存储中私密且不会与其他用户共享var demo new ChatInterface(fn: fn, saveHistory: true); await demo.Launch();收集用户反馈通过设置flaggingMode: manual用户可以对助手的响应点赞或点踩。每个被标记的响应以及整个聊天历史都将保存到工作目录中的 CSV 文件中async IAsyncEnumerableobject SlowEcho(object message, object history) { var msg message?.ToString() ?? ; for (int i 0; i msg.Length; i) { await Task.Delay(50); yield return You typed: msg[..i]; } } Funcobject, object, IAsyncEnumerableobject fn SlowEcho; var demo new ChatInterface( fn: fn, flaggingMode: manual, flaggingOptions: new Liststring { Like, Spam, Inappropriate, Other }, saveHistory: true); await demo.Launch();总结本章介绍了如何使用ChatInterface类快速创建聊天机器人‎基础聊天函数接受 ‎message和 ‎history两个参数返回字符串响应‎流式响应使用 ‎IAsyncEnumerableobject实现打字机效果‎自定义 UI通过 ‎title、‎description、‎examples、‎chatbot、‎textbox等参数定制界面‎额外输入通过 ‎additionalInputs添加系统提示、滑块等额外控件‎额外输出通过 ‎additionalOutputs输出代码、图像等内容‎复杂响应返回 Gradio.Net 组件或 ‎ChatMessage对象实现富文本展示‎聊天历史通过 ‎saveHistory: true持久化存储对话‎用户反馈通过 ‎flaggingMode收集用户反馈引入地址

相关新闻

最新新闻

游戏DLC解锁器技术原理与风险分析:从软件授权到安全防护

游戏DLC解锁器技术原理与风险分析:从软件授权到安全防护

1. 从“武装突袭3”的DLC生态说起:为什么会有“解锁器”的需求?如果你是一名《武装突袭3》(Arma 3)的玩家,尤其是那些在Steam上拥有本体但看着琳琅满目的DLC列表犹豫不决的玩家,那么“DLC解锁器”这个词对你…

2026/8/15 14:42:53
数据分析必备:用instascrape和Pandas实现Instagram数据可视化

数据分析必备:用instascrape和Pandas实现Instagram数据可视化

数据分析必备:用instascrape和Pandas实现Instagram数据可视化 【免费下载链接】instascrape Powerful and flexible Instagram scraping library for Python, providing easy-to-use and expressive tools for accessing data programmatically 项目地址: https:/…

2026/8/15 14:42:53
Docker一键部署OneDiffusion:Windows/Linux/macOS全平台安装教程

Docker一键部署OneDiffusion:Windows/Linux/macOS全平台安装教程

Docker一键部署OneDiffusion:Windows/Linux/macOS全平台安装教程 【免费下载链接】OneDiffusion Official implementation of OneDiffusion paper (CVPR 2025) 项目地址: https://gitcode.com/gh_mirrors/one/OneDiffusion OneDiffusion是CVPR 2025论文的官方…

2026/8/15 14:42:53
从0到1开发领域模型:shriek-fx 中聚合根与值对象的设计与实现

从0到1开发领域模型:shriek-fx 中聚合根与值对象的设计与实现

从0到1开发领域模型:shriek-fx 中聚合根与值对象的设计与实现 【免费下载链接】shriek-fx An easy-to-use rapid development framework developed on the basis of.NET Core 2.0, following the constraints of domain Driven Design (DDD) specifications, combin…

2026/8/15 14:42:53
instascrape核心功能详解:轻松抓取Instagram帖子、评论和用户资料

instascrape核心功能详解:轻松抓取Instagram帖子、评论和用户资料

instascrape核心功能详解:轻松抓取Instagram帖子、评论和用户资料 【免费下载链接】instascrape Powerful and flexible Instagram scraping library for Python, providing easy-to-use and expressive tools for accessing data programmatically 项目地址: htt…

2026/8/15 14:42:53
Tea Sepolia Testnet必备工具:Tea Auto Bot安装与配置教程

Tea Sepolia Testnet必备工具:Tea Auto Bot安装与配置教程

Tea Sepolia Testnet必备工具:Tea Auto Bot安装与配置教程 【免费下载链接】Tea-Auto-Bot A command-line interface (CLI) tool for automating interactions with the Tea Sepolia Testnet. This bot helps you manage your TEA tokens, stake, claim rewards, an…

2026/8/15 14:37:53