-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into feat/ai-detail-answ…
…er-mode
- Loading branch information
Showing
13 changed files
with
131 additions
and
20 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
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
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
29 changes: 29 additions & 0 deletions
29
apps/app/src/features/openai/server/services/replace-annotation-with-page-link.ts
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,29 @@ | ||
// See: https://platform.openai.com/docs/assistants/tools/file-search#step-5-create-a-run-and-check-the-output | ||
|
||
import type { IPageHasId, Lang } from '@growi/core/dist/interfaces'; | ||
import type { MessageContentDelta } from 'openai/resources/beta/threads/messages.mjs'; | ||
|
||
import VectorStoreFileRelationModel from '~/features/openai/server/models/vector-store-file-relation'; | ||
import { getTranslation } from '~/server/service/i18next'; | ||
|
||
export const replaceAnnotationWithPageLink = async(messageContentDelta: MessageContentDelta, lang?: Lang): Promise<void> => { | ||
if (messageContentDelta?.type === 'text' && messageContentDelta?.text?.annotations != null) { | ||
const annotations = messageContentDelta?.text?.annotations; | ||
for await (const annotation of annotations) { | ||
if (annotation.type === 'file_citation' && annotation.text != null) { | ||
|
||
const vectorStoreFileRelation = await VectorStoreFileRelationModel | ||
.findOne({ fileIds: { $in: [annotation.file_citation?.file_id] } }) | ||
.populate<{page: Pick<IPageHasId, 'path' | '_id'>}>('page', 'path'); | ||
|
||
if (vectorStoreFileRelation != null) { | ||
const { t } = await getTranslation(lang); | ||
messageContentDelta.text.value = messageContentDelta.text.value?.replace( | ||
annotation.text, | ||
` [${t('source')}: [${vectorStoreFileRelation.page.path}](/${vectorStoreFileRelation.page._id})]`, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
}; |
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
48 changes: 48 additions & 0 deletions
48
apps/app/src/migrations/20241107172359-rename-pageId-to-page.js
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,48 @@ | ||
import mongoose from 'mongoose'; | ||
|
||
import VectorStoreFileRelationModel from '~/features/openai/server/models/vector-store-file-relation'; | ||
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils'; | ||
import loggerFactory from '~/utils/logger'; | ||
|
||
|
||
const logger = loggerFactory('growi:migrate:rename-pageId-to-page'); | ||
|
||
async function dropIndexIfExists(db, collectionName, indexName) { | ||
// check existence of the collection | ||
const items = await db.listCollections({ name: collectionName }, { nameOnly: true }).toArray(); | ||
if (items.length === 0) { | ||
return; | ||
} | ||
|
||
const collection = await db.collection(collectionName); | ||
if (await collection.indexExists(indexName)) { | ||
await collection.dropIndex(indexName); | ||
} | ||
} | ||
|
||
module.exports = { | ||
async up(db) { | ||
logger.info('Apply migration'); | ||
await mongoose.connect(getMongoUri(), mongoOptions); | ||
|
||
// Drop index | ||
await dropIndexIfExists(db, 'vectorstorefilerelations', 'vectorStoreRelationId_1_pageId_1'); | ||
|
||
// Rename field (pageId -> page) | ||
await VectorStoreFileRelationModel.updateMany( | ||
{}, | ||
[ | ||
{ $set: { page: '$pageId' } }, | ||
{ $unset: ['pageId'] }, | ||
], | ||
); | ||
|
||
// Create index | ||
const collection = mongoose.connection.collection('vectorstorefilerelations'); | ||
await collection.createIndex({ vectorStoreRelationId: 1, page: 1 }, { unique: true }); | ||
}, | ||
|
||
async down() { | ||
// No rollback | ||
}, | ||
}; |