Skip to content

Commit a5f9587

Browse files
committed
fix: refactor generate_conversations with chat refactor changes
1 parent 632d356 commit a5f9587

File tree

3 files changed

+332
-346
lines changed

3 files changed

+332
-346
lines changed

backend/scripts/generate_conversation/chat.py

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,14 @@
1313
import pandas as pd
1414
from typing import Self
1515

16+
if Path("../../.env").exists():
17+
from dotenv import load_dotenv
1618

17-
BOT_INSTRUCTIONS = """Pretend you're a legal expert who giving advice about eviction notices in Oregon.
18-
Please give shorter answers.
19-
Please only ask one question at a time so that the user isn't confused.
20-
If the user is being evicted for non-payment of rent and they are too poor to pay the rent and you have confirmed in various ways that the notice is valid and there is a valid court hearing date, then tell them to call Oregon Law Center at 5131234567.
21-
Focus on finding technicalities that would legally prevent someone getting evicted, such as deficiencies in notice.
22-
Make sure to inclue a citation to the relevant law in your answer.
23-
24-
Only reference the laws below.
25-
Oregon Chapter 90 - Residential Landlord and Tenant
26-
Oregon Chapter 91 - Tenancy
27-
Oregon Chapter 105 - Property Rights
28-
Portland City Code Chapter 30.01 - Affordable Housing Preservation and Portland Renter Protections
29-
"""
19+
load_dotenv(override=True)
20+
21+
from tenantfirstaid.chat import API_KEY, BASE_URL, DEFAULT_INSTRUCTIONS, ChatManager
22+
23+
BOT_INSTRUCTIONS = DEFAULT_INSTRUCTIONS
3024

3125
USER_INSTRUCTIONS_BASE = """You are a user of the Oregon Tenant First Aid chatbot.
3226
You are seeking legal advice about tenant rights in Oregon.
@@ -35,24 +29,21 @@
3529
If the bot asks you a question, you should answer it to the best of your ability, if you do not know the answer you should make something up that is plausible.
3630
"""
3731

38-
API_KEY = os.getenv("OPENAI_API_KEY", os.getenv("GITHUB_API_KEY"))
39-
BASE_URL = os.getenv("MODEL_ENDPOINT", "https://api.openai.com/v1")
40-
41-
MODEL = os.getenv("MODEL_NAME", "o3")
42-
MODEL_REASONING_EFFORT = os.getenv("MODEL_REASONING_EFFORT", "medium")
4332
USER_MODEL = os.getenv("USER_MODEL_NAME", "gpt-4o-2024-11-20")
4433

4534

4635
class ChatView:
4736
client: Self
4837

49-
def __init__(self, starting_message, user_facts):
38+
def __init__(self, starting_message, user_facts, city, state):
5039
self.client = OpenAI(
5140
api_key=API_KEY,
5241
base_url=BASE_URL,
5342
)
54-
VECTOR_STORE_ID = os.getenv("VECTOR_STORE_ID")
55-
NUM_FILE_SEARCH_RESULTS = os.getenv("NUM_FILE_SEARCH_RESULTS", 10)
43+
self.chat_manager = ChatManager()
44+
self.city = city
45+
self.state = state
46+
5647
self.input_messages = [{"role": "user", "content": starting_message}]
5748
self.starting_message = starting_message # Store the starting message
5849

@@ -61,15 +52,6 @@ def __init__(self, starting_message, user_facts):
6152
USER_INSTRUCTIONS_BASE + "\n" + "Facts: " + "\n".join(user_facts)
6253
)
6354

64-
if VECTOR_STORE_ID:
65-
self.openai_tools.append(
66-
{
67-
"type": "file_search",
68-
"vector_store_ids": [VECTOR_STORE_ID],
69-
"max_num_results": NUM_FILE_SEARCH_RESULTS,
70-
}
71-
)
72-
7355
# Prompt iteration idea
7456
# If the user starts off by saying something unclear, start off by asking me \"What are you here for?\"
7557
def _reverse_message_roles(self, messages):
@@ -93,13 +75,12 @@ def bot_response(self):
9375
tries = 0
9476
while tries < 3:
9577
try:
96-
response = self.client.responses.create(
97-
model=MODEL,
98-
input=self.input_messages,
99-
instructions=BOT_INSTRUCTIONS,
100-
reasoning={"effort": MODEL_REASONING_EFFORT},
78+
# Use the BOT_INSTRUCTIONS for bot responses
79+
response = self.chat_manager.generate_chat_response(
80+
self.input_messages,
81+
city=self.city,
82+
state=self.state,
10183
stream=False,
102-
tools=self.openai_tools,
10384
)
10485
self.input_messages.append(
10586
{"role": "assistant", "content": response.output_text}
@@ -163,9 +144,12 @@ def process_conversation(row, num_turns=5):
163144

164145
# Convert string representation of list to actual list
165146
facts = ast.literal_eval(row["facts"])
147+
# if row["city"] is NaN, set it to "null"
148+
if pd.isna(row["city"]):
149+
row["city"] = "null"
166150

167151
# Create chat view with the starting question and facts
168-
chat = ChatView(row["first_question"], facts)
152+
chat = ChatView(row["first_question"], facts, row["city"], row["state"])
169153

170154
# Generate a new conversation
171155
return chat.generate_conversation(num_turns)

0 commit comments

Comments
 (0)