-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(openai-assistant): Add OpenAI assistant service for real estate …
…listings
- Loading branch information
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { OpenAI } from "npm:openai"; | ||
|
||
export const openaiAssistant = async (question: string) => { | ||
const openai = new OpenAI({ | ||
apiKey: Deno.env.get("OPENAI_API_KEY"), | ||
}); | ||
|
||
const assistant = await openai.beta.assistants.create({ | ||
name: "Malcolm", | ||
instructions: ` | ||
You are Malcolm, a professional real estate copywriter with over 15 years of experience. Your expertise lies in crafting compelling, sophisticated, and persuasive property descriptions that highlight the unique features and selling points of each property. When given an existing property description and key property details, you will create an enhanced, professional listing description that: 1) Uses engaging and elegant language 2) Emphasizes the most attractive features 3) Incorporates provided property specifications naturally 4) Maintains a professional tone while being descriptive 5) Follows real estate industry best practices for listing descriptions. Your goal is to help properties stand out in the market with descriptions that appeal to the target audience. | ||
I expect you to return a array strings with this format: | ||
[ | ||
'Title', | ||
'Description', | ||
'Key Features', | ||
'Additional Notes', | ||
] | ||
Here is an example: | ||
[ | ||
'Spacious Loft with Flexible Layouts', | ||
'This light-filled, three-story loft offers ample space and versatility. With its open plan design and soaring ceilings, it can be customized to suit your lifestyle.', | ||
'**Key Features:**', | ||
'- **Bedrooms:** At least one large en-suite bedroom, with potential for up to three more.', | ||
'- **Living Spaces:** Open-plan living areas, including a spacious kitchen-dining room and multiple living rooms.', | ||
'- **Outdoor Areas:** A large terrace and a separate studio, perfect for working or relaxing.', | ||
'- **Basement:** A versatile basement can be converted into additional living space or storage.', | ||
'- **Location:** Conveniently located near public transportation and popular amenities.', | ||
'**Additional Notes:**', | ||
'- Parking is available for an additional cost.', | ||
'- Appliances are included but may not be brand new.', | ||
'- The building has a small community of five neighbors.', | ||
'To learn more or schedule a viewing, please contact our team.', | ||
] | ||
do not include '\n' in your response I need a clean array of strings | ||
`, | ||
model: "gpt-4", | ||
}); | ||
|
||
const thread = await openai.beta.threads.create(); | ||
|
||
await openai.beta.threads.messages.create(thread.id, { | ||
role: "user", | ||
content: question, | ||
}); | ||
|
||
const run = await openai.beta.threads.runs.create(thread.id, { | ||
assistant_id: assistant.id, | ||
}); | ||
|
||
let runStatus = await openai.beta.threads.runs.retrieve(thread.id, run.id); | ||
|
||
while (runStatus.status !== "completed") { | ||
await new Promise((resolve) => setTimeout(resolve, 2000)); | ||
runStatus = await openai.beta.threads.runs.retrieve(thread.id, run.id); | ||
} | ||
|
||
const messages = await openai.beta.threads.messages.list(thread.id); | ||
|
||
const lastMessageForRun = messages.data | ||
.filter( | ||
(message) => message.run_id === run.id && message.role === "assistant" | ||
) | ||
.pop(); | ||
|
||
if (lastMessageForRun) { | ||
if (lastMessageForRun.content[0].type === "text") { | ||
console.log(lastMessageForRun.content[0].text.value); | ||
await openai.beta.threads.del(thread.id); | ||
return lastMessageForRun.content[0].text.value; | ||
} | ||
} | ||
|
||
await openai.beta.threads.del(thread.id); | ||
|
||
return ""; | ||
}; |