Skip to content

Commit

Permalink
fix validation
Browse files Browse the repository at this point in the history
  • Loading branch information
manthanguptaa committed Nov 29, 2024
1 parent 84a5481 commit c787e86
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 21 deletions.
19 changes: 11 additions & 8 deletions cookbook/examples/meeting_assistant/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MeetingAssistantWorkflow(Workflow):
"*Assignee*: <issue_assignee>",
"*Issue Link*: <issue_link>",
],
tools=[SlackTools(token=os.getenv("SLACK_TOKEN"))],
tools=[SlackTools()],
)

def run(self, meeting_notes: str, linear_users: Dict[str, str], use_cache: bool = False) -> RunResponse:
Expand Down Expand Up @@ -110,13 +110,16 @@ def run(self, meeting_notes: str, linear_users: Dict[str, str], use_cache: bool
linear_response: RunResponse = self.linear_agent.run(
f"Create issues in Linear for project {project_id} and team {team_id}: {tasks.model_dump_json()} and here is the dictionary of users and their uuid: {linear_users}. If you fail to create an issue, try again."
)
logger.info(f"Linear response: {linear_response}")
linear_issues = linear_response.content
linear_issues = None
if linear_response:
logger.info(f"Linear response: {linear_response}")
linear_issues = linear_response.content

# Send slack notification with tasks
logger.info(f"Sending slack notification with tasks: {linear_issues.model_dump_json()}")
slack_response: RunResponse = self.slack_agent.run(linear_issues.model_dump_json())
logger.info(f"Slack response: {slack_response}")
if linear_issues:
logger.info(f"Sending slack notification with tasks: {linear_issues.model_dump_json()}")
slack_response: RunResponse = self.slack_agent.run(linear_issues.model_dump_json())
logger.info(f"Slack response: {slack_response}")

return slack_response

Expand All @@ -135,10 +138,10 @@ def run(self, meeting_notes: str, linear_users: Dict[str, str], use_cache: bool
).read()
users_uuid = {
"Sarah": "8d4e1c9a-b5f2-4e3d-9a76-f12d8e3b4c5a",
"Mike": "2f9b7d6c-e4a3-42f1-b890-1c5d4e8f9a3b",
"Mike": "2f9b7d6c-e4a3-42f1-b890-1c5d4e8f9a3b",
"Emma": "7a1b3c5d-9e8f-4d2c-a6b7-8c9d0e1f2a3b",
"Alex": "4c5d6e7f-8a9b-0c1d-2e3f-4a5b6c7d8e9f",
"James": "1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d"
"James": "1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d",
}

# Run workflow
Expand Down
7 changes: 1 addition & 6 deletions cookbook/tools/slack_tools.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
"""Run `pip install openai slack-sdk` to install dependencies."""

import os

from phi.agent import Agent
from phi.tools.slack import SlackTools


slack_token = os.getenv("SLACK_TOKEN")
if not slack_token:
raise ValueError("SLACK_TOKEN not set")
slack_tools = SlackTools(token=slack_token)
slack_tools = SlackTools()

agent = Agent(tools=[slack_tools], show_tool_calls=True)

Expand Down
8 changes: 6 additions & 2 deletions phi/playground/playground.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ def __init__(
def get_router(self) -> APIRouter:
if self.agents:
return get_playground_router(self.agents)
else:
elif self.workflows:
return get_workflow_router(self.workflows)
else:
raise ValueError("Either agents or workflows must be provided.")

def get_async_router(self) -> APIRouter:
if self.agents:
return get_async_playground_router(self.agents)
else:
elif self.workflows:
return get_async_workflow_router(self.workflows)
else:
raise ValueError("Either agents or workflows must be provided.")

def get_app(self, use_async: bool = True, prefix: str = "/v1") -> FastAPI:
from starlette.middleware.cors import CORSMiddleware
Expand Down
8 changes: 6 additions & 2 deletions phi/playground/workflow_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def run_workflow(workflow_id: str, body: WorkflowRunRequest):
workflow.user_id = body.user_id
if workflow._run_return_type == "RunResponse":
return workflow.run(**body.input)
return StreamingResponse((r.model_dump_json() for r in workflow.run(**body.input)), media_type="text/event-stream")
return StreamingResponse(
(r.model_dump_json() for r in workflow.run(**body.input)), media_type="text/event-stream"
)

@workflow_router.post("/workflow/{workflow_id}/session/all")
def get_all_workflow_sessions(workflow_id: str, body: WorkflowSessionsRequest):
Expand Down Expand Up @@ -153,7 +155,9 @@ async def run_workflow(workflow_id: str, body: WorkflowRunRequest):
workflow.user_id = body.user_id
if workflow._run_return_type == "RunResponse":
return workflow.run(**body.input)
return StreamingResponse((r.model_dump_json() for r in workflow.run(**body.input)), media_type="text/event-stream")
return StreamingResponse(
(r.model_dump_json() for r in workflow.run(**body.input)), media_type="text/event-stream"
)

@workflow_router.post("/workflow/{workflow_id}/session/all")
async def get_all_workflow_sessions(workflow_id: str, body: WorkflowSessionsRequest):
Expand Down
12 changes: 10 additions & 2 deletions phi/tools/slack.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import json
from typing import Optional

from phi.tools.toolkit import Toolkit
from phi.utils.log import logger
Expand All @@ -12,10 +14,16 @@

class SlackTools(Toolkit):
def __init__(
self, token: str, send_message: bool = True, list_channels: bool = True, get_channel_history: bool = True
self,
send_message: bool = True,
list_channels: bool = True,
get_channel_history: bool = True,
):
super().__init__(name="slack")
self.client = WebClient(token=token)
self.token: Optional[str] = os.getenv("SLACK_TOKEN")
if self.token is None or self.token == "":
raise ValueError("SLACK_TOKEN is not set")
self.client = WebClient(token=self.token)
if send_message:
self.register(self.send_message)
if list_channels:
Expand Down
9 changes: 8 additions & 1 deletion phi/workflow/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,18 @@ def log_workflow_session(self):
logger.debug(f"*********** Logging WorkflowSession: {self.session_id} ***********")

def rename_session(self, session_id: str, name: str):
if self.storage is None:
raise ValueError("Storage is not set")
workflow_session = self.storage.read(session_id)
if workflow_session is None:
raise Exception(f"WorkflowSession not found: {session_id}")
workflow_session.session_data["session_name"] = name
if workflow_session.session_data is not None:
workflow_session.session_data["session_name"] = name
else:
workflow_session.session_data = {"session_name": name}
self.storage.upsert(workflow_session)

def delete_session(self, session_id: str):
if self.storage is None:
raise ValueError("Storage is not set")
self.storage.delete_session(session_id)

1 comment on commit c787e86

@ventz
Copy link

@ventz ventz commented on c787e86 Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manthanguptaa It looks like the documentation (https://docs.phidata.com/tools/slack) has a "token=" toolkit param, but it seems to be missing from the code?

Would it be possible to add it to the initializer of SlackTools()?

Please sign in to comment.