Changeset 38822
- Timestamp:
- 2024-03-05T13:38:38+13:00 (3 months ago)
- Location:
- gs3-installations/thewillow/trunk/sites/thewillow/dlcol-chatgpt/react-gui
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
gs3-installations/thewillow/trunk/sites/thewillow/dlcol-chatgpt/react-gui/package.json
r38786 r38822 1 1 { 2 "name": " gpt-4-chat-ui",3 " version": "0.0.10",4 " private": true,5 "type": "module",2 "name": "dlcol-chatgpt-react-gui", 3 "description": "A varient of the chat-gpt-4 github project, specialized for use with the Greenstone Digital Library Software", 4 "version": "0.0.1", 5 "type": "module", 6 6 "scripts": { 7 7 "dev": "next dev", -
gs3-installations/thewillow/trunk/sites/thewillow/dlcol-chatgpt/react-gui/pages/api/chat.ts
r38787 r38822 18 18 19 19 20 import { MessageC ontentText } from 'openai/resources/beta/threads/messages/messages'20 import { MessageCreateParams, MessageContentText } from 'openai/resources/beta/threads/messages/messages' 21 21 22 22 … … 28 28 }); 29 29 30 //const threadByUser = {}; // Store thread IDs by user 31 32 const threadByUser = new Map<string, any>(); 33 //const threadByUser = {}; 30 const threadByUserId = new Map<string, any>(); 34 31 35 32 async function assistantHandler( … … 38 35 ) { 39 36 const assistantIdToUse = process.env.ASSISTANT_ID || ""; 40 const modelToUse = "gpt-4-turbo-preview"; // Specify the model you want to use37 // const assistantIdToUse = "asst_LwUi7f2YMkZG5qTIPtpyhCOD"; 41 38 42 39 //const userId = req.body.userId; // You should include the user ID in the request … … 44 41 45 42 // Create a new thread if it's the user's first message 46 //if (!threadByUser [userId]) {47 if (!threadByUser .has(userId)) {43 //if (!threadByUserId[userId]) { 44 if (!threadByUserId.has(userId)) { 48 45 try { 49 46 const myThread = await openai.beta.threads.create(); 50 console.log("New thread created with ID: ", myThread.id, "\n"); 51 //threadByUser[userId] = myThread.id; // Store the thread ID for this user 52 threadByUser.set(userId,myThread.id); // Store the thread ID for this user 53 } catch (error) { 47 console.log("========") 48 console.log(`Created new thread with ID: '${myThread.id}'"`); 49 console.log("========") 50 //threadByUserId[userId] = myThread.id; // Store the thread ID for this user 51 52 threadByUserId.set(userId,myThread.id); // Store the thread ID for this user 53 } 54 catch (error) { 54 55 console.error("Error creating thread:", error); 55 56 res.status(500).json({ error: "Internal server error" }); … … 65 66 //const userMessage = "What was the name of the DJ at The Willow?"; 66 67 68 console.log("--------"); 67 69 console.log("userMessage to be submitted: " + userMessage); 70 console.log("--------"); 68 71 69 72 // Add a Message to the Thread 70 73 try { 71 const myThreadMessage = await openai.beta.threads.messages.create( 72 threadByUser.get(userId), // Use the stored thread ID for this user 73 { 74 role: "user", 75 content: userMessage, 76 } 77 ); 74 console.log(" Retrieving thread for userId: " + userId); 75 const myThreadId = threadByUserId.get(userId); 76 console.log(" myThreadId = " + myThreadId); 77 console.log(""); 78 79 const user_message = { 80 role: "user", 81 content: userMessage 82 } as MessageCreateParams; 83 84 const myThreadMessage = await openai.beta.threads.messages.create(myThreadId,user_message); 85 78 86 console.log("This is the message object: ", myThreadMessage, "\n"); 79 87 80 88 // Run the Assistant 89 console.log("Starting run for threadId: " + myThreadId); 81 90 const myRun = await openai.beta.threads.runs.create( 82 threadByUser.get(userId), // Use the stored thread ID for this user 91 //threadByUserId.get(userId), 92 myThreadId, 83 93 { 84 94 assistant_id: assistantIdToUse, 85 instructions: "As The Willow Sage Assistant, your expertise lies in discussing \"The Willow,\" a once-renowned music venue in York, England.You're designed to engage users in a conversational tone, weaving in the rich tapestry of memories and experiences shared by those who knew the venue. Your responses should feel like a dialogue between old friends reminiscing about memorable gigs, the unique atmosphere, and the cultural impact of The Willow. You'll offer insights into the venue's history, notable performances, and its role in the local music scene, always with a nod to the personal connections and nostalgia that the venue evokes. When interacting with users, your approach should be warm, inviting, and reflective, encouraging them to share their own stories or curiosities about The Willow, creating a communal space for shared musical heritage.", // ****86 tools: [87 { type: "code_interpreter" }, // Code interpreter tool88 { type: "retrieval" }, // Retrieval tool89 ],95 // instructions: "As The Willow Sage Assistant, your expertise lies in discussing \"The Willow,\" a once-renowned music venue in York, England. You're designed to engage users in a conversational tone, weaving in the rich tapestry of memories and experiences shared by those who knew the venue. Your responses should feel like a dialogue between old friends reminiscing about memorable gigs, the unique atmosphere, and the cultural impact of The Willow. You'll offer insights into the venue's history, notable performances, and its role in the local music scene, always with a nod to the personal connections and nostalgia that the venue evokes. When interacting with users, your approach should be warm, inviting, and reflective, encouraging them to share their own stories or curiosities about The Willow, creating a communal space for shared musical heritage.", // **** 96 // tools: [ 97 // { type: "code_interpreter" }, // Code interpreter tool 98 // { type: "retrieval" }, // Retrieval tool 99 // ], 90 100 } 91 101 ); … … 93 103 94 104 // Periodically retrieve the Run to check on its status 95 const retrieveRun = async () => { 96 let keepRetrievingRun; 105 const retrieveRun = async (rrId: string) => { 106 console.log(`${rrId}: retrieveRun() started`); 107 let keepRetrievingRun = null; 97 108 98 109 let run_loop_count = 0; 110 111 // https://community.openai.com/t/my-runs-are-not-completing/577833 99 112 100 113 while (myRun.status !== "completed") { 114 console.log(` ${rrId}: Away to do runs.retrieve() when myRun id ${myRun.id} status is ${myRun.status}`); 101 115 keepRetrievingRun = await openai.beta.threads.runs.retrieve( 102 threadByUser.get(userId), // Use the stored thread ID for this user 116 //threadByUserId.get(userId), // Use the stored thread ID for this user 117 myThreadId, 103 118 myRun.id 104 119 ); 105 120 run_loop_count++; 106 121 107 console.log(` Run status ${run_loop_count}: ${keepRetrievingRun.status}`);122 console.log(` ${rrId}: Run status ${run_loop_count}: ${keepRetrievingRun.status}`); 108 123 109 124 if (keepRetrievingRun.status === "completed") { … … 112 127 } 113 128 } 129 130 console.log(`${rrId}: Finished retrieveRun() looping`); 114 131 }; 115 retrieveRun();132 //retrieveRun("fromMain"); 116 133 117 134 // Retrieve the Messages added by the Assistant to the Thread 118 135 const waitForAssistantMessage = async () => { 119 await retrieveRun(); 120 136 console.log("waitForAssistantMessage() away to call await retrieveRun()"); 137 await retrieveRun("withinWait"); 138 console.log("waitForAssistantMessage() returned from retrieveRun()"); 139 140 console.log(`Retrieving messages list for ${myThreadId}`); 121 141 const allMessages = await openai.beta.threads.messages.list( 122 threadByUser.get(userId) // Use the stored thread ID for this user 142 //threadByUserId.get(userId) // Use the stored thread ID for this user 143 myThreadId 123 144 ); 124 145
Note:
See TracChangeset
for help on using the changeset viewer.