Get started with the Azure AI CLI
Note
Azure AI Studio is currently in public preview. This preview is provided without a service-level agreement, and we don't recommend it for production workloads. Certain features might not be supported or might have constrained capabilities. For more information, see Supplemental Terms of Use for Microsoft Azure Previews.
The Azure AI command-line interface (CLI) is a cross-platform command-line tool to connect to Azure AI services and execute control-plane and data-plane operations without having to write any code. The Azure AI CLI allows the execution of commands through a terminal using interactive command-line prompts or via script.
You can easily use the Azure AI CLI to experiment with key Azure AI features and see how they work with your use cases. Within minutes, you can set up all the required Azure resources needed, and build a customized copilot using Azure OpenAI chat completions APIs and your own data. You can try it out interactively, or script larger processes to automate your own workflows and evaluations as part of your CI/CD system.
Prerequisites
To use the Azure AI CLI, you need to install the prerequisites:
- The Azure AI SDK, following the instructions here
- The Azure CLI (not the Azure
AICLI), following the instructions here - The .NET SDK, following the instructions here for your operating system and distro
Note
If you launched VS Code from the Azure AI Studio, you don't need to install the prerequisites. See options without installing later in this article.
Install the CLI
The following set of commands are provided for a few popular operating systems.
To install the .NET SDK, Azure CLI, and Azure AI CLI, run the following command.
dotnet tool install --prerelease --global Azure.AI.CLI
To update the Azure AI CLI, run the following command:
dotnet tool update --prerelease --global Azure.AI.CLI
Run the Azure AI CLI without installing it
You can install the Azure AI CLI locally as described previously, or run it using a preconfigured Docker container in VS Code.
Option 1: Using VS Code (web) in Azure AI Studio
VS Code (web) in Azure AI Studio creates and runs the development container on a compute instance. To get started with this approach, follow the instructions in Work with Azure AI projects in VS Code.
Our prebuilt development environments are based on a docker container that has the Azure AI SDK generative packages, the Azure AI CLI, the Prompt flow SDK, and other tools. It's configured to run VS Code remotely inside of the container. The docker container is similar to this Dockerfile, and is based on Microsoft's Python 3.10 Development Container Image.
OPTION 2: Visual Studio Code Dev Container
You can run the Azure AI CLI in a Docker container using VS Code Dev Containers:
- Follow the installation instructions for VS Code Dev Containers.
- Clone the aistudio-copilot-sample repository and open it with VS Code:
git clone https://github.com/azure/aistudio-copilot-sample code aistudio-copilot-sample - Select the Reopen in Dev Containers button. If it doesn't appear, open the command palette (
Ctrl+Shift+Pon Windows and Linux,Cmd+Shift+Pon Mac) and run theDev Containers: Reopen in Containercommand.
Try the Azure AI CLI
The AI CLI offers many capabilities, including an interactive chat experience, tools to work with prompt flows and search and speech services, and tools to manage AI services.
If you plan to use the AI CLI as part of your development, we recommend you start by running ai init, which guides you through setting up your Azure resources and connections in your development environment.
Try ai help to learn more about these capabilities.
ai init
The ai init command allows interactive and non-interactive selection or creation of Azure AI hub resources. When an Azure AI hub resource is selected or created, the associated resource keys and region are retrieved and automatically stored in the local AI configuration datastore.
You can initialize the Azure AI CLI by running the following command:
ai init
If you run the Azure AI CLI with VS Code (Web) coming from Azure AI Studio, your development environment will already be configured. The ai init command takes fewer steps: you confirm the existing project and attached resources.
If your development environment hasn't already been configured with an existing project, or you select the Initialize something else option, there will be a few flows you can choose when running ai init: Initialize a new AI project, Initialize an existing AI project, or Initialize standalone resources.
The following table describes the scenarios for each flow.
| Scenario | Description |
|---|---|
| Initialize a new AI project | Choose if you don't have an existing AI project that you have been working with in the Azure AI Studio. The ai init command walks you through creating or attaching resources. |
| Initialize an existing AI project | Choose if you have an existing AI project you want to work with. The ai init command checks your existing linked resources, and asks you to set anything that hasn't been set before. |
| Initialize standalone resources | Choose if you're building a simple solution connected to a single AI service, or if you want to attach more resources to your development environment |
Working with an AI project is recommended when using the Azure AI Studio and/or connecting to multiple AI services. Projects come with An Azure AI hub resource that houses related projects and shareable resources like compute and connections to services. Projects also allow you to connect code to cloud resources (storage and model deployments), save evaluation results, and host code behind online endpoints. You're prompted to create and/or attach Azure AI Services to your project.
Initializing standalone resources is recommended when building simple solutions connected to a single AI service. You can also choose to initialize more standalone resources after initializing a project.
The following resources can be initialized standalone, or attached to projects:
- Azure AI services: Includes Azure OpenAI, Azure AI Speech, and Azure AI Vision.
- Azure OpenAI: Provides access to OpenAI's powerful language models.
- Azure AI Search: Provides keyword, vector, and hybrid search capabilities.
- Azure AI Speech: Provides speech recognition, synthesis, and translation.
Initializing a new AI project
- Run
ai initand choose Initialize new AI project. - Select your subscription. You might be prompted to sign in through an interactive flow.
- Select your Azure AI hub resource, or create a new one. An Azure AI hub resource can have multiple projects that can share resources.
- Select the name of your new project. There are some suggested names, or you can enter a custom one. Once you submit, the project might take a minute to create.
- Select the resources you want to attach to the project. You can skip resource types you don't want to attach.
ai initchecks you have the connections you need for the attached resources, and your development environment is configured with your new project.
Initializing an existing AI project
- Enter
ai initand choose "Initialize an existing AI project". - Select your subscription. You might be prompted to sign in through an interactive flow.
- Select the project from the list.
- Select the resources you want to attach to the project. There should be a default selection based on what is already attached to the project. You can choose to create new resources to attach.
ai initchecks you have the connections you need for the attached resources, and your development environment is configured with the project.
Initializing standalone resources
- Enter
ai initand choose "Initialize standalone resources". - Select the type of resource you want to initialize.
- Select your subscription. You might be prompted to sign in through an interactive flow.
- Choose the desired resources from the list(s). You can create new resources to attach inline.
ai initchecks you have the connections you need for the attached resources, and your development environment is configured with attached resources.
Project connections
When working the Azure AI CLI, you want to use your project's connections. Connections are established to attached resources and allow you to integrate services with your project. You can have project-specific connections, or connections shared at the Azure AI hub resource level. For more information, see Azure AI hub resources and connections.
When you run ai init your project connections get set in your development environment, allowing seamless integration with AI services. You can view these connections by running ai service connection list, and further manage these connections with ai service connection subcommands.
Any updates you make to connections in the Azure AI CLI is reflected in Azure AI Studio, and vice versa.
ai dev
ai dev helps you configure the environment variables in your development environment.
After running ai init, you can run the following command to set a .env file populated with environment variables you can reference in your code.
ai dev new .env
ai service
ai service helps you manage your connections to resources and services.
ai service resourcelets you list, create or delete Azure AI hub resources.ai service projectlets you list, create, or delete Azure AI projects.ai service connectionlets you list, create, or delete connections. These are the connections to your attached services.
ai flow
ai flow lets you work with prompt flows in an interactive way. You can create new flows, invoke and test existing flows, serve a flow locally to test an application experience, upload a local flow to the Azure AI Studio, or deploy a flow to an endpoint.
The following steps help you test out each capability. They assume you have run ai init.
- Run
ai flow new --name mynewflowto create a new flow folder based on a template for a chat flow. - Open the
flow.dag.yamlfile that was created in the previous step.- Update the
deployment_nameto match the chat deployment attached to your project. You can runai config @chat.deploymentto get the correct name. - Update the connection field to be Default_AzureOpenAI. You can run
ai service connection listto verify your connection names.
- Update the
ai flow invoke --name mynewflow --input question=hello- this runs the flow with provided input and return a response.ai flow serve --name mynewflow- this will locally serve the application and you can test interactively in a new window.ai flow package --name mynewflow- this packages the flow as a Dockerfile.ai flow upload --name mynewflow- this uploads the flow to the AI Studio, where you can continue working on it with the prompt flow UI.- You can deploy an uploaded flow to an online endpoint for inferencing via the Azure AI Studio UI, see Deploy a flow for real-time inference for more details.
Project connections with flows
As mentioned in step 2 above, your flow.dag.yaml should reference connection and deployment names matching those attached to your project.
If you're working in your own development environment (including Codespaces), you might need to manually update these fields so that your flow runs connected to Azure resources.
If you launched VS Code from the AI Studio, you are in an Azure-connected custom container experience, and you can work directly with flows stored in the shared folder. These flow files are the same underlying files prompt flow references in the Studio, so they should already be configured with your project connections and deployments. To learn more about the folder structure in the VS Code container experience, see Work with Azure AI projects in VS Code
ai chat
Once you have initialized resources and have a deployment, you can chat interactively or non-interactively with the AI language model using the ai chat command. The CLI has more examples of ways to use the ai chat capabilities, simply enter ai chat to try them. Once you have tested the chat capabilities, you can add in your own data.
Here's an example of interactive chat:
ai chat --interactive --system @prompt.txt
Here's an example of non-interactive chat:
ai chat --system @prompt.txt --user "Tell me about Azure AI Studio"
Chat with your data
Once you have tested the basic chat capabilities, you can add your own data using an Azure AI Search vector index.
- Create a search index based on your data
- Interactively chat with an AI system grounded in your data
- Clear the index to prepare for other chat explorations
ai search index update --name <index_name> --files "*.md"
ai chat --index-name <index_name> --interactive
When you use search index update to create or update an index (the first step above), ai config stores that index name. Run ai config in the CLI to see more usage details.
If you want to set a different existing index for subsequent chats, use:
ai config --set search.index.name <index_name>
If you want to clear the set index name, use
ai config --clear search.index.name
ai help
The Azure AI CLI is interactive with extensive help commands. You can explore capabilities not covered in this document by running:
ai help
Next steps
Feedback
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: https://aka.ms/ContentUserFeedback.
Submit and view feedback for