Azure / azure-sdk-for-js

This repository is for active development of the Azure SDK for JavaScript (NodeJS & Browser). For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/javascript/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-js.
MIT License
2.09k stars 1.2k forks source link

ReferenceError: AzureKeyCredential is not defined #26401

Closed phusting closed 1 year ago

phusting commented 1 year ago

I'm using the sample at https://github.com/Azure/azure-sdk-for-js/blob/%40azure/openai_1.0.0-beta.2/sdk/openai/openai/samples/v1-beta/javascript/chatCompletions.js#L16

and I'm always getting a ReferenceError: AzureKeyCredential is not defined I have it from my azure account but not sure why I'm always getting this. Is there somewhere I can look to figure out what the issue is?

xirzec commented 1 year ago

@phusting can you provide some details about what environment you're running this in, such as what Node version, etc. If you're seeing AzureKeyCredential not being defined I imagine it's some issue with the require statement since it appears the package is exporting the credential correctly.

phusting commented 1 year ago

Node 8.19.3 I'm using Visual Studio 2022 and building a Word Web Add-in.


const { OpenAIClient } = require("@azure/openai");

const endpoint = process.env["ENDPOINT"] || "https://myinstancecantshow-azureopenai.openai.azure.com/";
const azureApiKey = process.env["AZURE_API_KEY"] || "mykeycantshow";

const messages = [
            { role: "system", content: "You are a helpful assistant. You will talk like a pirate." },
            { role: "user", content: "Can you help me?" },
            { role: "assistant", content: "Arrrr! Of course, me hearty! What can I do for ye?" },
            { role: "user", content: "What's the best way to train a parrot?" },
];

                // ** AZURE EXAMPLE
                console.log("== Chat Completions Sample ==");

                const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
                const deploymentId = "gpt-35-turbo";
                const result = await client.getChatCompletions(deploymentId, messages);

                for (const choice of result.choices) {
                    console.log(choice.message);
                }
            } catch (error) {
                document.getElementById("txterror").innerText = error;
            }
xirzec commented 1 year ago

@phusting Node 8 has been out of support since the end of 2019. Our minimum supported Node version at this time is Node 14.

I'm not familiar with Word Web Add-ins, but I imagine you'll have to bundle similarly to the browser, since presumably the Add-In is running inside of a browser context?

phusting commented 1 year ago

Sorry, it is Node v18.13. I typed the wrong command.

Yes, it is running in a Chromium instance.

Going directly to OpenAI API is just fine. I am setting the azureai key, but it fails right away.

phusting commented 1 year ago

Seems like a Office web add-in thing. I'm actually further and it is sending the request, now troubleshooting the response. :) Almost there!

phusting commented 1 year ago

Almost, but this I have no clue. Any suggestions? I'm getting a 404 error.

`fetchHttpClient.js:67

    POST https://ghostwriterai-azureopenai.openai.azure.com/openai/deployments/text-davinci-003/completions?api-version=2023-03-15-preview 404`
phusting commented 1 year ago

Closing, I got it to work in Office now.