Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const getOpenAIResponse = async (
- query: string,
- instructions?: string,
- previousResponseId?: string,
- functionToolCall?: any,
- functionToolCallResponse?: string
- ) => {
- const input =
- functionToolCall && functionToolCallResponse
- ? [
- {
- type: "function_call_output",
- call_id: functionToolCall.call_id,
- output: functionToolCallResponse,
- },
- ]
- : query;
- let responseId: string;
- const stream = await openai.responses.create({
- model: "gpt-4o-mini",
- stream: true,
- user: "user-123",
- input,
- tools,
- tool_choice: "auto",
- instructions, // I want to omit this ONLY if it's a tool call phase
- previous_response_id: previousResponseId,
- });
- return from(stream).pipe(
- mergeMap((event) => {
- if (event.type === "response.created") {
- if (!responseId) {
- responseId = event.response.id;
- }
- return of({ type: "response.created", data: event.response.id });
- }
- if (event.type === "response.function_call_arguments.done") {
- const toolCall = finalToolCalls[event.output_index];
- if (!toolCall) return EMPTY;
- const toolHandler =
- openaiToolHandler[toolCall.name as keyof typeof openaiToolHandler];
- if (!toolHandler) {
- this.logger.error(`Tool call ${toolCall.name} is not supported`);
- return EMPTY;
- }
- const toolCallArguments = JSON.parse(toolCall.arguments);
- const toolCall = {}; // assume extracted
- const toolOutput = {}; // result of calling tool
- return this.getOpenAIResponse(
- query,
- instructions,
- responseId,
- toolCall,
- typeof toolCallResponse === "string"
- ? toolCallResponse
- : JSON.stringify(toolCallResponse)
- );
- }
- return of(event);
- })
- );
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement