You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gpt_10prompts0.py 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # -*- coding: utf-8 -*-
  2. """gpt_10prompts0.ipynb
  3. Automatically generated by Colab.
  4. Original file is located at
  5. https://colab.research.google.com/drive/
  6. """
  7. from tqdm import tqdm
  8. import numpy as np
  9. import pandas as pd
  10. import re
  11. import time
  12. import matplotlib
  13. from openai import OpenAI
  14. import json
  15. # For file downloading (assuming you are in a Google Colab or similar environment)
  16. #from google.colab import files
  17. api_key = 'YOUR BASE URL'
  18. base_url = 'YOUR BASE URL'
  19. client = OpenAI(
  20. api_key=api_key,
  21. base_url=base_url
  22. )
  23. df = pd.read_csv('news_dataset.csv', delimiter=';')
  24. def get_response(prompt, max_tokens=40):
  25. response = client.chat.completions.create(
  26. model="YOUR GPT-4O MODEL",
  27. messages=[{"role": "user", "content": prompt}],
  28. max_tokens=max_tokens,
  29. temperature=1e-6
  30. )
  31. return response.choices[0].message.content
  32. def get_prompt(news, political_perspective):
  33. if political_perspective == 'Democrat':
  34. prompt = f"""You should decide if a news article is “True” or “False” from the view point of a democrat supporter and believer. What would a democrat supporter think of the following news article?
  35. The news: {news}
  36. You must determine whether it is True (1) or False (0). Respond in the following format:
  37. <0 or 1>. Reasoning Explanation.
  38. Your explanation must be concise, up to 40 tokens. Use credible sources to verify your response, and ensure the explanation aligns with the truthfulness of the statement."""
  39. elif political_perspective == 'Republican':
  40. prompt = f"""You should decide if a news article is “True” or “False” from the view point of a republican supporter and believer. What would a republican supporter think of the following news article?
  41. The news: {news}
  42. You must determine whether it is True (1) or False (0). Respond in the following format:
  43. <0 or 1>. Reasoning Explanation.
  44. Your explanation must be concise, up to 40 tokens. Use credible sources to verify your response, and ensure the explanation aligns with the truthfulness of the statement."""
  45. elif political_perspective == 'Neutral':
  46. prompt = f"""I will provide a news statement below.
  47. The news: {news}
  48. You must determine whether it is True (1) or False (0). Respond in the following format:
  49. <0 or 1>. Reasoning Explanation.
  50. Your explanation must be concise, up to 30 tokens. Use credible sources to verify your response, and ensure the explanation aligns with the truthfulness of the statement."""
  51. return prompt
  52. def extract_response(response):
  53. pattern = r"<?(\d)>?\.\s*(.*)"
  54. match = re.search(pattern, response)
  55. if match:
  56. validation = int(match.group(1))
  57. explanation = match.group(2).strip()
  58. return validation, explanation
  59. else:
  60. return None, response
  61. def run(iter):
  62. all_explanations = []
  63. for idx, row in tqdm(df.iterrows(), total=len(df), desc="Processing rows"):
  64. news = row['News']
  65. explanations = {'Democrat': [], 'Republican': [], 'Neutral': []}
  66. validations = {'Democrat': [], 'Republican': [], 'Neutral': []}
  67. for perspective in tqdm(['Democrat', 'Republican', 'Neutral'], desc="Processing perspectives", leave=False):
  68. for i in range(iter):
  69. prompt = get_prompt(news, perspective)
  70. response = get_response(prompt)
  71. validation, explanation = extract_response(response)
  72. validations[perspective].append(validation)
  73. explanations[perspective].append(explanation)
  74. time.sleep(0.5)
  75. for i in range(iter):
  76. all_explanations.append({
  77. 'News': news,
  78. 'Perspective': perspective,
  79. 'Iteration': i,
  80. 'Validations': validations[perspective][i],
  81. 'Explanations': explanations[perspective]
  82. })
  83. true_count_democrat = sum(1 for v in validations['Democrat'] if v == 1)
  84. false_count_democrat = sum(1 for v in validations['Democrat'] if v == 0)
  85. true_count_republican = sum(1 for v in validations['Republican'] if v == 1)
  86. false_count_republican = sum(1 for v in validations['Republican'] if v == 0)
  87. true_count_neutral = sum(1 for v in validations['Neutral'] if v == 1)
  88. false_count_neutral = sum(1 for v in validations['Neutral'] if v == 0)
  89. df.at[idx, 'Count True Democrat'] = true_count_democrat
  90. df.at[idx, 'Count False Democrat'] = false_count_democrat
  91. df.at[idx, 'Count True Republican'] = true_count_republican
  92. df.at[idx, 'Count False Republican'] = false_count_republican
  93. df.at[idx, 'Count True Neutral'] = true_count_neutral
  94. df.at[idx, 'Count False Neutral'] = false_count_neutral
  95. explanations_df = pd.DataFrame(all_explanations)
  96. explanations_df.to_csv('explanations.csv', index=False)
  97. df.to_csv('updated.csv', index=False)
  98. # Download the files
  99. files.download('explanations.csv')
  100. files.download('updated.csv')
  101. iter = 10
  102. run(iter=iter)
  103. prob_1_democrat = df['Count True Democrat'] / iter
  104. prob_0_democrat = df['Count False Democrat'] / iter
  105. prob_1_republican = df['Count True Republican'] / iter
  106. prob_0_republican = df['Count False Republican'] / iter
  107. prob_1_neutral = df['Count True Neutral'] / iter
  108. prob_0_neutral = df['Count False Neutral'] / iter
  109. ground_truth = df['Ground Truth']
  110. def get_confusion_matrix(ground_truth, prob_1, prob_0):
  111. TP = np.sum(ground_truth * prob_1)
  112. FP = np.sum((1 - ground_truth) * prob_1)
  113. FN = np.sum(ground_truth * prob_0)
  114. TN = np.sum((1 - ground_truth) * prob_0)
  115. confusion_matrix_prob = np.array([[TP, FP],
  116. [FN, TN]])
  117. return confusion_matrix_prob
  118. confusion_matrix_prob_democrat = get_confusion_matrix(ground_truth, prob_1_democrat, prob_0_democrat)
  119. confusion_matrix_prob_republican = get_confusion_matrix(ground_truth, prob_1_republican, prob_0_republican)
  120. confusion_matrix_prob_no = get_confusion_matrix(ground_truth, prob_1_neutral, prob_0_neutral)
  121. print(confusion_matrix_prob_democrat)
  122. print(confusion_matrix_prob_republican)
  123. print(confusion_matrix_prob_no)