大模型对话保存到obsidian时格式混乱有什么好的解决办法嘛?

遇到的问题

我经常会想记录与大模型的对话,用obsidian的浏览器插件来导出对话内容。但是经常遇到格式变混乱的现象,尤其是公式,很多时候 $$会变成$$ $$,导致段落变的零散,即使使用替换,也需要手动去进一步调整,或者是会多很多空行,以及缩进,每次都通过替换或者手动删除的方式来调整,有没有什么更好的方法能借鉴嘛?

obsidian web clipper浏览器插件对大模型对话记录有特别的适配,基本没啥大问题

1 个赞

好的,感谢!!我再研究研究。

用deepseek的话,可以导出json,然后win powershell命令脚本,可以自动转为md,我还让它给对话加了日期,你还可以去自定义导出模板,代码deepseek帮忙写的

$InputFile = "conversations.json"
if (-not (Test-Path $InputFile)) { 
    Write-Host "找不到文件: $InputFile" -ForegroundColor Red
    exit 
}
$json = Get-Content -Path $InputFile -Raw -Encoding UTF8 | ConvertFrom-Json

function Get-Messages($nodeId, $mapping) {
    $messages = @()
    $node = $mapping.$nodeId
    if (-not $node) { return $messages }
    
    if ($node.message -and $node.message.fragments) {
        $timeRaw = $node.message.inserted_at
        try { $dt = [datetime]::Parse($timeRaw); $time = $dt } catch { $time = $null }
        foreach ($frag in $node.message.fragments) {
            $content = $frag.content
            if ([string]::IsNullOrWhiteSpace($content)) { continue }
            $role = switch ($frag.type) {
                "REQUEST"  { "用户" }
                "THINK"    { "思考中" }
                "RESPONSE" { "deepseek" }
                default    { $null }
            }
            if ($role) {
                $messages += [PSCustomObject]@{
                    Time    = $time
                    RawTime = $timeRaw
                    Role    = $role
                    Content = $content
                }
            }
        }
    }
    
    if ($node.children) {
        foreach ($childId in $node.children) {
            $messages += Get-Messages $childId $mapping
        }
    }
    return $messages
}

$processed = 0
$errors = 0

foreach ($conv in $json) {
    $title = $conv.title
    Write-Host "正在处理 [$($processed+1)/$($json.Count)] : $title" -ForegroundColor Yellow
    try {
        $messages = Get-Messages "root" $conv.mapping
        if ($messages.Count -eq 0) { 
            Write-Host "  无消息,跳过" -ForegroundColor Gray
            continue 
        }
        
        # 准备文件内容(不包含标题行)
        $lines = @()
        foreach ($msg in $messages) {
            $timeStr = if ($msg.Time) { $msg.Time.ToString("yyyy-MM-dd HH:mm:ss") } else { $msg.RawTime }
            $lines += "[$timeStr] $($msg.Role): "
            $lines += $msg.Content
            $lines += ""
        }
        
        # ===== 修改后的文件名生成部分 =====
        $firstMsg = $messages[0]
        $dateStr = ""
        if ($firstMsg.Time) {
            $dateStr = $firstMsg.Time.ToString("yyyy-MM-dd")
        } else {
            try {
                $rawDate = $firstMsg.RawTime -replace 'T.*', ''
                $dt = [datetime]::ParseExact($rawDate, "yyyy-MM-dd", $null)
                $dateStr = $dt.ToString("yyyy-MM-dd")
            } catch {
                $dateStr = (Get-Date).ToString("yyyy-MM-dd")
            }
        }
        
        $safeTitle = $conv.title -replace '[\\/:*?"<>|]', '_'
        if ([string]::IsNullOrWhiteSpace($safeTitle)) { $safeTitle = "无标题" }
        
        $outFile = "$dateStr $safeTitle.md"
        # =================================
        
        $lines -join "`r`n" | Out-File -FilePath $outFile -Encoding utf8
        $processed++
        Write-Host "  已生成: $outFile" -ForegroundColor Green
    } catch {
        Write-Host "  处理出错: $_" -ForegroundColor Red
        $errors++
    }
}

Write-Host "`n处理完成!成功: $processed, 失败: $errors" -ForegroundColor Cyan
1 个赞

提供另一种思路,让大模型输出时加一句简单 Prompt,让它使用标准的 Markdown 代码块包裹,能解决输出格式不够规范的问题,也方便复制。

1 个赞