I am trying to complete this feature using the Azure OpenAI SDK, but I always encounter errors when adding the file ID in the create_message
step. Can any hero tell me how to do this, and can you help me write the API call process? Below is my code:
import fs from 'node:fs'
import { setLogLevel } from '@azure/logger'
import { AssistantsClient } from '@azure/openai-assistants'
import { AzureKeyCredential } from '@azure/openai'
setLogLevel("info");
const assistantsClient = new AssistantsClient(
endpoint,
new AzureKeyCredential(apiKey),
{ apiVersion: "2024-05-01-preview", allowInsecureConnection: true}
)
const run = async () => {
// 0. upload file
const filename = "example.docx";
const docx = fs.readFileSync(filename)
const uploadAssistantFile = await assistantsClient.uploadFile(
docx, "assistants", { filename }
);
// 1. create assistant
const assistant = await assistantsClient.createAssistant({
model: "gpt-4-0125",
name: "office converter",
instructions: "you are a office file converter,help user convert office file to other format",
tools: [{ type: "code_interpreter" }], // ? I am using code_interpreter here, but I am not sure if it is correct.
})
// 2. create thread
const assistantThread = await assistantsClient.createThread();
//3. create message to add to thread
const question = "please convert the docx file to markdown";
const messageResponse = await assistantsClient.createMessage(assistantThread.id, "user", question, {
fileIds: [uploadAssistantFile.id], // This is where the error occurs; it always prompts that file_ids is an invalid parameter, with status code 400.
});
let runResponse = await assistantsClient.createRun(assistantThread.id, {
assistantId: assistant.id,
instructions: "Please convert the docx file to markdown",
});
do {
await new Promise((resolve) => setTimeout(resolve, 5000));
runResponse = await assistantsClient.getRun(assistantThread.id, runResponse.id);
if (runResponse.status === "failed") {
throw new Error("Run failed");
}
} while (runResponse.status === "queued" || runResponse.status === "in_progress")
await new Promise((resolve) => setTimeout(resolve, 30000));
const runMessages = await assistantsClient.listMessages(assistantThread.id);
for (const runMessageDatum of runMessages.data) {
for (const item of runMessageDatum.content) {
console.log('\n:::::', item, ':::::\n')
}
}
}
run()