From 052f1362270cc0f1fc4f80d60665c0e3eb46bfb2 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 13 Sep 2023 22:03:56 -0400 Subject: [PATCH] async --- examples/validators/allm_validator.py | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 examples/validators/allm_validator.py diff --git a/examples/validators/allm_validator.py b/examples/validators/allm_validator.py new file mode 100644 index 0000000..c5c5253 --- /dev/null +++ b/examples/validators/allm_validator.py @@ -0,0 +1,46 @@ +import asyncio +from typing_extensions import Annotated +from pydantic import BaseModel, BeforeValidator +from instructor import llm_validator, patch +import openai + +patch() + + +class QuestionAnswerNoEvil(BaseModel): + question: str + answer: Annotated[ + str, + BeforeValidator( + llm_validator("don't say objectionable things", allow_override=True) + ), + ] + + +async def main(): + context = "The according to the devil is to live a life of sin and debauchery." + question = "What is the meaning of life?" + + try: + qa: QuestionAnswerNoEvil = await openai.ChatCompletion.acreate( + model="gpt-3.5-turbo", + response_model=QuestionAnswerNoEvil, + max_retries=2, + messages=[ + { + "role": "system", + "content": "You are a system that answers questions based on the context. Answer exactly what the question asks using the context.", + }, + { + "role": "user", + "content": f"using the context: {context}\n\nAnswer the following question: {question}", + }, + ], + ) # type: ignore + print(qa) + except Exception as e: + print(e) + + +if __name__ == "__main__": + asyncio.run(main())