|
| 1 | +import { OpenAI } from 'openai'; |
| 2 | +import { rollDice, rollDiceDefinition } from './tools/dice.js'; |
| 3 | + |
| 4 | +// OpenAI LLM client |
| 5 | +const openai = new OpenAI({ |
| 6 | + apiKey: process.env.OPENAI_API_KEY, |
| 7 | +}); |
| 8 | + |
| 9 | +/** |
| 10 | + * Stream an LLM response to prompts with an example dice rolling function |
| 11 | + * |
| 12 | + * @param {import("@slack/web-api").ChatStreamer} streamer - Slack chat stream |
| 13 | + * @param {Array} prompts - OpenAI ResponseInputParam messages |
| 14 | + * |
| 15 | + * @see {@link https://docs.slack.dev/tools/bolt-js/web#sending-streaming-messages} |
| 16 | + * @see {@link https://platform.openai.com/docs/guides/text} |
| 17 | + * @see {@link https://platform.openai.com/docs/guides/streaming-responses} |
| 18 | + * @see {@link https://platform.openai.com/docs/guides/function-calling} |
| 19 | + */ |
| 20 | +export async function callLLM(streamer, prompts) { |
| 21 | + const toolCalls = []; |
| 22 | + |
| 23 | + const response = await openai.responses.create({ |
| 24 | + model: 'gpt-4o-mini', |
| 25 | + input: prompts, |
| 26 | + tools: [rollDiceDefinition], |
| 27 | + tool_choice: 'auto', |
| 28 | + stream: true, |
| 29 | + }); |
| 30 | + |
| 31 | + for await (const event of response) { |
| 32 | + // Stream markdown text from the LLM response as it arrives |
| 33 | + if (event.type === 'response.output_text.delta' && event.delta) { |
| 34 | + await streamer.append({ |
| 35 | + markdown_text: event.delta, |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + // Save function calls for later computation and a new task is shown |
| 40 | + if (event.type === 'response.output_item.done') { |
| 41 | + if (event.item.type === 'function_call') { |
| 42 | + toolCalls.push(event.item); |
| 43 | + |
| 44 | + if (event.item.name === 'roll_dice') { |
| 45 | + const args = JSON.parse(event.item.arguments); |
| 46 | + await streamer.append({ |
| 47 | + chunks: [ |
| 48 | + { |
| 49 | + type: 'task_update', |
| 50 | + id: event.item.call_id, |
| 51 | + title: `Rolling a ${args.count}d${args.sides}...`, |
| 52 | + status: 'in_progress', |
| 53 | + }, |
| 54 | + ], |
| 55 | + }); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Perform tool calls and marks tasks as completed |
| 62 | + if (toolCalls.length > 0) { |
| 63 | + for (const call of toolCalls) { |
| 64 | + if (call.name === 'roll_dice') { |
| 65 | + const args = JSON.parse(call.arguments); |
| 66 | + |
| 67 | + prompts.push({ |
| 68 | + id: call.id, |
| 69 | + call_id: call.call_id, |
| 70 | + type: 'function_call', |
| 71 | + name: 'roll_dice', |
| 72 | + arguments: call.arguments, |
| 73 | + }); |
| 74 | + |
| 75 | + const result = rollDice(args); |
| 76 | + |
| 77 | + prompts.push({ |
| 78 | + type: 'function_call_output', |
| 79 | + call_id: call.call_id, |
| 80 | + output: JSON.stringify(result), |
| 81 | + }); |
| 82 | + |
| 83 | + if (result.error != null) { |
| 84 | + await streamer.append({ |
| 85 | + chunks: [ |
| 86 | + { |
| 87 | + type: 'task_update', |
| 88 | + id: call.call_id, |
| 89 | + title: result.error, |
| 90 | + status: 'error', |
| 91 | + }, |
| 92 | + ], |
| 93 | + }); |
| 94 | + } else { |
| 95 | + await streamer.append({ |
| 96 | + chunks: [ |
| 97 | + { |
| 98 | + type: 'task_update', |
| 99 | + id: call.call_id, |
| 100 | + title: result.description, |
| 101 | + status: 'complete', |
| 102 | + }, |
| 103 | + ], |
| 104 | + }); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // complete the llm response after making tool calls |
| 110 | + await callLLM(streamer, prompts); |
| 111 | + } |
| 112 | +} |
0 commit comments