Skip to content

utils

chat_with(user_input)

summary

Parameters:

Name Type Description Default
user_input str

description

required
Source code in ttf/utils.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def chat_with(user_input:str)-> None:
    """_summary_

    Args:
        user_input (str): _description_
    """
    # Initialize the conversation history with a message from the chatbot
    # message_log = [{"role": "system", "content": "You are a helpful assistant."}]
    message_log = []

    # Set a flag to keep track of whether this is the first request in the conversation
    first_request = True

    console = Console()

    # Start a loop that runs until the user types "quit"
    while True:
        if first_request:
            # If this is the first request, get the user's input and add it to the conversation history
            # user_input = input("You: ")
            # user_input = Prompt.ask("You   ")
            message_log.append(
                {"role": "user", "content": [{"type": "text", "text": user_input}]}
            )

            # Send the conversation history to the chatbot and get its response
            response = send_message(message_log)

            # Add the chatbot's response to the conversation history and print it to the console
            message_log.append({"role": "assistant", "content": [{"type": "text", "text": response}]})
            # print(f"AI assistant: {response}")

            console.print(Markdown(f"Haiku : {response}\n\n---"))
            # Set the flag to False so that this branch is not executed again
            first_request = False
        else:
            # If this is not the first request, get the user's input and add it to the conversation history
            # user_input = input("You: ")
            user_input = Prompt.ask("You   ")

            # If the user types "quit", end the loop and print a goodbye message
            if user_input.lower() == "quit" or user_input.lower() == "exit":
                # print("Goodbye!")
                console.print("Goodbye!")
                break

            message_log.append(
                {"role": "user", "content": [{"type": "text", "text": user_input}]}
            )

            # Send the conversation history to the chatbot and get its response
            response = send_message(message_log)

            # Add the chatbot's response to the conversation history and print it to the console
            message_log.append({"role": "assistant", "content": [{"type": "text", "text": response}]})
            # print(f"AI assistant: {response}")
            console.print(Markdown(f"Haiku : {response}\n\n---"))

send_message(message_log)

summary

Parameters:

Name Type Description Default
message_log list

description

required

Raises:

Type Description
ValueError

description

Returns:

Name Type Description
str str

description

Source code in ttf/utils.py
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
def send_message(message_log: list) -> str:
    """_summary_

    Args:
        message_log (list): _description_

    Raises:
        ValueError: _description_

    Returns:
        str: _description_
    """
    try:
        api_key = os.environ["ANTHROPIC_API_KEY"]
    except KeyError:
        raise ValueError("ANTHROPIC_API_KEY not found in environment variables")

    client = anthropic.Client(api_key=api_key)

    response = client.messages.create(
        model="claude-3-haiku-20240307",
        max_tokens=3800,
        system="Respond like a no non-sense assistant who answers to the point without any bullshit",
        messages=message_log,
    )
    return response.content[0].text

summarize_pdf_old(client, path, prompt)

summary

Parameters:

Name Type Description Default
client Client

description

required
path str

description

required
prompt str

description

required

Returns:

Name Type Description
str str

description

Source code in ttf/utils.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def summarize_pdf_old(client: anthropic.Client, path: str, prompt:str) -> str:
    """_summary_

    Args:
        client (anthropic.Client): _description_
        path (str): _description_
        prompt (str): _description_

    Returns:
        str: _description_
    """
    reader = PdfReader(path)
    text = "\n".join([page.extract_text() for page in reader.pages])

    message_log = []
    message_log.append(
                {"role": "user", "content": [{"type": "text", "text": prompt + ":\n\n" + text}]}
            )

    # prompt = f"{anthropic.HUMAN_PROMPT}: Summarize the following text, should be readable:\n\n{text}\n\n{anthropic.AI_PROMPT}:\n\nSummary"

    response = client.messages.create(
        model="claude-3-haiku-20240307",
        max_tokens=3800,
        system="Respond like a no non-sense assistant who answers to the point without any bullshit",
        messages=message_log,
    )

    return response.content[0].text