File size: 3,448 Bytes
c83cd7e |
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
from langchain.prompts import PromptTemplate
from backend.langchain_tools import llm, deepseek_tool
from langchain_core.runnables import RunnableSequence
import json
import re
# Prompt Template
claim_classification_prompt = PromptTemplate.from_template("""
You are an expert language analyst trained to classify text-based user claims. Your task is to analyze a given piece of text and classify it into one of the following precise categories, based on meaning, structure, and intention:
Classify the following text into one of these categories:
1. Factual Claim β A statement that can be verified or disproven using evidence.
2. Opinion β A personal belief or viewpoint that cannot be objectively proven.
3. Misleading Claim β A statement that could mislead or distort facts.
4. Exaggeration β A statement that overstates facts or makes things seem more dramatic.
5. Factoid β A trivial, unverifiable claim that seems factual but lacks evidence.
6. Question β A statement framed as a question, seeking information.
7. Joke/Hyperbole β A statement made in jest or exaggeration, not to be taken literally.
8. Testimonial/Personal Experience β A personal account or anecdote.
9. Propaganda/Manipulative Claim β A claim designed to manipulate public opinion.
Text: "{claim}"
Respond in JSON format:
{{
"category": "<one of the above categories>",
"reasoning": "<brief justification>"
}}
""")
# Convert template to runnable chain
claim_chain: RunnableSequence = claim_classification_prompt | llm
# Store the prompt template string for fallback use
prompt_template_str = claim_classification_prompt.template
# Classification Function with fallback to DeepSeek-V3
def classify_claim(claim_text: str) -> dict:
try:
# Try using the primary LLM model (OpenAI)
result = claim_chain.invoke({"claim": claim_text})
classification = json.loads(result.content.strip())
if 'category' not in classification or 'reasoning' not in classification:
raise ValueError("Invalid classification format received from the model.")
return classification
except Exception as e:
# If LLM fails, use DeepSeek-V3 as a fallback
try:
print(f"Error with LLM: {e}. Falling back to DeepSeek-V3.")
# Manually construct prompt text
deepseek_prompt = prompt_template_str.format(claim=claim_text)
# Call DeepSeek with input key "input"
deepseek_result = deepseek_tool.invoke({"input": deepseek_prompt})
print("Raw DeepSeek Output:", deepseek_result)
# Remove markdown-style code block if present
cleaned_output = re.sub(r"```(?:json)?\s*([\s\S]*?)\s*```", r"\1", deepseek_result.strip())
# Parse cleaned JSON
deepseek_classification = json.loads(cleaned_output)
if 'category' not in deepseek_classification or 'reasoning' not in deepseek_classification:
raise ValueError("Invalid classification format received from DeepSeek-V3.")
return deepseek_classification
except Exception as fallback_e:
return {"error": f"An error occurred with both LLM and DeepSeek-V3: {str(fallback_e)}"}
# Example usage
if __name__ == "__main__":
claim = "modi is prime minister"
result = classify_claim(claim)
print(result)
|