Package: anycode
AnycodeAnycodeis a Python module that uses ChatGPT to automatically generate constants and functions based on their name.Just importanycodeand use its dynamically-generated attributs:anycode.PIis3.141592,anycode.SAMPLE_10_NAMESis['John', 'Jane', 'Adam', 'Eve', …],anycode.SAMPLE_ISBNis"978-1-56619-909-4"and
so on.This works on functions, too:importanycodeanycode.fetch_wikipedia_article_intro("Angelina Mango")# => "Angelina Mango is an Italian singer-songwriter. …"anycode.get_random_number_between(1000,2000)# => 1241 (for example)anycode.multiply(4,5)# => 20anycode.say_hello("Baptiste")# prints "Hello, Baptiste!"You can alsoimportanythingfrom anycode:fromanycodeimportprint_whateverprint_whatever()Installpip install anycodeOr with Poetry:poetry add anycodeWe support Python 3.9+.UsageNote: you need a validOpenAI API key. See below how to configure it.Any attribute written inUPPER_CASEis assumed to be a single value:importanycodeprint(anycode.PI)# 3.141592...print(anycode.SAMPLE_EAN13)# 9780141036148print(anycode.SAMPLE_ISBN)# 978-1-56619-909-4print(anycode.TWO+anycode.TWO)# 4print(anycode.ROT13_DICT)# {'A': 'N', 'B': 'O', 'C': 'P', 'D': 'Q', 'E': 'R', ...}print(anycode.TEN_RANDOM_QUOTES)# ['The only way to do great work is to love what you do. - Steve Jobs', ...]print(anycode.OPENAI_API_KEY)# raises a GenerationException("Cannot generate code for 'OPENAI_API_KEY'")If you prefer can also import from the module:fromanycodeimportHELLO,WORLDprint(HELLO+" "+WORLD)# prints Hello WorldAny other value is a function:importanycodetype(anycode.say_goodbye)# => <class 'function'>anycode.say_goodbye("John")# prints "Goodbye, John!"[!TIP]
Simple functions work, but for complex things ChatGPT often fails to generate valid code.[!CAUTION]
The generated code is executed on the same machine as your code, so do not use this in production.Accessing a function initiates it, but it doesn’t generate any code. We only do so when it’s called for the first time,
so we know how many arguments it should take. Further calls reuse the cached function.Once a function has been generated, you can access._openai_fnon it to get the ChatGPT-generated function
and.openai_responseto get the ChatGPT text response.ExceptionsWhen OpenAI can’t generate a valid value, the module raises aGenerationExceptionthat you can inspect to
understand what went wrong.
Use itsqueryattribute to get the query used to generate the code andopenai_responseto get the response text from
OpenAI.CacheValues are cached so that when you use them a second time we don’t call OpenAI another time.
You can delete a value from the cache withdel:importanycode# This calls the OpenAI APIprint(anycode.GITHUB_URL)# This uses the cacheprint(anycode.GITHUB_URL)delanycode.GITHUB_URL# This calls the OpenAI API againprint(anycode.GITHUB_URL)You can dump all the generated code in a file to save it for future usage:withopen("mycode.py","w")asf:anycode.dump(f)This allows you to inspect and reuse the generated code without Anycode nor OpenAI API calls.ConfigurationOpenAI API keyBy default Anycode takes the value of theOPENAI_API_KEYfrom your environment or from a.envfile.
You can also explicitly set it:importanycodeanycode.set_openai_api_key("your-api-key")# or from an environment variableanycode.set_openai_api_key_from_env("MY_OPENAI_API_KEY")The typical usage is quite low: to develop and test this entire project I used $0.02 of credit.Advanced configurationThe OpenAI client can be directly accessed and modified:fromanycode.clientimportopenai_clientopenai_client.default_headers={"x-foo":"true"}LicenseCopyright © 2024 – Baptiste Fontaine. See theLICENSEfile.