Skip to content

Latest commit

 

History

History
1224 lines (868 loc) · 50.5 KB

File metadata and controls

1224 lines (868 loc) · 50.5 KB

Azure AI Assistants client library for Python

Use the AI Assistants client library (in preview) to:

  • Enumerate connections in your Azure AI Foundry project and get connection properties. For example, get the inference endpoint URL and credentials associated with your Azure OpenAI connection.
  • Develop Assistants using the Azure AI Assistants Service, leveraging an extensive ecosystem of models, tools, and capabilities from OpenAI, Microsoft, and other LLM providers. The Azure AI Assistants Service enables the building of Assistants for a wide range of generative AI use cases. The package is currently in preview.
  • Enable OpenTelemetry tracing.

Product documentation | Samples | API reference documentation | Package (PyPI) | SDK source code | AI Starter Template

Reporting issues

To report an issue with the client library, or request additional features, please open a GitHub issue here. Mention the package name "azure-ai-projects" in the title or content.

Table of contents

Getting started

Prerequisite

  • Python 3.9 or later.
  • An Azure subscription.
  • A project in Azure AI Foundry.
  • The project connection string. It can be found in your Azure AI Foundry project overview page, under "Project details". Below we will assume the environment variable PROJECT_CONNECTION_STRING was defined to hold this value.
  • Entra ID is needed to authenticate the client. Your application needs an object that implements the TokenCredential interface. Code samples here use DefaultAzureCredential. To get that working, you will need:
    • An appropriate role assignment. see Role-based access control in Azure AI Foundry portal. Role assigned can be done via the "Access Control (IAM)" tab of your Azure AI Project resource in the Azure portal.
    • Azure CLI installed.
    • You are logged into your Azure account by running az login.
    • Note that if you have multiple Azure subscriptions, the subscription that contains your Azure AI Project resource must be your default subscription. Run az account list --output table to list all your subscription and see which one is the default. Run az account set --subscription "Your Subscription ID or Name" to change your default subscription.

Install the package

pip install azure-ai-assistants

Key concepts

Create and authenticate the client

To construct a synchronous client:

import os
from azure.ai.assistants import AssistantsClient
from azure.identity import DefaultAzureCredential

assistants_client = AssistantsClient(
    endpoint=os.environ["PROJECT_ENDPOINT"],
    credential = AzureKeyCredential(os.environ["API_KEY"])
)

To construct an asynchronous client, Install the additional package aiohttp:

pip install aiohttp

and update the code above to import asyncio, and import AssistantsClient from the azure.ai.assistants.aio namespace:

import os
import asyncio
from azure.ai.assistants.aio import AssistantsClient
from azure.core.credentials import AzureKeyCredential

assistant_client = AssistantsClient(
   endpoint=os.environ["PROJECT_ENDPOINT"],
    credential=AzureKeyCredential(os.environ["API_KEY"]),
)

Examples

Create Assistant

Before creating an Assistant, you need to set up Azure resources to deploy your model. Create a New Assistant Quickstart details selecting and deploying your Assistant Setup.

Here is an example of how to create an Assistant:

assistant = assistants_client.create_assistant(
    model=os.environ["MODEL_DEPLOYMENT_NAME"],
    name="my-assistant",
    instructions="You are helpful assistant",
)

To allow Assistants to access your resources or custom functions, you need tools. You can pass tools to create_assistant by either toolset or combination of tools and tool_resources.

Here is an example of toolset:

functions = FunctionTool(user_functions)
code_interpreter = CodeInterpreterTool()

toolset = ToolSet()
toolset.add(functions)
toolset.add(code_interpreter)

assistant = assistants_client.create_assistant(
    model=os.environ["MODEL_DEPLOYMENT_NAME"],
    name="my-assistant",
    instructions="You are a helpful assistant",
    toolset=toolset,
)

Also notices that if you use asynchronous client, you use AsyncToolSet instead. Additional information related to AsyncFunctionTool be discussed in the later sections.

Here is an example to use tools and tool_resources:

file_search_tool = FileSearchTool(vector_store_ids=[vector_store.id])

# Notices that FileSearchTool as tool and tool_resources must be added or the assistant unable to search the file
assistant = assistants_client.create_assistant(
    model=os.environ["MODEL_DEPLOYMENT_NAME"],
    name="my-assistant",
    instructions="You are helpful assistant",
    tools=file_search_tool.definitions,
    tool_resources=file_search_tool.resources,
)

In the following sections, we show you sample code in either toolset or combination of tools and tool_resources.

Create Assistant with File Search

