import os
import litellm

llm_model = "mistral/mistral-small-latest"

os.environ["LANGFUSE_OTEL_HOST"] = "https://cloud.langfuse.com"
litellm.callbacks = ["langfuse_otel"]


def log(m):
    print("[LOG] " + m)


def filter(t: str):
    t = t.lower()
    if t.startswith("true"):
        return True
    elif t.startswith("false"):
        return False
    else:
        return None


def ask_llm_for_correctness(spec: str, req: str) -> bool:
    try:
        chat_response = litellm.completion(
            model=llm_model,
            messages=[
                {
                    "role": "system",
                    "content": "Given a specification of a system, and a requirement,"
                    " evaluate if that requirements is correct with respect to the given specification."
                    + " Just answer True or False if you are sure. Just answer Unclear if you miss some information to decide. Don't explain.",
                },
                {
                    "role": "user",
                    "content": "Specification: "
                    + spec
                    + "(end of the specification) Requirement: "
                    + req
                    + "(end of requirement).",
                },
            ],
            max_tokens=5,
            temperature=1.2,
        )
        res = chat_response.choices[0].message.content
        log(
            "I had an interaction with "
            + llm_model
            + " to evaluate the correctness of a requirement."
        )
        log("I gave the following spec: " + spec)
        log("I also gave the following requirement: " + req)
        log("The LLM gave me the following answer: " + res)
        b = filter(res)
        return b
    except Exception as e:
        print(str(e))
        raise Exception("LLM failure: " + str(type(e))) from e
