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

OpenAI error handling #1232

Merged
merged 24 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion gui/pages/Content/Agents/AgentWorkspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ export default function AgentWorkspace({env, agentId, agentName, selectedView, a
{agentExecutions && agentExecutions.length > 1 && <li className="dropdown_item" onClick={() => {
updateRunStatus("TERMINATED")
}}>Delete Run</li>}
{agentExecutions && selectedRun && (selectedRun.status === 'CREATED' || selectedRun.status === 'PAUSED' || selectedRun.status === 'RUNNING' || agentExecutions.length > 1) && <div className={styles.dropdown_separator}/>}
{agentExecutions && selectedRun && (selectedRun.status === 'CREATED' || selectedRun.status === 'PAUSED' || selectedRun.status === 'RUNNING' || agentExecutions.length > 1 || selectedRun.status === 'ERROR_PAUSED') && <div className={styles.dropdown_separator}/>}
<li className="dropdown_item" onClick={() => saveAgentTemplate()}>Save as Template</li>
{agent && env === 'PROD' &&
<li className="dropdown_item" onClick={() => {
Expand Down
1 change: 1 addition & 0 deletions superagi/agent/agent_iteration_step_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def execute_step(self):
agent_feed = AgentExecutionFeed(agent_execution_id=self.agent_execution_id, agent_id=self.agent_id, role="system", feed="", error_message=response['message'], feed_group_id=execution.current_feed_group_id)
self.session.add(agent_feed)
self.session.commit()

if 'content' not in response or response['content'] is None:
raise RuntimeError(f"Failed to get response from llm")

Expand Down
6 changes: 6 additions & 0 deletions superagi/agent/agent_message_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ def _build_ltm_summary(self, past_messages, output_token_limit) -> str:
{"role": "assistant", "content": ltm_prompt}]
ltm_summary = self.llm.chat_completion(msgs)

execution = self.session.query(AgentExecution).filter(AgentExecution.id == self.agent_execution_id).first()
if 'error' in ltm_summary and ltm_summary['message'] is not None:
agent_feed = AgentExecutionFeed(agent_execution_id=self.agent_execution_id, agent_id=self.agent_id, role="system", feed="", error_message=ltm_summary['message'], feed_group_id=execution.current_feed_group_id)
self.session.add(agent_feed)
self.session.commit()

execution = AgentExecution(id=self.agent_execution_id)
agent_execution_configs = {"ltm_summary": ltm_summary["content"]}
AgentExecutionConfiguration.add_or_update_agent_execution_config(session=self.session, execution=execution,
Expand Down
12 changes: 12 additions & 0 deletions superagi/agent/agent_tool_step_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ def _process_input_instruction(self, agent_config, agent_execution_config, step_
# print(messages)
current_tokens = TokenCounter.count_message_tokens(messages, self.llm.get_model())
response = self.llm.chat_completion(messages, TokenCounter(session=self.session, organisation_id=self.organisation.id).token_limit(self.llm.get_model()) - current_tokens)

execution = self.session.query(AgentExecution).filter(AgentExecution.id == self.agent_execution_id).first()
if 'error' in response and response['message'] is not None:
agent_feed = AgentExecutionFeed(agent_execution_id=self.agent_execution_id, agent_id=self.agent_id, role="system", feed="", error_message=response['message'], feed_group_id=execution.current_feed_group_id)
self.session.add(agent_feed)
self.session.commit()
# ModelsHelper(session=self.session, organisation_id=organisation.id).create_call_log(execution.name,agent_config['agent_id'],response['response'].usage.total_tokens,json.loads(response['content'])['tool']['name'],agent_config['model'])
if 'content' not in response or response['content'] is None:
raise RuntimeError(f"Failed to get response from llm")
Expand Down Expand Up @@ -137,6 +143,12 @@ def _process_output_instruction(self, final_response: str, step_tool: AgentWorkf
current_tokens = TokenCounter.count_message_tokens(messages, self.llm.get_model())
response = self.llm.chat_completion(messages,
TokenCounter(session=self.session, organisation_id=self.organisation.id).token_limit(self.llm.get_model()) - current_tokens)
execution = self.session.query(AgentExecution).filter(AgentExecution.id == self.agent_execution_id).first()
if 'error' in response and response['message'] is not None:
agent_feed = AgentExecutionFeed(agent_execution_id=self.agent_execution_id, agent_id=self.agent_id, role="system", feed="", error_message=response['message'], feed_group_id=execution.current_feed_group_id)
self.session.add(agent_feed)
self.session.commit()

if 'content' not in response or response['content'] is None:
raise RuntimeError(f"ToolWorkflowStepHandler: Failed to get output response from llm")
total_tokens = current_tokens + TokenCounter.count_message_tokens(response, self.llm.get_model())
Expand Down
5 changes: 5 additions & 0 deletions superagi/agent/queue_step_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def _process_input_instruction(self, step_tool):
completion_prompt=step_tool.completion_prompt)
current_tokens = TokenCounter.count_message_tokens(messages, self.llm.get_model())
response = self.llm.chat_completion(messages, TokenCounter(session=self.session, organisation_id=self.organisation.id).token_limit(self.llm.get_model()) - current_tokens)
execution = self.session.query(AgentExecution).filter(AgentExecution.id == self.agent_execution_id).first()
if 'error' in response and response['message'] is not None:
agent_feed = AgentExecutionFeed(agent_execution_id=self.agent_execution_id, agent_id=self.agent_id, role="system", feed="", error_message=response['message'], feed_group_id=execution.current_feed_group_id)
self.session.add(agent_feed)
self.session.commit()
if 'content' not in response or response['content'] is None:
raise RuntimeError(f"Failed to get response from llm")
total_tokens = current_tokens + TokenCounter.count_message_tokens(response, self.llm.get_model())
Expand Down
10 changes: 6 additions & 4 deletions superagi/controllers/agent_execution_feed.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import asyncio
from datetime import datetime
import time
from typing import Optional

from fastapi import APIRouter
from fastapi import APIRouter, BackgroundTasks
from fastapi import HTTPException, Depends
from fastapi_jwt_auth import AuthJWT
from fastapi_sqlalchemy import db
Expand Down Expand Up @@ -145,7 +147,7 @@ def update_agent_execution_feed(agent_execution_feed_id: int,

@router.get("/get/execution/{agent_execution_id}")
def get_agent_execution_feed(agent_execution_id: int,
Authorize: AuthJWT = Depends(check_auth)):
Authorize: AuthJWT = Depends(check_auth)):
"""
Get agent execution feed with other execution details.

Expand All @@ -171,7 +173,8 @@ def get_agent_execution_feed(agent_execution_id: int,
if (agent_execution.last_shown_error_id is None) or (feed.id > agent_execution.last_shown_error_id):
#error occured
final_feeds.clear()
final_feeds.append(feed.error_message)
dict = {"error": feed.error_message}
final_feeds.append(dict)
agent_execution.last_shown_error_id = feed.id
agent_execution.status = "ERROR_PAUSED"
db.session.commit()
Expand All @@ -197,7 +200,6 @@ def get_agent_execution_feed(agent_execution_id: int,
} for permission in execution_permissions
]
logger.info(final_feeds)
logger.info(agent_execution_id)
return {
"status": agent_execution.status,
"feeds": final_feeds,
Expand Down
9 changes: 9 additions & 0 deletions superagi/tools/code/improve_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
from superagi.helper.token_counter import TokenCounter
from superagi.lib.logger import logger
from superagi.llms.base_llm import BaseLlm
from superagi.models.agent_execution import AgentExecution
from superagi.models.agent_execution_feed import AgentExecutionFeed
from superagi.resource_manager.file_manager import FileManager
from superagi.tools.base_tool import BaseTool
from superagi.tools.tool_response_query_manager import ToolResponseQueryManager
from fastapi_sqlalchemy import db


class ImproveCodeSchema(BaseModel):
Expand All @@ -29,6 +32,7 @@ class ImproveCodeTool(BaseTool):
"""
llm: Optional[BaseLlm] = None
agent_id: int = None
agent_execution_id: int = None
name = "ImproveCodeTool"
description = (
"This tool improves the generated code."
Expand Down Expand Up @@ -71,6 +75,11 @@ def _execute(self) -> str:

# Use LLM to generate improved code
result = self.llm.chat_completion([{'role': 'system', 'content': prompt}])
execution = db.session.query(AgentExecution).filter(AgentExecution.id == self.agent_execution_id).first()
if result is not None and 'error' in result and result['message'] is not None:
agent_feed = AgentExecutionFeed(agent_execution_id=self.agent_execution_id, agent_id=self.agent_id, role="system", feed="", error_message=result['message'], feed_group_id=execution.current_feed_group_id)
db.session.add(agent_feed)
db.session.commit()

# Extract the response first
response = result.get('response')
Expand Down
9 changes: 9 additions & 0 deletions superagi/tools/code/write_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
from superagi.helper.token_counter import TokenCounter
from superagi.lib.logger import logger
from superagi.llms.base_llm import BaseLlm
from superagi.models.agent_execution import AgentExecution
from superagi.models.agent_execution_feed import AgentExecutionFeed
from superagi.resource_manager.file_manager import FileManager
from superagi.tools.base_tool import BaseTool
from superagi.tools.tool_response_query_manager import ToolResponseQueryManager
from superagi.models.agent import Agent
from fastapi_sqlalchemy import db

class CodingSchema(BaseModel):
code_description: str = Field(
Expand All @@ -34,6 +37,7 @@ class CodingTool(BaseTool):
"""
llm: Optional[BaseLlm] = None
agent_id: int = None
agent_execution_id: int = None
name = "CodingTool"
description = (
"You will get instructions for code to write. You will write a very long answer. "
Expand Down Expand Up @@ -75,6 +79,11 @@ def _execute(self, code_description: str) -> str:
token_limit = TokenCounter(session=self.toolkit_config.session, organisation_id=organisation.id).token_limit(self.llm.get_model())

result = self.llm.chat_completion(messages, max_tokens=(token_limit - total_tokens - 100))
execution = db.session.query(AgentExecution).filter(AgentExecution.id == self.agent_execution_id).first()
if 'error' in result and result['message'] is not None:
agent_feed = AgentExecutionFeed(agent_execution_id=self.agent_execution_id, agent_id=self.agent_id, role="system", feed="", error_message=result['message'], feed_group_id=execution.current_feed_group_id)
db.session.add(agent_feed)
db.session.commit()

# Get all filenames and corresponding code blocks
regex = r"(\S+?)\n```\S*\n(.+?)```"
Expand Down
9 changes: 9 additions & 0 deletions superagi/tools/code/write_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
from superagi.helper.token_counter import TokenCounter
from superagi.lib.logger import logger
from superagi.llms.base_llm import BaseLlm
from superagi.models.agent_execution import AgentExecution
from superagi.models.agent_execution_feed import AgentExecutionFeed
from superagi.resource_manager.file_manager import FileManager
from superagi.tools.base_tool import BaseTool
from superagi.models.agent import Agent
from fastapi_sqlalchemy import db

class WriteSpecSchema(BaseModel):
task_description: str = Field(
Expand Down Expand Up @@ -37,6 +40,7 @@ class WriteSpecTool(BaseTool):
"""
llm: Optional[BaseLlm] = None
agent_id: int = None
agent_execution_id: int = None
name = "WriteSpecTool"
description = (
"A tool to write the spec of a program."
Expand Down Expand Up @@ -69,6 +73,11 @@ def _execute(self, task_description: str, spec_file_name: str) -> str:
token_limit = TokenCounter(session=self.toolkit_config.session, organisation_id=organisation.id).token_limit(self.llm.get_model())

result = self.llm.chat_completion(messages, max_tokens=(token_limit - total_tokens - 100))
execution = db.session.query(AgentExecution).filter(AgentExecution.id == self.agent_execution_id).first()
if 'error' in result and result['message'] is not None:
agent_feed = AgentExecutionFeed(agent_execution_id=self.agent_execution_id, agent_id=self.agent_id, role="system", feed="", error_message=result['message'], feed_group_id=execution.current_feed_group_id)
db.session.add(agent_feed)
db.session.commit()

# Save the specification to a file
write_result = self.resource_manager.write_file(spec_file_name, result["content"])
Expand Down
9 changes: 9 additions & 0 deletions superagi/tools/code/write_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
from superagi.helper.token_counter import TokenCounter
from superagi.lib.logger import logger
from superagi.llms.base_llm import BaseLlm
from superagi.models.agent_execution import AgentExecution
from superagi.models.agent_execution_feed import AgentExecutionFeed
from superagi.resource_manager.file_manager import FileManager
from superagi.tools.base_tool import BaseTool
from superagi.tools.tool_response_query_manager import ToolResponseQueryManager
from superagi.models.agent import Agent
from fastapi_sqlalchemy import db

class WriteTestSchema(BaseModel):
test_description: str = Field(
Expand All @@ -38,6 +41,7 @@ class WriteTestTool(BaseTool):
"""
llm: Optional[BaseLlm] = None
agent_id: int = None
agent_execution_id: int = None
name = "WriteTestTool"
description = (
"You are a super smart developer using Test Driven Development to write tests according to a specification.\n"
Expand Down Expand Up @@ -86,6 +90,11 @@ def _execute(self, test_description: str, test_file_name: str) -> str:
token_limit = TokenCounter(session=self.toolkit_config.session, organisation_id=organisation.id).token_limit(self.llm.get_model())

result = self.llm.chat_completion(messages, max_tokens=(token_limit - total_tokens - 100))
execution = db.session.query(AgentExecution).filter(AgentExecution.id == self.agent_execution_id).first()
if 'error' in result and result['message'] is not None:
agent_feed = AgentExecutionFeed(agent_execution_id=self.agent_execution_id, agent_id=self.agent_id, role="system", feed="", error_message=result['message'], feed_group_id=execution.current_feed_group_id)
db.session.add(agent_feed)
db.session.commit()

regex = r"(\S+?)\n```\S*\n(.+?)```"
matches = re.finditer(regex, result["content"], re.DOTALL)
Expand Down
10 changes: 10 additions & 0 deletions superagi/tools/duck_duck_go/duck_duck_go_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
from itertools import islice
from superagi.helper.token_counter import TokenCounter
from superagi.llms.base_llm import BaseLlm
from superagi.models.agent_execution import AgentExecution
from superagi.models.agent_execution_feed import AgentExecutionFeed
from superagi.tools.base_tool import BaseTool
from superagi.helper.webpage_extractor import WebpageExtractor
from fastapi_sqlalchemy import db

#Const variables
DUCKDUCKGO_MAX_ATTEMPTS = 3
Expand All @@ -33,6 +36,8 @@ class DuckDuckGoSearchTool(BaseTool):
"""
llm: Optional[BaseLlm] = None
name = "DuckDuckGoSearch"
agent_id: int = None
agent_execution_id: int = None
description = (
"A tool for performing a DuckDuckGo search and extracting snippets and webpages."
"Input should be a search query."
Expand Down Expand Up @@ -169,4 +174,9 @@ def summarise_result(self, query, snippets):

messages = [{"role": "system", "content": summarize_prompt}]
result = self.llm.chat_completion(messages, max_tokens=self.max_token_limit)
execution = db.session.query(AgentExecution).filter(AgentExecution.id == self.agent_execution_id).first()
if 'error' in result and result['message'] is not None:
agent_feed = AgentExecutionFeed(agent_execution_id=self.agent_execution_id, agent_id=self.agent_id, role="system", feed="", error_message=result['message'], feed_group_id=execution.current_feed_group_id)
db.session.add(agent_feed)
db.session.commit()
return result["content"]
8 changes: 8 additions & 0 deletions superagi/tools/github/review_pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
from superagi.helper.token_counter import TokenCounter
from superagi.llms.base_llm import BaseLlm
from superagi.models.agent import Agent
from superagi.models.agent_execution import AgentExecution
from superagi.models.agent_execution_feed import AgentExecutionFeed
from superagi.tools.base_tool import BaseTool
from fastapi_sqlalchemy import db


class GithubReviewPullRequestSchema(BaseModel):
Expand Down Expand Up @@ -87,6 +90,11 @@ def run_code_review(self, github_helper, content, latest_commit_id, organisation
token_limit = TokenCounter(session=self.toolkit_config.session,
organisation_id=organisation.id).token_limit(self.llm.get_model())
result = self.llm.chat_completion(messages, max_tokens=(token_limit - total_tokens - 100))
execution = db.session.query(AgentExecution).filter(AgentExecution.id == self.agent_execution_id).first()
if 'error' in result and result['message'] is not None:
agent_feed = AgentExecutionFeed(agent_execution_id=self.agent_execution_id, agent_id=self.agent_id, role="system", feed="", error_message=result['message'], feed_group_id=execution.current_feed_group_id)
db.session.add(agent_feed)
db.session.commit()
response = result["content"]
if response.startswith("```") and response.endswith("```"):
response = "```".join(response.split("```")[1:-1])
Expand Down
11 changes: 10 additions & 1 deletion superagi/tools/google_search/google_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
from superagi.helper.google_search import GoogleSearchWrap
from superagi.helper.token_counter import TokenCounter
from superagi.llms.base_llm import BaseLlm
from superagi.models.agent_execution import AgentExecution
from superagi.models.agent_execution_feed import AgentExecutionFeed
from superagi.tools.base_tool import BaseTool

from fastapi_sqlalchemy import db

class GoogleSearchSchema(BaseModel):
query: str = Field(
Expand All @@ -26,6 +28,8 @@ class GoogleSearchTool(BaseTool):
"""
llm: Optional[BaseLlm] = None
name = "GoogleSearch"
agent_id: int = None
agent_execution_id: int = None
description = (
"A tool for performing a Google search and extracting snippets and webpages."
"Input should be a search query."
Expand Down Expand Up @@ -88,4 +92,9 @@ def summarise_result(self, query, snippets):

messages = [{"role": "system", "content": summarize_prompt}]
result = self.llm.chat_completion(messages, max_tokens=self.max_token_limit)
execution = db.session.query(AgentExecution).filter(AgentExecution.id == self.agent_execution_id).first()
if 'error' in result and result['message'] is not None:
agent_feed = AgentExecutionFeed(agent_execution_id=self.agent_execution_id, agent_id=self.agent_id, role="system", feed="", error_message=result['message'], feed_group_id=execution.current_feed_group_id)
db.session.add(agent_feed)
db.session.commit()
return result["content"]
Loading