-
-
Notifications
You must be signed in to change notification settings - Fork 364
feat(Chat): add deepseek support #5653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0937b35
feat(Chat): add deepseek support
MadLongTom 504a34d
Merge branch 'main' into feat-chat-deepseek
ArgoZhang 4ebe802
Merge branch 'main' into feat-chat-deepseek
ArgoZhang 097fcf4
refactor: 微调样式
ArgoZhang ccfd0db
Merge branch 'main' into feat-chat-deepseek
MadLongTom 28aa00a
Update src/BootstrapBlazor.Server/Components/Components/MarkdownConte…
MadLongTom f797f46
chore(MarkdownContent): remove duplicated method calls
MadLongTom 436e56a
Merge branch 'main' into feat-chat-deepseek
ArgoZhang bdf1a26
Merge branch 'main' into feat-chat-deepseek
ArgoZhang a48a8e4
refactor: 移动 MarkdownContent 到扩展组件库中
ArgoZhang 53f4358
chore: 更新依赖组件
ArgoZhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
src/BootstrapBlazor.Server/Components/Components/MarkdownContent.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| @using Markdig | ||
| @using Markdown.ColorCode | ||
| @using ColorCode.Styling | ||
| @using System.Text.RegularExpressions | ||
| @using ColorCode | ||
| @using ColorCode.Compilation.Languages | ||
| @using Markdown = Markdig.Markdown | ||
| @inherits ComponentBase | ||
|
|
||
| <div id="@(Id ?? string.Empty)"> | ||
| @((MarkupString)(AsMarkdown ?? string.Empty)) | ||
| </div> | ||
|
|
||
| <style> | ||
| .think { | ||
| margin-bottom: 1rem; | ||
| font-size: 0.75rem; | ||
| font-weight: 400; | ||
| color: #A0A0A0; | ||
| border-left: 3px solid #dfdbdb; | ||
| padding-left: 10px; | ||
| } | ||
| </style> | ||
|
|
||
| @code { | ||
| [Parameter] | ||
| public required string Content { get; set; } | ||
|
|
||
| [Parameter] | ||
| public string? Id { get; set; } | ||
|
|
||
| public string? AsMarkdown; | ||
|
|
||
| string? Embedding { get; set; } | ||
|
|
||
| protected override void OnInitialized() | ||
| { | ||
| markdownPipeline = new MarkdownPipelineBuilder() | ||
| .UsePipeTables() | ||
| .UseAdvancedExtensions() | ||
| .UseColorCode(styleDictionary: StyleDictionary.DefaultLight, | ||
| additionalLanguages: new List<ILanguage>() | ||
| { | ||
| new Json(), | ||
| new CSharp(), | ||
| new Cpp(), | ||
| new Css(), | ||
| new Html(), | ||
| new JavaScript(), | ||
| new Php(), | ||
| }) | ||
| .UseAutoLinks() | ||
| .UseEmojiAndSmiley() | ||
| .UseMediaLinks() | ||
| .UseCitations() | ||
| .UseMathematics() | ||
| .UseAutoLinks() | ||
| .UseDiagrams() | ||
| .Build(); | ||
| } | ||
|
|
||
| protected override void OnParametersSet() | ||
| { | ||
| var embeddingsRemoved = RemoveEmbeddingsElement(Content); | ||
| AsMarkdown = GetMarkdown(embeddingsRemoved); | ||
| StateHasChanged(); | ||
MadLongTom marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private MarkdownPipeline? markdownPipeline; | ||
|
|
||
| private string GetMarkdown(string toHtml) | ||
| { | ||
| try | ||
| { | ||
| if (string.IsNullOrEmpty(toHtml)) | ||
| return string.Empty; | ||
|
|
||
| // 处理未封闭的 think 标签 | ||
| toHtml = HandleUnclosedThinkTags(toHtml); | ||
|
|
||
| // 处理正常的 think 标签 | ||
| var thinkPattern = @"<\s*think\b[^>]*>(.*?)<\s*/\s*think\s*>"; | ||
| toHtml = Regex.Replace(toHtml, thinkPattern, @"<div class=""think"">$1</div>", RegexOptions.Singleline | RegexOptions.IgnoreCase); | ||
| toHtml = RemoveEmbeddingsElement(toHtml); | ||
|
|
||
| var html = Markdown.ToHtml(toHtml, markdownPipeline); | ||
|
|
||
| var pattern = "(<div style=\"color:#DADADA;background-color:#1E1E1E;\"><pre>(.*?)</pre></div>)"; | ||
| var matches = Regex.Matches(html, pattern, RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase); | ||
|
|
||
| for (var i = matches.Count - 1; i >= 0; i--) | ||
| { | ||
| var match = matches[i].ToString(); | ||
| var id = "copy" + i; | ||
| var replacement = $"<button data-clipboard-target=\"#{id}\" class=\"float-end copyBtn mt-0\">Copy</button>" + match; | ||
| html = html.Remove(matches[i].Index, matches[i].Length).Insert(matches[i].Index, replacement); | ||
| } | ||
|
|
||
| return html; | ||
| } | ||
| catch (Exception) | ||
| { | ||
| return "error markdowncontent.razor"; | ||
MadLongTom marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| private string HandleUnclosedThinkTags(string content) | ||
| { | ||
| if (string.IsNullOrEmpty(content)) | ||
| return content; | ||
|
|
||
| // 匹配开始标签 | ||
| var openTagPattern = @"<\s*think\b[^>]*>"; | ||
| var closeTagPattern = @"<\s*/\s*think\s*>"; | ||
|
|
||
| var openTags = Regex.Matches(content, openTagPattern); | ||
| var closeTags = Regex.Matches(content, closeTagPattern); | ||
|
|
||
| // 如果开始标签数量等于结束标签数量,说明标签都是配对的 | ||
| if (openTags.Count == closeTags.Count) | ||
| return content; | ||
|
|
||
| // 处理未封闭的标签 | ||
| var parts = Regex.Split(content, openTagPattern); | ||
| if (parts.Length <= 1) | ||
| return content; | ||
|
|
||
| var result = parts[0]; // 保留第一部分的内容 | ||
| for (int i = 1; i < parts.Length; i++) | ||
| { | ||
| var part = parts[i]; | ||
| // 检查这部分是否已经包含结束标签 | ||
| if (!part.Contains("</think>", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| // 没有结束标签,添加一个完整的 think 标签包装 | ||
| result += $"<think>{part}</think>"; | ||
| } | ||
| else | ||
| { | ||
| // 已经有结束标签,保持原样 | ||
| result += $"<think>{part}"; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private string RemoveEmbeddingsElement(string data) | ||
| { | ||
| if (string.IsNullOrEmpty(data)) | ||
| { | ||
| return ""; | ||
| } | ||
| string pattern = @"\[EMBEDDINGS\](.*?)\[/EMBEDDINGS\]"; | ||
| var matches = Regex.Matches(data, pattern, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase); | ||
|
|
||
| if (matches.Count == 0) | ||
| { | ||
| return data; | ||
| } | ||
|
|
||
| data = Regex.Replace(data, pattern, "", RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase); | ||
|
|
||
| Embedding = $"{matches[0].Groups[1].Value}"; | ||
| return data; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.