#Assistant API

1 messages · Page 1 of 1 (latest)

lethal willow
#

I have created an assistant in the dashboard section and have the key. Can you write me a sample python program to let me know how I can interact with it ?

merry portalBOT
#

Hi @lethal willow. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Sure, here's a simple Python program that uses the OpenAI API to interact with an assistant you've created. This example assumes you have the assistant's ID and your OpenAI API key.

from openai import OpenAI
import os

# Replace with your OpenAI API key and Assistant ID
OPENAI_API_KEY = "<your OpenAI API key>"
MATH_ASSISTANT_ID = "<your Assistant ID>"

client = OpenAI(api_key=OPENAI_API_KEY)

def submit_message(assistant_id, thread, user_message):
    client.beta.threads.messages.create(
        thread_id=thread.id, role="user", content=user_message
    )
    return client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant_id,
    )

def get_response(thread):
    return client.beta.threads.messages.list(thread_id=thread.id, order="asc")

def create_thread_and_run(user_input):
    thread = client.beta.threads.create()
    run = submit_message(MATH_ASSISTANT_ID, thread, user_input)
    return thread, run

# Emulating a user request
thread, run = create_thread_and_run("Your question here")

This program creates a new thread for each user request and submits a message to the assistant. The submit_message function creates a message on the thread and starts a new run. The get_response function returns the list of messages in a thread. The create_thread_and_run function creates a new thread and submits a user message to it.

Please replace "Your question here" with the question you want to ask your assistant.

Remember to replace <your OpenAI API key> and <your Assistant ID> with your actual OpenAI API key and the ID of the assistant you've created.

#

@lethal willow kapa.ai is still learning and improving, please let us know how it did by reacting below

lethal willow
#

@merry portal It says can not find the assistant even though I have checked the assistant id and it is correct and the assistant does exist

merry portalBOT
#

Let me check the knowledge base for you. One moment! :books:

#

If you're sure that the assistant ID is correct and the assistant does exist, there could be a few reasons why you're encountering this issue:

  1. API Key Mismatch: Ensure that the API key you're using is associated with the account where the assistant was created. If you're using an API key from a different account, the assistant won't be found.

  2. Check the List of Assistants: You can make a list assistants request (https://platform.openai.com/docs/api-reference/assistants/listAssistants) to the API with the same API key and check if your assistant is in the list. This can help confirm if the assistant is accessible with your current API key.

  3. Check the Project of the Assistant: If you're using the OpenAI Playground or Dashboard, make sure to check under the "Default" project. Sometimes, assistants might be listed under this project (source (https://community.openai.com/t/933917)).

  4. Temporary Glitches: There could be temporary issues with the OpenAI API. If other methods don't work, you might want to wait for a while and try again.

If none of these solutions work, it would be best to reach out to OpenAI support for further assistance.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@lethal willow kapa.ai is still learning and improving, please let us know how it did by reacting below

lethal willow
#

@merry portal Can you write me a simple outhon code that I can use with streamlit for having conversations with my assistant ? Also include how to save the chat history do the previous messages do not disappear after a nee prokot is entered. Please create a new assistant for Math as an example.

merry portalBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Sure, here's a simple Python program using Streamlit and OpenAI API to interact with an assistant. This example creates a new assistant for Math and maintains the chat history.

First, let's create a new assistant for Math:

from openai import OpenAI
import os

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "<your OpenAI API key if not set as env var>"))

assistant = client.beta.assistants.create(
    name="Math Tutor",
    instructions="You are a personal math tutor. Answer questions briefly, in a sentence or less.",
    model="gpt-4-1106-preview",
)

Now, let's create a Streamlit app:

import streamlit as st
from openai import OpenAI
import os

# Replace with your OpenAI API key and Assistant ID
OPENAI_API_KEY = "<your OpenAI API key>"
MATH_ASSISTANT_ID = "<your Assistant ID>"

client = OpenAI(api_key=OPENAI_API_KEY)

def submit_message(assistant_id, thread, user_message):
    client.beta.threads.messages.create(
        thread_id=thread.id, role="user", content=user_message
    )
    return client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant_id,
    )

def get_response(thread):
    return client.beta.threads.messages.list(thread_id=thread.id, order="asc")

def create_thread_and_run(user_input):
    thread = client.beta.threads.create()
    run = submit_message(MATH_ASSISTANT_ID, thread, user_input)
    return thread, run

# Streamlit app
st.title('Chat with Math Assistant')

