Skip to content

Commit

Permalink
Added raw response if raw_yn is sent
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurvir committed Apr 16, 2024
1 parent bdd1323 commit c37e9bc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 8 additions & 3 deletions controllers/Agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
const db = new DBService();

async function getResponse(req, res) {
const { From, Body } = req.body
const { From, Body, raw_yn } = req.body

if(!From || !Body){
res.status(400).send("Bad Request")
Expand Down Expand Up @@ -38,14 +38,19 @@ async function getResponse(req, res) {
...session.text,
{ role: 'user', content: Body}
];
const response = await ai.getResponseFromOpenAI(messages)
const response = await ai.getResponseFromOpenAI(messages, raw_yn)

// prepare raw body if required
const responseBody = raw_yn ? response.raw : response.content;
delete response.raw;

messages.push(response);
session.text = messages; // Update session text (chat history)

// save session
await db.update_session(From, session)

res.send(response.content)
res.send(responseBody)
}

}
Expand Down
5 changes: 4 additions & 1 deletion services/AI.js
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ class AI {
return message;
}

async getResponseFromOpenAI(messages){
async getResponseFromOpenAI(messages, raw_yn=false){

const context = [
{role: 'assistant', content : "You are a travel planner ai agent that is capable of performing actions. "},
Expand Down Expand Up @@ -852,6 +852,9 @@ class AI {

// call again to get the response
responseMessage = await this.getResponseFromOpenAI(messages);
if(raw_yn) {
responseMessage.raw = response.data;
}
}
}
}
Expand Down

0 comments on commit c37e9bc

Please sign in to comment.