st3w4r / openai-partial-stream

Turn a stream of token into a parsable JSON object as soon as possible. Enable Streaming UI for AI app based on LLM.
https://partial.stream/
MIT License
91 stars 2 forks source link

How to use this with Assistants API #24

Open ArunBalajiR opened 2 months ago

ArunBalajiR commented 2 months ago

I'm trying to achieve the above in OpenAI Assistants API.

let assistantId = assistantDetails.assistantId;

    // Create a thread using the assistantId
    const thread = await openai.beta.threads.create();

    // Pass in the user question into the existing thread
    await openai.beta.threads.messages.create(thread.id, {
        role: "user",
        content: outputString,
    });

    // Create a run
    const run = await openai.beta.threads.runs.create(thread.id, {
        assistant_id: assistantId,
    });

    // Imediately fetch run-status, which will be "in_progress"
    let runStatus = await openai.beta.threads.runs.retrieve(
        thread.id,
        run.id
    );

    // Get the last assistant message from the messages array
    const messages = await openai.beta.threads.messages.list(thread.id);

    // Find the last message for the current run
    const lastMessageForRun = messages.data
        .filter(
            (message) =>
                message.run_id === run.id && message.role === "assistant"
        )
        .pop();

    let assistanceResponse = lastMessageForRun.content[0].text.value;
st3w4r commented 2 months ago

Hi @ArunBalajiR, thanks for using the lib. Could you describe in more detail what you've tried and which part isn't working?