Skip to content

Commit

Permalink
More modules
Browse files Browse the repository at this point in the history
  • Loading branch information
seanchatmangpt committed Feb 26, 2024
1 parent cee7d8b commit 31a4301
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 78 deletions.
6 changes: 6 additions & 0 deletions src/dspygen/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ def startup_event() -> None:
def read_root() -> str:
"""Read root."""
return "Hello world"


# Define endpoint
@app.get("/pingpong")
def ping_pong():
return {"message": "pong"}
2 changes: 1 addition & 1 deletion src/dspygen/modules/file_name_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typer import Typer
from inflection import underscore

app = Typer()
app = Typer(help="Generate a file name from any text if no text is provided it will use clipboard.")


class FileContentToFileNameModule(dspy.Module):
Expand Down
109 changes: 109 additions & 0 deletions src/dspygen/modules/gen_dspy_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import dspy
from pydantic import BaseModel, Field
from typer import Typer

from dspygen.modules.gen_pydantic_instance_module import gen_pydantic_instance_call
from dspygen.modules.source_code_pep8_docs_module import source_code_docs_call
from dspygen.utils.dspy_tools import init_dspy
from typetemp.functional import render

app = Typer()


class DSPyModuleTemplate(BaseModel):
"""{{ inputs }} -> {{ outputs }}"""
inputs: list[str] = Field(..., description="Inputs for dspy.Module jinja template.")
output: str = Field(..., description="Output for dspy.Module jinja template.")
class_name: str = Field(..., description="Class name combining the inputs and outputs")


dspy_module_template = '''"""
{{ docstring }}
"""
import dspy
from typer import Typer
from dspygen.utils.dspy_tools import init_dspy
app = Typer()
{%- set module_name = model.class_name | class_name ~ "Module" %}
{%- set inputs_join = model.inputs | join(', ') %}
{%- set inputs_join_kwargs = model.inputs | map('to_kwarg') | join(', ') %}
{% set var_name = model.class_name | var_name %}
class {{ module_name }}(dspy.Module):
"""{{ module_name }}"""
def forward(self, {{ inputs_join }}):
pred = dspy.Predict("{{ inputs_join }} -> {{ model.output }}")
result = pred({{ inputs_join_kwargs }}).{{ model.output }}
return result
def {{ var_name }}_call({{ inputs_join }}):
{{ var_name }} = {{ module_name }}()
return {{ var_name }}.forward({{ inputs_join_kwargs }})
@app.command()
def call({{ inputs_join }}):
"""{{ module_name }}"""
init_dspy()
print({{ var_name }}_call({{ inputs_join_kwargs }}))
def main():
init_dspy()
{% for input in model.inputs %}
{{ input }} = ""
{% endfor %}
print({{ var_name }}_call({{ inputs_join_kwargs }}))
if __name__ == "__main__":
main()
'''


class SignatureDspyModuleModule(dspy.Module):
"""SignatureDspyModuleModule"""

def forward(self, signature):
tmpl_model = gen_pydantic_instance_call(signature, DSPyModuleTemplate)

source = render(dspy_module_template, model=tmpl_model, docstring="")

docs = source_code_docs_call(source)

source = render(dspy_module_template, model=tmpl_model, docstring=docs)

return source


def gen_dspy_module_call(signature):
signature_dspy_module = SignatureDspyModuleModule()
return signature_dspy_module.forward(signature=signature)


@app.command()
def call(signature):
"""SignatureDspyModuleModule"""
init_dspy()

print(gen_dspy_module_call(signature=signature))


def main():
init_dspy()

signature = "subject -> newsletter_article"
print(gen_dspy_module_call(signature=signature))


