Skip to content

Commit

Permalink
added history creation with api
Browse files Browse the repository at this point in the history
  • Loading branch information
TottySnowman committed Oct 29, 2023
1 parent d251984 commit 1adf910
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
72 changes: 72 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Expand Up @@ -12,6 +12,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.6.0",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
Expand Down
31 changes: 24 additions & 7 deletions backend/src/controllers/index.ts
Expand Up @@ -13,6 +13,7 @@ import {
createdResponse,
docs,
} from "../types/conversation";
import axios from "axios";

dotenv.config();

Expand Down Expand Up @@ -85,6 +86,7 @@ app.post("/search", async (req: Request, res: Response) => {
const { userID, historyID } = req.query;
const data = req.body;

console.log({ data });
const convo = req.body;
const prompt = "Prompt from MML";
const results: docs[] = [];
Expand All @@ -94,9 +96,17 @@ app.post("/search", async (req: Request, res: Response) => {
const session = driver.session();

if (!historyID) {
const { response, statusCode, hID} = await createHistory(Number(userID), data[0].question);
const { response, statusCode, hID } = await createHistory(
Number(userID),
data[0].question
);

if (statusCode === 200) {
const axiosResp = await axios.get(
`http://185.119.87.85:8001/api/questions/get_answer?question=${data[0].question}`
);


const createdHistoryConversationRel = await session.run(
"MATCH (h:History) WHERE ID(h) = $hID CREATE (c:Conversation {createdAt: localdatetime()}) SET c += $conversation CREATE (h)-[:HAS_CONVERSATION]->(c)",
{ conversation: data, hID: Number(hID) }
Expand All @@ -120,18 +130,25 @@ async function createHistory(

const session = driver.session();

const response = await fetch(
"http://185.119.87.85:8000/api/questions/get_title?question=" + question
const axiosResponse = await axios.get(
`http://185.119.87.85:8001/api/questions/get_title?question=${question}`
);
if (!response.ok) {
console.log(axiosResponse);

if (axiosResponse.status !== 200) {
const response = {
statusCode: 500,
response: "Failed to fetch title",
};
return response;
}
const responseData = axiosResponse.data;
const allTitles = responseData.title
.split("\n")
.filter((step: any) => step.trim() !== "");

let historyData = {
title: "Test",
title: allTitles[0],
};
try {
const createdUserHistoryRel = await session.run(
Expand All @@ -141,7 +158,7 @@ async function createHistory(
const historyID: number = createdUserHistoryRel.records[0].get("nodeId");
const response = {
statusCode: 200,
response: "Successfully created conversation",
response: "Successfully created History",
};
return {
response: response,
Expand All @@ -151,7 +168,7 @@ async function createHistory(
} catch (error) {
const response = {
statusCode: 500,
response: "Successfully created conversation",
response: "Error creating History",
};

return {
Expand Down

0 comments on commit 1adf910

Please sign in to comment.