Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PydanticUserError when instantiating SyntheticDataGenerator #24

Open
zakhikhan opened this issue Dec 10, 2024 · 3 comments
Open

PydanticUserError when instantiating SyntheticDataGenerator #24

zakhikhan opened this issue Dec 10, 2024 · 3 comments

Comments

@zakhikhan
Copy link

zakhikhan commented Dec 10, 2024

I am attempting to use this library to generate tabular synthetic data. Any time I try to instantiate the SyntheticDataGenerator class from tabular_synthetic_data (either through the create_openai_data_generator function or directly), I get an error like the below:

PydanticUserError: SyntheticDataGenerator is not fully defined; you should define BaseCache, then call SyntheticDataGenerator.model_rebuild().

For further information visit https://errors.pydantic.dev/2.10/u/class-not-fully-defined

versions (all installed via pip)
langchain_experimental: 0.3.3
langchain: 0.3.11
langchain_openai 0.2.12
langchain_core: 0.3.24

Here is simplified example code that causes the error:

from langchain_openai import ChatOpenAI
from langchain_core.prompts import FewShotPromptTemplate
from langchain_experimental.tabular_synthetic_data.base import SyntheticDataGenerator
from pydantic import BaseModel

OPENAI_TEMPLATE = PromptTemplate(input_variables=["example"], template="{example}")

examples = [
    {"example": "toothbrush_name: Crest Brush, price: 10"},
    {"example": "toothbrush_name: Generic toothbrush, price: 5"}
]

class ToothbrushSchema(BaseModel):
    toothbrush_name: str
    price: int
    
output_schema=ToothbrushSchema

llm=ChatOpenAI(temperature=1)

prompt_template = FewShotPromptTemplate(
    examples=examples,
    suffix=("Generate synthetic data about {subject}."),
    input_variables=["subject"],
    example_prompt=OPENAI_TEMPLATE,
)

prompt=prompt_template

chain = llm.with_structured_output(ToothbrushSchema)

sdg = SyntheticDataGenerator(template=prompt_template,llm_chain=chain)
@zakhikhan
Copy link
Author

zakhikhan commented Dec 11, 2024

I got it to work by creating a dev install of langchain_experimental and editing line 27 of tabular_synthetic_data/base.py in the SyntheticDataGenerator class

BEFORE:
llm: Optional[BaseLanguageModel] = None

AFTER:
llm: Optional[BaseChatModel] = None

I am not sure why this worked. BaseLanguageModel is the parent class of BaseChatModel.

@shub7389
Copy link

Hi,
Please can you share the git repo link or collab link with the updated code?

@JeremyDeng-xin
Copy link

JeremyDeng-xin commented Feb 22, 2025

i got the same error
this is code
`from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate
from langchain_experimental.tabular_synthetic_data.openai import create_openai_data_generator
from langchain_experimental.tabular_synthetic_data.prompts import SYNTHETIC_FEW_SHOT_PREFIX, SYNTHETIC_FEW_SHOT_SUFFIX
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
import os

from pydantic import BaseModel, Field

os.environ['LANGCHAIN_TRACING_V2'] = "true"
os.environ['LANGCHAIN_API_KEY'] = "lsv2_pt_xxx"

model = ChatOpenAI(model='gpt-3.5-turbo', temperature=0.8)

class MedicalBilling(BaseModel):
patient_id: int = Field(..., alias='patientId')
patient_name: str = Field(..., alias='patientName')
diagnosis: str = Field(..., alias='diagnosis')
procedure: str = Field(..., alias='procedure')
total_charge: float = Field(..., alias='totalCharge')
insurance_claim_amount: float = Field(..., alias='insurance claim amount')

examples = [
{
"example": "Patient ID: 123456, Patient name: 刘娜, Diagnosis: J20.9, Procedure: z456, Total Charge: 653.8, insurance claim amount: 1.00",
},
{
"example": "Patient ID: 674357, Patient name: 马钢, Diagnosis: J10.6, Procedure: z496, Total Charge: 5653.8, insurance claim amount: 5000.00",
},
{
"example": "Patient ID: 746565, Patient name: 冯亮, Diagnosis: E28.9, Procedure: z056, Total Charge: 6993.8, insurance claim amount: 1000.00",
}
]
openai_template = PromptTemplate(input_variables=['example'], template="{example}")

prompt_template = FewShotPromptTemplate(
prefix=SYNTHETIC_FEW_SHOT_PREFIX,
suffix=SYNTHETIC_FEW_SHOT_SUFFIX,
examples=examples,
example_prompt=openai_template,
input_variables=['subject','extra'],
)
generator = create_openai_data_generator(
output_schema=MedicalBilling,
llm=model,
prompt=prompt_template,
)
result = generator.generate(
subject='医疗帐单',
extra='名字可以是随机的,最好使用比较生僻的人名。账单金额大一点',
runs=10 # records
)
print(result)`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants