-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cee7d8b
commit 31a4301
Showing
10 changed files
with
301 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
46 changes: 46 additions & 0 deletions
46
src/dspygen/modules/subject_destination_audience_newsletter_article_module.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.