generated from arvinxx/npm-template
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
362 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,4 @@ screenshot | |
example/.temp/* | ||
.eslintcache | ||
techUI* | ||
.vercel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { OpenAIStream, StreamingTextResponse } from 'ai'; | ||
import OpenAI from 'openai'; | ||
|
||
export const config = { | ||
runtime: 'edge', | ||
}; | ||
|
||
export default async (req: Request) => { | ||
const openai = new OpenAI(); | ||
const payload = (await req.json()) as OpenAIChatStreamPayload; | ||
const { messages, ...params } = payload; | ||
|
||
const formatMessages = messages.map((m) => ({ | ||
content: m.content, | ||
name: m.name, | ||
role: m.role, | ||
})); | ||
const response = await openai.chat.completions.create( | ||
{ | ||
messages: formatMessages, | ||
...params, | ||
stream: true, | ||
}, | ||
{ headers: { Accept: '*/*' } }, | ||
); | ||
const stream = OpenAIStream(response); | ||
return new StreamingTextResponse(stream); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "api", | ||
"version": "0.0.0", | ||
"description": "test openai api for pro chat", | ||
"dependencies": { | ||
"ai": "^2", | ||
"openai": "^4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": false, | ||
"declaration": false, | ||
"skipLibCheck": true, | ||
"esModuleInterop": true, | ||
"resolveJsonModule": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { createStyles } from 'antd-style'; | ||
import { memo } from 'react'; | ||
import { Flexbox } from 'react-layout-kit'; | ||
|
||
import ActionIcon from '@/ActionIcon'; | ||
import { ConfigProvider, Popconfirm } from 'antd'; | ||
import { Trash2 } from 'lucide-react'; | ||
|
||
import { useStore } from '../../store'; | ||
|
||
const useStyles = createStyles(({ css, token }) => ({ | ||
extra: css` | ||
color: ${token.colorTextTertiary}; | ||
`, | ||
})); | ||
|
||
export const ActionBar = () => { | ||
const [dispatchMessage] = useStore((s) => [s.dispatchMessage]); | ||
|
||
const { styles, theme } = useStyles(); | ||
|
||
return ( | ||
<ConfigProvider theme={{ token: { colorText: theme.colorTextSecondary } }}> | ||
<Flexbox | ||
align={'center'} | ||
direction={'horizontal-reverse'} | ||
paddingInline={12} | ||
className={styles.extra} | ||
gap={8} | ||
> | ||
<Popconfirm | ||
title={'你即将要清空会话,清空后将无法找回。是否清空当前会话?'} | ||
okButtonProps={{ danger: true }} | ||
okText={'清空会话'} | ||
onConfirm={() => { | ||
dispatchMessage({ type: 'resetMessages' }); | ||
}} | ||
> | ||
<ActionIcon title={'清空当前会话'} icon={Trash2} /> | ||
</Popconfirm> | ||
</Flexbox> | ||
</ConfigProvider> | ||
); | ||
}; | ||
|
||
export default memo(ActionBar); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { SendOutlined } from '@ant-design/icons'; | ||
import { Button, ConfigProvider, Input } from 'antd'; | ||
import { createStyles, useResponsive } from 'antd-style'; | ||
import { memo, useRef, useState } from 'react'; | ||
import { Flexbox } from 'react-layout-kit'; | ||
|
||
import { useStore } from '../../store'; | ||
|
||
import ActionBar from './ActionBar'; | ||
|
||
const useStyles = createStyles(({ css, responsive, token }) => ({ | ||
container: css` | ||
position: sticky; | ||
z-index: ${token.zIndexPopupBase}; | ||
bottom: 0; | ||
padding-top: 12px; | ||
padding-bottom: 24px; | ||
background-image: linear-gradient(to top, ${token.colorBgLayout} 88%, transparent 100%); | ||
${responsive.mobile} { | ||
width: 100%; | ||
} | ||
`, | ||
boxShadow: css` | ||
position: relative; | ||
border-radius: 8px; | ||
box-shadow: ${token.boxShadowSecondary}; | ||
`, | ||
input: css` | ||
width: 100%; | ||
border-radius: 8px; | ||
`, | ||
btn: css` | ||
position: absolute; | ||
z-index: 10; | ||
right: 8px; | ||
bottom: 8px; | ||
color: ${token.colorTextTertiary}; | ||
&:hover { | ||
color: ${token.colorTextSecondary}; | ||
} | ||
`, | ||
extra: css` | ||
color: ${token.colorTextTertiary}; | ||
`, | ||
})); | ||
|
||
export const InputArea = ({}) => { | ||
const [sendMessage, isLoading] = useStore((s) => [s.sendMessage, !!s.chatLoadingId]); | ||
const [message, setMessage] = useState(''); | ||
const isChineseInput = useRef(false); | ||
|
||
const { styles, theme } = useStyles(); | ||
const { mobile } = useResponsive(); | ||
|
||
const send = () => { | ||
sendMessage(message); | ||
setMessage(''); | ||
}; | ||
|
||
return ( | ||
<ConfigProvider | ||
theme={{ | ||
token: { | ||
borderRadius: 4, | ||
colorBgContainer: theme.colorBgElevated, | ||
controlHeightLG: 48, | ||
colorBorder: 'transparent', | ||
colorPrimaryHover: 'transparent', | ||
}, | ||
}} | ||
> | ||
<Flexbox gap={8} padding={16} className={styles.container}> | ||
<ActionBar /> | ||
<Flexbox horizontal gap={8} align={'center'} className={styles.boxShadow}> | ||
<Input.TextArea | ||
size={'large'} | ||
value={message} | ||
placeholder="请输入内容..." | ||
onChange={(e) => { | ||
setMessage(e.target.value); | ||
}} | ||
autoSize={{ maxRows: 8 }} | ||
onCompositionStart={() => { | ||
isChineseInput.current = true; | ||
}} | ||
onCompositionEnd={() => { | ||
isChineseInput.current = false; | ||
}} | ||
className={styles.input} | ||
onPressEnter={(e) => { | ||
if (!isLoading && !e.shiftKey && !isChineseInput.current) { | ||
e.preventDefault(); | ||
send(); | ||
} | ||
}} | ||
/> | ||
{mobile ? null : ( | ||
<Button | ||
loading={isLoading} | ||
type="text" | ||
className={styles.btn} | ||
onClick={() => send()} | ||
icon={<SendOutlined />} | ||
/> | ||
)} | ||
</Flexbox> | ||
</Flexbox> | ||
</ConfigProvider> | ||
); | ||
}; | ||
|
||
export default memo(InputArea); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* compact: true | ||
*/ | ||
import { ChatMessageMap, ProChat } from '@ant-design/pro-chat'; | ||
|
||
import { useTheme } from 'antd-style'; | ||
import { useState } from 'react'; | ||
import { Flexbox } from 'react-layout-kit'; | ||
|
||
export default () => { | ||
const theme = useTheme(); | ||
|
||
const [chats, setChats] = useState<ChatMessageMap>(); | ||
|
||
return ( | ||
<Flexbox style={{ background: theme.colorBgLayout }}> | ||
<ProChat chats={chats} onChatsChange={setChats} /> | ||
</Flexbox> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* compact: true | ||
*/ | ||
import { ProChat } from '@ant-design/pro-chat'; | ||
import { useTheme } from 'antd-style'; | ||
import { Flexbox } from 'react-layout-kit'; | ||
|
||
import { example } from '../mocks/basic'; | ||
import { MockResponse } from '../mocks/streamResponse'; | ||
|
||
export default () => { | ||
const theme = useTheme(); | ||
|
||
return ( | ||
<Flexbox style={{ background: theme.colorBgLayout }}> | ||
<ProChat | ||
request={async (messages) => { | ||
const mockedData: string = `这是一段模拟的流式字符串数据。本次会话传入了${messages.length}条消息`; | ||
const mockResponse = new MockResponse(mockedData); | ||
|
||
return mockResponse.getResponse(); | ||
}} | ||
config={example.config} | ||
/> | ||
</Flexbox> | ||
); | ||
}; |
Oops, something went wrong.