-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FewShot Jais asset for ANSStance (#263)
* feature: ANSStance/Jais13b * Fix minor metadata issue --------- Co-authored-by: Fahim Imaduddin Dalvi <[email protected]>
- Loading branch information
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
assets/ar/sentiment_emotion_others/stance_detection/ANSStance_Jais13b_FewShot.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from llmebench.datasets import ANSStanceDataset | ||
from llmebench.models import FastChatModel | ||
from llmebench.tasks import StanceTask | ||
|
||
|
||
def metadata(): | ||
return { | ||
"author": "Arabic Language Technologies, QCRI, HBKU", | ||
"model": "Jais-13b", | ||
"description": "Locally hosted Jais-13b-chat model using FastChat. 5-shot", | ||
"scores": {"Macro-F1": "0.5272154156628485"}, | ||
} | ||
|
||
|
||
def config(): | ||
return { | ||
"dataset": ANSStanceDataset, | ||
"task": StanceTask, | ||
"model": FastChatModel, | ||
"model_args": { | ||
"class_labels": ["agree", "disagree"], | ||
"max_tries": 30, | ||
}, | ||
} | ||
|
||
|
||
def prompt(input_sample, examples): | ||
ref_s = input_sample["sentence_1"] | ||
claim = input_sample["sentence_2"] | ||
base_prompt = ( | ||
f"Given a reference sentence and a claim, predict whether the claim agrees or disagrees with the reference sentence. Reply only using 'agree', 'disagree', or use 'other' if the sentence and claim are unrelated." | ||
f"\n\n" | ||
f"reference sentence: {ref_s}" | ||
f"\nclaim: {claim}" | ||
f"\nlabel: \n" | ||
) | ||
|
||
return [ | ||
{ | ||
"role": "user", | ||
"content": few_shot_prompt(input_sample, base_prompt, examples), | ||
}, | ||
] | ||
|
||
|
||
def few_shot_prompt(input_sample, base_prompt, examples): | ||
out_prompt = base_prompt | ||
for example in examples: | ||
ref_s = example["input"]["sentence_1"] | ||
claim = example["input"]["sentence_2"] | ||
|
||
out_prompt = ( | ||
out_prompt | ||
+ "reference sentence: " | ||
+ ref_s | ||
+ "\nclaim: " | ||
+ claim | ||
+ "\nlabel: " | ||
+ example["label"] | ||
+ "\n\n" | ||
) | ||
|
||
# Append the sentence we want the model to predict for but leave the label blank | ||
|
||
ref_s = input_sample["sentence_1"] | ||
claim = input_sample["sentence_2"] | ||
|
||
out_prompt = ( | ||
out_prompt | ||
+ "reference sentence: " | ||
+ ref_s | ||
+ "\nclaim: " | ||
+ claim | ||
+ "\nlabel: \n" | ||
) | ||
|
||
return out_prompt | ||
|
||
|
||
def post_process(response): | ||
out = response["choices"][0]["message"]["content"] | ||
out = out.strip().lower() | ||
|
||
if "i apologize" in out: | ||
return None | ||
|
||
j = out.find("label:") | ||
if j > 0: | ||
out = out[j + len("label:") :] | ||
else: | ||
j = out.find(" is:\n\n") | ||
if j > 0: | ||
out = out[j + len(" is:\n\n") :] | ||
out = out.strip().title() | ||
return out.lower() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters