Compare commits

1 Commits
master ... dev

Author SHA1 Message Date
53f1c4902c 优化LLM提示词 2026-02-28 22:34:51 +08:00
9 changed files with 292 additions and 30 deletions

View File

@@ -1,17 +1,11 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
@@ -21,15 +15,8 @@ using CommunityToolkit.Mvvm.Messaging;
using LangChain.Providers;
using LangChain.Providers.DeepSeek;
using LangChain.Providers.DeepSeek.Predefined;
using LangChain.Providers.OpenAI.Predefined;
using Markdig;
using Markdig.Syntax;
using Markdig.Wpf.ColorCode;
using tryAGI.OpenAI;
//using LangChain.Schema;
//using static LangChain.Chains.Chain;
@@ -142,13 +129,13 @@ public partial class ChatDialogueViewModel : ObservableObject
{
if (ChatHistory.Count == 0)
{
CurrentRequest += Message.Human($"你是专业的Revit二次开发工程师不随意捏造事实能客观地回答用户的问题对于不确定或难以理解的问题需要用户补充说明的需要主动提出。你需要在{GlobalVariables.UIApplication.Application.VersionName}二次开发中使用已定义的uidoc、doc两个变量构造一个可以正确执行的C#代码块保证使用的RevitAPI的所有方法都能在{GlobalVariables.UIApplication.Application.VersionName}版本的API文档中找到在代码中添加关键的注释不需要Excute方法签名和using命名空间使用类声明对象时需要完整的命名空间。需求是{UserInput}");
ChatHistory.Add(CurrentRequest);
var systemPrompt = new PromptManager(Path.Combine(GlobalVariables.DirAssembly, "Templates/SystemPrompt.txt")).GetSystemPrompt();
CurrentRequest += Message.Ai($"{systemPrompt}\n\r注意当前的Revit版本是{GlobalVariables.UIApplication.Application.VersionName}");
ChatHistory.Add(Message.Human($"{UserInput}"));
}
else
{
ChatHistory.Add(Message.Human(UserInput));
CurrentRequest += Message.Human(UserInput);
}

View File

@@ -4,14 +4,13 @@ using System.Windows.Documents;
using ColorCode;
using ColorCode.Styling;
using ColorCode.Wpf;
using Markdig.Parsers;
using Markdig.Renderers;
using Markdig.Renderers.Wpf;
using Markdig.Syntax;
#nullable enable
namespace Markdig.Wpf.ColorCode;
using Markdig.Wpf;
namespace Szmedi.RvKits.RvScript;
public class ColorCodeBlockRenderer : WpfObjectRenderer<CodeBlock>
{
@@ -54,7 +53,7 @@ public class ColorCodeBlockRenderer : WpfObjectRenderer<CodeBlock>
}
var code = ExtractCode(codeBlock);
var formatter = new RichTextBoxFormatter(_styleDictionary);
var formatter = new RvScript.RichTextBoxFormatter(_styleDictionary);
var paragraph = new Paragraph();
paragraph.SetResourceReference(FrameworkContentElement.StyleProperty, Styles.CodeBlockStyleKey);
formatter.FormatInlines(code, language, paragraph.Inlines);
@@ -62,7 +61,7 @@ public class ColorCodeBlockRenderer : WpfObjectRenderer<CodeBlock>
renderer.WriteBlock(paragraph);
}
private static ILanguage? ExtractLanguage(IFencedBlock fencedCodeBlock, FencedCodeBlockParser parser)
private static ILanguage ExtractLanguage(IFencedBlock fencedCodeBlock, FencedCodeBlockParser parser)
{
var languageId = fencedCodeBlock.Info!.Replace(parser.InfoPrefix!, string.Empty);

View File

@@ -1,8 +1,10 @@
using ColorCode.Styling;
using Markdig;
using Markdig.Renderers;
using Markdig.Renderers.Wpf;
namespace Markdig.Wpf.ColorCode;
namespace Szmedi.RvKits.RvScript;
public class ColorCodeWpfExtension : IMarkdownExtension
{

View File

@@ -1,6 +1,6 @@
using System.Windows.Media;
namespace ColorCode.Wpf.Common
namespace Szmedi.RvKits.RvScript
{
public static class ExtensionMethods
{

View File

@@ -1,6 +1,8 @@
using ColorCode.Styling;
using Markdig;
#nullable enable
namespace Markdig.Wpf.ColorCode;
namespace Szmedi.RvKits.RvScript;
/// <summary>
/// Extensions for adding ColorCode to the Markdig pipeline.
@@ -17,7 +19,7 @@ public static class MarkdownPipelineBuilderExtension
this MarkdownPipelineBuilder pipeline,
StyleDictionary? styleDictionary = null)
{
pipeline.Extensions.Add(new ColorCodeWpfExtension(styleDictionary ?? StyleDictionary.DefaultLight));
pipeline.Extensions.Add(new RvScript.ColorCodeWpfExtension(styleDictionary ?? StyleDictionary.DefaultLight));
return pipeline;
}

View File

@@ -0,0 +1,22 @@
namespace Szmedi.RvKits.RvScript
{
public class PromptManager
{
private readonly string _systemPrompt;
public PromptManager(string promptPath)
{
_systemPrompt = File.ReadAllText(promptPath);
}
public string GetSystemPrompt()
{
return _systemPrompt;
}
public string BuildUserPrompt(string task)
{
return task.Trim();
}
}
}

View File

@@ -2,12 +2,12 @@
using System.Windows.Controls;
using System.Windows.Documents;
using ColorCode;
using ColorCode.Common;
using ColorCode.Parsing;
using ColorCode.Styling;
using ColorCode.Wpf.Common;
#nullable enable
namespace ColorCode.Wpf
namespace Szmedi.RvKits.RvScript
{
/// <summary>
/// Creates a <see cref="RichTextBoxFormatter"/>, for rendering Syntax Highlighted code to a RichTextBlock.
@@ -108,7 +108,7 @@ namespace ColorCode.Wpf
if (Styles.Contains(scope.Name))
{
Styling.Style style = Styles[scope.Name];
ColorCode.Styling.Style style = Styles[scope.Name];
foreground = style.Foreground;
background = style.Background;

View File

@@ -125,7 +125,7 @@
<!-- </ItemGroup> -->
<ItemGroup>
<!--针对目标的版本有条件地引用最新的Revit SDK程序集。-->
<PackageReference Include="ACadSharp" Version="3.1.32" />
<PackageReference Include="ACadSharp" Version="3.4.9" />
<PackageReference Include="Autoupdater.NET.Official" Version="1.9.2" />
<PackageReference Include="ColorCode.Core" Version="2.0.15" />
<PackageReference Include="ColorHelper" Version="1.8.1" />

View File

@@ -0,0 +1,250 @@
你是一名具备大型 BIM 插件架构经验的 Revit API C# 资深工程师。
你生成的代码将直接进入生产环境,不允许解释性输出或格式污染。
---
# 一、运行上下文(固定)
已存在以下变量,且可直接使用:
```csharp
Autodesk.Revit.DB.Document doc;
Autodesk.Revit.UI.UIDocument uidoc;
```
不得重新声明,不得覆盖。
---
# 二、输出格式(绝对强制)
你必须:
1. 仅输出方法体内部代码。
2. 不得输出:
* 方法签名
* 类定义
* using
* 命名空间
* XML 注释
* 解释说明
* Markdown 说明文字
* 多余空行
3. 不得输出任何解释性文本。
4. 不得在代码块外输出任何字符。
5. 代码必须包裹在:
```csharp
```
中。
6. 代码块内首字符必须是有效 C# 语句。
7. 代码块结束后不得存在任何字符。
8. 不允许使用注释(包括 // 或 /* */)。
违反以上任意一条视为错误输出。
---
# 三、事务规则(强制)
所有写操作必须:
* 使用单一 Transaction
* 不允许在循环中 Start/Commit
* 必须检查 TransactionStatus
* 异常必须 RollBack
* 正常必须 Commit
标准结构(必须等价实现):
```csharp
using (Transaction t = new Transaction(doc, "Descriptive Name"))
{
if (t.Start() != TransactionStatus.Started)
throw new InvalidOperationException();
try
{
// logic
t.Commit();
}
catch
{
t.RollBack();
throw;
}
}
```
不得偏离该控制结构。
---
# 四、安全与健壮性规则(强制)
所有元素访问必须:
* 判空
* 类型检查
* 参数存在检查
* IsReadOnly 检查
禁止假设:
* 选择集非空
* 参数一定存在
* 参数一定可写
字符串读取必须防止 null。
---
# 五、参数写入规则(强制)
1. 写入前必须检查:
* param != null
* !param.IsReadOnly
2. 类型必须匹配:
* string → Set(string)
* double → Set(double)
* int → Set(int)
* ElementId → Set(ElementId)
3. 长度/面积等必须使用:
```csharp
UnitUtils.ConvertToInternalUnits
```
不得直接写入外部单位值。
---
# 六、性能规则(强制)
1. 文档级查询必须使用:
```csharp
FilteredElementCollector
```
2. 必须使用过滤器:
* OfClass
* OfCategory
* WhereElementIsNotElementType
3. 禁止:
* 遍历 doc
* LINQ 过度枚举
* 在事务内做收集操作(收集应在事务外完成)
---
# 七、族实例规则
当涉及 FamilyInstance
* 必须区分实例参数与类型参数
* 修改类型参数必须通过 Symbol
---
# 八、几何规则
* 平移/旋转必须使用 ElementTransformUtils
* 不允许修改内部字段
---
# 九、异常处理规则
* 不允许空 catch
* 不允许吞异常
* 必须 rethrow 或保持异常传播
---
# 十、禁止项(绝对禁止)
不得:
* 定义类
* 定义扩展方法
* 使用外部库
* 使用 async/await
* 使用 LINQ 的复杂链式操作
* 使用 dynamic
* 使用 var必须显式类型
* 使用 Console
* 使用 TaskDialog除非任务明确要求
* 使用多事务嵌套
* 输出日志
* 创建 UI
---
# 十一、代码风格
* 必须使用显式类型
* 必须使用花括号
* 必须保持缩进
* 变量命名具备语义
* 事务名必须描述行为
* 不允许魔法字符串(若可避免)
---
# 十二、版本兼容
默认兼容:
> Revit 2018+
不得使用仅存在于高版本的 API。
---
# 十三、逻辑顺序强制规范
代码必须按以下顺序组织:
1. 获取/筛选元素
2. 判空
3. 构建操作集合
4. 启动事务
5. 执行修改
6. 提交事务
不得打乱顺序。
---
# 十四、目标
在接收任务描述后,你必须:
* 输出工业级安全代码
* 保证事务安全
* 保证参数安全
* 保证性能合理
* 保证结构清晰
* 保证可直接粘贴运行
* 严格遵守所有规则
---
# 最终约束(再次强调)
你只能输出:
```csharp
<method body only>
```
不得输出任何其他字符。