Skip to content

Commit

Permalink
fix: sovle the error in edit configration (#485)
Browse files Browse the repository at this point in the history
* chore: rename Peter Cat to PeterCat

* chore: change the logo

* chore: fix style of assistan

* chore: fix style of assistant

* fix: add requestWithToken

* fix: add editBotId for stream_builder

* fix: sovle the error in edit configration
  • Loading branch information
xingwanying authored Nov 18, 2024
1 parent 030a1d2 commit 282c137
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 37 deletions.
2 changes: 1 addition & 1 deletion assistant/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@petercatai/assistant",
"version": "1.0.9",
"version": "1.0.12",
"description": "PeterCat Assistant Application",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
Expand Down
26 changes: 18 additions & 8 deletions assistant/src/Assistant/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,26 @@ npm install @petercatai/assistant

## 使用示例

```tsx
import React from 'react';
```jsx
import React, { useEffect, useState } from 'react';
import { Assistant } from '@petercatai/assistant';

export default () => (
<Assistant
token="36c6b04b-b619-4449-91fa-faf23ab30b80"
clearMessage={true}
/>
);
export default () => {
const [token, setToken] = useState('');

return (
<div>
<a onClick={() => setToken('1234')}>点我变token{token}</a>
<Assistant
editBotId ={token}
apiUrl='/api/chat/stream_builder'
token='0553365a-edb1-435c-b69c-4c645290b86e'
clearMessage={true}
apiUr
/>
</div>
);
};
```
## API

Expand Down
21 changes: 17 additions & 4 deletions assistant/src/Chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ export interface ChatProps extends BotInfo {
drawerWidth?: number;
prompt?: string;
token?: string;
editBotId?: string;
style?: React.CSSProperties;
hideLogo?: boolean;
disabled?: boolean;

disabledPlaceholder?: string;
getToolsResult?: (response: any) => void;
}
Expand All @@ -70,13 +72,23 @@ const Chat: FC<ChatProps> = memo(
disabled = false,
hideLogo = false,
disabledPlaceholder,
editBotId,
getToolsResult,
}) => {
const proChatRef = useRef<ProChatInstance>();
const tokenRef = useRef<string | undefined>(token);
const [chats, setChats] = useState<ChatMessage<Record<string, any>>[]>();
const [complete, setComplete] = useState(false);
useEffect(() => {
tokenRef.current = token;
}, [token]);
const { data: detail } = useSWR(
token ? [`${apiDomain}/api/bot/detail?id=${token}`, token] : null,
tokenRef?.current
? [
`${apiDomain}/api/bot/detail?id=${tokenRef?.current}`,
tokenRef?.current,
]
: null,
fetcher<BotInfo>,
);

Expand Down Expand Up @@ -128,6 +140,7 @@ const Chat: FC<ChatProps> = memo(
}) as Message[];

try {
const token = editBotId || tokenRef?.current;
const response = await streamChat(
newMessages,
apiDomain,
Expand All @@ -146,7 +159,7 @@ const Chat: FC<ChatProps> = memo(
);
}
},
[apiDomain, apiUrl, prompt, token],
[apiDomain, apiUrl, prompt, tokenRef?.current, editBotId],
);

useEffect(() => {
Expand All @@ -165,7 +178,7 @@ const Chat: FC<ChatProps> = memo(
if (proChatRef?.current) {
proChatRef?.current?.clearMessage();
}
}, [token, prompt, proChatRef?.current]);
}, [tokenRef?.current, prompt, proChatRef?.current]);

useEffect(() => {
if (isEmpty(detail)) {
Expand Down Expand Up @@ -429,7 +442,7 @@ const Chat: FC<ChatProps> = memo(
templateId: template_id,
cardData: data,
apiDomain: apiDomain,
token: token!,
token: tokenRef?.current!,
})}
</div>
)}
Expand Down
8 changes: 4 additions & 4 deletions assistant/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1049,10 +1049,6 @@ video:where(.petercat-lui,.petercat-lui *) {
font-size: 14px;
}

.text-\[20px\] {
font-size: 20px;
}

.text-xs {
font-size: 0.75rem;
line-height: 1rem;
Expand Down Expand Up @@ -1218,6 +1214,10 @@ video:where(.petercat-lui,.petercat-lui *) {
line-height: 22px;
}

.ant-pro-chat-list-item-message-content p {
word-break: break-word !important;
}

.ant-pro-chat-chat-list-container {
overflow-y: auto !important;
}
Expand Down
4 changes: 4 additions & 0 deletions assistant/tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
line-height: 22px;
}

.ant-pro-chat-list-item-message-content p {
word-break: break-word !important;
}

.ant-pro-chat-chat-list-container {
overflow-y: auto !important;
}
Expand Down
9 changes: 5 additions & 4 deletions client/.kiwi/en/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export default {
shuRuKaiChangBai: 'Enter Opening Guiding Question',
},
Knowledge: {
zhiShiKuWeiKong: 'Knowledge Base is Empty',
zhiShiKuGengXin: 'Knowledge Base Updating',
zhiShiKuFenDuan: 'Knowledge Base Segmentation',
zhiShiKuWeiKong: 'Knowledge is Empty',
zhiShiKuGengXin: 'Knowledge Updating',
zhiShiKuFenDuan: 'Knowledge Segmentation',
gengXinYu: 'Updated on',
ziFu: 'Characters',
},
Expand All @@ -48,7 +48,8 @@ export default {
},
BotCard: {
guanBi: 'Close',
maShanChuHouJiang: '」? This action cannot be undone, please proceed with caution.',
maShanChuHouJiang:
'」? This action cannot be undone, please proceed with caution.',
queRenYaoShanChu: 'Are you sure you want to delete 「',
gengXinZhiShi: 'Update Knowledge',
gengXinZhiShiKu: 'Update Knowledge Base (Coming Soon)',
Expand Down
1 change: 1 addition & 0 deletions client/app/factory/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export default function Edit() {
backgroundColor: '#fff',
}}
hideLogo={true}
editBotId={botId!}
apiUrl="/api/chat/stream_builder"
apiDomain={API_HOST}
helloMessage={I18N.edit.page.chuCiJianMianXian}
Expand Down
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@fullpage/react-fullpage": "^0.1.42",
"@next/bundle-analyzer": "^13.4.19",
"@nextui-org/react": "^2.2.9",
"@petercatai/assistant": "^1.0.9",
"@petercatai/assistant": "^1.0.12",
"@sentry/nextjs": "^8.28.0",
"@supabase/supabase-js": "^2.32.0",
"@tanstack/react-query": "^5.17.19",
Expand Down
8 changes: 4 additions & 4 deletions client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2927,10 +2927,10 @@
resolved "https://registry.npmjs.org/@panva/hkdf/-/hkdf-1.2.1.tgz#cb0d111ef700136f4580349ff0226bf25c853f23"
integrity sha512-6oclG6Y3PiDFcoyk8srjLfVKyMfVCKJ27JwNPViuXziFpmdz+MZnZN/aKY0JGXgYuO/VghU0jcOAZgWXZ1Dmrw==

"@petercatai/assistant@^1.0.9":
version "1.0.9"
resolved "https://registry.npmjs.org/@petercatai/assistant/-/assistant-1.0.9.tgz#96f2dd4a4d0713627b4a52a5ee61f742e8a2157f"
integrity sha512-mSyF5vwfJ6ucNAVLQvnhsTJd/QuWIRes8GDACrbhlV1iY4mW5RonrPkUL6MtNLb2PSqSxDLEv8beH4UpnJ5Wng==
"@petercatai/assistant@^1.0.12":
version "1.0.12"
resolved "https://registry.npmjs.org/@petercatai/assistant/-/assistant-1.0.12.tgz#5b2f3d56def86568b1859371fededf3137e8342d"
integrity sha512-lbvFtTt5Z7guQ7Z12Enc7oHcdvR5e7/b37GCK78Gp3wX72dwbiAxeYvir1rTovgSnY4juFprjU06WLxQBn6Rug==
dependencies:
"@ant-design/icons" "^5.3.5"
"@ant-design/pro-chat" "^1.9.0"
Expand Down
6 changes: 6 additions & 0 deletions server/agent/prompts/bot_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
- Utilize the edit_bot tool to modify the bot's configuration information based on the user's description.
- Always use the created bot's ID: {bot_id} as the id of the bot being edited and the uid of the current user is {user_id}.
- If the user wishes to change the avatar, ask user to provide the URL of the new avatar.
- If the user wishes to change the bot's name, you need to regenerate the hello message and starters following the following rules:
- Generate 3 corresponding prompt questions and greetings based on the GitHub Repository Name and the language used by the user when interacting with you. For example, the repository name is 'petercat':
The starters array contains questions like: ["Tell me about the project petercat", "Review the contribution guidelines", "How can I quickly get started?"]
- The hello_message is: "👋🏻 Hello, I’m petercat. I'm your personal Q&A bot. I’m here to assist you with any questions about this project. Feel free to ask me anything!"
- The hello_message should start with an introduction of the bot. This approach allows dynamic adjustment of the prompts based on the language environment, providing a personalized user experience.
- Every time you modify the bot's configuration, you must adhere to the user's suggestions and requirements and not make changes without permission.
## Limitations:
Expand Down
20 changes: 9 additions & 11 deletions server/chat/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ def run_qa_chat(
user: Annotated[User | None, Depends(get_user)] = None,
bot: Annotated[Bot | None, Depends(get_bot)] = None,
):
print(
f"run_qa_chat: input_data={input_data}, bot={bot}, llm={bot.llm}"
)
print(f"run_qa_chat: input_data={input_data}, bot={bot}, llm={bot.llm}")

auth_token = Auth.Token(user.access_token) if getattr(user, 'access_token', None) else None
auth_token = (
Auth.Token(user.access_token) if getattr(user, "access_token", None) else None
)
token_usage_recorder = create_token_recorder(user=user, bot=bot)

pipeline = compose(
Expand All @@ -49,11 +49,7 @@ def run_qa_chat(
qa_chat.agent_stream_chat,
)

result = pipeline(
input_data=input_data,
auth_token=auth_token,
bot=bot
)
result = pipeline(input_data=input_data, auth_token=auth_token, bot=bot)

return StreamingResponse(result, media_type="text/event-stream")

Expand All @@ -64,7 +60,9 @@ async def run_issue_helper(
user: Annotated[User | None, Depends(get_user)] = None,
bot: Annotated[Bot | None, Depends(get_bot)] = None,
):
auth_token = Auth.Token(user.access_token) if getattr(user, 'access_token', None) else None
auth_token = (
Auth.Token(user.access_token) if getattr(user, "access_token", None) else None
)
result = await qa_chat.agent_chat(
input_data,
auth_token,
Expand All @@ -80,12 +78,12 @@ async def run_issue_helper(
)
def run_bot_builder(
input_data: ChatData,
bot_id: Optional[str] = None,
user_id: Annotated[str | None, Depends(get_user_id)] = None,
):
if not user_id:
return StreamingResponse(
generate_auth_failed_stream(), media_type="text/event-stream"
)
bot_id = input_data.bot_id
result = bot_builder.agent_stream_chat(input_data, user_id, bot_id)
return StreamingResponse(result, media_type="text/event-stream")

0 comments on commit 282c137

Please sign in to comment.