Skip to content

Commit 55f95a7

Browse files
committed
2 parents 8f8bdae + 9b642f6 commit 55f95a7

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

Diff for: backend/.env

-3
This file was deleted.

Diff for: backend/src/app.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import WorkerModel from "./models/worker";
99

1010
import ReviewModel from "./models/review";
1111

12-
import { getWorkerRecommendation } from "./util/recommendations";
12+
import { getWorkerRecommendation, findRecommendations } from "./util/recommendations";
1313
// import TradesPerson from "./models/tradespeople"; // Corrected import
1414

1515
const app = express();
@@ -55,21 +55,21 @@ app.get("/service_workers", async (req, res) => {
5555
// POST request for recommendation
5656
app.post('/recommend', async (req, res) => {
5757
try {
58-
let workers = await WorkerModel.find().exec();
58+
const workers = await WorkerModel.find().exec();
5959

60-
const { message, trade } = req.body;
61-
workers = workers.filter(worker => worker.trade === trade);
60+
const { message } = req.body;
6261

6362
const gpt_prompt = "Request: " + message +
6463
" Choose the top three workers from the list, returning only their worker_ids in the content of your message. " +
6564
"Worker List: " + JSON.stringify(workers) +
66-
"You will give the result of my query in the following format: " +
65+
"You will give the result of my query in this exact format: " +
6766
"Result: {cheapest_id: 1, second_cheapest_id: 2, third_cheapest_id: 3}";
6867

69-
const workerRecommendation = await getWorkerRecommendation(gpt_prompt);
68+
let workerRecommendation = await getWorkerRecommendation(gpt_prompt);
69+
// let result = workerRecommendation.message.content;
70+
let result = findRecommendations(workerRecommendation.message.content);
7071

71-
res.json({ recommendation: workerRecommendation });
72-
console.log(workerRecommendation.message.content);
72+
res.json({ recommendation: result });
7373
} catch (error: any) {
7474
console.error('Error:', error.message);
7575
res.status(500).json({ error: 'Internal Server Error' });

Diff for: backend/src/util/recommendations.ts

+33
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,36 @@ export async function getWorkerRecommendation(userMessage: string) {
2929
throw new Error("Failed to get worker recommendation from OpenAI.");
3030
}
3131
}
32+
33+
export type ResultObject = {
34+
cheapest_id: number;
35+
second_cheapest_id: number;
36+
third_cheapest_id: number;
37+
};
38+
39+
export function findRecommendations(response: String) {
40+
try {
41+
// Extract the part of the string between curly braces
42+
const match = response.match(/\{(.+?)\}/)?.[0];
43+
44+
if (match) {
45+
// Parse the extracted JSON string
46+
const resultObject = JSON.parse(match);
47+
48+
// Validate the structure of the parsed object
49+
if (
50+
typeof resultObject === 'object' &&
51+
'cheapest_id' in resultObject &&
52+
'second_cheapest_id' in resultObject &&
53+
'third_cheapest_id' in resultObject
54+
) {
55+
return resultObject;
56+
}
57+
}
58+
59+
return null;
60+
} catch (error:any) {
61+
console.error('Error parsing result string:', error.message);
62+
return null;
63+
}
64+
}

0 commit comments

Comments
 (0)