Spaces:
Runtime error
Runtime error
nananie143
commited on
Commit
·
c9a9197
1
Parent(s):
bdc6438
Fixed agent output parsing and async execution
Browse files
app.py
CHANGED
@@ -13,7 +13,9 @@ import tempfile
|
|
13 |
import shutil
|
14 |
import gradio as gr
|
15 |
import networkx as nx
|
16 |
-
from langchain.prompts import PromptTemplate
|
|
|
|
|
17 |
import torch
|
18 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
19 |
from langchain.llms import HuggingFacePipeline
|
@@ -70,27 +72,42 @@ def get_agent(agent_type):
|
|
70 |
"""Get or initialize an agent with the specified type."""
|
71 |
try:
|
72 |
llm = get_llm()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
return initialize_agent(
|
74 |
-
tools=
|
75 |
-
Tool(
|
76 |
-
name="Code Formatter",
|
77 |
-
func=lambda x: subprocess.run(["black", "-"], input=x.encode(), capture_output=True).stdout.decode(),
|
78 |
-
description="Formats code using Black.",
|
79 |
-
),
|
80 |
-
Tool(
|
81 |
-
name="API Generator",
|
82 |
-
func=lambda x: json.dumps({"endpoints": {"example": "POST - Example endpoint."}}),
|
83 |
-
description="Generates API details from code.",
|
84 |
-
),
|
85 |
-
Tool(
|
86 |
-
name="Task Decomposer",
|
87 |
-
func=lambda x: json.dumps({"tasks": ["Design UI", "Develop Backend", "Test App", "Deploy App"]}),
|
88 |
-
description="Breaks down app requirements into smaller tasks.",
|
89 |
-
),
|
90 |
-
],
|
91 |
llm=llm,
|
92 |
-
agent=
|
93 |
verbose=True,
|
|
|
|
|
|
|
94 |
)
|
95 |
except Exception as e:
|
96 |
logger.error(f"Failed to get agent: {str(e)}")
|
@@ -468,7 +485,7 @@ class AIFlow:
|
|
468 |
raise ValueError(f"Unknown agent role: {role}")
|
469 |
|
470 |
# Execute the agent's task
|
471 |
-
result = agent.run
|
472 |
|
473 |
# Log the execution
|
474 |
logger.info(f"Agent {role.value} completed task")
|
@@ -1017,7 +1034,7 @@ For any issues or questions, please refer to the documentation or create an issu
|
|
1017 |
agent = get_agent(role)
|
1018 |
|
1019 |
# Execute the agent's task
|
1020 |
-
result = await agent.
|
1021 |
|
1022 |
# Process and return the result
|
1023 |
return {"output": result}
|
|
|
13 |
import shutil
|
14 |
import gradio as gr
|
15 |
import networkx as nx
|
16 |
+
from langchain.prompts import PromptTemplate, MessagesPlaceholder
|
17 |
+
from langchain.memory import ConversationBufferMemory
|
18 |
+
from langchain.agents import Tool, initialize_agent, AgentType
|
19 |
import torch
|
20 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
21 |
from langchain.llms import HuggingFacePipeline
|
|
|
72 |
"""Get or initialize an agent with the specified type."""
|
73 |
try:
|
74 |
llm = get_llm()
|
75 |
+
|
76 |
+
tools = [
|
77 |
+
Tool(
|
78 |
+
name="Code Formatter",
|
79 |
+
func=lambda x: subprocess.run(["black", "-"], input=x.encode(), capture_output=True).stdout.decode(),
|
80 |
+
description="Formats code using Black.",
|
81 |
+
),
|
82 |
+
Tool(
|
83 |
+
name="API Generator",
|
84 |
+
func=lambda x: json.dumps({"endpoints": {"example": "POST - Example endpoint."}}),
|
85 |
+
description="Generates API details from code.",
|
86 |
+
),
|
87 |
+
Tool(
|
88 |
+
name="Task Decomposer",
|
89 |
+
func=lambda x: json.dumps({"tasks": ["Design UI", "Develop Backend", "Test App", "Deploy App"]}),
|
90 |
+
description="Breaks down app requirements into smaller tasks.",
|
91 |
+
),
|
92 |
+
]
|
93 |
+
|
94 |
+
memory = ConversationBufferMemory(
|
95 |
+
memory_key="chat_history",
|
96 |
+
return_messages=True
|
97 |
+
)
|
98 |
+
|
99 |
+
agent_kwargs = {
|
100 |
+
"extra_prompt_messages": [MessagesPlaceholder(variable_name="chat_history")],
|
101 |
+
}
|
102 |
+
|
103 |
return initialize_agent(
|
104 |
+
tools=tools,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
llm=llm,
|
106 |
+
agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,
|
107 |
verbose=True,
|
108 |
+
memory=memory,
|
109 |
+
agent_kwargs=agent_kwargs,
|
110 |
+
handle_parsing_errors=True,
|
111 |
)
|
112 |
except Exception as e:
|
113 |
logger.error(f"Failed to get agent: {str(e)}")
|
|
|
485 |
raise ValueError(f"Unknown agent role: {role}")
|
486 |
|
487 |
# Execute the agent's task
|
488 |
+
result = await asyncio.to_thread(agent.run, prompt)
|
489 |
|
490 |
# Log the execution
|
491 |
logger.info(f"Agent {role.value} completed task")
|
|
|
1034 |
agent = get_agent(role)
|
1035 |
|
1036 |
# Execute the agent's task
|
1037 |
+
result = await asyncio.to_thread(agent.run, prompt)
|
1038 |
|
1039 |
# Process and return the result
|
1040 |
return {"output": result}
|