I’m running the azure-search-openai-demo, and I’ve successfully implemented searching across all files within a container in Azure Blob Storage. However, I want to limit the search to a specific file rather than searching through all the files in the container.
Current Setup:
I’m using the demo’s search capability, which is currently configured to search across all files in the container.
I want to modify the code to search within only one specific file.
Question:
How can I adjust the existing demo code to search within a single specified file instead of all files in the container? What modifications are needed to target just one file?
Any guidance or examples would be greatly appreciated!
@authenticated
async def chat_stream(auth_claims: Dict[str, Any]):
if not request.is_json:
return jsonify({"error": "request must be json"}), 415
request_json = await request.get_json()
context = request_json.get("context", {})
context["auth_claims"] = auth_claims
try:
use_gpt4v = context.get("overrides", {}).get("use_gpt4v", False)
approach: Approach
if use_gpt4v and CONFIG_CHAT_VISION_APPROACH in current_app.config:
approach = cast(Approach, current_app.config[CONFIG_CHAT_VISION_APPROACH])
else:
approach = cast(Approach, current_app.config[CONFIG_CHAT_APPROACH])
result = await approach.run_stream(
request_json["messages"],
context=context,
session_state=request_json.get("session_state"),
)
response = await make_response(format_as_ndjson(result))
response.timeout = None # type: ignore
response.mimetype = "application/json-lines"
return response
except Exception as error:
return error_response(error, "/chat")```
2
Answers
To modify the
azure-search-openai-demo
to search within a specific file instead of searching across all files in the Azure Blob Storage container, you need to adjust the code that handles the search query.I followed this document for Azure AI Search with OpenAI.
Modify the
chat_stream
function to include filtering by a specific file:If your
Approach
class’srun_stream
method doesn’t support asearch_filter
parameter, you’ll need to modify it to include this functionality:Output:
Put the name of the blob/file you are trying to filter in the filter build_filter inside the approach.py.
In my case im sending the blob in the overrides call from the frontend to the /chat/steam endpoint in the app.py.
It is then possible to use in the build_filter like so: