Skip to content

OpenAI Assistants Basics

The ai chat commands allow you to create, manage, and interact with OpenAI's Assistants and persisted threads.

Prerequisites

Before you begin, you'll need to install the ai CLI and set up Azure OpenAI.

Install the ai CLI
Setup Azure OpenAI

Create an Assistant

Create a simple assistant
ai chat assistant create --name MyAssistant

Threads

Start an interactive chat
ai chat --interactive
Continue with a previous thread
ai chat --interactive --thread-id ID
Ask a question and save the thread ID
ai chat --question "..." --output-thread-id myNewThread.txt
Use a saved thread ID
ai chat --question "..." --thread-id @myNewThread.txt
Interactive chat with thread ID and save history
ai chat --interactive --thread-id @myNewThread.txt --output-chat-history history.jsonl

The ai dev new command allows you to generate sample code that demonstrates how to use OpenAI Assistants.

Prerequisites

Before you begin, you'll need to install the ai CLI and set up Azure OpenAI.

Install the ai CLI
Setup Azure OpenAI

List samples

List available templates
ai dev new list
AI - Azure AI CLI, Version 1.0.0
Copyright (c) 2024 Microsoft Corporation. All Rights Reserved.

Name                                                      Short Name                                       Language                        
------------------------------------------------------    ---------------------------------------------    --------------------------------
Environment Variables                                     .env                                                                             
Azure AI Inference Chat Completions (Streaming)           az-inference-chat-streaming                      C#, Python                      
Helper Function Class Library                             helper-functions                                 C#                              
OpenAI Assistants                                         openai-asst                                      C#, JavaScript, Python          
OpenAI Assistants (Streaming)                             openai-asst-streaming                            C#, JavaScript, Python          
OpenAI Assistants (w/ Code Interpreter)                   openai-asst-streaming-with-code                  C#, JavaScript, Python          
OpenAI Assistants (w/ File Search)                        openai-asst-streaming-with-file-search           C#, JavaScript, Python          
OpenAI Assistants (w/ Functions)                          openai-asst-streaming-with-functions             C#, JavaScript, Python          
OpenAI Assistants Webpage                                 openai-asst-webpage                              JavaScript, TypeScript          
OpenAI Assistants Webpage (w/ Functions)                  openai-asst-webpage-with-functions               JavaScript                      
OpenAI Chat Completions                                   openai-chat                                      C#, Go, Java, JavaScript, Python
OpenAI Chat Completions (Streaming)                       openai-chat-streaming                            C#, Go, Java, JavaScript, Python
OpenAI Chat Completions (w/ Data + AI Search)             openai-chat-streaming-with-data                  C#, Go, Java, JavaScript, Python
OpenAI Chat Completions (w/ Functions)                    openai-chat-streaming-with-functions             C#, Go, JavaScript, Python      
OpenAI Chat Webpage                                       openai-chat-webpage                              JavaScript, TypeScript          
OpenAI Chat Webpage (w/ Functions)                        openai-chat-webpage-with-functions               JavaScript, TypeScript          
OpenAI Chat Webpage (w/ Speech input/output)              openai-chat-webpage-with-speech                  TypeScript                      
OpenAI Chat Webpage (w/ Functions + Speech)               openai-chat-webpage-with-speech-and-functions    TypeScript                      
Phi-3 Chat Completions (w/ ONNX)                          phi3-onnx-chat-streaming                         C#                              
Phi-3 Chat Completions (w/ ONNX + Functions)              phi3-onnx-chat-streaming-with-functions          C#                              
Semantic Kernel Chat Completions (Streaming)              sk-chat-streaming                                C#                              
Semantic Kernel Chat Completions (w/ Data + AI Search)    sk-chat-streaming-with-data                      C#                              
Semantic Kernel Chat Completions (w/ Functions)           sk-chat-streaming-with-functions                 C#                              
Semantic Kernel Chat Completions (w/ Agents)              sk-chat-with-agents                              C#                              
Speech-to-text                                            speech-to-text                                   C#, Python                      
Speech-to-text (w/ Continuous recognition)                speech-to-text-continuous-reco                   C#, Python                      
Speech-to-text (w/ File input)                            speech-to-text-with-file                         C#, Python                      
Speech-to-text (w/ Keyword detection)                     speech-to-text-with-keyword                      C#, Python                      
Speech-to-text (w/ Translation)                           speech-to-text-with-translation                  C#, Python                      
Text-to-speech                                            text-to-speech                                   C#, Python                      
Text-to-speech (w/ File output)                           text-to-speech-with-file                         C#, Python                      
List only C# samples
ai dev new list --csharp
Filter the list by name
ai dev new list openai-asst --csharp
ai dev new list openai-asst-streaming --csharp

Generate, build, and run a sample

