You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Library to ask OpenAI GPT for generating objects on the Python runtime.
This library is prepared to record prompts that would be useful for Python programs.If you developed something, let's make a pull request!
Getting started
Installation
To installdavinci-functions, you can simply type the following command:
pip instal davinci-functions
Using the library
Set your OpenAI Organization ID and API key before using this library.Then invoke the functions in the library.
importopenaiimportdavinci_functionsopenai.organization="YOUR_ORG_ID"openai.api_key="YOUR_API_KEY"prompt="""\Output the list of 10 random city names in the United States."""forvalindavinci_functions.list(prompt):print(val)
This script will print something like:
New YorkLos AngelesChicagoHoustonPhoenixPhiladelphiaSan AntonioSan DiegoDallasSan Jose
References
davinci_functions.list
Returns the list of something.
>>>davinci_functions.list("say hello.")['Hello']>>>davinci_functions.list("say hello world.")['Hello','world']>>>davinci_functions.list("Output first 5 prime numbers.")[2,3,5,7,11]>>>davinci_functions.list("5 random countries")['Japan','Australia','Brazil','India','China']
Solving some tasks (e.g., named entity recognition):
>>>prompt="""... Extract all named entities in the following paragraph:...... Google is founded by Larry Page and Sergey Brin in 1998.... The headquarter is located in Mountain View, Carifornia, United States.... """>>>davinci_functions.list(prompt)['Google','Larry Page','Sergey Brin','Mountain View','Carifornia','United States']
>>>davinci_functions.judge("The sum of 2 and 3 is 5.")True>>>davinci_functions.judge("The sum of 2 and 3 is 6.")False>>>davinci_functions.judge("San Francisco is the capital of the United States.")False>>>davinci_functions.judge("New York is the capital of the United States.")True# Wrong answer! This kind of mistakes happens very often: please take care.>>>davinci_functions.judge("Washington D.C. is the capital of the United States. How about New York?")False
davinci_functions.function
Synthesizes a Python function described in the prompt.
This function is not secure. Do not use this function in real products.
>>>f=davinci_functions.function("Multiply the argument x by 2.")>>>f(3)6>>>f=davinci_functions.function("Arithmetic mean of all elements in the list x.")>>>f([1,2,3])2.0>>>f=davinci_functions.function("""\... Given a list of unique integers x, return two positions so that the sum of the... numbers on that positions is equal to the argument y.... The function must be efficient as well as possible.... """)>>>f([1,4,9,16,25],25)(3,2)
davinci_functions.explain
Describes the behavior of given functions.
>>>deff(x):...returnx*3...>>>davinci_functions.explain(f)'This function takes a variable x and multiplies it by 3, then returns the result.'>>>deff(a,b,c):...return (-b+math.sqrt(b**2-4.0*a*c))/ (2.0*a)...>>>davinci_functions.explain(f)'ThisfunctionimplementstheQuadraticFormulatocalculatethesolutionofa ...quadraticequation.Theequationisoftheformax^2+bx+c=0.Thefunction ...takesthreeparametersa,b,andc,whicharethecoefficientsoftheequation.It ...thencalculatesthesolutionusingtheformula (-b+sqrt(b^2-4ac))/ (2a)and ...returnstheresult.'
Caveats
Right now, this library doesn't consider prompt injection and validity of the returnedexpression by GPT. Please don't use this library in the real products that needs totake care about consistency and security.
About
Library to ask OpenAI GPT for generating objects on the Python runtime.