Skip to content

Commit

Permalink
Merge pull request #45 from cs3216-a3-group-4/feat-add-point-generati…
Browse files Browse the repository at this point in the history
…on-and-recommendation-from-qn

feat: add point gen and recommendation
  • Loading branch information
marcus-ny authored Sep 24, 2024
2 parents 570d14e + ff19b31 commit 0ecab69
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
2 changes: 1 addition & 1 deletion backend/src/embeddings/vector_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def store_documents():
print(f"Stored {len(documents)} documents")


def get_similar_results(query: str, top_k: int = 5):
def get_similar_results(query: str, top_k: int = 3):
documents = vector_store.similarity_search_with_relevance_scores(
query=query, k=top_k
)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/lm/generate_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from src.common.constants import LANGCHAIN_API_KEY
from src.common.constants import LANGCHAIN_TRACING_V2
from src.common.constants import OPENAI_API_KEY
from src.lm.prompts import SYSPROMPT
from src.lm.prompts import EVENT_GEN_SYSPROMPT as SYSPROMPT

import os

Expand Down
50 changes: 50 additions & 0 deletions backend/src/lm/generate_points.py
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)
24 changes: 23 additions & 1 deletion backend/src/lm/prompts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SYSPROMPT = """
EVENT_GEN_SYSPROMPT = """
You are a Singaporean student studying for your A Levels. You are curating examples to supplement and bolster your arguments in your General Paper essays.
Given an article, you should provide relevant examples that can be used to support or refute arguments in a General Paper essay.
Given the article, you should also generate 2-3 GCE A Level General Paper essay questions that can potentially be answered using the events you have provided.
Expand Down Expand Up @@ -45,3 +45,25 @@
The article:
"""

QUESTION_POINT_GEN_SYSPROMPT = """
You are a Singaporean student studying for your GCE A Levels General Paper.
Given an General Paper essay question that is argumentative or discursive in nature, you should provide points that can be used to support or refute the argument in the question.
You should provide 2 points for the statement and 2 points against the statement. You should also provide a brief explanation for each point.
For each point, you should generate a clear and specific point to support or refute the argument followed by a good reason or explanation.
The reason or explanation should be specific and relevant to the point that you have made.
Do not provide any examples in your response.
Your response should be in the following json format:
{
"for_points": [
"The point that supports the argument + The explanation for the point",
],
"against_points": [
"The point that refutes the argument + The explanation for the point",
]
}
The question:
"""
2 changes: 1 addition & 1 deletion backend/src/scripts/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
def add_categories():
CATEGORIES = [
"Arts & Humanities",
"Science & Technology",
"Science & Tech",
"Politics",
"Media",
"Environment",
Expand Down

0 comments on commit 0ecab69

Please sign in to comment.