Skip to content

Commit

Permalink
Code interpreter response format + grooming
Browse files Browse the repository at this point in the history
  • Loading branch information
kgrofelnik committed Mar 28, 2024
1 parent 5b53a62 commit 5a2ca49
Show file tree
Hide file tree
Showing 12 changed files with 13 additions and 18 deletions.
4 changes: 2 additions & 2 deletions oracles/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ async def _call_function(function_call: FunctionCall, semaphore: Semaphore):
python_interpreter_result = await python_interpreter_use_case.execute(
formatted_input
)
response = python_interpreter_result.stdout
error_message = python_interpreter_result.stderr
response = python_interpreter_result.output
error_message = python_interpreter_result.error
else:
response = ""
error_message = f"Unknown function '{function_call.function_type}'"
Expand Down
4 changes: 2 additions & 2 deletions oracles/src/domain/tools/code_interpreter/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

@dataclass
class PythonInterpreterResult:
stdout: str
stderr: str
output: str
error: str
exit_code: int
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import re
import json
import base64
import aiohttp
import settings
from typing import Optional
from src.domain.tools.code_interpreter.entities import PythonInterpreterResult


async def _interpret_code(code: str) -> Optional[PythonInterpreterResult]:
async def _interpret_code(code: str) -> PythonInterpreterResult:
try:
async with aiohttp.ClientSession() as session:
async with session.post(
Expand All @@ -23,21 +23,16 @@ async def _interpret_code(code: str) -> Optional[PythonInterpreterResult]:
) as response:
response.raise_for_status()
data = await response.json()
stdout = base64.b64decode(data["stdoutBasesixtyfour"]).decode() if data["stdoutBasesixtyfour"] else ""
stderr = base64.b64decode(data["stderrBasesixtyfour"]).decode() if data["stderrBasesixtyfour"] else ""
output = json.dumps({"stdout": stdout, "stderr": stderr})
return PythonInterpreterResult(
stdout=(
base64.b64decode(data["stdoutBasesixtyfour"]).decode()
if data["stdoutBasesixtyfour"]
else ""
),
stderr=(
base64.b64decode(data["stderrBasesixtyfour"]).decode()
if data["stderrBasesixtyfour"]
else ""
),
output=output,
error="",
exit_code=data["exitCode"],
)
except Exception as e:
return PythonInterpreterResult(stdout="", stderr=str(e), exit_code=1)
return PythonInterpreterResult(output="", error=str(e), exit_code=1)


async def _strip_markdown_code(md_string: str) -> str:
Expand All @@ -46,7 +41,7 @@ async def _strip_markdown_code(md_string: str) -> str:
return stripped_string


async def execute(code: str) -> Optional[PythonInterpreterResult]:
async def execute(code: str) -> PythonInterpreterResult:
clean_code = await _strip_markdown_code(code)
return await _interpret_code(clean_code)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 5a2ca49

Please sign in to comment.