Skip to content

Commit

Permalink
Compability for server and local testing
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhang03 committed Jul 10, 2024
1 parent fffdf61 commit 70ec0ad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
7 changes: 3 additions & 4 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const OpenAI = require("openai");
const express = require("express");
const axios = require("axios");
const bodyParser = require("body-parser");
const cors = require("cors");
require("dotenv").config();
Expand Down Expand Up @@ -34,6 +33,6 @@ app.post("/api/chat", async (req, res) => {
}
});

// app.listen(port, () => {
// console.log(`Server is running on http://localhost:${port}`);
// });
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
25 changes: 18 additions & 7 deletions packages/plugin-chat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,24 @@ class ChatPlugin implements JsPsychPlugin<Info> {
// Call to backend, newMessage is the document item to print (optional because when chaining don't want them to display)
async fetchGPT(messages, newMessage?) {
try {
const response = await fetch("/api/chat", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ messages, ai_model: this.ai_model }), // Corrected JSON structure
});
var response;
if (window.location.href.includes("127.0.0.1")) {
response = await fetch("http://localhost:3000/api/chat", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ messages, ai_model: this.ai_model }), // Corrected JSON structure
});
} else {
response = await fetch("/api/chat", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ messages, ai_model: this.ai_model }), // Corrected JSON structure
});
}

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
Expand Down

0 comments on commit 70ec0ad

Please sign in to comment.