Skip to content

Commit 001b0f2

Browse files
committed
update
1 parent 4981d62 commit 001b0f2

3 files changed

Lines changed: 25 additions & 29 deletions

File tree

infra/main.bicep

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ param storageAccountName string = ''
6969
@description('The log analytics workspace name. If ommited will be generated')
7070
param logAnalyticsWorkspaceName string = ''
7171
@description('Random seed to be used during generation of new resources suffixes.')
72-
param seed string = newGuid()
72+
param seed string = '' //newGuid()
7373

7474
// Chat completion model
7575
@description('Format of the chat model to deploy')
@@ -418,6 +418,15 @@ module configStore 'core/config/configstore.bicep' = {
418418
}
419419
}
420420

421+
module configStoreDataOwnerAccess 'core/security/role.bicep' = {
422+
scope: rg
423+
name: 'config-store-data-owner-role'
424+
params: {
425+
principalId: principalId
426+
roleDefinitionId: '5ae67dd6-50cb-40e7-96ff-dc2bfa4b606b' // App Configuration Data Owner
427+
}
428+
}
429+
421430
// TODO: this will go away with auto-provisioning
422431
module onlineExperimentWorkspace 'core/config/onlineexperimentworkspace.bicep' = {
423432
name: 'online-experiment-workspace'

src/api/main.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
from azure.identity import DefaultAzureCredential
2020
from azure.appconfiguration.provider import load
2121

22-
from featuremanagement import FeatureManager
23-
from featuremanagement.azuremonitor import TargetingSpanProcessor,publish_telemetry
24-
2522
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
2623

2724
from .search_index_manager import SearchIndexManager
@@ -64,10 +61,14 @@ async def lifespan(app: fastapi.FastAPI):
6461
exit()
6562
else:
6663
from azure.monitor.opentelemetry import configure_azure_monitor
64+
from featuremanagement import FeatureManager
65+
from featuremanagement.azuremonitor import publish_telemetry, TargetingSpanProcessor
66+
6767
configure_azure_monitor(connection_string=application_insights_connection_string, span_processors=[TargetingSpanProcessor(targeting_context_accessor=get_targeting_context)])
6868

69-
# Inititalize the feature manager
69+
# Inititalize the feature manager / TODO: Add null check
7070
app_config_conn_str = os.getenv("APP_CONFIGURATION_ENDPOINT") # this will become: project.experiments.get_connection_string()
71+
7172
app_config = load(
7273
endpoint=app_config_conn_str,
7374
credential=DefaultAzureCredential(),
@@ -76,6 +77,7 @@ async def lifespan(app: fastapi.FastAPI):
7677
refresh_interval=30, # 30 seconds
7778
)
7879
feature_manager = FeatureManager(app_config, targeting_context_accessor=get_targeting_context, on_feature_evaluated=publish_telemetry)
80+
app.state.feature_manager = feature_manager
7981

8082
chat = await project.inference.get_chat_completions_client()
8183
embed = await project.inference.get_embeddings_client()
@@ -105,14 +107,16 @@ async def lifespan(app: fastapi.FastAPI):
105107
app.state.chat = chat
106108
app.state.search_index_manager = search_index_manager
107109
app.state.chat_model = os.environ["AZURE_AI_CHAT_DEPLOYMENT_NAME"]
108-
app.state.feature_manager = feature_manager
110+
109111

110112
yield
111113

112114
await project.close()
113-
114115
await chat.close()
115116

117+
if search_index_manager is not None:
118+
await search_index_manager.close()
119+
116120
# Below will be replaced by a helper function from App Config SDK
117121

118122
# class TargetingSpanProcessor(SpanProcessor):
@@ -171,6 +175,7 @@ def create_app():
171175

172176
app.include_router(routes.router)
173177

174-
FastAPIInstrumentor.instrument_app(app) #, server_request_hook=server_request_hook)
178+
# TODO: do we need this?
179+
#FastAPIInstrumentor.instrument_app(app) #, server_request_hook=server_request_hook)
175180

176181
return app

src/api/routes.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from .util import get_logger, ChatRequest
2222
from .search_index_manager import SearchIndexManager
2323
from azure.core.exceptions import HttpResponseError
24-
24+
import pydantic
2525

2626
logger = get_logger(
2727
name="azureaiapp_routes",
@@ -41,7 +41,6 @@
4141
router = fastapi.APIRouter()
4242
templates = Jinja2Templates(directory="api/templates")
4343

44-
4544
# Accessors to get app state
4645
def get_chat_client(request: Request) -> ChatCompletionsClient:
4746
return request.app.state.chat
@@ -79,15 +78,13 @@ async def chat_stream_handler(
7978
chat_client: ChatCompletionsClient = Depends(get_chat_client),
8079
model_deployment_name: str = Depends(get_chat_model),
8180
search_index_manager: SearchIndexManager = Depends(get_search_index_namager),
82-
feature_manager: FeatureManager = Depends
81+
feature_manager: FeatureManager = Depends(get_feature_manager)
8382
) -> fastapi.responses.StreamingResponse:
8483
if chat_client is None:
8584
raise Exception("Chat client not initialized")
8685

8786
async def response_stream():
8887
messages = [{"role": message.role, "content": message.content} for message in chat_request.messages]
89-
model_deployment_name = globals["chat_model"]
90-
feature_manager = globals["feature_manager"]
9188

9289
targeting_id = chat_request.sessionState.get('sessionId', str(uuid.uuid4()))
9390
attach(set_baggage("Microsoft.TargetingId", targeting_id))
@@ -107,23 +104,7 @@ async def response_stream():
107104
chat_coroutine = await chat_client.complete(
108105
model=model_deployment_name, messages=prompt_messages + messages, stream=True
109106
)
110-
async for event in chat_coroutine:
111-
if event.choices:
112-
first_choice = event.choices[0]
113-
yield (
114-
json.dumps(
115-
{
116-
"delta": {
117-
"content": first_choice.delta.content,
118-
"role": first_choice.delta.role,
119-
}
120-
},
121-
ensure_ascii=False,
122-
)
123-
+ "\n"
124-
)
125107

126-
prompt_messages = PromptTemplate.from_string('You are a helpful assistant').create_messages()
127108
# Use RAG model, only if we were provided index and we have found a context there.
128109
if search_index_manager is not None:
129110
context = await search_index_manager.search(chat_request)
@@ -135,6 +116,7 @@ async def response_stream():
135116
logger.info(f"{prompt_messages=}")
136117
else:
137118
logger.info("Unable to find the relevant information in the index for the request.")
119+
138120
try:
139121
chat_coroutine = await chat_client.complete(
140122
model=model_deployment_name, messages=prompt_messages + messages, stream=True

0 commit comments

Comments
 (0)