Generate a C# sample that demonstrates how to use OpenAI Assistants with streaming.

ai dev new openai-asst-streaming --csharp
cd openai-asst-streaming-cs
See the code; learn how it works...

Program.cs
OpenAIAssistantsStreamingClass.cs

Deep dive on how it works

Install dependencies
dotnet restore
Run the sample
ai dev shell
dotnet run

Generate a C# sample that demonstrates how to use OpenAI Assistants without streaming.

aid ev new openai-asst --csharp
cd openai-asst-csharp
See the code; learn how it works...

Program.cs
OpenAIAssistantsClass.cs

Deep dive on how it works

Install dependencies
dotnet restore
Run the sample
ai dev shell
dotnet run

List samples

List available templates
ai dev new list
AI - Azure AI CLI, Version 1.0.0
Copyright (c) 2024 Microsoft Corporation. All Rights Reserved.

Name                                                      Short Name                                       Language                        
------------------------------------------------------    ---------------------------------------------    --------------------------------
Environment Variables                                     .env                                                                             
Azure AI Inference Chat Completions (Streaming)           az-inference-chat-streaming                      C#, Python                      
Helper Function Class Library                             helper-functions                                 C#                              
OpenAI Assistants                                         openai-asst                                      C#, JavaScript, Python          
OpenAI Assistants (Streaming)                             openai-asst-streaming                            C#, JavaScript, Python          
OpenAI Assistants (w/ Code Interpreter)                   openai-asst-streaming-with-code                  C#, JavaScript, Python          
OpenAI Assistants (w/ File Search)                        openai-asst-streaming-with-file-search           C#, JavaScript, Python          
OpenAI Assistants (w/ Functions)                          openai-asst-streaming-with-functions             C#, JavaScript, Python          
OpenAI Assistants Webpage                                 openai-asst-webpage                              JavaScript, TypeScript          
OpenAI Assistants Webpage (w/ Functions)                  openai-asst-webpage-with-functions               JavaScript                      
OpenAI Chat Completions                                   openai-chat                                      C#, Go, Java, JavaScript, Python
OpenAI Chat Completions (Streaming)                       openai-chat-streaming                            C#, Go, Java, JavaScript, Python
OpenAI Chat Completions (w/ Data + AI Search)             openai-chat-streaming-with-data                  C#, Go, Java, JavaScript, Python
OpenAI Chat Completions (w/ Functions)                    openai-chat-streaming-with-functions             C#, Go, JavaScript, Python      
OpenAI Chat Webpage                                       openai-chat-webpage                              JavaScript, TypeScript          
OpenAI Chat Webpage (w/ Functions)                        openai-chat-webpage-with-functions               JavaScript, TypeScript          
OpenAI Chat Webpage (w/ Speech input/output)              openai-chat-webpage-with-speech                  TypeScript                      
OpenAI Chat Webpage (w/ Functions + Speech)               openai-chat-webpage-with-speech-and-functions    TypeScript                      
Phi-3 Chat Completions (w/ ONNX)                          phi3-onnx-chat-streaming                         C#                              
Phi-3 Chat Completions (w/ ONNX + Functions)              phi3-onnx-chat-streaming-with-functions          C#                              
Semantic Kernel Chat Completions (Streaming)              sk-chat-streaming                                C#                              
Semantic Kernel Chat Completions (w/ Data + AI Search)    sk-chat-streaming-with-data                      C#                              
Semantic Kernel Chat Completions (w/ Functions)           sk-chat-streaming-with-functions                 C#                              
Semantic Kernel Chat Completions (w/ Agents)              sk-chat-with-agents                              C#                              
Speech-to-text                                            speech-to-text                                   C#, Python                      
Speech-to-text (w/ Continuous recognition)                speech-to-text-continuous-reco                   C#, Python                      
Speech-to-text (w/ File input)                            speech-to-text-with-file                         C#, Python                      
Speech-to-text (w/ Keyword detection)                     speech-to-text-with-keyword                      C#, Python                      
Speech-to-text (w/ Translation)                           speech-to-text-with-translation                  C#, Python                      
Text-to-speech                                            text-to-speech                                   C#, Python                      
Text-to-speech (w/ File output)                           text-to-speech-with-file                         C#, Python                      
List only JavaScript samples
ai dev new list --javascript
Filter the list by name
ai dev new list openai-asst --javascript
ai dev new list openai-asst-streaming --javascript

Generate, build, and run a sample

Generate a JavaScript sample that demonstrates how to use OpenAI Assistants with streaming.

ai dev new openai-asst-streaming --javascript
cd openai-asst-streaming-js
See the code; learn how it works...

main.js
OpenAIAssistantsStreamingClass.js

Deep dive on how it works

Install dependencies
npm install
Run the sample
ai dev shell
node main.js

