-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClaudeChatUL.py
More file actions
75 lines (63 loc) · 2.45 KB
/
ClaudeChatUL.py
File metadata and controls
75 lines (63 loc) · 2.45 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import anthropic
import os
import PyPDF2
# Set up the API client
apiKey = os.getenv("ANTHROPIC_API_KEY")
client = anthropic.Anthropic(api_key=apiKey)
def extract_text_from_pdf(file_path):
with open(file_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text() + "\n"
return text
def upload_file(file_path):
"""Simulate file upload for Anthropic (you can modify based on actual API needs)."""
if not os.path.exists(file_path):
print(f"File not found: {file_path}")
return None
try:
print(f"File uploaded successfully: {file_path}")
return file_path # Simulate successful file upload
except Exception as e:
print(f"Failed to upload file: {e}")
return None
# Start the chat loop
messages = []
print("***************** N E W C H A T *****************")
while True:
print(">>>>>>>>>>>>>>>>>>>>>>>>>>")
user_input = input("Juan: ")
# Check if the user wants to exit
if user_input.lower() in ['exit', 'quit', 'bye']:
print("Claude: Goodbye!")
break
# Handle file upload with the "file:" prefix
if user_input.startswith("file:"):
file_path = user_input[5:].strip()
file_id = upload_file(file_path)
if file_id:
file_content = extract_text_from_pdf(file_id)
user_message = f"I've uploaded a PDF file. Here's the content:\n\n{file_content}\n\nPlease analyze this PDF content."
messages.append({"role": "user", "content": user_message})
print(f"File '{file_path}' uploaded and processed successfully.")
print(">>>>>>>>>>>>>>>>>>>>>>>>>>")
continue
# Add user message to the conversation
messages.append({"role": "user", "content": user_input})
# Send the message to Claude and get the response
try:
response = client.messages.create(
model="claude-3-opus-20240229",
max_tokens=1000,
temperature=0.99,
messages=messages
)
assistant_message = response.content[0].text
except Exception as e:
assistant_message = f"Error: {e}"
# Print Claude's response with <<<<<< markers
print("\n<<<<<<<<<<<<<<<<<<<<<<<<<<")
print(f"Claude: {assistant_message}")
# Add Claude's response to the conversation
messages.append({"role": "assistant", "content": assistant_message})