Assistant instructions are discarded

Hi everyone!

I am having trouble with the assistants. It works fine in playground and gives answers as expected but when I access it through my code it loses it’s “personality”. E.g I made a test assistant that only had the instructions that it’s name is bob. When asked in playground who it is it replies correctly bob but with my code it gives a generic reply like I am an AI assistant here to help … I have tripple checked the id. It is like this with all my assistants.

The code I use is just standard python and looks roughly like this:

client = OpenAI(api_key=key, default_headers={"OpenAI-Beta": "assistants=v2"})
assistant_id = os.environ.get("ASSISTANT_ID")
thread = client.beta.threads.create()
client.beta.threads.messages.create(
		thread_id=thread_id,
		role="user",
		content="Who are you?",
	)
run = client.beta.threads.runs.create_and_poll(
		thread_id=thread_id,
		assistant_id=assistant_id,
		instructions=instructions
	)
all_messages = client.beta.threads.messages.list(
		thread_id=thread_id
	)
	response = all_messages.data[0].content[0].text.value

Thanks for any help <3 It might be something stupid. I am going crazy with this

1 Like

assistant_id = os.environ.get(“ASSISTANT_ID”)

is the culprit

2 Likes

Here’s a step-by-step guide to troubleshoot and correct the issue:

  1. Verify API Headers and Versions:
    Make sure the correct API version header is being used. You’re correctly using assistants=v2.

  2. Check Assistant Creation:
    Ensure that the assistant is created with the correct instructions and those instructions are referenced properly. Here is a sample code snippet for creating an assistant:

    from openai import OpenAI
    import os
    
    client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
    
    assistant = client.beta.assistants.create(
        name="Test Assistant",
        instructions="Your name is Bob. You should always introduce yourself as Bob.",
        model="gpt-4-turbo"
    )
    
  3. Correctly Create and Use Threads and Runs:
    Ensure that the thread and run are created correctly and that the assistant’s instructions are applied. Here’s an updated version of your code:

    from openai import OpenAI
    import os
    import time
    
    client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"), default_headers={"OpenAI-Beta": "assistants=v2"})
    assistant_id = os.environ.get("ASSISTANT_ID") 
    # assistant_id = "ASSISTANT_ID"   # or like this, maybe you didn't set it well, test both versions
    
    
    # Create a new thread
    thread = client.beta.threads.create()
    
    # Add a user message to the thread
    client.beta.threads.messages.create(
        thread_id=thread.id,
        role="user",
        content="Who are you?"
    )
    
    # Create and poll a run to get the assistant's response
    run = client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant_id
    )
    
    def wait_on_run(run, thread):
        while run.status == "queued" or run.status == "in_progress":
            run = client.beta.threads.runs.retrieve(
                thread_id=thread.id,
                run_id=run.id,
            )
            time.sleep(0.5)
        return run
    
    run = wait_on_run(run, thread)
    
    # Retrieve the messages from the thread
    messages = client.beta.threads.messages.list(thread_id=thread.id)
    response = messages.data[-1].content[0].text.value
    
    print(response)  # Should print: "My name is Bob."
    
  4. Check Assistant ID and Instructions:
    Ensure the assistant ID is correctly set in the environment variable ASSISTANT_ID and that the instructions are being referenced correctly in the API calls.

  5. Debugging Steps:

    • Verify that the assistant is correctly created by checking the assistant’s details.
    • Print the assistant’s ID to ensure it matches the one used in the environment variable.
    • Check the content of the messages retrieved to see if the assistant’s instructions are reflected in the responses.

By following these steps, you should be able to ensure that the assistant retains its personality as defined in the instructions when accessed through your code. If issues persist, further debugging of each step in the process will be necessary to pinpoint where the configuration might be going wrong.

I hope this helps! Let me know if you need any further assistance.


2 Likes

Thanks for the detailed answer! I sadly have already tried all these steps.
Here is some more information on what I have tired so far:

  • I know the correct assistant is referenced because function calling works
  • I know the instructions are setup correctly because in playground I get the expected results
  • I have tried created another assistant (both in code and with playground)
  • When asking about a file connected to the assistant I got the following response (shortened): It appears there is a technical issue preventing me from accessing the files to provide information on (topic of file).
