Changeset 38764


Ignore:
Timestamp:
2024-02-21T18:23:46+13:00 (4 months ago)
Author:
davidb
Message:

Stricter (more strongly type) code, which turned out to be needed to run as 'npm run build'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs3-installations/thewillow/trunk/sites/thewillow/dlcol-chatgpt/react-gui/pages/api/chat.ts

    r38756 r38764  
    88
    99
     10// For an (alternative) approach that looks like it retrieve the already created Assistant directly, see:
     11//    https://gist.github.com/drifterz13/0cbe93ced5dc7958d7841a29c1721d1c
     12
    1013import {
    11   //Configuration,
    12   OpenAI,
    13   //ChatCompletionRequestMessageRoleEnum
     14    //Configuration,
     15    OpenAI,
     16    //ChatCompletionRequestMessageRoleEnum
    1417} from "openai";
     18
     19
     20import { MessageContentText } from 'openai/resources/beta/threads/messages/messages'
     21
    1522
    1623import type { NextApiRequest, NextApiResponse } from "next";
     
    2128});
    2229
    23 const threadByUser = {}; // Store thread IDs by user
     30//const threadByUser = {}; // Store thread IDs by user
    2431
    25 async function assistantHandler(res,req) {
    26    
    27     const assistantIdToUse = process.env.ASSISTANT_ID;
     32const threadByUser = new Map<string, any>();
     33//const threadByUser = {};
     34
     35async function assistantHandler(
     36    req: NextApiRequest,
     37    res: NextApiResponse
     38) {   
     39    const assistantIdToUse = process.env.ASSISTANT_ID || "";
    2840    const modelToUse = "gpt-4-turbo-preview"; // Specify the model you want to use
    2941   
    3042    //const userId = req.body.userId; // You should include the user ID in the request
    31     const userId = "a-username";
     43    const userId = "test_username";
    3244   
    3345    // 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)) {
    3548    try {
    3649        const myThread = await openai.beta.threads.create();
    3750        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
    3953    } catch (error) {
    4054        console.error("Error creating thread:", error);
     
    5670    try {
    5771    const myThreadMessage = await openai.beta.threads.messages.create(
    58         threadByUser[userId], // Use the stored thread ID for this user
     72        threadByUser.get(userId), // Use the stored thread ID for this user
    5973        {
    6074        role: "user",
     
    6680    // Run the Assistant
    6781    const myRun = await openai.beta.threads.runs.create(
    68         threadByUser[userId], // Use the stored thread ID for this user
     82        threadByUser.get(userId), // Use the stored thread ID for this user
    6983        {
    7084        assistant_id: assistantIdToUse,
     
    8498        while (myRun.status !== "completed") {
    8599        keepRetrievingRun = await openai.beta.threads.runs.retrieve(
    86             threadByUser[userId], // Use the stored thread ID for this user
     100            threadByUser.get(userId), // Use the stored thread ID for this user
    87101            myRun.id
    88102        );
     
    103117       
    104118        const allMessages = await openai.beta.threads.messages.list(
    105         threadByUser[userId] // Use the stored thread ID for this user
     119        threadByUser.get(userId) // Use the stored thread ID for this user
    106120        );
    107121       
     
    115129        "------------------------------------------------------------ \n"
    116130        );
    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);
    120137
    121138        var returned_message = {
    122139        role: "assistant",
    123         content: allMessages.data[0].content[0].text.value
     140        content: assistant_response_message.text.value
    124141        };
    125142       
     
    170187    */
    171188   
    172     const assistant_message = await assistantHandler(res,req);
     189    const assistant_message = await assistantHandler(req,res);
    173190
    174191    console.log("**** (assistant) completion message: ");
Note: See TracChangeset for help on using the changeset viewer.