Skip to content

Commit 4b4d3c4

Browse files
committed
Shift AI call to collab service
1 parent 2c7cc26 commit 4b4d3c4

File tree

8 files changed

+60
-27
lines changed

8 files changed

+60
-27
lines changed

.env.sample

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ MATCHING_SVC_PORT=6969
2525
## Collab service variables
2626
COLLAB_SVC_PORT=3002
2727
COLLAB_SVC_DB_URI=
28+
OPENAI_API_KEY=
2829

2930
## Redis variables
3031
REDIS_PORT=6379
3132

3233
## Redisinsight variables
33-
REDIS_INSIGHT_PORT=5540
34-
35-
## OpenAI API key
36-
OPENAI_API_KEY=
34+
REDIS_INSIGHT_PORT=5540
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { sendAiMessage } from "../model/repository.js";
2+
3+
// send ai message
4+
export async function sendAiMessageController(req, res) {
5+
const { message } = req.body;
6+
if (!message) {
7+
return res.status(400).json({ error: "Message content is required" });
8+
}
9+
10+
const data = await sendAiMessage(message);
11+
const aiResponse =
12+
data.choices?.[0]?.message?.content || "No response from AI";
13+
14+
if (aiResponse) {
15+
res.status(200).json({ data });
16+
} else {
17+
res.status(500).json({ error: "Failed to retrieve AI response" });
18+
}
19+
}

collab-service/app/model/repository.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export async function newRoom(user1, user2, roomId, questionId) {
99
try {
1010
// Remove any existing rooms where either user1 or user2 is a participant
1111
await UsersSession.deleteMany({ users: { $in: [user1, user2] } });
12-
12+
1313
const newRoom = new UsersSession({
1414
users: [user1, user2],
1515
roomId: roomId,
@@ -35,7 +35,6 @@ export async function getRoomId(user) {
3535
}
3636
}
3737

38-
3938
export async function getAllRooms() {
4039
try {
4140
const rooms = await UsersSession.find({});
@@ -113,4 +112,25 @@ export async function getQuestionIdByRoomId(roomId) {
113112
console.error(`Error finding questionId for roomId ${roomId}:`, error);
114113
return null;
115114
}
116-
}
115+
}
116+
117+
export async function sendAiMessage(message) {
118+
try {
119+
console.log("sending to openai");
120+
const response = await fetch("https://api.openai.com/v1/chat/completions", {
121+
method: "POST",
122+
headers: {
123+
'Content-Type': "application/json",
124+
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
125+
},
126+
body: JSON.stringify({
127+
model: "gpt-3.5-turbo",
128+
messages: [{ role: "user", content: message }],
129+
}),
130+
});
131+
const data = await response.json();
132+
return data;
133+
} catch (error) {
134+
console.error("Error in sending AI message:", error);
135+
}
136+
}

collab-service/app/routes/collab-routes.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getRoomChatHistory,
77
getQuestionId
88
} from "../controller/collab-controller.js";
9+
import { sendAiMessageController } from "../controller/ai-controller.js";
910

1011
const router = express.Router();
1112

@@ -19,4 +20,6 @@ router.get("/chat-history/:roomId", getRoomChatHistory);
1920

2021
router.get("/rooms/:roomId/questionId", getQuestionId);
2122

23+
router.post("/send-ai-message", sendAiMessageController)
24+
2225
export default router;

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ services:
88
- QUESTION_SVC_PORT=$QUESTION_SVC_PORT
99
- MATCHING_SVC_PORT=$MATCHING_SVC_PORT
1010
- COLLAB_SVC_PORT=$COLLAB_SVC_PORT
11-
- OPENAI_API_KEY=$OPENAI_API_KEY
1211
ports:
1312
- $FRONTEND_PORT:$FRONTEND_PORT
1413
depends_on:
@@ -80,4 +79,5 @@ services:
8079
- $COLLAB_SVC_PORT:$COLLAB_SVC_PORT
8180
environment:
8281
- PORT=$COLLAB_SVC_PORT
83-
- DB_URI=$COLLAB_SVC_DB_URI
82+
- DB_URI=$COLLAB_SVC_DB_URI
83+
- OPENAI_API_KEY=$OPENAI_API_KEY

frontend/Dockerfile

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ ARG BASE_URI \
44
USER_SVC_PORT \
55
QUESTION_SVC_PORT \
66
MATCHING_SVC_PORT \
7-
COLLAB_SVC_PORT \
8-
OPENAI_API_KEY
7+
COLLAB_SVC_PORT
98
WORKDIR /app
109
COPY package.json .
1110
COPY yarn.lock .
@@ -14,9 +13,8 @@ ENV NEXT_PUBLIC_BASE_URI=$BASE_URI \
1413
NEXT_PUBLIC_USER_SVC_PORT=$USER_SVC_PORT \
1514
NEXT_PUBLIC_QUESTION_SVC_PORT=$QUESTION_SVC_PORT \
1615
NEXT_PUBLIC_MATCHING_SVC_PORT=$MATCHING_SVC_PORT \
17-
NEXT_PUBLIC_COLLAB_SVC_PORT=$COLLAB_SVC_PORT \
18-
NEXT_PUBLIC_OPENAI_API_KEY=$OPENAI_API_KEY
19-
16+
NEXT_PUBLIC_COLLAB_SVC_PORT=$COLLAB_SVC_PORT
17+
2018
# Production build stage
2119
FROM base AS build
2220
COPY . .

frontend/components/collab/chat.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ export default function Chat({ roomId }: { roomId: string }) {
9898
id: crypto.randomUUID(),
9999
userId: "ai",
100100
text:
101-
data.choices && data.choices[0]?.message?.content
102-
? data.choices[0].message.content
101+
data.data.choices && data.data.choices[0]?.message?.content
102+
? data.data.choices[0].message.content
103103
: "An error occurred. Please try again.",
104104
timestamp: new Date(),
105105
};
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1+
import { collabServiceUri } from "@/lib/api/api-uri";
2+
13
export const sendAiMessage = async (message: string) => {
2-
const response = await fetch('https://api.openai.com/v1/chat/completions', {
4+
console.log(`{collabServiceUri(window.location.hostname)}/send-ai-message`);
5+
console.log(message);
6+
const response = await fetch(`${collabServiceUri(window.location.hostname)}/collab/send-ai-message`, {
37
method: 'POST',
48
headers: {
59
'Content-Type': 'application/json',
6-
'Authorization': `Bearer ${process.env.NEXT_PUBLIC_OPENAI_API_KEY}`,
710
},
8-
body: JSON.stringify({
9-
model: 'gpt-3.5-turbo',
10-
messages: [
11-
{
12-
role: 'user',
13-
content: message,
14-
},
15-
],
16-
}),
11+
body: JSON.stringify({message: message}),
1712
});
1813
return response;
1914
}

0 commit comments

Comments
 (0)