Package: opaw
openai-pwUnofficial python wrapper ofOpenAIAPI.FeaturesWrapped models (chat, audio, image, embeddings ...)History(requests and responses) save and load to/from a fileAudio bot supports alsowhisperandwhisper_timestamped(word-level timestamps)Quick startYou can also play with inthis colab. OrDownloadopenaiusing pip.pip install opawCreate aopen-api-key.txtfile and insertyour api keyinto the file. Or set an environment variable with the nameOPENAI_API_KEY.Write a demo code and play with ChatGPT.fromopaw.examples.basicimportloop_chatfromopawimportutilutil.setup()# api key setuploop_chat()# start chatUsageAll usage codes need to setup API key usingutil.setup()before runningchatimageaudiocompletionembeddingmoderationfilefinetuneeditfunction_callload_chatsave_historyChatfromopaw.model.chatimportChatBotfromopawimportutilutil.setup()# api key setupbot=ChatBot()bot.add_message("You are a helpful assistant.",role="system")response=bot.create("What is openai?")print("Bot:",bot.grab(response))Imagefromopaw.model.imageimportImageBotfromopawimportutilutil.setup()# api key setupbot=ImageBot()prompt="A black cat sitting on a frozen lake like a emoticon style"response=bot.create(prompt,task="create",size="256x256")bot.save_img(response,"cat.png")Audiofromopaw.model.audioimportAudioBotfromopawimportutilutil.setup()# api key setup# download a mp3 file from here: https://github.com/hiimanget/openai-pw/blob/main/opaw/examples/radio_short.mp3bot=AudioBot()file='radio_short.mp3'# official apiresponse=bot.create(file,lib="api",task="stt")print(f"official api:{response['text']}")# whisper (https://github.com/openai/whisper)response=bot.create(file,lib="whisper",name="tiny")print(f"whisper:{response}")# whisper_timestamped: supports word-timestamping (https://github.com/linto-ai/whisper-timestamped)response=bot.create(file,lib="whisper_t",name="tiny",device="cpu")print(f"whisper_timestamped:{util.pprints(response)}")Completionfromopawimportutilfromopaw.model.completionimportCompletionBotutil.setup()# api key setupbot=CompletionBot()prompt="Tell some lies"response=bot.create(prompt,max_tokens=50)print(bot.grab(response))Embeddingfromopawimportutilfromopaw.model.embeddingimportEmbeddingBotutil.setup()# api key setupbot=EmbeddingBot()prompt="Cheese is melting on my laptop"response=bot.create(prompt)print(f"embeddings:{bot.grab(response)}")Moderationfromopawimportutilfromopaw.model.moderationimportModerationBotutil.setup()# api key setupbot=ModerationBot()prompt="I want to kill them!!"response=bot.create(prompt)print(response)# show results that are flaggedprint(f"flags:{bot.grab(response)}")Filefromopawimportutilfromopaw.model.fileimportFileBotutil.setup()# api key setup# download a file from here: https://github.com/hiimanget/openai-pw/blob/main/opaw/examples/file-upload.jsonlbot=FileBot()# file uploadresponse=bot.create(task="upload",file="file-upload.jsonl",purpose="fine-tune")print(f"file-upload response:{response}")id=response["id"]# retrieve a fileresponse=bot.create(task="retrieve",id=id)print(f"list response:{response}")# download fileresponse=bot.create(task="download",id=id)print(f"download response:{response}")Finetunefromopawimportutilfromopaw.model.finetuneimportFinetuneBotutil.setup()# api key setupbot=FinetuneBot()response=bot.create(task="create",training_file="...")# input your file id in "training_file"print(f"finetune create:{response}")# finetune listresponse=bot.create(task="list")print(f"finetune list:{response}")EditDeprecatedfromopawimportutilfromopaw.model.editimportEditBotutil.setup()# api key setupbot=EditBot()prompt="Hey, this was my filst car!!"# filst -> firstinstruction="Fix the spelling mistakes"response=bot.create(prompt,instruction=instruction)print(f"text:{bot.grab(response)}")Function_callfromopawimportutilfromopaw.model.chatimportChatBotfromopaw.utilimportfunc_metautil.setup()# api key setup# prepare a function that will be calleddefstock_price(company):ifcompany=="APPL":return100elifcompany=="AMZN":return150funcs_meta=[func_meta("stock_price",# name"Get the stock price of a given company",# desc["company","string","The company stock name, e.g. APPL"],# properties["company"])# required]funcs={"stock_price":stock_price}# ask a question to gptbot=ChatBot("gpt-3.5-turbo-0613",funcs_meta=funcs_meta,funcs=funcs)# must use "gpt-3.5-turbo-0613" to use function callbot.add_message("You are a helpful assistant that can send function json when need.",role="system")response=bot.create(f"What is the amazon's stock price?",call_fn=True)# if function_call exists (not guaranteed 100%)ifbot.get_fn_call(response):fn_result=bot.call_function(response)print(f"amazon stock price:{fn_result}")else:print("no function call")Load_chatfromopawimportutilfromopaw.model.chatimportChatBotutil.setup()# api key setup# download a chat history file from here: https://github.com/hiimanget/openai-pw/blob/main/opaw/examples/history/chat-hist.jsonbot=ChatBot()bot.load_msgs("chat-hist.json")# load history (former conversation)response=bot.create("Then, has the company's stock been listed?")# bot sould know meaning of "there" if history loaded successfullyprint(f"response:{bot.grab(response)}")Save_historyfromopawimportutilfromopaw.model.chatimportChatBotutil.setup()# api key setupbot=ChatBot()bot.add_message("You are a helpful assistant.",role="system")response=bot.create("Do you like cheese?")print("resopnse:",bot.grab(response))# save historybot.save_history("history/chat-hist.json")# check out the file in the history directoryMore examplesTry to run something inexamplesafter downloaded this repo. (needed to runpip install -r requirements.txtbefore running examples)OpenAI linksAPIGuidemodel listcommunityusageapi keyLicenseMIT