Skip to content

Commit

Permalink
Fix code generation formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
botirk38 committed Jun 23, 2024
1 parent d26c65b commit 63e83dd
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions autogpts/SoloAgent/forge/actions/code_gen/code_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
from ..registry import action
from forge.sdk import ForgeLogger, PromptEngine
from forge.llm import chat_completion_request
import json
import os
from forge.sdk import Agent, LocalWorkspace

import re

LOG = ForgeLogger(__name__)

Expand All @@ -27,7 +26,7 @@
output_type="str",
)
async def generate_solana_code(agent: Agent, task_id: str, specification: str) -> str:
prompt_engine = PromptEngine("gpt-3.5-turbo")
prompt_engine = PromptEngine("gpt-4o")
lib_prompt = prompt_engine.load_prompt(
"anchor-lib", specification=specification)
instructions_prompt = prompt_engine.load_prompt(
Expand All @@ -46,6 +45,7 @@ async def generate_solana_code(agent: Agent, task_id: str, specification: str) -
{"role": "user", "content": errors_prompt},
{"role": "user", "content": cargo_toml_prompt},
{"role": "user", "content": anchor_toml_prompt},
{"role": "user", "content": "Return the whole code as a string with the file markers intact that you received in each of the input without changing their wording at all."}
]

chat_completion_kwargs = {
Expand All @@ -69,11 +69,12 @@ async def generate_solana_code(agent: Agent, task_id: str, specification: str) -
project_path = os.path.join(
base_path, 'solana_mvp_project', 'onchain', 'programs', 'my_anchor_program')

LOG.info(f"Parts: {response_content}")
await agent.abilities.run_action(
task_id, "write_file", file_path=os.path.join(project_path, 'src', 'lib.rs'), data=parts['lib.rs'].encode()
task_id, "write_file", file_path=os.path.join(project_path, 'src', 'lib.rs'), data=parts['anchor-lib.rs'].encode()
)
await agent.abilities.run_action(
task_id, "write_file", file_path=os.path.join(project_path, 'src', 'instructions.rs'), data=parts['instructions.rs'].encode()
task_id, "write_file", file_path=os.path.join(project_path, 'src', 'instructions.rs'), data=parts['anchor-instructions.rs'].encode()
)
await agent.abilities.run_action(
task_id, "write_file", file_path=os.path.join(project_path, 'src', 'errors.rs'), data=parts['errors.rs'].encode()
Expand Down Expand Up @@ -162,19 +163,19 @@ async def generate_frontend_code(agent, task_id: str, specification: str) -> str
def parse_response_content(response_content: str) -> dict:
# This function will split the response content into different parts
parts = {
'lib.rs': '',
'instructions.rs': '',
'anchor-lib.rs': '',
'anchor-instructions.rs': '',
'errors.rs': '',
'Cargo.toml': '',
'Anchor.toml': ''
}

current_part = None
for line in response_content.split('\n'):
if '// main.rs' in line:
current_part = 'lib.rs'
elif '// instructions.rs' in line:
current_part = 'instructions.rs'
if '// anchor-lib.rs' in line:
current_part = 'anchor-lib.rs'
elif '// anchor-instructions.rs' in line:
current_part = 'anchor-instructions.rs'
elif '// errors.rs' in line:
current_part = 'errors.rs'
elif '// Cargo.toml' in line:
Expand All @@ -184,8 +185,7 @@ def parse_response_content(response_content: str) -> dict:
elif current_part:
parts[current_part] += line + '\n'

# Log each part to verify correct parsing
for key, content in parts.items():
LOG.info(f"Parsed content for {key}:\n{content}")
for key in parts:
parts[key] = re.sub(r'```|rust|toml', '', parts[key]).strip()

return parts

0 comments on commit 63e83dd

Please sign in to comment.