-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforecasting.py
More file actions
154 lines (129 loc) · 4.98 KB
/
Copy pathforecasting.py
File metadata and controls
154 lines (129 loc) · 4.98 KB
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
import asyncio
from dataclasses import dataclass
from typing import List, Dict, Any
import httpx
OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions"
@dataclass
class ForecastResult:
question_type: str
forecasts: List[str]
judge_feedback: List[str]
supreme_decision: str
class LLMClient:
def __init__(self, api_key: str, model: str):
self.api_key = api_key.strip()
self.model = model.strip()
self.headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
}
async def complete(self, messages: List[Dict[str, Any]]) -> str:
payload = {"model": self.model, "messages": messages}
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.post(OPENROUTER_URL, headers=self.headers, json=payload)
response.raise_for_status()
data = response.json()
return data["choices"][0]["message"]["content"].strip()
def classification_prompt(title: str, context: str) -> List[Dict[str, str]]:
return [
{
"role": "system",
"content": (
"Classify the forecasting question as exactly one of NUMERIC, BINARY, or MCQ. "
"Use only the provided title and context. Reply with the label only."
),
},
{
"role": "user",
"content": f"Title: {title}\nContext: {context}",
},
]
def forecaster_prompt(question_type: str, title: str, context: str) -> List[Dict[str, str]]:
task_description = {
"NUMERIC": "Provide a single numeric forecast (number only).",
"BINARY": "Provide the probability that the answer is YES as a percentage between 0 and 100.",
"MCQ": "Provide probabilities for each option, ensuring they sum to 100%.",
}[question_type]
return [
{
"role": "system",
"content": (
"You are an independent forecaster. "
f"{task_description} Do not explain your answer."
),
},
{
"role": "user",
"content": f"Question type: {question_type}\nTitle: {title}\nContext: {context}",
},
]
def judge_prompt(question_type: str, title: str, context: str, forecasts: List[str]) -> List[Dict[str, str]]:
forecast_list = "\n".join(f"Forecaster {i+1}: {forecast}" for i, forecast in enumerate(forecasts))
return [
{
"role": "system",
"content": (
"You are a judge evaluating multiple forecasts. "
"Assess reasoning quality, identify inconsistencies or outliers, and suggest adjustments. "
"Do not provide a final forecast."
),
},
{
"role": "user",
"content": (
f"Question type: {question_type}\nTitle: {title}\nContext: {context}\n"
f"Forecasts:\n{forecast_list}"
),
},
]
def supreme_prompt(
question_type: str,
title: str,
context: str,
forecasts: List[str],
judge_feedback: List[str],
) -> List[Dict[str, str]]:
forecast_list = "\n".join(f"Forecaster {i+1}: {forecast}" for i, forecast in enumerate(forecasts))
feedback_text = "\n".join(f"Judge {i+1}: {feedback}" for i, feedback in enumerate(judge_feedback))
return [
{
"role": "system",
"content": (
"You are the Supreme Judge. Harmonize the forecasts using judge feedback. "
"Respond in two short paragraphs: first explain reasoning, second present the final forecast. "
"Use plain text only."
),
},
{
"role": "user",
"content": (
f"Question type: {question_type}\nTitle: {title}\nContext: {context}\n"
f"Forecasts:\n{forecast_list}\n"
f"Judge feedback:\n{feedback_text}"
),
},
]
async def run_pipeline(api_key: str, model: str, title: str, context: str) -> ForecastResult:
client = LLMClient(api_key, model)
question_type = (await client.complete(classification_prompt(title, context))).upper()
if question_type not in {"NUMERIC", "BINARY", "MCQ"}:
question_type = "NUMERIC"
forecasts_tasks = [
asyncio.create_task(client.complete(forecaster_prompt(question_type, title, context)))
for _ in range(4)
]
forecasts = [await task for task in forecasts_tasks]
judge_tasks = [
asyncio.create_task(client.complete(judge_prompt(question_type, title, context, forecasts)))
for _ in range(2)
]
judge_feedback = [await task for task in judge_tasks]
supreme_decision = await client.complete(
supreme_prompt(question_type, title, context, forecasts, judge_feedback)
)
return ForecastResult(
question_type=question_type,
forecasts=forecasts,
judge_feedback=judge_feedback,
supreme_decision=supreme_decision,
)