1 Like

I am not sure how you set the assistant id, try to copy paste and put it like this, maybe you didn’t set the assistant well.

assistant_id = "asst_qdEud4WDBfghO08SbpVI2IYI"

Right now without more information or some screenshots to see the errors, I cant help.

1 Like

The assistant API is in beta, there are no guarantees that it will work how you expect outside of the playground.

1 Like
  • I think I can help with this. Assistants work great outside (I have conducted many tests).
  • I will make my code open-source next week, and I will create tutorials too.
  • I hope this will make everything simpler for building Assistants with APIs and working with data in the Cloud or on a Personal PC.
1 Like

Check platform.openai.com. You should be able to see the details of every thread. It will show you what the id of the assistant used and also the instructions etc. Found in the left side menu. You might have to enable the view in settings.

1 Like

setting instructions parameter at the run level will replace the original instruction. in the basic create run method, there are 2 parameters that pertains to instructions: instructions and additional_instructions. in your case, you need to use additional_instructions if you want to append something to the main instruction at the run level.

however, you are using a helper method, create_and_poll, and looking at the documentation, i cannot see if there is additional_instructions parameter. perhaps there is. there should be. anyway, if there is none, what you can do is first fetch your assistant, get the instruction and during run, append your new instruction:

if no additional_instructions param

run = client.beta.threads.runs.create_and_poll(
		thread_id=thread_id,
		assistant_id=assistant_id,
		instructions=main_instructions + instructions
	)

where main_instructions is from

const myAssistant = await openai.beta.assistants.retrieve(
    "ASSISTANT_ID"
  );

const main_instructions = myAssistant.instructions
1 Like

The framework here, open-source, you can see functions or create your functions here, you have examples a Flask App, Multi Agent System and RAG for each agent in Multi Agent System

I got the instructions parameter to kind of work by putting it in the run creation:

run = client.beta.threads.runs.create(
    thread_id=thread.id,
    assistant_id=assistant.id,
    instructions="You are a helpful assistant whose answers are always short and who obeys the following rules.\
        1. Always ask for clarification if my request has more than one meaning.\
        2. Whenever the quantity of an item is _below_ the Reorder Point, put 'YES' in the Reorder? field for the item.\
        3. When you change a record, do this:\
        3.a Show all of the record in this format: before: <record before change>\
        3.b Change the record.\
        3.c Show all of the new record in this format: after: <record after change>",
)

The gpt-4o-mini obeys the spirit of the 'change the record', but not the letter, which is a problem because I want to use the responses to check if the changes have actually been done. It seems to me that there is another problem with this solution: every time I send a prompt to the API I'm adding a significant chunk to the context stream. Also there is this question: am I paying for this addition?

Here is an example of responses when I ask for a change to the DB:

Enter a question, or type 'exit' to end: pls show sugar items
Here are the sugar items from the inventory:

| Item Name           | Item Type         | Qty | Unit of Measure | Reorder Point | Reorder? |
|---------------------|-------------------|-----|------------------|---------------|----------|
| Sugar               | Demerara          | 50  | packets          | 50            |          |
| Sugar               | brown             | 50  | packets          | 50            |          |
| Sugar               | brown             | 50  | pounds           | 50            |          |
| Sugar               | cane              | 60  | packets          | 50            |          |
| Sugar               | confectioner's    | 50  | pounds           | 50            |          |
| Sugar               | granulated        | 50  | ounces           | 50            |          |
| Sugar               | granulated        | 50  | pounds           | 50            |          |
| Sugar               | powdered          | 50  | pounds           | 50            |          |
| Sugar               | turbinado         | 50  | packets          | 50            |          |
| Sugar               | white             | 50  | packets          | 50            |          |

Let me know if there is anything else you would like to do!
===============================
Enter a question, or type 'exit' to end: pls increase by 10
Which sugar item would you like to increase by 10? Please specify the item type.
===============================
Enter a question, or type 'exit' to end: white
Before:

