-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Auto-enable Azure AI Inference instrumentation in Azure Monitor, update docs #38071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b3cc74f
Auto-enable Azure AI Inference instrumentation in Azure Monitor, upda…
56b15de
nits
e500170
fix spelling and tests
a13c3d0
more nits
8f3afc2
fix instrument
4bd95c4
changelog
81ee815
add inference sample to azmon
c6a5af9
up
cd30d19
update sample
b63757f
typo
711e382
black
c9ce352
update sample
1cb6247
up
a8cf227
black
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Auto-enable Azure AI Inference instrumentation in Azure Monitor, upda…
…te docs
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| -e ../../../tools/azure-sdk-tools | ||
| ../../core/azure-core | ||
| ../../core/azure-core-tracing-opentelemetry | ||
| ../../monitor/azure-monitor-opentelemetry | ||
| aiohttp | ||
| opentelemetry-sdk |
148 changes: 148 additions & 0 deletions
148
sdk/ai/azure-ai-inference/samples/sample_chat_completions_with_azure_monitor_tracing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| """ | ||
| DESCRIPTION: | ||
| This sample demonstrates how to use tracing with the Inference client library. | ||
| Azure AI Inference is instrumented with OpenTelemetry. In order to enable tracing | ||
| you need to configure OpenTelemetry to export traces to your observability backend. | ||
| This sample shows how to capture the traces to a file. | ||
|
|
||
| This sample assumes the AI model is hosted on a Serverless API or | ||
| Managed Compute endpoint. For GitHub Models or Azure OpenAI endpoints, | ||
| the client constructor needs to be modified. See package documentation: | ||
| https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/README.md#key-concepts | ||
|
|
||
| USAGE: | ||
| python sample_chat_completions_with_tracing.py | ||
|
|
||
| Set these two environment variables before running the sample: | ||
| 1) AZURE_AI_CHAT_ENDPOINT - Your endpoint URL, in the form | ||
| https://<your-deployment-name>.<your-azure-region>.models.ai.azure.com | ||
| where `your-deployment-name` is your unique AI Model deployment name, and | ||
| `your-azure-region` is the Azure region where your model is deployed. | ||
| 2) AZURE_AI_CHAT_KEY - Your model key (a 32-character string). Keep it secret. | ||
| """ | ||
|
|
||
|
|
||
| import os | ||
lmolkova marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| from opentelemetry import trace | ||
| from azure.ai.inference import ChatCompletionsClient | ||
| from azure.ai.inference.models import SystemMessage, UserMessage, CompletionsFinishReason | ||
| from azure.core.credentials import AzureKeyCredential | ||
| from azure.monitor.opentelemetry import configure_azure_monitor | ||
|
|
||
|
|
||
| # [START trace_function] | ||
| from opentelemetry.trace import get_tracer | ||
| tracer = get_tracer(__name__) | ||
|
|
||
| # The tracer.start_as_current_span decorator will trace the function call and enable adding additional attributes | ||
| # to the span in the function implementation. Note that this will trace the function parameters and their values. | ||
| @tracer.start_as_current_span("get_temperature") # type: ignore | ||
| def get_temperature(city: str) -> str: | ||
|
|
||
| # Adding attributes to the current span | ||
| span = trace.get_current_span() | ||
| span.set_attribute("requested_city", city) | ||
|
|
||
| if city == "Seattle": | ||
| return "75" | ||
| elif city == "New York City": | ||
| return "80" | ||
| else: | ||
| return "Unavailable" | ||
| # [END trace_function] | ||
|
|
||
|
|
||
| def get_weather(city: str) -> str: | ||
| if city == "Seattle": | ||
| return "Nice weather" | ||
| elif city == "New York City": | ||
| return "Good weather" | ||
| else: | ||
| return "Unavailable" | ||
|
|
||
|
|
||
| def chat_completion_with_function_call(key, endpoint): | ||
| import json | ||
| from azure.ai.inference.models import ToolMessage, AssistantMessage, ChatCompletionsToolCall, ChatCompletionsToolDefinition, FunctionDefinition | ||
|
|
||
| weather_description = ChatCompletionsToolDefinition( | ||
| function=FunctionDefinition( | ||
| name="get_weather", | ||
| description="Returns description of the weather in the specified city", | ||
| parameters={ | ||
| "type": "object", | ||
| "properties": { | ||
| "city": { | ||
| "type": "string", | ||
| "description": "The name of the city for which weather info is requested", | ||
| }, | ||
| }, | ||
| "required": ["city"], | ||
| }, | ||
| ) | ||
| ) | ||
|
|
||
| temperature_in_city = ChatCompletionsToolDefinition( | ||
| function=FunctionDefinition( | ||
| name="get_temperature", | ||
| description="Returns the current temperature for the specified city", | ||
| parameters={ | ||
| "type": "object", | ||
| "properties": { | ||
| "city": { | ||
| "type": "string", | ||
| "description": "The name of the city for which temperature info is requested", | ||
| }, | ||
| }, | ||
| "required": ["city"], | ||
| }, | ||
| ) | ||
| ) | ||
|
|
||
| client = ChatCompletionsClient(endpoint=endpoint, credential=AzureKeyCredential(key), model="gpt-4o-mini") | ||
| messages=[ | ||
| SystemMessage(content="You are a helpful assistant."), | ||
| UserMessage(content="What is the weather and temperature in Seattle?"), | ||
| ] | ||
|
|
||
| response = client.complete(messages=messages, tools=[weather_description, temperature_in_city]) | ||
|
|
||
| if response.choices[0].finish_reason == CompletionsFinishReason.TOOL_CALLS: | ||
| # Append the previous model response to the chat history | ||
| messages.append(AssistantMessage(tool_calls=response.choices[0].message.tool_calls)) | ||
| # The tool should be of type function call. | ||
| if response.choices[0].message.tool_calls is not None and len(response.choices[0].message.tool_calls) > 0: | ||
| for tool_call in response.choices[0].message.tool_calls: | ||
| if type(tool_call) is ChatCompletionsToolCall: | ||
| function_args = json.loads(tool_call.function.arguments.replace("'", '"')) | ||
| print(f"Calling function `{tool_call.function.name}` with arguments {function_args}") | ||
| callable_func = globals()[tool_call.function.name] | ||
| function_response = callable_func(**function_args) | ||
| print(f"Function response = {function_response}") | ||
| # Provide the tool response to the model, by appending it to the chat history | ||
| messages.append(ToolMessage(tool_call_id=tool_call.id, content=function_response)) | ||
| # With the additional tools information on hand, get another response from the model | ||
| response = client.complete(messages=messages, tools=[weather_description, temperature_in_city]) | ||
|
|
||
| print(f"Model response = {response.choices[0].message.content}") | ||
|
|
||
|
|
||
| def main(): | ||
| configure_azure_monitor(connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]) | ||
|
|
||
| try: | ||
| endpoint = os.environ["AZURE_AI_CHAT_ENDPOINT"] | ||
| key = os.environ["AZURE_AI_CHAT_KEY"] | ||
| except KeyError: | ||
| print("Missing environment variable 'AZURE_AI_CHAT_ENDPOINT' or 'AZURE_AI_CHAT_KEY'") | ||
| print("Set them before running this sample.") | ||
| exit() | ||
|
|
||
| chat_completion_with_function_call(key, endpoint) | ||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.