File size: 3,531 Bytes
ccfab15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e39e5d3
ccfab15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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()