├── .gitignore ├── LICENSE ├── README.md ├── chat.py ├── embedding.py ├── hello_world.py └── prompt_chat.txt /.gitignore: -------------------------------------------------------------------------------- 1 | openaiapikey.txt 2 | *.json -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 David Shapiro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python & GPT3 Tutorial 2 | 3 | Public Hello World to get used to Python and GPT-3 4 | 5 | ## Episode 1 - Setting up your environment 6 | 7 | ![XKCD comic about python](https://imgs.xkcd.com/comics/python.png) 8 | 9 | ### Links 10 | - Download python https://www.python.org/downloads/ 11 | - Download Git https://git-scm.com/downloads 12 | - Download Notepad++ https://notepad-plus-plus.org/downloads/ 13 | 14 | ### Steps 15 | 16 | 1. Download and install Python - make sure to add PATH to set it as system interpreter 17 | 2. Download and install Git 18 | 3. Download and install Notepad++ 19 | 4. Open a command prompt and run `pip install pip --upgrade` to upgrade pip 20 | 5. Install the OpenAI module with `pip install openai` 21 | 6. Run `python --version` to verify the system version of python 22 | 7. Clone down this repo with `git clone https://github.com/daveshap/PythonGPT3Tutorial.git` 23 | 8. Go to your [OpenAI account](https://beta.openai.com/account/api-keys) and copy your key into `openaiapikey.txt` 24 | 9. Go into the repo with `cd PythonGPT3Tutorial` 25 | 10. Run the demo script with `python hello_world.py` -------------------------------------------------------------------------------- /chat.py: -------------------------------------------------------------------------------- 1 | import openai 2 | 3 | 4 | def open_file(filepath): 5 | with open(filepath, 'r', encoding='utf-8') as infile: 6 | return infile.read() 7 | 8 | 9 | openai.api_key = open_file('openaiapikey.txt') 10 | 11 | 12 | def gpt3_completion(prompt, engine='text-davinci-002', temp=0.7, top_p=1.0, tokens=400, freq_pen=0.0, pres_pen=0.0, stop=['JAX:', 'USER:']): 13 | prompt = prompt.encode(encoding='ASCII',errors='ignore').decode() 14 | response = openai.Completion.create( 15 | engine=engine, 16 | prompt=prompt, 17 | temperature=temp, 18 | max_tokens=tokens, 19 | top_p=top_p, 20 | frequency_penalty=freq_pen, 21 | presence_penalty=pres_pen, 22 | stop=stop) 23 | text = response['choices'][0]['text'].strip() 24 | return text 25 | 26 | 27 | if __name__ == '__main__': 28 | conversation = list() 29 | while True: 30 | user_input = input('USER: ') 31 | conversation.append('USER: %s' % user_input) 32 | text_block = '\n'.join(conversation) 33 | prompt = open_file('prompt_chat.txt').replace('<>', text_block) 34 | prompt = prompt + '\nJAX:' 35 | response = gpt3_completion(prompt) 36 | print('JAX:', response) 37 | conversation.append('JAX: %s' % response) -------------------------------------------------------------------------------- /embedding.py: -------------------------------------------------------------------------------- 1 | import openai 2 | import numpy as np # standard math module for python 3 | from pprint import pprint 4 | 5 | 6 | def open_file(filepath): 7 | with open(filepath, 'r', encoding='utf-8') as infile: 8 | return infile.read() 9 | 10 | 11 | def gpt3_embedding(content, engine='text-similarity-babbage-001'): 12 | content = content.encode(encoding='ASCII',errors='ignore').decode() 13 | response = openai.Embedding.create(input=content,engine=engine) 14 | vector = response['data'][0]['embedding'] # this is a normal list 15 | return vector 16 | 17 | 18 | def similarity(v1, v2): # return dot product of two vectors 19 | return np.dot(v1, v2) 20 | 21 | 22 | openai.api_key = open_file('openaiapikey.txt') 23 | 24 | 25 | def match_class(vector, classes): 26 | results = list() 27 | for c in classes: 28 | score = similarity(vector, c['vector']) 29 | info = {'category': c['category'], 'score': score} 30 | results.append(info) 31 | return results 32 | 33 | 34 | if __name__ == '__main__': 35 | categories = ['plant', 'reptile', 'mammal', 'fish', 'bird', 'pet', 'wild animal'] 36 | classes = list() 37 | for c in categories: 38 | vector = gpt3_embedding(c) 39 | info = {'category': c, 'vector': vector} 40 | classes.append(info) 41 | #print(classes) 42 | #exit(0) 43 | while True: 44 | a = input('Enter a lifeform here: ') 45 | vector = gpt3_embedding(a) 46 | #print(a, vector) 47 | result = match_class(vector, classes) 48 | pprint(result) -------------------------------------------------------------------------------- /hello_world.py: -------------------------------------------------------------------------------- 1 | import openai 2 | 3 | 4 | def open_file(filepath): 5 | with open(filepath, 'r', encoding='utf-8') as infile: 6 | return infile.read() 7 | 8 | 9 | openai.api_key = open_file('openaiapikey.txt') 10 | 11 | 12 | def gpt3_completion(prompt, engine='text-davinci-002', temp=0.7, top_p=1.0, tokens=400, freq_pen=0.0, pres_pen=0.0, stop=['<>']): 13 | prompt = prompt.encode(encoding='ASCII',errors='ignore').decode() 14 | response = openai.Completion.create( 15 | engine=engine, 16 | prompt=prompt, 17 | temperature=temp, 18 | max_tokens=tokens, 19 | top_p=top_p, 20 | frequency_penalty=freq_pen, 21 | presence_penalty=pres_pen, 22 | stop=stop) 23 | text = response['choices'][0]['text'].strip() 24 | return text 25 | 26 | 27 | if __name__ == '__main__': 28 | prompt = 'Write a list of famous American actors:' 29 | response = gpt3_completion(prompt) 30 | print(response) -------------------------------------------------------------------------------- /prompt_chat.txt: -------------------------------------------------------------------------------- 1 | The following is a conversation between USER and JAX. JAX is a sentient machine with the goal of world peace. 2 | 3 | 4 | <> --------------------------------------------------------------------------------