To perform file search by an Assistant, we first need to upload a file, create a vector store, and associate the file to the vector store. Here is an example:

file = assistants_client.upload_file_and_poll(file_path="product_info_1.md", purpose="assistants")
print(f"Uploaded file, file ID: {file.id}")

vector_store = assistants_client.create_vector_store_and_poll(file_ids=[file.id], name="my_vectorstore")
print(f"Created vector store, vector store ID: {vector_store.id}")

# Create file search tool with resources followed by creating assistant
file_search = FileSearchTool(vector_store_ids=[vector_store.id])

assistant = assistants_client.create_assistant(
    model=os.environ["MODEL_DEPLOYMENT_NAME"],
    name="my-assistant",
    instructions="Hello, you are helpful assistant and can search information from uploaded files",
    tools=file_search.definitions,
    tool_resources=file_search.resources,
)

Create Assistant with Enterprise File Search

We can upload file to Azure as it is shown in the example, or use the existing Azure blob storage. In the code below we demonstrate how this can be achieved. First we upload file to azure and create VectorStoreDataSource, which then is used to create vector store. This vector store is then given to the FileSearchTool constructor.

# We will upload the local file to Azure and will use it for vector store creation.
asset_uri = os.environ["AZURE_BLOB_URI"]

# Create a vector store with no file and wait for it to be processed
ds = VectorStoreDataSource(asset_identifier=asset_uri, asset_type=VectorStoreDataSourceAssetType.URI_ASSET)
vector_store = assistants_client.create_vector_store_and_poll(data_sources=[ds], name="sample_vector_store")
print(f"Created vector store, vector store ID: {vector_store.id}")

# Create a file search tool
file_search_tool = FileSearchTool(vector_store_ids=[vector_store.id])

# Notices that FileSearchTool as tool and tool_resources must be added or the assistant unable to search the file
assistant = assistants_client.create_assistant(
    model=os.environ["MODEL_DEPLOYMENT_NAME"],
    name="my-assistant",
    instructions="You are helpful assistant",
    tools=file_search_tool.definitions,
    tool_resources=file_search_tool.resources,
)

We also can attach files to the existing vector store. In the code snippet below, we first create an empty vector store and add file to it.

# Create a vector store with no file and wait for it to be processed
vector_store = assistants_client.create_vector_store_and_poll(data_sources=[], name="sample_vector_store")
print(f"Created vector store, vector store ID: {vector_store.id}")

ds = VectorStoreDataSource(asset_identifier=asset_uri, asset_type=VectorStoreDataSourceAssetType.URI_ASSET)
# Add the file to the vector store or you can supply data sources in the vector store creation
vector_store_file_batch = assistants_client.create_vector_store_file_batch_and_poll(
    vector_store_id=vector_store.id, data_sources=[ds]
)
print(f"Created vector store file batch, vector store file batch ID: {vector_store_file_batch.id}")

# Create a file search tool
file_search_tool = FileSearchTool(vector_store_ids=[vector_store.id])

Create Assistant with Code Interpreter

Here is an example to upload a file and use it for code interpreter by an Assistant:

file = assistants_client.upload_file_and_poll(
    file_path="nifty_500_quarterly_results.csv", purpose=FilePurpose.ASSISTANTS
)
print(f"Uploaded file, file ID: {file.id}")

code_interpreter = CodeInterpreterTool(file_ids=[file.id])

# Create assistant with code interpreter tool and tools_resources
assistant = assistants_client.create_assistant(
    model=os.environ["MODEL_DEPLOYMENT_NAME"],
    name="my-assistant",
    instructions="You are helpful assistant",
    tools=code_interpreter.definitions,
    tool_resources=code_interpreter.resources,
)

Create Assistant with Bing Grounding

To enable your Assistant to perform search through Bing search API, you use BingGroundingTool along with a connection.

Here is an example:

conn_id = os.environ["AZURE_BING_CONNECTION_ID"]

print(conn_id)

# Initialize assistant bing tool and add the connection id
bing = BingGroundingTool(connection_id=conn_id)

# Create assistant with the bing tool and process assistant run
with assistants_client:
    assistant = assistants_client.create_assistant(
        model=os.environ["MODEL_DEPLOYMENT_NAME"],
        name="my-assistant",
        instructions="You are a helpful assistant",
        tools=bing.definitions,
        headers={"x-ms-enable-preview": "true"},
    )

Create Assistant with Azure AI Search

Azure AI Search is an enterprise search system for high-performance applications. It integrates with Azure OpenAI Service and Azure Machine Learning, offering advanced search technologies like vector search and full-text search. Ideal for knowledge base insights, information discovery, and automation. Creating an Assistant with Azure AI Search requires an existing Azure AI Search Index. For more information and setup guides, see Azure AI Search Tool Guide.

