Skip to content

Commit

Permalink
Update rearanker code url. Add chat storage ip address (#717)
Browse files Browse the repository at this point in the history
* save chat origin ip

* reranker code url
  • Loading branch information
c121914yu authored Jan 9, 2024
1 parent 13eda40 commit 5876a47
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docSite/content/docs/development/custom-models/reranker.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ weight: 910
### 源码部署

1. 根据上面的环境配置配置好环境,具体教程自行 GPT;
2. 下载 [python 文件](app.py)
2. 下载 [python 文件](https://github.com/labring/FastGPT/tree/main/python/reranker/bge-reranker-base)
3. 在命令行输入命令 `pip install -r requirments.txt`
4. 按照[https://huggingface.co/BAAI/bge-reranker-base](https://huggingface.co/BAAI/bge-reranker-base)下载模型仓库到app.py同级目录
5. 添加环境变量 `export ACCESS_TOKEN=XXXXXX` 配置 token,这里的 token 只是加一层验证,防止接口被人盗用,默认值为 `ACCESS_TOKEN`
Expand Down
1 change: 1 addition & 0 deletions packages/global/core/chat/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type ChatSchema = {
shareId?: string;
outLinkUid?: string;
content: ChatItemType[];
metadata?: Record<string, any>;
};

export type ChatWithAppSchema = Omit<ChatSchema, 'appId'> & {
Expand Down
20 changes: 0 additions & 20 deletions packages/service/core/chat/chatSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,6 @@ const ChatSchema = new Schema({
//For special storage
type: Object,
default: {}
},
content: {
type: [
{
obj: {
type: String,
required: true,
enum: Object.keys(ChatRoleMap)
},
value: {
type: String,
default: ''
},
[ModuleOutputKeyEnum.responseData]: {
type: Array,
default: []
}
}
],
default: []
}
});

Expand Down
9 changes: 7 additions & 2 deletions projects/app/src/pages/api/v1/chat/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
} = req.body as Props;

try {
const originIp = requestIp.getClientIp(req);

await connectToDatabase();
// body data check
if (!messages) {
Expand Down Expand Up @@ -99,7 +101,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
if (shareId && outLinkUid) {
const { user, appId, authType, responseDetail, uid } = await authOutLinkChatStart({
shareId,
ip: requestIp.getClientIp(req),
ip: originIp,
outLinkUid,
question: question.value
});
Expand Down Expand Up @@ -245,7 +247,10 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
value: answerText,
responseData
}
]
],
metadata: {
originIp
}
});
}

Expand Down
18 changes: 14 additions & 4 deletions projects/app/src/service/utils/chat/saveChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Props = {
shareId?: string;
outLinkUid?: string;
content: [ChatItemType, ChatItemType];
metadata?: Record<string, any>;
};

export async function saveChat({
Expand All @@ -29,7 +30,8 @@ export async function saveChat({
source,
shareId,
outLinkUid,
content
content,
metadata = {}
}: Props) {
try {
const chat = await MongoChat.findOne(
Expand All @@ -39,9 +41,14 @@ export async function saveChat({
tmbId,
appId
},
'_id'
'_id metadata'
);

const metadataUpdate = {
...chat?.metadata,
...metadata
};

const promise: any[] = [
MongoChatItem.insertMany(
content.map((item) => ({
Expand All @@ -53,6 +60,7 @@ export async function saveChat({
}))
)
];
console.log(metadataUpdate);

const title =
chatContentReplaceBlock(content[0].value).slice(0, 20) ||
Expand All @@ -65,7 +73,8 @@ export async function saveChat({
{ chatId },
{
title,
updateTime: new Date()
updateTime: new Date(),
metadata: metadataUpdate
}
)
);
Expand All @@ -80,7 +89,8 @@ export async function saveChat({
title,
source,
shareId,
outLinkUid
outLinkUid,
metadata: metadataUpdate
})
);
}
Expand Down

0 comments on commit 5876a47

Please sign in to comment.