大家好,希望你们做得很好,我在使用 azure open ai 时遇到了问题,其中所有 api 详细信息都是正确的,当我完成聊天时它可以工作,但 ai 助手不想工作,即使我是遵循 azure open ai 文档,这是我的代码:
const
dotenv = require("dotenv");
dotenv.config();
const
{ AzureOpenAI } = require("openai");
const
endpoint = process.env["AZURE_OPENAI_ENDPOINT"];
const
apiKey = process.env["AZURE_OPENAI_API_KEY"];
const
apiVersion = process.env["API_VERSION"];
const
deployment = process.env["DEPLOYMENT"];
// Replace this value with the deployment name for your model.
const
client = new AzureOpenAI({ endpoint, apiKey, apiVersion, deployment });
async
function main() {
try {
const
assistant =
await
client.beta.assistants.create({
name: "Math Tutor",
instructions:
"You are a personal math tutor. Write and run code to answer math questions.",
tools: [{ type: "code_interpreter" }],
model: "gpt-4o",
});
console.log("Assistant created successfully:", assistant);
} catch (error) {
console.error("Error creating assistant:", error);
}
}
main();
The error : Error creating assistant: BadRequestError: 400 Unsupported data type
请参阅下面我的代码。这似乎对我有用。可以尝试添加您的端点详细信息并查看。代码来自 https://learn.microsoft.com/en-us/azure/ai-services/openai/assistants-quickstart?tabs=command-line%2Cjavascript-key%2Ctypescript-keyless&pivots=programming-language-javascript
我使用的软件包版本是:“openai”:“^4.62.1”
const { AzureOpenAI } = require("openai");
const azureOpenAIEndpoint = "https://xxxxx.openai.azure.com/";
const azureOpenAIKey =
"xxxxx";
const azureOpenAIVersion = "2024-08-01-preview";
const azureOpenAIDeployment = "xxxx-2"; // gpt-4o
// Replace this value with the deployment name for your model.
const main = async () => {
// Check env variables
if (
!azureOpenAIKey ||
!azureOpenAIEndpoint ||
!azureOpenAIDeployment ||
!azureOpenAIVersion
) {
throw new Error(
"Please set AZURE_OPENAI_KEY and AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_DEPLOYMENT_NAME in your environment variables."
);
}
// Get Azure SDK client
const getClient = () => {
const assistantsClient = new AzureOpenAI({
endpoint: azureOpenAIEndpoint,
apiVersion: azureOpenAIVersion,
apiKey: azureOpenAIKey,
});
return assistantsClient;
};
const assistantsClient = getClient();
const options = {
model: azureOpenAIDeployment, // Deployment name seen in Azure AI Foundry portal
name: "Math Tutor",
instructions:
"You are a personal math tutor. Write and run JavaScript code to answer math questions.",
tools: [{ type: "code_interpreter" }],
};
const role = "user";
const message =
"I need to solve the equation `3x + 11 = 14`. Can you help me?";
// Create an assistant
const assistantResponse = await assistantsClient.beta.assistants.create(
options
);
console.log(`Assistant created: ${JSON.stringify(assistantResponse)}`);
// Create a thread
const assistantThread = await assistantsClient.beta.threads.create({});
console.log(`Thread created: ${JSON.stringify(assistantThread)}`);
// Add a user question to the thread
const threadResponse = await assistantsClient.beta.threads.messages.create(
assistantThread.id,
{
role,
content: message,
}
);
console.log(`Message created: ${JSON.stringify(threadResponse)}`);
// Run the thread and poll it until it is in a terminal state
const runResponse = await assistantsClient.beta.threads.runs.createAndPoll(
assistantThread.id,
{
assistant_id: assistantResponse.id,
},
{ pollIntervalMs: 500 }
);
console.log(`Run created: ${JSON.stringify(runResponse)}`);
// Get the messages
const runMessages = await assistantsClient.beta.threads.messages.list(
assistantThread.id
);
for await (const runMessageDatum of runMessages) {
for (const item of runMessageDatum.content) {
// types are: "image_file" or "text"
if (item.type === "text") {
console.log(`Message content: ${JSON.stringify(item.text?.value)}`);
}
}
}
};
main().catch(console.error);