From 4745032c5fdd712df88be5bd7de190b80e14f6fc Mon Sep 17 00:00:00 2001 From: Bryan Ndjeutcha Date: Wed, 28 Aug 2024 10:22:34 -0400 Subject: [PATCH] feat: add a new input field for the form signature for the json_schema --- pipeline/gpt.py | 44 ++++++++------------------------------------ 1 file changed, 8 insertions(+), 36 deletions(-) diff --git a/pipeline/gpt.py b/pipeline/gpt.py index ae3a8ec..b33f67e 100644 --- a/pipeline/gpt.py +++ b/pipeline/gpt.py @@ -3,44 +3,14 @@ import dspy.adapters import dspy.utils +from pipeline.inspection import FertilizerInspection + MODELS_WITH_RESPONSE_FORMAT = [ "ailab-llm", "ailab-llm-gpt-4o" ] # List of models that support the response_format option -SPECIFICATION = """ -Keys: -"company_name" -"company_address" -"company_website" -"company_phone_number" -"manufacturer_name" -"manufacturer_address" -"manufacturer_website" -"manufacturer_phone_number" -"fertiliser_name" -"registration_number" (a series of letters and numbers) -"lot_number" -"weight" (array of objects with "value", and "unit") -"density" (an object with "value", and "unit") -"volume" (an object with "value", and "unit") -"npk" (format: "number-number-number") **important -"guaranteed_analysis" (array of objects with "nutrient", "value", and "unit") **important -"warranty" -"cautions_en" (array of strings) -"instructions_en" (array of strings) -"micronutrients_en" (array of objects with "nutrient", "value", and "unit") -"ingredients_en" (array of objects with "nutrient", "value", and "unit") -"specifications_en" (array of objects with "humidity", "ph", and "solubility") -"first_aid_en" (array of strings) -"cautions_fr" (array of strings) -"instructions_fr" (array of strings) -"micronutrients_fr" (array of objects with "nutrient", "value", and "unit") -"ingredients_fr" (array of objects with "nutrient", "value", and "unit") -"specifications_fr" (array of objects with "humidity", "ph", and "solubility") -"first_aid_fr" (array of strings) - -Requirements: +REQUIREMENTS = """ The content of keys with the suffix _en must be in English. The content of keys with the suffix _fr must be in French. Translation of the text is prohibited. @@ -56,7 +26,8 @@ class ProduceLabelForm(dspy.Signature): """ text = dspy.InputField(desc="The text of the fertilizer label extracted using OCR.") - specification = dspy.InputField(desc="The specification containing the fields to highlight and their requirements.") + json_schema = dspy.InputField(desc="The JSON schema of the object to be returned.") + requirements = dspy.InputField(desc="The instructions and guidelines to follow.") inspection = dspy.OutputField(desc="Only a complete JSON.") class GPT: @@ -87,9 +58,10 @@ def __init__(self, api_endpoint, api_key, deployment_id): response_format=response_format, ) - def create_inspection(self, prompt) -> Prediction: + def create_inspection(self, text) -> Prediction: with dspy.context(lm=self.dspy_client, experimental=True): + json_schema = FertilizerInspection.model_json_schema() signature = dspy.ChainOfThought(ProduceLabelForm) - prediction = signature(specification=SPECIFICATION, text=prompt) + prediction = signature(text=text, json_schema=json_schema, requirements=REQUIREMENTS) return prediction