Skip to content

Commit f3bb2b6

Browse files
authored
Merge pull request #2 from ppl-ai/james/fact-checking-app
James/fact checking app
2 parents 4b10372 + b03fb35 commit f3bb2b6

File tree

4 files changed

+221
-66
lines changed

4 files changed

+221
-66
lines changed

.gitignore

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,88 @@
1+
# API Keys
2+
pplx_api_key
3+
.pplx_api_key
4+
PPLX_API_KEY
5+
.PPLX_API_KEY
6+
*.key
7+
.env
8+
.env.*
9+
10+
# Python
11+
__pycache__/
12+
*.py[cod]
13+
*$py.class
14+
*.so
15+
.Python
16+
build/
17+
develop-eggs/
18+
dist/
19+
downloads/
20+
eggs/
21+
.eggs/
22+
lib/
23+
lib64/
24+
parts/
25+
sdist/
26+
var/
27+
wheels/
28+
*.egg-info/
29+
.installed.cfg
30+
*.egg
31+
32+
# Virtual environments
33+
venv/
34+
env/
35+
ENV/
36+
.venv/
37+
.env/
38+
.ENV/
39+
env.bak/
40+
venv.bak/
41+
.python-version
42+
43+
# IDE specific files
44+
.idea/
45+
.vscode/
46+
*.swp
47+
*.swo
48+
*~
49+
.project
50+
.pydevproject
51+
.settings/
152
.DS_Store
2-
**/.DS_Store
53+
54+
# Testing
55+
.coverage
56+
htmlcov/
57+
.pytest_cache/
58+
.tox/
59+
nosetests.xml
60+
coverage.xml
61+
*.cover
62+
.hypothesis/
63+
64+
# Logs
65+
logs/
66+
*.log
67+
npm-debug.log*
68+
yarn-debug.log*
69+
yarn-error.log*
70+
71+
# Project-specific
72+
article.txt
73+
claims.txt
74+
facts.txt
75+
test_articles/
76+
results/
77+
custom_prompt.md
78+
output.json
79+
report.json
80+
81+
# OS specific files
82+
.DS_Store
83+
.DS_Store?
84+
._*
85+
.Spotlight-V100
86+
.Trashes
87+
ehthumbs.db
88+
Thumbs.db

README.md

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,76 @@
1-
**Sonar Cookbook**
1+
# Sonar API Projects
22