Generate a JavaScript sample that demonstrates how to use OpenAI Assistants without streaming.

ai dev new openai-asst --javascript
cd openai-asst-js
See the code; learn how it works...

main.js
OpenAIAssistantsClass.js

Deep dive on how it works

Install dependencies
npm install
Run the sample
ai dev shell
node main.js

List samples

List available templates
ai dev new list
AI - Azure AI CLI, Version 1.0.0
Copyright (c) 2024 Microsoft Corporation. All Rights Reserved.

Name                                                      Short Name                                       Language                        
------------------------------------------------------    ---------------------------------------------    --------------------------------
Environment Variables                                     .env                                                                             
Azure AI Inference Chat Completions (Streaming)           az-inference-chat-streaming                      C#, Python                      
Helper Function Class Library                             helper-functions                                 C#                              
OpenAI Assistants                                         openai-asst                                      C#, JavaScript, Python          
OpenAI Assistants (Streaming)                             openai-asst-streaming                            C#, JavaScript, Python          
OpenAI Assistants (w/ Code Interpreter)                   openai-asst-streaming-with-code                  C#, JavaScript, Python          
OpenAI Assistants (w/ File Search)                        openai-asst-streaming-with-file-search           C#, JavaScript, Python          
OpenAI Assistants (w/ Functions)                          openai-asst-streaming-with-functions             C#, JavaScript, Python          
OpenAI Assistants Webpage                                 openai-asst-webpage                              JavaScript, TypeScript          
OpenAI Assistants Webpage (w/ Functions)                  openai-asst-webpage-with-functions               JavaScript                      
OpenAI Chat Completions                                   openai-chat                                      C#, Go, Java, JavaScript, Python
OpenAI Chat Completions (Streaming)                       openai-chat-streaming                            C#, Go, Java, JavaScript, Python
OpenAI Chat Completions (w/ Data + AI Search)             openai-chat-streaming-with-data                  C#, Go, Java, JavaScript, Python
OpenAI Chat Completions (w/ Functions)                    openai-chat-streaming-with-functions             C#, Go, JavaScript, Python      
OpenAI Chat Webpage                                       openai-chat-webpage                              JavaScript, TypeScript          
OpenAI Chat Webpage (w/ Functions)                        openai-chat-webpage-with-functions               JavaScript, TypeScript          
OpenAI Chat Webpage (w/ Speech input/output)              openai-chat-webpage-with-speech                  TypeScript                      
OpenAI Chat Webpage (w/ Functions + Speech)               openai-chat-webpage-with-speech-and-functions    TypeScript                      
Phi-3 Chat Completions (w/ ONNX)                          phi3-onnx-chat-streaming                         C#                              
Phi-3 Chat Completions (w/ ONNX + Functions)              phi3-onnx-chat-streaming-with-functions          C#                              
Semantic Kernel Chat Completions (Streaming)              sk-chat-streaming                                C#                              
Semantic Kernel Chat Completions (w/ Data + AI Search)    sk-chat-streaming-with-data                      C#                              
Semantic Kernel Chat Completions (w/ Functions)           sk-chat-streaming-with-functions                 C#                              
Semantic Kernel Chat Completions (w/ Agents)              sk-chat-with-agents                              C#                              
Speech-to-text                                            speech-to-text                                   C#, Python                      
Speech-to-text (w/ Continuous recognition)                speech-to-text-continuous-reco                   C#, Python                      
Speech-to-text (w/ File input)                            speech-to-text-with-file                         C#, Python                      
Speech-to-text (w/ Keyword detection)                     speech-to-text-with-keyword                      C#, Python                      
Speech-to-text (w/ Translation)                           speech-to-text-with-translation                  C#, Python                      
Text-to-speech                                            text-to-speech                                   C#, Python                      
Text-to-speech (w/ File output)                           text-to-speech-with-file                         C#, Python                      
List only Python samples
ai dev new list --python
Filter the list by name
ai dev new list openai-asst --python
ai dev new list openai-asst-streaming --python

Generate, build, and run a sample

Generate a Python sample that demonstrates how to use OpenAI Assistants with streaming.

ai dev new openai-asst-streaming --python
cd openai-asst-streaming-py
See the code; learn how it works...

main.py
openai_assistants_streaming.py

Deep dive on how it works

Create virtual environment
python -m venv env
env/Scripts/activate
Install requirements
pip install -r requirements.txt
Run the sample
ai dev shell
python main.py

Generate a Python sample that demonstrates how to use OpenAI Assistants without streaming.

ai dev new openai-asst --python
cd openai-asst-py
See the code; learn how it works...

main.py
openai_assistants.py

Deep dive on how it works

Create virtual environment
python -m venv env
env/Scripts/activate
Install requirements
pip install -r requirements.txt
Run the sample
ai dev shell
python main.py