Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -731,6 +731,10 @@ async def api_info():
|
|
| 731 |
# ============================================================================
|
| 732 |
|
| 733 |
async def generate_welcome_closing_messages(lead_data: Dict[str, Any]) -> Dict[str, str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
"""Generate personalized welcome and closing messages using LLM based on candidate details"""
|
| 735 |
|
| 736 |
if not LANGCHAIN_AVAILABLE:
|
|
@@ -747,6 +751,7 @@ async def generate_welcome_closing_messages(lead_data: Dict[str, Any]) -> Dict[s
|
|
| 747 |
temperature=0.7,
|
| 748 |
openai_api_key=openai_api_key
|
| 749 |
)
|
|
|
|
| 750 |
|
| 751 |
# Extract relevant information from lead data
|
| 752 |
first_name = lead_data.get("first_name", "")
|
|
@@ -758,7 +763,7 @@ async def generate_welcome_closing_messages(lead_data: Dict[str, Any]) -> Dict[s
|
|
| 758 |
|
| 759 |
# Create a summary of the candidate's background
|
| 760 |
candidate_info = f"""
|
| 761 |
-
Name: {first_name}
|
| 762 |
Company: {company_name}
|
| 763 |
Location: {location}
|
| 764 |
Title: {title}
|
|
@@ -776,13 +781,7 @@ async def generate_welcome_closing_messages(lead_data: Dict[str, Any]) -> Dict[s
|
|
| 776 |
- Reference their specific background/company/role when possible
|
| 777 |
- Keep messages concise and engaging
|
| 778 |
- Make them feel valued and understood
|
| 779 |
-
|
| 780 |
-
Respond with ONLY a JSON object:
|
| 781 |
-
{
|
| 782 |
-
"welcome_message": "Personalized welcome message here",
|
| 783 |
-
"closing_message": "Personalized closing message here"
|
| 784 |
-
}
|
| 785 |
-
|
| 786 |
IMPORTANT: Respond with ONLY valid JSON. No additional text."""
|
| 787 |
|
| 788 |
prompt_template = ChatPromptTemplate.from_messages([
|
|
@@ -791,22 +790,13 @@ async def generate_welcome_closing_messages(lead_data: Dict[str, Any]) -> Dict[s
|
|
| 791 |
])
|
| 792 |
|
| 793 |
messages = prompt_template.format_messages(candidate_info=candidate_info)
|
| 794 |
-
response = await
|
| 795 |
|
| 796 |
try:
|
| 797 |
-
content = response.content.strip()
|
| 798 |
-
|
| 799 |
-
if content.startswith("```json"):
|
| 800 |
-
content = content[7:]
|
| 801 |
-
if content.endswith("```"):
|
| 802 |
-
content = content[:-3]
|
| 803 |
-
|
| 804 |
-
content = content.strip()
|
| 805 |
-
parsed_data = json.loads(content)
|
| 806 |
|
| 807 |
return {
|
| 808 |
-
"welcome_message":
|
| 809 |
-
"closing_message":
|
| 810 |
}
|
| 811 |
|
| 812 |
except Exception as parse_error:
|
|
|
|
| 731 |
# ============================================================================
|
| 732 |
|
| 733 |
async def generate_welcome_closing_messages(lead_data: Dict[str, Any]) -> Dict[str, str]:
|
| 734 |
+
class structure(BaseModel):
|
| 735 |
+
welcome_message: str = Field(description="Welcome message for the candidate")
|
| 736 |
+
closing_message: str = Field(description="Closing message for the candidate")
|
| 737 |
+
|
| 738 |
"""Generate personalized welcome and closing messages using LLM based on candidate details"""
|
| 739 |
|
| 740 |
if not LANGCHAIN_AVAILABLE:
|
|
|
|
| 751 |
temperature=0.7,
|
| 752 |
openai_api_key=openai_api_key
|
| 753 |
)
|
| 754 |
+
str_llm = llm.with_structured_output(structure)
|
| 755 |
|
| 756 |
# Extract relevant information from lead data
|
| 757 |
first_name = lead_data.get("first_name", "")
|
|
|
|
| 763 |
|
| 764 |
# Create a summary of the candidate's background
|
| 765 |
candidate_info = f"""
|
| 766 |
+
Name: {first_name}
|
| 767 |
Company: {company_name}
|
| 768 |
Location: {location}
|
| 769 |
Title: {title}
|
|
|
|
| 781 |
- Reference their specific background/company/role when possible
|
| 782 |
- Keep messages concise and engaging
|
| 783 |
- Make them feel valued and understood
|
| 784 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 785 |
IMPORTANT: Respond with ONLY valid JSON. No additional text."""
|
| 786 |
|
| 787 |
prompt_template = ChatPromptTemplate.from_messages([
|
|
|
|
| 790 |
])
|
| 791 |
|
| 792 |
messages = prompt_template.format_messages(candidate_info=candidate_info)
|
| 793 |
+
response = await str_llm.ainvoke(messages)
|
| 794 |
|
| 795 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 796 |
|
| 797 |
return {
|
| 798 |
+
"welcome_message": response.welcome_message,
|
| 799 |
+
"closing_message": response.closing_message
|
| 800 |
}
|
| 801 |
|
| 802 |
except Exception as parse_error:
|