MedQAAssistant.predict:v32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import weave
@weave.op()
def predict(self, query: str, options: Optional[list[str]] = None) -> MedQAResponse:
"""
Generates a response to a medical query by retrieving relevant text chunks and figure descriptions
from a medical document and using a language model to generate the final response.
This function performs the following steps:
1. Retrieves relevant text chunks from the medical document based on the query and any provided options
using the retriever model.
2. Extracts the text and page indices from the retrieved chunks.
3. Retrieves figure descriptions from the pages identified in the previous step using the figure annotator.
4. Constructs a system prompt and user prompt combining the query, options (if provided), retrieved text chunks,
and figure descriptions.
5. Uses the language model client to generate a response based on the constructed prompts, either choosing
from provided options or generating a free-form response.
6. Returns the generated response, which includes the answer and explanation if options were provided.
The function can operate in two modes:
- Multiple choice: When options are provided, it selects the best answer from the options and explains the choice
- Free response: When no options are provided, it generates a comprehensive response based on the context
Args: