-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d5f8ba
commit 64672d2
Showing
8 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class PythonInterpreterResult: | ||
stdout: str | ||
stderr: str | ||
exit_code: int |
57 changes: 57 additions & 0 deletions
57
oracles/src/domain/tools/code_interpreter/python_interpreter_use_case.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import re | ||
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]: | ||
try: | ||
async with aiohttp.ClientSession() as session: | ||
async with session.post( | ||
"https://exec.bearly.ai/v1/interpreter", | ||
headers={ | ||
"Authorization": settings.BEARLY_API_KEY, | ||
}, | ||
json={ | ||
"fileContents": code, | ||
"inputFiles": [], | ||
"outputDir": "output/", | ||
"outputAsLinks": True, | ||
}, | ||
) as response: | ||
response.raise_for_status() | ||
data = await response.json() | ||
return PythonInterpreterResult( | ||
stdout=( | ||
base64.b64decode(data["stdoutBasesixtyfour"]).decode() | ||
if data["stdoutBasesixtyfour"] | ||
else "" | ||
), | ||
stderr=( | ||
base64.b64decode(data["stderrBasesixtyfour"]).decode() | ||
if data["stderrBasesixtyfour"] | ||
else "" | ||
), | ||
exit_code=data["exitCode"], | ||
) | ||
except Exception as e: | ||
return PythonInterpreterResult(stdout="", stderr=str(e), exit_code=1) | ||
|
||
|
||
async def _strip_markdown_code(md_string: str) -> str: | ||
stripped_string = re.sub(r"^`{1,3}.*?\n", "", md_string, flags=re.DOTALL) | ||
stripped_string = re.sub(r"`{1,3}$", "", stripped_string) | ||
return stripped_string | ||
|
||
|
||
async def execute(code: str) -> Optional[PythonInterpreterResult]: | ||
clean_code = await _strip_markdown_code(code) | ||
return await _interpret_code(clean_code) | ||
|
||
|
||
if __name__ == "__main__": | ||
import asyncio | ||
|
||
print(asyncio.run(execute("print(2 + 2)"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters