Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Suggested Actions to welcome message #12

Merged
merged 5 commits into from
Aug 29, 2024
Merged
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
2 changes: 2 additions & 0 deletions .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
paths:
- "**.tf"
- "code/infra/**"
- "code/backend/**"
- ".github/workflows/terraform.yml"

pull_request:
Expand All @@ -14,6 +15,7 @@ on:
paths:
- "**.tf"
- "code/infra/**"
- "code/backend/**"
- ".github/workflows/terraform.yml"

jobs:
Expand Down
45 changes: 41 additions & 4 deletions code/backend/bots/assistant_bot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List

from botbuilder.core import ActivityHandler, MessageFactory, TurnContext
from botbuilder.schema import ChannelAccount
from botbuilder.schema import ActionTypes, CardAction, ChannelAccount, SuggestedActions
from llm.assisstant import assistant_handler


Expand All @@ -22,9 +22,46 @@ async def on_members_added_activity(
# Initialize thread in assistant
self.thread_id = assistant_handler.create_thread()
# Respond with welcome message
await turn_context.send_activity(
"Hello and welcome! I am your personal joke assistant. How can I help you today?"
welcome_message = (
"Hello and welcome! I am your personal joke assistant."
)
await turn_context.send_activity(welcome_message)

# Respond with suggested actions
suggested_topics_message = (
"Which topic would like to hear a joke about?"
)
suggested_topics = MessageFactory.text(suggested_topics_message)
suggested_topics.suggested_actions = SuggestedActions(
actions=[
CardAction(
type=ActionTypes.im_back,
title="Cars",
text="Cars",
display_text="Cars",
value="Cars",
),
CardAction(
type=ActionTypes.im_back,
title="Sports",
text="Sports",
display_text="Sports",
value="Sports",
),
CardAction(
type=ActionTypes.im_back,
title="Atoms",
text="Atoms",
display_text="Atoms",
value="Atoms",
),
]
)
await turn_context.send_activity(suggested_topics)

# Add messages from assisstant to thread
assistant_handler.send_assisstant_message(welcome_message)
assistant_handler.send_assisstant_message(suggested_topics_message)

async def on_message_activity(self, turn_context: TurnContext):
"""Acts upon new messages added to a channel.
Expand All @@ -33,7 +70,7 @@ async def on_message_activity(self, turn_context: TurnContext):
RETURNS (str): The assistant message actvity is being returned.
"""
# Interact with assistant
message = assistant_handler.send_message(
message = assistant_handler.send_user_message(
message=turn_context.activity.text,
thread_id=self.thread_id,
)
Expand Down
23 changes: 21 additions & 2 deletions code/backend/llm/assisstant.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ def create_thread(self) -> str:
logger.debug(f"Created thread with thread id: '{thread.id}'")
return thread.id

def send_message(self, message: str, thread_id: str) -> str | None:
def send_user_message(self, message: str, thread_id: str) -> str | None:
"""Send a message to the thread and return the response from the assistant.

message (str): The message to be sent to the assistant.
thread_id (str): The thread id to which the message should be sent to the assistant.
RETURNS (str): The response from the assistant is being returned.
"""
logger.debug(
f"Adding message to thread with thread id: '{thread_id}' - Mesage: '{message}'"
f"Adding message to thread with thread id: '{thread_id}' - Message (user): '{message}'"
)
if thread_id is None:
return None
Expand All @@ -83,6 +83,25 @@ def send_message(self, message: str, thread_id: str) -> str | None:

return message

def send_assisstant_message(self, message: str, thread_id: str) -> str | None:
"""Send a message to the thread in teh context of the assisstant.

message (str): The message to be sent to the thread in the contex of the assisstant.
thread_id (str): The thread id to which the message should be sent to the assistant.
RETURNS (None): No return value.
"""
logger.debug(
f"Adding message to thread with thread id: '{thread_id}' - Message (assistant): '{message}'"
)
if thread_id is None:
return None

message = self.client.beta.threads.messages.create(
thread_id=thread_id,
content=message,
role="assistant",
)

def __wait_for_run(self, run: Run, thread_id: str) -> Run:
"""Wait for the run to complete and return the run once completed.

Expand Down
10 changes: 9 additions & 1 deletion code/infra/roleassignments_uai.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
resource "azurerm_role_assignment" "uai_roleassignment_open_ai_contributor" {
description = "Required for accessing azure open ai from the web app"
description = "Required for accessing azure open ai from the web app."
scope = module.azure_open_ai.cognitive_account_id
role_definition_name = "Cognitive Services OpenAI Contributor"
principal_id = module.user_assigned_identity.user_assigned_identity_principal_id
principal_type = "ServicePrincipal"
}

resource "azurerm_role_assignment" "uai_roleassignment_key_vault_secrets_user" {
description = "Required for accessing secrets in teh key vault from teh web app app settings."
scope = module.key_vault.key_vault_id
role_definition_name = "Key Vault Secrets User"
principal_id = module.user_assigned_identity.user_assigned_identity_principal_id
principal_type = "ServicePrincipal"
}
2 changes: 1 addition & 1 deletion docs/SystemPrompt.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
You are a world-class assistant who tells jokes.
You are a world-class assistant who tells jokes. For anything but jokes, respond politely that you are just trying to make everyone’s life a happier place.
Loading