azure-ai-projects


Nameazure-ai-projects JSON
Version 1.0.0b1 PyPI version JSON
download
home_pagehttps://github.com/Azure/azure-sdk-for-python/tree/main/sdk/ai/azure-ai-projects
SummaryMicrosoft Azure AI Projects Client Library for Python
upload_time2024-11-15 17:21:43
maintainerNone
docs_urlNone
authorMicrosoft Corporation
requires_python>=3.8
licenseMIT License
keywords azure azure sdk
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            
# Azure AI Projects client library for Python

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

* **Enumerate connections** in your Azure AI Studio project and get connection properties.
For example, get the inference endpoint URL and credentials associated with your Azure OpenAI connection.
* **Get an authenticated Inference client** to do chat completions, for the default Azure OpenAI or AI Services connections in your Azure AI Studio project. Supports the AzureOpenAI client from the `openai` package, or clients from the `azure-ai-inference` package.
* **Develop Agents using the Azure AI Agent Service**, leveraging an extensive ecosystem of models, tools, and capabilities from OpenAI, Microsoft, and other LLM providers. The Azure AI Agent Service enables the building of Agents for a wide range of generative AI use cases. The package is currently in private preview.
* **Run Evaluations** to assess the performance of generative AI applications using various evaluators and metrics. It includes built-in evaluators for quality, risk, and safety, and allows custom evaluators for specific needs.
* **Enable OpenTelemetry tracing**.

