-
Notifications
You must be signed in to change notification settings - Fork 8
/
agreement_gate.py
85 lines (73 loc) · 2.93 KB
/
agreement_gate.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
"""Utils for running the agreement gate."""
import os
import time
from typing import Any, Dict, Tuple
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
def parse_api_response(api_response: str) -> Tuple[bool, str, str]:
"""Extract the agreement gate state and the reasoning from the GPT-3 API response.
Our prompt returns questions as a string with the format of an ordered list.
This function parses this response in a list of questions.
Args:
api_response: Agreement gate response from GPT-3.
Returns:
is_open: Whether the agreement gate is open.
reason: The reasoning for why the agreement gate is open or closed.
decision: The decision of the status of the gate in string form.
"""
api_response = api_response.strip().split("\n")
if len(api_response) < 2:
reason = "Failed to parse."
decision = None
is_open = False
else:
reason = api_response[0]
decision = api_response[1].split("Therefore:")[-1].strip()
is_open = "disagrees" in api_response[1]
return is_open, reason, decision
def run_agreement_gate(
claim: str,
query: str,
evidence: str,
model: str,
prompt: str,
context: str = None,
num_retries: int = 5,
) -> Dict[str, Any]:
"""Checks if a provided evidence contradicts the claim given a query.
Checks if the answer to a query using the claim contradicts the answer using the
evidence. If so, we open the agreement gate, which means that we allow the editor
to edit the claim. Otherwise the agreement gate is closed.
Args:
claim: Text to check the validity of.
query: Query to guide the validity check.
evidence: Evidence to judge the validity of the claim against.
model: Name of the OpenAI GPT-3 model to use.
prompt: The prompt template to query GPT-3 with.
num_retries: Number of times to retry OpenAI call in the event of an API failure.
Returns:
gate: A dictionary with the status of the gate and reasoning for decision.
"""
if context:
gpt3_input = prompt.format(
context=context, claim=claim, query=query, evidence=evidence
).strip()
else:
gpt3_input = prompt.format(claim=claim, query=query, evidence=evidence).strip()
for _ in range(num_retries):
try:
response = openai.Completion.create(
model=model,
prompt=gpt3_input,
temperature=0.0,
max_tokens=256,
stop=["\n\n"],
logit_bias={"50256": -100}, # Don't allow <|endoftext|> to be generated
)
break
except openai.error.OpenAIError as exception:
print(f"{exception}. Retrying...")
time.sleep(2)
is_open, reason, decision = parse_api_response(response.choices[0].text)
gate = {"is_open": is_open, "reason": reason, "decision": decision}
return gate