@@ -233,17 +233,57 @@ asyncio.run(main())
233233
234234``` python
235235import os
236+ import asyncio
236237from azure.identity import DefaultAzureCredential
237238from azure.ai.language.questionanswering.aio import QuestionAnsweringClient
238239
239- endpoint = os.environ[" AZURE_QUESTIONANSWERING_ENDPOINT" ]
240- client = QuestionAnsweringClient(endpoint, DefaultAzureCredential())
240+ async def main ():
241+ endpoint = os.environ[" AZURE_QUESTIONANSWERING_ENDPOINT" ]
242+ client = QuestionAnsweringClient(endpoint, DefaultAzureCredential())
243+ output = await client.get_answers(
244+ question = " How long should my Surface battery last?" ,
245+ project_name = " FAQ" ,
246+ deployment_name = " production"
247+ )
248+ for candidate in output.answers:
249+ print (f " ( { candidate.confidence:.2f } ) { candidate.answer} " )
250+
251+ asyncio.run(main())
252+ ```
253+
254+ #### Filtering with metadata (QueryFilters)
241255
242- output = await client.get_answers(
256+ You can narrow answers using metadata stored in your knowledge base:
257+
258+ ``` python
259+ from azure.ai.language.questionanswering.models import (
260+ AnswersOptions,
261+ QueryFilters,
262+ MetadataFilter,
263+ MetadataRecord
264+ )
265+
266+ # Tuple form (supported)
267+ metadata_filter_tuple = MetadataFilter(metadata = [(" product" , " surface" ), (" locale" , " en-US" )])
268+
269+ # MetadataRecord form (recommended for static typing)
270+ metadata_filter_records = MetadataFilter(metadata = [
271+ MetadataRecord(key = " product" , value = " surface" ),
272+ MetadataRecord(key = " locale" , value = " en-US" )
273+ ])
274+
275+ options = AnswersOptions(
243276 question = " How long should my Surface battery last?" ,
244- project_name = " FAQ" ,
245- deployment_name = " production"
277+ filters = QueryFilters(metadata_filter = metadata_filter_tuple),
278+ confidence_threshold = 0.2 ,
279+ top = 3
246280)
281+
282+ resp = client.get_answers(options, project_name = " FAQ" , deployment_name = " production" )
283+ for ans in resp.answers:
284+ print (f " { ans.answer} ( { ans.confidence:.2f } ) " )
285+
286+ # Note: Passing metadata as a dict (e.g. {'product': 'surface'}) is no longer supported.
247287```
248288
249289## Optional Configuration
0 commit comments