Created
May 30, 2026 21:30
-
-
Save flushpot1125/89a184c77b2994a2651067d632e70194 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require('dotenv').config(); | |
| const express = require('express'); | |
| const cors = require('cors'); | |
| const { OpenAI } = require('openai'); | |
| const app = express(); | |
| const port = process.env.PORT || 3000; | |
| // ミドルウェア設定 | |
| app.use(cors()); | |
| app.use(express.json()); | |
| app.use(express.static('public')); | |
| // Azure AI Foundry設定 | |
| const endpoint = process.env.AZURE_OPENAI_ENDPOINT || "https://<your azure resource name>.services.ai.azure.com/openai/v1"; | |
| const deploymentName = process.env.AZURE_OPENAI_DEPLOYMENT || "gpt-4.1"; | |
| const apiKey = process.env.AZURE_OPENAI_API_KEY; | |
| // OpenAIクライアントの初期化 | |
| if (!apiKey) { | |
| console.error('エラー: AZURE_OPENAI_API_KEYが設定されていません'); | |
| process.exit(1); | |
| } | |
| const client = new OpenAI({ | |
| baseURL: endpoint, | |
| apiKey: apiKey | |
| }); | |
| console.log('Azure AI Foundry接続設定:'); | |
| console.log('- エンドポイント:', endpoint); | |
| console.log('- デプロイメント:', deploymentName); | |
| console.log('- API Key:', apiKey ? '設定済み (長さ: ' + apiKey.length + ')' : '未設定'); | |
| // チャットエンドポイント | |
| app.post('/api/chat', async (req, res) => { | |
| try { | |
| const { message } = req.body; | |
| if (!message) { | |
| return res.status(400).json({ error: 'メッセージが必要です' }); | |
| } | |
| console.log('受信したメッセージ:', message); | |
| // Azure AI Foundryにリクエスト送信 | |
| const completion = await client.chat.completions.create({ | |
| model: deploymentName, | |
| messages: [ | |
| { | |
| role: "user", | |
| content: message | |
| } | |
| ], | |
| temperature: 0.7, | |
| max_tokens: 800 | |
| }); | |
| const reply = completion.choices[0].message.content; | |
| console.log('AI回答:', reply); | |
| res.json({ reply }); | |
| } catch (error) { | |
| console.error('エラーが発生しました:'); | |
| console.error('- メッセージ:', error.message); | |
| console.error('- スタック:', error.stack); | |
| console.error('- 詳細:', error); | |
| res.status(500).json({ | |
| error: 'チャット処理中にエラーが発生しました', | |
| details: error.message | |
| }); | |
| } | |
| }); | |
| // ヘルスチェックエンドポイント | |
| app.get('/api/health', (req, res) => { | |
| res.json({ status: 'OK', timestamp: new Date().toISOString() }); | |
| }); | |
| // サーバー起動 | |
| app.listen(port, () => { | |
| console.log(`サーバーがポート ${port} で起動しました`); | |
| console.log(`エンドポイント: ${endpoint}`); | |
| console.log(`デプロイメント名: ${deploymentName}`); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment