def get_gpt(gpt_key, gpt_engine, gpt_temperature, gpt_max_tokens,
            example_file):
    ''' define a gpt object
    
    Args:
        gpt_key: key under "Secret" here https://beta.openai.com/developer-quickstart
        gpt_engine: language model identifier (see https://beta.openai.com/api-ref for valid values)
        gpt_temperature: sampling temperature - Higher values means the model will take more risks
        gpt_max_tokens: How many tokens to complete to, up to a maximum of 512.
    
    Returns:
        gpt: gpt object (newly created gpt object if use_saved_gpt is False; gpt object from pickle file if use_saved_gpt is True)

    '''
    try:
        # check whether to use gpt from pickle file
        # create a new gpt object
        openai.api_key = gpt_key
        gpt = GPT(engine=gpt_engine,
                  temperature=gpt_temperature,
                  max_tokens=gpt_max_tokens)
        # add examples
        # load dataframe from example file
        path = get_path()
        example_df = pd.read_csv(os.path.join(path, example_file))
        for index, row in example_df.iterrows():
            # print(row['question'],row['answer'])
            gpt.add_example(Example(row['question'], row['answer']))
    except Exception as error:
        print('ERROR', error)
    else:
        return gpt
Example #2
0
    def parse(self, test):
        gpt3 = GPT(engine="davinci", temperature=0.9, max_tokens=200)

        for instance in self.data["data"]:
            gpt3.add_example(Example(get_content(instance, self.c_type), get_answer(instance)))
        output = gpt3.get_top_reply(test).replace("output: ", "").strip()

        output = output[: output.find("}") + 1]
        output = output.replace("'", '"')
        key_list = [list(x["answer"].keys()) for x in self.data["data"]]
        return output, key_list
Example #3
0
def main():
    openai_key = os.getenv("OPENAI_KEY")
    set_openai_key(openai_key)
    gpt = GPT(temperature=0.2, max_tokens=10)

    train, validation = combustion_reactions[:4], combustion_reactions[4:]

    for example in train:
        gpt.add_example(Example(example.lhs, example.rhs))

    for idx, example in enumerate(validation):
        print(idx + 1)
        print(f"GPT prediction: {gpt.get_top_reply(example.lhs)}")
        print(f"Actual: {example.rhs}")
        print("==========================")
def main():
    with open('GPT_SECRET_KEY.json') as f:
        data = json.load(f)
    openai.api_key = data["API_KEY"]
    gpt = GPT(engine="davinci",
              temperature=0.0,
              max_tokens=100,
              output_prefix="output (positive/neutral/negative):")
    all_data = pd.read_csv(filename, header=0, encoding="utf8", sep=":->")
    all_data['Sentiment_class_label'] = all_data[
        'Sentiment_class_label'].apply(lambda x: change_labels(x))
    for row in range(all_data.shape[0]):
        gpt.add_example(
            Example(all_data['Phrase_text'][row],
                    all_data['Sentiment_class_label'][row]))
    return (gpt.get_prime_text())
