Skip to content

Commit af048e7

Browse files
committed
Fixs to new vector store setup
1 parent 3527248 commit af048e7

File tree

6 files changed

+35
-32
lines changed

6 files changed

+35
-32
lines changed

src/client/components/ChatV2/ChatV2.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export const ChatV2 = () => {
228228
const { tokenUsageAnalysis, stream } = await getCompletionStream({
229229
assistantInstructions: assistantInstructions.content,
230230
messages: newMessages,
231-
ragIndexId: ragIndexId ?? undefined,
231+
ragIndexId,
232232
model: activeModel.name,
233233
formData,
234234
modelTemperature: modelTemperature.value,

src/server/routes/openai.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,36 +137,38 @@ openaiRouter.post('/stream/v2', upload.single('file'), async (r, res) => {
137137
}
138138

139139
// Check rag index
140-
let vectorStoreId: string | undefined
141140
let instructions: string | undefined = options.assistantInstructions
141+
let ragIndex: RagIndex | undefined
142142

143143
if (ragIndexId) {
144144
if (!courseId && !user.isAdmin) {
145145
logger.error('User is not admin and trying to access non-course rag')
146146
throw ApplicationError.Forbidden('User is not admin and trying to access non-course rag')
147147
}
148148

149-
const ragIndex = await RagIndex.findByPk(ragIndexId, {
150-
include: {
151-
model: ChatInstance,
152-
as: 'chatInstances',
153-
where: courseId ? { courseId } : {},
154-
},
155-
})
149+
ragIndex =
150+
(await RagIndex.findByPk(ragIndexId, {
151+
include: {
152+
model: ChatInstance,
153+
as: 'chatInstances',
154+
where: courseId ? { courseId } : {},
155+
},
156+
})) ?? undefined
156157
if (ragIndex) {
157-
vectorStoreId = ragIndex.metadata.azureVectorStoreId
158158
instructions = `${instructions} ${ragIndex.metadata.instructions ?? DEFAULT_RAG_SYSTEM_PROMPT}`
159-
} else {
159+
}
160+
if (!ragIndex) {
160161
logger.error('RagIndex not found', { ragIndexId })
161162
res.status(404).send('RagIndex not found')
162163
return
163164
}
164165
}
165166

167+
console.log('ragIndex', ragIndexId, ragIndex)
168+
166169
const responsesClient = new ResponsesClient({
167170
model: options.model,
168-
vectorStoreId,
169-
ragIndexId,
171+
ragIndex,
170172
instructions,
171173
temperature: options.modelTemperature,
172174
user,

src/server/routes/rag/rag.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getAzureOpenAIClient } from '../../util/azure/client'
88
import { TEST_COURSES } from '../../util/config'
99
import ragIndexRouter, { ragIndexMiddleware } from './ragIndex'
1010
import { getPrimaryVectorStoreId } from '../../services/azureFileSearch/vectorStore'
11+
import { randomUUID } from 'node:crypto'
1112

1213
const router = Router()
1314

@@ -60,6 +61,7 @@ router.post('/indices', async (req, res) => {
6061
name,
6162
dim,
6263
azureVectorStoreId: vectorStoreId,
64+
ragIndexFilterValue: randomUUID(),
6365
},
6466
})
6567

src/server/routes/rag/ragIndex.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,12 @@ ragIndexRouter.post('/upload', [indexUploadDirMiddleware, uploadMiddleware], asy
237237

238238
const uploadedFile = await client.files.create({
239239
file: stream,
240-
purpose: 'user_data',
240+
purpose: 'assistants',
241241
})
242242
const vectorStoreFile = await client.vectorStores.files.create(ragIndex.metadata.azureVectorStoreId, {
243243
file_id: uploadedFile.id,
244244
attributes: {
245-
ragIndexId: ragIndex.id,
245+
ragIndexFilter: ragIndex.metadata.ragIndexFilterValue,
246246
},
247247
})
248248

src/server/util/azure/ResponsesAPI.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { createMockStream } from './mocks/MockStream'
1111
import { createFileSearchTool } from './util'
1212
import { FileSearchResultsStore } from '../../services/azureFileSearch/fileSearchResultsStore'
1313
import { getAzureOpenAIClient } from './client'
14+
import { RagIndex } from '../../db/models'
1415

1516
const client = getAzureOpenAIClient(process.env.GPT_4O_MINI ?? '')
1617

@@ -27,43 +28,40 @@ export class ResponsesClient {
2728
temperature: number
2829
tools: FileSearchTool[]
2930
user: User
30-
ragIndexId?: number
31+
ragIndex?: RagIndex
3132

3233
constructor({
3334
model,
3435
temperature,
35-
vectorStoreId,
3636
instructions,
3737
user,
38-
ragIndexId,
38+
ragIndex,
3939
}: {
4040
model: string
4141
temperature: number
42-
vectorStoreId?: string
4342
instructions?: string
4443
user: User
45-
ragIndexId?: number
44+
ragIndex?: RagIndex
4645
}) {
4746
const selectedModel = validModels.find((m) => m.name === model)?.deployment
4847

4948
if (!selectedModel) throw new Error(`Invalid model: ${model}, not one of ${validModels.map((m) => m.name).join(', ')}`)
5049

51-
const fileSearchTool =
52-
vectorStoreId && ragIndexId
53-
? [
54-
createFileSearchTool({
55-
vectorStoreId,
56-
filters: { key: 'ragIndexId', value: ragIndexId, type: 'eq' },
57-
}),
58-
]
59-
: [] // needs to retrun empty array for null
50+
const fileSearchTool = ragIndex
51+
? [
52+
createFileSearchTool({
53+
vectorStoreId: ragIndex.metadata.azureVectorStoreId,
54+
filters: { key: 'ragIndexFilterValue', value: ragIndex.metadata.ragIndexFilterValue, type: 'eq' },
55+
}),
56+
]
57+
: [] // needs to retrun empty array for null
6058

6159
this.model = selectedModel
6260
this.temperature = temperature
6361
this.instructions = instructions ?? ''
6462
this.tools = fileSearchTool
6563
this.user = user
66-
this.ragIndexId = ragIndexId
64+
this.ragIndex = ragIndex
6765
}
6866

6967
async createResponse({
@@ -145,7 +143,7 @@ export class ResponsesClient {
145143

146144
case 'response.output_item.done': {
147145
if (event.item.type === 'file_search_call') {
148-
if (!this.ragIndexId) throw new Error('how is this possible. you managed to invoke file search without ragIndexId')
146+
if (!this.ragIndex) throw new Error('how is this possible. you managed to invoke file search without ragIndexId')
149147

150148
if (event.item.results) {
151149
await FileSearchResultsStore.saveResults(event.item.id, event.item.results, this.user)
@@ -159,7 +157,7 @@ export class ResponsesClient {
159157
queries: event.item.queries,
160158
status: event.item.status,
161159
type: event.item.type,
162-
ragIndexId: this.ragIndexId,
160+
ragIndexId: this.ragIndex.id,
163161
},
164162
},
165163
res,

src/shared/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type RagIndexMetadata = {
66
name: string
77
dim?: number
88
azureVectorStoreId: string
9+
ragIndexFilterValue: string
910
instructions?: string
1011
}
1112

0 commit comments

Comments
 (0)