Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions discord_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,43 @@ async def select_personality(ctx, personality: str):

@bot.command(name='coach', help='Generates a response based on the specified personality and conversation context.')
async def coach(ctx, num_messages: int = 10):
if num_messages <= 0:
await ctx.send('Number of messages must be greater than 0.')
return

try:
conversation_history = await get_conversation(ctx, num_messages)
if not conversation_history:
await ctx.send('No conversation history found.')
return

prompt = f'As an AI language model with a {selected_personality} personality, embody the characteristics and traits of this personality while responding to the following conversation:\n\n{conversation_history}\nHow would a {selected_personality}-like AI respond?'
response = generate_response(prompt, conversation_history)
await ctx.send(filter_response(response))
filtered_response = filter_response(response)

if not filtered_response:
await ctx.send('Unable to generate a suitable response. Please try again.')
else:
await ctx.send(filtered_response)
except Exception as e:
print(f'Error in `coach` command: {e}')
await ctx.send('An error occurred while generating a response. Please try again.')

async def get_conversation(ctx, num_messages: int):
messages = await ctx.channel.history(limit=num_messages).flatten()
conversation_history = [{"role": "system" if message.author.bot else "user", "content": message.content} for message in messages]
conversation_history = [{"role": "system" if message.author.bot else "user", "content": message.content} for message in reversed(messages)]
return conversation_history

def generate_response(prompt, messages):
try:
response = openai.ChatCompletion.create(
model=selected_engine,
messages=messages,
max_tokens=500,
n=1,
stop=None,
response = openai.Completion.create(
engine=selected_engine,
prompt=prompt,
max_tokens=150,
temperature=0.8,
n=1
)
generated_text = response.choices[0].text.strip()
generated_text = response["choices"][0]["text"].strip()
return generated_text
except Exception as e:
print(f'Error: {e}')
Expand Down
6 changes: 4 additions & 2 deletions test_discord_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def setUp(self):
def test_generate_response_with_chat_model(self):
with patch('openai.api_call') as mock_api_call:
mock_api_call.return_value = {'choices': [{'text': 'Test response.'}]}
response = generate_response(prompt="Test prompt.", model="gpt-4-chat", messages=[])
response = generate_response(prompt="Test prompt.", model="gpt-4-chat", messages=[{'role': 'system', 'content': 'Starting a conversation.'}])
self.assertEqual(response, 'Test response.')
mock_api_call.assert_called_once_with(
'v1/chat/completions', data={'messages': [], 'model': 'gpt-4-chat', 'prompt': 'Test prompt.'}
'v1/engines/gpt-4-chat/completions', data={'messages': [{'role': 'system', 'content': 'Starting a conversation.'}], 'model': 'gpt-4-chat', 'prompt': 'Test prompt.'}
)

def test_generate_response_with_non_chat_model(self):
Expand All @@ -24,6 +24,8 @@ def test_generate_response_with_non_chat_model(self):
mock_api_call.assert_called_once_with(
'v1/engines/gpt-4/completions', data={'messages': [], 'model': 'gpt-4', 'prompt': 'Test prompt.'}
)
'v1/engines/gpt-4/completions', data={'messages': [], 'model': 'gpt-4', 'prompt': 'Test prompt.'}
)
def test_coach_command_with_valid_input(self):
with patch('discord_bot.generate_response') as mock_generate_response:
mock_generate_response.return_value = 'Coaching advice'
Expand Down