File size: 5,356 Bytes
0b6ff77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import json
import os
from typing import Dict

import gradio as gr
from openai import OpenAI
from opensearchpy import OpenSearch

host = "localhost"
port = 9200
OPENSEARCH_ADMIN_PASSWORD = os.getenv("OPENSEARCH_ADMIN_PASSWORD", "yw7L5u9nLs3a")
auth = (
    "admin",
    OPENSEARCH_ADMIN_PASSWORD,
)  # For testing only. Don't store credentials in code.

# Create the client with SSL/TLS enabled, but hostname verification disabled.
os_client = OpenSearch(
    hosts=[{"host": host, "port": port}],
    http_compress=True,  # enables gzip compression for request bodies
    http_auth=auth,
    use_ssl=True,
    verify_certs=False,
    ssl_assert_hostname=False,
    ssl_show_warn=False,
)

all_props = json.load(open("all_properties.json"))
props_core = [
    {
        "property": prop["property"],
        "address": prop["address"],
        "school": prop["school"],
        "listPrice": prop["listPrice"],
    }
    for prop in all_props
]

client = OpenAI(
    # This is the default and can be omitted
    api_key=os.environ.get("OPENAI_API_KEY"),
)

REQUIREMENTS_KEYS = [
    "location",
    "budget",
    "house type",
    "layout",
]

requirements: Dict[str, str] = {}


def get_requirement_prompt():
    return f"Current collected requirements: {requirements}.\nPlease let me know your requirements: {[key for key in REQUIREMENTS_KEYS if key not in requirements.keys()]}"


def get_requirements(input_str):
    global requirements
    chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "system",
                "content": """
You are a real estate agent looking for properties for your clients. You are now extracting information to understand user's requests.
Requirements are:

* location. For example:
- City: Mountainview, San Jose, Dublin, etc
- Postal Code: 95134, etc
- Work or Regular Destination: work at Google; regular business flight
- School: a specific school name; specify a rating range for schools.

* budget. For example:
- around 1 million; no more than 1.5 million; between 800k to 1 million

* layout. For example:
- Bedroom: 3 bedrooms; no less than 2 bedrooms; single; married; 2 kids
- Bathroom: same as above

* house type (one or multiple choices). For example:
- condo, townhouse, single family

If user's input is a requirement, please provide the content in the format '{requirement_type}:{content}'. For example, 'location:Houston'. If multiple requirements are provided, separate them with |. For example, 'location:Houston|budget:1 million'.
If multiple requirements for the same type are provided, separate them with ;. For example, 'location:Houston;San Francisco'. DO NOT output multiple pairs with the same requirement type.
Otherwise please provide helpful response to the user
                """,
            },
            {
                "role": "user",
                "content": input_str,
            },
        ],
        model="gpt-4o-mini",
    )
    output = chat_completion.choices[0].message.content
    print(f"model output {output}")

    message_out = ""

    def set_requirement(output):
        nonlocal message_out
        if ":" not in output:
            return
        parts = output.split(":")
        req = parts[0].strip()
        content = output[len(req) + 1 :].strip()
        if req in REQUIREMENTS_KEYS:
            message_out = (
                message_out
                + f"\nThanks! Collected requirement: {req}\n{get_requirement_prompt()}"
            )
            requirements[req] = content

    if "|" in output:
        all_requirements = output.split("|")
        for req in all_requirements:
            set_requirement(req)
    else:
        set_requirement(output)

    return message_out.strip()


chat_history = []


def find_property(input_str):
    global requirements
    global chat_history

    chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "system",
                "content": f"You are a real estate agent looking for properties for your clients. Here are some properties that might be of interest to you: {json.dumps(props_core)}. Client is looking for properties that meet the following requirements: {requirements}",
            },
        ]
        + chat_history,
        model="gpt-4o-mini",
    )

    output = chat_completion.choices[0].message.content

    chat_history.append(
        {
            "role": "assistant",
            "content": output,
        }
    )

    return output


def process_chat_message(message, history):
    global chat_history
    output = ""
    if len(requirements) < len(REQUIREMENTS_KEYS):
        output = get_requirements(message)
        if len(requirements) == len(REQUIREMENTS_KEYS):
            output += "\n" + find_property(message)
    else:
        chat_history.append(
            {
                "role": "user",
                "content": message,
            }
        )
        output = find_property(message)
    return output


demo = gr.ChatInterface(
    fn=process_chat_message,
    chatbot=gr.Chatbot(value=[[None, get_requirement_prompt()]]),
    examples=[
        "I want a house near Houston",
        "I have two kids, what type of house would I need?",
        "I have a budget of 1 million dollars",
    ],
    title="Buyer Agent Bot",
)
demo.launch(share=True)