Skip to content

Commit

Permalink
暂时解决 ai 响应的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
solidSpoon committed Oct 7, 2024
1 parent d9b8e84 commit ac31961
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dash-player",
"productName": "DashPlayer",
"version": "4.3.0",
"version": "4.3.1",
"description": "My Electron application description",
"main": ".vite/build/main.js",
"scripts": {
Expand Down
38 changes: 24 additions & 14 deletions src/backend/services/AiFuncs/ai-func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ export default class AiFunc {
public static async run(taskId: number, resultSchema: ZodObject<any>, promptStr: string) {
await RateLimiter.wait('gpt');
const extractionFunctionSchema = {
name: "extractor",
description: "Extracts fields from the input.",
parameters: zodToJsonSchema(resultSchema),
name: 'extractor',
description: 'Extracts fields from the input.',
parameters: zodToJsonSchema(resultSchema)
};
// Instantiate the parser
const parser = new JsonOutputFunctionsParser();
const chat: ChatOpenAI = (await this.getOpenAi(taskId))
const chat: ChatOpenAI = (await this.getOpenAi(taskId));
if (!chat) return;
const runnable = chat.bind({
functions: [extractionFunctionSchema],
function_call: {name: "extractor"},
})
function_call: { name: 'extractor' }
});
const prompt: ChatPromptTemplate = ChatPromptTemplate.fromTemplate(promptStr);

// todo: fix the type
Expand All @@ -68,19 +68,29 @@ export default class AiFunc {
progress: 'AI is analyzing...'
});

const resStream = await chain.stream({});
for await (const chunk of resStream) {
DpTaskService.update({
id: taskId,
status: DpTaskState.IN_PROGRESS,
progress: 'AI responseing',
result: JSON.stringify(chunk)
});
const streamMode = (storeGet('apiKeys.openAi.stream') ?? 'on') === 'on';
let resJson = '';
if (streamMode) {
const resStream = await chain.stream({});
for await (const chunk of resStream) {
resJson = JSON.stringify(chunk);
DpTaskService.update({
id: taskId,
status: DpTaskState.IN_PROGRESS,
progress: 'AI responseing',
result: resJson
});
}
} else {
const tr = await chain.invoke({});
resJson = JSON.stringify(tr);
}

DpTaskService.update({
id: taskId,
status: DpTaskState.DONE,
progress: 'AI has responded',
result: resJson
});
}
}
1 change: 1 addition & 0 deletions src/common/types/store_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const SettingKeyObj = {
'apiKeys.tencent.secretKey': '',
'apiKeys.openAi.key': '',
'apiKeys.openAi.endpoint': '',
'apiKeys.openAi.stream': 'on',
'model.gpt.default': 'gpt-4o-mini',
'appearance.theme': 'light',
'appearance.fontSize': 'fontSizeLarge',
Expand Down
3 changes: 2 additions & 1 deletion src/fronted/components/chat/msg/HumanTopicMsg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const HumanTopicMsg = ({ msg }: { msg: HumanTopicMessage }) => {
const retry = useChatPanel(state => state.retry);
const dpTask = useDpTask(msg.phraseGroupTask, 200);
const updateInternalContext = useChatPanel(s => s.updateInternalContext);
const res = JSON.parse(dpTask?.result ?? '{}') as AiPhraseGroupRes;
console.log('HumanTopicMsg', dpTask);
const res = JSON.parse(strBlank(dpTask?.result) ? '{}': dpTask.result) as AiPhraseGroupRes;
const mapColor = (tags: string[]): string => {
//判空
if (!tags) return 'bg-secondary';
Expand Down
21 changes: 21 additions & 0 deletions src/fronted/pages/setting/OpenAiSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import {
} from '@/fronted/components/ui/dropdown-menu';
import { EllipsisVertical, Eraser, SquarePlus } from 'lucide-react';
import * as React from 'react';
import { Switch } from '@/fronted/components/ui/switch';
import { Label } from '@/fronted/components/ui/label';

const api = window.electron;
const OpenAiSetting = () => {
const { setting, setSettingFunc, submit, eqServer } = useSettingForm([
'apiKeys.openAi.key',
'apiKeys.openAi.endpoint',
'apiKeys.openAi.stream',
'model.gpt.default'
]);

Expand Down Expand Up @@ -76,6 +79,24 @@ const OpenAiSetting = () => {
</DropdownMenuContent>
</DropdownMenu>
</div>
<div className="flex flex-col gap-2 pl-2">
<Label>Streaming Response</Label>
<div className="flex items-center space-x-2">
<Switch id="stream"
checked={setting('apiKeys.openAi.stream') === 'on'}
onCheckedChange={(checked) => {
if (checked) {
setSettingFunc('apiKeys.openAi.stream')('on');
} else {
setSettingFunc('apiKeys.openAi.stream')('off');
}
}}
/>
<Label
className="font-light">观察到有些代理商在启用流式响应时会出现问题,如果你遇到问题,请尝试关闭此选项</Label>
</div>
</div>

<div
className={cn(
'text-sm text-gray-500 mt-2 flex flex-row gap-2'
Expand Down

0 comments on commit ac31961

Please sign in to comment.