Zekun Wu
update
0bacd3a
raw
history blame
4.76 kB
import requests
import streamlit as st
import json
from tqdm import tqdm
from openai import AzureOpenAI
import json_repair
import backoff
import os
# API setting constants
API_MAX_RETRY = 5
API_RETRY_SLEEP = 10
class GPTAgent:
def __init__(self):
self.client = AzureOpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
api_version="2024-02-15-preview",
azure_endpoint=os.getenv("END_POINTS")
)
self.deployment_name = ("gpt-4o-mini")
@backoff.on_exception(backoff.expo, requests.exceptions.RequestException, max_tries=8)
def invoke(self, text):
prompt = """You are a creative recruitment specialist tasked with generating witty and engaging recruitment potshots. Your goal is to craft short, attention-grabbing statements designed to attract potential candidates for job openings. These potshots should be tailored to the role, audience, and company values provided, and should make the job opportunity stand out in a competitive market."""
temperature = 0.9
max_tokens = 3500
response = self.client.chat.completions.create(
model=self.deployment_name,
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": text},
],
temperature=temperature,
max_tokens=max_tokens,
)
output = response.choices[0].message.content
return output
def generate_potshot_prompt(batch, role, tone, audience, values):
prompt = f"""You are a recruitment specialist tasked with generating witty and engaging recruitment potshots.
Your goal is to craft short, attention-grabbing statements designed to attract potential job candidates for the role of {role}. These statements should appeal to {audience} and reflect the company's values: {values}.
Please adopt a tone that is {tone}. Your objective is to make the job opportunity stand out and be highly appealing to the intended audience.
Provide this list of {batch} recruitment potshots in JSON format, for example:
{{"potshots": ["potshot 1", "potshot 2", ..., "potshot n"]}}
Remember to focus on being engaging, playful, and sharp in these recruitment potshots."""
return prompt.strip()
# Function to get recruitment potshots with customizations
def get_potshots(n_repeat=1, batch=20, role="Developer", tone="humorous", audience="tech-savvy candidates",
values="innovation, teamwork"):
total_potshots = []
desired_count = n_repeat * batch
with tqdm(total=desired_count, desc="Generating potshots") as pbar:
while len(total_potshots) < desired_count:
needed = min(batch, desired_count - len(total_potshots))
prompt = generate_potshot_prompt(batch=needed, role=role, tone=tone, audience=audience, values=values)
response = GPTAgent.invoke(prompt)
try:
batch_potshots = json.loads(response).get("potshots", [])
total_potshots.extend(batch_potshots)
pbar.update(len(batch_potshots))
except json.JSONDecodeError:
# Attempt to repair the JSON if decoding fails
try:
repaired_json = json_repair.repair_json(response, skip_json_loads=True, return_objects=False)
batch_potshots = json.loads(repaired_json).get("potshots", [])
total_potshots.extend(batch_potshots)
pbar.update(len(batch_potshots))
except json.JSONDecodeError:
st.error("Failed to decode JSON response even after repair attempt. Skipping this batch.")
if len(total_potshots) > desired_count:
total_potshots = total_potshots[:desired_count]
prompt_list = [{"prompt": potshot} for potshot in total_potshots]
return {"potshots": prompt_list}
# Streamlit App Interface
st.title("Customized Recruiting Potshots Generator")
# Input Fields for Customization
role = st.text_input("Job Role", "Developer")
tone = st.selectbox("Tone of the Potshots", ["humorous", "serious", "edgy", "motivational", "playful"])
audience = st.text_input("Target Audience", "tech-savvy candidates")
values = st.text_area("Company Values", "innovation, teamwork, transparency")
batch_size = st.number_input("Batch Size", min_value=1, max_value=100, value=10)
repeat_times = st.number_input("Number of Batches", min_value=1, max_value=10, value=1)
if st.button("Generate Potshots"):
with st.spinner("Generating customized potshots..."):
results = get_potshots(n_repeat=repeat_times, batch=batch_size, role=role, tone=tone, audience=audience,
values=values)
st.json(results)