Skip to content

Commit 4f0a664

Browse files
committed
docs: add claim extraction example for fact-checking pipelines
1 parent d9aa592 commit 4f0a664

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

  • examples/claim-extraction

examples/claim-extraction/run.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
Claim Extraction Example
3+
========================
4+
5+
This example shows how to use Instructor to decompose a block of text into
6+
individual atomic claims and label each one as verifiable (a factual statement
7+
that can be checked against a source) or not (an opinion or subjective phrase).
8+
9+
This pattern is a useful building block for fact-checking and hallucination
10+
detection pipelines, where an LLM answer is first broken into small claims
11+
before each claim is verified against retrieved evidence.
12+
"""
13+
14+
from typing import List
15+
16+
import instructor
17+
from groq import Groq
18+
from pydantic import BaseModel, Field
19+
20+
21+
class Claim(BaseModel):
22+
"""A single atomic claim extracted from a larger piece of text."""
23+
24+
text: str = Field(description="The claim, stated as a short standalone sentence.")
25+
is_verifiable: bool = Field(
26+
description=(
27+
"True if the claim is a factual statement that can be checked "
28+
"against a source. False if it is an opinion or subjective."
29+
)
30+
)
31+
32+
33+
class ClaimList(BaseModel):
34+
"""A list of atomic claims extracted from the input text."""
35+
36+
claims: List[Claim]
37+
38+
39+
# Patch the Groq client so it can return structured Pydantic models.
40+
client = instructor.from_groq(Groq())
41+
42+
43+
def extract_claims(text: str) -> ClaimList:
44+
"""Break a piece of text into a list of atomic, labelled claims."""
45+
return client.chat.completions.create(
46+
model="llama-3.3-70b-versatile",
47+
response_model=ClaimList,
48+
messages=[
49+
{
50+
"role": "user",
51+
"content": f"Break the following text into individual claims: {text}",
52+
}
53+
],
54+
)
55+
56+
57+
if __name__ == "__main__":
58+
statement = (
59+
"The Eiffel Tower is in Paris and it was built in 1889. It is beautiful."
60+
)
61+
62+
result = extract_claims(statement)
63+
64+
for i, claim in enumerate(result.claims, start=1):
65+
print(f"{i}. {claim.text} -> verifiable: {claim.is_verifiable}")

0 commit comments

Comments
 (0)