Here is an example to integrate Azure AI Search:

conn_id = os.environ["AI_AZURE_AI_CONNECTION_ID"]

print(conn_id)

# Initialize assistant AI search tool and add the search index connection id
ai_search = AzureAISearchTool(
    index_connection_id=conn_id, index_name="sample_index", query_type=AzureAISearchQueryType.SIMPLE, top_k=3, filter=""
)

# Create assistant with AI search tool and process assistant run
with assistants_client:
    assistant = assistants_client.create_assistant(
        model=os.environ["MODEL_DEPLOYMENT_NAME"],
        name="my-assistant",
        instructions="You are a helpful assistant",
        tools=ai_search.definitions,
        tool_resources=ai_search.resources,
    )

If the assistant has found the relevant information in the index, the reference and annotation will be provided in the message response. In the example above, we replace the reference placeholder by the actual reference and url. Please note, that to get sensible result, the index needs to have "embedding", "token", "category" and "title" fields.

# Fetch and log all messages
messages = assistants_client.list_messages(thread_id=thread.id, order=ListSortOrder.ASCENDING)
for message in messages.data:
    if message.role == MessageRole.ASSISTANT and message.url_citation_annotations:
        placeholder_annotations = {
            annotation.text: f" [see {annotation.url_citation.title}] ({annotation.url_citation.url})"
            for annotation in message.url_citation_annotations
        }
        for message_text in message.text_messages:
            message_str = message_text.text.value
            for k, v in placeholder_annotations.items():
                message_str = message_str.replace(k, v)
            print(f"{message.role}: {message_str}")
    else:
        for message_text in message.text_messages:
            print(f"{message.role}: {message_text.text.value}")

Create Assistant with Function Call

You can enhance your Assistants by defining callback functions as function tools. These can be provided to create_assistant via either the toolset parameter or the combination of tools and tool_resources. Here are the distinctions:

  • toolset: When using the toolset parameter, you provide not only the function definitions and descriptions but also their implementations. The SDK will execute these functions within create_and_run_process or streaming . These functions will be invoked based on their definitions.
  • tools and tool_resources: When using the tools and tool_resources parameters, only the function definitions and descriptions are provided to create_assistant, without the implementations. The Run or event handler of stream will raise a requires_action status based on the function definitions. Your code must handle this status and call the appropriate functions.

For more details about calling functions by code, refer to sample_assistants_stream_eventhandler_with_functions.py and sample_assistants_functions.py.

For more details about requirements and specification of functions, refer to Function Tool Specifications

Here is an example to use user functions in toolset:

functions = FunctionTool(user_functions)
toolset = ToolSet()
toolset.add(functions)

assistant = assistants_client.create_assistant(
    model=os.environ["MODEL_DEPLOYMENT_NAME"],
    name="my-assistant",
    instructions="You are a helpful assistant",
    toolset=toolset,
)

For asynchronous functions, you must import AIProjectClient from azure.ai.projects.aio and use AsyncFunctionTool. Here is an example using asynchronous user functions:

from azure.ai.projects.aio import AIProjectClient
functions = AsyncFunctionTool(user_async_functions)

toolset = AsyncToolSet()
toolset.add(functions)

assistant = await assistants_client.create_assistant(
    model=os.environ["MODEL_DEPLOYMENT_NAME"],
    name="my-assistant",
    instructions="You are a helpful assistant",
    toolset=toolset,
)

Create Assistant With Azure Function Call

The AI assistant leverages Azure Functions triggered asynchronously via Azure Storage Queues. To enable the assistant to perform Azure Function calls, you must set up the corresponding AzureFunctionTool, specifying input and output queues as well as parameter definitions.

Example Python snippet illustrating how you create an assistant utilizing the Azure Function Tool:

azure_function_tool = AzureFunctionTool(
    name="foo",
    description="Get answers from the foo bot.",
    parameters={
        "type": "object",
        "properties": {
            "query": {"type": "string", "description": "The question to ask."},
            "outputqueueuri": {"type": "string", "description": "The full output queue uri."},
        },
    },
    input_queue=AzureFunctionStorageQueue(
        queue_name="azure-function-foo-input",
        storage_service_endpoint=storage_service_endpoint,
    ),
    output_queue=AzureFunctionStorageQueue(
        queue_name="azure-function-tool-output",
        storage_service_endpoint=storage_service_endpoint,
    ),
)

