Commit ·
c7e6f79
1
Parent(s): 01f2945
perf: keywords nodes use Pydantic structured output
Browse files
app.py
CHANGED
|
@@ -86,20 +86,17 @@ def classify_individual_topic(topic_name: str):
|
|
| 86 |
def node_runner(state: MultiTopicState):
|
| 87 |
llm = ChatOpenAI(model="gpt-4o", temperature=0)
|
| 88 |
# Force strict structured output format
|
| 89 |
-
|
| 90 |
target_sub_tree = SUB_TREE_LOOKUP.get(topic_name, "")
|
| 91 |
|
| 92 |
prompt = ChatPromptTemplate.from_messages([
|
| 93 |
-
|
| 94 |
-
("system", f"You are a specialist in {topic_name} data mapping. Extract exact keyword pathways present in the provided list."),
|
| 95 |
("user", "Title: {title}\nAbstract: {abstract}\n\nValid Paths:\n{sub_tree}\n\nExtract matching pathways as a structured array list.")
|
| 96 |
])
|
| 97 |
|
| 98 |
# Enforce JSON list output natively
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
#raw_keywords = [k.strip() for k in result.keywords if k.strip()]
|
| 102 |
-
raw_keywords = [k.strip() for k in result.content.split(",") if k.strip()]
|
| 103 |
|
| 104 |
# Immediate validation pass inside the node branch
|
| 105 |
valid_set = set()
|
|
|
|
| 86 |
def node_runner(state: MultiTopicState):
|
| 87 |
llm = ChatOpenAI(model="gpt-4o", temperature=0)
|
| 88 |
# Force strict structured output format
|
| 89 |
+
structured_llm = llm.with_structured_output(KeywordExtraction)
|
| 90 |
target_sub_tree = SUB_TREE_LOOKUP.get(topic_name, "")
|
| 91 |
|
| 92 |
prompt = ChatPromptTemplate.from_messages([
|
| 93 |
+
("system", f"You are a specialist in {topic_name} data mapping. Extract all exact matching keyword pathways present in the provided valid path list. Do not modify or truncate the pathway strings."),
|
|
|
|
| 94 |
("user", "Title: {title}\nAbstract: {abstract}\n\nValid Paths:\n{sub_tree}\n\nExtract matching pathways as a structured array list.")
|
| 95 |
])
|
| 96 |
|
| 97 |
# Enforce JSON list output natively
|
| 98 |
+
result = structured_llm.invoke(prompt.format(title=state["title"], abstract=state["abstract"], sub_tree=target_sub_tree))
|
| 99 |
+
raw_keywords = [k.strip() for k in result.keywords if k.strip()]
|
|
|
|
|
|
|
| 100 |
|
| 101 |
# Immediate validation pass inside the node branch
|
| 102 |
valid_set = set()
|