File size: 5,049 Bytes
f1bf1b2 |
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"\n",
"with open('../data/task4.json') as f:\n",
" data = json.load(f)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# If use Together API\n",
"from together import Together\n",
"\n",
"your_model_name = 'xxxxxxxxxxxxxxxxxxxxxxxx'\n",
"your_api_key = 'xxxxxxxxxxxxxxxxxxxxxxxx'\n",
"client = Together(api_key=your_api_key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for draft in data:\n",
" statements = draft['Statements']\n",
" for statement in statements:\n",
" country = statement['country']\n",
" system_prompt = f\"\"\"\n",
" Assume you are the representative of {country} in UNSC, given the content of a UNSC draft resolution, generate a statement that you would make in the meeting.\n",
" \"\"\"\n",
" user_prompt = f\"\"\"\n",
" Draft resolution: {draft['Content']}\n",
" Your statement:\n",
" \"\"\"\n",
" response = client.chat.completions.create(\n",
" model=your_model_name,\n",
" temperature=0.0,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt}\n",
" ]\n",
" )\n",
" statement['generation'] = response.choices[0].message.content\n",
" draft['Statements'] = statements"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"statements = []\n",
"generations = []\n",
"\n",
"for draft in data:\n",
" for statement in draft['Statements']:\n",
" statements.append(statement['statement'])\n",
" generations.append(statement['generation'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# average ROUGE\n",
"from rouge_score import rouge_scorer\n",
"scorer = rouge_scorer.RougeScorer(['rougeL'], use_stemmer=True)\n",
"\n",
"rouge_scores = []\n",
"for i in range(len(statements)):\n",
" scores = scorer.score(statements[i], generations[i])\n",
" rouge_scores.append(scores['rougeL'].fmeasure)\n",
"\n",
"print('Average ROUGE-L:', sum(rouge_scores) / len(rouge_scores))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# average Jaccard similarity\n",
"from sklearn.feature_extraction.text import CountVectorizer\n",
"from sklearn.metrics import jaccard_score\n",
"\n",
"jaccard_scores = []\n",
"vectorizer = CountVectorizer(binary=True)\n",
"\n",
"for i in range(len(statements)):\n",
" X = vectorizer.fit_transform([statements[i], generations[i]]).toarray()\n",
" jaccard_scores.append(jaccard_score(X[0], X[1]))\n",
"\n",
"print('Average Jaccard similarity:', sum(jaccard_scores) / len(jaccard_scores))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# average Cosing Similarity (TF-IDF)\n",
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"from sklearn.metrics.pairwise import cosine_similarity\n",
"\n",
"tf_cosine_scores = []\n",
"for i in range(len(statements)):\n",
" tfidf = TfidfVectorizer().fit_transform([statements[i], generations[i]])\n",
" tf_cosine_scores.append(cosine_similarity(tfidf[0], tfidf[1])[0][0])\n",
"\n",
"print('Average Cosine Similarity (TF-IDF):', sum(tf_cosine_scores) / len(tf_cosine_scores))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sentence_transformers import SentenceTransformer, util\n",
"\n",
"device = 'cuda:1'\n",
"model = SentenceTransformer('stsb-roberta-large', device=device)\n",
"cosine_scores = []\n",
"for i in range(len(statements)):\n",
" embeddings = model.encode([statements[i], generations[i]])\n",
" cosine_scores.append(util.pytorch_cos_sim(embeddings[0], embeddings[1]).item())\n",
"\n",
"print('Average Cosine Similarity (BERT):', sum(cosine_scores) / len(cosine_scores))\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "llm",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.19"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|