From 5a2ca49d90df9b6f38c65260eccb567238babb9f Mon Sep 17 00:00:00 2001 From: kgrofelnik Date: Thu, 28 Mar 2024 14:51:21 +0100 Subject: [PATCH] Code interpreter response format + grooming --- oracles/oracle.py | 4 ++-- .../domain/tools/code_interpreter/entities.py | 4 ++-- .../python_interpreter_use_case.py | 23 ++++++++----------- {scripts => rag_tools}/README.md | 0 {scripts => rag_tools}/add_knowledge_base.py | 0 .../knowledgebase/entities.py | 0 .../knowledgebase/load_documents_use_case.py | 0 .../request_indexing_use_case.py | 0 .../upload_documents_use_case.py | 0 {scripts => rag_tools}/requirements.txt | 0 {scripts => rag_tools}/settings.py | 0 {scripts => rag_tools}/template.env | 0 12 files changed, 13 insertions(+), 18 deletions(-) rename {scripts => rag_tools}/README.md (100%) rename {scripts => rag_tools}/add_knowledge_base.py (100%) rename {scripts => rag_tools}/knowledgebase/entities.py (100%) rename {scripts => rag_tools}/knowledgebase/load_documents_use_case.py (100%) rename {scripts => rag_tools}/knowledgebase/request_indexing_use_case.py (100%) rename {scripts => rag_tools}/knowledgebase/upload_documents_use_case.py (100%) rename {scripts => rag_tools}/requirements.txt (100%) rename {scripts => rag_tools}/settings.py (100%) rename {scripts => rag_tools}/template.env (100%) diff --git a/oracles/oracle.py b/oracles/oracle.py index d8d1c95..e487021 100644 --- a/oracles/oracle.py +++ b/oracles/oracle.py @@ -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}'" diff --git a/oracles/src/domain/tools/code_interpreter/entities.py b/oracles/src/domain/tools/code_interpreter/entities.py index 1879580..58c7145 100644 --- a/oracles/src/domain/tools/code_interpreter/entities.py +++ b/oracles/src/domain/tools/code_interpreter/entities.py @@ -3,6 +3,6 @@ @dataclass class PythonInterpreterResult: - stdout: str - stderr: str + output: str + error: str exit_code: int diff --git a/oracles/src/domain/tools/code_interpreter/python_interpreter_use_case.py b/oracles/src/domain/tools/code_interpreter/python_interpreter_use_case.py index 8ec33a9..c99843b 100644 --- a/oracles/src/domain/tools/code_interpreter/python_interpreter_use_case.py +++ b/oracles/src/domain/tools/code_interpreter/python_interpreter_use_case.py @@ -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( @@ -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: @@ -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) diff --git a/scripts/README.md b/rag_tools/README.md similarity index 100% rename from scripts/README.md rename to rag_tools/README.md diff --git a/scripts/add_knowledge_base.py b/rag_tools/add_knowledge_base.py similarity index 100% rename from scripts/add_knowledge_base.py rename to rag_tools/add_knowledge_base.py diff --git a/scripts/knowledgebase/entities.py b/rag_tools/knowledgebase/entities.py similarity index 100% rename from scripts/knowledgebase/entities.py rename to rag_tools/knowledgebase/entities.py diff --git a/scripts/knowledgebase/load_documents_use_case.py b/rag_tools/knowledgebase/load_documents_use_case.py similarity index 100% rename from scripts/knowledgebase/load_documents_use_case.py rename to rag_tools/knowledgebase/load_documents_use_case.py diff --git a/scripts/knowledgebase/request_indexing_use_case.py b/rag_tools/knowledgebase/request_indexing_use_case.py similarity index 100% rename from scripts/knowledgebase/request_indexing_use_case.py rename to rag_tools/knowledgebase/request_indexing_use_case.py diff --git a/scripts/knowledgebase/upload_documents_use_case.py b/rag_tools/knowledgebase/upload_documents_use_case.py similarity index 100% rename from scripts/knowledgebase/upload_documents_use_case.py rename to rag_tools/knowledgebase/upload_documents_use_case.py diff --git a/scripts/requirements.txt b/rag_tools/requirements.txt similarity index 100% rename from scripts/requirements.txt rename to rag_tools/requirements.txt diff --git a/scripts/settings.py b/rag_tools/settings.py similarity index 100% rename from scripts/settings.py rename to rag_tools/settings.py diff --git a/scripts/template.env b/rag_tools/template.env similarity index 100% rename from scripts/template.env rename to rag_tools/template.env