Back to Browse

Programming - Python - using the openAI API to access chatGPT and DALL-E

393 views
Jan 5, 2023
20:02

#chatgpt #dalle2 #python #pythonprogramming #openai example call to the script: =========================================================================== python chat.py --mtype com --temperature 1.0 --prompt "Tell me why its a really good idea to subscribe to this youtube channel." creds.json ================== { "org": "your_org", "key": "your_key" } chat.py =================================== import os, re, argparse, json, openai # setup openai def init_openai(): f = open('creds.json') creds = json.load(f) openai.organization = creds["org"] openai.api_key = creds["key"] f.close() # parse args def init_argparse(): parser = argparse.ArgumentParser(description='Make calls the the OpenAI API.') parser.add_argument("--mtype", type=str, default="com", help="The type of model to use. com, cod, img, or emb == completion, code, image, or embedded") parser.add_argument("--temperature", type=float, default=1.0, help="The temperature of the model. Must be greater than 0.0 and less than 1.0, Zero provides 'safer' responses and One provides 'riskier' responses") parser.add_argument("--prompt", type=str, default="I am a clown", help="The input string (prompt), which can vary depending on the type of model you are using.") args = parser.parse_args() mtype = args.mtype temperature = args.temperature prompt = re.sub(r"\(\S{1,20}\)", "", args.prompt.replace('\n', '').strip()) return {"mtype": mtype, "temperature": temperature, "prompt": prompt} # run model, based on args def run_model(input_data): if(input_data["mtype"] == "com"): ''' Completion Test ''' res = openai.Completion.create( model="text-davinci-003", prompt=input_data["prompt"], max_tokens=3000, temperature=input_data["temperature"], echo=True, user="session000" ) txtResponse = res.choices[0].text print(txtResponse) elif(input_data["mtype"] == "mar"): ''' Marv Test ''' res = openai.Completion.create( model="text-davinci-003", prompt=input_data["prompt"], temperature=0.5, max_tokens=60, top_p=0.3, frequency_penalty=0.5, presence_penalty=0.0 ) txtResponse = res.choices[0].text print(txtResponse) elif(input_data["mtype"] == "cod"): ''' Coding Test ''' res = openai.Completion.create( model="code-davinci-002", prompt=input_data["prompt"], temperature=input_data["temperature"], max_tokens=3500, top_p=1, frequency_penalty=0, presence_penalty=0 ) txtResponse = res.choices[0].text print(txtResponse) elif(input_data["mtype"] == "img"): ''' Image Test ''' res = openai.Image.create( prompt=input_data["prompt"], n=1, size="1024x1024" ) image_url = res['data'][0]['url'] print(image_url) elif(input_data["mtype"] == "emb"): ''' Embedding Test ''' res = openai.Embedding.create( model="text-embedding-ada-002", input=input_data["prompt"] ) print(res) elif(input_data["mtype"] == "rec"): ''' Recipe Test ''' res = openai.Completion.create( model="text-davinci-003", prompt=input_data["prompt"], temperature=0.3, max_tokens=2000, top_p=1.0, frequency_penalty=0.0, presence_penalty=0.0 ) txtResponse = res.choices[0].text print(txtResponse) else: print("invalid input parameters") def main(): init_openai() input_data = init_argparse() run_model(input_data) if __name__ == "__main__": main()

Download

0 formats

No download links available.

Programming - Python - using the openAI API to access chatGPT and DALL-E | NatokHD