3-
This Cookbook provides code and guides designed to help developers build with Sonar, offering code snippets that can easily be integrated into your own projects.
3+
A collection of practical applications and tools built with [Perplexity's Sonar API](https://sonar.perplexity.ai/), the fastest, most cost-effective AI answer engine with robust search capabilities.
44

5-
**Prerequisites**
5+
## ⚠️ API Key Required
66

7-
You'll need an API key (sign up by following the steps [here](https://docs.perplexity.ai/guides/getting-started))
7+
**Before you begin:** You'll need a Perplexity API key to use any project in this repository.
8+
9+
Sign up for an API key by following the steps in the [Perplexity documentation](https://docs.perplexity.ai/guides/getting-started).
10+
11+
## About This Repository
12+
13+
This repository contains various projects that demonstrate how to leverage Perplexity's Sonar API for different use cases. Each project is designed to showcase the API's capabilities while providing practical, ready-to-use tools for developers.
14+
15+
## Projects
16+
17+
### Fact Checker CLI
18+
19+
The flagship project in this collection is a command-line tool that analyzes claims or articles for factual accuracy. Using Sonar API's powerful search capabilities, it identifies false or misleading statements, provides corrections, and cites reliable sources.
20+
21+
[Read more about the Fact Checker CLI →](./fact-checker/README.md)
22+
23+
### Coming Soon
24+
25+
We're actively working on adding more projects to this collection:
26+
27+
- **Research Assistant**: Automated literature review and summarization tool
28+
- **News Analyzer**: Track developing stories and analyze reporting biases
29+
- **Content Verifier**: Browser extension for real-time fact checking
30+
- **Data Explorer**: Query and visualize complex datasets through natural language
31+
32+
## Why Sonar API?
33+
34+
Perplexity's Sonar API offers several advantages that make it ideal for building powerful AI applications:
35+
36+
- **Fast & Cost-Effective**: Get reliable answers at a fraction of the cost of other AI APIs
37+
- **Search Grounding**: Internet-connected models that can access and cite the latest information
38+
- **Easy Integration**: Simple, straightforward API that developers can integrate in minutes
39+
- **Scalable Pricing**: Pay-as-you-go model that scales with your usage
40+
41+
## Getting Started
42+
43+
To use any of the projects in this repository, you'll need:
44+
45+
1. A Perplexity API key (sign up [here](https://docs.perplexity.ai/guides/getting-started))
46+
3. Python 3.7+ installed on your system
47+
48+
Each project includes its own README with specific installation and usage instructions.
49+
50+
## API Access
51+
52+
You can sign up for Perplexity's Sonar API at [sonar.perplexity.ai](https://sonar.perplexity.ai/).
53+
54+
The API offers:
55+
- Free tier to get started
56+
- Straightforward documentation
57+
- Transparent usage-based pricing
58+
59+
## Contributing
60+
61+
Contributions to this repository are welcome! Whether you want to:
62+
63+
- Fix bugs in existing projects
64+
- Add new features to current tools
65+
- Contribute entirely new Sonar API projects
66+
- Improve documentation or examples
67+
68+
Please check the [CONTRIBUTING.md](./CONTRIBUTING.md) file for guidelines.
69+
70+
## License
71+
72+
Unless otherwise specified, all projects in this repository are licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.
73+
74+
---
75+
76+
*Build with the best AI answer engine. Power your products with the fastest, cheapest API offering out there with search grounding. Get started in minutes.*

fact_checker_cli/README.md

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@ A command-line tool that identifies false or misleading claims in articles or st
44

55
## Features
66

7-
- Analyze claims or entire articles for factual accuracy
8-
- Identify false, misleading, or unverifiable claims
9-
- Provide explanations and corrections for inaccurate information
10-
- Output results in human-readable format or structured JSON
11-
- Cite reliable sources for fact-checking assessments
7+
- Analyze claims or entire articles for factual accuracy
8+
- Identify false, misleading, or unverifiable claims
9+
- Provide explanations and corrections for inaccurate information
10+
- Output results in human-readable format or structured JSON
11+
- Cite reliable sources for fact-checking assessments
1212
- Leverages Perplexity's structured outputs for reliable JSON parsing (for Tier 3+ users)
1313

1414
## Installation
1515

16-
17-
1. Install required dependencies:
16+
### 1. Install required dependencies
1817

1918
```bash
2019
pip install requests pydantic
2120
```
2221

23-
2. Make the script executable:
22+
### 2. Make the script executable
2423

2524
```bash
2625
chmod +x fact_checker.py
@@ -30,95 +29,110 @@ chmod +x fact_checker.py
3029

3130
The tool requires a Perplexity API key to function. You can provide it in one of these ways:
3231

33-
1. As a command-line argument: `--api-key YOUR_API_KEY`
34-
2. As an environment variable: `export PPLX_API_KEY=YOUR_API_KEY`
35-
3. In a file named `pplx_api_key` or `.pplx_api_key` in the same directory as the script:
32+
### 1. As a command-line argument
33+
34+
```bash
35+
./fact_checker.py --api-key YOUR_API_KEY
36+
```
37+
38+
### 2. As an environment variable
39+
40+
```bash
41+
export PPLX_API_KEY=YOUR_API_KEY
42+
```
43+
44+
### 3. In a file
45+
46+
Create a file named `pplx_api_key` or `.pplx_api_key` in the same directory as the script:
3647

3748
```bash
38-
# Create a file to store your API key
3949
echo "YOUR_API_KEY" > .pplx_api_key
40-
# Make sure to protect your API key
4150
chmod 600 .pplx_api_key
4251
```
4352

4453
**Note:** If you're using the structured outputs feature, you'll need a Perplexity API account with Tier 3 or higher access level.
4554

4655
## Quick Start
4756

48-
Here's a command you can copy and run immediately after setup:
57+
Run the following command immediately after setup:
4958

5059
```bash
51-
# Make sure your API key is set up as described above, then run:
5260
./fact_checker.py -t "The Earth is flat and NASA is hiding the truth."
5361
```
5462

5563
This will analyze the claim, research it using Perplexity's Sonar API, and return a detailed fact check with ratings, explanations, and sources.
5664

5765
## Usage
5866

59-
### Basic Usage
60-
61-
Check a claim:
67+
### Check a claim
6268

6369
```bash
6470
./fact_checker.py --text "The Earth is flat and NASA is hiding the truth."
6571
```
6672

67-
### Check an article from a file:
73+
### Check an article from a file
6874

6975
```bash
7076
./fact_checker.py --file article.txt
7177
```
7278

73-
### Specify a different model:
79+
### Specify a different model
7480

7581
```bash
7682
./fact_checker.py --text "Global temperatures have decreased over the past century." --model "sonar-pro"
7783
```
7884

79-
### Output results as JSON:
85+
### Output results as JSON
8086

8187
```bash
8288
./fact_checker.py --text "Mars has a breathable atmosphere." --json
8389
```
8490

85-
### Use a custom prompt file:
91+
### Use a custom prompt file
8692

8793
```bash
8894
./fact_checker.py --text "The first human heart transplant was performed in the United States." --prompt-file custom_prompt.md
8995
```
9096

91-
### Disable structured outputs (for lower tier accounts):
97+
### Enable structured outputs (for Tier 3+ users)
98+
99+
Structured output is disabled by default. To enable it, pass the `--structured-output` flag:
92100

93101
```bash
94-
./fact_checker.py --text "Vaccines cause autism." --no-structured-output
102+
./fact_checker.py --text "Vaccines cause autism." --structured-output
95103
```
96104

97-
### Get help:
105+
### Get help
98106

99107
```bash
100108
./fact_checker.py --help
101109
```
102110

103111
## Output Format
104112

105-
The tool provides a structured output with:
113+
The tool provides output including:
106114

107-
- Overall rating of the content (MOSTLY_TRUE, MIXED, or MOSTLY_FALSE)
108-
- Summary of findings
109-
- List of specific claims with individual ratings:
115+
- **Overall Rating**: MOSTLY_TRUE, MIXED, or MOSTLY_FALSE
116+
- **Summary**: A brief overview of the fact-checking findings
117+
- **Claims Analysis**: A list of specific claims with individual ratings:
110118
- TRUE: Factually accurate and supported by evidence
111119
- FALSE: Contradicted by evidence
112-
- MISLEADING: Contains some truth but presented in a way that could lead to incorrect conclusions
120+
- MISLEADING: Contains some truth but could lead to incorrect conclusions
113121
- UNVERIFIABLE: Cannot be conclusively verified with available information
114-
- Explanations for each claim
115-
- Sources used for verification
122+
- **Explanations**: Detailed reasoning for each claim
123+
- **Sources**: Citations and URLs used for verification
116124

117125
## Example
118126

127+
Run the following command:
128+
129+
```bash
130+
./fact_checker.py -t "The Great Wall of China is visible from the moon."
119131
```
120-
$ ./fact_checker.py -t "The Great Wall of China is visible from the moon."
121132

133+
Example output:
134+
135+
```
122136
Fact checking in progress...
123137
124138
🔴 OVERALL RATING: MOSTLY_FALSE
@@ -128,9 +142,9 @@ The claim that the Great Wall of China is visible from the moon is false. This i
128142
129143
🔍 CLAIMS ANALYSIS:
130144
131-
Claim 1: ❌ FALSE
132-
Statement: "The Great Wall of China is visible from the moon."
133-
Explanation: The Great Wall of China is not visible from the moon with the naked eye. NASA astronauts have confirmed this, including Neil Armstrong who stated he could not see the Wall from lunar orbit. The Wall is too narrow and is similar in color to its surroundings when viewed from such a distance.
145+
Claim 1: ❌ FALSE
146+
Statement: "The Great Wall of China is visible from the moon."
147+
Explanation: The Great Wall of China is not visible from the moon with the naked eye. NASA astronauts have confirmed this, including Neil Armstrong who stated he could not see the Wall from lunar orbit. The Wall is too narrow and is similar in color to its surroundings when viewed from such a distance.
134148
Sources:
135149
- NASA.gov
136150
- Scientific American
@@ -139,7 +153,7 @@ Claim 1: ❌ FALSE
139153

140154
## Limitations
141155

142-
- The accuracy of fact-checking depends on the quality of information available through the Perplexity Sonar API
143-
- Like all language models, the underlying AI may have limitations in certain specialized domains
144-
- The structured outputs feature requires a Tier 3 or higher Perplexity API account
145-
- The tool does not replace professional fact-checking services for highly sensitive or complex content
156+
- The accuracy of fact-checking depends on the quality of information available through the Perplexity Sonar API.
157+
- Like all language models, the underlying AI may have limitations in certain specialized domains.
158+
- The structured outputs feature requires a Tier 3 or higher Perplexity API account.
159+
- The tool does not replace professional fact-checking services for highly sensitive or complex content.

0 commit comments

Comments
 (0)