Skip to content

Instantly share code, notes, and snippets.

@seanchatmangpt
seanchatmangpt / gen_python_primitive.py
Created January 23, 2024 21:33
Create any python primitive with DSPy
import ast
from dspy import Module, OpenAI, settings, ChainOfThought, Assert
class GenPythonPrimitive(Module):
def __init__(self, primitive_type, lm=None):
if lm is None:
turbo = OpenAI(max_tokens=500)
@seanchatmangpt
seanchatmangpt / gen_module.py
Created January 30, 2024 18:48
A module with self-correction
import logging # Import the logging module
from dspy import Module, OpenAI, settings, ChainOfThought, Assert
logger = logging.getLogger(__name__) # Create a logger instance
logger.setLevel(logging.ERROR) # Set the logger's level to ERROR or the appropriate level
class GenModule(Module):
def __init__(self, output_key, input_keys: list[str] = None, lm=None):
if lm is None:
@seanchatmangpt
seanchatmangpt / gen_python_primitive.py
Created January 30, 2024 18:48
Generate a python primitive
import ast
from dspy import Assert
from rdddy.generators.gen_module import GenModule
def is_primitive_type(data_type):
primitive_types = {
int, float, str, bool, list, tuple, dict, set
import ast
import dspy
from typing import Dict, Any, Optional
lm = dspy.OpenAI(max_tokens=500)
dspy.settings.configure(lm=lm)
import inspect
from dspy import Assert
@seanchatmangpt
seanchatmangpt / gen_pydantic_instance.py
Last active April 14, 2024 20:11
Convert your prompt into a pydantic instance.
import ast
import logging
import inspect
from typing import Type, TypeVar
from dspy import Assert, Module, ChainOfThought, Signature, InputField, OutputField
from pydantic import BaseModel, ValidationError
logger = logging.getLogger(__name__)
logger.setLevel(logging.ERROR)
@seanchatmangpt
seanchatmangpt / signature_factory.py
Created February 1, 2024 00:14
Create Signature classes at runtime.
import dspy
from dspy import Signature
from dspy.signatures.field import InputField, OutputField
from pydantic import BaseModel, Field
from rdddy.generators.gen_pydantic_instance import GenPydanticInstance
class InputFieldModel(BaseModel):
@seanchatmangpt
seanchatmangpt / contact_model.py
Last active October 3, 2024 07:17
Generate a pydantic class from a prompt
from datetime import datetime
from pydantic import BaseModel, Field, validator, root_validator, EmailStr, UrlStr
class ContactModel(BaseModel):
"""A Pydantic model representing a contact in the friend of a friend ontology."""
name: str = Field(default=None, title="", description="The name of the contact.", min_length=2, max_length=50)
email: EmailStr = Field(default=None, title="", description="The email address of the contact.", min_length=5, max_length=50)
phone_number: str = Field(default=None, title="", description="The phone number of the contact.", min_length=10, max_length=15)
address: str = Field(default=None, title="", description="The address of the contact.", min_length=10, max_length=100)
@seanchatmangpt
seanchatmangpt / VAVAILABILITYModel.py
Last active September 21, 2024 10:23
Just created 7 icalendar models in one function
from pydantic import BaseModel, Field, validator, root_validator, EmailStr, UrlStr
from typing import List, Optional
from datetime import datetime
class VAVAILABILITYModel(BaseModel):
"""A Pydantic model for RFC 5545 compliance."""
dtstart: datetime = Field(default=None, title="", description="The start date and time of the event.", required)
dtend: datetime = Field(default=None, title="", description="The end date and time of the event.", required)
summary: str = Field(default=None, title="", description="A brief summary or title of the event.", max_length=255)
@seanchatmangpt
seanchatmangpt / gen_pydantic_instance.py
Created February 6, 2024 18:15
Create a pydantic instance from a class
import ast
import logging
import inspect
from typing import Type, TypeVar
from dspy import Assert, Module, ChainOfThought, Signature, InputField, OutputField
from pydantic import BaseModel, ValidationError
logger = logging.getLogger(__name__)
logger.setLevel(logging.ERROR)
@seanchatmangpt
seanchatmangpt / test_gen_pydantic_instance.py
Created February 6, 2024 18:16
Unit tests for generating a pydantic instance
from rdddy.generators.gen_pydantic_instance import (
GenPydanticInstance,
)
import pytest
from unittest.mock import patch, MagicMock
from dspy import settings, OpenAI, DSPyAssertionError
from typing import Dict, Any, Optional
from pydantic import BaseModel, Field, ValidationError