BuilderIO / micro-agent

An AI agent that writes (actually useful) code for you
https://www.builder.io/blog/micro-agent
MIT License
2.61k stars 206 forks source link

Support Azure Ai OpenAi api #54

Closed M-Sakr closed 1 month ago

M-Sakr commented 1 month ago

Hi, i'm big fan of your project, i use azure ai, and i have instance at openai.azure.com, is there a way to use it?, how can i configure it?

steve8708 commented 1 month ago

you absolutely can, set the OPENAI_API_ENDPOINT to your azure base url to do that, and use your azure openai key for the api key as well

yuichiromukaiyama commented 1 month ago

@steve8708

Thank you for this wonderful project. I would like to use this amazing application with Azure OpenAI as well. However, when I tried to use it, a 404 error was displayed.

% npx micro-agent config set OPENAI_API_ENDPOINT=https://${NAME}.openai.azure.com/openai/deployments/gpt-35-turbo-16k
% npx micro-agent config set OPENAI_KEY=${AZURE OPENAI API KEY}
% npx micro-agent config set MODEL=gpt-35-turbo-16k
% npx micro-agent       

┌  🦾 Micro Agent
│
◇  What would you like to do?
│  hello world
│
◒  
✖ 404 Resource not found
    at APIError.generate (file:///micro-agent/node_modules/openai/error.mjs:50:20)
    at OpenAI.makeStatusError (file:///micro-agent/node_modules/openai/core.mjs:268:25)
    at OpenAI.makeRequest (file:///micro-agent/node_modules/openai/core.mjs:312:30)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async getSimpleCompletion (file:///micro-agent/node_modules/@builder.io/micro-agent/dist/cli.mjs:1056:22)
    at async getFileSuggestion (file:///micro-agent/node_modules/@builder.io/micro-agent/dist/cli.mjs:955:9)
    at async interactiveMode (file:///micro-agent/node_modules/@builder.io/micro-agent/dist/cli.mjs:735:33)
    at async file:///micro-agent/node_modules/@builder.io/micro-agent/dist/cli.mjs:1777:11

Is the usage correct? Upon checking the code, it looks like the following, but in the case of Azure, we may need to pass the API key in the header.

https://github.com/BuilderIO/micro-agent/blob/be4743c37b31bb83f12a9e64e282ce62f240a1d2/src/helpers/llm.ts#L57

before

const openai = new OpenAI({
  apiKey: openaiKey,
  baseURL: endpoint
});

after

const client = new OpenAI({
  apiKey: openaiKey,
  baseURL: endpoint
  defaultHeaders: {
    "api-key": openaiKey,
  },
  defaultQuery: {
    "api-version": "2024-05-01-preview",
  },
});

client.chat.completions
  .create({
    model: "gpt-35-turbo-16k",
    messages: [{ role: "user", content: "hello world" }],
  })
  .then((e) => {
    console.log(e);
  });