File size: 3,125 Bytes
abc7a1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b977b0
 
abc7a1c
82a2ab8
 
 
 
abc7a1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Module: answer_augmentation
Description: Uses an LLM to enhance real estate listing descriptions for better personalization.
"""

import json
import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate

class LlmAugmentation:
    """Handles LLM-based augmentation of real estate listings."""

    def __init__(self):
        """
        Initializes the LLM model using LangChain.
        """
        # Load API credentials securely (avoid hardcoding API keys)
        os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
        # os.environ["OPENAI_API_BASE"] = "https://openai.vocareum.com/v1"

        if not os.environ["OPENAI_API_KEY"]:
            raise ValueError("❌ OpenAI API key is missing. Set VOCAREUM_OPENAI_API_KEY in environment variables.")


        self.llm = ChatOpenAI(
            model_name="gpt-3.5-turbo",
            temperature=0.9
        )

        # Define the LLM prompt template
        self.llm_prompt = PromptTemplate.from_template(
            """
            Your role is to enhance real estate listing descriptions.
            You will receive {n_answers} real estate listings retrieved using similarity search. 
            Each listing matches the buyer’s preferences in terms of location, property features, amenities, and neighborhood.

            Your task:
            - **Enhance the property descriptions**: Subtly emphasize features of the property to make it appealing to possible buyers.
            - **Maintain factual integrity**: Do not add fictional information. Keep the facts unchanged while improving appeal.
            - **Do NOT** use bullet points or numered lists. Separate the listings by a new blank line.
            - **Recommended lenght**: Use between 80 to 120 words.

            Here are the listings:
            {listings}

            Now, rewrite the descriptions to highlight the aspects most relevant to the buyer.
            """
        )

    def generate_augmented_descriptions(self, listings):
        """
        Uses the LLM to augment property descriptions for better personalization.

        Args:
            listings (list): A list of retrieved real estate listings (dictionaries).

        Returns:
            list: A list of augmented listing descriptions.
        """
        try:
            if not listings:
                raise ValueError("Listings data is empty. Cannot generate augmented descriptions.")

            # Format listings into readable JSON
            listings_text = json.dumps(listings, indent=2)

            # Generate augmented descriptions using LangChain's LLM
            response = self.llm.invoke(
                self.llm_prompt.format(n_answers=len(listings), listings=listings_text)
            )

            # Ensure response is valid
            if not response or not hasattr(response, "content"):
                raise ValueError("Received invalid response from LLM.")

            return response.content.strip()

        except Exception as e:
            print(f"❌ Error generating augmented descriptions: {e}")
            return []