import dspy import dsp import gradio as gr class problem_formulation(dspy.Signature): """Declare the order-of-logic type (if any). Convert the problem-statement into a predicate formula and ontology graph of entities and relationships.""" problem_question = dspy.InputField(desc="the problem-statement") output = dspy.OutputField( desc="the predicate formula and ontology graph of entities and relationships" ) class mathematical_prover(dspy.Signature): """Use known theorems and axioms to prove or disprove the predicate formula.""" formula = dspy.InputField(desc="the predicate formula") output = dspy.OutputField( desc="Use theorems and axioms to prove or disprove the predicate formula" ) class Adjudicate(dspy.Signature): """Articulate the solution(s) based on the final formula.""" final_formula = dspy.InputField(desc="the final predicate formula") proof = dspy.InputField(desc="the proof") original_question = dspy.InputField(desc="the problem-statement") output = dspy.OutputField(desc="Be concise. Write detail solution(s).") class Formulation(dspy.Module): def __init__(self): super().__init__() self.problem_form = dspy.ChainOfThought(problem_formulation) self.math_prover = dspy.ChainOfThought(mathematical_prover) self.arbitrator = dspy.ChainOfThought(Adjudicate) def forward(self, input_text): formula = self.problem_form(problem_question=input_text).output proof = self.math_prover(formula=formula).output final_formula = formula solution = self.arbitrator( final_formula=final_formula, proof=proof, original_question=input_text, ).output return solution def run_formulation(api_key, model, input_text, temperature): groq = dsp.GroqLM( api_key=api_key, model=model, max_tokens=3000, temperature=temperature ) dspy.settings.configure(lm=groq) result = Formulation()(input_text) return result def main(): with gr.Blocks() as demo: gr.Markdown("# Problem Formulation and Solution") gr.Markdown( "Note: The llama3-8b-8192 and llama3-70b-8192 models have shown the best success in generating solutions." ) with gr.Row(): with gr.Column(): api_key_input = gr.Textbox(label="Groq API Key", type="password") model_input = gr.Dropdown( label="Model", choices=["llama3-8b-8192", "llama3-70b-8192", "mixtral-8x7b-32768", "llama-3.2-1b-preview", "llama-3.2-3b-preview"], value="llama3-70b-8192", ) temperature_slider = gr.Slider( label="Temperature", minimum=0.0, maximum=1.0, value=0.3, step=0.1, ) input_text = gr.Textbox(label="Problem Statement") clear_button = gr.Button("Clear") with gr.Column(): result_output = gr.Textbox(label="Solution") submit_button = gr.Button("Submit") submit_button.click( run_formulation, inputs=[api_key_input, model_input, input_text, temperature_slider], outputs=[result_output], ) clear_button.click(lambda: "", inputs=[], outputs=[input_text]) demo.launch(share=True) if __name__ == "__main__": main()