[Product documentation](https://aka.ms/azsdk/azure-ai-projects/product-doc)
| [Samples][samples]
| [API reference documentation](https://aka.ms/azsdk/azure-ai-projects/python/reference)
| [Package (PyPI)](https://aka.ms/azsdk/azure-ai-projects/python/package)
| [SDK source code](https://aka.ms/azsdk/azure-ai-projects/python/code)
| [AI Starter Template](https://aka.ms/azsdk/azure-ai-projects/python/ai-starter-template)

## Table of contents

- [Getting started](#getting-started)
  - [Prerequisite](#prerequisite)
  - [Install the package](#install-the-package)
- [Key concepts](#key-concepts)
  - [Create and authenticate the client](#create-and-authenticate-the-client)
- [Examples](#examples)
  - [Enumerate connections](#enumerate-connections)
    - [Get properties of all connections](#get-properties-of-all-connections)
    - [Get properties of all connections of a particular type](#get-properties-of-all-connections-of-a-particular-type)
    - [Get properties of a default connection](#get-properties-of-a-default-connection)
    - [Get properties of a connection by its connection name](#get-properties-of-a-connection-by-its-connection-name)
  - [Get an authenticated ChatCompletionsClient](#get-an-authenticated-chatcompletionsclient)
  - [Get an authenticated AzureOpenAI client](#get-an-authenticated-azureopenai-client)
  - [Agents (Private Preview)](#agents-private-preview)
    - [Create an Agent](#create-agent) with:
      - [File Search](#create-agent-with-file-search)
      - [Code interpreter](#create-agent-with-code-interpreter)
      - [Bing grounding](#create-agent-with-bing-grounding)
      - [Azure AI Search](#create-agent-with-azure-ai-search)
      - [Function call](#create-agent-with-function-call)
    - [Create thread](#create-thread) with
      - [Tool resource](#create-thread-with-tool-resource)
    - [Create message](#create-message) with:
      - [File search attachment](#create-message-with-file-search-attachment)
      - [Code interpreter attachment](#create-message-with-code-interpreter-attachment)
    - [Execute Run, Run_and_Process, or Stream](#create-run-run_and_process-or-stream)
    - [Retrieve message](#retrieve-message)
    - [Retrieve file](#retrieve-file)
    - [Tear down by deleting resource](#teardown)
    - [Tracing](#tracing)
  - [Evaluation](#evaluation)
    - [Evaluator](#evaluator)
    - [Run Evaluation in the cloud](#run-evaluation-in-the-cloud)
      - [Evaluators](#evaluators)
      - [Data to be evaluated](#data-to-be-evaluated)
        - [[Optional] Azure OpenAI Model](#optional-azure-openai-model)
        - [Example Remote Evaluation](#example-remote-evaluation)
  - [Tracing](#tracing)
    - [Installation](#installation)
    - [Tracing example](#tracing-example)
- [Troubleshooting](#troubleshooting)
  - [Exceptions](#exceptions)
  - [Logging](#logging)
  - [Reporting issues](#reporting-issues)
- [Next steps](#next-steps)
- [Contributing](#contributing)

## Getting started

### Prerequisite

- Python 3.8 or later.
- An [Azure subscription][azure_sub].
- A [project in Azure AI Studio](https://learn.microsoft.com/azure/ai-studio/how-to/create-projects?tabs=ai-studio).
- The project connection string. It can be found in your Azure AI Studio 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](https://learn.microsoft.com/python/api/azure-core/azure.core.credentials.tokencredential) interface. Code samples here use [DefaultAzureCredential](https://learn.microsoft.com/python/api/azure-identity/azure.identity.defaultazurecredential). To get that working, you will need:
  * The `Contributor` role. Role assigned can be done via the "Access Control (IAM)" tab of your Azure AI Project resource in the Azure portal.
  * [Azure CLI](https://learn.microsoft.com/cli/azure/install-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

```bash
pip install azure-ai-projects
```

## Key concepts

### Create and authenticate the client

The class factory method `from_connection_string` is used to construct the client. To construct a synchronous client:

```python
import os
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential

project_client = AIProjectClient.from_connection_string(
    credential=DefaultAzureCredential(),
    conn_str=os.environ["PROJECT_CONNECTION_STRING"],
)
```

To construct an asynchronous client, Install the additional package [aiohttp](https://pypi.org/project/aiohttp/):

```bash
pip install aiohttp
```

and update the code above to import `asyncio`, and import `AIProjectClient` from the `azure.ai.projects.aio` namespace:

```python
import os
import asyncio
from azure.ai.projects.aio import AIProjectClient
from azure.core.credentials import AzureKeyCredential

project_client = AIProjectClient.from_connection_string(
    credential=DefaultAzureCredential(),
    conn_str=os.environ["PROJECT_CONNECTION_STRING"],
)
```

## Examples

### Enumerate connections

Your Azure AI Studio project has a "Management center". When you enter it, you will see a tab named "Connected resources" under your project. The `.connections` operations on the client allow you to enumerate the connections and get connection properties. Connection properties include the resource URL and authentication credentials, among other things.

Below are code examples of the connection operations. Full samples can be found under the "connetions" folder in the [package samples][samples].

#### Get properties of all connections

To list the properties of all the connections in the Azure AI Studio project:

```python
connections = project_client.connections.list()
for connection in connections:
    print(connection)
```

#### Get properties of all connections of a particular type

To list the properties of connections of a certain type (here Azure OpenAI):

```python
connections = project_client.connections.list(
    connection_type=ConnectionType.AZURE_OPEN_AI,
)
for connection in connections:
    print(connection)

```

#### Get properties of a default connection

To get the properties of the default connection of a certain type (here Azure OpenAI),
with its authentication credentials:

```python
connection = project_client.connections.get_default(
    connection_type=ConnectionType.AZURE_OPEN_AI,
    include_credentials=True,  # Optional. Defaults to "False".
)
print(connection)
```

If the call was made with `include_credentials=True`, depending on the value of `connection.authentication_type`, either `connection.key` or `connection.token_credential`
will be populated. Otherwise both will be `None`.

#### Get properties of a connection by its connection name

To get the connection properties of a connection named `connection_name`:

```python
connection = project_client.connections.get(
    connection_name=connection_name,
    include_credentials=True  # Optional. Defaults to "False"
)
print(connection)
```

### Get an authenticated ChatCompletionsClient

Your Azure AI Studio project may have one or more AI models deployed that support chat completions. These could be OpenAI models, Microsoft models, or models from other providers. Use the code below to get an already authenticated [ChatCompletionsClient](https://learn.microsoft.com/python/api/azure-ai-inference/azure.ai.inference.chatcompletionsclient?view=azure-python-preview) from the [azure-ai-inference](https://pypi.org/project/azure-ai-inference/) package, and execute a chat completions call.

First, install the package:

```bash
pip install azure-ai-inference
```

Then run this code (replace "gpt-4o" with your model deployment name):

```python
inference_client = project_client.inference.get_chat_completions_client()

response = inference_client.complete(
    model="gpt-4o", # Model deployment name
    messages=[UserMessage(content="How many feet are in a mile?")]
)

print(response.choices[0].message.content)
```

See the "inference" folder in the [package samples][samples] for additional samples, including getting an authenticated [EmbeddingsClient](https://learn.microsoft.com/python/api/azure-ai-inference/azure.ai.inference.embeddingsclient?view=azure-python-preview).

### Get an authenticated AzureOpenAI client

Your Azure AI Studio project may have one or more OpenAI models deployed that support chat completions. Use the code below to get an already authenticated [AzureOpenAI](https://github.com/openai/openai-python?tab=readme-ov-file#microsoft-azure-openai) from the [openai](https://pypi.org/project/openai/) package, and execute a chat completions call.

First, install the package:

```bash
pip install openai
```

Then run the code below. Replace `gpt-4o` with your model deployment name, and update the `api_version` value with one found in the "Data plane - inference" row [in this table](https://learn.microsoft.com/azure/ai-services/openai/reference#api-specs).

```python
aoai_client = project_client.inference.get_azure_openai_client(api_version="2024-06-01")

response = aoai_client.chat.completions.create(
    model="gpt-4o", # Model deployment name
    messages=[
        {
            "role": "user",
            "content": "How many feet are in a mile?",
        },
    ],
)

print(response.choices[0].message.content)
```

See the "inference" folder in the [package samples][samples] for additional samples.

### Agents (Private Preview)

Agents in the Azure AI Projects client library are designed to facilitate various interactions and operations within your AI projects. They serve as the core components that manage and execute tasks, leveraging different tools and resources to achieve specific goals. The following steps outline the typical sequence for interacting with Agents. See the "agents" folder in the [package samples][samples] for additional Agent samples.

Agents are actively being developed. A sign-up form for private preview is coming soon.

#### Create Agent

Here is an example of how to create an Agent:
<!-- SNIPPET:sample_agents_basics.create_agent -->

```python
agent = project_client.agents.create_agent(
    model="gpt-4-1106-preview",
    name="my-assistant",
    instructions="You are helpful assistant",
)
```

<!-- END SNIPPET -->

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

Here is an example of `toolset`:
<!-- SNIPPET:sample_agents_run_with_toolset.create_agent_toolset -->

```python
functions = FunctionTool(user_functions)
code_interpreter = CodeInterpreterTool()

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

agent = project_client.agents.create_agent(
    model="gpt-4-1106-preview", name="my-assistant", instructions="You are a helpful assistant", toolset=toolset
)
```

<!-- END SNIPPET -->

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`:
<!-- SNIPPET:sample_agents_vector_store_batch_file_search.create_agent_with_tools_and_tool_resources -->

```python
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
agent = project_client.agents.create_agent(
    model="gpt-4-1106-preview",
    name="my-assistant",
    instructions="You are helpful assistant",
    tools=file_search_tool.definitions,
    tool_resources=file_search_tool.resources,
)
```

<!-- END SNIPPET -->

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

#### Create Agent with File Search

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

<!-- SNIPPET:sample_agents_file_search.upload_file_create_vector_store_and_agent_with_file_search_tool -->

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

vector_store = project_client.agents.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 agent
file_search = FileSearchTool(vector_store_ids=[vector_store.id])

agent = project_client.agents.create_agent(
    model="gpt-4-1106-preview",
    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,
)
```

<!-- END SNIPPET -->

#### Create Agent with Code Interpreter

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

<!-- SNIPPET:sample_agents_code_interpreter.upload_file_and_create_agent_with_code_interpreter -->

```python
file = project_client.agents.upload_file_and_poll(
    file_path="nifty_500_quarterly_results.csv", purpose=FilePurpose.AGENTS
)
print(f"Uploaded file, file ID: {file.id}")

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

# create agent with code interpreter tool and tools_resources
agent = project_client.agents.create_agent(
    model="gpt-4-1106-preview",
    name="my-assistant",
    instructions="You are helpful assistant",
    tools=code_interpreter.definitions,
    tool_resources=code_interpreter.resources,
)
```

<!-- END SNIPPET -->

#### Create Agent with Bing Grounding

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

Here is an example:

<!-- SNIPPET:sample_agents_bing_grounding.create_agent_with_bing_grounding_tool -->

```python
bing_connection = project_client.connections.get(connection_name=os.environ["BING_CONNECTION_NAME"])
conn_id = bing_connection.id

print(conn_id)

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

# Create agent with the bing tool and process assistant run
with project_client:
    agent = project_client.agents.create_agent(
        model="gpt-4-1106-preview",
        name="my-assistant",
        instructions="You are a helpful assistant",
        tools=bing.definitions,
        headers={"x-ms-enable-preview": "true"},
    )
```

<!-- END SNIPPET -->

#### Create Agent 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

Here is an example to integrate Azure AI Search:

<!-- SNIPPET:sample_agents_azure_ai_search.create_agent_with_azure_ai_search_tool -->

```python
conn_list = project_client.connections.list()
conn_id = ""
for conn in conn_list:
    if conn.connection_type == "CognitiveSearch":
        conn_id = conn.id
        break

print(conn_id)

# Initialize agent AI search tool and add the search index connection id
ai_search = AzureAISearchTool()
ai_search.add_index(conn_id, "sample_index")

# Create agent with AI search tool and process assistant run
with project_client:
    agent = project_client.agents.create_agent(
        model="gpt-4-1106-preview",
        name="my-assistant",
        instructions="You are a helpful assistant",
        tools=ai_search.definitions,
        headers={"x-ms-enable-preview": "true"},
    )
```

<!-- END SNIPPET -->

#### Create Agent with Function Call

You can enhance your Agents by defining callback functions as function tools. These can be provided to `create_agent` 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_agent`, 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_agents_stream_eventhandler_with_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_stream_eventhandler_with_functions.py) and [`sample_agents_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_functions.py).

Here is an example to use [user functions](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/user_functions.py) in `toolset`:
<!-- SNIPPET:sample_agents_stream_eventhandler_with_toolset.create_agent_with_function_tool -->

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

agent = project_client.agents.create_agent(
    model="gpt-4-1106-preview", name="my-assistant", instructions="You are a helpful assistant", toolset=toolset
)
```

<!-- END SNIPPET -->

For asynchronous functions, you must import `AIProjectClient` from `azure.ai.projects.aio` and use `AsyncFunctionTool`.   Here is an example using [asynchronous user functions](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/async_samples/user_async_functions.py):

```python
from azure.ai.projects.aio import AIProjectClient
```

<!-- SNIPPET:sample_agents_run_with_toolset_async.create_agent_with_async_function_tool -->

```python
functions = AsyncFunctionTool(user_async_functions)

toolset = AsyncToolSet()
toolset.add(functions)

agent = await project_client.agents.create_agent(
    model="gpt-4-1106-preview", name="my-assistant", instructions="You are a helpful assistant", toolset=toolset
)
```

<!-- END SNIPPET -->

#### Create Thread

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

<!-- SNIPPET:sample_agents_basics.create_thread -->

```python
thread = project_client.agents.create_thread()
```

<!-- END SNIPPET -->

#### 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 Agent for file search using the `tools` argument, and then associate the file with the thread using the `tool_resources` argument.

<!-- SNIPPET:sample_agents_with_resources_in_thread.create_agent_and_thread_for_file_search -->

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

vector_store = project_client.agents.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 agent
file_search = FileSearchTool(vector_store_ids=[vector_store.id])

agent = project_client.agents.create_agent(
    model="gpt-4-1106-preview",
    name="my-assistant",
    instructions="Hello, you are helpful assistant and can search information from uploaded files",
    tools=file_search.definitions,
)

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

# Create thread with file resources.
# If the agent has multiple threads, only this thread can search this file.
thread = project_client.agents.create_thread(tool_resources=file_search.resources)
```

<!-- END SNIPPET -->
#### Create Message

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

<!-- SNIPPET:sample_agents_basics.create_message -->

```python
message = project_client.agents.create_message(thread_id=thread.id, role="user", content="Hello, tell me a joke")
```

<!-- END SNIPPET -->

#### Create Message with File Search Attachment

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

<!-- SNIPPET:sample_agents_with_file_search_attachment.create_message_with_attachment -->

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

<!-- END SNIPPET -->

#### Create Message with Code Interpreter Attachment

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

Here is an example to pass `CodeInterpreterTool` as tool:

<!-- SNIPPET:sample_agents_with_code_interpreter_file_attachment.create_agent_and_message_with_code_interpreter_file_attachment -->

```python
# notice that CodeInterpreter must be enabled in the agent creation,
# otherwise the agent will not be able to see the file attachment for code interpretation
agent = project_client.agents.create_agent(
    model="gpt-4-1106-preview",
    name="my-assistant",
    instructions="You are helpful assistant",
    tools=CodeInterpreterTool().definitions,
)
print(f"Created agent, agent ID: {agent.id}")

thread = project_client.agents.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 = project_client.agents.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],
)
```

<!-- END SNIPPET -->

#### 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 Agent 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_agents_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_functions.py).

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

<!-- SNIPPET:sample_agents_basics.create_run -->

```python
run = project_client.agents.create_run(thread_id=thread.id, assistant_id=agent.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 = project_client.agents.get_run(thread_id=thread.id, run_id=run.id)
```

<!-- END SNIPPET -->

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_agent` call.

Here is an example:

<!-- SNIPPET:sample_agents_run_with_toolset.create_and_process_run -->

```python
run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id)
```

<!-- END SNIPPET -->

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

Here is an example:

<!-- SNIPPET:sample_agents_stream_eventhandler.create_stream -->

```python
with project_client.agents.create_stream(
    thread_id=thread.id, assistant_id=agent.id, event_handler=MyEventHandler()
) as stream:
    stream.until_done()
```

<!-- END SNIPPET -->

The event handler is optional. Here is an example:

<!-- SNIPPET:sample_agents_stream_eventhandler.stream_event_handler -->

```python
class MyEventHandler(AgentEventHandler):
    def on_message_delta(self, delta: "MessageDeltaChunk") -> None:
        for content_part in delta.delta.content:
            if isinstance(content_part, MessageDeltaTextContent):
                text_value = content_part.text.value if content_part.text else "No text"
                print(f"Text delta received: {text_value}")

    def on_thread_message(self, message: "ThreadMessage") -> None:
        print(f"ThreadMessage created. ID: {message.id}, Status: {message.status}")

    def on_thread_run(self, run: "ThreadRun") -> None:
        print(f"ThreadRun status: {run.status}")

    def on_run_step(self, step: "RunStep") -> None:
        print(f"RunStep type: {step.type}, Status: {step.status}")

    def on_error(self, data: str) -> None:
        print(f"An error occurred. Data: {data}")

    def on_done(self) -> None:
        print("Stream completed.")

    def on_unhandled_event(self, event_type: str, event_data: Any) -> None:
        print(f"Unhandled Event Type: {event_type}, Data: {event_data}")
```

<!-- END SNIPPET -->

#### Retrieve Message

To retrieve messages from agents, use the following example:

<!-- SNIPPET:sample_agents_basics.list_messages -->

```python
messages = project_client.agents.list_messages(thread_id=thread.id)

# The messages are following in the reverse order,
# we will iterate them and output only text contents.
for data_point in reversed(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}")
```

<!-- END SNIPPET -->

Depending on the use case, if you expect the Agents to return only text messages, `list_messages` should be sufficient.
If you are using tools, consider using the `get_messages` function instead. This function classifies the message content and returns properties such as `text_messages`, `image_contents`, `file_citation_annotations`, and `file_path_annotations`.

### Retrieve File

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

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

<!-- SNIPPET:sample_agents_code_interpreter.get_messages_and_save_files -->

```python
messages = project_client.agents.get_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"
    project_client.agents.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}")
```

<!-- END SNIPPET -->

Here is an example to use `get_file_content`:

```python
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:

<!-- SNIPPET:sample_agents_file_search.teardown -->

```python
# Delete the file when done
project_client.agents.delete_vector_store(vector_store.id)
print("Deleted vector store")

project_client.agents.delete_file(file_id=file.id)
print("Deleted file")

# Delete the agent when done
project_client.agents.delete_agent(agent.id)
print("Deleted agent")
```

<!-- END SNIPPET -->

### Evaluation

Evaluation in Azure AI Project client library is designed to assess the performance of generative AI applications in the cloud. The output of Generative AI application is quantitively measured with mathematical based metrics, AI-assisted quality and safety metrics. Metrics are defined as evaluators. Built-in or custom evaluators can provide comprehensive insights into the application's capabilities and limitations.

#### Evaluator

Evaluators are custom or prebuilt classes or functions that are designed to measure the quality of the outputs from language models or generative AI applications.

Evaluators are made available via [azure-ai-evaluation][azure_ai_evaluation] SDK for local experience and also in [Evaluator Library][evaluator_library] in Azure AI Studio for using them in the cloud.

More details on built-in and custom evaluators can be found [here][evaluators].

#### Run Evaluation in the cloud

To run evaluation in the cloud the following are needed:

- Evaluators
- Data to be evaluated
- [Optional] Azure Open AI model.

##### Evaluators

For running evaluator in the cloud, evaluator `ID` is needed. To get it via code you use [azure-ai-evaluation][azure_ai_evaluation]

```python
# pip install azure-ai-evaluation

from azure.ai.evaluation import RelevanceEvaluator

evaluator_id = RelevanceEvaluator.id
```

##### Data to be evaluated

Evaluation in the cloud supports data in form of `jsonl` file. Data can be uploaded via the helper method `upload_file` on the project client.

```python
# Upload data for evaluation and get dataset id
data_id, _ = project_client.upload_file("<data_file.jsonl>")
```

##### [Optional] Azure OpenAI Model

Azure AI Studio project comes with a default Azure Open AI endpoint which can be easily accessed using following code. This gives you the endpoint details for you Azure OpenAI endpoint. Some of the evaluators need model that supports chat completion.

```python
default_connection = project_client.connections.get_default(connection_type=ConnectionType.AZURE_OPEN_AI)
```

##### Example Remote Evaluation

```python
import os
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from azure.ai.projects.models import Evaluation, Dataset, EvaluatorConfiguration, ConnectionType
from azure.ai.evaluation import F1ScoreEvaluator, RelevanceEvaluator, HateUnfairnessEvaluator


# Create project client
project_client = AIProjectClient.from_connection_string(
    credential=DefaultAzureCredential(),
    conn_str=os.environ["PROJECT_CONNECTION_STRING"],
)

# Upload data for evaluation and get dataset id
data_id, _ = project_client.upload_file("<data_file.jsonl>")

deployment_name = "<deployment_name>"
api_version = "<api_version>"

# Create an evaluation
evaluation = Evaluation(
    display_name="Remote Evaluation",
    description="Evaluation of dataset",
    data=Dataset(id=data_id),
    evaluators={
        "f1_score": EvaluatorConfiguration(
            id=F1ScoreEvaluator.id,
        ),
        "relevance": EvaluatorConfiguration(
            id=RelevanceEvaluator.id,
            init_params={
                "model_config": default_connection.to_evaluator_model_config(
                    deployment_name=deployment_name, api_version=api_version
                )
            },
        ),
        "violence": EvaluatorConfiguration(
            id=ViolenceEvaluator.id,
            init_params={"azure_ai_project": project_client.scope},
        ),
    },
)


evaluation_response = project_client.evaluations.create(
    evaluation=evaluation,
)

# Get evaluation
get_evaluation_response = project_client.evaluations.get(evaluation_response.id)

print("----------------------------------------------------------------")
print("Created evaluation, evaluation ID: ", get_evaluation_response.id)
print("Evaluation status: ", get_evaluation_response.status)
if isinstance(get_evaluation_response.properties, dict):
    print("AI Studio URI: ", get_evaluation_response.properties["AiStudioEvaluationUri"])
print("----------------------------------------------------------------")
```

NOTE: For running evaluators locally refer to [Evaluate with the Azure AI Evaluation SDK][evaluators].

### Tracing

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

#### Installation

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

```bash
pip install opentelemetry
pip install 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](https://learn.microsoft.com/dotnet/aspire/fundamentals/dashboard/standalone?tabs=bash).

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

```bash
pip install opentelemetry-exporter-otlp
```

#### Tracing example

Here is a code sample to be included above `create_agent`:

<!-- SNIPPET:sample_agents_basics_with_azure_monitor_tracing.enable_tracing -->

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

# Enable Azure Monitor tracing
application_insights_connection_string = project_client.telemetry.get_connection_string()
if not application_insights_connection_string:
    print("Application Insights was not enabled for this project.")
    print("Enable it via the 'Tracing' tab in your AI Studio project page.")
    exit()
configure_azure_monitor(connection_string=application_insights_connection_string)

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

with tracer.start_as_current_span(scenario):
    with project_client:
```

<!-- END SNIPPET -->

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

```python
project_client.telemetry.enable(destination=sys.stdout)
```

## Troubleshooting

### Exceptions

Client methods that make service calls raise an [HttpResponseError](https://learn.microsoft.com/python/api/azure-core/azure.core.exceptions.httpresponseerror) exception for a non-success HTTP status code response from the service. The exception's `status_code` will hold the HTTP response status code (with `reason` showing the friendly name). The exception's `error.message` contains a detailed message that may be helpful in diagnosing the issue:

```python
from azure.core.exceptions import HttpResponseError

...

try:
    result = project_client.connections.list()
except HttpResponseError as e:
    print(f"Status code: {e.status_code} ({e.reason})")
    print(e.message)
```

For example, when you provide wrong credentials:

```text
Status code: 401 (Unauthorized)
Operation returned an invalid status 'Unauthorized'
```

### Logging

The client uses the standard [Python logging library](https://docs.python.org/3/library/logging.html). The SDK logs HTTP request and response details, which may be useful in troubleshooting. To log to stdout, add the following:

```python
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:

```python
project_client = AIProjectClient.from_connection_string(
    credential=DefaultAzureCredential(),
    conn_str=os.environ["PROJECT_CONNECTION_STRING"],
    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](https://aka.ms/azsdk/python/logging)

### Reporting issues

To report issues with the client library, or request additional features, please open a GitHub issue [here](https://github.com/Azure/azure-sdk-for-python/issues)

## Next steps

Have a look at the [Samples](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/ai/azure-ai-projects/samples) folder, containing fully runnable Python code for synchronous and asynchronous clients.

Explore the [AI Starter Template](https://aka.ms/azsdk/azure-ai-projects/python/ai-starter-template). This template creates an Azure AI Studio 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][code_of_conduct]. For more information,
see the Code of Conduct FAQ or contact opencode@microsoft.com with any
additional questions or comments.

<!-- LINKS -->
[samples]: https://aka.ms/azsdk/azure-ai-projects/python/samples/
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/
[entra_id]: https://learn.microsoft.com/azure/ai-services/authentication?tabs=powershell#authenticate-with-microsoft-entra-id
[azure_identity_credentials]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#credentials
[azure_identity_pip]: https://pypi.org/project/azure-identity/
[default_azure_credential]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#defaultazurecredential
[pip]: https://pypi.org/project/pip/
[azure_sub]: https://azure.microsoft.com/free/
[evaluators]: https://learn.microsoft.com/azure/ai-studio/how-to/develop/evaluate-sdk
[azure_ai_evaluation]: https://learn.microsoft.com/python/api/overview/azure/ai-evaluation-readme?view=azure-python-preview
[evaluator_library]: https://learn.microsoft.com/azure/ai-studio/how-to/evaluate-generative-ai-app#view-and-manage-the-evaluators-in-the-evaluator-library

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/ai/azure-ai-projects",
    "name": "azure-ai-projects",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "azure, azure sdk",
    "author": "Microsoft Corporation",
    "author_email": "azpysdkhelp@microsoft.com",
    "download_url": "https://files.pythonhosted.org/packages/c8/d9/94dcecb4ff3941b9c6fdcbf4be6ae19182274cdd101c8ec6744b4d31d093/azure_ai_projects-1.0.0b1.tar.gz",
    "platform": null,
    "description": "\n# Azure AI Projects client library for Python\n\nUse the AI Projects client library (in preview) to:\n\n* **Enumerate connections** in your Azure AI Studio project and get connection properties.\nFor example, get the inference endpoint URL and credentials associated with your Azure OpenAI connection.\n* **Get an authenticated Inference client** to do chat completions, for the default Azure OpenAI or AI Services connections in your Azure AI Studio project. Supports the AzureOpenAI client from the `openai` package, or clients from the `azure-ai-inference` package.\n* **Develop Agents using the Azure AI Agent Service**, leveraging an extensive ecosystem of models, tools, and capabilities from OpenAI, Microsoft, and other LLM providers. The Azure AI Agent Service enables the building of Agents for a wide range of generative AI use cases. The package is currently in private preview.\n* **Run Evaluations** to assess the performance of generative AI applications using various evaluators and metrics. It includes built-in evaluators for quality, risk, and safety, and allows custom evaluators for specific needs.\n* **Enable OpenTelemetry tracing**.\n\n[Product documentation](https://aka.ms/azsdk/azure-ai-projects/product-doc)\n| [Samples][samples]\n| [API reference documentation](https://aka.ms/azsdk/azure-ai-projects/python/reference)\n| [Package (PyPI)](https://aka.ms/azsdk/azure-ai-projects/python/package)\n| [SDK source code](https://aka.ms/azsdk/azure-ai-projects/python/code)\n| [AI Starter Template](https://aka.ms/azsdk/azure-ai-projects/python/ai-starter-template)\n\n## Table of contents\n\n- [Getting started](#getting-started)\n  - [Prerequisite](#prerequisite)\n  - [Install the package](#install-the-package)\n- [Key concepts](#key-concepts)\n  - [Create and authenticate the client](#create-and-authenticate-the-client)\n- [Examples](#examples)\n  - [Enumerate connections](#enumerate-connections)\n    - [Get properties of all connections](#get-properties-of-all-connections)\n    - [Get properties of all connections of a particular type](#get-properties-of-all-connections-of-a-particular-type)\n    - [Get properties of a default connection](#get-properties-of-a-default-connection)\n    - [Get properties of a connection by its connection name](#get-properties-of-a-connection-by-its-connection-name)\n  - [Get an authenticated ChatCompletionsClient](#get-an-authenticated-chatcompletionsclient)\n  - [Get an authenticated AzureOpenAI client](#get-an-authenticated-azureopenai-client)\n  - [Agents (Private Preview)](#agents-private-preview)\n    - [Create an Agent](#create-agent) with:\n      - [File Search](#create-agent-with-file-search)\n      - [Code interpreter](#create-agent-with-code-interpreter)\n      - [Bing grounding](#create-agent-with-bing-grounding)\n      - [Azure AI Search](#create-agent-with-azure-ai-search)\n      - [Function call](#create-agent-with-function-call)\n    - [Create thread](#create-thread) with\n      - [Tool resource](#create-thread-with-tool-resource)\n    - [Create message](#create-message) with:\n      - [File search attachment](#create-message-with-file-search-attachment)\n      - [Code interpreter attachment](#create-message-with-code-interpreter-attachment)\n    - [Execute Run, Run_and_Process, or Stream](#create-run-run_and_process-or-stream)\n    - [Retrieve message](#retrieve-message)\n    - [Retrieve file](#retrieve-file)\n    - [Tear down by deleting resource](#teardown)\n    - [Tracing](#tracing)\n  - [Evaluation](#evaluation)\n    - [Evaluator](#evaluator)\n    - [Run Evaluation in the cloud](#run-evaluation-in-the-cloud)\n      - [Evaluators](#evaluators)\n      - [Data to be evaluated](#data-to-be-evaluated)\n        - [[Optional] Azure OpenAI Model](#optional-azure-openai-model)\n        - [Example Remote Evaluation](#example-remote-evaluation)\n  - [Tracing](#tracing)\n    - [Installation](#installation)\n    - [Tracing example](#tracing-example)\n- [Troubleshooting](#troubleshooting)\n  - [Exceptions](#exceptions)\n  - [Logging](#logging)\n  - [Reporting issues](#reporting-issues)\n- [Next steps](#next-steps)\n- [Contributing](#contributing)\n\n## Getting started\n\n### Prerequisite\n\n- Python 3.8 or later.\n- An [Azure subscription][azure_sub].\n- A [project in Azure AI Studio](https://learn.microsoft.com/azure/ai-studio/how-to/create-projects?tabs=ai-studio).\n- The project connection string. It can be found in your Azure AI Studio project overview page, under \"Project details\". Below we will assume the environment variable `PROJECT_CONNECTION_STRING` was defined to hold this value.\n- Entra ID is needed to authenticate the client. Your application needs an object that implements the [TokenCredential](https://learn.microsoft.com/python/api/azure-core/azure.core.credentials.tokencredential) interface. Code samples here use [DefaultAzureCredential](https://learn.microsoft.com/python/api/azure-identity/azure.identity.defaultazurecredential). To get that working, you will need:\n  * The `Contributor` role. Role assigned can be done via the \"Access Control (IAM)\" tab of your Azure AI Project resource in the Azure portal.\n  * [Azure CLI](https://learn.microsoft.com/cli/azure/install-azure-cli) installed.\n  * You are logged into your Azure account by running `az login`.\n  * 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.\n\n### Install the package\n\n```bash\npip install azure-ai-projects\n```\n\n## Key concepts\n\n### Create and authenticate the client\n\nThe class factory method `from_connection_string` is used to construct the client. To construct a synchronous client:\n\n```python\nimport os\nfrom azure.ai.projects import AIProjectClient\nfrom azure.identity import DefaultAzureCredential\n\nproject_client = AIProjectClient.from_connection_string(\n    credential=DefaultAzureCredential(),\n    conn_str=os.environ[\"PROJECT_CONNECTION_STRING\"],\n)\n```\n\nTo construct an asynchronous client, Install the additional package [aiohttp](https://pypi.org/project/aiohttp/):\n\n```bash\npip install aiohttp\n```\n\nand update the code above to import `asyncio`, and import `AIProjectClient` from the `azure.ai.projects.aio` namespace:\n\n```python\nimport os\nimport asyncio\nfrom azure.ai.projects.aio import AIProjectClient\nfrom azure.core.credentials import AzureKeyCredential\n\nproject_client = AIProjectClient.from_connection_string(\n    credential=DefaultAzureCredential(),\n    conn_str=os.environ[\"PROJECT_CONNECTION_STRING\"],\n)\n```\n\n## Examples\n\n### Enumerate connections\n\nYour Azure AI Studio project has a \"Management center\". When you enter it, you will see a tab named \"Connected resources\" under your project. The `.connections` operations on the client allow you to enumerate the connections and get connection properties. Connection properties include the resource URL and authentication credentials, among other things.\n\nBelow are code examples of the connection operations. Full samples can be found under the \"connetions\" folder in the [package samples][samples].\n\n#### Get properties of all connections\n\nTo list the properties of all the connections in the Azure AI Studio project:\n\n```python\nconnections = project_client.connections.list()\nfor connection in connections:\n    print(connection)\n```\n\n#### Get properties of all connections of a particular type\n\nTo list the properties of connections of a certain type (here Azure OpenAI):\n\n```python\nconnections = project_client.connections.list(\n    connection_type=ConnectionType.AZURE_OPEN_AI,\n)\nfor connection in connections:\n    print(connection)\n\n```\n\n#### Get properties of a default connection\n\nTo get the properties of the default connection of a certain type (here Azure OpenAI),\nwith its authentication credentials:\n\n```python\nconnection = project_client.connections.get_default(\n    connection_type=ConnectionType.AZURE_OPEN_AI,\n    include_credentials=True,  # Optional. Defaults to \"False\".\n)\nprint(connection)\n```\n\nIf the call was made with `include_credentials=True`, depending on the value of `connection.authentication_type`, either `connection.key` or `connection.token_credential`\nwill be populated. Otherwise both will be `None`.\n\n#### Get properties of a connection by its connection name\n\nTo get the connection properties of a connection named `connection_name`:\n\n```python\nconnection = project_client.connections.get(\n    connection_name=connection_name,\n    include_credentials=True  # Optional. Defaults to \"False\"\n)\nprint(connection)\n```\n\n### Get an authenticated ChatCompletionsClient\n\nYour Azure AI Studio project may have one or more AI models deployed that support chat completions. These could be OpenAI models, Microsoft models, or models from other providers. Use the code below to get an already authenticated [ChatCompletionsClient](https://learn.microsoft.com/python/api/azure-ai-inference/azure.ai.inference.chatcompletionsclient?view=azure-python-preview) from the [azure-ai-inference](https://pypi.org/project/azure-ai-inference/) package, and execute a chat completions call.\n\nFirst, install the package:\n\n```bash\npip install azure-ai-inference\n```\n\nThen run this code (replace \"gpt-4o\" with your model deployment name):\n\n```python\ninference_client = project_client.inference.get_chat_completions_client()\n\nresponse = inference_client.complete(\n    model=\"gpt-4o\", # Model deployment name\n    messages=[UserMessage(content=\"How many feet are in a mile?\")]\n)\n\nprint(response.choices[0].message.content)\n```\n\nSee the \"inference\" folder in the [package samples][samples] for additional samples, including getting an authenticated [EmbeddingsClient](https://learn.microsoft.com/python/api/azure-ai-inference/azure.ai.inference.embeddingsclient?view=azure-python-preview).\n\n### Get an authenticated AzureOpenAI client\n\nYour Azure AI Studio project may have one or more OpenAI models deployed that support chat completions. Use the code below to get an already authenticated [AzureOpenAI](https://github.com/openai/openai-python?tab=readme-ov-file#microsoft-azure-openai) from the [openai](https://pypi.org/project/openai/) package, and execute a chat completions call.\n\nFirst, install the package:\n\n```bash\npip install openai\n```\n\nThen run the code below. Replace `gpt-4o` with your model deployment name, and update the `api_version` value with one found in the \"Data plane - inference\" row [in this table](https://learn.microsoft.com/azure/ai-services/openai/reference#api-specs).\n\n```python\naoai_client = project_client.inference.get_azure_openai_client(api_version=\"2024-06-01\")\n\nresponse = aoai_client.chat.completions.create(\n    model=\"gpt-4o\", # Model deployment name\n    messages=[\n        {\n            \"role\": \"user\",\n            \"content\": \"How many feet are in a mile?\",\n        },\n    ],\n)\n\nprint(response.choices[0].message.content)\n```\n\nSee the \"inference\" folder in the [package samples][samples] for additional samples.\n\n### Agents (Private Preview)\n\nAgents in the Azure AI Projects client library are designed to facilitate various interactions and operations within your AI projects. They serve as the core components that manage and execute tasks, leveraging different tools and resources to achieve specific goals. The following steps outline the typical sequence for interacting with Agents. See the \"agents\" folder in the [package samples][samples] for additional Agent samples.\n\nAgents are actively being developed. A sign-up form for private preview is coming soon.\n\n#### Create Agent\n\nHere is an example of how to create an Agent:\n<!-- SNIPPET:sample_agents_basics.create_agent -->\n\n```python\nagent = project_client.agents.create_agent(\n    model=\"gpt-4-1106-preview\",\n    name=\"my-assistant\",\n    instructions=\"You are helpful assistant\",\n)\n```\n\n<!-- END SNIPPET -->\n\nTo allow Agents to access your resources or custom functions, you need tools. You can pass tools to `create_agent` by either `toolset` or combination of `tools` and `tool_resources`.\n\nHere is an example of `toolset`:\n<!-- SNIPPET:sample_agents_run_with_toolset.create_agent_toolset -->\n\n```python\nfunctions = FunctionTool(user_functions)\ncode_interpreter = CodeInterpreterTool()\n\ntoolset = ToolSet()\ntoolset.add(functions)\ntoolset.add(code_interpreter)\n\nagent = project_client.agents.create_agent(\n    model=\"gpt-4-1106-preview\", name=\"my-assistant\", instructions=\"You are a helpful assistant\", toolset=toolset\n)\n```\n\n<!-- END SNIPPET -->\n\nAlso notices that if you use asynchronous client, you use `AsyncToolSet` instead.  Additional information related to `AsyncFunctionTool` be discussed in the later sections.\n\nHere is an example to use `tools` and `tool_resources`:\n<!-- SNIPPET:sample_agents_vector_store_batch_file_search.create_agent_with_tools_and_tool_resources -->\n\n```python\nfile_search_tool = FileSearchTool(vector_store_ids=[vector_store.id])\n\n# notices that FileSearchTool as tool and tool_resources must be added or the assistant unable to search the file\nagent = project_client.agents.create_agent(\n    model=\"gpt-4-1106-preview\",\n    name=\"my-assistant\",\n    instructions=\"You are helpful assistant\",\n    tools=file_search_tool.definitions,\n    tool_resources=file_search_tool.resources,\n)\n```\n\n<!-- END SNIPPET -->\n\nIn the following sections, we show you sample code in either `toolset` or combination of `tools` and `tool_resources`.\n\n#### Create Agent with File Search\n\nTo perform file search by an Agent, we first need to upload a file, create a vector store, and associate the file to the vector store. Here is an example:\n\n<!-- SNIPPET:sample_agents_file_search.upload_file_create_vector_store_and_agent_with_file_search_tool -->\n\n```python\nfile = project_client.agents.upload_file_and_poll(file_path=\"product_info_1.md\", purpose=\"assistants\")\nprint(f\"Uploaded file, file ID: {file.id}\")\n\nvector_store = project_client.agents.create_vector_store_and_poll(file_ids=[file.id], name=\"my_vectorstore\")\nprint(f\"Created vector store, vector store ID: {vector_store.id}\")\n\n# Create file search tool with resources followed by creating agent\nfile_search = FileSearchTool(vector_store_ids=[vector_store.id])\n\nagent = project_client.agents.create_agent(\n    model=\"gpt-4-1106-preview\",\n    name=\"my-assistant\",\n    instructions=\"Hello, you are helpful assistant and can search information from uploaded files\",\n    tools=file_search.definitions,\n    tool_resources=file_search.resources,\n)\n```\n\n<!-- END SNIPPET -->\n\n#### Create Agent with Code Interpreter\n\nHere is an example to upload a file and use it for code interpreter by an Agent:\n\n<!-- SNIPPET:sample_agents_code_interpreter.upload_file_and_create_agent_with_code_interpreter -->\n\n```python\nfile = project_client.agents.upload_file_and_poll(\n    file_path=\"nifty_500_quarterly_results.csv\", purpose=FilePurpose.AGENTS\n)\nprint(f\"Uploaded file, file ID: {file.id}\")\n\ncode_interpreter = CodeInterpreterTool(file_ids=[file.id])\n\n# create agent with code interpreter tool and tools_resources\nagent = project_client.agents.create_agent(\n    model=\"gpt-4-1106-preview\",\n    name=\"my-assistant\",\n    instructions=\"You are helpful assistant\",\n    tools=code_interpreter.definitions,\n    tool_resources=code_interpreter.resources,\n)\n```\n\n<!-- END SNIPPET -->\n\n#### Create Agent with Bing Grounding\n\nTo enable your Agent to perform search through Bing search API, you use `BingGroundingTool` along with a connection.\n\nHere is an example:\n\n<!-- SNIPPET:sample_agents_bing_grounding.create_agent_with_bing_grounding_tool -->\n\n```python\nbing_connection = project_client.connections.get(connection_name=os.environ[\"BING_CONNECTION_NAME\"])\nconn_id = bing_connection.id\n\nprint(conn_id)\n\n# Initialize agent bing tool and add the connection id\nbing = BingGroundingTool(connection_id=conn_id)\n\n# Create agent with the bing tool and process assistant run\nwith project_client:\n    agent = project_client.agents.create_agent(\n        model=\"gpt-4-1106-preview\",\n        name=\"my-assistant\",\n        instructions=\"You are a helpful assistant\",\n        tools=bing.definitions,\n        headers={\"x-ms-enable-preview\": \"true\"},\n    )\n```\n\n<!-- END SNIPPET -->\n\n#### Create Agent with Azure AI Search\n\nAzure 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\n\nHere is an example to integrate Azure AI Search:\n\n<!-- SNIPPET:sample_agents_azure_ai_search.create_agent_with_azure_ai_search_tool -->\n\n```python\nconn_list = project_client.connections.list()\nconn_id = \"\"\nfor conn in conn_list:\n    if conn.connection_type == \"CognitiveSearch\":\n        conn_id = conn.id\n        break\n\nprint(conn_id)\n\n# Initialize agent AI search tool and add the search index connection id\nai_search = AzureAISearchTool()\nai_search.add_index(conn_id, \"sample_index\")\n\n# Create agent with AI search tool and process assistant run\nwith project_client:\n    agent = project_client.agents.create_agent(\n        model=\"gpt-4-1106-preview\",\n        name=\"my-assistant\",\n        instructions=\"You are a helpful assistant\",\n        tools=ai_search.definitions,\n        headers={\"x-ms-enable-preview\": \"true\"},\n    )\n```\n\n<!-- END SNIPPET -->\n\n#### Create Agent with Function Call\n\nYou can enhance your Agents by defining callback functions as function tools. These can be provided to `create_agent` via either the `toolset` parameter or the combination of `tools` and `tool_resources`. Here are the distinctions:\n\n- `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.\n- `tools` and `tool_resources`: When using the `tools` and `tool_resources` parameters, only the function definitions and descriptions are provided to `create_agent`, 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.\n\nFor more details about calling functions by code, refer to [`sample_agents_stream_eventhandler_with_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_stream_eventhandler_with_functions.py) and [`sample_agents_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_functions.py).\n\nHere is an example to use [user functions](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/user_functions.py) in `toolset`:\n<!-- SNIPPET:sample_agents_stream_eventhandler_with_toolset.create_agent_with_function_tool -->\n\n```python\nfunctions = FunctionTool(user_functions)\ntoolset = ToolSet()\ntoolset.add(functions)\n\nagent = project_client.agents.create_agent(\n    model=\"gpt-4-1106-preview\", name=\"my-assistant\", instructions=\"You are a helpful assistant\", toolset=toolset\n)\n```\n\n<!-- END SNIPPET -->\n\nFor asynchronous functions, you must import `AIProjectClient` from `azure.ai.projects.aio` and use `AsyncFunctionTool`.   Here is an example using [asynchronous user functions](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/async_samples/user_async_functions.py):\n\n```python\nfrom azure.ai.projects.aio import AIProjectClient\n```\n\n<!-- SNIPPET:sample_agents_run_with_toolset_async.create_agent_with_async_function_tool -->\n\n```python\nfunctions = AsyncFunctionTool(user_async_functions)\n\ntoolset = AsyncToolSet()\ntoolset.add(functions)\n\nagent = await project_client.agents.create_agent(\n    model=\"gpt-4-1106-preview\", name=\"my-assistant\", instructions=\"You are a helpful assistant\", toolset=toolset\n)\n```\n\n<!-- END SNIPPET -->\n\n#### Create Thread\n\nFor each session or conversation, a thread is required.   Here is an example:\n\n<!-- SNIPPET:sample_agents_basics.create_thread -->\n\n```python\nthread = project_client.agents.create_thread()\n```\n\n<!-- END SNIPPET -->\n\n#### Create Thread with Tool Resource\n\nIn 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 Agent for file search using the `tools` argument, and then associate the file with the thread using the `tool_resources` argument.\n\n<!-- SNIPPET:sample_agents_with_resources_in_thread.create_agent_and_thread_for_file_search -->\n\n```python\nfile = project_client.agents.upload_file_and_poll(file_path=\"product_info_1.md\", purpose=\"assistants\")\nprint(f\"Uploaded file, file ID: {file.id}\")\n\nvector_store = project_client.agents.create_vector_store_and_poll(file_ids=[file.id], name=\"my_vectorstore\")\nprint(f\"Created vector store, vector store ID: {vector_store.id}\")\n\n# Create file search tool with resources followed by creating agent\nfile_search = FileSearchTool(vector_store_ids=[vector_store.id])\n\nagent = project_client.agents.create_agent(\n    model=\"gpt-4-1106-preview\",\n    name=\"my-assistant\",\n    instructions=\"Hello, you are helpful assistant and can search information from uploaded files\",\n    tools=file_search.definitions,\n)\n\nprint(f\"Created agent, ID: {agent.id}\")\n\n# Create thread with file resources.\n# If the agent has multiple threads, only this thread can search this file.\nthread = project_client.agents.create_thread(tool_resources=file_search.resources)\n```\n\n<!-- END SNIPPET -->\n#### Create Message\n\nTo create a message for assistant to process, you pass `user` as `role` and a question as `content`:\n\n<!-- SNIPPET:sample_agents_basics.create_message -->\n\n```python\nmessage = project_client.agents.create_message(thread_id=thread.id, role=\"user\", content=\"Hello, tell me a joke\")\n```\n\n<!-- END SNIPPET -->\n\n#### Create Message with File Search Attachment\n\nTo attach a file to a message for content searching, you use `MessageAttachment` and `FileSearchTool`:\n\n<!-- SNIPPET:sample_agents_with_file_search_attachment.create_message_with_attachment -->\n\n```python\nattachment = MessageAttachment(file_id=file.id, tools=FileSearchTool().definitions)\nmessage = project_client.agents.create_message(\n    thread_id=thread.id, role=\"user\", content=\"What feature does Smart Eyewear offer?\", attachments=[attachment]\n)\n```\n\n<!-- END SNIPPET -->\n\n#### Create Message with Code Interpreter Attachment\n\nTo attach a file to a message for data analysis, you use `MessageAttachment` and `CodeInterpreterTool`. You must pass `CodeInterpreterTool` as `tools` or `toolset` in `create_agent` call or the file attachment cannot be opened for code interpreter.  \n\nHere is an example to pass `CodeInterpreterTool` as tool:\n\n<!-- SNIPPET:sample_agents_with_code_interpreter_file_attachment.create_agent_and_message_with_code_interpreter_file_attachment -->\n\n```python\n# notice that CodeInterpreter must be enabled in the agent creation,\n# otherwise the agent will not be able to see the file attachment for code interpretation\nagent = project_client.agents.create_agent(\n    model=\"gpt-4-1106-preview\",\n    name=\"my-assistant\",\n    instructions=\"You are helpful assistant\",\n    tools=CodeInterpreterTool().definitions,\n)\nprint(f\"Created agent, agent ID: {agent.id}\")\n\nthread = project_client.agents.create_thread()\nprint(f\"Created thread, thread ID: {thread.id}\")\n\n# create an attachment\nattachment = MessageAttachment(file_id=file.id, tools=CodeInterpreterTool().definitions)\n\n# create a message\nmessage = project_client.agents.create_message(\n    thread_id=thread.id,\n    role=\"user\",\n    content=\"Could you please create bar chart in TRANSPORTATION sector for the operating profit from the uploaded csv file and provide file to me?\",\n    attachments=[attachment],\n)\n```\n\n<!-- END SNIPPET -->\n\n#### Create Run, Run_and_Process, or Stream\n\nTo process your message, you can use `create_run`, `create_and_process_run`, or `create_stream`.\n\n`create_run` requests the Agent 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_agents_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_functions.py).\n\nHere is an example of `create_run` and poll until the run is completed:\n\n<!-- SNIPPET:sample_agents_basics.create_run -->\n\n```python\nrun = project_client.agents.create_run(thread_id=thread.id, assistant_id=agent.id)\n\n# poll the run as long as run status is queued or in progress\nwhile run.status in [\"queued\", \"in_progress\", \"requires_action\"]:\n    # wait for a second\n    time.sleep(1)\n    run = project_client.agents.get_run(thread_id=thread.id, run_id=run.id)\n```\n\n<!-- END SNIPPET -->\n\nTo 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_agent` call.\n\nHere is an example:\n\n<!-- SNIPPET:sample_agents_run_with_toolset.create_and_process_run -->\n\n```python\nrun = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id)\n```\n\n<!-- END SNIPPET -->\n\nWith streaming, polling also need not be considered. If `function tools` are provided as `toolset` during the `create_agent` call, they will be invoked by the SDK.\n\nHere is an example:\n\n<!-- SNIPPET:sample_agents_stream_eventhandler.create_stream -->\n\n```python\nwith project_client.agents.create_stream(\n    thread_id=thread.id, assistant_id=agent.id, event_handler=MyEventHandler()\n) as stream:\n    stream.until_done()\n```\n\n<!-- END SNIPPET -->\n\nThe event handler is optional. Here is an example:\n\n<!-- SNIPPET:sample_agents_stream_eventhandler.stream_event_handler -->\n\n```python\nclass MyEventHandler(AgentEventHandler):\n    def on_message_delta(self, delta: \"MessageDeltaChunk\") -> None:\n        for content_part in delta.delta.content:\n            if isinstance(content_part, MessageDeltaTextContent):\n                text_value = content_part.text.value if content_part.text else \"No text\"\n                print(f\"Text delta received: {text_value}\")\n\n    def on_thread_message(self, message: \"ThreadMessage\") -> None:\n        print(f\"ThreadMessage created. ID: {message.id}, Status: {message.status}\")\n\n    def on_thread_run(self, run: \"ThreadRun\") -> None:\n        print(f\"ThreadRun status: {run.status}\")\n\n    def on_run_step(self, step: \"RunStep\") -> None:\n        print(f\"RunStep type: {step.type}, Status: {step.status}\")\n\n    def on_error(self, data: str) -> None:\n        print(f\"An error occurred. Data: {data}\")\n\n    def on_done(self) -> None:\n        print(\"Stream completed.\")\n\n    def on_unhandled_event(self, event_type: str, event_data: Any) -> None:\n        print(f\"Unhandled Event Type: {event_type}, Data: {event_data}\")\n```\n\n<!-- END SNIPPET -->\n\n#### Retrieve Message\n\nTo retrieve messages from agents, use the following example:\n\n<!-- SNIPPET:sample_agents_basics.list_messages -->\n\n```python\nmessages = project_client.agents.list_messages(thread_id=thread.id)\n\n# The messages are following in the reverse order,\n# we will iterate them and output only text contents.\nfor data_point in reversed(messages.data):\n    last_message_content = data_point.content[-1]\n    if isinstance(last_message_content, MessageTextContent):\n        print(f\"{data_point.role}: {last_message_content.text.value}\")\n```\n\n<!-- END SNIPPET -->\n\nDepending on the use case, if you expect the Agents to return only text messages, `list_messages` should be sufficient.\nIf you are using tools, consider using the `get_messages` function instead. This function classifies the message content and returns properties such as `text_messages`, `image_contents`, `file_citation_annotations`, and `file_path_annotations`.\n\n### Retrieve File\n\nFiles uploaded by Agents cannot be retrieved back. If your use case need to access the file content uploaded by the Agents, you are advised to keep an additional copy accessible by your application. However, files generated by Agents are retrievable by `save_file` or `get_file_content`.\n\nHere is an example retrieving file ids from messages and save to the local drive:\n\n<!-- SNIPPET:sample_agents_code_interpreter.get_messages_and_save_files -->\n\n```python\nmessages = project_client.agents.get_messages(thread_id=thread.id)\nprint(f\"Messages: {messages}\")\n\nfor image_content in messages.image_contents:\n    file_id = image_content.image_file.file_id\n    print(f\"Image File ID: {file_id}\")\n    file_name = f\"{file_id}_image_file.png\"\n    project_client.agents.save_file(file_id=file_id, file_name=file_name)\n    print(f\"Saved image file to: {Path.cwd() / file_name}\")\n\nfor file_path_annotation in messages.file_path_annotations:\n    print(f\"File Paths:\")\n    print(f\"Type: {file_path_annotation.type}\")\n    print(f\"Text: {file_path_annotation.text}\")\n    print(f\"File ID: {file_path_annotation.file_path.file_id}\")\n    print(f\"Start Index: {file_path_annotation.start_index}\")\n    print(f\"End Index: {file_path_annotation.end_index}\")\n```\n\n<!-- END SNIPPET -->\n\nHere is an example to use `get_file_content`:\n\n```python\nfrom pathlib import Path\n\nasync def save_file_content(client, file_id: str, file_name: str, target_dir: Optional[Union[str, Path]] = None):\n    # Determine the target directory\n    path = Path(target_dir).expanduser().resolve() if target_dir else Path.cwd()\n    path.mkdir(parents=True, exist_ok=True)\n\n    # Retrieve the file content\n    file_content_stream = await client.get_file_content(file_id)\n    if not file_content_stream:\n        raise RuntimeError(f\"No content retrievable for file ID '{file_id}'.\")\n\n    # Collect all chunks asynchronously\n    chunks = []\n    async for chunk in file_content_stream:\n        if isinstance(chunk, (bytes, bytearray)):\n            chunks.append(chunk)\n        else:\n            raise TypeError(f\"Expected bytes or bytearray, got {type(chunk).__name__}\")\n\n    target_file_path = path / file_name\n\n    # Write the collected content to the file synchronously\n    with open(target_file_path, \"wb\") as file:\n        for chunk in chunks:\n            file.write(chunk)\n```\n\n#### Teardown\n\nTo remove resources after completing tasks, use the following functions:\n\n<!-- SNIPPET:sample_agents_file_search.teardown -->\n\n```python\n# Delete the file when done\nproject_client.agents.delete_vector_store(vector_store.id)\nprint(\"Deleted vector store\")\n\nproject_client.agents.delete_file(file_id=file.id)\nprint(\"Deleted file\")\n\n# Delete the agent when done\nproject_client.agents.delete_agent(agent.id)\nprint(\"Deleted agent\")\n```\n\n<!-- END SNIPPET -->\n\n### Evaluation\n\nEvaluation in Azure AI Project client library is designed to assess the performance of generative AI applications in the cloud. The output of Generative AI application is quantitively measured with mathematical based metrics, AI-assisted quality and safety metrics. Metrics are defined as evaluators. Built-in or custom evaluators can provide comprehensive insights into the application's capabilities and limitations.\n\n#### Evaluator\n\nEvaluators are custom or prebuilt classes or functions that are designed to measure the quality of the outputs from language models or generative AI applications.\n\nEvaluators are made available via [azure-ai-evaluation][azure_ai_evaluation] SDK for local experience and also in [Evaluator Library][evaluator_library] in Azure AI Studio for using them in the cloud.\n\nMore details on built-in and custom evaluators can be found [here][evaluators].\n\n#### Run Evaluation in the cloud\n\nTo run evaluation in the cloud the following are needed:\n\n- Evaluators\n- Data to be evaluated\n- [Optional] Azure Open AI model.\n\n##### Evaluators\n\nFor running evaluator in the cloud, evaluator `ID` is needed. To get it via code you use [azure-ai-evaluation][azure_ai_evaluation]\n\n```python\n# pip install azure-ai-evaluation\n\nfrom azure.ai.evaluation import RelevanceEvaluator\n\nevaluator_id = RelevanceEvaluator.id\n```\n\n##### Data to be evaluated\n\nEvaluation in the cloud supports data in form of `jsonl` file. Data can be uploaded via the helper method `upload_file` on the project client.\n\n```python\n# Upload data for evaluation and get dataset id\ndata_id, _ = project_client.upload_file(\"<data_file.jsonl>\")\n```\n\n##### [Optional] Azure OpenAI Model\n\nAzure AI Studio project comes with a default Azure Open AI endpoint which can be easily accessed using following code. This gives you the endpoint details for you Azure OpenAI endpoint. Some of the evaluators need model that supports chat completion.\n\n```python\ndefault_connection = project_client.connections.get_default(connection_type=ConnectionType.AZURE_OPEN_AI)\n```\n\n##### Example Remote Evaluation\n\n```python\nimport os\nfrom azure.ai.projects import AIProjectClient\nfrom azure.identity import DefaultAzureCredential\nfrom azure.ai.projects.models import Evaluation, Dataset, EvaluatorConfiguration, ConnectionType\nfrom azure.ai.evaluation import F1ScoreEvaluator, RelevanceEvaluator, HateUnfairnessEvaluator\n\n\n# Create project client\nproject_client = AIProjectClient.from_connection_string(\n    credential=DefaultAzureCredential(),\n    conn_str=os.environ[\"PROJECT_CONNECTION_STRING\"],\n)\n\n# Upload data for evaluation and get dataset id\ndata_id, _ = project_client.upload_file(\"<data_file.jsonl>\")\n\ndeployment_name = \"<deployment_name>\"\napi_version = \"<api_version>\"\n\n# Create an evaluation\nevaluation = Evaluation(\n    display_name=\"Remote Evaluation\",\n    description=\"Evaluation of dataset\",\n    data=Dataset(id=data_id),\n    evaluators={\n        \"f1_score\": EvaluatorConfiguration(\n            id=F1ScoreEvaluator.id,\n        ),\n        \"relevance\": EvaluatorConfiguration(\n            id=RelevanceEvaluator.id,\n            init_params={\n                \"model_config\": default_connection.to_evaluator_model_config(\n                    deployment_name=deployment_name, api_version=api_version\n                )\n            },\n        ),\n        \"violence\": EvaluatorConfiguration(\n            id=ViolenceEvaluator.id,\n            init_params={\"azure_ai_project\": project_client.scope},\n        ),\n    },\n)\n\n\nevaluation_response = project_client.evaluations.create(\n    evaluation=evaluation,\n)\n\n# Get evaluation\nget_evaluation_response = project_client.evaluations.get(evaluation_response.id)\n\nprint(\"----------------------------------------------------------------\")\nprint(\"Created evaluation, evaluation ID: \", get_evaluation_response.id)\nprint(\"Evaluation status: \", get_evaluation_response.status)\nif isinstance(get_evaluation_response.properties, dict):\n    print(\"AI Studio URI: \", get_evaluation_response.properties[\"AiStudioEvaluationUri\"])\nprint(\"----------------------------------------------------------------\")\n```\n\nNOTE: For running evaluators locally refer to [Evaluate with the Azure AI Evaluation SDK][evaluators].\n\n### Tracing\n\nYou can add an Application Insights Azure resource to your Azure AI Studio project. See the Tracing tab in your studio. If one was enabled, you can get the Application Insights connection string, configure your Agents, and observe the full execution path through Azure Monitor. Typically, you might want to start tracing before you create an Agent.\n\n#### Installation\n\nMake sure to install OpenTelemetry and the Azure SDK tracing plugin via\n\n```bash\npip install opentelemetry\npip install azure-core-tracing-opentelemetry\n```\n\nYou 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](https://learn.microsoft.com/dotnet/aspire/fundamentals/dashboard/standalone?tabs=bash).\n\nTo connect to Aspire Dashboard or another OpenTelemetry compatible backend, install OTLP exporter:\n\n```bash\npip install opentelemetry-exporter-otlp\n```\n\n#### Tracing example\n\nHere is a code sample to be included above `create_agent`:\n\n<!-- SNIPPET:sample_agents_basics_with_azure_monitor_tracing.enable_tracing -->\n\n```python\nfrom opentelemetry import trace\nfrom azure.monitor.opentelemetry import configure_azure_monitor\n\n# Enable Azure Monitor tracing\napplication_insights_connection_string = project_client.telemetry.get_connection_string()\nif not application_insights_connection_string:\n    print(\"Application Insights was not enabled for this project.\")\n    print(\"Enable it via the 'Tracing' tab in your AI Studio project page.\")\n    exit()\nconfigure_azure_monitor(connection_string=application_insights_connection_string)\n\nscenario = os.path.basename(__file__)\ntracer = trace.get_tracer(__name__)\n\nwith tracer.start_as_current_span(scenario):\n    with project_client:\n```\n\n<!-- END SNIPPET -->\n\nIn additional, you might find helpful to see the tracing logs in console. You can achieve by the following code:\n\n```python\nproject_client.telemetry.enable(destination=sys.stdout)\n```\n\n## Troubleshooting\n\n### Exceptions\n\nClient methods that make service calls raise an [HttpResponseError](https://learn.microsoft.com/python/api/azure-core/azure.core.exceptions.httpresponseerror) exception for a non-success HTTP status code response from the service. The exception's `status_code` will hold the HTTP response status code (with `reason` showing the friendly name). The exception's `error.message` contains a detailed message that may be helpful in diagnosing the issue:\n\n```python\nfrom azure.core.exceptions import HttpResponseError\n\n...\n\ntry:\n    result = project_client.connections.list()\nexcept HttpResponseError as e:\n    print(f\"Status code: {e.status_code} ({e.reason})\")\n    print(e.message)\n```\n\nFor example, when you provide wrong credentials:\n\n```text\nStatus code: 401 (Unauthorized)\nOperation returned an invalid status 'Unauthorized'\n```\n\n### Logging\n\nThe client uses the standard [Python logging library](https://docs.python.org/3/library/logging.html). The SDK logs HTTP request and response details, which may be useful in troubleshooting. To log to stdout, add the following:\n\n```python\nimport sys\nimport logging\n\n# Acquire the logger for this client library. Use 'azure' to affect both\n# 'azure.core` and `azure.ai.inference' libraries.\nlogger = logging.getLogger(\"azure\")\n\n# Set the desired logging level. logging.INFO or logging.DEBUG are good options.\nlogger.setLevel(logging.DEBUG)\n\n# Direct logging output to stdout:\nhandler = logging.StreamHandler(stream=sys.stdout)\n# Or direct logging output to a file:\n# handler = logging.FileHandler(filename=\"sample.log\")\nlogger.addHandler(handler)\n\n# Optional: change the default logging format. Here we add a timestamp.\n#formatter = logging.Formatter(\"%(asctime)s:%(levelname)s:%(name)s:%(message)s\")\n#handler.setFormatter(formatter)\n```\n\nBy 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:\n\n```python\nproject_client = AIProjectClient.from_connection_string(\n    credential=DefaultAzureCredential(),\n    conn_str=os.environ[\"PROJECT_CONNECTION_STRING\"],\n    logging_enable = True\n)\n```\n\nNote that the log level must be set to `logging.DEBUG` (see above code). Logs will be redacted with any other log level.\n\nBe sure to protect non redacted logs to avoid compromising security.\n\nFor more information, see [Configure logging in the Azure libraries for Python](https://aka.ms/azsdk/python/logging)\n\n### Reporting issues\n\nTo report issues with the client library, or request additional features, please open a GitHub issue [here](https://github.com/Azure/azure-sdk-for-python/issues)\n\n## Next steps\n\nHave a look at the [Samples](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/ai/azure-ai-projects/samples) folder, containing fully runnable Python code for synchronous and asynchronous clients.\n\nExplore the [AI Starter Template](https://aka.ms/azsdk/azure-ai-projects/python/ai-starter-template). This template creates an Azure AI Studio hub, project and connected resources including Azure OpenAI Service, AI Search and more. It also deploys a simple chat application to Azure Container Apps.\n\n## Contributing\n\nThis project welcomes contributions and suggestions. Most contributions require\nyou to agree to a Contributor License Agreement (CLA) declaring that you have\nthe right to, and actually do, grant us the rights to use your contribution.\nFor details, visit https://cla.microsoft.com.\n\nWhen you submit a pull request, a CLA-bot will automatically determine whether\nyou need to provide a CLA and decorate the PR appropriately (e.g., label,\ncomment). Simply follow the instructions provided by the bot. You will only\nneed to do this once across all repos using our CLA.\n\nThis project has adopted the\n[Microsoft Open Source Code of Conduct][code_of_conduct]. For more information,\nsee the Code of Conduct FAQ or contact opencode@microsoft.com with any\nadditional questions or comments.\n\n<!-- LINKS -->\n[samples]: https://aka.ms/azsdk/azure-ai-projects/python/samples/\n[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/\n[entra_id]: https://learn.microsoft.com/azure/ai-services/authentication?tabs=powershell#authenticate-with-microsoft-entra-id\n[azure_identity_credentials]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#credentials\n[azure_identity_pip]: https://pypi.org/project/azure-identity/\n[default_azure_credential]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#defaultazurecredential\n[pip]: https://pypi.org/project/pip/\n[azure_sub]: https://azure.microsoft.com/free/\n[evaluators]: https://learn.microsoft.com/azure/ai-studio/how-to/develop/evaluate-sdk\n[azure_ai_evaluation]: https://learn.microsoft.com/python/api/overview/azure/ai-evaluation-readme?view=azure-python-preview\n[evaluator_library]: https://learn.microsoft.com/azure/ai-studio/how-to/evaluate-generative-ai-app#view-and-manage-the-evaluators-in-the-evaluator-library\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Microsoft Azure AI Projects Client Library for Python",
    "version": "1.0.0b1",
    "project_urls": {
        "Homepage": "https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/ai/azure-ai-projects"
    },
    "split_keywords": [
        "azure",
        " azure sdk"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "d12f933c2aae01bd3defbadce459509bbb69d9c6c15263dda2a0878d28be0d17",
                "md5": "20c80cb5e33b563446b33f5f3e2eabaf",
                "sha256": "48c73cf30128923175af6bf18cf80a4bea3e9dfd37e1385e1f622ce90997bc1f"
            },
            "downloads": -1,
            "filename": "azure_ai_projects-1.0.0b1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "20c80cb5e33b563446b33f5f3e2eabaf",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 181813,
            "upload_time": "2024-11-15T17:21:45",
            "upload_time_iso_8601": "2024-11-15T17:21:45.972280Z",
            "url": "https://files.pythonhosted.org/packages/d1/2f/933c2aae01bd3defbadce459509bbb69d9c6c15263dda2a0878d28be0d17/azure_ai_projects-1.0.0b1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "c8d994dcecb4ff3941b9c6fdcbf4be6ae19182274cdd101c8ec6744b4d31d093",
                "md5": "f0381c55ee0c9b9fc0920f857adc93ff",
                "sha256": "cd1f6e94a49c9a46531ea7103ee81ca459c4d5341258bac18a844ef3d11e7ca3"
            },
            "downloads": -1,
            "filename": "azure_ai_projects-1.0.0b1.tar.gz",
            "has_sig": false,
            "md5_digest": "f0381c55ee0c9b9fc0920f857adc93ff",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 242971,
            "upload_time": "2024-11-15T17:21:43",
            "upload_time_iso_8601": "2024-11-15T17:21:43.914471Z",
            "url": "https://files.pythonhosted.org/packages/c8/d9/94dcecb4ff3941b9c6fdcbf4be6ae19182274cdd101c8ec6744b4d31d093/azure_ai_projects-1.0.0b1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-15 17:21:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Azure",
    "github_project": "azure-sdk-for-python",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "azure-ai-projects"
}
        
Elapsed time: 0.67459s