Dominic commited on
Commit
44422c1
·
unverified ·
2 Parent(s): d871fbf a31142b

Merge pull request #21 from Valian/verbosity-in-session

Browse files
Files changed (1) hide show
  1. codeinterpreterapi/session.py +12 -12
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
 
@@ -51,10 +52,9 @@ class CodeInterpreterSession:
51
  model = "gpt-4"
52
 
53
  if openai_api_key is None:
54
- if settings.OPENAI_API_KEY is None:
55
- raise ValueError("OpenAI API key missing.")
56
- else:
57
- openai_api_key = settings.OPENAI_API_KEY
58
 
59
  return ChatOpenAI(
60
  temperature=0.03,
@@ -78,13 +78,13 @@ class CodeInterpreterSession:
78
  callbacks=[CodeCallbackHandler(self)],
79
  max_iterations=9,
80
  tools=self.tools,
81
- verbose=settings.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 settings.VERBOSE:
88
  print(code)
89
 
90
  def run_handler(self, code: str):
@@ -113,7 +113,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 settings.VERBOSE:
117
  print("Error:", output.content)
118
 
119
  elif modifications := await get_file_modifications(code, self.llm):
@@ -170,7 +170,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 settings.VERBOSE:
174
  import traceback
175
 
176
  traceback.print_exc()
@@ -186,10 +186,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=settings.OPENAI_API_KEY, verbose=settings.VERBOSE) -> None:
24
  self.codebox = CodeBox()
25
+ self.verbose = verbose
26
  self.tools: list[StructuredTool] = self._tools()
27
  self.llm: BaseChatModel = self._llm(model, openai_api_key)
28
  self.agent_executor: AgentExecutor = self._agent_executor()
29
  self.input_files: list[File] = []
30
  self.output_files: list[File] = []
31
+
32
  async def astart(self) -> None:
33
  await self.codebox.astart()
34
 
 
52
  model = "gpt-4"
53
 
54
  if openai_api_key is None:
55
+ raise ValueError(
56
+ "OpenAI API key missing. Set OPENAI_API_KEY env variable or pass `openai_api_key` to session."
57
+ )
 
58
 
59
  return ChatOpenAI(
60
  temperature=0.03,
 
78
  callbacks=[CodeCallbackHandler(self)],
79
  max_iterations=9,
80
  tools=self.tools,
81
+ verbose=self.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 self.verbose:
88
  print(code)
89
 
90
  def run_handler(self, code: str):
 
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 self.verbose:
117
  print("Error:", output.content)
118
 
119
  elif modifications := await get_file_modifications(code, self.llm):
 
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 self.verbose:
174
  import traceback
175
 
176
  traceback.print_exc()
 
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