-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02_06_solution.py
35 lines (28 loc) · 974 Bytes
/
02_06_solution.py
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
# pip install llama-index-llms-openai-like
from llama_index.llms.openai_like import OpenAILike
from llama_index.core.llms import ChatMessage
from llama_index.core.readers import SimpleDirectoryReader
application_prompt = """Given the following documents,
summarize them so that each section contains only the most
important information and relevant facts:
DOCUMENT:
"""
llm = OpenAILike(
is_chat_model=True,
temperature=0.7,
model="gpt-4-1106-preview" # 128K context window
)
documents = SimpleDirectoryReader("handbook").load_data()
fulltext = "\n\n".join([d.get_text() for d in documents])
textlen = len(fulltext)
print(f"Document text size is {textlen}")
if textlen > 100000:
print("Too much text")
exit()
messages = [
ChatMessage(role="system", content=application_prompt),
ChatMessage(role="user", content=fulltext),
]
results = llm.chat(messages)
with open("summary.txt", "w") as f:
f.write(results.message.content)