Saravanan666/pypi-org huggingface dataset:
Package: openai-model-router
OpenAI Model RouterGiven the number of OpenAI models, it can get tricky to identify which model to call for a given prompt. So we built the Model Router, to dynamically select themost appropriate modelfor our prompt.List of supported models:gpt-3.5-turbo, gpt-3.5-turbo-0613, gpt-3.5-turbo-16k, gpt-3.5-turbo-16k-0613Support for more models coming soon ;)UsageSimply use the Model Router instead of using OpenAI directly. And, no need to specify the model anymore!Example 1: Simple MessageBeforeimportopenaiopenai.api_key="sk-..."chat_completion=openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=[{"role":"user","content":"Hello world"}])print(chat_completion.choices[0].message.content)Afterfrommodel_routerimportopenai# import from model_router insteadopenai.api_key="sk-..."chat_completion=openai.ChatCompletion.create(# skip the modelmessages=[{"role":"user","content":"Hello world"}])print(chat_completion.choices[0].message.content)Example 2: Function CallingBeforeimportopenaiopenai.api_key="sk-..."chat_completion=openai.ChatCompletion.create(model="gpt-3.5-turbo-0613",messages=[{"role":"user","content":"What's the weather like in Boston?"}],functions=[...],function_call="auto",)print(chat_completion.choices[0].message.content)Afterfrommodel_routerimportopenai# import from model_router insteadopenai.api_key="sk-..."chat_completion=openai.ChatCompletion.create(# skip the modelmessages=[{"role":"user","content":"What's the weather like in Boston?"}],functions=[...],function_call="auto",)print(chat_completion.choices[0].message.content)Questions/FeedbackFor any questions/feedback, please reach out to@sambuddha_basu
Package: openai-multi-tool-use-parallel-patch
Patchingmulti_tool_use.parallelin OpenAIQuick monkey-patching fix for OpenAI's hallucinated multi_tool_use.parallel issue.This is a simple workaround forthis issuewhere the GPT ChatCompletions API sometimes hallucinates a strange alternative way of calling multiple tools at once.
In my experience, these calls at least always follow a predictable structure and can be hotfixed to look like normal, legal tool calls. This patch does that, and also rewrites the message accordingly (which, in my experience, makes the model perform calls work in the normal expected way.)This fix will be redundant soon, but it looks like I am not the only one who occasionally runs into this error, so I thought I would share this workaround.Installation and usagepip install openai-multi-tool-use-parallel-patchIn your code:importopenai_multi_tool_use_parallel_patch# import applies the patchimportopenaiclient=openai.AsyncOpenAI(...)# sync client will be patched too...response=awaitclient.chat.completions.create(...)# no changes to the call signature or response vs vanilla OpenAI clientThat's it - theoretically this should work even if youimport openaifirst, but at the least you will need to make sure not to create youropenai.OpenAIoropenai.AsyncOpenAIinstances before importing this patch.
Package: openai-pricing
openai-pricing================This repository hosts an unofficial library for OpenAI pricing calculations. Its purpose is to simplify the prediction of costs when using GPT models. The data is obtained fromhttps://gptforwork.com/tools/openai-chatgpt-api-pricing-calculator.Library usageThe module provides just one class,OpneAIPricing, which loads the pricing data when instanced and answers about max tokens and costs per tokens (prompt and completion tokens).Currently, it supports the following models:GPT-4 32kGPT-4GPT-3.5 16kGPT-3.5DavinciCurieBabbageAdaThis list can be obtained by callingOpenAIPricing.list_available_models().Examplefromopenai_pricingimportOpenAIPricingp=OpenAIPricing()p.list_available_models()model='gpt-4-32k'tokens=1000# default value for tokens is 1000model_name=p.model_name(model)# get model namemax_tokens=p.get_max_tokens(model)# get the max tokens for a modelprice_prompt=p.get_price(model,type='prompt',tokens=1000)# get the current price per 1,000 tokens in promptprice_completion=p.get_price(model,type='completion',tokens=1000)# get the current price per 1,000 tokens in completionprint('The model',model_name)print('accepts up to',max_tokens,'tokens.')print('It costs $',price_prompt,'per 1,000 prompt tokens')print('and $',price_completion,'per 1,000 completion tokens.')# Get an average price per requests with  "p.get_price(model, type='both')". "both" is the default value for the "type" argument and 1000 is the default value for the "tokens" argument.print('The average value for request is',p.get_price(model,type='both'),'per 1,000 tokens (500 tokens in prompt and 500 pormpt in completion.')Output:+--------------+-------------------+|Modelname|Technicalname||--------------+-------------------||GPT-432k|gpt-4-32k||GPT-4|gpt-4||GPT-3.516k|gpt-3.5-turbo-16k||GPT-3.5|gpt-3.5-turbo||Davinci|text-davinci-003||Curie|text-curie-001||Babbage|text-babbage-001||Ada|text-ada-001|+--------------+-------------------+
ThemodelGPT-432k
acceptsupto32768tokens.
Itcosts$0.06per1,000prompttokens
and$0.12per1,000completiontokens.
Theaveragevalueforrequestis0.09per1,000tokens(500tokensinpromptand500pormptincompletion.
Package: openai-pricing-calc-draft
OPEN AI API - PRICE CALCULATOROverviewThis package is created to calculate cost of OPEN AI API usage.Pricing based on following url :OPEN AI Pricing API.
Source code:GithubUsageInstallationInstall Pagepip install openai-pricing-calc-draftWithout Surrounding Codefromlll_pricing_calculationimportcalculate_openai_pricing# Without surrounding CodecostForThousandCurrency,embeddingsCost,promptCost,completionTokenCost,total_cost=calculate_openai_pricing("GPT-3.5 Turbo","4K context",token_counter.total_embedding_token_count,token_counter.prompt_llm_token_count,token_counter.completion_llm_token_count)print("currency:"+costForThousandCurrency)print("embeddingsCost:"+str(embeddingsCost))print("promptCost:"+str(promptCost))print("completionTokenCost:"+str(completionTokenCost))print("total cost:"+str(total_cost))With Surrounding Code Using Llama Indeximporttiktokenfromllama_index.callbacksimportCallbackManager,TokenCountingHandlerfromllama_indeximportVectorStoreIndex,SimpleDirectoryReader,ServiceContextfromlll_pricing_calculationimportcalculate_openai_pricingsampleQuery="Sample Query"token_counter=TokenCountingHandler(tokenizer=tiktoken.encoding_for_model("text-davinci-003").encode,verbose=False# set to true to see usage printed to the console)callback_manager=CallbackManager([token_counter])service_context=ServiceContext.from_defaults(callback_manager=callback_manager)defaskQuestion(quest,storage,service_context,token_counter):token_counter.reset_counts()# index defined outsidespecificindex=index.get_index(dataFolder,"./storage"+storage,service_context)print(quest)result=query.query_index(specificindex,quest,"./storage"+storage)print(result)# otherwise, you can access the count directlyprint("Embeddings Token Counter stuff is below (total_embedding_token_count):")print(token_counter.total_embedding_token_count)print("Detailed ")print('Embedding Tokens: ',token_counter.total_embedding_token_count,'\n','LLM Prompt Tokens: ',token_counter.prompt_llm_token_count,'\n','LLM Completion Tokens: ',token_counter.completion_llm_token_count,'\n','Total LLM Token Count: ',token_counter.total_llm_token_count)# CALCULATE PRICING TAKES PLACE HEREcostForThousandCurrency,embeddingsCost,promptCost,completionTokenCost,total_cost=calculate_openai_pricing("GPT-3.5 Turbo","4K context",token_counter.total_embedding_token_count,token_counter.prompt_llm_token_count,token_counter.completion_llm_token_count)print("currency:"+costForThousandCurrency)print("embeddingsCost:"+str(embeddingsCost))print("promptCost:"+str(promptCost))print("completionTokenCost:"+str(completionTokenCost))print("total cost:"+str(total_cost))askQuestion(sampleQuery,"4",service_context,token_counter)
Package: openai-pricing-scraper
OpenAI Pricing ScraperThis package provides a simple way to fetch and parse the pricing data from the OpenAI pricing page. It can be used as a standalone script or as a module imported into other applications.InstallationTo install the package, you can either clone the repository and install it locally or install it directly from the source using pip:pipinstallgit+https://github.com/8ByteSword/openai_pricing_scraper.gitorpipinstallopenai_pricing_scraper(Github and pypi versions may differ)UsageAs a modulefromopenai_pricing_scraperimportfetch_pricing_datapricing_data=fetch_pricing_data()print(pricing_data)As a standalone scriptRun thefetch_pricing.pyscript directly:pythonfetch_pricing.pyResultThe result will be given as a JSON. At the time of writing would be:LicenseThis project is licensed under theMIT License.
Package: openai-promptify-py
openai-promptify-pyImportant Alert: This library is not supported by OpenAI. But only a utility wrapper maintained by certain human
beings.OpenAI Promptify Py is a Python library that provides a wrapper on top of OpenAI API models and abstracts out some
details, such as retrying and chunking into smaller calls. The library also enables users to give model configurations
as a dictionary, including the OpenAI model name, temperature, frequency penalty, presence penalty, and a prompt. Users
can then call the API with a "proxy model name" and get the results without having to provide all configurations again
and again.InstallationYou can install OpenAI Promptify Py using pip:pip install openai-promptify-pyUsageHere's an example of how to use OpenAI Promptify Py:from openai_promptify_py import OpenAIPromptifyPy

    models = {
        'foo': {
            'model_name': 'text-ada-001',
            'prompt': 'Lazy brown {animal} jumped {location}',
        },
    }

    feature = OpenAIPromptify(openai_key=os.environ.get('OPENAI_KEY'), model_repo=models)
    response = feature.get_response('foo', {'animal': 'fox', 'location': 'over the moon'})ContributingIf you'd like to contribute to OpenAI Promptify Py, please open an issue or pull request on our GitHub repository:https://github.com/tumatrix/openai-promptify-pyTo publish:python -m pip install build twine

python -m build

twine check dist/*

twine upload dist/*LicenseOpenAI Promptify Py is licensed under the MIT License.
Package: openai-pygenerator
openai-pygeneratorImport NoteThe openai Python API versions 1.0.0 and later now incorporate retry functionality and type annotations. This package has been migrated to the new API, but should only be used as a temporary measure to ensure backward compatibility.  If you are using this package in production, consider rewriting your code so that it uses the new openai API directly.OverviewThis is a simple type-annotated wrapper around the OpenAI Python API which:provides configurable retry functionality,reduces the default timeout from 10 minutes to 20 seconds (configurable),provides a simple class to manage chat session state, andprovides a generator over completions.It can also be used to chain together completions from different prompts in a very straightforwardfunctional-programmingstyle usingPython generators.Installationpipinstallopenai-pygeneratorBasic usageIn the example below we will retry automatically if there is aRateLimitError.fromopenai_pygeneratorimportChatSessionsession=ChatSession()solution=session.ask("What is the square root of 256?")print(solution)working=session.ask("Show your working")print(working)print("Transcript:")print(session.transcript)Completion pipelines and overriding parametersfromtypingimportIterablefromopenai_pygeneratorimport(ChatSession,Completions,completer,content,next_completion,user_message,)high_temp_completions=completer(temperature=0.8)defheading(message:str,margin:int=80)->None:print()print("-"*margin)print(message)print("-"*margin)print()defexample_square_root(session:ChatSession)->None:solution=session.ask("What is the square root of 256?")print(solution)working=session.ask("Show your working")print(working)heading("Session transcript:")print(session.transcript)defcreative_answer(prompt:str,num_completions:int=1)->Completions:returnhigh_temp_completions([user_message(prompt)],n=num_completions)defpick_color(num_completions:int)->Completions:returncreative_answer("Pick a color at random and then just tell me your choice, e.g. 'red'",num_completions,)defgenerate_sentence(color_completions:Completions)->Iterable[str]:forcolor_completionincolor_completions:color=content(color_completion)result=next_completion(creative_answer(f"Write a sentence about the color{color}."))ifresultisnotNone:yieldcontent(result)if__name__=="__main__":heading("Find square root - using environment variables for parameters")example_square_root(session=ChatSession())heading("Find square root - overriding temperature, max_tokens, max_retries")example_square_root(session=ChatSession(generate=completer(temperature=0.5,max_tokens=300,max_retries=5)))heading("Example completion pipeline")forsentenceingenerate_sentence(pick_color(num_completions=10)):print(sentence)RunningexportOPENAI_API_KEY=<key>
pythonsrc/openai_pygenerator/example.pyConfigurationTo override default parameters use the following shell environment variables:exportGPT_MODEL=gpt-3.5-turboexportGPT_TEMPERATURE=0.2exportGPT_MAX_TOKENS=500exportGPT_MAX_RETRIES=5exportGPT_REQUEST_TIMEOUT_SECONDS=20exportOPENAI_API_KEY=<key>
pythonsrc/openai_pygenerator/example.py
Package: openai-python-cache
🍰 openai-python-cacheA thin wrapper around the OpenAI Python bindings for caching responses.MotivationI'm experimenting with a large-ish dataset locally that gets injected into GPT
prompts. Responses are not perfect, and occassionally I have to tweak some
of my data. This means that I'm making API calls for results that are okay,
because it's iterating over the entire dataset.This solves the issue by cache-ing OpenAI responses in a local SQLite3 database.Only ChatCompletion is supportedat this time because it's the only API I use.This is a quick and dirty solution. I'd go a level lower and inject this
behaviour directly in the requestor, but I don't have time to figure that
part out (yet?)!Installation# Using pip:$pipinstallopenai-python-cache# Using poetry:$poetryaddopenai-python-cacheUsageimportosimportopenaifromopenai_python_cache.apiimportChatCompletionfromopenai_python_cache.providerimportSqlite3CacheProvideropenai.api_key=os.environ.get("OPENAI_API_KEY")# Create a cache providercache_provider=Sqlite3CacheProvider()# Use the ChatCompletion class from `openai_python_cache`completion=ChatCompletion.create(model="gpt-3.5-turbo",messages=[{"role":"user","content":"Tell the world about the ChatGPT API in the style of a pirate.",}],# Inject the cache provider. Requests will NOT be cached if this is not# provided!cache_provider=cache_provider,)print(completion)Demoimportosimporttimeimportopenaifromopenai_python_cache.apiimportChatCompletionfromopenai_python_cache.providerimportSqlite3CacheProvideropenai.api_key=os.environ.get("OPENAI_API_KEY")cache_provider=Sqlite3CacheProvider()params={'model':"gpt-3.5-turbo",'messages':[{"role":"user","content":"Testing cache!",}]}# First request, cache miss. This will result in an API call to OpenAI, and# the response will be saved to cache.c0start=time.time()ChatCompletion.create(cache_provider,**params)c0end=time.time()-c0startprint(f"First request is a cache miss. It took{c0end}seconds!")# >>> First request is a cache miss. It took 1.7009928226470947 seconds!# Second request, cache hit. This will NOT result in an API call to OpenAI.# The response will be served from cache.c1start=time.time()ChatCompletion.create(cache_provider,**params)c1end=time.time()-c1startprint(f"Second request is a cache hit. It took{c1end}seconds!")# >>> Second request is a cache hit. It took 0.00015616416931152344 seconds!
Package: openai-quickstart-python
OpenAI API Quickstart - Python example appThis is an example pet name generator app used in the OpenAI APIquickstart tutorial. It uses theFlaskweb framework. Check out the tutorial or follow the instructions below to get set up.SetupIf you don’t have Python installed,install it from here.Clone this repository.Navigate into the project directory:$cdopenai-quickstart-pythonCreate a new virtual environment:$python-mvenvvenv
$.venv/bin/activateInstall the requirements:$pipinstall-rrequirements.txtMake a copy of the example environment variables file:$cp.env.example.envAdd yourAPI keyto the newly created.envfile.Run the app:$flaskrunYou should now be able to access the app athttp://localhost:5000! For the full context behind this example app, check out thetutorial.
Package: openai_request_runner
OpenAI Request RunnerA lightweight Python package designed to facilitate parallel processing of OpenAI API requests. This implementation is inspired by theOpenAI cookbook examplebut offers advanced customization capabilities and integration with OpenAI Functions (leaning on the greatopenai_function_call library). It ensures efficient and organized interactions with the OpenAI models.
FeaturesParallel Processing: Handle multiple OpenAI API requests concurrently.Rate Limiting: Adheres to rate limits set by the OpenAI API.Advanced Customization: Allows for detailed input preprocessing and API response postprocessing.OpenAI Functions: Seamlessly integrates with OpenAI Functions for added capabilities.Error Handling: Efficiently manage and log errors, including rate limit errors.Extendable: Easily integrate with custom schemas and other extensions.InstallationUsing pip (wip)pipinstallopenai_request_runnerGitpipinstallgit@https://github.com/jphme/openai_request_runnerUsing poetryFor local development and testing:poetryinstallUsageMinimal example:fromopenai_request_runnerimportrun_openai_requestsexample_input=[{"id":0,"prompt":"What is 1+1?"}]results=run_openai_requests(example_input,system_prompt="Translate input to French")#in a notebook env you need nest_asyncio, see belowprint(results[0]["content"])# "Qu'est-ce que 1+1 ?"Seeexamples/classify_languages.pyandexamples/translate.pyfor detailed examples of how to use the package for advanced usecases.The package allows for extensive customization. You can set your desired preprocessing function, postprocessing function, and other parameters to suit your specific needs.Refer to the inline documentation and docstrings in the code for detailed information on each function and its parameters.Run inside a notbookIf you want to run openai_request_runner inside a notebook, usenest_asynciolike this:importnest_asyncionest_asyncio.apply()Run Testspoetryrunpytesttests/ContributingContributions are welcome! Please open an issue if you encounter any problems or would like to suggest enhancements. Pull requests are also appreciated.LicenseMIT
Package: openai-requests-utils
openai_requests_utilsYou need to create an environment variable called "OPENAI_API_KEY" which contains your OpenAI API key. You also need to create a config file called oai_config.json in the same folder as your script to choose the model and the API call parameters. Every parameter except "model" is optional and can be removed.Example of oai_config.json:
{
"model": "gpt-3.5-turbo-1106",
"max_response_length": 400,
"timeout": 10,
"temperature": 1,
"top_p": 0.95,
"presence_penalty": 0.5,
"frequency_penalty": 0.5
}
Package: openai-summarizer
# OpenAI SummarizerUse the Completion engine from OpenAI to summarize text.## ApplicationsEmail Summarizer: Summarize your emails in less than 50 words.…## Installation`sh pip install openai_summarizer `## PrerequisitesIf you use Gmail, you doNOTneed to use your gmail’s password to share access of your mailbox with this client.For running the binaryemail-summarizerthrough CLI, you may be able to create an “app password” by following [these instructions](https://support.google.com/accounts/answer/185833?hl=en&ref_topic=7189145) and then using the generated token as your password.(WIP)For accessing the RESTful API, we’ll have OAuth set up for most common email providers so that you don’t have to share your email access credentials with this service.## Usage### Take inputs from stdin.
Pass the email password (or some IMAP access token) and api key through stdin.`shemail-summarizer--mailhost<some-imap-server>-u<some-gmail-email@gmail.com>--prompt-password--prompt-apikey`Example`shemail-summarizer--mailhost'imap.gmail.com'-uuser@gmail.com--prompt-password--prompt-apikey`### Take inputs from env variable.
Pass the email password (or some IMAP access token) and api key as an env variable.`shOPENAI_API_KEY=<API_KEY>EMAIL_SUMMARIZER_MAIL_PASSWORD=<PASSWORD>email-summarizer--mailhost'imap.gmail.com'-uuser@gmail.com `Example`shOPENAI_API_KEY="somesupersecretopenaiapikey"EMAIL_SUMMARIZER_MAIL_PASSWORD="passwordtoaccessemail"email-summarizer--mailhost'imap.gmail.com'-uuser@gmail.com `
Package: openaitelemetry
llm-traceguardllm-traceguardis an open-source Python library that provides Open Telemetry integration with Large Language Models (LLMs). It is designed to facilitate tracing and safeguarding applications that leverage LLMs, ensuring better monitoring and reliability.FeaturesEasy integration with Open Telemetry for LLM applications.Real-time tracing and monitoring of LLM-based systems.Enhanced safeguards and insights for your LLM applications.InstallationTo installllm-traceguard, simply use pip:pipinstallllm-traceguardUsage 🚀Here's a basic example of how to usellm-traceguardin your project:importllm_traceguardllm_traceguard.instrument()Requirements 📋Python 3.8 or higheropentelemetry-apiopentelemetry-sdkContributing 👐Contributions are welcome! For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.License 📄llm-traceguardis licensed under the Apache-2.0 License. SeeLICENSEfor more details.Contact 📧For support or any questions, feel free to contact us atsupport@whylabs.ai.
Package: openai-token-counter
OpenAI Token CounterToken counter for OpenAI messages with support for function token calculation.
This project was ported to python based on the following repository:https://github.com/hmarr/openai-chat-tokensAs stated in hmarr project:Estimating token usage for chat completions isn't quite as easy as it sounds.
For regular chat messages, you need to consider how the messages are formatted by OpenAI when they're provided to the model, as they don't simply dump the JSON messages they receive via the API into the model.
For function calling, things are even more complex, as the OpenAPI-style function definitions get rewritten into TypeScript type definitions.
This library handles both of those cases, as well as a minor adjustment needed for handling the results of function calling. tiktoken is used to do the tokenization.This library is tested nightly againts the openai API to detect for potential breaks if any internal change is made by openai, because as stated above we implement token calculation based on internal OpenAI techniques that are not exposed and can potentially change without notice.InstallationYou can installOpenAI Token Countervia [pip] from [PyPI]:$pipinstallopenai-token-counterUsagefromopenai_token_counterimportopenai_token_countermessages=[{"role":"user","content":"hello"}]functions=[{"name":"bing_bong","description":"Do a bing bong","parameters":{"type":"object","properties":{"foo":{"type":"string"},"bar":{"type":"number","description":"A number"},}}}]result=openai_token_counter(messages=messages,model="gpt-3.5-turbo",# Optional, deafults to cl100k_base encoding which is used by GPT modelsfunctions=functions,# Optionalfunction_call="auto"# Optional)print(result)# Output: '57'ContributingContributions are very welcome.Install poetryInstall the project dependenciespoetryinstallMake the changesTest locally using nox (no need to test all python versions, select only 3.10):nox --python=3.10Create a PR in GitHub.LicenseDistributed under the terms of theMIT,OpenAI Token Counteris free and open source software.IssuesIf you encounter any problems,
please [file an issue] along with a detailed description.