Hi,
I have the following line of code and I get the above-mentioned error, does anyone know what I am doing wrong? “An error occurred: module ‘openai’ has no attribute ‘Assistant’”
Create an Assistant with OpenAI
assistant_response = openai.ChatCompletion.create(
instructions="You are an SEO expert that should help people with SEO.",
model="gpt-4-1106-preview",
tools=[{"type": "retrieval"}],
file_ids=[file_id]
)
assistant_id = assistant_response['id']
Create a Run to generate a response from the Assistant
run_response = openai.Run.create(
thread_id=thread_id,
assistant_id=assistant_id
)
# Poll OpenAI for the Assistant's response
assistant_messages = await poll_for_run_completion(thread_id, run_response['id'])
# Send the Assistant's response to the Discord channel
for content in assistant_messages:
await ctx.send(content)
really appreciate the help
If the error you’re getting is “An error occurred: module ‘openai’ has no attribute ‘Assistant” you’re probably trying to use the Assistant API?
I don’t see any reference to “Assistant” in your code though.
Also, I’m pretty sure you don’t use “openai.ChatCompletion.create” to create a assistant.
(edit: Assuming you’re using Python and the official OpenAI library your should take a look at their API docs for it, which you can find on the github page.)
1 Like
I believe you need to use this formulation (per the documentation):
assistant = client.beta.assistants.create(
name=“your app name”,
description=“your instruction”,
model=“gpt-4-1106-preview”,
tools=[{“type”: “retrieval”}],
file_ids=[file.id]
)
Hope this helps and let me know if it works…
Here is the full code:
import os
import discord
from discord.ext import commands
import openai
import asyncio
Load your API keys from environment variables
openai.api_key = os.getenv(‘OPENAI_API_KEY’)
discord_bot_token = os.getenv(‘DISCORD_BOT_TOKEN’)
Define the needed Discord intents for the bot to function properly
intents = discord.Intents.default()
intents.messages = True # Enables receiving messages
intents.message_content = True # Enables receiving message content
intents.guilds = True # Enables guild-related events, necessary for the bot to function in servers
Initialize the Discord bot with the specified intents
bot = commands.Bot(command_prefix=“!”, intents=intents)
async def poll_for_run_completion(thread_id, run_id):
while True:
run_status = openai.Run.retrieve(thread_id=thread_id, run_id=run_id)
if run_status[‘status’] == ‘completed’:
# Run completed, retrieve messages
messages_response = openai.Message.list(thread_id=thread_id)
return [msg[‘content’] for msg in messages_response[‘data’] if msg[‘role’] == ‘assistant’]
elif run_status[‘status’] in [‘failed’, ‘expired’]:
# Handle failed or expired run
return [‘The assistant failed to process the request.’]
await asyncio.sleep(1) # Use asyncio.sleep to avoid blocking
@bot.event
async def on_ready():
print(f’{bot.user} has connected to Discord!')
@bot.command(name=‘assist’)
async def assist(ctx, *, question: str):
try:
# Use your existing file_id provided by OpenAI
file_id = ‘taken away file id for now’
# The changed part of creating an assistant
assistant = client.beta.assistants.create(
name="your app name", # You need to specify your app name here
description="A bot test ", # Your assistant's description
model="gpt-4-1106-preview", # The model you want to use
tools=[{"type": "retrieval"}], # Any tools you want to use
file_ids=[file_id] # The file id associated with your assistant
)
assistant_id = assistant['id']
# Create a Thread for the conversation
thread_response = openai.Thread.create()
thread_id = thread_response['id']
# Add the user's question to the Thread as a Message
message_response = openai.Message.create(
thread_id=thread_id,
role="user",
content=question,
file_ids=[file_id] # The file_id is included here as well
)
# Create a Run to generate a response from the Assistant
run_response = openai.Run.create(
thread_id=thread_id,
assistant_id=assistant_id
)
# Poll OpenAI for the Assistant's response
assistant_messages = await poll_for_run_completion(thread_id, run_response['id'])
# Send the Assistant's response to the Discord channel
for content in assistant_messages:
await ctx.send(content)
except Exception as e:
await ctx.send(f'An error occurred: {e}')
Run the bot
bot.run(discord_bot_token)
Would have been nice if you sent it in a code-block but either way, you need to define client in your code. Which according to their API is done using
client = OpenAI(
# defaults to os.environ.get("OPENAI_API_KEY")
api_key="My API Key",
)
The api_key parameter is optional, if you put your API key inside OPENAI_AP_KEY environment variable on your system.
1 Like
Sorry I missed to send it in a code block! Thank you so much for the help, will give it a try! Really appreciate it!
This is my small test, which doesn’t work. Any help on what’s missing would be fantastic. I can’t get the imports and OpenAI correct.
import openai
from openai import OpenAI
import os
client = OpenAI(
api_key=“My API Key”,
)
assistant = client.beta.assistants.create(
name=“your app name”, # You need to specify your app name here
description="A bot test ", # Your assistant’s description
model=“gpt-4-1106-preview”, # The model you want to use
tools=[{“type”: “retrieval”}]
)
print(assistant)