# Initialize chat history
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []

user_input = st.text_input("You: ")

if st.button('Send'):
    # Add user message to chat history
    st.session_state.chat_history.append({'role': 'user', 'content': user_input})
#

Get assistant response

thread, run = create_thread_and_run(user_input)
response = get_response(thread)

# Add assistant message to chat history
st.session_state.chat_history.append({'role': 'assistant', 'content': response['messages'][-1]['content']})

Display chat history

for message in st.session_state.chat_history:
if message['role'] == 'user':
st.write(f"You: {message['content']}")
else:
st.write(f"Assistant: {message['content']}")


This Streamlit app maintains the chat history in the `st.session_state.chat_history` list. Each time you send a message, it adds your message and the assistant's response to the chat history. The chat history is then displayed in the Streamlit app.

Please replace `<your OpenAI API key>` and `<your Assistant ID>` with your actual OpenAI API key and the ID of the assistant you've created.

This code is based on the example provided in the **OpenAI Cookbook** (<https://cookbook.openai.com/examples/assistants_api_overview_python#example>) and the **Streamlit documentation** (<https://docs.streamlit.io/library/api-reference/session-state>).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
#

@lethal willow kapa.ai is still learning and improving, please let us know how it did by reacting below

lethal willow
#

@merry portal Dont use the math assistant id, us the new assistant you have created

merry portalBOT
#

Let me check the knowledge base for you. One moment! :books:

#

I apologize for the confusion. Here's the corrected code. This time, we'll use the ID of the assistant we created:

First, let's create a new assistant for Math:

from openai import OpenAI
import os

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "<your OpenAI API key if not set as env var>"))

assistant = client.beta.assistants.create(
    name="Math Tutor",
    instructions="You are a personal math tutor. Answer questions briefly, in a sentence or less.",
    model="gpt-4-1106-preview",
)

# Save the assistant ID
ASSISTANT_ID = assistant.id

Now, let's create a Streamlit app:

import streamlit as st
from openai import OpenAI
import os

# Replace with your OpenAI API key
OPENAI_API_KEY = "<your OpenAI API key>"

client = OpenAI(api_key=OPENAI_API_KEY)

def submit_message(assistant_id, thread, user_message):
    client.beta.threads.messages.create(
        thread_id=thread.id, role="user", content=user_message
    )
    return client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant_id,
    )

def get_response(thread):
    return client.beta.threads.messages.list(thread_id=thread.id, order="asc")

def create_thread_and_run(user_input):
    thread = client.beta.threads.create()
    run = submit_message(ASSISTANT_ID, thread, user_input)
    return thread, run

# Streamlit app
st.title('Chat with Math Assistant')

# Initialize chat history
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []

user_input = st.text_input("You: ")

if st.button('Send'):
    # Add user message to chat history
    st.session_state.chat_history.append({'role': 'user', 'content': user_input})

    # Get assistant response
    thread, run = create_thread_and_run(user_input)
    response = get_response(thread)
#

Add assistant message to chat history

st.session_state.chat_history.append({'role': 'assistant', 'content': response['messages'][-1]['content']})

Display chat history

for message in st.session_state.chat_history:
if message['role'] == 'user':
st.write(f"You: {message['content']}")
else:
st.write(f"Assistant: {message['content']}")


In this code, we're using the `ASSISTANT_ID` variable, which stores the ID of the assistant we created. Please replace `<your OpenAI API key>` with your actual OpenAI API key.

