Spaces:
Runtime error
Runtime error
File size: 5,054 Bytes
4962437 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
from typing import Dict, List
from langchain.base_language import BaseLanguageModel
from langchain.tools.base import BaseTool
from langchain_experimental.autonomous_agents.hugginggpt.repsonse_generator import (
load_response_generator,
)
from langchain_experimental.autonomous_agents.hugginggpt.task_executor import (
TaskExecutor,
)
from langchain_experimental.autonomous_agents.hugginggpt.task_planner import (
load_chat_planner,
)
from transformers import load_tool
from swarms.agents.message import Message
class Step:
def __init__(
self,
task: str,
id: int,
dep: List[int],
args: Dict[str, str],
tool: BaseTool
):
self.task = task
self.id = id
self.dep = dep
self.args = args
self.tool = tool
class Plan:
def __init__(
self,
steps: List[Step]
):
self.steps = steps
def __str__(self) -> str:
return str([str(step) for step in self.steps])
def __repr(self) -> str:
return str(self)
class OmniModalAgent:
"""
OmniModalAgent
LLM -> Plans -> Tasks -> Tools -> Response
Architecture:
1. LLM: Language Model
2. Chat Planner: Plans
3. Task Executor: Tasks
4. Tools: Tools
Args:
llm (BaseLanguageModel): Language Model
tools (List[BaseTool]): List of tools
Returns:
str: response
Usage:
from swarms import OmniModalAgent, OpenAIChat,
llm = OpenAIChat()
agent = OmniModalAgent(llm)
response = agent.run("Hello, how are you? Create an image of how your are doing!")
"""
def __init__(
self,
llm: BaseLanguageModel,
# tools: List[BaseTool]
):
self.llm = llm
print("Loading tools...")
self.tools = [
load_tool(tool_name)
for tool_name in [
"document-question-answering",
"image-captioning",
"image-question-answering",
"image-segmentation",
"speech-to-text",
"summarization",
"text-classification",
"text-question-answering",
"translation",
"huggingface-tools/text-to-image",
"huggingface-tools/text-to-video",
"text-to-speech",
"huggingface-tools/text-download",
"huggingface-tools/image-transformation",
]
]
self.chat_planner = load_chat_planner(llm)
self.response_generator = load_response_generator(llm)
# self.task_executor = TaskExecutor
self.history = []
def run(
self,
input: str
) -> str:
"""Run the OmniAgent"""
plan = self.chat_planner.plan(
inputs={
"input": input,
"hf_tools": self.tools,
}
)
self.task_executor = TaskExecutor(plan)
self.task_executor.run()
response = self.response_generator.generate(
{"task_execution": self.task_executor}
)
return response
def chat(
self,
msg: str = None,
streaming: bool = False
):
"""
Run chat
Args:
msg (str, optional): Message to send to the agent. Defaults to None.
language (str, optional): Language to use. Defaults to None.
streaming (bool, optional): Whether to stream the response. Defaults to False.
Returns:
str: Response from the agent
Usage:
--------------
agent = MultiModalAgent()
agent.chat("Hello")
"""
#add users message to the history
self.history.append(
Message(
"User",
msg
)
)
#process msg
try:
response = self.agent.run(msg)
#add agent's response to the history
self.history.append(
Message(
"Agent",
response
)
)
#if streaming is = True
if streaming:
return self._stream_response(response)
else:
response
except Exception as error:
error_message = f"Error processing message: {str(error)}"
#add error to history
self.history.append(
Message(
"Agent",
error_message
)
)
return error_message
def _stream_response(
self,
response: str = None
):
"""
Yield the response token by token (word by word)
Usage:
--------------
for token in _stream_response(response):
print(token)
"""
for token in response.split():
yield token
|