Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving chatgpt/telegram workload #13

Merged
merged 4 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/services/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ async def read_all(
order_by: Optional[str] = None,
skip: int = None,
per_page: int = None,
get_count: bool = False,
**kwargs
):
if order_by and order_by != "?":
Expand All @@ -129,15 +130,20 @@ async def read_all(
)
else:
order_by = tuple(order_by.split(","))

result = await self.model.objects.filter(
query = self.model.objects.filter(
joins=joins,
order_by=order_by,
skip=skip,
limit=per_page,
**kwargs
).execute(db_session=db_session)
return result
)
count = await query.count(db_session) if get_count else None
result = await query.execute(db_session=db_session)

if get_count:
return result, count
else:
return query

async def paginated_read_all(
self,
Expand All @@ -153,11 +159,10 @@ async def paginated_read_all(
query_params.update(order_by=order_by)

skip = (page_num - 1) * per_page
results = await self.read_all(
results, count = await self.read_all(
db_session, order_by=order_by, skip=skip, per_page=per_page,
joins=joins, get_count=True, **kwargs
)
count = 30
next_page = None
previous_page = None

Expand Down
2 changes: 1 addition & 1 deletion src/services/tech/loaddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
"chai",
"sinon",
"enzyme",
"react_testing_library",
"react_native",
"apollo",
"graphql",
"relay",
Expand Down
19 changes: 18 additions & 1 deletion src/services/tel_users/repositories.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Depends, Response, status
from fastapi import APIRouter, Depends, Response, status, BackgroundTasks
import redis
from .schemas import (
UserOut,
Expand All @@ -8,6 +8,7 @@
)
from db import get_redis_db
from .factory import TelegramCrud, TelegramRetriever
from . import tasks

router = APIRouter()

Expand Down Expand Up @@ -64,6 +65,7 @@ async def get_all_users(
return dict(
users=TelegramRetriever(db).get_all_active_users()
)



@router.get("/filters")
Expand All @@ -73,3 +75,18 @@ async def get_all_filters(
return dict(
users=TelegramRetriever(db).get_all_filters()
)


@router.post("/users/forward")
async def forward_to_all_users(
background_tasks: BackgroundTasks,
redis_db: redis.Redis = Depends(get_redis_db),
data: ForwardMessage = Depends(),
):
background_tasks.add_task(
tasks.forward_message_to_all,
redis_db=redis_db,
message_id=data.dict().get("message_id"),
from_chat_id=data.dict().get("from_chat_id"),
)
return Response(status_code=status.HTTP_200_OK)
1 change: 1 addition & 0 deletions src/services/tel_users/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ class UserFilter(BaseModel):

class ForwardMessage(BaseModel):
message_id: str
from_chat_id: str
12 changes: 10 additions & 2 deletions src/services/tel_users/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@


class TelegrmNotifier(TelegramRetriever):
def forward_message(self, message_id, from_chat_id, all_related_users):
def forward_message(self, message_id, from_chat_id):
token = get_app_settings().telegram_token
for user in all_related_users:
for user in self.get_all_active_users():
payload = {
"chat_id": user,
"from_chat_id": from_chat_id,
Expand All @@ -20,3 +20,11 @@ def forward_message(self, message_id, from_chat_id, all_related_users):
if response.status_code != 200 or not response.json(
).get("ok"):
logging.error(response.text)


async def forward_message_to_all(
redis_db, message_id, from_chat_id
):
TelegrmNotifier(redis_db()).forward_message(
message_id, from_chat_id
)
23 changes: 12 additions & 11 deletions tel/plugins/filters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from functools import wraps


from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant
from pyrogram import enums
from pyrogram.types import Message
import loguru
Expand All @@ -11,14 +9,17 @@


async def is_user_a_member_of_channel(bot: app, message: Message) -> bool:
user = await bot.get_chat_member(
int(env.channel_id), message.from_user.id
)
return user.status in [
enums.ChatMemberStatus.ADMINISTRATOR,
enums.ChatMemberStatus.MEMBER,
enums.ChatMemberStatus.OWNER,
]
try:
user = await bot.get_chat_member(
int(env.channel_id), message.from_user.id
)
return user.status in [
enums.ChatMemberStatus.ADMINISTRATOR,
enums.ChatMemberStatus.MEMBER,
enums.ChatMemberStatus.OWNER,
]
except UserNotParticipant:
return False


async def is_not_user_a_member_of_channel(bot: app, message: Message) -> bool:
Expand Down
20 changes: 2 additions & 18 deletions worker/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ async def create_ads(
await helpers.safe_fill_form(
chatgpt_page, xpaths.GPT_FILL,
f"""
Company Name is {company_name}

{prompt.ANALYZE_ADS} \n

{body}
{prompt.analyze_ads(company_name, body)} \n
""",
timeout=5000
)
Expand All @@ -96,19 +92,7 @@ async def create_ads(
await helpers.safe_fill_form(
chatgpt_page, xpaths.GPT_FILL,
f"""
PROMPT: Read the the text, then follow up the instructions that is given at the end.

KEYWORDS_LIST = '''{helpers.get_all_keywords()}'''
\n
\n
\n
`Job Title`: {title} \n
`Real Advertisement`: \n
{first_resp["text"]}

\n \n \n
{prompt.TAG_ADS}

{prompt.get_tag_ads(title, first_resp["text"], helpers.get_all_keywords())}
""",
timeout=5000
)
Expand Down
84 changes: 49 additions & 35 deletions worker/prompt.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,70 @@
ANALYZE_ADS = """

TASK:
Simplify a job advertisement you find while job searching.\n \n
def analyze_ads(company_name, job_description):
return f"""
company_name = "{company_name}"
TASK = 'Simplify a job advertisement you find while job searching.'\n \n

This is a sample output, it IS NOT THE ACTUAL JOB ADVERTISEMENT.\n
WARNING! This is a sample output, it IS NOT THE ACTUAL JOB ADVERTISEMENT!!
-----------------------
SAMPLE OUTPUT:

sample_output = '''
Visa Sponsor: #Yes

• 3 years of experience with Delphi programming language
• Knowledge of Rest APIs, RemObjects SDK
'''
-----------------------
\n
INSTRUCTIONS:\n
1. Summarize hard skills from the `Job Description`, removing extra unnecessary details, list them as bullet points, max 70 characters each\n
1. Summarize hard skills or requiremenets from the `job_description`, removing extra unnecessary details, list them as bullet points, max 70 characters each. DO not write more than 6 bullet point, only includes the MOST related and important one.\n
2. Translate non-English content to English. \n
3. Format visa sponsership as : `Visa Sponsor: #Yes, #No, or #NA`. \n
4. Check your 2021 database for the company's sponsorship status. Maybe in `Job Description` this was mentioned, so give priority to `Job Description` than your database \n
4. Check your 2021 database for the company's sponsorship status using `company_name`. Maybe in `job_description` this was mentioned, if so give priority to `job_description`, if not then use your database \n
5. Provide a definite sponsorship status if possible, avoiding '#NA'. \n
6. Include programming languages in frameworks from `Job Description`, if applicable. \n
7. Remember to not consider `SAMPLE OUTPUT` as the actual job advertisement, which i will send you as `Job Description` below \n
6. Include programming languages in frameworks from `job_description`, if applicable. \n
7. Remember to not consider `sample_output` as the actual job advertisement, for the acutal job advertisement, i will send it to you as `job_description` in few lines below \n
----------- \n
Job Description (The Real INPUT): \n
\n
job_description = '''
{job_description}
'''
"""

SAMPLE = {
"keywords": ["python", "django", "backend"]
}

GOOD_SAMPLE = {
"keywords": ["backend", "germany", "dot_net", "c_sharp"]
}
def get_tag_ads(title, job_description, keywords):
sample = {
"keywords": ["python", "django", "backend"]
}
good_sample = {
"keywords": ["backend", "germany", "dot_net", "c_sharp"]
}
bad_sample = {
"keywords": ["back end", "Germany", ".NET", "C#"]
}
return f"""
TASK = "Read the the text, then follow up the instructions that is given at the end."
job_title = "{title}"\n

BAD_SAMPLE = {
"keywords": ["back end", "Germany", ".NET", "C#"]
}
JOB_LISTS = `["backend", "frontend", "devops", "full_stack", "data_science", "machine_learning", "network_engineering", "cybersecurity"]` \n
This is a list of keywords, that has {len(keywords)} index \n
KEYWORDS_LIST = '''{keywords}''' \n

`job_description`: \n
{job_description} \n
\n

TAG_ADS = f"""
INSTRUCTIONS: \n
1. I'm accessing you through a Python script and I need your response in a JSON string format. Make sure that you only send me this Json, with no other text, otherwise my program would have an exception and would not work perfect. Please make sure to ONLY respond using a JSON string and adhere to the following format: \n
{str(SAMPLE)}
\n
2. Read the `KEYWORDS_LIST` that i have sent you at first. Read the `Real Advertisement` that have sent you. Check which of the provided `KEYWORDS_LIST` are mentioned and required in `Real Advertisement`. \n
3. Following step2, Do not find keywords that do not exists in the given `KEYWORDS_LIST` given at first, I only need matching KEYWORD LIST. Only include results from `KEYWORDS_LIST` which I sent you at first \n
4. Double check that keywords you've gathered in step 3 to be well-mentioned in the `Real Advertisement`
5. Double check that your gathered keyword from step4 matches the exact spelling and case of the `KEYWORDS_LIST` I provided as first, as `KEYWORDS_LIST` are case-sensitive \n
6. Do not stop writing answer unless you have at least included 5 different keyword/results, make sure they are REALLY RELEVANT, dont just write anything.\n
7. Analyze the job from `Job Title` and `Real Advertisement`, and match it with: `backend` `frontend` `devops` `software` `full_stack` `data_science` `network_engineering` `cybersecurity` or if none match, then label it as `others`. You may even pick more than 1 in some cases\n
8. Avoid the patterns of Bad Output, follow the patterns of Good Output, when generating output.\n
Good Output: {str(GOOD_SAMPLE)} \n
Bad Output: {str(BAD_SAMPLE)} \n
9. Consider `Good Output` and `Bad Output` as just examples! The actual job advertisement is marked as `Real Advertisement` which you need to analyze.
"""
1. I'm accessing you through a Python script and I need your response in a JSON string format. Make sure that you only send me this Json, with no other text, otherwise my program would have an exception and would not work perfect. Please make sure to ONLY respond using a JSON string and adhere to the following format: \n
`SAMPLE` = {str(sample)}
\n
2. Read the `KEYWORDS_LIST` that i have sent you at first. Read the `job_description` that have sent you. Check which of the provided `KEYWORDS_LIST` are mentioned and required in `job_description`. \n
3. Following step2, Do not find keywords that do not exists in the given `KEYWORDS_LIST` given at first, I only need matching KEYWORD LIST. Only include results from `KEYWORDS_LIST` which I sent you at first \n
4. Double check that keywords you've gathered in step 3 to be well-mentioned in the `job_description`
5. Double check that your gathered keyword from step4 matches the exact spelling and case of the `KEYWORDS_LIST` I provided as first, as `KEYWORDS_LIST` are case-sensitive \n
6. Do not stop writing answer unless you have at least included 5 different keyword/results, make sure they are REALLY RELEVANT, dont just write anything. Write at max 10 related ones. Include the most important ones only.\n
7. Analyze the job from `job_title` and `job_description`, and match it with `JOB_LISTS` list that I provided you at top or if none match, then label it as `others`. You may even pick more than 1 in some cases\n
8. Avoid the patterns of bad_output, follow the patterns of Good Output, when generating output.\n
`good_output` = {str(good_sample)} \n
`bad_output`= {str(bad_sample)} \n
9. Consider `good_output` and `bad_output` and `SAMPLE` as just examples! The actual job advertisement is marked as `job_description` which you need to analyze.
10. Rewrite a summary of the `job_description` so that you can understand it better. Add your rewritten text 3 lines after the json.
"""