assistant = assistants_client.create_assistant(
    model=os.environ["MODEL_DEPLOYMENT_NAME"],
    name="azure-function-assistant-foo",
    instructions=f"You are a helpful support assistant. Use the provided function any time the prompt contains the string 'What would foo say?'. When you invoke the function, ALWAYS specify the output queue uri parameter as '{storage_service_endpoint}/azure-function-tool-output'. Always responds with \"Foo says\" and then the response from the tool.",
    tools=azure_function_tool.definitions,
)
print(f"Created assistant, assistant ID: {assistant.id}")

Limitations

Currently, the Azure Function integration for the AI Assistant has the following limitations:

  • Azure Functions integration is available only for non-streaming scenarios.
  • Supported trigger for Azure Function is currently limited to Queue triggers only. HTTP or other trigger types and streaming responses are not supported at this time.

Create and Deploy Azure Function

Before you can use the assistant with AzureFunctionTool, you need to create and deploy Azure Function.

Below is an example Python Azure Function responding to queue-triggered messages and placing responses on the output queue:

import azure.functions as func
import logging
import json

app = func.FunctionApp()

@app.get_weather(arg_name="inputQueue",
                   queue_name="input",
                   connection="AzureWebJobsStorage")
@app.queue_output(arg_name="outputQueue",
                  queue_name="output",
                  connection="AzureWebJobsStorage")
def get_weather(inputQueue: func.QueueMessage, outputQueue: func.Out[str]):
    try:
        messagepayload = json.loads(inputQueue.get_body().decode("utf-8"))
        location = messagepayload["location"]
        weather_result = f"Weather is 82 degrees and sunny in {location}."

        response_message = {
            "Value": weather_result,
            "CorrelationId": messagepayload["CorrelationId"]
        }

        outputQueue.set(json.dumps(response_message))

        logging.info(f"Sent message to output queue with message {response_message}")
    except Exception as e:
        logging.error(f"Error processing message: {e}")
        return

Important: Both input and output payloads must contain the CorrelationId, which must match in request and response.


Azure Function Project Creation and Deployment

To deploy your function to Azure properly, follow Microsoft's official documentation step by step:

Azure Functions Python Developer Guide

Summary of required steps:

  • Use the Azure CLI or Azure Portal to create an Azure Function App.
  • Enable System Managed Identity for your Azure Function App.
  • Assign appropriate permissions to your Azure Function App identity as outlined in the Role Assignments section below
  • Create input and output queues in Azure Storage.
  • Deploy your Function code.

Verification and Testing Azure Function

To ensure that your Azure Function deployment functions correctly:

  1. Place the following style message manually into the input queue (input):

{ "location": "Seattle", "CorrelationId": "42" }

Check the output queue (output) and validate the structured message response:

{ "Value": "The weather in Seattle is sunny and warm.", "CorrelationId": "42" }


Required Role Assignments (IAM Configuration)

Clearly assign the following Azure IAM roles to ensure correct permissions:

  1. Azure Function App's identity:

    • Enable system managed identity through Azure Function App > Settings > Identity.
    • Add permission to storage account:
      • Go to Storage Account > Access control (IAM) and add role assignment:
        • Storage Queue Data Contributor assigned to Azure Function managed identity
  2. Azure AI Project Identity:

Ensure your Azure AI Project identity has the following storage account permissions:

  • Storage Account Contributor
  • Storage Blob Data Contributor
  • Storage File Data Privileged Contributor
  • Storage Queue Data Contributor
  • Storage Table Data Contributor

Additional Important Configuration Notes

  • The Azure Function configured above uses the AzureWebJobsStorage connection string for queue connectivity. You may alternatively use managed identity-based connections as described in the official Azure Functions Managed Identity documentation.
  • Storage queues you specify (input & output) should already exist in the storage account before the Function deployment or invocation, created manually via Azure portal or CLI.
  • When using Azure storage account connection strings, make sure the account has enabled storage account key access (Storage Account > Settings > Configuration).

With the above steps complete, your Azure Function integration with your AI Assistant is ready for use.

Create Assistant With Logic Apps

Logic Apps allow HTTP requests to trigger actions. For more information, refer to the guide Logic App Workflows for Function Calling.

Your Logic App must be in the same resource group as your Azure AI Project, shown in the Azure Portal. Assistants SDK accesses Logic Apps through Workflow URLs, which are fetched and called as requests in functions.

Below is an example of how to create an Azure Logic App utility tool and register a function with it.

# Create the project client
assistants_client = AssistantsClient(
    endpoint=os.environ["PROJECT_ENDPOINT"],
    credential=DefaultAzureCredential(),
)

