Skip to content

Commit 00d9ce8

Browse files
committed
update
1 parent 80031e2 commit 00d9ce8

File tree

1 file changed

+45
-5
lines changed
  • sdk/cognitivelanguage/azure-ai-language-questionanswering

1 file changed

+45
-5
lines changed

sdk/cognitivelanguage/azure-ai-language-questionanswering/README.md

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,57 @@ asyncio.run(main())
233233

234234
```python
235235
import os
236+
import asyncio
236237
from azure.identity import DefaultAzureCredential
237238
from 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

Comments
 (0)