Skip to content

Commit ae72fd6

Browse files
committed
Switch to coze v2 api
1 parent 14a5588 commit ae72fd6

File tree

4 files changed

+14
-24
lines changed

4 files changed

+14
-24
lines changed

hacker_news/llm/coze.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def summarize_by_coze(content: str) -> str:
5858
content = sanitize_for_openai(content, overhead=1000)
5959

6060
# For model: GPT-4 Turbo (128K), temperature: 0.5 - GPT-4 Turbo is an excellent rule follower.
61-
prompt = (f'Use third person mood to summarize the main points of the following article delimited by triple backticks in 2 concise sentences. '
61+
prompt = (f'Use third person mood to summarize the main points of the following article delimited by triple backticks in 2 concise English sentences. '
6262
f'Ensure the summary does not exceed 300 characters.\n'
6363
f'```{content}.```')
6464

@@ -75,20 +75,7 @@ def summarize_by_coze(content: str) -> str:
7575
'stream': False,
7676
})
7777
resp.raise_for_status()
78-
79-
for line in resp.iter_lines():
80-
if line and line.startswith(b'data:'):
81-
line = line[len(b'data:'):].strip()
82-
try:
83-
resp_json = json.loads(line)
84-
except json.JSONDecodeError as e:
85-
logger.warning(f'Failed to decode coze response, unexpected json {line}, error: {e}')
86-
return ''
87-
break
88-
else:
89-
logger.warning(f'Unexpected coze response, no data line found')
90-
return ''
91-
78+
resp_json = resp.json()
9279
except Exception as e:
9380
logger.warning(f'Failed to summarize using coze, {e}')
9481
return ''

hacker_news/llm/openai.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ def sanitize_for_openai(text, overhead):
88

99
# one token generally corresponds to ~4 characters, from https://platform.openai.com/tokenizer
1010
if len(text) > 4096 * 2:
11-
enc = tiktoken.encoding_for_model(config.openai_model)
11+
try:
12+
enc = tiktoken.encoding_for_model(config.openai_model) # We have openai compatible apis now
13+
except KeyError:
14+
enc = tiktoken.encoding_for_model('gpt-3.5-turbo')
1215
tokens = enc.encode(text)
1316
if len(tokens) > 4096 - overhead: # 4096: model's context limit
1417
text = enc.decode(tokens[:4096 - overhead])

hacker_news/news.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,13 @@ def summarize_by_openai(self, content):
147147
f'3 - Provide a Chinese translation of sentence: "{title}".\n' \
148148
f'```{content.strip(".")}.```'
149149
try:
150-
answer = self.openai_complete(prompt, True)
151-
summary = self.parse_step_answer(answer).strip()
152-
if not summary: # If step parse failed, ignore the translation
153-
summary = self.openai_complete(
154-
f'Summarize the article delimited by triple backticks in 2 sentences.\n'
155-
f'```{content.strip(".")}.```', False)
150+
# Too many exceptions to support translation, give up...
151+
# answer = self.openai_complete(prompt, True)
152+
# summary = self.parse_step_answer(answer).strip().strip(' *-')
153+
# if not summary: # If step parse failed, ignore the translation
154+
summary = self.openai_complete(
155+
f'Use third person mood to summarize the main points of the following article delimited by triple backticks in 2 concise sentences. Ensure the summary does not exceed 100 characters.\n'
156+
f'```{content.strip(".")}.```', False)
156157
return summary
157158
except Exception as e:
158159
logger.exception(f'Failed to summarize using openai, key #{config.openai_key_index}, {e}') # Make this error explicit in the log
@@ -219,7 +220,7 @@ def openai_complete(self, prompt, need_json):
219220
return answer
220221

221222
def parse_step_answer(self, answer):
222-
if not answer:
223+
if not answer or isinstance(answer, str):
223224
return answer
224225
db.translation.add(answer.get('summary', ''), answer.get('summary_zh', ''), 'zh')
225226
db.translation.add(self.title, self.parse_title_translation(answer.get('translation', '')), 'zh')

page_content_extractor/http.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import urllib3
66
from fake_useragent import UserAgent
77
from requests.adapters import HTTPAdapter
8-
from urllib3.exceptions import InsecureRequestWarning
98
from urllib3.util import timeout
109
from urllib3.util.ssl_ import create_urllib3_context
1110

0 commit comments

Comments
 (0)