Skip to content

talk

chat_with_file(path, prompt)

Chat with a file.

Parameters:

Name Type Description Default
path str

The path to the file.

required
prompt str

The prompt to the chatbot.

required

Returns:

Name Type Description
str str

The response from the chatbot.

Source code in ttf/talk.py
 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
def chat_with_file(path: str, prompt: str) -> str:
    """
    Chat with a file.

    Args:
        path (str): The path to the file.
        prompt (str): The prompt to the chatbot.

    Returns:
        str: The response from the chatbot.
    """

    if path.endswith(".pdf"):
        reader = PdfReader(path)
        text = "\n".join([page.extract_text() for page in reader.pages])

    elif path.endswith(".txt"):
        text = open(path, "r").read()
    elif path.endswith(".csv"):
        text = pd.read_csv(path)[["date", "text"]][500:600].to_string()
    else:
        extension = path.split(".")[-1]
        raise ValueError(f'Files ending in "{extension}" are not yet supported')

    user_input = prompt + ":\n\n" + text
    chat_with(user_input)