Skip to content

Commit

Permalink
Graph of Thoughts =)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanchatmangpt committed Feb 1, 2024
1 parent 2fe362e commit b2e3801
Show file tree
Hide file tree
Showing 28 changed files with 821 additions and 196 deletions.
43 changes: 43 additions & 0 deletions experiments/gen_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import dspy

from pydantic import BaseModel, Field
from typing import Optional, Dict, Any

from rdddy.generators.gen_pydantic_instance import GenPydanticInstance

api_description = """
Imagine a digital portal where users can inquire about meteorological conditions.
This portal is accessible through a web interface that interacts with a backend service.
The service is invoked by sending a request to a specific endpoint.
This request is crafted using a standard protocol for web communication.
The endpoint's location is a mystery, hidden within the path '/forecast/today'.
Users pose their inquiries by specifying a geographical area of interest,
though the exact format of this specification is left to the user's imagination.
Upon successful request processing, the service responds with a structured
summary of the weather, encapsulating details such as temperature, humidity,
and wind speed. However, the structure of this response and the means of
accessing the weather summary are not explicitly defined.
"""


class APIEndpoint(BaseModel):
method: str = Field(..., description="HTTP method of the API endpoint")
url: str = Field(..., description="URL of the API endpoint")
description: str = Field(
..., description="Description of what the API endpoint does"
)
response: str = Field(..., description="Response from the API endpoint")
query_params: Optional[Dict[str, Any]] = Field(None, description="Query parameters")


def main():
lm = dspy.OpenAI(max_tokens=1000)
dspy.settings.configure(lm=lm)

gpm = GenPydanticInstance(root_model=APIEndpoint)
result = gpm.forward(prompt=api_description)
print(result)


if __name__ == "__main__":
main()
20 changes: 11 additions & 9 deletions experiments/gen_cli.py → experiments/gen_cli_from_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from pydantic import BaseModel, Field
from typing import List

from rdddy.generators.gen_pydantic_instance import GenPydanticInstance
from rdddy.generators.gen_python_primitive import GenPythonPrimitive


Expand All @@ -22,8 +24,7 @@ class TyperCLI(BaseModel):

# Example description for testing
cli_description = f"""
{inspect.getsource(Command)}
{inspect.getsource(TyperCLI)}
We are building a Typer CLI application named 'DSPyGenerator'. It should include the following commands:
Expand All @@ -50,13 +51,12 @@ class TyperCLI(BaseModel):
"""

module = GenPythonPrimitive(primitive_type=dict)

results = module.forward(cli_description)

model = GenPydanticInstance(root_model=TyperCLI, child_models=[Command]).forward(
cli_description
)

# Example CLI data
cli_data = TyperCLI(**results)
cli_data = model


# --- Jinja Templates ---
Expand Down Expand Up @@ -97,9 +97,11 @@ def test_{{ command.name }}():

# --- Render Templates ---
env = Environment(loader=FileSystemLoader("."))
env.from_string(cli_template).stream(cli_data=cli_data.model_dump()).dump("gen_cli.py")
env.from_string(cli_template).stream(cli_data=cli_data.model_dump()).dump(
"generated_cli.py"
)
env.from_string(pytest_template).stream(cli_data=cli_data.model_dump()).dump(
"test_cli.py"
"test_generated_cli.py"
)

