Skip to content

Commit

Permalink
Module renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
seanchatmangpt committed Feb 24, 2024
1 parent 3999460 commit ed27ff2
Show file tree
Hide file tree
Showing 12 changed files with 410 additions and 25 deletions.
51 changes: 51 additions & 0 deletions new_module_4987462224.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import dspy
from typer import Typer

app = Typer()


lm = dspy.OpenAI(max_tokens=500)
dspy.settings.configure(lm=lm)


class BookDescToProductInfo(dspy.Module):
"""Converts book descriptions to product information"""

def forward(self, book_desc):
pred = dspy.Predict("book_desc -> product_info")

result = pred(book_desc=book_desc).product_info
return result

def main():

book_desc = """If you’re a business leader, you already know that Lean Six Sigma is one of the most popular and powerful business tools in the world today. You also probably know that implementing the process can be more than a little challenging. This step-by-step guide shows you how to customize and apply the principles of Lean Six Sigma to your own organizational needs, giving you more options, strategies, and solutions than you’ll find in any other book on the subject. With these simple, proven techniques, you can:
* Assess your current business model and shape your future goals
* Plan and prepare a Lean Six Sigma program that’s right for your company
* Engage your leadership and your team throughout the entire process
* Align your LSS efforts with the culture and values of your business
* Develop deeper insights into your customer experience
* Master the art of project selection and pipeline management
* Tackle bigger problems and find better solutions
* Become more efficient, more productive, and more profitable
This innovative approach to the Lean Six Sigma process allows you to mold and shape your strategy as you go, making small adjustments along the way that can have a big impact. In this book, you’ll discover the most effective methods for deploying LSS at every level, from the leaders at the top to the managers in the middle to the very foundation of your company culture. You’ll hear from leading business experts who have guided companies through the LSS process―and get the inside story on how they turned those companies around. You’ll also learn how to use the latest, greatest management tools like Enterprise Kaizen, Customer Journey Maps, and Hoshin Planning. Everything you need to implement Lean Six Sigma―smoothly and successfully―is right here at your fingertips. Also included is a special chapter focusing exclusively on how to implement Lean Six Sigma in healthcare.
When it comes to running a business, there is no better way to improve efficiency, increase productivity, and escalate profits than Lean Six Sigma. And there is no better book on how to make it work than Innovating Lean Six Sigma.""" # Initialize your inputs here. Adjust as necessary.

book_desc_to_product_info = BookDescToProductInfo()
print(book_desc_to_product_info.forward(book_desc=book_desc))


# @app.command()
# def module_test(book_desc):
# """Converts book descriptions to product information"""
# book_desc_to_product_info = BookDescToProductInfo()
#
# print(book_desc_to_product_info.forward(book_desc=book_desc))


if __name__ == "__main__":
# app()
main()
39 changes: 39 additions & 0 deletions new_module_5206162896.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import dspy
from typer import Typer

app = Typer()


lm = dspy.OpenAI(max_tokens=500)
dspy.settings.configure(lm=lm)


class SubjectToBlog(dspy.Module):
"""This module takes in a subject and outputs a blog post."""

def forward(self, subject):
pred = dspy.Predict("subject -> blog_post")

result = pred(subject=subject).blog_post
return result

def main():

subject = "Summer fun" # Initialize your inputs here. Adjust as necessary.

ds_py_module_template = SubjectToBlog()
print(ds_py_module_template.forward(subject=subject))


@app.command()
def module_test(subject):
"""This module takes in a subject and outputs a blog post."""
ds_py_module_template = SubjectToBlog()

print(ds_py_module_template.forward(subject=subject))


if __name__ == "__main__":
# app()
main()

107 changes: 107 additions & 0 deletions openai_usage.log
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,110 @@ NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
NumExpr defaulting to 8 threads.
HTTP Request: POST https://api.openai.com/v1/completions "HTTP/1.1 200 OK"
15 changes: 15 additions & 0 deletions src/experiments/ping_pong_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import dspy

lm = dspy.OpenAI(max_tokens=500)
dspy.settings.configure(lm=lm)


class PingPongModule(dspy.Module):
"""A module that simulates a game of ping pong."""

def forward(self, player1, player2):
pred = dspy.Predict("player1, player2 -> winner")

result = pred(player1, player2).winner

return result
16 changes: 0 additions & 16 deletions src/rdddy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,3 @@ async def process_transcript(data: TranscriptData):
)
print(cot)
return {"message": f"Transcript received: {cot}"}


@app.on_event("startup")
def startup_event() -> None:
"""Run API startup events."""
# Remove all handlers associated with the root logger object.
for handler in logging.root.handlers:
logging.root.removeHandler(handler)
# Add coloredlogs' coloured StreamHandler to the root logger.
coloredlogs.install()


@app.get("/")
def read_root() -> str:
"""Read root."""
return "Hello world"
90 changes: 89 additions & 1 deletion src/rdddy/cli.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,101 @@
import dspy

