I’m having trouble configuring a proxy server for my OpenAI API integration. When I enable the proxy settings, the connection fails with an error. Without the proxy, everything works perfectly but I need the requests to go through a proxy server. Here’s my current setup:
const { Configuration, OpenAIApi } = require('openai');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const { HttpsProxyAgent } = require('https-proxy-agent');
require('dotenv').config();
const proxyAgent = new HttpsProxyAgent(process.env.PROXY_SERVER_URL);
const config = new Configuration({
apiKey: process.env.OPENAI_KEY,
baseOptions: {
httpsAgent: proxyAgent
}
});
const ai = new OpenAIApi(config);
const botId = process.env.BOT_ID;
const server = express();
const PORT = 3000;
server.use(cors({
origin: ['http://localhost:5500'],
methods: ['GET', 'POST'],
credentials: true
}));
server.use(bodyParser.json());
server.post('/chat', async (req, res) => {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
try {
console.log('User input:', req.body.text);
const bot = await ai.retrieveAssistant(botId);
console.log('Bot retrieved:', bot);
const conversation = await ai.createThread();
console.log('Thread created:', conversation);
await ai.createMessage(conversation.id, {
role: "user",
content: req.body.text
});
console.log('User message added:', req.body.text);
const stream = ai.createRun(conversation.id, {
assistant_id: bot.id
})
.on('textCreated', (text) => {
res.write(`data: \nbot > \n`);
})
.on('textDelta', (delta) => {
const cleanText = delta.value.replace(/ /g, ' ').replace(/\n/g, '<br>');
res.write(`data: ${cleanText}\n`);
console.log('Text chunk:', delta);
})
.on('end', () => {
res.end();
console.log('Stream finished.');
});
} catch (err) {
console.error('Request failed:', err);
res.status(500).json({ error: "API request failed" });
}
});
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
When I comment out the httpsAgent line, the server works but doesn’t use the proxy. With the proxy enabled, I get this error:
User input: hello
Request failed: APIConnectionError: Connection error.
FetchError: request to https://api.openai.com/v1/assistants/asst_xyz failed, reason: Proxy connection ended before receiving CONNECT response
The proxy connection seems to close before getting a proper response. Has anyone solved this issue before?