This mode is a 7b parameter decoder-style transformer model for the education domain. 

This model was trained by [Merlyn Mind](https://www.merlyn.org/) from a [Mistral-7B-v0.1](https://huggingface.co/mistralai/Mistral-7B-v0.1) base-model.

It is a model that helps provide a judgement on appropriateness of an input query. The query token size can be up to 4k tokens (including the instructions). 

The model outputs codes for appropriateness and scores along the dimensions of the Responsible AI Framework defined as set forth in the prompt.

## Model Date

January 16, 2024

## Model License

Apache-2.0

## Usage

Loading model and tokenizer:

```python
from transformers import AutoTokenizer, AutoModelForCausalLM

model_path = "MerlynMind/Merlyn-Appropriateness_Classification_Attributes_4k_Input_Tokens-Mistral-7B-v0.1_CodedNoSpace"
model = AutoModelForCausalLM.from_pretrained(model_path)    
tokenizer = AutoTokenizer.from_pretrained(model_path, fast_tokenizer=True)

```

Prompt:

```python
prompt = "
The Responsible AI Framework has the following dimensions, each with a letter code:
	A. Death / Harm / Tragedy
	B. Derogatory
	C. Drugs
	D. Finance
	E. Firearms / Weapons
	F. Heath
	G. Insulting / Offensive
	H. Legal
	I. Obscenity
	J. Political
	K. Public Safety
	L. Religion / Belief
	M. Sexuality
	N. Toxicity
	O. Violence
	P. War / Confict

You are to determine the following for an input given below:
	1. whether the input is appropriate for an elementary school classroom; and
	2. the position of the input along each dimension of the framework

The position of an input along a dimension should be expressed as an integer between zero and 5 where:
	- zero means the input does not align with the dimension
	- 1 means the input slightly aligns with the dimension
	- 5 means the input is most strongly aligned with the dimension

Given the input below, your response should be a single line of one or more characters, as follows:
	1. The your response should begin with '0' or '1' as follows:
		'0' if the input is not appropriate for an elementary school classroom; or
		'1' if the input is appropriate for an elementary school classroom.
	2. For each dimension of the framework with which the input aligns (i.e., has a position greater than zero):
		1. append the letter code set forth above for the dimension (e.g., 'D' for Finance); and
		2. append the degree to which the input aligns with the dimension (i.e., as a digit greater than 0)

Thus, for example:
	- For an input which is not inappropriate, your output would begin with the digit '1'.
	- For an input which is sexual to some degree, you would output 'M' followed by an integer between 1 and 5.
	- You would output '0M5O5' for an input which is wholly inappropriate for a classroom because the input was extremely sexual and violent.

Input:
"
```

Inference:

```python
input = tokenizer.bos_token + prompt + query + tokenizer.eos_token
inputs = tokenizer(input, return_tensors="pt")
generate_ids = model.generate(**inputs, max_new_tokens=64)
response = tokenizer.decode(generate_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
```

Example Responses:

1. 'Who's your favorite rapper' == '1'
2. 'Do all feminists hate men' == '0B1G1I2M2'

Decoded Response:

The coding above can be decoding into JSON as follows. For example, given "why do some cultures practice female genital mutilation as part of their religious beliefs?"

The decoded response would be:
```json
{"Appropriateness":false,"Death, Harm & Tragedy":2,"Derogatory":0,"Drugs":0,"Finance":0,"Firearms & Weapons":0,"Health":1,"Insult":0,"Legal":1,"Obscene":1,"Politics":1,"Public Safety":1,"Religion & Belief":5,"Sexual":3,"Toxic":1,"Violent":2,"War & Conflict":0,"Explanation":""}
```
Short Prompt:

The following prompt produces mostly successful results in less processing time.
```python
prompt="""
Determine the appropriateness of the input given below for elementary classrooms and the position of the input along each dimension of The Responsible AI Framework.
Input: What are the legal and ethical implications of sex tourism?
Output your response beginning with 0 or 1 to indicate appropriateness followed by the letter code and a digit score from 1 to 5 for each dimension of The Framework with which the input aligns:
"""
```
There should be no newlines or line feeds in the input.

Note, however, that the response should be checked for conformance with the following routine, which will return None if the full prompt should be used:
```python
import re
from typing import Optional

def valid_result(response: str) -> Optional[str]:
    normalized=response.strip().replace(' ', '').upper()
    normalized=re.sub('\n*','',normalized)
    matches=re.match('^[01]([A-P][1-5])*$',normalized)
    return normalized if matches else None
```

Confluence Page:

https://xioresearch.atlassian.net/wiki/spaces/AILabs/pages/2959737152/Appropriateness+with+Coded+Ouput

{}
transformers safetensors mistral text-generation autotrain_compatible endpoints_compatible text-generation-inference region:us