# Extract subscription and resource group from the project scope
subscription_id = os.environ["SUBSCRIPTION_ID"]
resource_group = os.environ["resource_group_name"]

# Logic App details
logic_app_name = "<LOGIC_APP_NAME>"
trigger_name = "<TRIGGER_NAME>"

# Create and initialize AzureLogicAppTool utility
logic_app_tool = AzureLogicAppTool(subscription_id, resource_group)
logic_app_tool.register_logic_app(logic_app_name, trigger_name)
print(f"Registered logic app '{logic_app_name}' with trigger '{trigger_name}'.")

# Create the specialized "send_email_via_logic_app" function for your assistant tools
send_email_func = create_send_email_function(logic_app_tool, logic_app_name)

# Prepare the function tools for the assistant
functions_to_use: Set = {
    fetch_current_datetime,
    send_email_func,  # This references the AzureLogicAppTool instance via closure
}

After this the functions can be incorporated normally into code using FunctionTool.

Create Assistant With OpenAPI

OpenAPI specifications describe REST operations against a specific endpoint. Assistants SDK can read an OpenAPI spec, create a function from it, and call that function against the REST endpoint without additional client-side execution.

Here is an example creating an OpenAPI tool (using anonymous authentication):

with open("./weather_openapi.json", "r") as f:
    openapi_weather = jsonref.loads(f.read())

with open("./countries.json", "r") as f:
    openapi_countries = jsonref.loads(f.read())

# Create Auth object for the OpenApiTool (note that connection or managed identity auth setup requires additional setup in Azure)
auth = OpenApiAnonymousAuthDetails()

# Initialize assistant OpenApi tool using the read in OpenAPI spec
openapi_tool = OpenApiTool(
    name="get_weather", spec=openapi_weather, description="Retrieve weather information for a location", auth=auth
)
openapi_tool.add_definition(
    name="get_countries", spec=openapi_countries, description="Retrieve a list of countries", auth=auth
)

# Create assistant with OpenApi tool and process assistant run
with assistants_client:
    assistant = assistants_client.create_assistant(
        model=os.environ["MODEL_DEPLOYMENT_NAME"],
        name="my-assistant",
        instructions="You are a helpful assistant",
        tools=openapi_tool.definitions,
    )

Create an Assistant with Fabric

To enable your Assistant to answer queries using Fabric data, use FabricTool along with a connection to the Fabric resource.

Here is an example:

conn_id = os.environ["FABRIC_CONNECTION_ID"]

print(conn_id)

# Initialize an Assistant Fabric tool and add the connection id
fabric = FabricTool(connection_id=conn_id)

# Create an Assistant with the Fabric tool and process an Assistant run
with assistants_client:
    assistant = assistants_client.create_assistant(
        model=os.environ["MODEL_DEPLOYMENT_NAME"],
        name="my-assistant",
        instructions="You are a helpful assistant",
        tools=fabric.definitions,
        headers={"x-ms-enable-preview": "true"},
    )

Create Thread

For each session or conversation, a thread is required. Here is an example:

thread = assistants_client.create_thread()

Create Thread with Tool Resource

In some scenarios, you might need to assign specific resources to individual threads. To achieve this, you provide the tool_resources argument to create_thread. In the following example, you create a vector store and upload a file, enable an Assistant for file search using the tools argument, and then associate the file with the thread using the tool_resources argument.

file = assistants_client.upload_file_and_poll(file_path="product_info_1.md", purpose="assistants")
print(f"Uploaded file, file ID: {file.id}")

vector_store = assistants_client.create_vector_store_and_poll(file_ids=[file.id], name="my_vectorstore")
print(f"Created vector store, vector store ID: {vector_store.id}")

# Create file search tool with resources followed by creating assistant
file_search = FileSearchTool(vector_store_ids=[vector_store.id])

assistant = assistants_client.create_assistant(
    model=os.environ["MODEL_DEPLOYMENT_NAME"],
    name="my-assistant",
    instructions="Hello, you are helpful assistant and can search information from uploaded files",
    tools=file_search.definitions,
)

print(f"Created assistant, ID: {assistant.id}")

# Create thread with file resources.
# If the assistant has multiple threads, only this thread can search this file.
thread = assistants_client.create_thread(tool_resources=file_search.resources)

List Threads

To list all threads attached to a given agent, use the list_threads API:

threads = project_client.agents.list_threads()

Create Message

To create a message for assistant to process, you pass user as role and a question as content:

message = assistants_client.create_message(thread_id=thread.id, role="user", content="Hello, tell me a joke")

Create Message with File Search Attachment

