File size: 1,750 Bytes
0ae0ade
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from openai import OpenAI
import os
from typing import Tuple


def retrieve_with_gpt(pdf_file: str, claim: str) -> Tuple[str, int, int]:
    """
    Retrieve evidence from PDF using GPT

    Args:
        pdf_file: Path to PDF file
        claim: Claim to find evidence for

    Returns:
        Tuple with retrieved evidence text, prompt tokens, and completion tokens
    """

    model = "gpt-4o-mini-2024-07-18"

    prompt = """Retrieve sentences from the PDF (title, abstract, text, sections, not References/Bibliography) to support or refute this claim. \
    Summarize any information from images. \
    Respond only with verbatim sentences from the text and/or summarized sentences from images. \
    If no conclusive evidence is found, respond with the five sentences that are most relevant to the claim. \
    Combine all sentences into one response without quotation marks or line numbers. \
    """

    prompt = "".join([prompt, f"CLAIM: {claim}"])

    client = OpenAI()

    file = client.files.create(file=open(pdf_file, "rb"), purpose="user_data")

    completion = client.chat.completions.create(
        model=model,
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "file",
                        "file": {
                            "file_id": file.id,
                        },
                    },
                    {
                        "type": "text",
                        "text": prompt,
                    },
                ],
            }
        ],
    )

    return (
        completion.choices[0].message.content,
        completion.usage.prompt_tokens,
        completion.usage.completion_tokens,
    )