Skip to content

Commit b91f482

Browse files
committed
1.Tasks Accomplished:
GPT-Hanlder Added
1 parent 4e695a7 commit b91f482

File tree

6 files changed

+55
-6
lines changed

6 files changed

+55
-6
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"express": "^4.18.2",
2929
"fs-extra": "^11.1.1",
3030
"morgan": "^1.10.0",
31+
"openai": "^3.2.1",
3132
"twilio": "^4.11.2",
3233
"web3": "^1.10.0",
3334
"web3-utils": "^1.10.0"

src/handlers/callHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import fs from "fs-extra";
44
import axios from "axios";
55
import Web3 from "web3";
66
import { AbiItem } from "web3-utils";
7-
import { Client } from "twilio/lib/twiml/VoiceResponse";
87

98
const base_url = "https://api.assemblyai.com/v2";
109
// Assembly AI headers to be added
@@ -46,6 +45,7 @@ export const handleRecording = async (
4645
const transcriptionResult = pollingResponse.data;
4746

4847
if (transcriptionResult === "completed") {
48+
// Twilio code to be added
4949
} else if (transcriptionResult.status === "error") {
5050
throw new Error(`Transcription failed: ${transcriptionResult.error}`);
5151
} else {

src/handlers/gptHandler.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Configuration, OpenAIApi } from "openai";
2+
import { Request, Response } from "express";
3+
import dotenv from "dotenv";
4+
dotenv.config();
5+
6+
const API_KEY: string = process.env.OPENAI_API_KEY || "";
7+
8+
const configuration = new Configuration({
9+
apiKey: API_KEY,
10+
});
11+
12+
const openai = new OpenAIApi(configuration);
13+
14+
export const sendReq = async (req: Request, res: Response) => {
15+
const { transcribed_text }: { transcribed_text: string } = req.body;
16+
17+
if (!transcribed_text) {
18+
res.status(400).json({ error: "Transcribed text is required" });
19+
return;
20+
}
21+
22+
console.log("Sending request to GPT-3");
23+
24+
const model_engine = "text-davinci-003";
25+
const temperature = 0.5;
26+
const max_tokens = 300;
27+
28+
const prompt = `What is the emergency type, location, and priority of the following message. Your response must be in the following JSON format: {name, location, emergency_type, priority, reply_msg}. Available emergency_types=['Ambulance', 'NSG', 'Police', 'Rescue team', 'Fire brigade', 'Forest ranger']. Available priority=['high', 'medium', 'low']. Prompt: ${transcribed_text}`;
29+
30+
try {
31+
const response = await openai.createCompletion({
32+
model: model_engine,
33+
prompt,
34+
temperature,
35+
max_tokens,
36+
});
37+
38+
// const completionText = response.choices[0].text;
39+
const completionText = response.data.choices[0].text;
40+
41+
res.json({ result: completionText });
42+
} catch (error) {
43+
console.error("OpenAI API request failed:", error);
44+
res.status(500).json({ error: "Failed to process the request" });
45+
}
46+
};

src/handlers/testHandler.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Download the helper library from https://www.twilio.com/docs/node/install
22
// Find your Account SID and Auth Token at twilio.com/console
33
// and set the environment variables. See http://twil.io/secure
4-
const accountSid = process.env.TWILIO_ACCOUNT_SID;
5-
const authToken = process.env.TWILIO_AUTH_TOKEN;
4+
import dotenv from "dotenv";
5+
dotenv.config();
6+
const accountSid = process.env.TWILIO_ACCOUNT_SID!;
7+
const authToken = process.env.TWILIO_AUTH_TOKEN!;
68

79
const client = require("twilio")(accountSid, authToken);
810

@@ -14,5 +16,5 @@ export const init = async () => {
1416
from: "+918580732070",
1517
})
1618
.then((call: any) => console.log(call.sid, call))
17-
.catch((err: any) => console.log(err));
19+
.catch((err: any) => console.log(accountSid));
1820
};

src/routes/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import express from "express";
22
import CallRouter from "./call";
3+
import { sendReq } from "../handlers/gptHandler";
34

45
const router = express.Router();
56

67
router.use("/call", CallRouter);
8+
router.post("/gpt", sendReq);
79

810
export default router;

src/server.ts

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import dotenv from "dotenv";
55

66
dotenv.config();
77

8-
console.log(process.env.TWILIO_ACCOUNT_SID);
9-
108
const app = express();
119
const port = 3000;
1210

0 commit comments

Comments
 (0)