Changeset 38822


Ignore:
Timestamp:
2024-03-05T13:38:38+13:00 (3 months ago)
Author:
davidb
Message:

Last set of changes before this code base shifts over to ext/dl-chatbot

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  
    11{
    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",
    66  "scripts": {
    77    "dev": "next dev",
  • gs3-installations/thewillow/trunk/sites/thewillow/dlcol-chatgpt/react-gui/pages/api/chat.ts

    r38787 r38822  
    1818
    1919
    20 import { MessageContentText } from 'openai/resources/beta/threads/messages/messages'
     20import { MessageCreateParams, MessageContentText } from 'openai/resources/beta/threads/messages/messages'
    2121
    2222
     
    2828});
    2929
    30 //const threadByUser = {}; // Store thread IDs by user
    31 
    32 const threadByUser = new Map<string, any>();
    33 //const threadByUser = {};
     30const threadByUserId = new Map<string, any>();
    3431
    3532async function assistantHandler(
     
    3835) {   
    3936    const assistantIdToUse = process.env.ASSISTANT_ID || "";
    40     const modelToUse = "gpt-4-turbo-preview"; // Specify the model you want to use
     37    // const assistantIdToUse = "asst_LwUi7f2YMkZG5qTIPtpyhCOD";
    4138   
    4239    //const userId = req.body.userId; // You should include the user ID in the request
     
    4441   
    4542    // 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)) {
    4845    try {
    4946        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) {
    5455        console.error("Error creating thread:", error);
    5556        res.status(500).json({ error: "Internal server error" });
     
    6566    //const userMessage = "What was the name of the DJ at The Willow?";
    6667
     68    console.log("--------");
    6769    console.log("userMessage to be submitted: " + userMessage);
     70    console.log("--------");
    6871   
    6972    // Add a Message to the Thread
    7073    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
    7886    console.log("This is the message object: ", myThreadMessage, "\n");
    7987   
    8088    // Run the Assistant
     89    console.log("Starting run for threadId: " + myThreadId);
    8190    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,
    8393        {
    8494        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 tool
    88             { type: "retrieval" },        // Retrieval tool
    89         ],
     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//      ],
    90100        }
    91101    );
     
    93103   
    94104    // 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;
    97108
    98109        let run_loop_count = 0;
     110
     111        // https://community.openai.com/t/my-runs-are-not-completing/577833
    99112       
    100113        while (myRun.status !== "completed") {
     114        console.log(`    ${rrId}: Away to do runs.retrieve() when myRun id ${myRun.id} status is ${myRun.status}`);
    101115        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,
    103118            myRun.id
    104119        );
    105120        run_loop_count++;
    106121       
    107         console.log(`Run status ${run_loop_count}: ${keepRetrievingRun.status}`);
     122        console.log(`    ${rrId}: Run status ${run_loop_count}: ${keepRetrievingRun.status}`);
    108123       
    109124        if (keepRetrievingRun.status === "completed") {
     
    112127        }
    113128        }
     129
     130        console.log(`${rrId}: Finished retrieveRun() looping`);
    114131    };
    115     retrieveRun();
     132    //retrieveRun("fromMain");
    116133   
    117134    // Retrieve the Messages added by the Assistant to the Thread
    118135    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}`);
    121141        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
    123144        );
    124145       
Note: See TracChangeset for help on using the changeset viewer.