#Assistant API & Discord bot Integration - Not Working

1 messages · Page 1 of 1 (latest)

pure kettle
#

Hey there, I'm currently trying to build a Discord Bot using the Assistants API but I'm running into an error that I can't seem to fix. The Discord bot itself is working and responding as well as creating a thread through the assistants api but in the send message to thread request returns as follows:

"Error in send_message_tothread: Request failed with status 404: {
"error": {
"message": "Invalid URL (POST /v1/assistants/asst*/runs)",
"type": "invalid_request_error",
"param": null,
"code": null
}
}

the assistant ID as well as API key etc. are all correct and I followed the documentation on the OpenAI site as well as I could. As I'm not a dev myself I'm sure that I made a simple error somewhere that I am just not aware of and it would be amazing if someone could help me fix this error.

If anyone is able to hop on a call or chat with me directly that would be absolutely incredible, thank you in advance ❤️

rapid notch
pure kettle
#

thank you @rapid notch , now I'm getting another error but at least not the same one anymore. This is the one I'm getting now

Error in send_message_to_thread: Request failed with status 400: {
"error": {
"message": "2 validation errors for Request\nbody -> assistant_id\n field required (type=value_error.missing)\nbody -> messages\n extra fields not permitted (type=value_error.extra)",
"type": "invalid_request_error",
"param": null,
"code": null

rapid notch
pure kettle
#
    print(f"Assistant ID: {assistant_id}")

    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {OPENAI_API_KEY}',
        'OpenAI-Beta': 'assistants=v1'
    }
    data = json.dumps({
        "model": "gpt-4",
        "messages": [{"role": "user", "content": query}]
    })

    async with aiohttp.ClientSession() as session:
        async with session.post(
            f'https://api.openai.com/v1/threads/{thread_id}/runs',
            headers=headers,
            data=data
        ) as response:
            if response.status != 200:
                raise Exception(f"Request failed with status {response.status}: {await response.text()}")
            return await response.json()

async def send_message_to_thread(thread_id, query):
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {OPENAI_API_KEY}',
        'OpenAI-Beta': 'assistants=v1'
    }
    data = json.dumps({"role": "user", "content": query})

    async with aiohttp.ClientSession() as session:
        try:
            # Sending message to the thread
            async with session.post(
                f'https://api.openai.com/v1/threads/{thread_id}/messages',
                headers=headers,
                data=data
            ) as response:
                if response.status != 200:
                    raise Exception(f"Failed to send message: {await response.text()}")

            # Initiate the run
            run_response = await initiate_run(ASSISTANT_ID, thread_id, query)
            print(f"Run initiated: {run_response}")  # Debugging

            # Wait for the assistant's response
            await asyncio.sleep(30)

            # Retrieve assistant's response
            async with session.get(
                f'https://api.openai.com/v1/threads/{thread_id}/messages',
                headers=headers
            ) as response:
                if response.status != 200:
                    raise Exception(f"Failed to retrieve messages: {await response.text()}")
                response_messages = await response.json()

        except Exception as e:
            print(f"Error in send_message_to_thread: {str(e)}")
            return "An error occurred."

        for message in response_messages.get('data', []):
            if message['role'] == 'assistant':
                return message['content'][0]['text']['value']
        return "No response from the Assistant." ```
rapid notch