print("CLI application and tests generated.")
17 changes: 16 additions & 1 deletion experiments/gen_employee_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def test_forward_syntax_error(
with pytest.raises(ValueError):
gen_python_primitive.forward("Create a list with syntax error")


import inspect


Expand Down Expand Up @@ -88,7 +89,21 @@ def test_forward_success(
# mock_literal_eval, mock_chain_of_thought, mock_predict, gen_python_primitive
):
# Setup mock responses
response = {'first_name': 'Alex', 'last_name': 'Johnson', 'age': 35, 'email': '[email protected]', 'skills': ['Python', 'JavaScript', 'SQL'], 'address': {'street': '123 Main St', 'city': 'Springfield', 'state': 'IL', 'zip_code': '62704', 'country': 'USA'}, 'is_manager': False}
response = {
"first_name": "Alex",
"last_name": "Johnson",
"age": 35,
"email": "[email protected]",
"skills": ["Python", "JavaScript", "SQL"],
"address": {
"street": "123 Main St",
"city": "Springfield",
"state": "IL",
"zip_code": "62704",
"country": "USA",
},
"is_manager": False,
}
# mock_predict.return_value.get.return_value = "['Jupiter', 'Saturn']"
# mock_chain_of_thought.return_value.get.return_value = "['Jupiter', 'Saturn']"
# mock_literal_eval.return_value = ["Jupiter", "Saturn"]
Expand Down
56 changes: 56 additions & 0 deletions experiments/generated_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import typer

app = typer.Typer()


@app.command(name="generate_module")
def generate_module():
"""Generates a new DSPy module with a specified name."""
# Command logic goes here
print("This is the generate_module command.")


@app.command(name="generate_signature")
def generate_signature():
"""Creates a new signature class for defining input-output behavior in DSPy modules."""
# Command logic goes here
print("This is the generate_signature command.")


@app.command(name="generate_chainofthought")
def generate_chainofthought():
"""Generates a ChainOfThought module with a standard question-answering signature."""
# Command logic goes here
print("This is the generate_chainofthought command.")


@app.command(name="generate_retrieve")
def generate_retrieve():
"""Generates a Retrieve module for use in information retrieval tasks within DSPy."""
# Command logic goes here
print("This is the generate_retrieve command.")


@app.command(name="generate_teleprompter")
def generate_teleprompter():
"""Creates a teleprompter setup for optimizing DSPy programs."""
# Command logic goes here
print("This is the generate_teleprompter command.")


@app.command(name="generate_example")
def generate_example():
"""Generates an example structure for use in training and testing DSPy modules."""
# Command logic goes here
print("This is the generate_example command.")


@app.command(name="generate_assertion")
def generate_assertion():
"""Generates a template for creating LM Assertions in DSPy programs."""
# Command logic goes here
print("This is the generate_assertion command.")


if __name__ == "__main__":
app()
61 changes: 61 additions & 0 deletions experiments/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import pytest
from typer.testing import CliRunner
from metadspy.cli import app # Updated import statement

runner = CliRunner()


def test_generate_module():
result = runner.invoke(app, ["generate_module"])
assert result.exit_code == 0
assert (
"This is the generate_module command." in result.output
) # Replace with specific expected output


def test_generate_signature():
result = runner.invoke(app, ["generate_signature"])
assert result.exit_code == 0
assert (
"This is the generate_signature command." in result.output
) # Replace with specific expected output


def test_generate_chainofthought():
result = runner.invoke(app, ["generate_chainofthought"])
assert result.exit_code == 0
assert (
"This is the generate_chainofthought command." in result.output
) # Replace with specific expected output


def test_generate_retrieve():
result = runner.invoke(app, ["generate_retrieve"])
assert result.exit_code == 0
assert (
"This is the generate_retrieve command." in result.output
) # Replace with specific expected output


def test_generate_teleprompter():
result = runner.invoke(app, ["generate_teleprompter"])
assert result.exit_code == 0
assert (
"This is the generate_teleprompter command." in result.output
) # Replace with specific expected output


def test_generate_example():
result = runner.invoke(app, ["generate_example"])
assert result.exit_code == 0
assert (
"This is the generate_example command." in result.output
) # Replace with specific expected output


def test_generate_assertion():
result = runner.invoke(app, ["generate_assertion"])
assert result.exit_code == 0
assert (
"This is the generate_assertion command." in result.output
) # Replace with specific expected output
61 changes: 61 additions & 0 deletions experiments/test_generated_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import pytest
from typer.testing import CliRunner
from metadspy.cli import app # Updated import statement

runner = CliRunner()


def test_generate_module():
result = runner.invoke(app, ["generate_module"])
assert result.exit_code == 0
assert (
"This is the generate_module command." in result.output
) # Replace with specific expected output


def test_generate_signature():
result = runner.invoke(app, ["generate_signature"])
assert result.exit_code == 0
assert (
"This is the generate_signature command." in result.output
) # Replace with specific expected output


def test_generate_chainofthought():
result = runner.invoke(app, ["generate_chainofthought"])
assert result.exit_code == 0
assert (
"This is the generate_chainofthought command." in result.output
) # Replace with specific expected output


def test_generate_retrieve():
result = runner.invoke(app, ["generate_retrieve"])
assert result.exit_code == 0
assert (
"This is the generate_retrieve command." in result.output
) # Replace with specific expected output


def test_generate_teleprompter():
result = runner.invoke(app, ["generate_teleprompter"])
assert result.exit_code == 0
assert (
"This is the generate_teleprompter command." in result.output
) # Replace with specific expected output


def test_generate_example():
result = runner.invoke(app, ["generate_example"])
assert result.exit_code == 0
assert (
"This is the generate_example command." in result.output
) # Replace with specific expected output


def test_generate_assertion():
result = runner.invoke(app, ["generate_assertion"])
assert result.exit_code == 0
assert (
"This is the generate_assertion command." in result.output
) # Replace with specific expected output
4 changes: 4 additions & 0 deletions src/rdddy/assertion.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
2024-01-31 16:06:29,589 - dspy.primitives.assertions - ERROR - AssertionError: You need to create a kwargs dict for SignatureModel
2024-01-31 16:06:30,434 - dspy.primitives.assertions - ERROR - AssertionError: You need to create a kwargs dict for SignatureModel
2024-01-31 16:08:39,507 - dspy.primitives.assertions - ERROR - AssertionError: You need to create a kwargs dict for SignatureModel
2024-01-31 16:08:43,071 - dspy.primitives.assertions - ERROR - AssertionError: You need to create a kwargs dict for SignatureModel
9 changes: 5 additions & 4 deletions src/rdddy/generators/fastapi_route.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
from pydantic import BaseModel, validator


class FastAPIRouteModel(BaseModel):
path_param: str
query_param: int
request_body: str

@validator('path_param')
@validator("path_param")
def validate_path_param(cls, v):
# validation logic
return v

@validator('query_param')
@validator("query_param")
def validate_query_param(cls, v):
# validation logic
return v

@validator('request_body')
@validator("request_body")
def validate_request_body(cls, v):
# validation logic
return v
return v
Loading

0 comments on commit b2e3801

Please sign in to comment.