if __name__ == "__main__":
main()
42 changes: 42 additions & 0 deletions src/dspygen/modules/insight_tweet_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
The source code is used to import the necessary libraries and modules for the program. It also defines a class called "InsightTweetModule" which contains a function called "forward" that takes in an "insight" parameter and returns a result. The "insight_tweet_call" function uses the "InsightTweetModule" class to call the "forward" function and return the result. The "call" function is used to initialize the program and print the result of the "insight_tweet_call" function. The "main" function is used to initialize the program and print the result of the "insight_tweet_call" function.
"""
import dspy
import pyperclip
from typer import Typer
from dspygen.utils.dspy_tools import init_dspy


app = Typer()


class InsightTweetModule(dspy.Module):
"""InsightTweetModule"""

def forward(self, insight):
pred = dspy.Predict("insight -> tweet")
result = pred(insight=insight).tweet
return result


def insight_tweet_call(insight):
insight_tweet = InsightTweetModule()
return insight_tweet.forward(insight=insight)


@app.command()
def call(insight):
"""InsightTweetModule"""
init_dspy()

print(insight_tweet_call(insight=insight))


def main():
init_dspy()
insight = pyperclip.paste()
print(insight_tweet_call(insight=insight))


if __name__ == "__main__":
main()
36 changes: 36 additions & 0 deletions src/dspygen/modules/python_source_code_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import dspy
from typer import Typer


app = Typer()


class PromptPep8PythonSourceCodeModule(dspy.Module):
"""Verbose Documentation for the DSPy Module"""

def forward(self, prompt):
pred = dspy.Predict("prompt -> pep8_python_source_code")
result = pred(prompt=prompt).pep8_python_source_code
return result


def python_source_code_call(prompt):
prompt_pep8_python_source_code = PromptPep8PythonSourceCodeModule()
return prompt_pep8_python_source_code.forward(prompt=prompt)


@app.command(name="call")
def module_test(prompt):
"""Verbose Documentation for the DSPy Module"""
print(python_source_code_call(prompt=prompt))


def main():
lm = dspy.OpenAI(max_tokens=500)
dspy.settings.configure(lm=lm)
prompt = "Hello World def with print FastAPI call with import"
print(python_source_code_call(prompt=prompt))


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
from typer import Typer


app = Typer()


lm = dspy.OpenAI(max_tokens=500)
dspy.settings.configure(lm=lm)
app = Typer(help="Create React JSX source code.")


class PromptReactJsxModule(dspy.Module):
Expand All @@ -19,6 +15,9 @@ def forward(self, prompt):


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

prompt = "Hello World Functional Component"

prompt_react_jsx = PromptReactJsxModule()
Expand Down
50 changes: 50 additions & 0 deletions src/dspygen/modules/source_code_pep8_docs_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""The `SourceCodePep8DocsModule` class is used to create documentation for source code using best practices. It
first imports the necessary libraries and modules, including `dspy`, `pyperclip`, and `typer`. Then, in the `forward`
method, it checks if a context is provided and if not, sets a default context. The source code is then formatted and
passed through a `ChainOfThought` model to generate the pep8_docs. The `source_code_pep8_docs_call` function calls
the `forward` method and returns the result. In the `call` command, the source code is passed through `pyperclip` and
the result is printed and copied to the clipboard. The `main` function sets the `lm` variable to use the `OpenAI`
model and calls the `source_code_pep8_docs_call` function with an empty source code to test it."""
import dspy
import pyperclip
import typer
from typer import Typer

from dspygen.utils.dspy_tools import init_dspy

app = Typer()


class SourceCodePep8DocsModule(dspy.Module):
"""SourceCodePep8DocsModule"""

def forward(self, source_code):
pred = dspy.Predict("source_code -> documentation")
result = pred(source_code=source_code).documentation
return result


def source_code_docs_call(source_code):
source_code_pep8_docs = SourceCodePep8DocsModule()
return source_code_pep8_docs.forward(source_code=source_code)


@app.command()
def call(source_code=pyperclip.paste()):
"""SourceCodePep8DocsModule"""
init_dspy(max_tokens=3000)

result = source_code_docs_call(source_code=source_code)
typer.echo(result)
pyperclip.copy(result)


def main():
init_dspy(max_tokens=3000)

source_code = ""
print(source_code_docs_call(source_code=source_code))


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""The source code is importing the necessary libraries and creating a Typer app. It also defines a class for a
module that takes in subject, destination, and audience as inputs and returns a newsletter article. The function
"subject_destination_audience_newsletter_article_call" calls this module and returns the result. The Typer app has a
command "call" that takes in subject, destination, and audience as arguments and prints the result of calling the
module. The main function initializes the Typer app and calls the
"subject_destination_audience_newsletter_article_call" function with empty inputs."""
import dspy
from typer import Typer
from dspygen.utils.dspy_tools import init_dspy


app = Typer()


class SubjectDestinationAudienceNewsletterArticleModule(dspy.Module):
"""SubjectDestinationAudienceNewsletterArticleModule"""

def forward(self, subject, destination, audience):
pred = dspy.Predict("subject, destination, audience -> newsletter_article")
result = pred(subject=subject, destination=destination, audience=audience).newsletter_article
return result


def subject_destination_audience_newsletter_article_call(subject, destination, audience):
subject_destination_audience_newsletter_article = SubjectDestinationAudienceNewsletterArticleModule()
return subject_destination_audience_newsletter_article.forward(subject=subject, destination=destination, audience=audience)


@app.command()
def call(subject, destination, audience):
"""SubjectDestinationAudienceNewsletterArticleModule"""
init_dspy()

print(subject_destination_audience_newsletter_article_call(subject=subject, destination=destination, audience=audience))


def main():
init_dspy(max_tokens=3000)
subject = "Language Models in the year 2050"
destination = "LinkedIn"
audience = "Non technical people over the age of 60"
print(subject_destination_audience_newsletter_article_call(subject=subject, destination=destination, audience=audience))


if __name__ == "__main__":
main()
6 changes: 3 additions & 3 deletions src/dspygen/modules/text_summary_module_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
app = Typer()


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


class TextSummaryModuleModule(dspy.Module):
"""A DSPy Module that takes in text and produces a summary."""
Expand All @@ -24,6 +21,9 @@ def text_summary_module_call(text):


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

text = ""
print(text_summary_module_call(text=text))

Expand Down
Loading

0 comments on commit 31a4301

Please sign in to comment.