79298712

Date: 2024-12-21 01:15:45
Score: 5
Natty:
Report link

please see my code below. it seems working for me. might try adding your endpoint details and see. code is from https://learn.microsoft.com/en-us/azure/ai-services/openai/assistants-quickstart?tabs=command-line%2Cjavascript-key%2Ctypescript-keyless&pivots=programming-language-javascript

the package version I am using is: "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);

Reasons:
  • Blacklisted phrase (1): help me
  • Blacklisted phrase (0.5): I need
  • RegEx Blacklisted phrase (3): Can you help me
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: qkfang