Skip to content

Commit 2776cde

Browse files
author
Pablo Marin
committed
improvements in bot.py
1 parent ea38a1f commit 2776cde

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

apps/backend/backend.zip

204 Bytes
Binary file not shown.

apps/backend/bot.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from botbuilder.core import ActivityHandler, TurnContext
2323
from botbuilder.schema import ChannelAccount, Activity, ActivityTypes
2424

25-
25+
# Env variables needed by langchain
2626
os.environ["OPENAI_API_BASE"] = os.environ.get("AZURE_OPENAI_ENDPOINT")
2727
os.environ["OPENAI_API_KEY"] = os.environ.get("AZURE_OPENAI_API_KEY")
2828
os.environ["OPENAI_API_VERSION"] = os.environ.get("AZURE_OPENAI_API_VERSION")
@@ -48,11 +48,14 @@ def on_agent_action(self, action: AgentAction, **kwargs: Any) -> Any:
4848
asyncio.run(self.tc.send_activity(f"\u2611 Searching: {action} ..."))
4949
asyncio.run(self.tc.send_activity(Activity(type=ActivityTypes.typing)))
5050

51+
52+
# Bot Class
5153
class MyBot(ActivityHandler):
5254

53-
def __init__(self, conversation_state: ConversationState, user_state: UserState):
55+
def __init__(self):
5456
self.model_name = os.environ.get("AZURE_OPENAI_MODEL_NAME")
5557

58+
# Function to show welcome message to new users
5659
async def on_members_added_activity(self, members_added: ChannelAccount, turn_context: TurnContext):
5760
for member_added in members_added:
5861
if member_added.id != turn_context.activity.recipient.id:
@@ -62,7 +65,7 @@ async def on_members_added_activity(self, members_added: ChannelAccount, turn_co
6265
# See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
6366
async def on_message_activity(self, turn_context: TurnContext):
6467

65-
# Extract info from TurnContext
68+
# Extract info from TurnContext - You can change this to whatever , this is just one option
6669
session_id = turn_context.activity.conversation.id
6770
user_id = turn_context.activity.from_property.id + "-" + turn_context.activity.channel_id
6871
input_text_metadata = dict()
@@ -99,7 +102,7 @@ async def on_message_activity(self, turn_context: TurnContext):
99102

100103
tools = [www_search, sql_search, doc_search, chatgpt_search, book_search]
101104

102-
# Set brain Agent
105+
# Set brain Agent with persisten memory in CosmosDB
103106
cosmos = CosmosDBChatMessageHistory(
104107
cosmos_endpoint=os.environ['AZURE_COSMOSDB_ENDPOINT'],
105108
cosmos_database=os.environ['AZURE_COSMOSDB_NAME'],
@@ -114,10 +117,11 @@ async def on_message_activity(self, turn_context: TurnContext):
114117
agent_chain = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, memory=memory)
115118

116119
await turn_context.send_activity(Activity(type=ActivityTypes.typing))
120+
117121
# Please note below that running a non-async function like run_agent in a separate thread won't make it truly asynchronous. It allows the function to be called without blocking the event loop, but it may still have synchronous behavior internally.
118122
loop = asyncio.get_event_loop()
119123
answer = await loop.run_in_executor(ThreadPoolExecutor(), run_agent, input_text, agent_chain)
120-
124+
121125
await turn_context.send_activity(answer)
122126

123127

0 commit comments

Comments
 (0)