Spaces:
Runtime error
Runtime error
Valian
commited on
Commit
·
e97538a
1
Parent(s):
d871fbf
verbosity might be set directly in session, fallback to global settings
Browse files
codeinterpreterapi/session.py
CHANGED
|
@@ -20,14 +20,15 @@ from codeinterpreterapi.chains.remove_download_link import remove_download_link
|
|
| 20 |
|
| 21 |
|
| 22 |
class CodeInterpreterSession:
|
| 23 |
-
def __init__(self, model=None, openai_api_key=None) -> None:
|
| 24 |
self.codebox = CodeBox()
|
| 25 |
self.tools: list[StructuredTool] = self._tools()
|
| 26 |
self.llm: BaseChatModel = self._llm(model, openai_api_key)
|
| 27 |
self.agent_executor: AgentExecutor = self._agent_executor()
|
| 28 |
self.input_files: list[File] = []
|
| 29 |
self.output_files: list[File] = []
|
| 30 |
-
|
|
|
|
| 31 |
async def astart(self) -> None:
|
| 32 |
await self.codebox.astart()
|
| 33 |
|
|
@@ -78,13 +79,13 @@ class CodeInterpreterSession:
|
|
| 78 |
callbacks=[CodeCallbackHandler(self)],
|
| 79 |
max_iterations=9,
|
| 80 |
tools=self.tools,
|
| 81 |
-
verbose=
|
| 82 |
memory=ConversationBufferMemory(memory_key="memory", return_messages=True),
|
| 83 |
)
|
| 84 |
|
| 85 |
async def show_code(self, code: str) -> None:
|
| 86 |
"""Callback function to show code to the user."""
|
| 87 |
-
if
|
| 88 |
print(code)
|
| 89 |
|
| 90 |
def run_handler(self, code: str):
|
|
@@ -113,7 +114,7 @@ class CodeInterpreterSession:
|
|
| 113 |
return f"{package.group(1)} was missing but got installed now. Please try again."
|
| 114 |
else: pass
|
| 115 |
# TODO: preanalyze error to optimize next code generation
|
| 116 |
-
if
|
| 117 |
print("Error:", output.content)
|
| 118 |
|
| 119 |
elif modifications := await get_file_modifications(code, self.llm):
|
|
@@ -170,7 +171,7 @@ class CodeInterpreterSession:
|
|
| 170 |
response = await self.agent_executor.arun(input=user_request.content)
|
| 171 |
return await self.output_handler(response)
|
| 172 |
except Exception as e:
|
| 173 |
-
if
|
| 174 |
import traceback
|
| 175 |
|
| 176 |
traceback.print_exc()
|
|
@@ -186,10 +187,10 @@ class CodeInterpreterSession:
|
|
| 186 |
|
| 187 |
async def is_running(self) -> bool:
|
| 188 |
return await self.codebox.astatus() == "running"
|
| 189 |
-
|
| 190 |
async def astop(self) -> None:
|
| 191 |
await self.codebox.astop()
|
| 192 |
-
|
| 193 |
async def __aenter__(self) -> "CodeInterpreterSession":
|
| 194 |
await self.astart()
|
| 195 |
return self
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
class CodeInterpreterSession:
|
| 23 |
+
def __init__(self, model=None, openai_api_key=None, verbose=None) -> None:
|
| 24 |
self.codebox = CodeBox()
|
| 25 |
self.tools: list[StructuredTool] = self._tools()
|
| 26 |
self.llm: BaseChatModel = self._llm(model, openai_api_key)
|
| 27 |
self.agent_executor: AgentExecutor = self._agent_executor()
|
| 28 |
self.input_files: list[File] = []
|
| 29 |
self.output_files: list[File] = []
|
| 30 |
+
self.verbose = verbose if verbose is not None else settings.VERBOSE
|
| 31 |
+
|
| 32 |
async def astart(self) -> None:
|
| 33 |
await self.codebox.astart()
|
| 34 |
|
|
|
|
| 79 |
callbacks=[CodeCallbackHandler(self)],
|
| 80 |
max_iterations=9,
|
| 81 |
tools=self.tools,
|
| 82 |
+
verbose=self.verbose,
|
| 83 |
memory=ConversationBufferMemory(memory_key="memory", return_messages=True),
|
| 84 |
)
|
| 85 |
|
| 86 |
async def show_code(self, code: str) -> None:
|
| 87 |
"""Callback function to show code to the user."""
|
| 88 |
+
if self.verbose:
|
| 89 |
print(code)
|
| 90 |
|
| 91 |
def run_handler(self, code: str):
|
|
|
|
| 114 |
return f"{package.group(1)} was missing but got installed now. Please try again."
|
| 115 |
else: pass
|
| 116 |
# TODO: preanalyze error to optimize next code generation
|
| 117 |
+
if self.verbose:
|
| 118 |
print("Error:", output.content)
|
| 119 |
|
| 120 |
elif modifications := await get_file_modifications(code, self.llm):
|
|
|
|
| 171 |
response = await self.agent_executor.arun(input=user_request.content)
|
| 172 |
return await self.output_handler(response)
|
| 173 |
except Exception as e:
|
| 174 |
+
if self.verbose:
|
| 175 |
import traceback
|
| 176 |
|
| 177 |
traceback.print_exc()
|
|
|
|
| 187 |
|
| 188 |
async def is_running(self) -> bool:
|
| 189 |
return await self.codebox.astatus() == "running"
|
| 190 |
+
|
| 191 |
async def astop(self) -> None:
|
| 192 |
await self.codebox.astop()
|
| 193 |
+
|
| 194 |
async def __aenter__(self) -> "CodeInterpreterSession":
|
| 195 |
await self.astart()
|
| 196 |
return self
|