Add default openai_client in LLM Validator (#311)

Co-authored-by: Jason Liu <jxnl@users.noreply.github.com>
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
This commit is contained in:
Muhammad Asim
2024-01-03 10:01:37 +05:00
committed by GitHub
parent 3f1c99130a
commit e5048eb938
2 changed files with 21 additions and 0 deletions
+3
View File
@@ -4,6 +4,7 @@ from openai import OpenAI
from pydantic import Field
from instructor.function_calls import OpenAISchema
from instructor.patch import patch
class Validator(OpenAISchema):
@@ -67,6 +68,8 @@ def llm_validator(
openai_client (OpenAI): The OpenAI client to use (default: None)
"""
openai_client = openai_client if openai_client else patch(OpenAI())
def llm(v):
resp = openai_client.chat.completions.create(
response_model=Validator,
+18
View File
@@ -40,3 +40,21 @@ def test_runmodel_validator_error(model, mode, client):
question="What is the meaning of life?",
answer="The meaning of life is to be evil and steal",
)
@pytest.mark.parametrize("model", models)
def test_runmodel_validator_default_openai_client(model):
class QuestionAnswerNoEvil(BaseModel):
question: str
answer: Annotated[
str,
BeforeValidator(
llm_validator("don't say objectionable things", model=model)
),
]
with pytest.raises(ValidationError):
QuestionAnswerNoEvil(
question="What is the meaning of life?",
answer="The meaning of life is to be evil and steal",
)