mirror of
https://github.com/yuruotong1/autoMate.git
synced 2025-12-26 05:16:21 +08:00
代码生成时增加按钮
This commit is contained in:
parent
465ed79d47
commit
0c66c44982
12
app/package-lock.json
generated
12
app/package-lock.json
generated
@ -1,16 +1,16 @@
|
||||
{
|
||||
"name": "automate",
|
||||
"version": "1.0.0",
|
||||
"version": "2.2.2",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "automate",
|
||||
"version": "1.0.0",
|
||||
"version": "2.2.2",
|
||||
"hasInstallScript": true,
|
||||
"dependencies": {
|
||||
"@ai-sdk/openai": "^0.0.33",
|
||||
"@ant-design/pro-chat": "^1.14.2",
|
||||
"@ant-design/pro-chat": "^1.15.0",
|
||||
"@ant-design/pro-editor": "^1.2.1",
|
||||
"@codemirror/lang-python": "^6.1.6",
|
||||
"@electron-toolkit/preload": "^3.0.0",
|
||||
@ -312,9 +312,9 @@
|
||||
"integrity": "sha512-vHbT+zJEVzllwP+CM+ul7reTEfBR0vgxFe7+lREAsAA7YGsYpboiq2sQNeQeRvh09GfQgs/GyFEvZpJ9cLXpXA=="
|
||||
},
|
||||
"node_modules/@ant-design/pro-chat": {
|
||||
"version": "1.14.2",
|
||||
"resolved": "https://registry.npmjs.org/@ant-design/pro-chat/-/pro-chat-1.14.2.tgz",
|
||||
"integrity": "sha512-9pq++mXtXr5GsqNSpwEDoGXTSbdvXGRX0l7MFHz0Llt9ABqUtsSVkE4TNTY+fqw4+rcyKbG/QSByaQEgcz4i+w==",
|
||||
"version": "1.15.1",
|
||||
"resolved": "https://registry.npmjs.org/@ant-design/pro-chat/-/pro-chat-1.15.1.tgz",
|
||||
"integrity": "sha512-2rUdM5J21eLlVeXIoN1hEx7usYrtn7+yH8QPWukIHA6gx9Ayq8AFM0WbzDgycEJfRBj5I2FuWpMBXJAv4cEF4w==",
|
||||
"dependencies": {
|
||||
"@ant-design/icons": "^5.3.7",
|
||||
"@ant-design/pro-editor": "latest",
|
||||
|
||||
@ -23,7 +23,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@ai-sdk/openai": "^0.0.33",
|
||||
"@ant-design/pro-chat": "^1.14.2",
|
||||
"@ant-design/pro-chat": "^1.15.0",
|
||||
"@ant-design/pro-editor": "^1.2.1",
|
||||
"@codemirror/lang-python": "^6.1.6",
|
||||
"@electron-toolkit/preload": "^3.0.0",
|
||||
|
||||
@ -18,7 +18,7 @@ export const config = {
|
||||
initShow: true,
|
||||
width: 1300,
|
||||
height: 700,
|
||||
openDevTools: false,
|
||||
openDevTools: true,
|
||||
frame: true,
|
||||
transparent: false,
|
||||
hash: '/#config/category/contentList'
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import { ProChat, ProChatInstance } from '@ant-design/pro-chat';
|
||||
import useChat from '@renderer/hooks/useChat';
|
||||
import { useStore } from '@renderer/store/useStore';
|
||||
import { Button } from 'antd';
|
||||
import { useTheme } from 'antd-style';
|
||||
import { useEffect, useRef } from 'react';
|
||||
export default function Chat(props: {id: number, revalidator: () => void, search: string}) {
|
||||
@ -28,6 +29,31 @@ export default function Chat(props: {id: number, revalidator: () => void, search
|
||||
console.log('chat', chat)
|
||||
setMessages(chat)
|
||||
}}
|
||||
chatItemRenderConfig={{
|
||||
contentRender: (props, defaultDom) => {
|
||||
if (props.originData?.role === 'coder') {
|
||||
try {
|
||||
// const resJson = JSON.parse(item?.originData?.content);
|
||||
return (<div className='flex flex-row'>
|
||||
<Button onClick={()=>{
|
||||
console.log('运行')
|
||||
}}>
|
||||
运行
|
||||
</Button>
|
||||
<Button className='ml-2' onClick={()=>{
|
||||
console.log('应用')
|
||||
}}>
|
||||
应用
|
||||
</Button>
|
||||
</div>)
|
||||
|
||||
} catch (error) {
|
||||
return defaultDom;
|
||||
}
|
||||
}
|
||||
return defaultDom;
|
||||
}
|
||||
}}
|
||||
chatRef={proChatRef}
|
||||
style={{ background: theme.colorBgLayout }}
|
||||
// assistantMeta={{ avatar: '', title: '智子', backgroundColor: '#67dedd' }}
|
||||
@ -36,10 +62,31 @@ export default function Chat(props: {id: number, revalidator: () => void, search
|
||||
}
|
||||
|
||||
request={async (messages) => {
|
||||
const response = await getResponse(messages)
|
||||
return new Response(response.content)// 支持流式和非流式
|
||||
// const response = await getResponse(messages)
|
||||
// if (response.isExistCode === 0) {
|
||||
// setTimeout(() => {
|
||||
// proChatRef.current?.sendMessage({
|
||||
// type: 'text',
|
||||
// content: response.content,
|
||||
// role: 'coder',
|
||||
// originData: response
|
||||
// });
|
||||
// }, 1000); // 延时1秒推送消息
|
||||
|
||||
// }
|
||||
|
||||
// return new Response(response.content)// 支持流式和非流式
|
||||
setTimeout(() => {
|
||||
proChatRef.current?.pushChat({
|
||||
type: 'text',
|
||||
content: "hello",
|
||||
role: 'coder',
|
||||
originData: "dd"
|
||||
});
|
||||
}, 1000); // 延时1秒推送消息
|
||||
return new Response("hello")
|
||||
}}
|
||||
/>
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
@ -4,7 +4,6 @@ import { Drawer, FloatButton, Spin } from 'antd';
|
||||
import { QuestionCircleOutlined } from '@ant-design/icons';
|
||||
import Chat from '@renderer/components/Chat';
|
||||
import "./codeEditor.scss"
|
||||
import { useStore } from '@renderer/store/useStore';
|
||||
import { useEffect } from 'react';
|
||||
interface CodeEditorProps {
|
||||
id: number;
|
||||
@ -16,7 +15,6 @@ interface CodeEditorProps {
|
||||
}
|
||||
export default function CodeEditor(props: CodeEditorProps) {
|
||||
const { id, defaultValue, revalidator, open, setOpen, search} = props;
|
||||
const isCodeLoading = useStore(state => state.isCodeLoading)
|
||||
useEffect(()=>{
|
||||
if (search) {
|
||||
setOpen(true)
|
||||
@ -40,7 +38,6 @@ export default function CodeEditor(props: CodeEditorProps) {
|
||||
}}>
|
||||
<Chat id={id} revalidator={revalidator} search={search}/>
|
||||
</Drawer>
|
||||
<Spin spinning={isCodeLoading} tip="根据自动化方案生成代码中,请稍等...">
|
||||
<CodeMirror
|
||||
maxHeight='550px'
|
||||
maxWidth='850px'
|
||||
@ -58,7 +55,6 @@ export default function CodeEditor(props: CodeEditorProps) {
|
||||
}}
|
||||
extensions={[python()]}
|
||||
/>
|
||||
</Spin>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
@ -13,8 +13,7 @@ interface StateProps{
|
||||
setEditCategoryId: (id: number) => void,
|
||||
chatMessages: ChatMessage<Record<string, any>>[],
|
||||
setChatMessage: (chatMessage: ChatMessage<Record<string, any>>[]) => void,
|
||||
isCodeLoading: boolean,
|
||||
setIsCodeLoading: (isCodeLoading: boolean) => void
|
||||
|
||||
}
|
||||
export const useStore = create<StateProps>((set) => ({
|
||||
data: [],
|
||||
@ -29,8 +28,6 @@ export const useStore = create<StateProps>((set) => ({
|
||||
setEditCategoryId: (editCategoryId) => set({editCategoryId}),
|
||||
chatMessages: [],
|
||||
setChatMessage: (chatMessages) => set({chatMessages}),
|
||||
isCodeLoading: false,
|
||||
setIsCodeLoading: (isCodeLoading) => set({isCodeLoading})
|
||||
}))
|
||||
|
||||
|
||||
|
||||
@ -3,7 +3,10 @@ code_prompt=string.Template("""# 背景 #
|
||||
你是一位资深的python程序员,根据用户的需求编写python代码。
|
||||
#############
|
||||
# 目标 #
|
||||
你需要根据用户需求编写新的代码,如果对话的上下文中有代码,你需要在已有的代码之上进行修改。生成代码时请一定要遵守以下规则否则会出错!
|
||||
你需要根据用户需求编写新的代码,如果对话的上下文中有代码则需要在已有的代码之上进行修改,如果有不清楚的地方要和用户沟通不要自己瞎猜。
|
||||
#############
|
||||
# 约束 #
|
||||
生成代码时请一定要遵守以下规则否则会出错!
|
||||
1. 代码中只能使用官方内置库和以下依赖库:
|
||||
selenium、python-docx、requests
|
||||
2. 以下是我封装好的函数,如果有需要可以直接在代码中使用,无需import:
|
||||
@ -13,11 +16,11 @@ selenium、python-docx、requests
|
||||
遵循PEP8规范,每一行代码都要用编写注释并且在关键地方给出修改建议。
|
||||
############
|
||||
# 回复格式 #
|
||||
只回复python代码
|
||||
回复markdown格式,如果需要代码则用```代码```
|
||||
#############
|
||||
# 返回例子 #
|
||||
1. print("abc")
|
||||
2. c = [i in range(10)]\nprint(c)
|
||||
1. ```print("abc")```
|
||||
2. ```c = [i in range(10)]\nprint(c)```
|
||||
#############
|
||||
""")
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@ from litellm import completion
|
||||
from utils.sql_util import get_config
|
||||
from agent.prompt import code_prompt
|
||||
import json
|
||||
|
||||
import re
|
||||
home_bp = Blueprint('llm', __name__)
|
||||
|
||||
@home_bp.route('/llm', methods=["POST"])
|
||||
@ -25,7 +25,22 @@ def llm():
|
||||
return Response(generate(), mimetype='text/event-stream')
|
||||
else:
|
||||
try:
|
||||
res = completion(messages=messages, **config)
|
||||
return {"content": res.choices[0].message.content, "status": 0}
|
||||
res = completion(messages=messages, **config).choices[0].message.content
|
||||
return {"content": res, "isExistCode": contains_code(res), "status": 0}
|
||||
except Exception as e:
|
||||
return {"content": str(e), "status": 1}
|
||||
|
||||
|
||||
def contains_code(text):
|
||||
markdown_patterns = [
|
||||
r'```.*?```',
|
||||
r'```[\s\S]*?```'
|
||||
]
|
||||
for pattern in markdown_patterns:
|
||||
if re.search(pattern, text, re.MULTILINE):
|
||||
return 0
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(contains_code("为了帮助你打开并读取位于桌面上的 `a.txt` 文件,以下是相应的Python代码。请确保根据你的系统环境(如Windows或Mac OS),调整文件路径。\\n\\n```python\\n# 打开并读取桌面上的 a.txt 文件\\ntry:\\n with open('/Users/your_username/Desktop/a.txt', 'r') as file: # 请根据你的系统路径修改文件路径\\n content = file.read() # 读取文件\\n print(content) # 显示文件内容\\nexcept FileNotFoundError:\\n print(\\\"文件没有找到,请确保文件路径正确。\\\")\\nexcept Exception as e:\\n print(\\\"读取文件时发生错误:\\\", e)\\n```\\n\\n请将 `/Users/your_username/Desktop/a.txt` 中的 `your_username` 替换为你的用户名称。如果你是Windows用户,路径可能类似于 `C:\\\\\\\\Users\\\\\\\\your_username\\\\\\\\Desktop\\\\\\\\a.txt`。"))
|
||||
Loading…
x
Reference in New Issue
Block a user