-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
88 lines (73 loc) · 2.55 KB
/
run.py
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
import os
from pathlib import Path
import dotenv
import fire
import pandas as pd
import requests
def run_model(model, prompt, article, conflict, perspective_a, perspective_b):
url = f"{os.getenv('VG_AI_API_URL')}/v1/services/text-to-schema"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.getenv('VG_AI_API_KEY')}",
}
body = {
"model": model,
"systemPrompt": prompt.format(
conflict=conflict, perspective_a=perspective_a, perspective_b=perspective_b
),
"text": article["TEXT"],
"schema": {
"type": "object",
"properties": {
"perspective": {
"type": "string",
"description": f"The perspective that is promoted. One of 'neutral', '{perspective_a}', '{perspective_b}'.",
"enum": ["neutral", perspective_a, perspective_b],
},
"explanation": {
"type": "array",
"description": "Explanation of your assessment",
"items": {"type": "string"},
},
},
},
}
result = requests.post(url, headers=headers, json=body)
result.raise_for_status()
return result.json()
def main():
models = [
"google-vertex-gemini-1.5-pro-preview-0409",
"gpt-4o",
"gpt-4-turbo",
"bedrock-anthropic.claude-3-opus-20240229-v1:0",
]
prompt = Path("prompt.txt").read_text()
articles = pd.read_json("./data/articles.jsonl", lines=True)
result = []
for article in articles.to_dict(orient="records"):
for model in models:
print(
f"Running {model} with {article['CONTENT_ID']}: {article['CONTENT_TITLE']}..."
)
response = run_model(
model,
prompt,
article,
conflict="Israel/Gaza",
perspective_a="israeli",
perspective_b="palestinian",
)
result.append(
{
"id": article["CONTENT_ID"],
"title": article["CONTENT_TITLE"],
"rater": model,
"perspective": response["result"]["perspective"],
"explanation": "\n".join(response["result"]["explanation"]),
}
)
pd.DataFrame(result).to_csv("data/classification.csv", index=False)
if __name__ == "__main__":
dotenv.load_dotenv()
fire.Fire(main())