Example #5
0
def segment(prompt):
    openai.api_key = os.environ.get("GPT_KEY")

    gpt = GPT(temperature=0,
              max_tokens=500,
              append_output_prefix_to_query=False,
              output_prefix="")

    gpt.add_example(
        Example(
            """George Washington (February 22, 1732[b] – December 14, 1799) was an American political leader, military general, statesman, and Founding Father who served as the first president of the United States from 1789 to 1797. Previously, he led Patriot forces to victory in the nation's War for Independence. He presided at the Constitutional Convention of 1787, which established the U.S. Constitution and a federal government. Washington has been called the "Father of His Country" for his manifold leadership in the formative days of the new nation.

Washington's first public office was serving as official Surveyor of Culpeper County, Virginia from 1749 to 1750. Subsequently, he received his initial military training and a command with the Virginia Regiment during the French and Indian War. He was later elected to the Virginia House of Burgesses and was named a delegate to the Continental Congress, where he was appointed Commanding General of the Continental Army. He commanded American forces, allied with France, in the defeat and surrender of the British during the Siege of Yorktown. He resigned his commission after the Treaty of Paris in 1783.

Washington played a key role in adopting and ratifying the Constitution and was then twice elected president by the Electoral College. He implemented a strong, well-financed national government while remaining impartial in a fierce rivalry between cabinet members Thomas Jefferson and Alexander Hamilton. During the French Revolution, he proclaimed a policy of neutrality while sanctioning the Jay Treaty. He set enduring precedents for the office of president, including the title "Mr. President", and his Farewell Address is widely regarded as a pre-eminent statement on republicanism.
""", "[\"George Washington was born on February 22, 1732.\", \" George Washington died on December 14, 1799\", \"George Washington was an American political leader and military general, statesman.\", \"George Wahington was the Founding Father who served as the first president of the United States from 1789 to 1797.\", \"George Washington led the Patriot forces to victory in the nation's War for Independence. \", \"George Washington presided at the Constitutional Convention of 1787, which established the U.S. Constitution and a federal government.\", \"George Washington has been called the 'Father of His Country' for his manifold leadership in the formative days of the new nation.\", \"George Washington was serving as official Surveyor of Culpeper County, Virginia from 1749 to 1750. \", \"George Washington was later elected to the Virginia House of Burgesses and was named a delegate to the Continental Congress.\", \"George Washington was appointed Commanding General of the Continental Army.\", \"George Washington commanded American forces, allied with France, in the defeat and surrender of the British during the Siege of Yorktown. \", \"George Washington resigned his commission after the Treaty of Paris in 1783.\", \"George Washington played a key role in adopting and ratifying the Constitution \", \"George Washington was then twice elected president by the Electoral College.\", \"George Washington during the French Revolution, proclaimed a policy of neutrality while sanctioning the Jay Treaty. \"]"
        ))

    sjExample = Example(
        """Apple was founded in April 1976 by the late Steve Jobs and Ronald Wayne. Wayne would leave the Apple company only three months after its founding to take a job with Atari, and Jobs would end up buying the company from him.""",
        "[\"Apple was founded in April 1976 by the late Steve Jobs and Ronald Wayne.\", \"Ronald Wayne would leave the Apple company only three months after its founding to take a job with Atari, and Jobs would end up buying the company from him.\"]"
    )
    gpt.add_example(sjExample)

    gpt.add_example(
        Example(
            """Adenosine triphosphate (ATP) is an organic compound and hydrotrope that provides energy to drive many processes in living cells, such as muscle contraction, nerve impulse propagation, condensate dissolution, and chemical synthesis. Found in all known forms of life, ATP is often referred to as the "molecular unit of currency" of intracellular energy transfer. [2] When consumed in metabolic processes such as cellular respiration, it converts either to adenosine diphosphate (ADP) or to adenosine monophosphate (AMP). Other processes regenerate ATP so that the human body recycles its own body weight equivalent in ATP each day.[3] It is also a precursor to DNA and RNA, and is used as a coenzyme.""",
            "[\"ATP is an organic compound and hydrotrope.\", \"ATP provides energy to drive many processes in living cells.\", \"ATP is often referred to as the 'molecular unit of currency' of intracellular energy transfer.\"]"
        ))

    out = gpt.submit_request(prompt).choices[0].text
    return out
Example #6
0
import openai
import random
from os import path

from openai.api_resources.engine import Engine

openai.api_key = ""  # find your own key by joining the waiting list
# at https://beta.openai.com/?demo=1

from gpt import GPT
from gpt import Example

gpt = GPT(engine="davinci", temperature=0.7, max_tokens=258)

gpt.add_example(
    Example(
        'This was my space and I chose to write this. Nothing else, but exactly this.'
    ))
premise3 = "Here is a premise: The announcement of Tillerson’s departure sent shock waves across the globe ."
prompt3 = premise3 + " " + \
    "The phrase   were not prepared  is entailed by which part of the premise: "
output3 = "shock waves"

prompt4 = premise3 + " " + \
    "The phrase  very famous  is entailed by which part of the premise: "
output4 = "across the globe"

premise4 = "Here is a premise: An elderly couple in heavy coats are looking at black and white photos displayed on a wall."
prompt5 = premise4 + " " + \
    "The phrase  decorated the wall  is entailed by which part of the premise: "
output5 = "displayed on a wall"

gpt.add_example(Example(prompt1, output1))
gpt.add_example(Example(prompt2, output2))
#gpt.add_example(Example(prompt3, output3))
#gpt.add_example(Example(prompt4, output4))
gpt.add_example(Example(prompt5, output5))
gpt.add_example(Example(prompt6, output6))

# Inferences
prompt = "Here is a premise: Three young boys enjoying a day at the beach"
prompt = prompt + " " + \
    "The phrase  in the beach  is entailed by which part of the premise: "
