0

I'm developing a chatbot using OpenAI's Assistants API and facing an issue with integrating the Thread functionality. When trying to create a thread using openai.Thread.create, I encounter an AttributeError stating that the 'openai' module has no attribute 'Thread'.

Here's the code:

import openai

openai.api_key = "my_key"

def my_chatbot(user_input, assistant_id):
    thread_id = openai.Thread.create(assistant=assistant_id)["id"]

    openai.Message.create(
        thread=thread_id,
        role="user",
        content=user_input
    )

    run = openai.Run.create(
        thread=thread_id,
        assistant=assistant_id
    )

    messages = openai.Message.list(thread=thread_id)
    return messages["data"][-1]["content"]

if __name__ == "__main__":
    assistant_id = "my_id"
    while True:
        user_input = input("Você: ")
        if user_input.lower() in ["sair", "fechar"]:
            break

        chatbot_response = my_chatbot(user_input, assistant_id)
        print("Chatbot:", chatbot_response)

The error message is

Traceback (most recent call last):
  File "/Users/felipestoker/PycharmProjects/chatbot/main.py", line 34, in <module>
    chatbot_response = chatbot_webjasper(user_input, assistant_id)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/felipestoker/PycharmProjects/chatbot/main.py", line 8, in chatbot_webjasper
    thread_id = openai.Thread.create(assistant=assistant_id)["id"]
                ^^^^^^^^^^^^^
AttributeError: module 'openai' has no attribute 'Thread'
5
  • 1
    Please include the ful traceback error. What version of OpenAI do you have installed? Commented Dec 19, 2023 at 1:33
  • Though this might help you community.openai.com/t/module-openai-has-no-attribute-thread/… Commented Dec 19, 2023 at 1:34
  • @ewokx full traceback error updated in the question. I tried this link, but it is not working. Commented Dec 19, 2023 at 1:42
  • 1
    That traceback isn't the traceback for the code you posted. I don't see chatbot_webjasper mentioned in the posted code. Commented Dec 19, 2023 at 1:54
  • @weokx it is, I deleted for privacy. Commented Dec 19, 2023 at 2:20

2 Answers 2

3

Problem

The method names you're trying to use don't work with the OpenAI Python SDK version 1.0.0 or newer.

Solution

The following are the correct method names if you have the OpenAI Python SDK version 1.0.0 or newer:

  • Create thread: openai.beta.threads.create
  • Create message: openai.beta.threads.messages.create
  • Create run: openai.beta.threads.runs.create
  • List messages: openai.beta.threads.messages.list

See the official OpenAI documentation.

Also, I've made a YouTube tutorial on how to use the Assistants API and posted the code on my GitHub profile.

Sign up to request clarification or add additional context in comments.

Comments

2

Please take a look at this

https://github.com/rajib76/assistants/blob/main/assistants/llm_assistants/openai_assistants.py

This shows how to create thread and messages. This is part of the langassist package. If you are still not able to follow, please let me know. I will try to rewrite the program that you have above.

1 Comment

I am a beginner developer. It sounded difficult to me. In my case, I want to use Assistants API as a model. Because I have trained an Assistant to work with chatbot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.