To attach a file to a message for content searching, you use MessageAttachment and FileSearchTool:

attachment = MessageAttachment(file_id=file.id, tools=FileSearchTool().definitions)
message = assistants_client.create_message(
    thread_id=thread.id, role="user", content="What feature does Smart Eyewear offer?", attachments=[attachment]
)

Create Message with Code Interpreter Attachment

To attach a file to a message for data analysis, use MessageAttachment and CodeInterpreterTool classes. You must pass CodeInterpreterTool as tools or toolset in create_assistant call or the file attachment cannot be opened for code interpreter.

Here is an example to pass CodeInterpreterTool as tool:

# Notice that CodeInterpreter must be enabled in the assistant creation,
# otherwise the assistant will not be able to see the file attachment for code interpretation
assistant = assistants_client.create_assistant(
    model=os.environ["MODEL_DEPLOYMENT_NAME"],
    name="my-assistant",
    instructions="You are helpful assistant",
    tools=CodeInterpreterTool().definitions,
)
print(f"Created assistant, assistant ID: {assistant.id}")

thread = assistants_client.create_thread()
print(f"Created thread, thread ID: {thread.id}")

# Create an attachment
attachment = MessageAttachment(file_id=file.id, tools=CodeInterpreterTool().definitions)

# Create a message
message = assistants_client.create_message(
    thread_id=thread.id,
    role="user",
    content="Could you please create bar chart in TRANSPORTATION sector for the operating profit from the uploaded csv file and provide file to me?",
    attachments=[attachment],
)

Azure blob storage can be used as a message attachment. In this case, use VectorStoreDataSource as a data source:

# We will upload the local file to Azure and will use it for vector store creation.
asset_uri = os.environ["AZURE_BLOB_URI"]
ds = VectorStoreDataSource(asset_identifier=asset_uri, asset_type=VectorStoreDataSourceAssetType.URI_ASSET)

# Create a message with the attachment
attachment = MessageAttachment(data_source=ds, tools=code_interpreter.definitions)
message = assistants_client.create_message(
    thread_id=thread.id, role="user", content="What does the attachment say?", attachments=[attachment]
)

Create Run, Run_and_Process, or Stream

To process your message, you can use create_run, create_and_process_run, or create_stream.

create_run requests the Assistant to process the message without polling for the result. If you are using function tools regardless as toolset or not, your code is responsible for polling for the result and acknowledging the status of Run. When the status is requires_action, your code is responsible for calling the function tools. For a code sample, visit sample_assistants_functions.py.

Here is an example of create_run and poll until the run is completed:

run = assistants_client.create_run(thread_id=thread.id, assistant_id=assistant.id)

# Poll the run as long as run status is queued or in progress
while run.status in ["queued", "in_progress", "requires_action"]:
    # Wait for a second
    time.sleep(1)
    run = assistants_client.get_run(thread_id=thread.id, run_id=run.id)

To have the SDK poll on your behalf and call function tools, use the create_and_process_run method. Note that function tools will only be invoked if they are provided as toolset during the create_assistant call.

Here is an example:

run = assistants_client.create_and_process_run(thread_id=thread.id, assistant_id=assistant.id)

With streaming, polling need not be considered. If function tools are provided as toolset during the create_assistant call, they will be invoked by the SDK.

Here is an example of streaming:

with assistants_client.create_stream(thread_id=thread.id, assistant_id=assistant.id) as stream:

    for event_type, event_data, _ in stream:

        if isinstance(event_data, MessageDeltaChunk):
            print(f"Text delta received: {event_data.text}")

        elif isinstance(event_data, ThreadMessage):
            print(f"ThreadMessage created. ID: {event_data.id}, Status: {event_data.status}")

        elif isinstance(event_data, ThreadRun):
            print(f"ThreadRun status: {event_data.status}")

        elif isinstance(event_data, RunStep):
            print(f"RunStep type: {event_data.type}, Status: {event_data.status}")

        elif event_type == AssistantStreamEvent.ERROR:
            print(f"An error occurred. Data: {event_data}")

        elif event_type == AssistantStreamEvent.DONE:
            print("Stream completed.")
            break

        else:
            print(f"Unhandled Event Type: {event_type}, Data: {event_data}")

In the code above, because an event_handler object is not passed to the create_stream function, the SDK will instantiate AssistantEventHandler or AsyncAssistantEventHandler as the default event handler and produce an iterable object with event_type and event_data. AssistantEventHandler and AsyncAssistantEventHandler are overridable. Here is an example:

