Changeset 38764
- Timestamp:
- 2024-02-21T18:23:46+13:00 (4 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gs3-installations/thewillow/trunk/sites/thewillow/dlcol-chatgpt/react-gui/pages/api/chat.ts
r38756 r38764 8 8 9 9 10 // For an (alternative) approach that looks like it retrieve the already created Assistant directly, see: 11 // https://gist.github.com/drifterz13/0cbe93ced5dc7958d7841a29c1721d1c 12 10 13 import { 11 //Configuration,12 OpenAI,13 //ChatCompletionRequestMessageRoleEnum14 //Configuration, 15 OpenAI, 16 //ChatCompletionRequestMessageRoleEnum 14 17 } from "openai"; 18 19 20 import { MessageContentText } from 'openai/resources/beta/threads/messages/messages' 21 15 22 16 23 import type { NextApiRequest, NextApiResponse } from "next"; … … 21 28 }); 22 29 23 const threadByUser = {}; // Store thread IDs by user30 //const threadByUser = {}; // Store thread IDs by user 24 31 25 async function assistantHandler(res,req) { 26 27 const assistantIdToUse = process.env.ASSISTANT_ID; 32 const threadByUser = new Map<string, any>(); 33 //const threadByUser = {}; 34 35 async function assistantHandler( 36 req: NextApiRequest, 37 res: NextApiResponse 38 ) { 39 const assistantIdToUse = process.env.ASSISTANT_ID || ""; 28 40 const modelToUse = "gpt-4-turbo-preview"; // Specify the model you want to use 29 41 30 42 //const userId = req.body.userId; // You should include the user ID in the request 31 const userId = " a-username";43 const userId = "test_username"; 32 44 33 45 // Create a new thread if it's the user's first message 34 if (!threadByUser[userId]) { 46 //if (!threadByUser[userId]) { 47 if (!threadByUser.has(userId)) { 35 48 try { 36 49 const myThread = await openai.beta.threads.create(); 37 50 console.log("New thread created with ID: ", myThread.id, "\n"); 38 threadByUser[userId] = myThread.id; // Store the thread ID for this user 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 39 53 } catch (error) { 40 54 console.error("Error creating thread:", error); … … 56 70 try { 57 71 const myThreadMessage = await openai.beta.threads.messages.create( 58 threadByUser [userId], // Use the stored thread ID for this user72 threadByUser.get(userId), // Use the stored thread ID for this user 59 73 { 60 74 role: "user", … … 66 80 // Run the Assistant 67 81 const myRun = await openai.beta.threads.runs.create( 68 threadByUser [userId], // Use the stored thread ID for this user82 threadByUser.get(userId), // Use the stored thread ID for this user 69 83 { 70 84 assistant_id: assistantIdToUse, … … 84 98 while (myRun.status !== "completed") { 85 99 keepRetrievingRun = await openai.beta.threads.runs.retrieve( 86 threadByUser [userId], // Use the stored thread ID for this user100 threadByUser.get(userId), // Use the stored thread ID for this user 87 101 myRun.id 88 102 ); … … 103 117 104 118 const allMessages = await openai.beta.threads.messages.list( 105 threadByUser [userId]// Use the stored thread ID for this user119 threadByUser.get(userId) // Use the stored thread ID for this user 106 120 ); 107 121 … … 115 129 "------------------------------------------------------------ \n" 116 130 ); 117 118 console.log("User: ", myThreadMessage.content[0].text.value); 119 console.log("Assistant: ", allMessages.data[0].content[0].text.value); 131 132 const user_request_message: MessageContentText = (myThreadMessage.content[0]) as MessageContentText; 133 const assistant_response_message = allMessages.data[0].content[0] as MessageContentText; 134 135 console.log("User: ", user_request_message.text.value); 136 console.log("Assistant: ", assistant_response_message.text.value); 120 137 121 138 var returned_message = { 122 139 role: "assistant", 123 content: a llMessages.data[0].content[0].text.value140 content: assistant_response_message.text.value 124 141 }; 125 142 … … 170 187 */ 171 188 172 const assistant_message = await assistantHandler(re s,req);189 const assistant_message = await assistantHandler(req,res); 173 190 174 191 console.log("**** (assistant) completion message: ");
Note:
See TracChangeset
for help on using the changeset viewer.