-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect the prerequisites of user query (#307)
- Loading branch information
1 parent
465cfe6
commit fe31504
Showing
8 changed files
with
156 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import logging | ||
import dspy | ||
from dspy.functional import TypedChainOfThought, TypedPredictor | ||
from typing import List, Optional | ||
from pydantic import BaseModel, Field | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class Prerequisites(BaseModel): | ||
"""Decomposed prerequisite questions extracted from the main query""" | ||
|
||
questions: List[str] = Field( | ||
description="List of prerequisite questions necessary for answering the main query." | ||
) | ||
|
||
class DecomposePrerequisites(dspy.Signature): | ||
"""You are an expert in query analysis and decomposition. Your task is to identify any prerequisite questions that need to be answered in order to fully address the main query. | ||
Step-by-Step Analysis: | ||
1. Analyze the main query to identify terms, references, or concepts that are unclear, ambiguous, or require additional information. | ||
- Focus on entities like versions, specific terminologies, or any references that may not be immediately clear. | ||
2. Formulate clear and concise prerequisite questions that, when answered, will provide the necessary information to address the main query effectively. | ||
## Instructions: | ||
- Limit the number of prerequisite questions to no more than 3. | ||
- Ensure that the prerequisite questions are directly relevant and necessary for answering the main query. | ||
- Do not include unnecessary or unrelated questions. | ||
- Ensure that the questions are grounded and factual, based on the query provided. | ||
""" | ||
|
||
query: str = dspy.InputField( | ||
desc="The main query text that needs to be analyzed for prerequisite questions." | ||
) | ||
prerequisites: Prerequisites = dspy.OutputField( | ||
desc="The decomposed prerequisite questions extracted from the main query." | ||
) | ||
|
||
class DecomposePrerequisitesModule(dspy.Module): | ||
def __init__(self, dspy_lm: dspy.LM): | ||
super().__init__() | ||
self.dspy_lm = dspy_lm | ||
self.prog = TypedChainOfThought(DecomposePrerequisites) | ||
|
||
def forward(self, query): | ||
with dspy.settings.context(lm=self.dspy_lm): | ||
return self.prog(query=query) | ||
|
||
class PrerequisiteAnalyzer: | ||
def __init__(self, dspy_lm: dspy.LM, compiled_program_path: Optional[str] = None): | ||
self.prerequisite_analysis_prog = DecomposePrerequisitesModule(dspy_lm=dspy_lm) | ||
if compiled_program_path is not None: | ||
self.prerequisite_analysis_prog.load(compiled_program_path) | ||
|
||
def analyze(self, query: str) -> Prerequisites: | ||
return self.prerequisite_analysis_prog(query=query).prerequisites |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
backend/dspy_compiled_program/decompose_prerequisites_program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"prog.predictor": { | ||
"lm": null, | ||
"traces": [], | ||
"train": [], | ||
"demos": [ | ||
{ | ||
"query": "Explain the impact of artificial intelligence on modern education.", | ||
"prerequisites": "{\"questions\":[\"What are the current applications of artificial intelligence in education?\",\"What defines modern education in today's context?\",\"How is artificial intelligence different from traditional educational technologies?\"]}" | ||
}, | ||
{ | ||
"query": "summary the performance improvement from version 6.5 to newest version for TiDB", | ||
"prerequisites": "{\"questions\":[\"What is the newest version of TiDB?\",\"What performance enhancements have been made in TiDB versions between 6.5 and the newest version?\"]}" | ||
} | ||
], | ||
"signature_instructions": "You are an expert in query analysis and decomposition. Your task is to identify any prerequisite questions that need to be answered in order to fully address the main query.\n\nStep-by-Step Analysis:\n\n1. Analyze the main query to identify terms, references, or concepts that are unclear, ambiguous, or require additional information.\n - Focus on entities like versions, specific terminologies, or any references that may not be immediately clear.\n2. Formulate clear and concise prerequisite questions that, when answered, will provide the necessary information to address the main query effectively.\n\n## Instructions:\n\n- Limit the number of prerequisite questions to no more than 3.\n- Ensure that the prerequisite questions are directly relevant and necessary for answering the main query.\n- Do not include unnecessary or unrelated questions.\n- Ensure that the questions are grounded and factual, based on the query provided.", | ||
"signature_prefix": "Prerequisites:" | ||
} | ||
} |
Binary file not shown.