Skip to content

Commit c8d2593

Browse files
committed
make --no-structured-output default
1 parent a2cb7c1 commit c8d2593

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

fact_checker_cli/fact_checker.py

100755100644
Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env python3
22
"""
33
Fact Checker CLI - A tool to identify false or misleading claims in articles or statements
4-
using Perplexity's Sonar API with structured outputs.
4+
using Perplexity's Sonar API.
5+
Structured output is disabled by default.
56
"""
67

78
import argparse
@@ -64,12 +65,10 @@ def _get_api_key(self) -> str:
6465
Returns:
6566
The API key if found, empty string otherwise.
6667
"""
67-
# Try environment variable
6868
api_key = os.environ.get("PPLX_API_KEY", "")
6969
if api_key:
7070
return api_key
7171

72-
# Try to read from a file named pplx_api_key or .pplx_api_key in the current directory
7372
for key_file in ["pplx_api_key", ".pplx_api_key", "PPLX_API_KEY", ".PPLX_API_KEY"]:
7473
key_path = Path(key_file)
7574
if key_path.exists():
@@ -102,7 +101,7 @@ def _load_system_prompt(self, prompt_file: str) -> str:
102101
"Focus on identifying false, misleading, or unsubstantiated claims."
103102
)
104103

105-
def check_claim(self, text: str, model: str = DEFAULT_MODEL, use_structured_output: bool = True) -> Dict[str, Any]:
104+
def check_claim(self, text: str, model: str = DEFAULT_MODEL, use_structured_output: bool = False) -> Dict[str, Any]:
106105
"""
107106
Check the factual accuracy of a claim or article.
108107
@@ -122,7 +121,6 @@ def check_claim(self, text: str, model: str = DEFAULT_MODEL, use_structured_outp
122121
"Authorization": f"Bearer {self.api_key}"
123122
}
124123

125-
# Basic message structure
126124
data = {
127125
"model": model,
128126
"messages": [
@@ -131,7 +129,6 @@ def check_claim(self, text: str, model: str = DEFAULT_MODEL, use_structured_outp
131129
]
132130
}
133131

134-
# Add structured output format if supported and requested
135132
can_use_structured_output = model in self.STRUCTURED_OUTPUT_MODELS and use_structured_output
136133
if can_use_structured_output:
137134
data["response_format"] = {
@@ -144,17 +141,14 @@ def check_claim(self, text: str, model: str = DEFAULT_MODEL, use_structured_outp
144141
response.raise_for_status()
145142
result = response.json()
146143

147-
# Extract any citations from the top-level response if present
148144
citations = result.get("citations", [])
149145

150-
# Extract the content from the response
151146
if "choices" in result and result["choices"] and "message" in result["choices"][0]:
152147
content = result["choices"][0]["message"]["content"]
153148

154149
if can_use_structured_output:
155150
try:
156151
parsed = json.loads(content)
157-
# Merge top-level citations if they aren't already in parsed data
158152
if citations and "citations" not in parsed:
159153
parsed["citations"] = citations
160154
return parsed
@@ -187,7 +181,6 @@ def _parse_response(self, content: str) -> Dict[str, Any]:
187181
A dictionary with parsed JSON fields or with a fallback containing raw response and extracted citations.
188182
"""
189183
try:
190-
# Try to extract JSON from markdown-formatted content
191184
if "```json" in content:
192185
json_content = content.split("```json")[1].split("```")[0].strip()
193186
return json.loads(json_content)
@@ -197,7 +190,6 @@ def _parse_response(self, content: str) -> Dict[str, Any]:
197190
else:
198191
return json.loads(content)
199192
except (json.JSONDecodeError, IndexError):
200-
# Fallback: attempt to extract citations using a regex.
201193
citations = re.findall(r"Sources?:\s*(.+)", content)
202194
return {
203195
"raw_response": content,
@@ -224,15 +216,12 @@ def display_results(results: Dict[str, Any], format_json: bool = False):
224216
print(json.dumps(results, indent=2))
225217
return
226218

227-
# If structured keys exist, we update claim sources if needed.
228219
if "overall_rating" in results:
229-
# If we have a top-level citations list, map inline markers to full URLs.
230220
citation_list = results.get("citations", [])
231221
if citation_list and "claims" in results:
232222
for claim in results["claims"]:
233223
updated_sources = []
234224
for source in claim.get("sources", []):
235-
# If the source is just a marker like "[13]", extract the number.
236225
m = re.match(r"\[(\d+)\]", source.strip())
237226
if m:
238227
idx = int(m.group(1)) - 1
@@ -275,7 +264,6 @@ def display_results(results: Dict[str, Any], format_json: bool = False):
275264
for source in claim["sources"]:
276265
print(f" - {source}")
277266

278-
# Fallback for non-structured output
279267
elif "raw_response" in results:
280268
print("Response:")
281269
print(results["raw_response"])
@@ -287,7 +275,6 @@ def display_results(results: Dict[str, Any], format_json: bool = False):
287275
else:
288276
print(f" {results['extracted_citations']}")
289277

290-
# Also print any top-level citations if present (for extra clarity)
291278
if "citations" in results:
292279
print("\nCitations:")
293280
for citation in results["citations"]:
@@ -330,17 +317,16 @@ def main():
330317
help="Output results as JSON"
331318
)
332319
parser.add_argument(
333-
"--no-structured-output",
320+
"--structured-output",
334321
action="store_true",
335-
help="Disable structured output format (not recommended)"
322+
help="Enable structured output format (default is non-structured output)"
336323
)
337324

338325
args = parser.parse_args()
339326

340327
try:
341328
fact_checker = FactChecker(api_key=args.api_key, prompt_file=args.prompt_file)
342329

343-
# Get text from file if provided
344330
if args.file:
345331
try:
346332
with open(args.file, "r", encoding="utf-8") as f:
@@ -355,7 +341,7 @@ def main():
355341
results = fact_checker.check_claim(
356342
text,
357343
model=args.model,
358-
use_structured_output=not args.no_structured_output
344+
use_structured_output=args.structured_output
359345
)
360346
display_results(results, format_json=args.json)
361347

@@ -367,4 +353,4 @@ def main():
367353

368354

369355
if __name__ == "__main__":
370-
sys.exit(main())
356+
sys.exit(main())

0 commit comments

Comments
 (0)