Item Name: Sugar
Item Type: white
Qty: 50
Unit of Measure: packets
Reorder Point: 50
Reorder?: NaN


After:

Item Name: Sugar
Item Type: white
Qty: 60
Unit of Measure: packets
Reorder Point: 50
Reorder?: NaN


HOWEVER, there seems to be a solution to the problem tho' costly in terms increase increase to context stream and token charges. Here is a session to illustrate:

Enter a question, or type 'exit' to end: please show the sugar items
The inventory data doesn't seem to include any sugar items based on what I've scanned. Would you like me to search for a specific type or additional details?
===============================
Enter a question, or type 'exit' to end: please give me the first line of the file
The first line of the inventory file is:

- **Item Name:** Almond butter
- **Item Type:** crunchy
- **Qty:** 50
- **Unit of Measure:** jars
- **Reorder Point:** 50
- **Reorder?:** NaN
===============================
Enter a question, or type 'exit' to end: try for sugar again
Here are the sugar items in the inventory:

1. **Sugar** - Demerara: 50 packets
2. **Sugar** - brown: 50 packets
3. **Sugar** - brown: 50 pounds
4. **Sugar** - cane: 50 packets
5. **Sugar** - confectioner's: 50 pounds
6. **Sugar** - granulated: 50 ounces
7. **Sugar** - granulated: 50 pounds
8. **Sugar** - powdered: 50 pounds
9. **Sugar** - turbinado: 50 packets
10. **Sugar** - white: 50 packets
===============================
Enter a question, or type 'exit' to end: please add 20 to powdered
The record was changed as follows:

before: Item Name: Sugar, Item Type: powdered, Qty: 50, Unit of Measure: pounds, Reorder Point: 50, Reorder?: NaN  
after: Item Name: Sugar, Item Type: powdered, Qty: 70, Unit of Measure: pounds, Reorder Point: 50, Reorder?: NaN
===============================
Enter a question, or type 'exit' to end: This is an example of how you should respond if I tell you to make a change in the inventory file:\\before: Sugar,cane,60,packets,50,\\after: Sugar,cane,70,packets,50, 
Understood! I'll follow that structure for future changes. Would you like to make any more adjustments?
===============================
Enter a question, or type 'exit' to end: pls add 20 to cheddar scones
It appears there are no cheddar scones listed in the inventory. Would you like me to check for any other item or perform a different action?
===============================
Enter a question, or type 'exit' to end: please show all the scone items
Here are the scone items in the inventory:

1. **Scones** - blueberry: 50 dozen
2. **Scones** - cheddar and chive: 50 dozen
3. **Scones** - cheddar: 50 dozen
4. **Scones** - cranberry orange: 50 dozen
5. **Scones** - plain: 50 dozen
===============================
Enter a question, or type 'exit' to end: pls add 20 to cheddar scones
before: Scones, cheddar, 50, dozen, 50,  
after: Scones, cheddar, 70, dozen, 50,  
===============================
Enter a question, or type 'exit' to end: exit
(base) tom@tom-7040:~/now$ ./go
===============================
Enter a question, or type 'exit' to end: pls add 10 to blueberry scones
before: Scones, blueberry, 50, dozen, 50,  
after: Scones, blueberry, 60, dozen, 50,  
===============================
Enter a question, or type 'exit' to end: add 10 to confectioner sugar
before: Sugar, confectioner's, 50, pounds, 50,  
after: Sugar, confectioner's, 60, pounds, 50,  
===============================
Enter a question, or type 'exit' to end: exit


Even here tho' there is disobedience tho'. The instruction is this:
before: Sugar,cane,60,packets,50,
after: Sugar,cane,70,packets,50, 

What I get instead is:
before: Sugar, confectioner's, 50, pounds, 50,  
after: Sugar, confectioner's, 60, pounds, 50,  

i.e., the bot has added spaces. Picky perhaps but any disobedient behavior raises of course the question, "What next?"

I've been working with this app in various forms for a good while now and my dissatisfaction has got me thinking of quitting dbm apps for a while and trying AI at writing tasks. There seems to be a lot of need for people who can train others to use AI for writing.