blazickjp / GPT-CodeApp

This project is everything Chat-GPT should be for developers! An advanced AI-driven coding companion tailored for developers. Seamlessly bridging the gap between traditional coding and AI capabilities, we offer real-time chat interactions, on-demand agent functions, and intuitive code management. Feedback welcome!
MIT License
33 stars 12 forks source link

Release #34

Closed blazickjp closed 6 months ago

blazickjp commented 6 months ago

User description

/describe


Type

enhancement, bug_fix


Description


Changes walkthrough

Relevant files
Enhancement
7 files
agent_prompts.py
Introduce New System Prompt and Refine Existing Prompt     

backend/agent/agent_prompts.py
  • Introduced a new system prompt DEFAULT_SYSTEM_PROMPT_V2 with detailed
    instructions and examples for logical reasoning tasks.
  • Updated the existing DEFAULT_SYSTEM_PROMPT to include a more
    structured and detailed guide for code integration tasks.
  • +117/-74
    coding_agent.py
    Update AI Model and Refine Streaming Method                           

    backend/agent/coding_agent.py
  • Updated the GPT model from gpt-4-0125-preview to gpt-4-turbo.
  • Reduced the temperature setting from 0.75 to 0.2 for more
    deterministic outputs.
  • Streamlined the call_model_streaming method by removing unnecessary
    print statements and handling different model types more cleanly.
  • +9/-12   
    main.py
    Add Temperature Setting Endpoint and Enhance Logging         

    backend/main.py
  • Added new endpoint /set_temperature to adjust system temperature
    settings.
  • Removed unnecessary print statements and replaced them with logging.
  • +40/-7   
    memory_manager.py
    Refactor MemoryManager for Improved Efficiency                     

    backend/memory/memory_manager.py
  • Major refactor of the MemoryManager class, removing unused methods and
    streamlining the message handling.
  • +17/-173
    system_prompt_handler.py
    Refactor SystemPromptHandler for Better Prompt Management

    backend/memory/system_prompt_handler.py
  • Refactored SystemPromptHandler to manage system prompts more
    effectively, including CRUD operations and diff generation from the
    main branch.
  • +179/-151
    working_context.py
    Implement WorkingContext for User Profile Management         

    backend/memory/working_context.py
  • Introduced a new class WorkingContext to manage user profiles and
    relations using RDF and SPARQL.
  • +142/-0 
    DirectorySelectOption.js
    Add Frontend Support for Temperature Adjustment                   

    frontend/components/DirectorySelectOption.js
  • Added functionality to update temperature settings through a new API
    endpoint.
  • +25/-1   
    Configuration changes
    1 files
    app_setup.py
    Integrate New System Prompt and Adjust Logging Level         

    backend/app_setup.py
  • Integrated DEFAULT_SYSTEM_PROMPT_V2 into the app setup.
  • Changed logging level from INFO to WARNING to reduce log verbosity.
  • +19/-9   
    Miscellaneous
    1 files
    my_codebase.py
    Clean-up Database Codebase Access Method                                 

    backend/database/my_codebase.py
  • Minor clean-up in the tree method, removing unnecessary print
    statements.
  • +0/-1     

    โœจ PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    sweep-ai[bot] commented 6 months ago

    Apply Sweep Rules to your PR?

    This is an automated message generated by Sweep AI.

    github-actions[bot] commented 6 months ago

    PR Description updated to latest commit (https://github.com/blazickjp/GPT-CodeApp/commit/e859636d92477c4bf4339d7882a10978e3693511)

    github-actions[bot] commented 6 months ago

    PR Review

    โฑ๏ธ Estimated effort to review [1-5] 3, because the PR includes a significant amount of changes across multiple files, involving both backend and frontend components. The changes include updates to AI models, introduction of new system prompts, and adjustments to logging and temperature settings, which require careful review to ensure they integrate well without introducing bugs or performance issues.
    ๐Ÿงช Relevant tests No
    ๐Ÿ” Possible issues Possible Bug: The method `set_files_in_prompt` in `SystemPromptHandler` might not handle the `anth` parameter correctly if it's not explicitly set to `True` or `False`, as the method's behavior significantly changes based on this flag.
    Performance Concern: The new system prompt `DEFAULT_SYSTEM_PROMPT_V2` includes a lot of static text and structured commands which might not be optimized for frequent updates or modifications, potentially leading to maintenance challenges.
    ๐Ÿ”’ Security concerns No
    Code feedback:
    relevant filebackend/agent/coding_agent.py
    suggestion       Consider adding a check for `self.GPT_MODEL` in the `call_model_streaming` method to ensure it's not `None` before proceeding with operations. This can prevent potential runtime errors if the model is not set. [important]
    relevant lineif self.GPT_MODEL.startswith("gpt") or self.GPT_MODEL is None:

    relevant filebackend/agent/agent_prompts.py
    suggestion       For the new `DEFAULT_SYSTEM_PROMPT_V2`, consider breaking down the static content into smaller, reusable components or templates that can be dynamically assembled. This approach can improve maintainability and flexibility of prompt modifications. [important]
    relevant lineDEFAULT_SYSTEM_PROMPT_V2 = """

    relevant filebackend/app_setup.py
    suggestion       Refactor the `StreamToLogger` class to handle potential recursive calls to `write` more gracefully by using a reentrant lock instead of a boolean flag. This can prevent issues where logging inside a logging call might skip certain log messages. [medium]
    relevant lineif not self._is_logging:

    relevant filefrontend/components/DirectorySelectOption.js
    suggestion       Implement error handling in the UI, such as displaying a notification to the user when the temperature update fails. This improves user experience by providing feedback on the success or failure of their actions. [medium]
    relevant lineconsole.error('Error updating temperature setting:', error);


    โœจ Review tool usage guide:
    **Overview:** The `review` tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be [added](https://pr-agent-docs.codium.ai/tools/review/#general-configurations) by configuring the tool. The tool can be triggered [automatically](https://pr-agent-docs.codium.ai/usage-guide/automations_and_usage/#github-app-automatic-tools-when-a-new-pr-is-opened) every time a new PR is opened, or can be invoked manually by commenting on any PR. - When commenting, to edit [configurations](https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/settings/configuration.toml#L23) related to the review tool (`pr_reviewer` section), use the following template: ``` /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=... ``` - With a [configuration file](https://pr-agent-docs.codium.ai/usage-guide/configuration_options/), use the following template: ``` [pr_reviewer] some_config1=... some_config2=... ``` See the review [usage page](https://pr-agent-docs.codium.ai/tools/review/) for a comprehensive guide on using this tool.
    github-actions[bot] commented 6 months ago

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Maintainability
    Refactor the long string into smaller functions to improve code maintainability. ___ **The DEFAULT_SYSTEM_PROMPT_V2 string is very long and contains multiple responsibilities
    and information. It would be beneficial to split this into smaller, more manageable
    components or functions, each handling a specific aspect of the prompt setup. This would
    improve readability and maintainability.** [backend/agent/agent_prompts.py [44-89]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-a2db0403ffba97946298786bd3e5a169208bc14b5fe15771a0d1b5a6856bed25R44-R89) ```diff -DEFAULT_SYSTEM_PROMPT_V2 = """ -## ๐Ÿค– Code Assistant ๐Ÿค– -... -**Let's start coding!** ๐Ÿš€ -""" +def get_intro(): + return """ + ## ๐Ÿค– Code Assistant ๐Ÿค– + ... + """ +def get_examples(): + return """ + Examples: + ... + """ + +DEFAULT_SYSTEM_PROMPT_V2 = get_intro() + get_examples() + "**Let's start coding!** ๐Ÿš€" + ```
    Bug
    Add explicit handling for when self.GPT_MODEL is None to avoid unintended behavior. ___ **The call_model_streaming method has a condition that checks if self.GPT_MODEL starts with
    "gpt" or is None, which could lead to unexpected behavior if self.GPT_MODEL is None
    because the subsequent code assumes a valid model name. It's better to separate these
    conditions and handle the None case explicitly.** [backend/agent/coding_agent.py [311-314]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-3a108924ad95c23ef1f3590e7e22a26b554f6499b73461abf2796a794a7d75ddR311-R314) ```diff -if self.GPT_MODEL.startswith("gpt") or self.GPT_MODEL is None: +if self.GPT_MODEL is None: + raise ValueError("GPT_MODEL is not set") +elif self.GPT_MODEL.startswith("gpt"): print("Calling OpenAI") for chunk in self.client.chat.completions.create(**kwargs): yield chunk ```
    Enhancement
    Simplify the logging mechanism in StreamToLogger to avoid unnecessary state management. ___ **The StreamToLogger class method write uses a flag _is_logging to prevent recursive
    logging. However, this approach can be simplified by directly checking if the message is
    not empty, which avoids the need for an additional flag and makes the code cleaner.** [backend/app_setup.py [68-72]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-08a9fede4f022b5ec42166f359a5df1642cf0018105ebaa995ac11446c1e2725R68-R72) ```diff def write(self, message): - if not self._is_logging: - self._is_logging = True + if message.strip(): self.logger.log(self.log_level, message.rstrip()) - self._is_logging = False ```
    Add error handling to the anthropic model branch to manage exceptions gracefully. ___ **The call_model_streaming method has a branch for handling an "anthropic" model, but it
    lacks a clear exception or error handling if the generate_anthropic_prompt method fails.
    It would be beneficial to add error handling to manage exceptions more gracefully.** [backend/agent/coding_agent.py [316-319]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-3a108924ad95c23ef1f3590e7e22a26b554f6499b73461abf2796a794a7d75ddR316-R319) ```diff elif self.GPT_MODEL == "anthropic": print("Calling anthropic") try: print(self.generate_anthropic_prompt()) + except Exception as e: + print(f"Error generating anthropic prompt: {e}") ```
    Improve error handling in the database connection function to manage connection failures. ___ **The create_database_connection function attempts to connect to a SQLite database but does
    not handle potential exceptions that could arise during connection, such as
    sqlite3.OperationalError. Adding a try-except block would make the function more robust by
    handling these exceptions.** [backend/app_setup.py [88-89]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-08a9fede4f022b5ec42166f359a5df1642cf0018105ebaa995ac11446c1e2725R88-R89) ```diff def create_database_connection() -> sqlite3.Connection: try: return sqlite3.connect("my_database.db") + except sqlite3.OperationalError as e: + print(f"Failed to connect to database: {e}") + return None ```
    Add error handling for database operations to improve application robustness. ___ **Add error handling for potential failures when adding messages to the database to ensure
    the application handles exceptions gracefully.** [backend/main.py [160-176]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-df5184bf1f67239fcedd4578cd15b7c94f3f765feabd09c5f20b5831bca33903R160-R176) ```diff -self.cur.execute( - f""" - INSERT INTO {self.memory_table_name} - (interaction_index, role, content, content_tokens, summarized_message, summarized_message_tokens, project_directory, is_function_call, system_prompt) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?); - """, - ( - timestamp, - role, - content, - message_tokens, - summary, - summary_tokens, - self.project_directory, - is_function_call, - system_prompt, - ), -) +try: + self.cur.execute( + f""" + INSERT INTO {self.memory_table_name} + (interaction_index, role, content, content_tokens, summarized_message, summarized_message_tokens, project_directory, is_function_call, system_prompt) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?); + """, + ( + timestamp, + role, + content, + message_tokens, + summary, + summary_tokens, + self.project_directory, + is_function_call, + system_prompt, + ), + ) +except Exception as e: + logging.error(f"Failed to insert message into database: {str(e)}") ```
    Improve the robustness of string concatenation in system prompt construction. ___ **Replace the direct string concatenation with a more robust and error-free method using
    str.join(). This will ensure that the system prompt is constructed correctly even if some
    parts are missing or None.** [backend/memory/system_prompt_handler.py [63-70]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-19926b4d84af3e8cc2821482ce161659fa2329ca092bc3b72f2a9c1802f4068aR63-R70) ```diff -self.system = ( - self.identity - + "\n\n" - + "The following information is intended to aid in your responses to the User\n\n" - + "The project directory is setup as follows:\n" -) -self.system += self.tree + "\n\n" if self.tree else "" +parts = [ + self.identity, + "\n\nThe following information is intended to aid in your responses to the User\n\n", + "The project directory is setup as follows:\n", + self.tree + "\n\n" if self.tree else "" +] +self.system = "".join(filter(None, parts)) ```
    Replace subprocess with GitPython for executing git commands. ___ **Avoid using subprocess to run git commands directly. Instead, use a library like GitPython
    for better error handling and integration.** [backend/memory/system_prompt_handler.py [173-176]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-19926b4d84af3e8cc2821482ce161659fa2329ca092bc3b72f2a9c1802f4068aR173-R176) ```diff -command = ["git", "-C", repo_path, "diff", "release..main"] -return subprocess.run( - command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True -) +from git import Repo +repo = Repo(repo_path) +diff = repo.git.diff('release..main') +return diff ```
    Simplify mocking by using return_value instead of side_effect. ___ **Instead of using side_effect on fetchall, consider using return_value if the intention is
    to return the same result consistently for simpler mocking.** [backend/tests/test_memory_manager.py [28]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-49299b207fb65ca7da2c4ecb6c8c8670ffdaea38cf7e7f09b82ee241e4a072dbR28-R28) ```diff -cursor.fetchall.side_effect = [ - [ # Second call to fetchall - ("assistant", "full_assistant_message", "assistant_message", 20), - ("user", "full_user_message", "user_message", 10), +cursor.fetchall.return_value = [ + ("assistant", "full_assistant_message", "assistant_message", 20), + ("user", "full_user_message", "user_message", 10), ```
    Add a test case to verify the correctness of token counting. ___ **Consider adding a test case to verify the output of get_total_tokens_in_message function
    to ensure its correctness.** [backend/tests/test_memory_manager.py [21]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-49299b207fb65ca7da2c4ecb6c8c8670ffdaea38cf7e7f09b82ee241e4a072dbR21-R21) ```diff tokens = self.memory_manager.get_total_tokens_in_message(message) +self.assertEqual(tokens, expected_tokens_count) # expected_tokens_count needs to be defined based on the expected outcome ```
    Best practice
    Replace print with logging.info for consistent logging practices. ___ **Replace the use of print for logging with the appropriate logging level method to maintain
    consistency and control over logging output.** [backend/main.py [366]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-df5184bf1f67239fcedd4578cd15b7c94f3f765feabd09c5f20b5831bca33903R366-R366) ```diff -print(f"Setting system temperature to: {temperature}") +logging.info(f"Setting system temperature to: {temperature}") ```
    Improve error handling by catching specific exceptions. ___ **Handle exceptions more specifically rather than catching all exceptions. This will help in
    identifying the exact issue and handling it appropriately.** [backend/memory/system_prompt_handler.py [160-163]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-19926b4d84af3e8cc2821482ce161659fa2329ca092bc3b72f2a9c1802f4068aR160-R163) ```diff +except sqlite3.DatabaseError as db_err: + logger.error(f"Database error occurred: {db_err}") + raise except Exception as e: - logger.error(f"Failed to create table: {e}") - logger.error(f"{e.traceback}") + logger.error(f"An unexpected error occurred: {e}") raise ```
    Improve test assertions for better error reporting. ___ **Consider using assertEqual instead of assert for comparing lists in tests to get more
    detailed error messages if the test fails.** [backend/tests/test_memory_manager.py [37]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-49299b207fb65ca7da2c4ecb6c8c8670ffdaea38cf7e7f09b82ee241e4a072dbR37-R37) ```diff -assert messages[1:] == [ +self.assertEqual(messages[1:], [ { "role": "user", "content": "user_message", ```
    Ensure the variable messages is a list before manipulating it. ___ **It's recommended to use assertIsInstance to check the type of messages to ensure it is a
    list before performing list operations.** [backend/tests/test_memory_manager.py [34]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-49299b207fb65ca7da2c4ecb6c8c8670ffdaea38cf7e7f09b82ee241e4a072dbR34-R34) ```diff messages = self.memory_manager.get_messages() +self.assertIsInstance(messages, list) print(messages) ```
    Possible issue
    Ensure the system_prompt parameter is effectively utilized within the method. ___ **Ensure that the system_prompt parameter is utilized when adding a message, as it is
    currently not being used in the method call.** [backend/main.py [56-60]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-df5184bf1f67239fcedd4578cd15b7c94f3f765feabd09c5f20b5831bca33903R56-R60) ```diff AGENT.memory_manager.add_message( "assistant", accumulated_messages[id], system_prompt=AGENT.memory_manager.prompt_handler.system, ) +# Ensure that the `system_prompt` is being used inside the `add_message` method ```
    Add a check to ensure the list has enough elements before accessing by index. ___ **To avoid potential issues with index out of range, add a check to ensure messages list has
    the expected number of elements before accessing by index.** [backend/tests/test_memory_manager.py [37]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-49299b207fb65ca7da2c4ecb6c8c8670ffdaea38cf7e7f09b82ee241e4a072dbR37-R37) ```diff +self.assertTrue(len(messages) > 1) assert messages[1:] == [ { "role": "user", "content": "user_message", ```
    Security
    Implement validation and error handling for temperature settings to enhance security. ___ **Use a more secure method to handle the temperature setting to prevent potential security
    risks such as injection or unauthorized access.** [backend/main.py [361-369]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-df5184bf1f67239fcedd4578cd15b7c94f3f765feabd09c5f20b5831bca33903R361-R369) ```diff temperature = input.get("temperature") if temperature is not None: - AGENT.temperature = temperature - logging.info(f"Setting system temperature to: {temperature}") - return JSONResponse( - status_code=200, content={"message": "Temperature set successfully"} - ) + try: + # Validate and possibly convert temperature to a safe format + safe_temperature = float(temperature) # Example of type casting for validation + AGENT.temperature = safe_temperature + logging.info(f"Setting system temperature to: {safe_temperature}") + return JSONResponse( + status_code=200, content={"message": "Temperature set successfully"} + ) + except ValueError: + return JSONResponse( + status_code=400, content={"error": "Invalid temperature value"} + ) ```
    Enhance security by using parameterized SQL queries. ___ **Use parameterized queries to prevent SQL injection vulnerabilities. Replace direct string
    insertion into SQL queries with placeholders and parameter passing.** [backend/memory/system_prompt_handler.py [82-86]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-19926b4d84af3e8cc2821482ce161659fa2329ca092bc3b72f2a9c1802f4068aR82-R86) ```diff -self.cur.execute("DELETE FROM system_prompt") +self.cur.execute("DELETE FROM system_prompt WHERE role = ?", ("system",)) self.cur.execute( "INSERT INTO system_prompt (role, content) VALUES (?, ?)", ("system", self.system), ) ```
    Performance
    Optimize JSON serialization for better performance in streaming contexts. ___ **Replace the use of json.dumps with a more efficient method or library that can handle
    streaming JSON data more effectively, especially in high-load environments.** [backend/main.py [55]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-df5184bf1f67239fcedd4578cd15b7c94f3f765feabd09c5f20b5831bca33903R55-R55) ```diff -yield json.dumps({"id": id, "content": content}) + "@@" +yield orjson.dumps({"id": id, "content": content}).decode("utf-8") + "@@" ```
    Optimize SQL query and list comprehension for retrieving system prompts. ___ **Refactor the method to use list comprehensions for building the list of prompts, which can
    enhance readability and performance.** [backend/memory/system_prompt_handler.py [185-195]](https://github.com/blazickjp/GPT-CodeApp/pull/34/files#diff-19926b4d84af3e8cc2821482ce161659fa2329ca092bc3b72f2a9c1802f4068aR185-R195) ```diff -self.cur.execute("SELECT * FROM system_prompts") +self.cur.execute("SELECT id, prompt, created_at, updated_at FROM system_prompts") return [ - { - "id": prompt[0], - "name": prompt[0], - "prompt": prompt[1], - "created_at": prompt[2], - "updated_at": prompt[3], - } - for prompt in self.cur.fetchall() + {"id": id, "name": id, "prompt": prompt, "created_at": created_at, "updated_at": updated_at} + for id, prompt, created_at, updated_at in self.cur.fetchall() ] ```

    โœจ Improve tool usage guide:
    **Overview:** The `improve` tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered [automatically](https://pr-agent-docs.codium.ai/usage-guide/automations_and_usage/#github-app-automatic-tools-when-a-new-pr-is-opened) every time a new PR is opened, or can be invoked manually by commenting on a PR. - When commenting, to edit [configurations](https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/settings/configuration.toml#L78) related to the improve tool (`pr_code_suggestions` section), use the following template: ``` /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=... ``` - With a [configuration file](https://pr-agent-docs.codium.ai/usage-guide/configuration_options/), use the following template: ``` [pr_code_suggestions] some_config1=... some_config2=... ``` See the improve [usage page](https://pr-agent-docs.codium.ai/tools/improve/) for a comprehensive guide on using this tool.