File size: 1,319 Bytes
8360ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Utils for generating fake evidence given a query."""
import os
import time
from typing import Dict

import openai

openai.api_key = os.getenv("OPENAI_API_KEY")


def run_evidence_hallucination(
    query: str,
    model: str,
    prompt: str,
    num_retries: int = 5,
) -> Dict[str, str]:
    """Generates a fake piece of evidence via LLM given the question.

    Args:
        query: Query to guide the validity check.
        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:
        output: A potentially inaccurate piece of evidence.
    """
    gpt3_input = prompt.format(query=query).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\n"],
            )
            break
        except openai.error.OpenAIError as exception:
            print(f"{exception}. Retrying...")
            time.sleep(2)

    hallucinated_evidence = response.choices[0].text.strip()
    output = {"text": hallucinated_evidence, "query": query}
    return output