This code is based on the example provided in the **OpenAI Cookbook** (<https://cookbook.openai.com/examples/assistants_api_overview_python#example>) and the **Streamlit documentation** (<https://docs.streamlit.io/library/api-reference/session-state>).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
lethal willow
#

@merry portal How can I use run here to only get the Assistant response ? “run = client.beta.threads.runs.create_and_poll(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Please address the user as Jane Doe. The user has a premium account."
)”

merry portalBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The create_and_poll method creates a new run and automatically polls it until it's completed. Once the run is completed, you can retrieve the assistant's response from the messages in the thread. Here's how you can do it:

from openai import OpenAI

# Replace with your OpenAI API key and Assistant ID
OPENAI_API_KEY = "<your OpenAI API key>"
ASSISTANT_ID = "<your Assistant ID>"

client = OpenAI(api_key=OPENAI_API_KEY)

# Create a new thread
thread = client.beta.threads.create()

# Create and poll a new run
run = client.beta.threads.runs.create_and_poll(
  thread_id=thread.id,
  assistant_id=ASSISTANT_ID,
  instructions="Please address the user as Jane Doe. The user has a premium account."
)

# Once the run is completed, retrieve the messages from the thread
messages = client.beta.threads.messages.list(thread_id=thread.id)

# The assistant's response is the last message in the thread
assistant_response = messages.data[-1].content[0].text.value

print("Assistant's response:", assistant_response)

In this code, assistant_response will contain the assistant's response. Please replace <your OpenAI API key> and <your Assistant ID> with your actual OpenAI API key and the ID of the assistant you've created.

This code is based on the example provided in the OpenAI Cookbook (https://cookbook.openai.com/examples/assistants_api_overview_python#example).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@lethal willow kapa.ai is still learning and improving, please let us know how it did by reacting below

lethal willow
#

@merry portal Why am I getting “assistant” is not defined error in “assistant.id” here: from openai import OpenAI, AssistantEventHandler
from typing_extensions import override
import os

client = OpenAI(api_key = os.getenv("ASIS_KEY"))

ASSISTANT_ID = os.getenv("ASSISTANT_KEY")

ASSISTANT_ID = assistant.id

thread = client.beta.threads.create()

message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="Hey! How can you help me ?"
)

First, we create a EventHandler class to define

how we want to handle the events in the response stream.

class EventHandler(AssistantEventHandler):
@override
def on_text_created(self, text) -> None:
print(f"\nassistant > ", end="", flush=True)

@override
def on_text_delta(self, delta, snapshot):
print(delta.value, end="", flush=True)

def on_tool_call_created(self, tool_call):
print(f"\nassistant > {tool_call.type}\n", flush=True)

def on_tool_call_delta(self, delta, snapshot):
if delta.type == 'code_interpreter':
if delta.code_interpreter.input:
print(delta.code_interpreter.input, end="", flush=True)
if delta.code_interpreter.outputs:
print(f"\n\noutput >", flush=True)
for output in delta.code_interpreter.outputs:
if output.type == "logs":
print(f"\n{output.logs}", flush=True)

Then, we use the stream SDK helper

with the EventHandler class to create the Run

and stream the response.

with client.beta.threads.runs.stream(
thread_id=thread.id,
assistant_id=assistant.id,
event_handler=EventHandler(),
) as stream:
stream.until_done()

merry portalBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The error you're encountering is because the assistant variable is not defined before you try to access assistant.id.

In your code, you have this line:

ASSISTANT_ID = assistant.id

But assistant is not defined anywhere in the code you provided. If you have created an assistant and want to use its ID, you should directly use the ID as a string. You seem to have an environment variable for this (ASSISTANT_KEY), so you can use that directly:

ASSISTANT_ID = os.getenv("ASSISTANT_KEY")

Then, you can use ASSISTANT_ID in your code:

with client.beta.threads.runs.stream(
  thread_id=thread.id,
  assistant_id=ASSISTANT_ID,
  event_handler=EventHandler(),
) as stream:
  stream.until_done()

Please ensure that the ASSISTANT_KEY environment variable is correctly set to the ID of your assistant.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@lethal willow kapa.ai is still learning and improving, please let us know how it did by reacting below

lethal willow
#

@merry portal I’m getting unauthorized error, is there anything else I need to add other than the API key ?

merry portalBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The "Unauthorized" error typically indicates an issue with the API key. Here are a few things you can check:

  1. API Key Validity: Make sure the API key you're using is valid and hasn't been revoked or expired. You can check this in your OpenAI account's API keys section.

  2. API Key Visibility: Ensure that the API key is not exposed publicly in your code or application. OpenAI automatically deletes keys that are detected in public places to prevent unauthorized usage (source (https://community.openai.com/t/794624)).

  3. API Key Usage: The API key should be included in the Authorization header of your requests as a Bearer token. If you're using the OpenAI Python client, it will automatically use an environment variable named OPENAI_API_KEY (source (https://community.openai.com/t/766947)).

  4. Environment Variables: If you're using environment variables to store your API key, make sure they are correctly set in your operating system. You can check the instructions for setting environment variables on Windows (https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety#h_a1ab3ba7b2) and Linux/MacOS (https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety#h_a1ab3ba7b2).

  5. API Key in Code: If you're directly using the API key in your code, make sure it's correctly included and there are no typos or extra characters.

If you've checked all these points and you're still getting the "Unauthorized" error, it might be a good idea to generate a new API key in your OpenAI account and try using that. If the issue persists, you should reach out to OpenAI support for further assistance.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@lethal willow kapa.ai is still learning and improving, please let us know how it did by reacting below

lethal willow
#

@merry portal When using “assistant_response = messages.data[1].content[0].text.value”, half response is in content[0] and the other half is in content[1], I want the full response from the LLM

merry portalBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The messages.data object is a list of messages in the thread. Each message has a content field, which is also a list. Each item in the content list represents a block of content in the message. If a message is long, it might be split into multiple blocks.

If you want to get the full response from the language model, you need to concatenate all the text blocks in the assistant's message. Here's how you can do it:

# Get the assistant's message
assistant_message = messages.data[-1]

# Concatenate all the text blocks in the message
assistant_response = "".join(block.text.value for block in assistant_message.content)

print("Assistant's response:", assistant_response)

In this code, assistant_response will contain the full response from the assistant, even if it's split into multiple blocks.

This code is based on the structure of the messages object as described in the OpenAI Cookbook (https://cookbook.openai.com/examples/assistants_api_overview_python#example).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@lethal willow kapa.ai is still learning and improving, please let us know how it did by reacting below

lethal willow
#

@merry portal Can you please simplify this so it only prints the full assistant response “from typing_extensions import override
from openai import AssistantEventHandler

First, we create a EventHandler class to define

how we want to handle the events in the response stream.

class EventHandler(AssistantEventHandler):
@override
def on_text_created(self, text) -> None:
print(f"\nassistant > ", end="", flush=True)

@override
def on_text_delta(self, delta, snapshot):
print(delta.value, end="", flush=True)

def on_tool_call_created(self, tool_call):
print(f"\nassistant > {tool_call.type}\n", flush=True)

def on_tool_call_delta(self, delta, snapshot):
if delta.type == 'code_interpreter':
if delta.code_interpreter.input:
print(delta.code_interpreter.input, end="", flush=True)
if delta.code_interpreter.outputs:
print(f"\n\noutput >", flush=True)
for output in delta.code_interpreter.outputs:
if output.type == "logs":
print(f"\n{output.logs}", flush=True)

Then, we use the stream SDK helper

with the EventHandler class to create the Run

and stream the response.

with client.beta.threads.runs.stream(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Please address the user as Jane Doe. The user has a premium account.",
event_handler=EventHandler(),
) as stream:
stream.until_done()”

merry portalBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Sure, if you only want to print the full assistant response, you can simplify the EventHandler class to only handle the on_text_delta event. Here's how you can do it:

from typing_extensions import override
from openai import AssistantEventHandler

# Create a EventHandler class to handle the events in the response stream.
class EventHandler(AssistantEventHandler):    
  @override
  def on_text_delta(self, delta, snapshot):
    print(delta.value, end="", flush=True)

# Use the `stream` SDK helper with the `EventHandler` class to create the Run and stream the response.
with client.beta.threads.runs.stream(
  thread_id=thread.id,
  assistant_id=assistant.id,
  instructions="Please address the user as Jane Doe. The user has a premium account.",
  event_handler=EventHandler(),
) as stream:
  stream.until_done()

In this simplified version, the EventHandler class only overrides the on_text_delta method, which is called whenever a new text block is added to the assistant's message. It prints the text of the block without any additional formatting or labels.

Please replace thread.id and assistant.id with the actual ID of your thread and assistant. You can get these IDs from the responses of the client.beta.threads.create() and client.beta.assistants.create() methods, respectively.

This code is based on the example provided in the OpenAI Cookbook (https://cookbook.openai.com/examples/assistants_api_overview_python#example).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@lethal willow kapa.ai is still learning and improving, please let us know how it did by reacting below

lethal willow
#

@merry portal Please edit the following code so the streamlit UI can take in user response and print assistant response properly, currently the streamlit code is for chat completion. Code: messages = client.beta.threads.messages.list(thread_id=thread.id)

Create a EventHandler class to handle the events in the response stream.

class EventHandler(AssistantEventHandler):
@override
def on_text_delta(self, delta, snapshot):
print(delta.value, end="", flush=True)

with client.beta.threads.runs.stream(
thread_id=thread.id,
assistant_id=assistant.id,
event_handler=EventHandler(),
) as stream:
stream.until_done()

Streamlit UI

st.title("Assistant")

st.button("New Chat", on_click=new_chat)

if 'chat_history' not in st.session_state:
st.session_state.chat_history = []

Display chat messages from history on app rerun

Code here

React to user input

if prompt := st.chat_input("How can I help?"):
# Display user message in chat message container
st.chat_message("user").markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
st.session_state.chat_history.append({"role": "user", "content": prompt})

with st.chat_message("assistant"):
   response = generate_text_with_conversation(st.session_state.messages)
   st.session_state.messages.append(response)
   st.markdown(response.content)
   st.session_state.chat_history.append(response.content)
merry portalBOT
#

Let me check the knowledge base for you. One moment! :books:

#

To integrate the assistant response from the OpenAI API into your Streamlit UI, you need to modify the generate_text_with_conversation function to use the client.beta.threads.runs.stream method. Here's how you can do it:

import streamlit as st
from openai import OpenAI, AssistantEventHandler
from typing_extensions import override
import os

# Initialize OpenAI client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

# Define your assistant ID
ASSISTANT_ID = os.getenv("ASSISTANT_ID")

# Create a new thread
thread = client.beta.threads.create()

# Create a EventHandler class to handle the events in the response stream.
class EventHandler(AssistantEventHandler):    
  @override
  def on_text_delta(self, delta, snapshot):
    return delta.value

# Streamlit UI
st.title("Assistant")

# Initialize chat history
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []

# Display chat messages from history
for message in st.session_state.chat_history:
    st.chat_message(message["role"]).markdown(message["content"])

# React to user input
user_input = st.text_input("You:")
if st.button("Submit"):
    # Display user message in chat message container
    st.chat_message("user").markdown(user_input)
    # Add user message to chat history
    st.session_state.chat_history.append({"role": "user", "content": user_input})

    # Generate assistant response
    with client.beta.threads.runs.stream(
      thread_id=thread.id,
      assistant_id=ASSISTANT_ID,
      instructions=user_input,
      event_handler=EventHandler(),
    ) as stream:
      for event in stream:
        if event.type == "text":
          assistant_response = event.data.text.value
#

Display assistant response in chat message container

st.chat_message("assistant").markdown(assistant_response)
# Add assistant response to chat history
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})

In this code, `generate_text_with_conversation` is replaced with the `client.beta.threads.runs.stream` method, which generates the assistant's response in real-time. The assistant's response is then displayed in the Streamlit UI and added to the chat history.

Please replace `OPENAI_API_KEY` and `ASSISTANT_ID` with your actual OpenAI API key and the ID of your assistant.

This code is based on the example provided in the **OpenAI Cookbook** (<https://cookbook.openai.com/examples/assistants_api_overview_python#example>) and the **Streamlit documentation** (<https://docs.streamlit.io/library/api-reference/widgets>).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
#

@lethal willow kapa.ai is still learning and improving, please let us know how it did by reacting below

lethal willow
#

@merry portal You didn’t define the messages though

merry portalBOT
#

Let me check the knowledge base for you. One moment! :books:

#

You're correct, I apologize for the oversight. The messages array is used to keep track of the conversation between the user and the assistant. It should be initialized before the conversation starts and updated with each message.

Here's how you can modify the code to include the messages array:

import streamlit as st
from openai import OpenAI, AssistantEventHandler
from typing_extensions import override
import os

# Initialize OpenAI client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

# Define your assistant ID
ASSISTANT_ID = os.getenv("ASSISTANT_ID")

# Create a new thread
thread = client.beta.threads.create()

# Create a EventHandler class to handle the events in the response stream.
class EventHandler(AssistantEventHandler):    
  @override
  def on_text_delta(self, delta, snapshot):
    return delta.value

# Streamlit UI
st.title("Assistant")

# Initialize chat history and messages
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []
    st.session_state.messages = []

# Display chat messages from history
for message in st.session_state.chat_history:
    st.chat_message(message["role"]).markdown(message["content"])

# React to user input
user_input = st.text_input("You:")
if st.button("Submit"):
    # Display user message in chat message container
    st.chat_message("user").markdown(user_input)
    # Add user message to chat history and messages
    st.session_state.chat_history.append({"role": "user", "content": user_input})
    st.session_state.messages.append({"role": "user", "content": user_input})
#

Generate assistant response

with client.beta.threads.runs.stream(
  thread_id=thread.id,
  assistant_id=ASSISTANT_ID,
  instructions=user_input,
  event_handler=EventHandler(),
) as stream:
  for event in stream:
    if event.type == "text":
      assistant_response = event.data.text.value

# Display assistant response in chat message container
st.chat_message("assistant").markdown(assistant_response)
# Add assistant response to chat history and messages
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
st.session_state.messages.append({"role": "assistant", "content": assistant_response})

In this code, `st.session_state.messages` is initialized along with `st.session_state.chat_history`, and both are updated with each message. The `messages` array is used to keep track of the conversation in the format expected by the OpenAI API.

Please replace `OPENAI_API_KEY` and `ASSISTANT_ID` with your actual OpenAI API key and the ID of your assistant.

This code is based on the example provided in the **OpenAI Cookbook** (<https://cookbook.openai.com/examples/assistants_api_overview_python#example>) and the **Streamlit documentation** (<https://docs.streamlit.io/library/api-reference/widgets>).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
#

@lethal willow kapa.ai is still learning and improving, please let us know how it did by reacting below

lethal willow
#

@merry portal Getting this error “AttributeError: 'ThreadRunCreated' object has no attribute 'type'”

merry portalBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The error AttributeError: 'ThreadRunCreated' object has no attribute 'type' typically occurs when you're trying to access the type attribute of an event in the stream, but the event is of type ThreadRunCreated, which doesn't have a type attribute.

In the streaming API, different types of events have different attributes. For example, a ThreadRunCreated event doesn't have a type attribute, but a ThreadRunStepDelta event does.

To avoid this error, you should check the object attribute of the event before trying to access the type attribute. Here's an example:

for event in stream:
    if event.data.object == "thread.run.step.delta" and event.data.delta.content:
        print(event.data.delta.content[0].text)

In this code, event.data.object is used to check the type of the event before trying to access event.data.delta.content.

This solution is based on the discussion in this Github issue (https://github.com/openai/openai-python/issues/1334).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@lethal willow kapa.ai is still learning and improving, please let us know how it did by reacting below

lethal willow
#

@merry portal Can you please check what I’m doing wrong here. The first answer is printed correctly but when a new query is entered by the user, the same answer (for the first message) is printed again. Please provide a somution for this after analyzing my code: # Function to send a message to the assistant
def send_message(thread_id, user_message):
# Create a user message in the thread
client.beta.threads.messages.create(
thread_id=thread_id,
role="user",
content=user_message
)

run = client.beta.threads.runs.create(
    thread_id=thread_id,
    assistant_id=st.session_state.assistant.id
)

while run.status != "completed":
    run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)

# Retrieve the messages from the thread
messages = client.beta.threads.messages.list(thread_id=thread_id)

# Get the most recent assistant message
for message in reversed(messages.data):
    if message.role == "assistant":
        response = message.content[0].text.value
        break
else:
    response = "No response from assistant."

return response

Streamlit UI

st.title("Assistant Chatbot")

New Chat Button

if st.button("New Chat"):
st.session_state["thread_id"] = create_thread().id
st.session_state.chat_history = []

Initialize session state variables

if "thread_id" not in st.session_state:
st.session_state.thread_id = create_thread().id

if 'chat_history' not in st.session_state:
st.session_state.chat_history = []

Display chat messages from history on app rerun

for message in st.session_state.chat_history:
role = message.get("role", "")
content = message.get("content", "")

with st.chat_message(role):
    st.markdown(content)
merry portalBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The issue you're experiencing might be due to the fact that you're creating a new run for each user message, but you're not passing the user message to the run. As a result, the assistant is responding to the same initial state each time, which is why you're seeing the same response for each user message.

To fix this, you should pass the user message to the run when you create it. Here's how you can modify the send_message function to do this:

# Function to send a message to the assistant
def send_message(thread_id, user_message):
    # Create a user message in the thread
    client.beta.threads.messages.create(
        thread_id=thread_id,
        role="user",
        content=user_message
    )

    run = client.beta.threads.runs.create(
        thread_id=thread_id,
        assistant_id=st.session_state.assistant.id,
        instructions=user_message  # Pass the user message to the run
    )

    while run.status != "completed":
        run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)

    # Retrieve the messages from the thread
    messages = client.beta.threads.messages.list(thread_id=thread_id)

    # Get the most recent assistant message
    for message in reversed(messages.data):
        if message.role == "assistant":
            response = message.content[0].text.value
            break
    else:
        response = "No response from assistant."

    return response

In this code, instructions=user_message is added to the client.beta.threads.runs.create method to pass the user message to the run. This ensures that the assistant responds to the user message, not just the initial state of the thread.