-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Azure AppInsights Monitoring using Open telemetry #475
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
This file contains 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 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,15 +1,49 @@ | ||
from typing import Any, Sequence | ||
|
||
import os | ||
import logging | ||
from logging.handlers import RotatingFileHandler | ||
import openai | ||
import tiktoken | ||
from azure.search.documents import SearchClient | ||
from azure.search.documents.models import QueryType | ||
from approaches.approach import Approach | ||
from text import nonewlines | ||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleExportSpanProcessor | ||
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter | ||
|
||
from core.messagebuilder import MessageBuilder | ||
from core.modelhelper import get_token_limit | ||
|
||
AZURE_MONITOR_CONNECTION_STRING = os.environ.get("AZURE_MONITOR_CONNECTION_STRING") or "" | ||
|
||
# Set up Azure Monitor Trace Exporter | ||
azure_exporter = AzureMonitorTraceExporter( | ||
connection_string=AZURE_MONITOR_CONNECTION_STRING | ||
) | ||
|
||
# Set the tracer_provider to the SDK's TracerProvider | ||
trace.set_tracer_provider(TracerProvider()) | ||
|
||
# Configure the tracer_provider with the Azure exporter | ||
trace.get_tracer_provider().add_span_processor( | ||
SimpleExportSpanProcessor(azure_exporter) | ||
) | ||
# Get the tracer | ||
tracer = trace.get_tracer(__name__) | ||
|
||
# Now you can use `tracer.start_as_current_span` to create spans | ||
|
||
# Set up logging | ||
logging.basicConfig(level=logging.ERROR, | ||
format="%(asctime)s [%(levelname)s] %(message)s", | ||
handlers=[ | ||
RotatingFileHandler("app.log", maxBytes=100000, backupCount=10), | ||
logging.StreamHandler() | ||
]) | ||
logger = logging.getLogger(__name__) | ||
|
||
class ChatReadRetrieveReadApproach(Approach): | ||
# Chat roles | ||
SYSTEM = "system" | ||
|
@@ -58,14 +92,15 @@ def __init__(self, search_client: SearchClient, chatgpt_deployment: str, chatgpt | |
self.chatgpt_token_limit = get_token_limit(chatgpt_model) | ||
|
||
def run(self, history: Sequence[dict[str, str]], overrides: dict[str, Any]) -> Any: | ||
has_text = overrides.get("retrieval_mode") in ["text", "hybrid", None] | ||
has_vector = overrides.get("retrieval_mode") in ["vectors", "hybrid", None] | ||
use_semantic_captions = True if overrides.get("semantic_captions") and has_text else False | ||
top = overrides.get("top") or 3 | ||
exclude_category = overrides.get("exclude_category") or None | ||
filter = "category ne '{}'".format(exclude_category.replace("'", "''")) if exclude_category else None | ||
with tracer.start_as_current_span("ChatReadRetrieveReadApproach.run"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there an approach that doesnt require adding "with tracer" to each of these functions? Ideally, we could just attach middleware to the routes. |
||
has_text = overrides.get("retrieval_mode") in ["text", "hybrid", None] | ||
has_vector = overrides.get("retrieval_mode") in ["vectors", "hybrid", None] | ||
use_semantic_captions = True if overrides.get("semantic_captions") and has_text else False | ||
top = overrides.get("top") or 3 | ||
exclude_category = overrides.get("exclude_category") or None | ||
filter = "category ne '{}'".format(exclude_category.replace("'", "''")) if exclude_category else None | ||
|
||
user_q = 'Generate search query for: ' + history[-1]["user"] | ||
user_q = 'Generate search query for: ' + history[-1]["user"] | ||
|
||
# STEP 1: Generate an optimized keyword search query based on the chat history and the last question | ||
messages = self.get_messages_from_history( | ||
|
@@ -181,4 +216,4 @@ def get_messages_from_history(self, system_prompt: str, model_id: str, history: | |
break | ||
|
||
messages = message_builder.messages | ||
return messages | ||
return messages |
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please set AZURE_MONITOR_CONNECTION_STRING in infra/main.bicep