-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from cs3216-a3-group-4/feat-add-point-generati…
…on-and-recommendation-from-qn feat: add point gen and recommendation
- Loading branch information
Showing
5 changed files
with
76 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from typing import List | ||
from langchain_core.messages import HumanMessage, SystemMessage | ||
from langchain_core.output_parsers import JsonOutputParser | ||
from pydantic import BaseModel | ||
from src.lm.generate_events import lm_model | ||
from src.embeddings.vector_store import get_similar_results | ||
|
||
from src.lm.prompts import QUESTION_POINT_GEN_SYSPROMPT as SYSPROMPT | ||
|
||
|
||
class Points(BaseModel): | ||
for_points: List[str] | ||
against_points: List[str] | ||
|
||
|
||
def generate_points_from_question(question: str) -> dict: | ||
messages = [SystemMessage(content=SYSPROMPT), HumanMessage(content=question)] | ||
|
||
result = lm_model.invoke(messages) | ||
parser = JsonOutputParser(pydantic_object=Points) | ||
points = parser.invoke(result) | ||
return points | ||
|
||
|
||
def get_relevant_analyses(question: str, analyses_per_point: int = 3) -> List[str]: | ||
points = generate_points_from_question(question) | ||
|
||
for_pts = points.get("for_points", []) | ||
against_pts = points.get("against_points", []) | ||
|
||
relevant_results = {"for_points": [], "against_points": []} | ||
for point in for_pts: | ||
relevant_analyses = get_similar_results(point, top_k=analyses_per_point) | ||
relevant_results.get("for_points").append( | ||
{"point": point, "analyses": relevant_analyses} | ||
) | ||
|
||
for point in against_pts: | ||
relevant_analyses = get_similar_results(point, top_k=analyses_per_point) | ||
relevant_results.get("against_points").append( | ||
{"point": point, "analyses": relevant_analyses} | ||
) | ||
|
||
return relevant_results | ||
|
||
|
||
if __name__ == "__main__": | ||
question = "Should the government provide free education for all citizens?" | ||
relevant_analyses = get_relevant_analyses(question) | ||
print(relevant_analyses) |
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