1
1
import type { Tiktoken } from '@dqbd/tiktoken'
2
2
import type { Response } from 'express'
3
- import { AzureOpenAI } from 'openai'
4
3
import type { FileSearchTool , ResponseIncludable , ResponseInput , ResponseItemsPage , ResponseStreamEvent } from 'openai/resources/responses/responses'
5
4
import type { Stream } from 'openai/streaming'
6
5
import { z } from 'zod/v4'
7
6
import { validModels } from '../../../config'
8
7
import type { ResponseStreamEventData } from '../../../shared/types'
9
8
import type { APIError , User } from '../../types'
10
- import { AZURE_API_KEY , AZURE_RESOURCE } from '../config'
11
9
import logger from '../logger'
12
10
import { createMockStream } from './mocks/MockStream'
13
11
import { createFileSearchTool } from './util'
14
- import { FileSearchResultsStore } from './fileSearchResultsStore'
15
-
16
- const endpoint = `https://${ AZURE_RESOURCE } .openai.azure.com/`
17
-
18
- export const getAzureOpenAIClient = ( deployment : string ) =>
19
- new AzureOpenAI ( {
20
- apiKey : AZURE_API_KEY ,
21
- deployment,
22
- apiVersion : '2025-03-01-preview' ,
23
- endpoint,
24
- } )
12
+ import { FileSearchResultsStore } from '../../services/azureFileSearch/fileSearchResultsStore'
13
+ import { getAzureOpenAIClient } from './client'
25
14
26
15
const client = getAzureOpenAIClient ( process . env . GPT_4O_MINI ?? '' )
27
16
@@ -38,37 +27,43 @@ export class ResponsesClient {
38
27
temperature : number
39
28
tools : FileSearchTool [ ]
40
29
user : User
30
+ ragIndexId ?: number
41
31
42
32
constructor ( {
43
33
model,
44
34
temperature,
45
35
vectorStoreId,
46
36
instructions,
47
37
user,
38
+ ragIndexId,
48
39
} : {
49
40
model : string
50
41
temperature : number
51
42
vectorStoreId ?: string
52
43
instructions ?: string
53
44
user : User
45
+ ragIndexId ?: number
54
46
} ) {
55
47
const selectedModel = validModels . find ( ( m ) => m . name === model ) ?. deployment
56
48
57
49
if ( ! selectedModel ) throw new Error ( `Invalid model: ${ model } , not one of ${ validModels . map ( ( m ) => m . name ) . join ( ', ' ) } ` )
58
50
59
- const fileSearchTool = vectorStoreId
60
- ? [
61
- createFileSearchTool ( {
62
- vectorStoreId,
63
- } ) ,
64
- ]
65
- : [ ] // needs to retrun empty array for null
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
66
60
67
61
this . model = selectedModel
68
62
this . temperature = temperature
69
63
this . instructions = instructions ?? ''
70
64
this . tools = fileSearchTool
71
65
this . user = user
66
+ this . ragIndexId = ragIndexId
72
67
}
73
68
74
69
async createResponse ( {
@@ -116,7 +111,7 @@ export class ResponsesClient {
116
111
}
117
112
}
118
113
119
- async handleResponse ( { events, encoding, res, ragIndexId } : { events : Stream < ResponseStreamEvent > ; encoding : Tiktoken ; res : Response ; ragIndexId ?: number } ) {
114
+ async handleResponse ( { events, encoding, res } : { events : Stream < ResponseStreamEvent > ; encoding : Tiktoken ; res : Response } ) {
120
115
let tokenCount = 0
121
116
const contents : string [ ] = [ ]
122
117
@@ -150,7 +145,7 @@ export class ResponsesClient {
150
145
151
146
case 'response.output_item.done' : {
152
147
if ( event . item . type === 'file_search_call' ) {
153
- if ( ! ragIndexId ) throw new Error ( 'how is this possible. you managed to invoke file search without ragIndexId' )
148
+ if ( ! this . ragIndexId ) throw new Error ( 'how is this possible. you managed to invoke file search without ragIndexId' )
154
149
155
150
if ( event . item . results ) {
156
151
await FileSearchResultsStore . saveResults ( event . item . id , event . item . results , this . user )
@@ -164,7 +159,7 @@ export class ResponsesClient {
164
159
queries : event . item . queries ,
165
160
status : event . item . status ,
166
161
type : event . item . type ,
167
- ragIndexId,
162
+ ragIndexId : this . ragIndexId ,
168
163
} ,
169
164
} ,
170
165
res ,
0 commit comments