Azure OpenAI Web Search

According the documentation above, ai-sdk allows web search via azure. However, google search, and chatbots do not return more details on Azure’s support of web search.

import { createAzure } from '@ai-sdk/azure';
import { generateText } from 'ai';

const endpoint = process.env.AZURE_OPENAI_ENDPOINT;
const apiKey = process.env.AZURE_OPENAI_API_KEY;
const deploymentName = process.env.AZURE_OPENAI_DEPLOYMENT;

if (!endpoint || !apiKey || !deploymentName) {
  console.error(
    'Error: One or more required env vars are missing: AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_API_KEY, AZURE_OPENAI_DEPLOYMENT',
  );
  process.exit(1);
}

// Extract resource name from endpoint
const resourceName = endpoint.replace(/^https?:\/\//, '').split('.')[0];

const azure = createAzure({
  resourceName,
  apiKey,
});

const prompt = process.argv.slice(2).join(' ');
if (!prompt) {
  console.error('Usage: pnpm tsx azure-chat.ts "Your prompt here"');
  process.exit(1);
}

async function main() {
  try {
    const { text } = await generateText({
      model: azure.responses(deploymentName as string),
      prompt,
      // tools: {
      //   web_search_preview: azure.tools.webSearchPreview({
      //     // optional configuration:
      //     searchContextSize: 'high',
      //     userLocation: {
      //       type: 'approximate',
      //       city: 'San Francisco',
      //       region: 'California',
      //     },
      //   }),
      //   // Force web search tool:
      //   toolChoice: { type: 'tool', toolName: 'web_search_preview' },
      //   // You can add providerOptions here if needed
      // },
    });
    console.log('\nAzure OpenAI response:\n', text);
  } catch (err) {
    console.error('Error communicating with Azure OpenAI:', err);
    process.exit(1);
  }
}

main();

My above code works. The commented part is copied from the documentation. But if I uncomment it , error occurs.

TypeError: undefined is not an object (evaluating ‘azure.tools.webSearchPreview’)

@ai-sdk/azure": “^1.3.19”,
“ai”: “4.3.4”,

Hey! This appears to be an error in our docs. I don’t believe we support provider tools with Azure yet. I will update the docs to remove.

2 Likes

Nico. Appreciate your response! two quick follow-up questions, 1) any plan to support Azure Tools soon? 2) is the aisdk documentation page open sourced?

Nico has submitted this fix already but yes the docs are open source. Here’s the link to the page in quesiton

1 Like

Great! Thank you. Look forward to more progress and integration with Azure OpenAI

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.