diff --git a/docs/docs/examples/elocution-bot.md b/docs/docs/examples/elocution-bot.md new file mode 100644 index 00000000..3646440b --- /dev/null +++ b/docs/docs/examples/elocution-bot.md @@ -0,0 +1,49 @@ +--- +sidebar_position: 4 +--- + +# Elocution Bot + +This bot makes an API call to OpenAI and helps users learn proper elocution and pronunciation for different words. It uses GPT-3.5 Turbo. + +## Textbase Bot +- This bot is the same as Open AI bot except with an updated prompt focusing on elocution and pronunciation of words. +- Please set the Open AI API Key in the `main.py` file inside the `elocution-bot` folder. +- The following prompt has been used for this bot: +```text +SYSTEM_PROMPT = """You are an elocution and pronunciation tutor. Your goal is to help users improve their speech and pronunciation by focusing on syllable sounds. +Please provide clear explanations and examples to help users understand and practice the correct pronunciation of words with different syllable structures. +Also encourage users to ask questions and engage in pronunciation exercises with you. +""" +``` + +## Discord Bot +- This bot provides the above functionality as a discord bot with audio cues to help the user understand the correct pronunciation. + +### Setup Instructions +- Please set the Open AI API Key in the `bot_discord.py` file inside the `elocution-bot` folder. +- To setup this bot go to [Discord Developer Portal](https://discord.com/developers/applications). +- Click on `New Application`, fill in the details and then click on `Create`. +- Feel free to customize your bot as per your liking. +- On the left sidebar, click on `Bot`, then `Reset Token`, copy it and set it as the `DISCORD_TOKEN` in `bot_discord.py` file. +- Turn all three options ON under `Privileged Gateway Inputs`. +- Again on the left sidebar, click on `OAuth2`, set `Authorization Method` to `In-App Authorization`, set `Scopes` to `bot` and turn on the following permissions: +```text +Read Messages/View Channel +Send Messages +Attach Files +Read Message History +Mention everyone +``` +- On the left sidebar, click on `URL Generator`, set `Scopes` to `bot`, and turn on the same permissions as above. +- Copy the `Generated URL`, paste it in a browser and add the bot to your required Server. + +### Usage Instructions +- To start the bot, run the following command inside `elocution-bot` folder. +```bash +python bot_discord.py +``` +- Use the following command (without the braces) to get a response from the bot on any channel in your Discord Server. +```text +/pronounce {word} +``` \ No newline at end of file diff --git a/examples/elocution-bot/bot_discord.py b/examples/elocution-bot/bot_discord.py new file mode 100644 index 00000000..bac73a97 --- /dev/null +++ b/examples/elocution-bot/bot_discord.py @@ -0,0 +1,55 @@ +import os +import openai +import discord +from discord.ext import commands +from gtts import gTTS +intents = discord.Intents.default() +intents.message_content = True +bot = commands.Bot(command_prefix='/', intents=intents) + +# Set your Tokens/API Keys here +openai.api_key = '' +DISCORD_TOKEN = '' + +# A display message to show that the bot is running +@bot.event +async def on_ready(): + print("Elocution Bot is up and running!") + +# An error message for using wrong command format +@bot.event +async def on_command_error(ctx, error): + if isinstance(error, commands.CommandNotFound): + await ctx.send("Please use the correct syntax: `/pronounce {word}`") + print("Error! Command not found.") + +# Function to handle the /pronounce command +@bot.command() +async def pronounce(ctx, *, word=None): + print(f"Request arrived from {ctx.author.name} for the word: {word}") + if word is None: + await ctx.reply("Please use the correct syntax: `/pronounce {word}`") + return + + try: + response = openai.ChatCompletion.create( + model="gpt-3.5-turbo", + messages=[ + {"role": "system", "content": "You are an elocution and pronunciation tutor. Your goal is to help users improve their speech and pronunciation by focusing on syllable sounds."}, + {"role": "user", "content": f"Help me pronounce the word: {word}"} + ], + max_tokens=150, + temperature=0.7 + ) + pronunciation_instructions = response.choices[0].message["content"].strip() + tts = gTTS(text=pronunciation_instructions, lang='en') + tts.save(f"{word}.mp3") + await ctx.reply(f"Here's your pronunciation for: {word}") + await ctx.reply(file=discord.File(f"{word}.mp3")) + os.remove(f"{word}.mp3") + + except Exception as e: + print("Error generating response: ", e) + await ctx.reply("Error generating response! Please try again.") + +bot.run(DISCORD_TOKEN) \ No newline at end of file diff --git a/examples/elocution-bot/main.py b/examples/elocution-bot/main.py new file mode 100644 index 00000000..1167e718 --- /dev/null +++ b/examples/elocution-bot/main.py @@ -0,0 +1,44 @@ +from textbase import bot, Message +from textbase.models import OpenAI +from typing import List + +# Load your OpenAI API key +OpenAI.api_key = "" + +# Prompt for GPT-3.5 Turbo +SYSTEM_PROMPT = """You are an elocution and pronunciation tutor. Your goal is to help users improve their speech and pronunciation by focusing on syllable sounds. +Please provide clear explanations and examples to help users understand and practice the correct pronunciation of words with different syllable structures. +Also encourage users to ask questions and engage in pronunciation exercises with you. +""" + +@bot() +def on_message(message_history: List[Message], state: dict = None): + + # Generate GPT-3.5 Turbo response + bot_response = OpenAI.generate( + system_prompt=SYSTEM_PROMPT, + message_history=message_history, # Assuming history is the list of user messages + model="gpt-3.5-turbo", + ) + + response = { + "data": { + "messages": [ + { + "data_type": "STRING", + "value": bot_response + } + ], + "state": state + }, + "errors": [ + { + "message": "" + } + ] + } + + return { + "status_code": 200, + "response": response + } \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index fd2dd53a..5362ce09 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,8 @@ tabulate = "^0.9.0" functions-framework = "^3.4.0" yaspin = "^3.0.0" pydantic = "^2.3.0" +discord = "^2.3.2" +gTTS = "^2.3.2" [build-system] requires = ["poetry-core"]