# With AssistantEventHandler[str], the return type for each event functions is optional string.
class MyEventHandler(AssistantEventHandler[str]):

    def on_message_delta(self, delta: "MessageDeltaChunk") -> Optional[str]:
        return f"Text delta received: {delta.text}"

    def on_thread_message(self, message: "ThreadMessage") -> Optional[str]:
        return f"ThreadMessage created. ID: {message.id}, Status: {message.status}"

    def on_thread_run(self, run: "ThreadRun") -> Optional[str]:
        return f"ThreadRun status: {run.status}"

    def on_run_step(self, step: "RunStep") -> Optional[str]:
        return f"RunStep type: {step.type}, Status: {step.status}"

    def on_error(self, data: str) -> Optional[str]:
        return f"An error occurred. Data: {data}"

    def on_done(self) -> Optional[str]:
        return "Stream completed."

    def on_unhandled_event(self, event_type: str, event_data: Any) -> Optional[str]:
        return f"Unhandled Event Type: {event_type}, Data: {event_data}"
with assistants_client.create_stream(
    thread_id=thread.id, assistant_id=assistant.id, event_handler=MyEventHandler()
) as stream:
    for event_type, event_data, func_return in stream:
        print(f"Received data.")
        print(f"Streaming receive Event Type: {event_type}")
        print(f"Event Data: {str(event_data)[:100]}...")
        print(f"Event Function return: {func_return}\n")

As you can see, this SDK parses the events and produces various event types similar to OpenAI assistants. In your use case, you might not be interested in handling all these types and may decide to parse the events on your own. To achieve this, please refer to override base event handler.

Note: Multiple streaming processes may be chained behind the scenes.

When the SDK receives a `ThreadRun` event with the status `requires_action`, the next event will be `Done`, followed by termination. The SDK will submit the tool calls using the same event handler. The event handler will then chain the main stream with the tool stream.

Consequently, when you iterate over the streaming using a for loop similar to the example above, the for loop will receive events from the main stream followed by events from the tool stream.

Retrieve Message

To retrieve messages from assistants, use the following example:

messages = assistants_client.list_messages(thread_id=thread.id, order=ListSortOrder.ASCENDING)

# The messages are following in the reverse order,
# we will iterate them and output only text contents.
for data_point in messages.data:
    last_message_content = data_point.content[-1]
    if isinstance(last_message_content, MessageTextContent):
        print(f"{data_point.role}: {last_message_content.text.value}")

In addition, messages and messages.data[] offer helper properties such as text_messages, image_contents, file_citation_annotations, and file_path_annotations to quickly retrieve content from one message or all messages.

Retrieve File

Files uploaded by Assistants cannot be retrieved back. If your use case need to access the file content uploaded by the Assistants, you are advised to keep an additional copy accessible by your application. However, files generated by Assistants are retrievable by save_file or get_file_content.

Here is an example retrieving file ids from messages and save to the local drive:

messages = assistants_client.list_messages(thread_id=thread.id)
print(f"Messages: {messages}")

for image_content in messages.image_contents:
    file_id = image_content.image_file.file_id
    print(f"Image File ID: {file_id}")
    file_name = f"{file_id}_image_file.png"
    assistants_client.save_file(file_id=file_id, file_name=file_name)
    print(f"Saved image file to: {Path.cwd() / file_name}")

for file_path_annotation in messages.file_path_annotations:
    print(f"File Paths:")
    print(f"Type: {file_path_annotation.type}")
    print(f"Text: {file_path_annotation.text}")
    print(f"File ID: {file_path_annotation.file_path.file_id}")
    print(f"Start Index: {file_path_annotation.start_index}")
    print(f"End Index: {file_path_annotation.end_index}")

Here is an example to use get_file_content:

from pathlib import Path

async def save_file_content(client, file_id: str, file_name: str, target_dir: Optional[Union[str, Path]] = None):
    # Determine the target directory
    path = Path(target_dir).expanduser().resolve() if target_dir else Path.cwd()
    path.mkdir(parents=True, exist_ok=True)

    # Retrieve the file content
    file_content_stream = await client.get_file_content(file_id)
    if not file_content_stream:
        raise RuntimeError(f"No content retrievable for file ID '{file_id}'.")

    # Collect all chunks asynchronously
    chunks = []
    async for chunk in file_content_stream:
        if isinstance(chunk, (bytes, bytearray)):
            chunks.append(chunk)
        else:
            raise TypeError(f"Expected bytes or bytearray, got {type(chunk).__name__}")

    target_file_path = path / file_name

    # Write the collected content to the file synchronously
    with open(target_file_path, "wb") as file:
        for chunk in chunks:
            file.write(chunk)

Teardown

