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

Ankit/add composio agent #71

Merged
merged 5 commits into from
Nov 20, 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
7 changes: 6 additions & 1 deletion backend/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ SLACK_CHANNEL_NAME=
SLACK_BOT_TOKEN=

# Dubbing AGENT
ELEVENLABS_API_KEY=
ELEVENLABS_API_KEY=

# Composio Agent
# https://composio.dev/tools
COMPOSIO_API_KEY=
COMPOSIO_APPS=["HACKERNEWS"]
87 changes: 87 additions & 0 deletions backend/director/agents/composio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os
import logging
import json

from director.agents.base import BaseAgent, AgentResponse, AgentStatus
from director.core.session import (
Session,
ContextMessage,
RoleTypes,
TextContent,
MsgStatus,
)

from director.tools.composio_tool import composio_tool
from director.llm.openai import OpenAI
from director.llm.base import LLMResponseStatus

logger = logging.getLogger(__name__)

COMPOSIO_PARAMETERS = {
"type": "object",
"properties": {
"task": {
"type": "string",
"description": "The task to perform",
},
},
"required": ["task"],
}


class ComposioAgent(BaseAgent):
def __init__(self, session: Session, **kwargs):
self.agent_name = "composio"
self.description = f'The Composio agent is used to run tasks related to apps like {os.getenv("COMPOSIO_APPS")} '
self.parameters = COMPOSIO_PARAMETERS
self.llm = OpenAI()
super().__init__(session=session, **kwargs)

def run(self, task: str, *args, **kwargs) -> AgentResponse:
"""
Run the composio with the given task.

:return: The response containing information about the sample processing operation.
:rtype: AgentResponse
"""
try:
self.output_message.actions.append("Running task..")
self.output_message.push_update()

text_content = TextContent(
agent_name=self.agent_name,
status=MsgStatus.progress,
status_message="Running task..",
)
self.output_message.content.append(text_content)
self.output_message.push_update()

composio_response = composio_tool(task=task)
llm_prompt = (
ashish-spext marked this conversation as resolved.
Show resolved Hide resolved
f"User has asked to run a task: {task} in Composio. \n"
"Format the following reponse into text.\n"
"Give the output which can be directly send to use \n"
"Don't add any extra text \n"
f"{json.dumps(composio_response)}"
)
composio_response = ContextMessage(content=llm_prompt, role=RoleTypes.user)
llm_response = self.llm.chat_completions([composio_response.to_llm_msg()])
if llm_response.status == LLMResponseStatus.ERROR:
raise Exception(f"LLM Failed with error {llm_response.content}")

text_content.text = llm_response.content
text_content.status = MsgStatus.success
text_content.status_message = "Here is the response from Composio"

except Exception as e:
logger.exception(f"Error in {self.agent_name}")
text_content.status = MsgStatus.error
self.output_message.publish()
error_message = f"Agent failed with error {e}"
return AgentResponse(status=AgentStatus.ERROR, message=error_message)

return AgentResponse(
status=AgentStatus.SUCCESS,
message=f"Agent {self.name} completed successfully.",
data={"composio_response": composio_response},
)
2 changes: 2 additions & 0 deletions backend/director/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from director.agents.subtitle import SubtitleAgent
from director.agents.slack_agent import SlackAgent
from director.agents.dubbing import DubbingAgent
from director.agents.composio import ComposioAgent


from director.core.session import Session, InputMessage, MsgStatus
Expand Down Expand Up @@ -50,6 +51,7 @@ def __init__(self, db, **kwargs):
SubtitleAgent,
SlackAgent,
DubbingAgent,
ComposioAgent,
]

def add_videodb_state(self, session):
Expand Down
30 changes: 30 additions & 0 deletions backend/director/tools/composio_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os
import json

from director.llm.openai import OpenAIChatModel


def composio_tool(task: str):
from composio_openai import ComposioToolSet
from openai import OpenAI

openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

toolset = ComposioToolSet(api_key=os.getenv("COMPOSIO_API_KEY"))
tools = toolset.get_tools(apps=json.loads(os.getenv("COMPOSIO_APPS")))
print(tools)

response = openai_client.chat.completions.create(
model=OpenAIChatModel.GPT4o,
tools=tools,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": task},
],
)

composio_response = toolset.handle_tool_calls(response=response)
if composio_response:
return composio_response
else:
return response.choices[0].message.content or ""
1 change: 1 addition & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-e .
anthropic==0.37.1
composio_openai==0.5.42
elevenlabs==1.9.0
Flask==3.0.3
Flask-SocketIO==5.3.6
Expand Down