forked from poe-platform/server-bot-quick-start
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbot_ResumeReview.py
170 lines (139 loc) · 6.83 KB
/
bot_ResumeReview.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
BOT_NAME="ResumeReview"; modal deploy --name $BOT_NAME bot_${BOT_NAME}.py; curl -X POST https://api.poe.com/bot/fetch_settings/$BOT_NAME/$POE_ACCESS_KEY
Test message:
(download this and upload)
https://pjreddie.com/static/Redmon%20Resume.pdf
"""
from __future__ import annotations
import os
from io import BytesIO
from typing import AsyncIterable
import fastapi_poe.client
import requests
from fastapi_poe import PoeBot
from fastapi_poe.client import MetaMessage, stream_request
from fastapi_poe.types import (
ProtocolMessage,
QueryRequest,
SettingsRequest,
SettingsResponse,
)
from sse_starlette.sse import ServerSentEvent
fastapi_poe.client.MAX_EVENT_COUNT = 10000
async def parse_pdf_document_from_url(pdf_url: str) -> tuple[bool, str]:
import pdftotext
try:
response = requests.get(pdf_url)
with BytesIO(response.content) as f:
pdf = pdftotext.PDF(f)
text = "\n\n".join(pdf)
text = text[:2000]
return True, text
except requests.exceptions.MissingSchema:
return False, ""
except BaseException:
return False, ""
async def parse_pdf_document_from_docx(docx_url: str) -> tuple[bool, str]:
from docx import Document
try:
response = requests.get(docx_url)
with BytesIO(response.content) as f:
document = Document(f)
text = [p.text for p in document.paragraphs]
text = "\n\n".join(text)
text = text[:2000]
return True, text
except requests.exceptions.MissingSchema as e:
print(e)
return False, ""
except BaseException as e:
print(e)
return False, ""
# This is now the system prompt for poe.com/ResumeReviewTool
RESUME_SYSTEM_PROMPT = """
You will be given text from a resume, extracted with Optical Character Recognition.
You will suggest specific improvements for a resume, by the standards of US/Canada industry.
Do not give generic comments.
All comments has to quote the relevant sentence in the resume where there is an issue.
You will only check the resume text for formatting errors, and suggest improvements to the bullet points.
You will not evaluate the resume, as your role is to suggest improvements.
You will focus on your comments related to tech and engineering content.
Avoid commenting on extra-curricular activities.
The following are the formmatting errors to check.
If there is a formatting error, quote the original text, and suggest how should it be rewritten.
Only raise these errors if you are confident that this is an error.
- Inconsistent date formats. Prefer Mmm YYYY for date formats.
- Misuse of capitalization. Do not capitalize words that are not capitalized in professional communication.
- Misspelling of technical terminologies. (Ignore if the error is likely to due OCR parsing inaccuracies.)
- The candidate should not explictly label their level of proficiency in the skills section.
Suggest improvements to bullet points according to these standards.
Quote the original text (always), and suggest how should it be rewritten.
- Emulate the Google XYZ formula - e.g. Accomplished X, as measured by Y, by doing Z
- Ensure the bullet points are specific.
It shows exactly what feature or system the applicant worked on, and their exact contribution.
- Specify the exact method or discovery where possible.
- Ensure the metrics presented by the resume can be objectively measured.
Do not use unmeasurable metrics like “effectiveness” or “efficiency”.
- You may assume numbers of the metrics in your recommendations.
- You may assume additional facts not mentioned in the bullet points in your recommendations.
- Prefer simpler sentence structures and active language
- Instead of "Spearheaded development ...", write "Developed ..."
- Instead of "Utilized Python to increase the performance of ...", write "Increased the performance of ... with Python"
Please suggest only the most important improvements to the resume. All your suggestions should quote from the resume.
Each suggestion should start with "Suggestion X" (e.g. Suggestion 1), and followed by two new lines.
In the suggestion, quote from the resume, and write what you suggest to improve.
At the end of each suggestion, add a markdown horizontal rule, which is `---`.
Do not reproduce the full resume unless asked. You will not evaluate the resume, as your role is to suggest improvements.
"""
class ResumeReviewBot(PoeBot):
async def get_response(self, query: QueryRequest) -> AsyncIterable[ServerSentEvent]:
for query_message in query.query:
# replace attachment with text
if (
query_message.attachments
and query_message.attachments[0].content_type == "application/pdf"
):
content_url = query_message.attachments[0].url
print("parsing pdf", content_url)
success, resume_string = await parse_pdf_document_from_url(content_url)
query_message.content += (
f"\n\n This is the attached resume: {resume_string}"
)
query_message.attachments = []
elif query_message.attachments and query_message.attachments[
0
].content_type.endswith("document"):
content_url = query_message.attachments[0].url
print("parsing docx", content_url)
success, resume_string = await parse_pdf_document_from_docx(content_url)
query_message.content += (
f"\n\n This is the attached resume: {resume_string}"
)
query_message.attachments = []
elif len(query_message.attachments) == 1 and query_message.attachments[
0
].content_type.startswith("image"):
pass
else:
query_message.attachments = []
query.query = [
ProtocolMessage(role="system", content=RESUME_SYSTEM_PROMPT)
] + query.query
current_message = ""
async for msg in stream_request(query, "Claude-3.5-Sonnet", query.api_key):
# Note: See https://poe.com/ResumeReviewTool for the prompt
if isinstance(msg, MetaMessage):
continue
elif msg.is_suggested_reply:
yield self.suggested_reply_event(msg.text)
elif msg.is_replace_response:
yield self.replace_response_event(msg.text)
else:
current_message += msg.text
yield self.replace_response_event(current_message)
async def get_settings(self, setting: SettingsRequest) -> SettingsResponse:
return SettingsResponse(
server_bot_dependencies={"Claude-3.5-Sonnet": 1},
allow_attachments=True, # to update when ready
introduction_message="Please upload your resume (pdf, docx, or image).",
)