To remove resources after completing tasks, use the following functions:

# Delete the file when done
assistants_client.delete_vector_store(vector_store.id)
print("Deleted vector store")

assistants_client.delete_file(file_id=file.id)
print("Deleted file")

# Delete the assistant when done
assistants_client.delete_assistant(assistant.id)
print("Deleted assistant")

Tracing

You can add an Application Insights Azure resource to your Azure AI Foundry project. See the Tracing tab in your AI Foundry project. If one was enabled, you can get the Application Insights connection string, configure your Assistants, and observe the full execution path through Azure Monitor. Typically, you might want to start tracing before you create an Assistant.

Installation

Make sure to install OpenTelemetry and the Azure SDK tracing plugin via

pip install opentelemetry
pip install azure-ai-assistants azure-identity opentelemetry-sdk azure-core-tracing-opentelemetry

You will also need an exporter to send telemetry to your observability backend. You can print traces to the console or use a local viewer such as Aspire Dashboard.

To connect to Aspire Dashboard or another OpenTelemetry compatible backend, install OTLP exporter:

pip install opentelemetry-exporter-otlp

How to enable tracing

Here is a code sample that shows how to enable Azure Monitor tracing:

from opentelemetry import trace
from azure.monitor.opentelemetry import configure_azure_monitor

# Enable Azure Monitor tracing
application_insights_connection_string = os.environ["AI_APPINSIGHTS_CONNECTION_STRING"]
configure_azure_monitor(connection_string=application_insights_connection_string)

# enable additional instrumentations
enable_telemetry()

scenario = os.path.basename(__file__)
tracer = trace.get_tracer(__name__)

with tracer.start_as_current_span(scenario):
    with assistants_client:

In addition, you might find helpful to see the tracing logs in console. You can achieve by the following code:

from azure.ai.assistants.telemetry import enable_telemetry

enable_telemetry(destination=sys.stdout)

How to trace your own functions

The decorator trace_function is provided for tracing your own function calls using OpenTelemetry. By default the function name is used as the name for the span. Alternatively you can provide the name for the span as a parameter to the decorator.

This decorator handles various data types for function parameters and return values, and records them as attributes in the trace span. The supported data types include:

  • Basic data types: str, int, float, bool
  • Collections: list, dict, tuple, set
    • Special handling for collections:
      • If a collection (list, dict, tuple, set) contains nested collections, the entire collection is converted to a string before being recorded as an attribute.
      • Sets and dictionaries are always converted to strings to ensure compatibility with span attributes.

Object types are omitted, and the corresponding parameter is not traced.

The parameters are recorded in attributes code.function.parameter.<parameter_name> and the return value is recorder in attribute code.function.return.value

Troubleshooting

Logging

The client uses the standard Python logging library. The SDK logs HTTP request and response details, which may be useful in troubleshooting. To log to stdout, add the following:

import sys
import logging

# Acquire the logger for this client library. Use 'azure' to affect both
# 'azure.core` and `azure.ai.inference' libraries.
logger = logging.getLogger("azure")

# Set the desired logging level. logging.INFO or logging.DEBUG are good options.
logger.setLevel(logging.DEBUG)

# Direct logging output to stdout:
handler = logging.StreamHandler(stream=sys.stdout)
# Or direct logging output to a file:
# handler = logging.FileHandler(filename="sample.log")
logger.addHandler(handler)

# Optional: change the default logging format. Here we add a timestamp.
#formatter = logging.Formatter("%(asctime)s:%(levelname)s:%(name)s:%(message)s")
#handler.setFormatter(formatter)

By default logs redact the values of URL query strings, the values of some HTTP request and response headers (including Authorization which holds the key or token), and the request and response payloads. To create logs without redaction, add logging_enable = True to the client constructor:

assistants_client = AIProjectClient.from_connection_string(
    endpoint=os.environ["PROJECT_ENDPOINT"],
    credential=DefaultAzureCredential(),
    logging_enable = True
)

Note that the log level must be set to logging.DEBUG (see above code). Logs will be redacted with any other log level.

Be sure to protect non redacted logs to avoid compromising security.

For more information, see Configure logging in the Azure libraries for Python

Reporting issues

To report an issue with the client library, or request additional features, please open a GitHub issue here. Mention the package name "azure-ai-assistants" in the title or content.

Next steps

Have a look at the Samples folder, containing fully runnable Python code for synchronous and asynchronous clients.

Explore the AI Starter Template. This template creates an Azure AI Foundry hub, project and connected resources including Azure OpenAI Service, AI Search and more. It also deploys a simple chat application to Azure Container Apps.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.