How can I create dynamic instructions for the assistants?

I want to use dynamic variables in the instructions of each execution, can you help me?

I am trying to do it with one instruction, but if I want to join the thread, it only executes twice and then does not execute the run instruction.

Does anyone know how I can achieve this?

Welcome to the community forum.

Would you please share the code that you used to try that?

1 Like

do you want to totally change the instructions in the next run? or you just want to append to the instructions? you can do both. use the following properties when creating run:

2 Likes

Of course

This method is for when I have a thread
private async createStreamRun(
threadId: string,
assistantId: string,
infoRelations: any,
) {
const streamRun = await this.client.beta.threads.runs.create(threadId, {
assistant_id: assistantId,
stream: true,
instructions: instructions,
});

return streamRun;

}

This method is for when there is no thread and it is the first message

private async createAndRunStream(
assistantId: string,
message: string,
metadata: AssistantMetadata,
infoRelations: any,
): Promise<Stream<OpenAI.Beta.Assistants.AssistantStreamEvent>> {
const streamRun = await this.client.beta.threads.createAndRun({
assistant_id: assistantId,
thread: {
messages: [{ role: AIRole.USER, content: message }],
},
stream: true,
metadata: metadata,
instructions: instructions,
});

return streamRun;

}

What I want is for the Run instructions to be executed in each message Is there a way to do this?

Create dynamic instructions by adapting to context with NLP, using flexible templates, leveraging machine learning, gathering feedback, personalizing content, and continuously refining through testing.

when you add new message, you call run. for each run, you can change the instruction or append to the instruction using the properties i wrote beforehand.

to illustrate:

  • assistant main instruction: you are a cat
  • user: what is your name?
  • first run, additional instruction: your name is garfield
  • assistant: my name is garfield, meow
  • user: are you a cat?
  • second run, instruction: you are a dog
  • assistant: no, i am a dog

I understand and when a message is related to a thread?

what do you mean? all messages are related to a thread.

When adding additional_instructions, they are not being executed on all messages; they only run for the first two and then stop.

you need to keep the additional_instructions when calling runs. otherwise, it will fall back to just the main instructions. the effect of using instructions and additional_instructions when calling run is only for that run. it will not persist to the next run.

Best way is to use additional instructions in the API.

We’re storing what we refer to as “AI functions” in our RAG database, implying that when users says for instance “Send email”, it will match towards a “Send email” RAG record, providing context instructions to the LLM, resulting in that we can arguably dynamically “change” instructions we’re sending based upon the prompting of the user.

The code is proprietary, but you can probably figure out its rough mechanics from the following article.