import typer
from jinja2 import Environment
from pydantic import BaseModel, Field
from typer import Typer

from rdddy.generators.gen_pydantic_class import GenPydanticClass
from rdddy.generators.gen_pydantic_instance import GenPydanticInstance
from typetemp.extension.inflection_extension import InflectionExtension


app = Typer()


def setup_jinja_env():
env = Environment()
env.add_extension(InflectionExtension)

env.filters['to_kwarg'] = lambda input_name: f"{input_name}={input_name}"
return env


class DSPyModuleTemplate(BaseModel):
'''
class {{ model.class_name | camelize }}(dspy.Module):
"""{{ model.docstring }}"""
def forward(self, {{ model.inputs | join(', ') }}):
pred = dspy.Predict("{{ model.inputs | join(', ') }} -> {{ model.output }}")
result = pred({{ model.inputs | map('to_kwarg') | join(', ') }}).{{ model.output }}
return result
'''
class_name: str = Field(..., description="Class name of the DSPy Module. Do not include DSPy in the class_name")
docstring: str = Field(..., description="Documentation for the DSPy Module")
inputs: list[str] = Field(..., description="Inputs for dspy.Module")
output: str = Field(..., description="Output for dspy.Module")


dspy_module_template = '''import dspy
from typer import Typer
app = Typer()
lm = dspy.OpenAI(max_tokens=500)
dspy.settings.configure(lm=lm)
class {{ model.class_name | camelize }}(dspy.Module):
"""{{ model.docstring }}"""
def forward(self, {{ model.inputs | join(', ') }}):
pred = dspy.Predict("{{ model.inputs | join(', ') }} -> {{ model.output }}")
result = pred({{ model.inputs | map('to_kwarg') | join(', ') }}).{{ model.output }}
return result
def main():
{% for input in model.inputs %}
{{ input }} = "" # Initialize your inputs here. Adjust as necessary.
{% endfor %}
{{ model.class_name | underscore }} = {{ model.class_name | camelize }}()
print({{ model.class_name | underscore }}.forward({{ model.inputs | map('to_kwarg') | join(', ') }}))
@app.command()
def module_test({{ model.inputs | join(', ') }}):
"""{{ model.docstring }}"""
{{ model.class_name | underscore }} = {{ model.class_name | camelize }}()
print({{ model.class_name | underscore }}.forward({{ model.inputs | map('to_kwarg') | join(', ') }}))
if __name__ == "__main__":
app()
# main()
'''


@app.command(name="module")
def gen_module(prompt: str):
lm = dspy.OpenAI(max_tokens=500)
dspy.settings.configure(lm=lm)

env = setup_jinja_env()

template = env.from_string(dspy_module_template)

tmpl_model = GenPydanticInstance(root_model=DSPyModuleTemplate)(prompt)

source = template.render(model=tmpl_model)

with open(f"new_module_{id(tmpl_model)}.py", "w") as file:
file.write(source)

print(source)


@app.command(name="model", short_help="Generates a Pydantic model class.")
Expand Down
8 changes: 4 additions & 4 deletions src/rdddy/generators/gen_pydantic_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ def forward(self, prompt) -> T:

return self.validate_output(corrected_output)

def __call__(self, *args, **kwargs):
return self.forward(kwargs.get("prompt"))
def __call__(self, prompt):
return self.forward(prompt=prompt)


hygen_prompt = prompt = """
hygen_prompt = """
```prompt
Automated Hygen template full stack system for NextJS.
Express
Expand Down Expand Up @@ -215,7 +215,7 @@ def main():
root_model=EventStormingDomainSpecificationModel,
child_models=[AbstractEvent, AbstractCommand, AbstractQuery],
)
model_inst = model_module(prompt=prompt)
model_inst = model_module(hygen_prompt)
print(model_inst)


Expand Down
26 changes: 26 additions & 0 deletions src/rdddy/hello_world_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import dspy

lm = dspy.OpenAI(max_tokens=500)
dspy.settings.configure(lm=lm)


class SummarizeText(dspy.Module):
"""This module summarizes text using a pre-trained model."""

def forward(self, text):
pred = dspy.Predict("text -> summary")

result = pred(text=text).summary
return result


def main():
text = """
In his famous commencement speech delivered at Stanford University in 2005, Steve Jobs emphasized the importance of connecting the dots in life, reflecting on his own journey of personal and professional development. Jobs highlighted how seemingly unrelated experiences and decisions in the past could later align and lead to significant opportunities and achievements. He spoke about how dropping out of college and attending calligraphy classes eventually influenced the design and typography of the Macintosh computer, illustrating the unpredictable but crucial nature of connecting dots in hindsight. This perspective encouraged listeners to trust in their intuition, follow their passions, and have faith that the dots will connect in the future, even if the path forward isn't always clear at the present moment.""" # Initialize your inputs here. Adjust as necessary.

summarize_text = SummarizeText()
print(summarize_text.forward(text=text))


if __name__ == '__main__':
main()
Loading

0 comments on commit ed27ff2

Please sign in to comment.