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
1.99k stars 1.16k forks source link

Azure OpenAI Assistants - assistantsClient.createMessage returns 500 #29224

Open diberry opened 3 months ago

diberry commented 3 months ago

Describe the bug Code correctly creates assistant and thread but returns 500 on create message

To Reproduce Steps to reproduce the behavior:

  1. Create deployments
image
  1. Create .env with following (use your own key and endpoint)
AZURE_OPENAI_MODEL_ID=gpt-35-turbo-1106
AZURE_OPENAI_KEY=b8f...
AZURE_OPENAI_RESOURCE=diberry-openai-swedencentral
AZURE_OPENAI_ENDPOINT=https://diberry-openai-swedencentral.openai.azure.com/

I assume the model id of gpt-35-turbo-1106 is a concatenation of the model name and model version from the screenshot and not the deployment name.

  1. Use following code, build and run.
import { AssistantsClient, AssistantCreationOptions, MessageRole, CodeInterpreterToolDefinition, ToolDefinition } from "@azure/openai-assistants";
import { AzureKeyCredential } from "@azure/core-auth";

import 'dotenv/config'

const azureOpenAIKey = process.env.AZURE_OPENAI_KEY as string;
const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT as string;
const azureOpenAIModelId = process.env.AZURE_OPENAI_MODEL_ID as string;

if (!azureOpenAIKey || !azureOpenAIEndpoint) {
  throw new Error(
    "Please ensure to set AZURE_OPENAI_KEY and AZURE_OPENAI_ENDPOINT in your environment variables."
  );
}

const assistantsClient = new AssistantsClient(azureOpenAIEndpoint, new AzureKeyCredential(azureOpenAIKey));

//"gpt-35-turbo-1106",
//"gpt-35-turbo-instruct-0914",
const options: AssistantCreationOptions = {
  model: azureOpenAIModelId,
  name: "Math Tutor",
  instructions: "You are a personal math tutor. Write and run JavaScript code to answer math questions.",
  tools: [{ type: "code_interpreter" } as ToolDefinition],
};

// Create an assistant
const assistantResponse = await assistantsClient.createAssistant(options);
console.log(`Assistant created: ${JSON.stringify(assistantResponse)}`);

// Create a thread
const assistantThread = await assistantsClient.createThread({});
console.log(`Thread created: ${JSON.stringify(assistantThread)}`);

// Add a user question to the thread
const threadResponse = await assistantsClient.createMessage(assistantThread.id, "user", "What is 3x + 11 = 14?");
console.log(`Message created:  ${JSON.stringify(threadResponse)}`);

// Run the thread
let runResponse = await assistantsClient.createRun(
  assistantThread.id,
  {
    assistantId: assistantResponse.id
  }
);
console.log(`Run created:  ${JSON.stringify(runResponse)}`);

// Wait for the assistant to respond
do {
  await new Promise((r) => setTimeout(r, 500));
  runResponse = await assistantsClient.getRun(assistantThread.id, runResponse.id);
} while (runResponse.status === "queued" || runResponse.status === "in_progress");

// Get the messages
const runMessages = await assistantsClient.listMessages(assistantThread.id);
for (const runMessageDatum of runMessages.data) {
  for (const item of runMessageDatum.content) {
    if (item.type === "text") {
      console.log(`Message content: ${JSON.stringify(item.text?.value)}`);
    }
  }
}
  1. Get response such as
@diberry ➜ /workspaces/azure-typescript-e2e-apps/quickstarts/azure-openai-assistants (dfberry/azure-openai-assistants) $ npm run start

> azure-openai-assistants@1.0.0 start
> node dist/index.js

