|
| 1 | +const https = require('https'); |
| 2 | + |
| 3 | +// Example dummy function hard coded to return the same weather |
| 4 | +// In production, this could be your backend API or an external API |
| 5 | +function get_current_weather(location, unit = 'celsius') { |
| 6 | + const weather_info = { |
| 7 | + location: location, |
| 8 | + temperature: '23', |
| 9 | + unit: unit, |
| 10 | + forecast: ['sunny', 'windy'], |
| 11 | + }; |
| 12 | + return JSON.stringify(weather_info); |
| 13 | +} |
| 14 | + |
| 15 | +async function runConversation() { |
| 16 | + // Step 1: send the conversation and available functions to GPT |
| 17 | + const messages = [{ role: 'user', content: "weather in melbourne" }]; |
| 18 | + const functions = [ |
| 19 | + { |
| 20 | + name: 'get_current_weather', |
| 21 | + description: 'Get the current weather in a given location', |
| 22 | + parameters: { |
| 23 | + type: 'object', |
| 24 | + properties: { |
| 25 | + location: { |
| 26 | + type: 'string', |
| 27 | + description: 'The city and state, e.g. San Francisco, CA', |
| 28 | + }, |
| 29 | + unit: { type: 'string', enum: ['celsius', 'fahrenheit'] }, |
| 30 | + }, |
| 31 | + required: ['location'], |
| 32 | + }, |
| 33 | + }, |
| 34 | + ]; |
| 35 | + |
| 36 | + const requestData = JSON.stringify({ |
| 37 | + model: 'gpt-3.5-turbo', |
| 38 | + messages: messages, |
| 39 | + functions: functions, |
| 40 | + function_call: 'auto', // auto is default, but we'll be explicit |
| 41 | + }); |
| 42 | + |
| 43 | + const options = { |
| 44 | + hostname: 'api.openai.com', |
| 45 | + path: '/v1/chat/completions', |
| 46 | + method: 'POST', |
| 47 | + headers: { |
| 48 | + 'Content-Type': 'application/json', |
| 49 | + 'Authorization': 'Bearer sk-xxxxxxxxx', // Replace with your OpenAI API key |
| 50 | + }, |
| 51 | + }; |
| 52 | + |
| 53 | + const response = await new Promise((resolve, reject) => { |
| 54 | + const req = https.request(options, (res) => { |
| 55 | + let data = ''; |
| 56 | + |
| 57 | + res.on('data', (chunk) => { |
| 58 | + data += chunk; |
| 59 | + }); |
| 60 | + |
| 61 | + res.on('end', () => { |
| 62 | + resolve(JSON.parse(data)); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + req.on('error', (error) => { |
| 67 | + reject(error); |
| 68 | + }); |
| 69 | + |
| 70 | + req.write(requestData); |
| 71 | + req.end(); |
| 72 | + }); |
| 73 | + |
| 74 | + const responseMessage = response.choices[0].message; |
| 75 | + |
| 76 | + // Step 2: check if GPT wanted to call a function |
| 77 | + if (responseMessage.function_call) { |
| 78 | + // Step 3: call the function |
| 79 | + const availableFunctions = { |
| 80 | + get_current_weather: get_current_weather, |
| 81 | + }; |
| 82 | + const functionName = responseMessage.function_call.name; |
| 83 | + const functionToCall = availableFunctions[functionName]; |
| 84 | + const functionArgs = JSON.parse(responseMessage.function_call.arguments); |
| 85 | + const functionResponse = functionToCall( |
| 86 | + functionArgs.location, |
| 87 | + functionArgs.unit |
| 88 | + ); |
| 89 | + |
| 90 | + // Step 4: send the info on the function call and function response to GPT |
| 91 | + messages.push(responseMessage); // extend conversation with assistant's reply |
| 92 | + messages.push({ |
| 93 | + role: 'function', |
| 94 | + name: functionName, |
| 95 | + content: functionResponse, |
| 96 | + }); // extend conversation with function response |
| 97 | + |
| 98 | + const secondRequestData = JSON.stringify({ |
| 99 | + model: 'gpt-3.5-turbo', |
| 100 | + messages: messages, |
| 101 | + }); |
| 102 | + |
| 103 | + const secondResponse = await new Promise((resolve, reject) => { |
| 104 | + const req = https.request(options, (res) => { |
| 105 | + let data = ''; |
| 106 | + |
| 107 | + res.on('data', (chunk) => { |
| 108 | + data += chunk; |
| 109 | + }); |
| 110 | + |
| 111 | + res.on('end', () => { |
| 112 | + resolve(JSON.parse(data)); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + req.on('error', (error) => { |
| 117 | + reject(error); |
| 118 | + }); |
| 119 | + |
| 120 | + req.write(secondRequestData); |
| 121 | + req.end(); |
| 122 | + }); |
| 123 | + return secondResponse; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +runConversation() |
| 128 | + .then((response) => { |
| 129 | + console.log(response.choices[0].message.content); |
| 130 | + }) |
| 131 | + .catch((error) => { |
| 132 | + console.error(error); |
| 133 | + }); |
0 commit comments