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
def train_python_model(): gpt_python.add_example( Example('How many unique values in Division Column?', 'df["Division"].nunique()')) gpt_python.add_example( Example( 'Find the Division of boy who scored 55 marks', 'df,loc[(df,loc[:, "Gender"] == "boy") & (df,loc[:, "Marks"] == 55)]' )) gpt_python.add_example( Example('Find the average Marks scored by Girls', 'np.mean(df.loc[(df.loc[:, "Gender"] == "girl"), "Marks"])'))
def train_php_model(): gpt_php.add_example( Example( 'Start a bucket for a cluster', ''' 'couchbase' => [ 'driver' => 'couchbase', 'host' => 'couchbase://' . env('COUCHBASE_SERVER', '127.0.0.1'), 'user' => env('COUCHBASE_USER', 'Administrator'), 'password' => env('COUCHBASE_PASS', 'password'), ], ''')) gpt_php.add_example( Example( 'User management', ''' public function create(Request $request) { $credentials = [ 'name' => $request->user, 'password' => $request->password, ]; $user = new User($credentials); try { $this->db->insert("user::".$request->user, $user); return response()->json(["data" => ["token" => $this->buildToken($user)]]); } catch (\Couchbase\Exception $ex) { return response()->json(["failure" => 'Failed to create user'], 409); } } ''')) gpt_php.add_example( Example( 'Generic query', ''' $queryBody = SearchQuery::conjuncts(SearchQuery::term("hotel")->field("type")); if (!empty($location) && $location != "*") { $queryBody->every(SearchQuery::disjuncts( SearchQuery::match($location)->field("country"), SearchQuery::match($location)->field("city"), SearchQuery::match($location)->field("state"), SearchQuery::match($location)->field("address") )); } ''')) #prompt = "Display Division of girl who scored maximum marks" #print(gpt.get_top_reply(prompt)) #print(df.loc[(df.loc[:, "Gender"] == "girl") & (df.loc[:, "Marks"] == max(df.loc[:, "Marks"]))])
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
def add_examples( gpt_instance, df_subset): # n is the number of Example instances to "train" GPT-3 on for row in range(df_subset.shape[0]): gpt_instance.add_example( Example(df_subset['Phrase_text'][row], df_subset['Sentiment_class_label'][row])) return gpt_instance
def train_js_model(): gpt_js.add_example( Example( 'Connect to a MONGODB', '''//Import the mongoose module var mongoose = require('mongoose'); //Set up default mongoose connection var mongoDB = 'mongodb://127.0.0.1/my_database'; mongoose.connect(mongoDB, {useNewUrlParser: true, useUnifiedTopology: true}); //Get the default connection var db = mongoose.connection; //Bind connection to error event (to get notification of connection errors) db.on('error', console.error.bind(console, 'MongoDB connection error:'));''')) gpt_js.add_example( Example( 'Define a schema for MongoBD', ''' //Require Mongoose var mongoose = require('mongoose'); //Define a schema var Schema = mongoose.Schema; var SomeModelSchema = new Schema({ a_string: String, a_date: Date }); ''')) gpt_js.add_example( Example( 'Create a model', ''' // Define schema var Schema = mongoose.Schema; var SomeModelSchema = new Schema({ a_string: String, a_date: Date }); // Compile model from schema var SomeModel = mongoose.model('SomeModel', SomeModelSchema ); '''))
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
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
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())
def train_sql_model(): gpt_sql.add_example( Example('Fetch unique values of DEPARTMENT from Worker table.', 'Select distinct DEPARTMENT from Worker;')) gpt_sql.add_example( Example( 'Print the first three characters of FIRST_NAME from Worker table.', 'Select substring(FIRST_NAME,1,3) from Worker;')) gpt_sql.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_sql.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_sql.add_example( Example( "Display the second highest salary from the Worker table.", "Select max(Salary) from Worker where Salary not in (Select max(Salary) from Worker);" )) gpt_sql.add_example( Example("Display the highest salary from the Worker table.", "Select max(Salary) from Worker;")) gpt_sql.add_example( Example( "Fetch the count of employees working in the department Admin.", "SELECT COUNT(*) FROM worker WHERE DEPARTMENT = 'Admin';")) gpt_sql.add_example( Example( "Get all details of the Workers whose SALARY lies between 100000 and 500000.", "Select * from Worker where SALARY between 100000 and 500000;")) gpt_sql.add_example( Example("Get Salary details of the Workers", "Select Salary from Worker"))
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 ."
def post_example(): """Adds an empty example.""" new_example = Example("", "") gpt.add_example(new_example) return json.dumps(gpt.get_all_examples())
#Author Nícolas A. Ramos. from pocketsphinx import LiveSpeech from gpt import GPT, Example, add_example from openai import api_key api_key = data["API_KEY"] #Definition of gpt gpt_ = GPT(engine="davinci", temperature=0.5, max_tokens=100) #Trainer add_example(Example('INPUT_ONE', 'OUTPUT_ONE')) add_example(Example('INPUT_TWO', 'OUTPUT_TWO')) add_example(Example('INPUT_TREE', 'OUTPUT_TREE')) add_example(Example('INPUT_FOUR', 'OUTPUT_FOUR')) add_example(Example('INPUT_FIVE', 'OUTPUT_FIVE')) #Capture of Speech while the gpt analyzes the instructions. for phrase in LiveSpeech(): output = gpt_.submit_request(phrase) output.choices[0].text
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'))