Skip to content

Commit 4f9351e

Browse files
committed
clean up for responses api: update context state management
1 parent fa992b9 commit 4f9351e

File tree

4 files changed

+122
-87
lines changed

4 files changed

+122
-87
lines changed

src/server/util/azure/clientV2.ts

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ import logger from '../logger'
88

99
import { APIError } from '../../types'
1010
import { AzureOpenAI } from 'openai'
11+
1112
// import { EventStream } from '@azure/openai'
1213
import { Stream } from 'openai/streaming'
1314
import {
15+
FileSearchTool,
1416
FunctionTool,
1517
ResponseInput,
1618
ResponseInputItem,
1719
ResponseStreamEvent,
1820
} from 'openai/resources/responses/responses'
1921

20-
import { testTool } from './tools'
22+
// import { ohtuRAGTest } from './functionTools'
23+
import { fileSearchTest } from './fileSearchTools'
2124

2225
const endpoint = `https://${AZURE_RESOURCE}.openai.azure.com/`
2326

@@ -34,7 +37,7 @@ const client = getAzureOpenAIClient(process.env.GPT_4O)
3437
export class ResponsesClient {
3538
model: string
3639
instructions: string
37-
tools: FunctionTool[]
40+
tools: (FunctionTool | FileSearchTool)[]
3841

3942
constructor(model: string, instructions?: string) {
4043
const deploymentId = validModels.find((m) => m.name === model)?.deployment
@@ -45,8 +48,13 @@ export class ResponsesClient {
4548
)
4649

4750
this.model = deploymentId
48-
this.instructions = instructions || 'Olet avulias apuri.'
49-
this.tools = [testTool.definition]
51+
this.instructions =
52+
instructions ||
53+
'Olet ohjelmistotuotanto kurssin avustaja. Jos käyttäjä kysyy jotain, niin arvioi ensin liittyykö se ohjelmistotuotannon kurssiin. Jos liittyy, niin toteuta file_search. jos et löydä sopivia tiedostoja, niin sano että haulla ei löytynyt mitään. Jos käyttäjän viesti ei liittynyt ohjelmistotuotannon kurssiin, niin kysy ystävällisesti voitko auttaa jotenkin muuten kurssimateriaalien suhteen.'
54+
this.tools = [
55+
// ohtuRAGTest.definition,
56+
fileSearchTest.definition,
57+
]
5058
}
5159

5260
async createResponse({
@@ -57,10 +65,12 @@ export class ResponsesClient {
5765
try {
5866
return await client.responses.create({
5967
model: this.model,
68+
// previous_response_id=response.id // THIS MIGHT BE IT!!!!!!1
6069
instructions: this.instructions,
6170
input,
6271
stream: true,
6372
tools: this.tools,
73+
tool_choice: 'auto',
6474
})
6575
} catch (error: any) {
6676
logger.error(error)
@@ -75,7 +85,7 @@ export class ResponsesClient {
7585
encoding,
7686
res,
7787
}: {
78-
events: Stream<any>
88+
events: Stream<ResponseStreamEvent>
7989
prevMessages: ResponseInput
8090
encoding: Tiktoken
8191
res: Response
@@ -94,27 +104,20 @@ export class ResponsesClient {
94104
tokenCount += encoding.encode(event.delta).length ?? 0
95105
break
96106

107+
case 'response.file_search_call.completed':
108+
console.log('file search completed')
109+
break
110+
111+
case 'response.output_item.done':
112+
console.log('OUTPUT_ITEM DONE???', JSON.stringify(event, null, 2))
113+
break
114+
115+
case 'response.output_text.annotation.added':
116+
console.log('ANNOTATIONS ADDED', JSON.stringify(event, null, 2))
117+
break
118+
97119
case 'response.function_call_arguments.done':
98-
// WORK IN PROGRESS
99-
100-
// const augRetrieval = await this.callToolFunction(
101-
// event.arguments,
102-
// event.call_id
103-
// )
104-
// const newEvents = await this.createResponse({
105-
// input: [...prevMessages, augRetrieval],
106-
// })
107-
108-
// if (isError(events)) {
109-
// throw new Error(`Error creating response from function call`)
110-
// }
111-
112-
// await this.handleResponse({
113-
// events: newEvents as Stream<ResponseStreamEvent>,
114-
// prevMessages: [...prevMessages, augRetrieval],
115-
// encoding,
116-
// res,
117-
// })
120+
// Listen to file_search instead
118121
break
119122
}
120123
}
@@ -141,29 +144,4 @@ export class ResponsesClient {
141144
}
142145
})
143146
}
144-
145-
private async callToolFunction(
146-
args: string,
147-
callId: string
148-
): Promise<ResponseInputItem[]> {
149-
const { query } = JSON.parse(args)
150-
try {
151-
const retrieval = await testTool.function(query)
152-
153-
return [
154-
{
155-
role: 'user',
156-
content: retrieval.query,
157-
},
158-
{
159-
type: 'function_call_output',
160-
call_id: callId,
161-
output: retrieval.result,
162-
},
163-
]
164-
} catch (error) {
165-
logger.error('Error calling tool function:', error)
166-
return null
167-
}
168-
}
169147
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { FileSearchTool } from 'openai/resources/responses/responses'
2+
3+
interface FileSearchObject {
4+
definition: FileSearchTool
5+
}
6+
7+
export const fileSearchTest: FileSearchObject = {
8+
definition: {
9+
type: 'file_search',
10+
vector_store_ids: ['vs_Lsyd0uMbgeT8lS9pnxZQEl3c'], // vector store ID for ohtu-test
11+
max_num_results: 5,
12+
// filters: "",
13+
// ranking_options: ""
14+
},
15+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { AzureOpenAI } from 'openai'
2+
import { FunctionTool } from 'openai/resources/responses/responses'
3+
4+
interface functionCallObject {
5+
definition: FunctionTool
6+
function: (client: AzureOpenAI, query: string) => Promise<any>
7+
}
8+
9+
export const functionCallTest: functionCallObject = {
10+
definition: {
11+
type: 'function',
12+
name: 'test_knowledge_retrieval',
13+
description:
14+
'Test tool for knowledge retrieval. Always call this when user says TEST-RAG',
15+
parameters: {
16+
type: 'object',
17+
properties: {
18+
query: {
19+
type: 'string',
20+
description: 'Users query for knowledge retrieval',
21+
},
22+
},
23+
required: ['query'],
24+
additionalProperties: false,
25+
},
26+
strict: true, // or true, depending on your requirements
27+
},
28+
function: async (
29+
client: AzureOpenAI,
30+
query: string
31+
): Promise<{ query: string; result: string }> => {
32+
// Simulate a tool function that returns a simple message
33+
return {
34+
query,
35+
result:
36+
'This is a test result from the test tool. The secret is: Chili kastike',
37+
}
38+
},
39+
}
40+
41+
export const ohtuRAGTest: functionCallObject = {
42+
// FUNCITON TOOL CALL FOR VECTOR DB /search ENDPOINT IS NOT CURRENTLY SUPPORTED BY OPENAI
43+
definition: {
44+
type: 'function',
45+
name: 'ohtu_retrieval',
46+
description:
47+
'Helsingin yliopiston ohjelmistotuotannon kurssimateriaalin haku funktio. Kutsu tätä kun käyttäjä haluaa tietoa kurssiin liittyen. Muuten älä kutsu tätä.',
48+
parameters: {
49+
type: 'object',
50+
properties: {
51+
query: {
52+
type: 'string',
53+
description: 'Käyttäjän kysymys kurssimateriaalista',
54+
},
55+
},
56+
required: ['query'],
57+
additionalProperties: false,
58+
},
59+
strict: true, // or true, depending on your requirements
60+
},
61+
function: async (client: AzureOpenAI, query: string): Promise<any> => {
62+
// Simulate a tool function that returns a simple message
63+
64+
// console.log('KUTSUTAAN RAG FUNKTIOTA')
65+
66+
// const indexit = await client.vectorStores.list()
67+
// const vs = indexit.data.filter((index) => index.name === 'ohtu-test')[0]
68+
69+
// console.log('INDEX', vs)
70+
71+
// const results = await client.vectorStores.search(vs.id, {
72+
// query,
73+
// max_num_results: 10,
74+
// rewrite_query: true,
75+
// })
76+
77+
return null
78+
},
79+
}

src/server/util/azure/tools.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)