Skip to content

Commit 1b51ce4

Browse files
add pydantic and documentation to generate_object_agent.py and update generate_object_example.py
1 parent 1f20ea5 commit 1b51ce4

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

examples/generate_object_example.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import asyncio
2-
from core.generate_object_agent import GenerateObjectAgent
2+
from core.generate_object_agent import GenerateObjectAgent, ObjectGenerationInput
33

44
async def run_generate_object_example():
5-
description = "A machine that can sort fruits."
6-
goal = "Generate a high-level design of the machine."
7-
agent = GenerateObjectAgent(object_description=description, goal=goal)
5+
input_data = ObjectGenerationInput(
6+
object_description="A machine that can sort fruits.",
7+
goal="Generate a high-level design of the machine.",
8+
max_tokens=1000
9+
)
10+
11+
agent = GenerateObjectAgent(input_data)
812
generated_object = await agent.generate_object()
913

10-
print("Object description:", description)
14+
print("Object description:", input_data.object_description)
1115
print("Generated object:", generated_object)
1216

1317
if __name__ == "__main__":

src/core/generate_object_agent.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,46 @@
1-
import asyncio
1+
from pydantic import BaseModel, Field
22
from typing import Dict
33
from .openai_api import OpenAIClient
44

5+
class ObjectGenerationInput(BaseModel):
6+
object_description: str = Field(..., description="A description of the object to generate")
7+
goal: str = Field(..., description="The goal of the generation process")
8+
max_tokens: int = Field(1000, description="The maximum number of tokens to generate")
9+
510
class GenerateObjectAgent:
6-
def __init__(self, object_description: str, goal: str, max_tokens: int = 1000):
7-
self.object_description = object_description
8-
self.goal = goal
9-
self.max_tokens = max_tokens
11+
"""
12+
A class to generate objects based on a given description and goal using the OpenAI API.
13+
14+
Attributes:
15+
object_description (str): A description of the object to generate.
16+
goal (str): The goal of the generation process.
17+
max_tokens (int): The maximum number of tokens to generate.
18+
openai_client (OpenAIClient): An instance of OpenAIClient to interact with the API.
19+
20+
Methods:
21+
generate_object(): Generates an object based on the description and goal.
22+
"""
23+
24+
def __init__(self, data: ObjectGenerationInput):
25+
"""
26+
Constructs all the necessary attributes for the GenerateObjectAgent object.
27+
28+
Args:
29+
data (ObjectGenerationInput): An instance of ObjectGenerationInput containing
30+
the object description, goal, and max_tokens.
31+
"""
32+
self.object_description = data.object_description
33+
self.goal = data.goal
34+
self.max_tokens = data.max_tokens
1035
self.openai_client = OpenAIClient()
1136

1237
async def generate_object(self) -> Dict:
38+
"""
39+
Generates an object based on the given description and goal.
40+
41+
Returns:
42+
dict: A dictionary containing the original object description and the generated object.
43+
"""
1344
system_prompt = f"You are an assistant tasked with generating objects based on a given description. The goal is: {self.goal}."
1445
user_prompt = f"Generate an object based on the following description: {self.object_description}."
1546

0 commit comments

Comments
 (0)