添加(Chat.tsx):引入ProChat组件,更新useChat hook以支持配置项

 添加(useChat.ts):更新streamText函数以使用配置中的参数
  ⬆️ 更新(useStore.ts):更新默认配置项,添加llm模型配置信息
This commit is contained in:
yuruo
2024-06-25 12:59:33 +08:00
parent e5a05f9c0a
commit 27021daafc
10 changed files with 48 additions and 38 deletions

View File

@@ -7,13 +7,13 @@ export const config = {
options: {
initShow: true,
hash: '',
openDevTools: false,
openDevTools: true,
}
},
code: {
id: 1,
id: 0,
options: {
initShow: false,
initShow: true,
width: 1300,
height: 700,
openDevTools: false,
@@ -23,7 +23,7 @@ export const config = {
}
},
config: {
id: 2,
id: 0,
options: {
initShow: true,
width: 600,
@@ -35,7 +35,7 @@ export const config = {
}
},
chat: {
id: 3,
id: 0,
options: {
initShow: true,
width: 600,
@@ -71,5 +71,8 @@ export const getWindowByEvent = (event: IpcMainEvent | IpcMainInvokeEvent) => {
app.whenReady().then(() => {
getWindowByName('search')
// getWindowByName('code')
// getWindowByName('config')
getWindowByName('chat')
})

View File

@@ -1,17 +1,19 @@
import { streamText } from "ai";
import { StreamingTextResponse, streamText } from "ai";
import { createOpenAI } from '@ai-sdk/openai';
import { useStore } from "@renderer/store/useStore";
export default async (messages: any)=> {
const config = useStore(state => state.config)
// todo从配置项中拿数据
const openai = createOpenAI({
baseURL: 'https://api.openai.com/v1',
apiKey: '',
baseURL: config.llm.baseURL,
apiKey: config.llm.apiKey,
});
const res = await streamText({
model: openai('gpt-4-turbo'),
model: openai(config.llm.model),
messages: messages
});
return res.textStream;
return new StreamingTextResponse(res.textStream);
}

View File

@@ -1,4 +1,4 @@
import { ChatMessage, ProChat } from '@ant-design/pro-chat';
import { ProChat } from '@ant-design/pro-chat';
import useChat from '@renderer/hooks/useChat';
export const Chat = () => {
@@ -8,9 +8,9 @@ export const Chat = () => {
'<b>你好我叫智子你的智能Agent助手</b><br><br>你可以输入“/”搜索行为,或者可有什么要求可以随时吩咐!'
}
request={async (messages) => {
const response = await useChat(messages)
const response = await useChat(messages)
// 使用 Message 作为参数发送请求
return response; // 支持流式和非流式
return response// 支持流式和非流式
}}
/>
)

View File

@@ -1,17 +0,0 @@
export default async ({request})=>{
console.log("request", request)
const formData = await request.formData()
console.log("data:", formData)
const data = Object.fromEntries(formData)
const isRegistered = await window.api.shortCut(data.shortCut)
if (isRegistered){
return window.api.sql(`update config set content=@content where id = 1`,
'update',
{
content: JSON.stringify(data)
}
)
}
return {}
}

View File

@@ -1,4 +0,0 @@
export default async ({}) =>{
const config = (await window.api.sql('select * from config where id=1', 'findOne', {})) as ConfigType
return JSON.parse(config.content) as ConfigDataType
}

View File

@@ -1,4 +1,4 @@
import { Form, useLoaderData } from 'react-router-dom'
import { Form } from 'react-router-dom'
import styles from './styles.module.scss'
import { useState } from 'react'
import { useStore } from '@renderer/store/useStore'
@@ -54,6 +54,19 @@ export const Setting = () => {
/>
</section>
<section>
<h5></h5>
<input
type="text"
name="llm-model"
defaultValue={JSON.stringify(config.llm)}
onChange={(e)=>{
const value = JSON.parse(e.target.value)
setConfig({...config, llm: value})
}}
/>
</section>
</main>
</Form>
)

View File

@@ -8,9 +8,14 @@
h5 {
@apply text-xs font-bold mb-2 text-slate-700 opacity-80;
}
h6 {
@apply text-sm mb-2 text-slate-700 opacity-80;
}
@apply border p-3 rounded-md bg-white my-5;
input {
@apply border w-full outline-none;
}
}
}

View File

@@ -12,14 +12,17 @@ import { Welcome } from "@renderer/pages/Welcome";
import ContentListAction from "@renderer/pages/ContentList/ContentListAction";
import CategoryAction from "@renderer/pages/Category/CategoryAction";
import { Setting } from "@renderer/pages/Setting";
import SettingAction from "@renderer/pages/Setting/SettingAction";
import SettingLoader from "@renderer/pages/Setting/SettingLoader";
import { Chat } from "@renderer/pages/Chat/Chat";
const router = createHashRouter([
{
path: "/",
element: <Home />
},
{
path: "chat",
element: <Chat />
},
{
path: "config",
element: <Config />,

View File

@@ -14,7 +14,7 @@ interface StateProps{
setEditCategoryId: (id: number) => void
}
export const useStore = create<StateProps>((set) => ({
config: {shortCut: "alt+d", databaseDirectory: ""},
config: {shortCut: "alt+d", databaseDirectory: "", llm: {model: "gpt-4-turbo", apiKey: "sk-xxx", baseURL: "https://api.openai.com/v1"}},
setConfig: (config) => set({config}),
data: [],
setData: (data) => set({data}),

5
types.d.ts vendored
View File

@@ -25,4 +25,9 @@ type ConfigType = {
type ConfigDataType = {
shortCut: string
databaseDirectory: string
llm: {
model: string
apiKey: string
baseURL: string
}
}