Skip to content

Commit

Permalink
Merge pull request #994 from kristiankyvik/generate-docstrings
Browse files Browse the repository at this point in the history
Looks great! If you intend to be at the meeting on Thursday, I would be curious to hear how you did this in more detail!
  • Loading branch information
ATheorell authored Feb 13, 2024
2 parents e55f840 + f65f89c commit 19446fa
Show file tree
Hide file tree
Showing 39 changed files with 1,689 additions and 430 deletions.
3 changes: 1 addition & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@
"members": True,
"show-inheritance": True,
"inherited-members": "BaseModel",
"undoc-members": True,
"special-members": "__call__",
"undoc-members": False,
}

# Add any paths that contain templates here, relative to this directory.
Expand Down
2 changes: 1 addition & 1 deletion docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Once you have a copy of the source, you can install it with:
.. _Github repo: https://github.com/gpt-engineer-org/gpt-engineer.git

Troubleshooting
-------------
---------------

For mac and linux system, there are sometimes slim python installations that do not include the gpt-engineer requirement tkinter, which is a standard library and thus not pip installable.

Expand Down
15 changes: 0 additions & 15 deletions gpt_engineer/applications/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +0,0 @@
"""
gpt_engineer.cli
-----------------
The CLI package for the GPT Engineer project, providing the command line interface
for the application.
Modules:
- main: The primary CLI module for GPT Engineer.
- collect: Collect send learning tools for analysis and improvement.
- file_selector: Selecting files using GUI and terminal-based file explorer.
- learning: Tools and data structures for data collection.
For more specific details, refer to the docstrings within each module.
"""
147 changes: 110 additions & 37 deletions gpt_engineer/applications/cli/cli_agent.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""
This module provides the CliAgent class which manages the lifecycle of code generation and improvement
using an AI model. It includes functionalities to initialize code generation, improve existing code,
and process the code through various steps defined in the step bundle.
"""

from typing import Callable, Optional, TypeVar

# from gpt_engineer.core.default.git_version_manager import GitVersionManager
Expand Down Expand Up @@ -28,43 +34,50 @@

class CliAgent(BaseAgent):
"""
The `Agent` class is responsible for managing the lifecycle of code generation and improvement.
Attributes:
path (str): The file path where the `Agent` will operate, used for version management and
file operations.
version_manager (BaseVersionManager): An object that adheres to the VersionManagerInterface,
responsible for version control of the generated code. Defaults to `VersionManager`
if not provided. PROBABLY GIT SHOULD BE USED IN THE DEFAULT
step_bundle (StepBundleInterface): Workflows of code generation steps that define the behavior of gen_code and
improve.
ai (AI): Manages calls to the LLM.
Methods:
__init__(self, path: str, version_manager: VersionManagerInterface = None,
step_bundle: StepBundleInterface = None, ai: AI = None):
Initializes a new instance of the Agent class with the provided path, version manager,
step bundle, and AI. It falls back to default instances if specific components are not provided.
init(self, prompt: str) -> Code:
Generates a new piece of code using the AI and step bundle based on the provided prompt.
It also snapshots the generated code using the version manager.
Parameters:
prompt (str): A string prompt that guides the code generation process.
Returns:
FilesDict: An instance of the `Code` class containing the generated code.
improve(self, prompt: str) -> Code:
Improves an existing piece of code using the AI and step bundle based on the provided prompt.
It also snapshots the improved code using the version manager.
Parameters:
prompt (str): A string prompt that guides the code improvement process.
Returns:
FilesDict: An instance of the `Code` class containing the improved code.
The `CliAgent` class is responsible for managing the lifecycle of code generation and improvement
using an AI model. It orchestrates the generation of new code and the improvement of existing code
based on given prompts and utilizes a memory system and execution environment for processing.
Parameters
----------
memory : BaseMemory
An instance of a class that adheres to the BaseMemory interface, used for storing and retrieving
information during the code generation process.
execution_env : BaseExecutionEnv
An instance of a class that adheres to the BaseExecutionEnv interface, used for executing code
and managing the execution environment.
ai : AI, optional
An instance of the AI class that manages calls to the language model. If not provided, a default
instance is created.
code_gen_fn : CodeGenType, optional
A callable that takes an AI instance, a prompt, and a memory instance to generate code. Defaults
to the `gen_code` function.
improve_fn : ImproveType, optional
A callable that takes an AI instance, a prompt, a FilesDict instance, and a memory instance to
improve code. Defaults to the `improve` function.
process_code_fn : CodeProcessor, optional
A callable that takes an AI instance, an execution environment, and a FilesDict instance to
process code. Defaults to the `execute_entrypoint` function.
preprompts_holder : PrepromptsHolder, optional
An instance of PrepromptsHolder that manages preprompt templates. If not provided, a default
instance is created using the PREPROMPTS_PATH.
Attributes
----------
memory : BaseMemory
The memory instance where the agent stores and retrieves information.
execution_env : BaseExecutionEnv
The execution environment instance where the agent executes and manages code.
ai : AI
The AI instance used for interacting with the language model.
code_gen_fn : CodeGenType
The function used for generating code.
improve_fn : ImproveType
The function used for improving code.
process_code_fn : CodeProcessor
The function used for processing code.
preprompts_holder : PrepromptsHolder
The holder for preprompt templates.
"""

def __init__(
Expand Down Expand Up @@ -96,6 +109,34 @@ def with_default_config(
process_code_fn: CodeProcessor = execute_entrypoint,
preprompts_holder: PrepromptsHolder = None,
):
"""
Creates a new instance of CliAgent with default configurations for memory, execution environment,
AI, and other functional parameters.
Parameters
----------
memory : DiskMemory
An instance of DiskMemory for storing and retrieving information.
execution_env : DiskExecutionEnv
An instance of DiskExecutionEnv for executing code.
ai : AI, optional
An instance of AI for interacting with the language model. Defaults to None, which will create
a new AI instance.
code_gen_fn : CodeGenType, optional
A function for generating code. Defaults to `gen_code`.
improve_fn : ImproveType, optional
A function for improving code. Defaults to `improve`.
process_code_fn : CodeProcessor, optional
A function for processing code. Defaults to `execute_entrypoint`.
preprompts_holder : PrepromptsHolder, optional
An instance of PrepromptsHolder for managing preprompt templates. Defaults to None, which will
create a new PrepromptsHolder instance using PREPROMPTS_PATH.
Returns
-------
CliAgent
An instance of CliAgent configured with the provided or default parameters.
"""
return cls(
memory=memory,
execution_env=execution_env,
Expand All @@ -107,6 +148,20 @@ def with_default_config(
)

def init(self, prompt: str) -> FilesDict:
"""
Generates a new piece of code using the AI and step bundle based on the provided prompt.
Parameters
----------
prompt : str
A string prompt that guides the code generation process.
Returns
-------
FilesDict
An instance of the `FilesDict` class containing the generated code.
"""

files_dict = self.code_gen_fn(
self.ai, prompt, self.memory, self.preprompts_holder
)
Expand All @@ -129,6 +184,24 @@ def improve(
prompt: str,
execution_command: Optional[str] = None,
) -> FilesDict:
"""
Improves an existing piece of code using the AI and step bundle based on the provided prompt.
Parameters
----------
files_dict : FilesDict
An instance of `FilesDict` containing the code to be improved.
prompt : str
A string prompt that guides the code improvement process.
execution_command : str, optional
An optional command to execute the code. If not provided, the default execution command is used.
Returns
-------
FilesDict
An instance of the `FilesDict` class containing the improved code.
"""

files_dict = self.improve_fn(
self.ai, prompt, files_dict, self.memory, self.preprompts_holder
)
Expand Down
105 changes: 58 additions & 47 deletions gpt_engineer/applications/cli/collect.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
"""
Module `collect` - Data Handling and RudderStack Integration
This module provides functionalities to handle and send learning data to RudderStack
for the purpose of analysis and to improve the gpt-engineer system. The data is sent
only when the user gives consent to share.
The module provides the following main functions:
- `send_learning`: Directly send a learning data to RudderStack.
- `collect_learnings`: Extract, possibly adjust, and send the learning data based on
provided input parameters.
- `steps_file_hash`: Computes the SHA-256 hash of the steps file, which might be used
for identifying the exact version or changes in the steps.
Functions:
send_learning(learning): Sends learning data to RudderStack.
collect_learnings(prompt, model, temperature, config, memory, review): Processes and sends learning data.
collect_and_send_human_review(prompt, model, temperature, config, memory): Collects human feedback and sends it.
Dependencies:
- hashlib: For generating SHA-256 hash.
- typing: For type annotations.
- gpt_engineer.core: Core functionalities of gpt-engineer.
- gpt_engineer.cli.learning: Handles the extraction of learning data.
hashlib: For generating SHA-256 hash.
typing: For type annotations.
gpt_engineer.core: Core functionalities of gpt-engineer.
gpt_engineer.cli.learning: Handles the extraction of learning data.
Note:
Notes:
Data sent to RudderStack is not shared with third parties and is used solely to
improve gpt-engineer and allow it to handle a broader range of use cases.
Consent logic is in gpt_engineer/learning.py.
"""

from typing import Tuple
Expand All @@ -39,16 +37,17 @@ def send_learning(learning: Learning):
"""
Send the learning data to RudderStack for analysis.
Note:
This function is only called if consent is given to share data.
Data is not shared to a third party. It is used with the sole purpose of
improving gpt-engineer, and letting it handle more use cases.
Consent logic is in gpt_engineer/learning.py
Parameters
----------
learning : Learning
The learning data to send.
An instance of the Learning class containing the data to be sent.
Notes
-----
This function is only called if consent is given to share data.
Data is not shared to a third party. It is used with the sole purpose of
improving gpt-engineer, and letting it handle more use cases.
Consent logic is in gpt_engineer/learning.py.
"""
import rudderstack.analytics as rudder_analytics

Expand All @@ -75,14 +74,23 @@ def collect_learnings(
Parameters
----------
prompt : str
The initial prompt or question that was provided to the model.
model : str
The name of the model used.
The name of the model used for generating the response.
temperature : float
The temperature used.
steps : List[Step]
The list of steps.
dbs : DBs
The database containing the workspace.
The temperature setting used in the model's response generation.
config : any
Configuration parameters used for the learning session.
memory : DiskMemory
An instance of DiskMemory for storing and retrieving data.
review : Review
An instance of Review containing human feedback on the model's response.
Notes
-----
This function attempts to send the learning data to RudderStack. If the data size exceeds
the maximum allowed size, it trims the data and retries sending it.
"""
learnings = extract_learning(prompt, model, temperature, config, memory, review)
try:
Expand Down Expand Up @@ -137,29 +145,32 @@ def collect_and_send_human_review(
memory: DiskMemory,
):
"""
Collects human feedback on the code and stores it in memory.
This function prompts the user for a review of the generated or improved code using the `human_review_input`
function. If a valid review is provided, it's serialized to JSON format and stored within the database's
memory under the "review" key.
Parameters:
- ai (AI): An instance of the AI model. Although not directly used within the function, it is kept as
a parameter for consistency with other functions.
- dbs (DBs): An instance containing the database configurations, user prompts, project metadata,
and memory storage. This function specifically interacts with the memory storage to save the human review.
Returns:
- list: Returns an empty list, indicating that there's no subsequent interaction with the LLM
or no further messages to be processed.
Notes:
- It's assumed that the `human_review_input` function handles all the interactions with the user to
gather feedback and returns either the feedback or None if no feedback was provided.
- Ensure that the database's memory has enough space or is set up correctly to store the serialized review data.
Collects human feedback on the code and sends it for analysis.
Parameters
----------
prompt : str
The initial prompt or question that was provided to the model.
model : str
The name of the model used for generating the response.
temperature : float
The temperature setting used in the model's response generation.
config : Tuple[str, ...]
Configuration parameters used for the learning session.
memory : DiskMemory
An instance of DiskMemory for storing and retrieving data.
Returns
-------
None
Notes
-----
This function prompts the user for a review of the generated or improved code using the
`human_review_input` function. If a valid review is provided, it's serialized to JSON format
and stored within the database's memory under the "review" key.
"""

"""Collects and stores human review of the code"""
review = human_review_input()
if review:
collect_learnings(prompt, model, temperature, config, memory, review)
Loading

0 comments on commit 19446fa

Please sign in to comment.