Assistant created: {"id":"asst_dyBhrIKCUKgWGvKY4O7U2XEZ","createdAt":"2024-04-08T17:39:49.000Z","name":"Math Tutor","description":null,"model":"gpt-35-turbo-1106","instructions":"You are a personal math tutor. Write and run JavaScript code to answer math questions.","tools":[{"type":"code_interpreter"}],"fileIds":[],"metadata":{}}
Thread created: {"id":"thread_m5DRrVS4AGTwIdYYULmDNO7h","createdAt":"2024-04-08T17:39:49.000Z","metadata":{}}
Message created:  {"id":"msg_MaiXA4hHmjP0mXT8K4LR6Gtv","createdAt":"2024-04-08T17:39:50.000Z","threadId":"thread_m5DRrVS4AGTwIdYYULmDNO7h","role":"user","content":[{"type":"text","text":{"value":"What is 3x + 11 = 14?","annotations":[]},"imageFile":{}}],"assistantId":null,"runId":null,"fileIds":[],"metadata":{}}
file:///workspaces/azure-typescript-e2e-apps/quickstarts/azure-openai-assistants/node_modules/@azure-rest/core-client/dist/esm/restError.js:10
    return new RestError(message, {
           ^

RestError: The server had an error processing your request. Sorry about that! You can retry your request, or contact us through our help center at oai-support@microsoft.com if you keep seeing this error. (Please include the request ID 41b61e8e-0b5e-4821-8958-4c02c08622c7 in your email.)
    at createRestError (file:///workspaces/azure-typescript-e2e-apps/quickstarts/azure-openai-assistants/node_modules/@azure-rest/core-client/dist/esm/restError.js:10:12)
    at _createRunDeserialize (file:///workspaces/azure-typescript-e2e-apps/quickstarts/azure-openai-assistants/node_modules/@azure/openai-assistants/dist-esm/src/api/operations.js:501:15)
    at createRun (file:///workspaces/azure-typescript-e2e-apps/quickstarts/azure-openai-assistants/node_modules/@azure/openai-assistants/dist-esm/src/api/operations.js:342:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async file:///workspaces/azure-typescript-e2e-apps/quickstarts/azure-openai-assistants/dist/index.js:29:19 {
  code: null,
  statusCode: 500,
  request: PipelineRequestImpl {
    url: 'https://diberry-openai-swedencentral.openai.azure.com/openai/threads/thread_m5DRrVS4AGTwIdYYULmDNO7h/runs?api-version=2024-02-15-preview',
    method: 'POST',
    headers: HttpHeadersImpl {
      _headersMap: Map(7) {
        'accept' => { name: 'accept', value: 'application/json' },
        'content-type' => {
          name: 'content-type',
          value: 'application/json; charset=UTF-8'
        },
        'accept-encoding' => { name: 'Accept-Encoding', value: 'gzip,deflate' },
        'user-agent' => {
          name: 'User-Agent',
          value: 'azsdk-js-openai-assistants-rest/1.0.0-beta.5 core-rest-pipeline/1.15.1 Node/18.16.0 OS/(x64-Linux-6.2.0-1019-azure)'
        },
        'x-ms-client-request-id' => {
          name: 'x-ms-client-request-id',
          value: '6cc4a56e-283d-4152-9222-2024f42e2212'
        },
        'api-key' => { name: 'api-key', value: 'b8f...' },
        'content-length' => { name: 'Content-Length', value: '48' }
      }
    },
    timeout: 0,
    withCredentials: false,
    body: '{"assistant_id":"asst_dyBhrIKCUKgWGvKY4O7U2XEZ"}',
    multipartBody: undefined,
    formData: undefined,
    streamResponseStatusCodes: undefined,
    enableBrowserStreams: true,
    proxySettings: undefined,
    disableKeepAlive: false,
    abortSignal: undefined,
    requestId: '6cc4a56e-283d-4152-9222-2024f42e2212',
    tracingOptions: undefined,
    allowInsecureConnection: false,
    onUploadProgress: undefined,
    onDownloadProgress: undefined
  },
  response: {
    headers: HttpHeadersImpl {
      _headersMap: Map(9) {
        'content-length' => { name: 'content-length', value: '379' },
        'content-type' => { name: 'content-type', value: 'application/json' },
        'x-request-id' => {
          name: 'x-request-id',
          value: '41b61e8e-0b5e-4821-8958-4c02c08622c7'
        },
        'apim-request-id' => {
          name: 'apim-request-id',
          value: 'feffa411-5d34-478e-866f-7e4141166990'
        },
        'strict-transport-security' => {
          name: 'strict-transport-security',
          value: 'max-age=31536000; includeSubDomains; preload'
        },
        'x-ms-client-request-id' => {
          name: 'x-ms-client-request-id',
          value: '6cc4a56e-283d-4152-9222-2024f42e2212'
        },
        'x-content-type-options' => { name: 'x-content-type-options', value: 'nosniff' },
        'x-ms-region' => { name: 'x-ms-region', value: 'Sweden Central' },
        'date' => { name: 'date', value: 'Mon, 08 Apr 2024 17:39:57 GMT' }
      }
    },
    request: PipelineRequestImpl {
      url: 'https://diberry-openai-swedencentral.openai.azure.com/openai/threads/thread_m5DRrVS4AGTwIdYYULmDNO7h/runs?api-version=2024-02-15-preview',
      method: 'POST',
      headers: HttpHeadersImpl {
        _headersMap: Map(7) {
          'accept' => { name: 'accept', value: 'application/json' },
          'content-type' => {
            name: 'content-type',
            value: 'application/json; charset=UTF-8'
          },
          'accept-encoding' => { name: 'Accept-Encoding', value: 'gzip,deflate' },
          'user-agent' => {
            name: 'User-Agent',
            value: 'azsdk-js-openai-assistants-rest/1.0.0-beta.5 core-rest-pipeline/1.15.1 Node/18.16.0 OS/(x64-Linux-6.2.0-1019-azure)'
          },
          'x-ms-client-request-id' => {
            name: 'x-ms-client-request-id',
            value: '6cc4a56e-283d-4152-9222-2024f42e2212'
          },
          'api-key' => {
            name: 'api-key',
            value: 'b8f...'
          },
          'content-length' => { name: 'Content-Length', value: '48' }
        }
      },
      timeout: 0,
      withCredentials: false,
      body: '{"assistant_id":"asst_dyBhrIKCUKgWGvKY4O7U2XEZ"}',
      multipartBody: undefined,
      formData: undefined,
      streamResponseStatusCodes: undefined,
      enableBrowserStreams: true,
      proxySettings: undefined,
      disableKeepAlive: false,
      abortSignal: undefined,
      requestId: '6cc4a56e-283d-4152-9222-2024f42e2212',
      tracingOptions: undefined,
      allowInsecureConnection: false,
      onUploadProgress: undefined,
      onDownloadProgress: undefined
    },
    status: 500
  },
  details: undefined
}

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Add any other context about the problem here.

diberry commented 3 months ago

Wow, I ran it again and it all succeeded. That's surprising.