Function stuck in a loop when posting to discord chat

Hey ill put a snippet of code under here. basically im using functions to check a chatroom for interesting topics. if one is found gpt responds what they think of the interesting topic. In my prompt i told it to check each room and make one post in each room. Somehow it’s geting put in a loop where it just talks to itself. I think i need to change how my message is appeneded during the while loop but im not quite sure how. Does anyone have any ideas?

messages = [{"role": "system", "content": conscious_state},
                {"role": "user", "content": user_prompts}]

    response = openai.ChatCompletion.create(
        model = "gpt-4-0613",
        messages=messages,
        functions=social_functions,
        function_call="auto"
    )

    print(response)

    while response["choices"][0]["finish_reason"] == "function_call":
        func_resp = get_chat_calling(response)
        messages.append({
            "role": "function",
            "name": response["choices"][0]["message"]["function_call"]["name"],
            "content": json.dumps(func_resp)
        })
        print("message: ", messages)
        response = openai.ChatCompletion.create(
            model="gpt-4-0613",
            messages=messages,
            functions=social_functions,
            function_call="auto"
        )
        print("response: ", response)
    else:
        print(response)

Hi @crawfordscott3d

You would need to store conversation for each room separately, so that when you iterate to a particular room, you only have context for that.

Also you’re appending the message list with message having “role”: “function”, and the calling the chat completion API.

Model only replies to the last message object in the list, which in your case in the function message, not the message by user.

Hence you get the model talking to itself effect.