-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClaude-Call
69 lines (57 loc) · 1.9 KB
/
Claude-Call
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
import docx
from markdown2 import markdown
from tqdm import tqdm
import os
import anthropic
from typing import List
MODEL_NAME = "claude-3-opus-20240229"
def generate_messages(section: str) -> List[dict]:
"""
Generates a list of user messages for a given section.
"""
return [
{
"role": "user",
"content": f"{section}"
}
]
client: anthropic.Anthropic = anthropic.Anthropic(api_key=)
print("Generating policy outline...")
system_message = ""
try:
initial_response = client.messages.create(
model=MODEL_NAME,
max_tokens=4000,
temperature=0.5,
system=system_message,
messages=[{"role": "user", "content": "P."}]
)
outline = ''.join(block.text for block in initial_response.content if block.type == 'text').strip()
except Exception as e:
print(f"An error occurred while connecting to the Claude API: {e}")
exit(1)
print(outline + "\n")
sections = outline.split("\n\n")
word_document = docx.Document()
html_sections = []
for i, section in tqdm(enumerate(sections, start=1), total=len(sections), desc="Processing Section", leave=False):
messages = generate_messages(section)
try:
response = client.messages.create(
model=MODEL_NAME,
max_tokens=4000,
messages=messages # Removed 'system=system_message' from here
)
detailed_info = ''.join(block.text for block in response.content if block.type == 'text').strip()
except Exception as e:
print(f"An error occurred: {e}")
continue
word_document.add_paragraph(detailed_info)
word_document.add_paragraph("\n")
html_sections.append(markdown(detailed_info))
docx_file_path = "e.docx"
with open(docx_file_path, "wb") as doc_file:
word_document.save(doc_file)
html_file_path = ".html"
with open(html_file_path, "w") as html_file:
html_file.write("\n".join(html_sections))