Skip to content

Commit

Permalink
Completely removed APIKeyCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiannis128 committed Feb 4, 2025
1 parent b970475 commit 86faf61
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 46 deletions.
11 changes: 0 additions & 11 deletions esbmc_ai/api_key_collection.py

This file was deleted.

1 change: 1 addition & 0 deletions esbmc_ai/commands/chat_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(

@property
def config(self) -> BaseConfig:
"""Gets the config for this chat command."""
return self._config

@config.setter
Expand Down
22 changes: 8 additions & 14 deletions esbmc_ai/commands/fix_code_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from esbmc_ai.solution import Solution, SourceFile
from esbmc_ai.ai_models import AIModel
from esbmc_ai.api_key_collection import APIKeyCollection
from esbmc_ai.chat_response import FinishReason
from esbmc_ai.chats import LatestStateSolutionGenerator, SolutionGenerator
from esbmc_ai.verifiers.base_source_verifier import (
Expand Down Expand Up @@ -64,6 +63,7 @@ def __init__(self) -> None:
command_name="fix-code",
help_message="Generates a solution for this code, and reevaluates it with ESBMC.",
)
self.anim: BaseLoadingWidget

def print_raw_conversation(self, solution_generator: SolutionGenerator) -> None:
"""Debug prints the raw conversation"""
Expand All @@ -80,7 +80,7 @@ def execute(self, **kwargs: Any) -> FixCodeCommandResult:
# Handle kwargs
source_file: SourceFile = kwargs["source_file"]
original_source_file: SourceFile = SourceFile(
source_file.file_path, source_file.content
source_file.file_path, Path("."), source_file.content
)

generate_patches: bool = (
Expand All @@ -91,7 +91,7 @@ def execute(self, **kwargs: Any) -> FixCodeCommandResult:
kwargs["message_history"] if "message_history" in kwargs else "normal"
)

api_keys: APIKeyCollection = kwargs["api_keys"]
api_keys = kwargs["api_keys"]
ai_model: AIModel = kwargs["ai_model"]
temperature: float = kwargs["temperature"]
max_tries: int = kwargs["requests_max_tries"]
Expand All @@ -103,18 +103,13 @@ def execute(self, **kwargs: Any) -> FixCodeCommandResult:
raw_conversation: bool = (
kwargs["raw_conversation"] if "raw_conversation" in kwargs else False
)
output_dir: Optional[Path] = (
kwargs["output_dir"] if "output_dir" in kwargs else None
)
self.anim: BaseLoadingWidget = (
kwargs["anim"] if "anim" in kwargs else BaseLoadingWidget()
)
self.anim = kwargs["anim"] if "anim" in kwargs else BaseLoadingWidget()
entry_function: str = (
kwargs["entry_function"] if "entry_function" in kwargs else "main"
)
# End of handle kwargs

solution: Solution = Solution()
solution: Solution = Solution([])
solution.add_source_file(source_file)

printv(f"Temperature: {temperature}")
Expand Down Expand Up @@ -219,12 +214,14 @@ def _attempt_repair(
attempt: int,
max_attempts: int,
solution_generator: SolutionGenerator,
source_file: SourceFile,
solution: Solution,
verifier: BaseSourceVerifier,
output_dir: Optional[Path],
raw_conversation: bool,
**kwargs: Any,
) -> Optional[FixCodeCommandResult]:
source_file: SourceFile = solution.files[0]

# Get a response. Use while loop to account for if the message stack
# gets full, then need to compress and retry.
while True:
Expand All @@ -246,9 +243,6 @@ def _attempt_repair(
print_horizontal_line(3)
printvvv("")

solution: Solution = Solution()
solution.add_source_file(source_file)

# Pass to ESBMC, a workaround is used where the file is saved
# to a temporary location since ESBMC needs it in file format.
with self.anim("Verifying with ESBMC... Please Wait"):
Expand Down
41 changes: 20 additions & 21 deletions esbmc_ai/commands/user_chat_command.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Author: Yiannis Charalambous

from pathlib import Path
import sys
from typing import Any, Optional, override

Expand All @@ -13,7 +14,7 @@
from esbmc_ai.config import Config
from esbmc_ai.loading_widget import BaseLoadingWidget, LoadingWidget
from esbmc_ai.logging import print_horizontal_line, printv, printvv
from esbmc_ai.solution import SourceFile, get_solution
from esbmc_ai.solution import Solution, SourceFile
from esbmc_ai.verifier_runner import VerifierRunner
from esbmc_ai.verifiers.base_source_verifier import VerifierOutput

Expand All @@ -38,20 +39,19 @@ def __init__(
self.command_runner: CommandRunner = command_runner
self.verifier_runner: VerifierRunner = verifier_runner
self.fix_code_command: FixCodeCommand = fix_code_command

self.chat: UserChat
self.solution: Solution
self.anim: BaseLoadingWidget = (
LoadingWidget()
if Config().get_value("loading_hints")
else BaseLoadingWidget()
)

def _run_esbmc(self, source_file: SourceFile, anim: BaseLoadingWidget) -> str:
assert source_file.file_path

with anim("Verifier is processing... Please Wait"):
def _run_esbmc(self) -> str:
with self.anim("Verifier is processing... Please Wait"):
verifier_result: VerifierOutput = (
self.verifier_runner.verifier.verify_source(
source_file=source_file,
solution=self.solution,
esbmc_params=Config().get_value("verifier.esbmc.params"),
timeout=Config().get_value("verifier.esbmc.timeout"),
)
Expand All @@ -73,12 +73,13 @@ def init_commands(self) -> None:
into the commands array in order for the command to register to be called by
the user and also register in the help system."""

def update_source(solution: Solution, content: str) -> None:
solution.files[0].content = content

# Let the AI model know about the corrected code.
self.fix_code_command.on_solution_signal.add_listener(self.chat.set_solution)
self.fix_code_command.on_solution_signal.add_listener(
lambda source_code: get_solution()
.files[0]
.update_content(content=source_code, reset_changes=True)
lambda source_code: update_source(self.solution, source_code)
)

def print_assistant_response(
Expand Down Expand Up @@ -127,22 +128,20 @@ def _execute_fix_code_command_one_file(

@override
def execute(self, **kwargs: Optional[Any]) -> Optional[CommandResult]:
_ = kwargs
# Read the source code and esbmc output.
print("Reading source code...")
get_solution().load_source_files(Config().filenames)
print(f"Running ESBMC with {Config().get_value('verifier.esbmc.params')}\n")
source_file: SourceFile = get_solution().files[0]
file_paths: list[Path] = self.get_config_value("solution.filenames")
self.solution = Solution(file_paths)

esbmc_output: str = self._run_esbmc(source_file, self.anim)
print(f"Running ESBMC with {Config().get_value('verifier.esbmc.params')}\n")
esbmc_output: str = self._run_esbmc()

# Print verbose lvl 2
print_horizontal_line(2)
printvv(esbmc_output)
print_horizontal_line(2)

source_file.assign_verifier_output(esbmc_output)
del esbmc_output

printv(f"Initializing the LLM: {Config().get_ai_model().name}\n")
chat_llm: BaseChatModel = (
Config()
Expand All @@ -156,12 +155,12 @@ def execute(self, **kwargs: Optional[Any]) -> Optional[CommandResult]:
)

printv("Creating user chat")
self.chat: UserChat = UserChat(
self.chat = UserChat(
ai_model=Config().get_ai_model(),
llm=chat_llm,
verifier=self.verifier_runner.verifier,
source_code=source_file.latest_content,
esbmc_output=source_file.latest_verifier_output,
solution=self.solution,
esbmc_output=esbmc_output,
system_messages=Config().get_user_chat_system_messages(),
set_solution_messages=Config().get_user_chat_set_solution(),
)
Expand Down Expand Up @@ -211,7 +210,7 @@ def execute(self, **kwargs: Optional[Any]) -> Optional[CommandResult]:
result: FixCodeCommandResult = (
self._execute_fix_code_command_one_file(
fix_code_command=self.fix_code_command,
source_file=source_file,
source_file=self.solution.files[0],
)
)

Expand Down

0 comments on commit 86faf61

Please sign in to comment.