-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_summarization_from_content.py
More file actions
74 lines (60 loc) · 1.86 KB
/
Copy pathget_summarization_from_content.py
File metadata and controls
74 lines (60 loc) · 1.86 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
import os
import time
import requests
ENDPOINT = os.environ["AZURE_OPENAI_ENDPOINT"]
API_KEY = os.environ["AZURE_OPENAI_API_KEY"]
DEPLOYMENT = os.environ["AZURE_OPENAI_DEPLOYMENT"]
API_VERSION = os.environ["AZURE_OPENAI_API_VERSION"]
def chunk_text(text, max_chars):
chunks = []
start = 0
length = len(text)
while start < length:
end = start + max_chars
chunks.append(text[start:end])
start = end
return chunks
def summarize_chunk(text):
url = f"{ENDPOINT}/openai/deployments/{DEPLOYMENT}/chat/completions?api-version={API_VERSION}"
headers = {
"Content-Type": "application/json",
"api-key": API_KEY
}
payload = {
"messages": [
{
"role": "system",
"content": "You are a professional summarization engine."
},
{
"role": "user",
"content": f"Summarize the given text: \n\n{text}"
}
],
"temperature": 0.2
}
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]
def extract_from_chunk(query, text):
url = f"{ENDPOINT}/openai/deployments/{DEPLOYMENT}/chat/completions?api-version={API_VERSION}"
headers = {
"Content-Type": "application/json",
"api-key": API_KEY
}
payload = {
"messages": [
{
"role": "system",
"content": "You are a professional extraction engine."
},
{
"role": "user",
"content": f"{query}: \n\n{text}"
}
],
"temperature": 0.2
}
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]