I want to make a chat room. I follow the step with MS tutorial.
My folder is like figure1.
But the function doesn’t be create. When I go to azure portal, I saw the Function app is created successfully, but their is no any function can be trig (fig2).
I use function start locally and it works fine, but I put it in azure and it doesn’t work.
here is my message.js code (I apologize for not being able to reveal the contents of the function getAIResponse, but I don’t think this program affects the deploy function.)
const { WebPubSubServiceClient } = require('@azure/web-pubsub');
const { app: azureApp } = require('@azure/functions');
const { OpenAIClient, AzureKeyCredential } = require("@azure/openai");
const connectionString = process.env["AzureWebPubSubConnectionString"];
const hubName = 'simplechat';
const serviceClient = new WebPubSubServiceClient(connectionString, hubName);
// Azure Function (PubSub WebSocket)
azureApp.http('message', {
methods: ['POST', 'OPTIONS'],
authLevel: 'anonymous',
handler: async (request, context) => {
if (request.method === 'OPTIONS') {
return {
status: 204,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'Access-Control-Allow-Headers': 'X-Requested-With, content-type',
'Content-Type': 'application/json'
}
};
}
try {
const body = await request.json();
const message = body.messages;
if (!message) {
return {
status: 400,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
},
body: JSON.stringify({ error: "Message is required", data: request })
};
}
const aiResponse = await getAIResponse(body);
await serviceClient.sendToAll(aiResponse, { contentType: "text/plain" });
return {
status: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
},
body: JSON.stringify(aiResponse)
};
} catch (error) {
return {
status: 500,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
},
body: JSON.stringify({ error: error.message })
};
}
}
});
Here is my package.json
{
"name": "chat",
"version": "1.0.1",
"description": "",
"main": "src/functions/*.js",
"scripts": {
"start": "func start",
"test": "echo "No tests yet...""
},
"dependencies": {
"@azure/core-auth": "^1.9.0",
"@azure/functions": "^4.5.0",
"@azure/openai": "^1.0.0-beta.7",
"@azure/web-pubsub": "^1.1.3",
"axios": "^1.7.7",
"dotenv": "^16.4.5"
},
"devDependencies": {
"azure-functions-core-tools": "^4.x"
}
}
5
It looks like you haven’t created the V4 model JavaScript functions correctly, as V4 model doesn’t contains function.json file but V3 does. Folder structure for V4 model should look like below.
| .funcignore
| .gitignore
| host.json
| index.html
| local.settings.json
| package-lock.json
| package.json
|
+---.vscode
| extensions.json
| launch.json
| settings.json
| tasks.json
|
+---node_modules
|
---src
---functions
index.js
message.js
negotiate.js
I have given code in package.json file.
{
"name": "79116585",
"version": "1.0.0",
"description": "",
"scripts": {
"start": "func start",
"test": "echo "No tests yet...""
},
"dependencies": {
"@azure/functions": "^4.0.0"
},
"devDependencies": {},
"main": "src/functions/*.js"
}
I also have followed this documentation to create a chat App using V4 model JavaScript Function App. You should be able to see the http triggers endpoint post successful deployment.
I am able to navigate to the index page using the function URL.
I would suggest you to follow the documentation clearly and create the function from the scratch.