feat: add DeepSeek official API support and capture reasoning content
- Add DeepSeek provider routing: deepseek-chat/deepseek-reasoner use api.deepseek.com, other models use SiliconFlow - Add stream_with_reasoning() using raw OpenAI SDK to capture reasoning_content (langchain_openai strips this field) - RAG chain and conversation chain both use stream_with_reasoning for proper reasoning display in thinking models - Frontend model selector: grouped by provider (DeepSeek official + SiliconFlow), default changed to deepseek-chat - Regenerate message converted to streaming with reasoning capture - Minor UI: globals.css additions, chat store refactoring Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -322,6 +322,62 @@ button {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
/* Prose 修复 — 确保有序/无序列表样式正确 */
|
||||
.prose ol {
|
||||
list-style-type: decimal !important;
|
||||
list-style-position: outside !important;
|
||||
padding-left: 1.625em !important;
|
||||
margin-top: 0;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
.prose ol li {
|
||||
display: list-item !important;
|
||||
margin-top: 0.25em;
|
||||
margin-bottom: 0.25em;
|
||||
}
|
||||
.prose ol li::before {
|
||||
content: none !important;
|
||||
}
|
||||
.prose ul {
|
||||
list-style-type: disc !important;
|
||||
list-style-position: outside !important;
|
||||
padding-left: 1.625em !important;
|
||||
}
|
||||
.prose ul li {
|
||||
display: list-item !important;
|
||||
margin-top: 0.25em;
|
||||
margin-bottom: 0.25em;
|
||||
}
|
||||
.prose ul li::before {
|
||||
content: none !important;
|
||||
}
|
||||
.prose p {
|
||||
margin-top: 0.5em;
|
||||
margin-bottom: 0.5em;
|
||||
}
|
||||
.prose h1, .prose h2, .prose h3, .prose h4 {
|
||||
font-family: inherit;
|
||||
letter-spacing: normal;
|
||||
margin-top: 1em;
|
||||
margin-bottom: 0.5em;
|
||||
font-weight: 600;
|
||||
}
|
||||
.prose h2 { font-size: 1.25em; }
|
||||
.prose h3 { font-size: 1.1em; }
|
||||
.prose h4 { font-size: 1em; }
|
||||
.prose strong { font-weight: 700; }
|
||||
.prose em { font-style: italic; }
|
||||
.prose blockquote {
|
||||
border-left: 3px solid hsl(var(--border));
|
||||
padding-left: 1em;
|
||||
color: hsl(var(--muted-foreground));
|
||||
font-style: italic;
|
||||
}
|
||||
.prose hr {
|
||||
border-color: hsl(var(--border));
|
||||
margin: 1.5em 0;
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
*, *::before, *::after {
|
||||
animation-duration: 0.01ms !important;
|
||||
|
||||
@@ -15,7 +15,7 @@ import { knowledgeBaseAPI } from "@/lib/api";
|
||||
export default function ChatInterface() {
|
||||
const [inputMessage, setInputMessage] = useState("");
|
||||
const [isComposing, setIsComposing] = useState(false);
|
||||
const [selectedModel, setSelectedModel] = useState("deepseek-ai/DeepSeek-V3");
|
||||
const [selectedModel, setSelectedModel] = useState("deepseek-chat");
|
||||
const [selectedKnowledgeBases, setSelectedKnowledgeBases] = useState<string[]>([]);
|
||||
const [systemKnowledgeBases, setSystemKnowledgeBases] = useState<KnowledgeBase[]>([]);
|
||||
const [userKnowledgeBases, setUserKnowledgeBases] = useState<KnowledgeBase[]>([]);
|
||||
|
||||
@@ -8,9 +8,10 @@ import {
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuLabel,
|
||||
} from "@/components/ui/dropdown-menu";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { ChevronDown, Cpu, Zap, Sparkles } from "lucide-react";
|
||||
import { ChevronDown, Cpu, Zap, Sparkles, Brain } from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
export interface ModelOption {
|
||||
@@ -18,6 +19,7 @@ export interface ModelOption {
|
||||
name: string;
|
||||
description: string;
|
||||
provider: string;
|
||||
providerLabel: string;
|
||||
icon?: React.ComponentType<{ className?: string }>;
|
||||
}
|
||||
|
||||
@@ -27,31 +29,64 @@ interface ModelSelectorProps {
|
||||
className?: string;
|
||||
}
|
||||
|
||||
const models: ModelOption[] = [
|
||||
const modelGroups = [
|
||||
{
|
||||
id: "deepseek-ai/DeepSeek-V3",
|
||||
name: "DeepSeek-V3",
|
||||
description: "DeepSeek 最新版本,强大的推理能力",
|
||||
provider: "DeepSeek",
|
||||
icon: Sparkles,
|
||||
provider: "deepseek-official",
|
||||
label: "DeepSeek 官方",
|
||||
models: [
|
||||
{
|
||||
id: "deepseek-chat",
|
||||
name: "DeepSeek-V3",
|
||||
description: "DeepSeek 最新通用模型,速度快、能力强",
|
||||
provider: "deepseek-official",
|
||||
providerLabel: "DeepSeek",
|
||||
icon: Sparkles,
|
||||
},
|
||||
{
|
||||
id: "deepseek-reasoner",
|
||||
name: "DeepSeek-R1",
|
||||
description: "深度推理模型,适合复杂分析任务",
|
||||
provider: "deepseek-official",
|
||||
providerLabel: "DeepSeek",
|
||||
icon: Brain,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "Qwen/QwQ-32B",
|
||||
name: "QwQ-32B",
|
||||
description: "Qwen 量子化模型,高效推理",
|
||||
provider: "Qwen",
|
||||
icon: Zap,
|
||||
provider: "siliconflow",
|
||||
label: "SiliconFlow(硅基流动)",
|
||||
models: [
|
||||
{
|
||||
id: "deepseek-ai/DeepSeek-V3",
|
||||
name: "DeepSeek-V3",
|
||||
description: "通过硅基流动调用,稳定的推理能力",
|
||||
provider: "siliconflow",
|
||||
providerLabel: "硅基流动",
|
||||
icon: Sparkles,
|
||||
},
|
||||
{
|
||||
id: "Qwen/QwQ-32B",
|
||||
name: "QwQ-32B",
|
||||
description: "Qwen 推理模型,高效准确",
|
||||
provider: "siliconflow",
|
||||
providerLabel: "硅基流动",
|
||||
icon: Zap,
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export default function ModelSelector({
|
||||
selectedModel,
|
||||
onModelChange,
|
||||
className
|
||||
const allModels = modelGroups.flatMap((g) => g.models);
|
||||
|
||||
export default function ModelSelector({
|
||||
selectedModel,
|
||||
onModelChange,
|
||||
className,
|
||||
}: ModelSelectorProps) {
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
|
||||
const selectedModelData = models.find(model => model.id === selectedModel) || models[0];
|
||||
|
||||
const selectedModelData =
|
||||
allModels.find((model) => model.id === selectedModel) || allModels[0];
|
||||
const IconComponent = selectedModelData.icon || Cpu;
|
||||
|
||||
return (
|
||||
@@ -68,53 +103,55 @@ export default function ModelSelector({
|
||||
>
|
||||
<IconComponent className="w-4 h-4 mr-2" />
|
||||
<span className="hidden sm:inline">{selectedModelData.name}</span>
|
||||
<span className="sm:hidden">{selectedModelData.name.split(' ')[0]}</span>
|
||||
<span className="sm:hidden">{selectedModelData.name.split(" ")[0]}</span>
|
||||
<ChevronDown className="w-3 h-3 ml-1 opacity-50" />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="start" className="w-64">
|
||||
<DropdownMenuContent align="start" className="w-72">
|
||||
<div className="px-2 py-1.5 text-xs font-medium text-muted-foreground">
|
||||
选择AI模型
|
||||
</div>
|
||||
<DropdownMenuSeparator />
|
||||
{models.map((model) => {
|
||||
const ModelIcon = model.icon || Cpu;
|
||||
const isSelected = model.id === selectedModel;
|
||||
|
||||
return (
|
||||
<DropdownMenuItem
|
||||
key={model.id}
|
||||
onClick={() => {
|
||||
onModelChange(model.id);
|
||||
setIsOpen(false);
|
||||
}}
|
||||
className={cn(
|
||||
"flex items-start space-x-3 p-3 cursor-pointer",
|
||||
isSelected && "bg-muted/50"
|
||||
)}
|
||||
>
|
||||
<ModelIcon className="w-4 h-4 mt-0.5 flex-shrink-0" />
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center space-x-2">
|
||||
<span className="font-medium text-sm">{model.name}</span>
|
||||
{isSelected && (
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
已选择
|
||||
</Badge>
|
||||
{modelGroups.map((group) => (
|
||||
<div key={group.provider}>
|
||||
<DropdownMenuLabel className="text-xs text-muted-foreground/70 font-normal px-2 pt-2">
|
||||
{group.label}
|
||||
</DropdownMenuLabel>
|
||||
{group.models.map((model) => {
|
||||
const ModelIcon = model.icon || Cpu;
|
||||
const isSelected = model.id === selectedModel;
|
||||
|
||||
return (
|
||||
<DropdownMenuItem
|
||||
key={model.id}
|
||||
onClick={() => {
|
||||
onModelChange(model.id);
|
||||
setIsOpen(false);
|
||||
}}
|
||||
className={cn(
|
||||
"flex items-start space-x-3 p-3 cursor-pointer",
|
||||
isSelected && "bg-muted/50"
|
||||
)}
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground mt-0.5">
|
||||
{model.description}
|
||||
</p>
|
||||
<div className="flex items-center space-x-1 mt-1">
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{model.provider}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</DropdownMenuItem>
|
||||
);
|
||||
})}
|
||||
>
|
||||
<ModelIcon className="w-4 h-4 mt-0.5 flex-shrink-0" />
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center space-x-2">
|
||||
<span className="font-medium text-sm">{model.name}</span>
|
||||
{isSelected && (
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
已选择
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground mt-0.5">
|
||||
{model.description}
|
||||
</p>
|
||||
</div>
|
||||
</DropdownMenuItem>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
))}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
);
|
||||
|
||||
+110
-91
@@ -255,6 +255,36 @@ export const useChatStore = create<ChatStore>((set, get) => ({
|
||||
let chunkCount = 0;
|
||||
let totalChars = 0;
|
||||
let thinkingSteps: ThinkingStep[] = [];
|
||||
let contentBuffer = '';
|
||||
let thinkingBuffer: ThinkingStep[] = [];
|
||||
let rafId: number | null = null;
|
||||
|
||||
const flushBuffer = () => {
|
||||
rafId = null;
|
||||
const bufferedContent = contentBuffer;
|
||||
const bufferedThinking = thinkingBuffer.length > 0 ? [...thinkingBuffer] : null;
|
||||
contentBuffer = '';
|
||||
thinkingBuffer = [];
|
||||
|
||||
if (!bufferedContent && !bufferedThinking) return;
|
||||
|
||||
set((state) => ({
|
||||
messages: state.messages.map(msg => {
|
||||
if (msg.id !== assistantMessage.id) return msg;
|
||||
return {
|
||||
...msg,
|
||||
...(bufferedContent ? { content: msg.content + bufferedContent } : {}),
|
||||
...(bufferedThinking ? { thinking: [...(msg.thinking || []), ...bufferedThinking] } : {}),
|
||||
};
|
||||
}),
|
||||
}));
|
||||
};
|
||||
|
||||
const scheduleFlush = () => {
|
||||
if (rafId === null) {
|
||||
rafId = requestAnimationFrame(flushBuffer);
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
await chatAPI.streamMessage(
|
||||
@@ -264,59 +294,37 @@ export const useChatStore = create<ChatStore>((set, get) => ({
|
||||
knowledgeBaseIds,
|
||||
abortController.signal, // 新增参数
|
||||
(chunk: string) => {
|
||||
// 解析chunk
|
||||
try {
|
||||
const data = JSON.parse(chunk);
|
||||
|
||||
if (data.type === 'thinking') {
|
||||
// 处理思考过程
|
||||
thinkingSteps.push({
|
||||
stage: data.stage,
|
||||
message: data.message,
|
||||
doc_count: data.doc_count,
|
||||
time: data.time
|
||||
});
|
||||
|
||||
// 立即更新思考过程到UI
|
||||
requestAnimationFrame(() => {
|
||||
set((state) => ({
|
||||
messages: state.messages.map(msg =>
|
||||
msg.id === assistantMessage.id
|
||||
? { ...msg, thinking: [...thinkingSteps] }
|
||||
: msg
|
||||
),
|
||||
}));
|
||||
});
|
||||
} else if (data.type === 'chunk') {
|
||||
// 处理内容chunk
|
||||
chunkCount++;
|
||||
totalChars += data.content.length;
|
||||
if (chunk.startsWith('{')) {
|
||||
try {
|
||||
const data = JSON.parse(chunk);
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
set((state) => ({
|
||||
messages: state.messages.map(msg =>
|
||||
msg.id === assistantMessage.id
|
||||
? { ...msg, content: msg.content + data.content }
|
||||
: msg
|
||||
),
|
||||
}));
|
||||
});
|
||||
if (data.type === 'thinking') {
|
||||
const step: ThinkingStep = {
|
||||
stage: data.stage,
|
||||
message: data.message,
|
||||
doc_count: data.doc_count,
|
||||
time: data.time
|
||||
};
|
||||
thinkingSteps.push(step);
|
||||
thinkingBuffer.push(step);
|
||||
scheduleFlush();
|
||||
return;
|
||||
} else if (data.type === 'chunk') {
|
||||
chunkCount++;
|
||||
totalChars += data.content.length;
|
||||
contentBuffer += data.content;
|
||||
scheduleFlush();
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
// JSON 解析失败,按纯文本处理
|
||||
}
|
||||
} catch (e) {
|
||||
// 向后兼容:纯文本chunk
|
||||
chunkCount++;
|
||||
totalChars += chunk.length;
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
set((state) => ({
|
||||
messages: state.messages.map(msg =>
|
||||
msg.id === assistantMessage.id
|
||||
? { ...msg, content: msg.content + chunk }
|
||||
: msg
|
||||
),
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
chunkCount++;
|
||||
totalChars += chunk.length;
|
||||
contentBuffer += chunk;
|
||||
scheduleFlush();
|
||||
},
|
||||
(_sessionId: number, messageId?: number, userMessageId?: number) => {
|
||||
if (messageId) {
|
||||
@@ -438,10 +446,37 @@ export const useChatStore = create<ChatStore>((set, get) => ({
|
||||
abortController,
|
||||
});
|
||||
|
||||
// 判断模式:检查之前的消息中是否有知识库相关内容
|
||||
const mode = "normal";
|
||||
|
||||
let thinkingSteps: any[] = [];
|
||||
let regenContentBuffer = '';
|
||||
let regenThinkingBuffer: any[] = [];
|
||||
let regenRafId: number | null = null;
|
||||
|
||||
const regenFlush = () => {
|
||||
regenRafId = null;
|
||||
const bc = regenContentBuffer;
|
||||
const bt = regenThinkingBuffer.length > 0 ? [...regenThinkingBuffer] : null;
|
||||
regenContentBuffer = '';
|
||||
regenThinkingBuffer = [];
|
||||
if (!bc && !bt) return;
|
||||
set((state) => ({
|
||||
messages: state.messages.map(msg => {
|
||||
if (msg.id !== assistantPlaceholder.id) return msg;
|
||||
return {
|
||||
...msg,
|
||||
...(bc ? { content: msg.content + bc } : {}),
|
||||
...(bt ? { thinking: [...(msg.thinking || []), ...bt] } : {}),
|
||||
};
|
||||
}),
|
||||
}));
|
||||
};
|
||||
|
||||
const regenSchedule = () => {
|
||||
if (regenRafId === null) {
|
||||
regenRafId = requestAnimationFrame(regenFlush);
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
await chatAPI.streamMessage(
|
||||
@@ -451,48 +486,32 @@ export const useChatStore = create<ChatStore>((set, get) => ({
|
||||
undefined, // knowledgeBaseIds
|
||||
abortController.signal,
|
||||
(chunk: string) => {
|
||||
// 处理 chunk
|
||||
try {
|
||||
const data = JSON.parse(chunk);
|
||||
if (data.type === 'thinking') {
|
||||
thinkingSteps.push({
|
||||
stage: data.stage,
|
||||
message: data.message,
|
||||
doc_count: data.doc_count,
|
||||
time: data.time
|
||||
});
|
||||
requestAnimationFrame(() => {
|
||||
set((state) => ({
|
||||
messages: state.messages.map(msg =>
|
||||
msg.id === assistantPlaceholder.id
|
||||
? { ...msg, thinking: [...thinkingSteps] }
|
||||
: msg
|
||||
),
|
||||
}));
|
||||
});
|
||||
} else if (data.type === 'chunk') {
|
||||
requestAnimationFrame(() => {
|
||||
set((state) => ({
|
||||
messages: state.messages.map(msg =>
|
||||
msg.id === assistantPlaceholder.id
|
||||
? { ...msg, content: msg.content + data.content }
|
||||
: msg
|
||||
),
|
||||
}));
|
||||
});
|
||||
if (chunk.startsWith('{')) {
|
||||
try {
|
||||
const data = JSON.parse(chunk);
|
||||
if (data.type === 'thinking') {
|
||||
const step = {
|
||||
stage: data.stage,
|
||||
message: data.message,
|
||||
doc_count: data.doc_count,
|
||||
time: data.time
|
||||
};
|
||||
thinkingSteps.push(step);
|
||||
regenThinkingBuffer.push(step);
|
||||
regenSchedule();
|
||||
return;
|
||||
} else if (data.type === 'chunk') {
|
||||
regenContentBuffer += data.content;
|
||||
regenSchedule();
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
// JSON 解析失败,按纯文本处理
|
||||
}
|
||||
} catch {
|
||||
// 纯文本 chunk
|
||||
requestAnimationFrame(() => {
|
||||
set((state) => ({
|
||||
messages: state.messages.map(msg =>
|
||||
msg.id === assistantPlaceholder.id
|
||||
? { ...msg, content: msg.content + chunk }
|
||||
: msg
|
||||
),
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
regenContentBuffer += chunk;
|
||||
regenSchedule();
|
||||
},
|
||||
(_sessionId: number, messageId?: number) => {
|
||||
// onComplete: 只替换 assistant 占位符 ID,不重新加载
|
||||
|
||||
Reference in New Issue
Block a user