output = gpt.get_top_reply(prompt)
print(prompt, ":", output)
print("----------------------------------------")

prompt = "Here is a premise: While at Skidmore , Smith also designed an even taller mixed-use skyscraper , the Burj Dubai , now under construction in the United Arab Emirates ."
Example #8
0
def get_gpt(gpt_key, gpt_engine, gpt_temperature, gpt_max_tokens):
    ''' define a gpt object
    
    Args:
        gpt_key: key under "Secret" here https://beta.openai.com/developer-quickstart
        gpt_engine: language model identifier (see https://beta.openai.com/api-ref for valid values)
        gpt_temperature: sampling temperature - Higher values means the model will take more risks
        gpt_max_tokens: How many tokens to complete to, up to a maximum of 512.
    
    Returns:
        gpt: gpt object (newly created gpt object if use_saved_gpt is False; gpt object from pickle file if use_saved_gpt is True)

    '''
    try:
        # check whether to use gpt from pickle file
        # create a new gpt object
        openai.api_key = gpt_key
        gpt = GPT(engine=gpt_engine,
                  temperature=gpt_temperature,
                  max_tokens=gpt_max_tokens)
        # add examples - potential improvement: read these examples from a file rather than hardcoding them
        gpt.add_example(Example('initialize a git repository', 'git init'))
        gpt.add_example(
            Example('add file foo to the staging area for git', 'git add foo'))
        gpt.add_example(
            Example(
                'add all files in the current directory to the staging area for git',
                'git add .'))
        gpt.add_example(
            Example(
                'record the changes made to the files to a local repository',
                'git commit -m "commit message"'))
        gpt.add_example(
            Example('return the current state of the repository',
                    'git status'))
        gpt.add_example(
            Example(
                'Clone the remote repository https://github.com/ryanmark1867/webview_rasa_example',
                'git clone https://github.com/ryanmark1867/webview_rasa_example'
            ))
        gpt.add_example(
            Example('remove file foo from the staging area', 'git rm -f foo'))
        gpt.add_example(
            Example('show the chronological commit history for a repository',
                    'git log'))
    except Exception as error:
        print('ERROR', error)
    else:
        return gpt
import json
import openai

with open('GPT_SECRET_KEY.json') as f:
    data = json.load(f)

openai.api_key = data["API_KEY"]

from gpt import GPT
from gpt import Example

gpt = GPT(engine="davinci", temperature=0.5, max_tokens=100)

gpt.add_example(
    Example('Fetch unique values of DEPARTMENT from Worker table.',
            'Select distinct DEPARTMENT from Worker;'))
gpt.add_example(
    Example(
        'Print the first three characters of FIRST_NAME from Worker table.',
        'Select substring(FIRST_NAME,1,3) from Worker;'))
gpt.add_example(
    Example(
        "Find the position of the alphabet ('a') in the first name column 'Amitabh' from Worker table.",
        "Select INSTR(FIRST_NAME, BINARY'a') from Worker where FIRST_NAME = 'Amitabh';"
    ))
gpt.add_example(
    Example(
        "Print the FIRST_NAME from Worker table after replacing 'a' with 'A'.",
        "Select CONCAT(FIRST_NAME, ' ', LAST_NAME) AS 'COMPLETE_NAME' from Worker;"
    ))
gpt.add_example(
Example #10
0
from sklearn.metrics.pairwise import cosine_similarity
import transformers
from sentence_transformers import SentenceTransformer, LoggingHandler

engine = create_engine('postgresql://gpt3')

global gpt

app = Flask(__name__, static_url_path='')
openai.api_key = os.getenv("OPENAI_API_KEY")

gpt = GPT(engine="curie", temperature=0, max_tokens=300)
# davinci curie babbage ada

gpt.add_example(
    Example(
        'I am a 78-year-old man with angina. What is my risk of having heart failure?',
        'angina, heart failure, 78, male'))
gpt.add_example(
    Example(
        'What is my risk of having angina if I am a 53-year-old woman with a history of stroke?',
        'stroke, angina, 53, female'))
gpt.add_example(
    Example('I am at 40 with angina. What is my risk of having headache?',
            'angina, headache, 40, na'))
gpt.add_example(
    Example('I am a guy at 47 with migraine. What is my risk of stroke?',
            'migraine, stroke, 47, male'))
gpt.add_example(
    Example(
        'What is my risk of angina if I am a female with a history of heart attack?',
        'heart attack, angina, na, female'))