> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/lovishchopra/NL2FOL/llms.txt
> Use this file to discover all available pages before exploring further.

# Basic Usage

> Learn how to translate natural language to first-order logic with a simple example

## Overview

This guide walks you through a basic example of using NL2FOL to translate a natural language statement into first-order logic and detect logical fallacies.

## Prerequisites

Before you begin, ensure you have:

* Installed NL2FOL and its dependencies
* Set up your OpenAI API key (for GPT models) or Llama model access
* Installed the required NLI model

## Quick Example

<Steps>
  <Step title="Import the NL2FOL class">
    Start by importing the necessary modules:

    ```python theme={null}
    from transformers import AutoModelForSequenceClassification, AutoTokenizer
    from nl_to_fol import NL2FOL
    from openai import OpenAI

    client = OpenAI()
    ```
  </Step>

  <Step title="Initialize the models">
    Set up your language model and NLI model:

    ```python theme={null}
    # For GPT-4
    model_type = 'gpt'
    pipeline = None
    tokenizer = None

    # Initialize NLI model for entity relation detection
    nli_model_name = "microsoft/deberta-large-mnli"
    nli_tokenizer = AutoTokenizer.from_pretrained(nli_model_name)
    nli_model = AutoModelForSequenceClassification.from_pretrained(nli_model_name)
    ```
  </Step>

  <Step title="Create an NL2FOL instance">
    Initialize the translator with your sentence:

    ```python theme={null}
    sentence = "All politicians are corrupt. John is a politician. Therefore, John is corrupt."

    nl2fol = NL2FOL(
        sentence=sentence,
        model_type=model_type,
        pipeline=pipeline,
        tokenizer=tokenizer,
        nli_model=nli_model,
        nli_tokenizer=nli_tokenizer,
        debug=True
    )
    ```
  </Step>

  <Step title="Convert to first-order logic">
    Run the conversion pipeline:

    ```python theme={null}
    final_lf, final_lf2 = nl2fol.convert_to_first_order_logic()

    print("Logical Form 1:", final_lf)
    print("Logical Form 2:", final_lf2)
    ```

    The output will show:

    * Claim and implication extraction
    * Referring expressions
    * Entity mappings
    * Property relations
    * Final first-order logic formulas
  </Step>
</Steps>

## Understanding the Output

When you run the example above with `debug=True`, you'll see detailed output including:

<CodeGroup>
  ```text Claim & Implication theme={null}
  Claim: All politicians are corrupt
  Implication: John is corrupt
  ```

  ```text Referring Expressions theme={null}
  Referring Expressions, Claim: politicians
  Referring Expressions, Implication: John
  ```

  ```text Entity Mappings theme={null}
  Mappings: {'politicians': 'a', 'John': 'b'}
  ```

  ```text Properties theme={null}
  Claim Properties: Politician(a), Corrupt(a)
  Implication Properties: Corrupt(b)
  ```

  ```text Final Logic theme={null}
  Final Lf= (forall a (Politician(a) and Corrupt(a))) -> (exists b (Corrupt(b)))
  ```
</CodeGroup>

## Complete Working Example

Here's a complete script you can run:

```python nl2fol_basic.py theme={null}
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from nl_to_fol import NL2FOL
from openai import OpenAI

client = OpenAI()

def basic_example():
    # Setup models
    model_type = 'gpt'
    pipeline = None
    tokenizer = None
    
    nli_model_name = "microsoft/deberta-large-mnli"
    nli_tokenizer = AutoTokenizer.from_pretrained(nli_model_name)
    nli_model = AutoModelForSequenceClassification.from_pretrained(nli_model_name)
    
    # Example sentence with a logical structure
    sentence = "All birds can fly. Penguins are birds. Therefore, penguins can fly."
    
    # Create NL2FOL instance
    nl2fol = NL2FOL(
        sentence=sentence,
        model_type=model_type,
        pipeline=pipeline,
        tokenizer=tokenizer,
        nli_model=nli_model,
        nli_tokenizer=nli_tokenizer,
        debug=True
    )
    
    # Convert to first-order logic
    final_lf, final_lf2 = nl2fol.convert_to_first_order_logic()
    
    print("\n" + "="*50)
    print("FINAL RESULTS")
    print("="*50)
    print(f"Logical Form 1: {final_lf}")
    print(f"Logical Form 2: {final_lf2}")
    
    return final_lf, final_lf2

if __name__ == "__main__":
    basic_example()
```

## Key Parameters

<ParamField path="sentence" type="string" required>
  The natural language statement to translate. This should contain a claim and implication structure.
</ParamField>

<ParamField path="model_type" type="string" required>
  The LLM backend to use. Options: `'gpt'` or `'llama'`
</ParamField>

<ParamField path="debug" type="boolean" default="false">
  Enable detailed logging of the translation pipeline steps
</ParamField>

<ParamField path="pipeline" type="Pipeline">
  HuggingFace text generation pipeline (required for Llama models, `None` for GPT)
</ParamField>

<ParamField path="tokenizer" type="Tokenizer">
  HuggingFace tokenizer (required for Llama models, `None` for GPT)
</ParamField>

<ParamField path="nli_model" type="Model" required>
  Natural Language Inference model for entity relation detection
</ParamField>

<ParamField path="nli_tokenizer" type="Tokenizer" required>
  Tokenizer for the NLI model
</ParamField>

## Next Steps

<CardGroup cols={2}>
  <Card title="Model Backends" icon="microchip" href="/examples/model-backends">
    Learn how to switch between GPT-4 and Llama models
  </Card>

  <Card title="Custom Datasets" icon="database" href="/examples/custom-datasets">
    Process your own datasets for fallacy detection
  </Card>

  <Card title="SMT Solving" icon="gears" href="/usage/fol-to-smt">
    Use CVC5 to verify logical formulas
  </Card>

  <Card title="API Reference" icon="code" href="/api/nl2fol">
    Explore the complete NL2FOL API
  </Card>
</CardGroup>

<Note>
  The translation pipeline uses multiple LLM calls to extract claims, referring expressions, properties, and logical forms. With GPT-4, expect the process to take 10-30 seconds per sentence depending on complexity.
</Note>
