Hello, I tried to make an discord ai bot and it gave me an error
Error:
You tried to access openai.Completion, but this is no longer supported in openai>=1.0.0 - see the README at (github link. I can t post it) python for the API.
You can run openai migrate
to automatically upgrade your codebase to use the 1.0.0 interface.
Alternatively, you can pin your installation to the old version, e.g. pip install openai==0.28
A detailed migration guide is available here: (github migration guide.)
The code:
import discord
from discord.ext import commands
from googleapiclient.discovery import build
import openai
class AIClient(commands.Bot):
def init(self):
super().init(command_prefix=‘/’)
self.synced = False
async def on_ready(self):
if not self.synced:
await self.sync_commands()
self.synced = True
print(f"We have logged in as {self.user}")
GUILD_ID = Your server id
GOOGLE_API_KEY = ‘YOUR_GOOGLE_API_KEY’
CSE_ID = ‘YOUR_CSE_ID’
OPENAI_API_KEY = ‘YOUR_OPENAI_API_KEY’
openai.api_key = OPENAI_API_KEY
def google_search(query):
service = build(“customsearch”, “v1”, developerKey=GOOGLE_API_KEY)
res = service.cse().list(q=query, cx=CSE_ID).execute()
return res[‘items’] if ‘items’ in res else
def generate_steps(message):
response = openai.ChatCompletion.create(
model=“text-davinci-003”,
prompt=f"Describe and provide the steps to make {message}.",
max_tokens=150
)
return response[‘choices’][0][‘text’].strip()
bot = AIClient()
@bot.command(name=“heyai”, description=“Search on Google”)
async def heyai(ctx, *, message):
search_results = google_search(message)
if search_results:
response = “\n”.join([f"{item[‘title’]}: {item[‘link’]}" for item in search_results[:3]])
else:
response = “No results found.”
await ctx.send(response)
@bot.command(name=“ai”, description=“Describe and provide steps to make something”)
async def ai(ctx, *, message):
steps = generate_steps(message)
await ctx.send(steps)
bot.run(‘YOUR_BOT_TOKEN’)