{"id":134893,"date":"2023-09-21T14:54:48","date_gmt":"2023-09-21T14:54:48","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=1651685"},"modified":"2023-09-21T14:54:48","modified_gmt":"2023-09-21T14:54:48","slug":"can-i-run-openais-api-in-parallel-yes-with-python-async","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2023\/09\/21\/can-i-run-openais-api-in-parallel-yes-with-python-async\/","title":{"rendered":"Can I Run OpenAI\u2019s API in Parallel? Yes, with Python Async!"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1651685&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Can I Run OpenAI&#039;s API in Parallel? Yes, with Python Async!&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/p><\/div>\n<p>If you&#8217;re like me, you&#8217;re using OpenAI API a lot in your Python code. So the natural question arises: <em>&#8220;How to use OpenAI&#8217;s API asynchronously by issuing multiple requests at once?&#8221;<\/em><\/p>\n<p>I will give you my code for asynchronous <a href=\"https:\/\/blog.finxter.com\/openapi-cheat-sheet\/\">OpenAI API<\/a> requests for copy and paste below. But first, allow me to give you a word of warning from coder to coder:<\/p>\n<p>Generally speaking, coders who use asynchronous code do it just because they want to, not because it is needed. Asynchronous code is hard to read, error-prone, inefficient due to context switches, and unpredictable.<\/p>\n<p>Specifically, when using asynchronous requests against the OpenAI API, you should be aware of the <a href=\"https:\/\/blog.finxter.com\/fix-rate-limit-error-in-openai-codex\/\">rate limits<\/a> that may become the bottleneck of your asynchronous Python app:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" fetchpriority=\"high\" width=\"1024\" height=\"1002\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-103-1024x1002.png\" alt=\"\" class=\"wp-image-1651693\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-103-1024x1002.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-103-300x293.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-103-768x751.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-103.png 1241w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\"><a href=\"https:\/\/platform.openai.com\/account\/rate-limits\">https:\/\/platform.openai.com\/account\/rate-limits<\/a><\/figcaption><\/figure>\n<\/div>\n<p>Okay, enough of the warnings. Let&#8217;s do it: <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f447.png\" alt=\"\ud83d\udc47\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2 class=\"wp-block-heading\">Method 1: Using OpenAI API Calls Asynchronously<\/h2>\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"742\" height=\"595\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/async_2222-1.gif\" alt=\"\" class=\"wp-image-1651692\"\/><\/figure>\n<p>I have developed the following code to issue OpenAI requests asynchronously in Python &#8212; make sure to replace the highlighted lines with your OpenAI key and your desired prompts:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"6,9-11\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import aiohttp\nimport asyncio\nimport openai # Set up your OpenAI API key\nopenai.api_key = 'sk-...' # Example prompts\nprompts = [\"What is the capital of France?\", \"How does photosynthesis work?\", \"Who wrote 'Pride and Prejudice'?\"] async def async_openai_request(prompt): url = \"https:\/\/api.openai.com\/v1\/chat\/completions\" headers = { \"Authorization\": f\"Bearer {openai.api_key}\", \"Content-Type\": \"application\/json\" } data = { \"model\": \"gpt-4\", \"messages\": [ { \"role\": \"user\", \"content\": prompt } ], \"temperature\": 1, \"max_tokens\": 150, \"top_p\": 1, \"frequency_penalty\": 0, \"presence_penalty\": 0 } async with aiohttp.ClientSession() as session: async with session.post(url, json=data, headers=headers) as response: return await response.json() async def main(): # Gather results from all asynchronous tasks results = await asyncio.gather(*(async_openai_request(prompt) for prompt in prompts)) for prompt, result in zip(prompts, results): print(f\"Prompt: {prompt}\") print(f\"Response: {result['choices'][0]['message']['content']}\\n\") # Run the main function\nasyncio.run(main())\n<\/pre>\n<p>I&#8217;ll give you the output at the end of this article. But first let&#8217;s go through the code step by step to ensure you understand everything.<\/p>\n<p>Note that if you need a refresher on the Python OpenAI API, feel free to check out this Finxter Academy course:<\/p>\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/academy.finxter.com\/university\/prompt-engineering-with-python-and-openai\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"915\" height=\"244\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-104.png\" alt=\"\" class=\"wp-image-1651703\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-104.png 915w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-104-300x80.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-104-768x205.png 768w\" sizes=\"auto, (max-width: 915px) 100vw, 915px\" \/><\/a><\/figure>\n<h3 class=\"wp-block-heading\">Step 1: Imports<\/h3>\n<p>The code begins by importing three essential libraries. <\/p>\n<p><code>aiohttp<\/code> is used for making <a href=\"https:\/\/blog.finxter.com\/python-async-requests-getting-urls-concurrently-via-https\/\">asynchronous HTTP requests<\/a>, allowing the program to send and receive data from the OpenAI API without blocking the main thread. <\/p>\n<p><code>asyncio<\/code> provides the tools to write concurrent code using the <a href=\"https:\/\/blog.finxter.com\/python-async-await-mastering-concurrent-programming\/\">async\/await<\/a> syntax to handle multiple tasks simultaneously. <\/p>\n<p>Lastly, the <code><a href=\"https:\/\/blog.finxter.com\/top-5-llm-python-libraries\/\">openai<\/a><\/code> library is the official OpenAI API client, facilitating interactions with the OpenAI platform.<\/p>\n<h3 class=\"wp-block-heading\">Step 2: Set up the OpenAI API Key<\/h3>\n<p>Following the imports, the OpenAI API key is set up. It&#8217;s important to note that hard-coding API keys directly in the code is not a recommended practice. For security reasons, it&#8217;s better to use environment variables or configuration files to store such sensitive information.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/blog.finxter.com\/openai-python-api-a-helpful-illustrated-guide-in-5-steps\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"573\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-113-1-1024x573.png\" alt=\"\" class=\"wp-image-1651687\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-113-1-1024x573.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-113-1-300x168.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-113-1-768x430.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-113-1.png 1286w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n<p class=\"has-base-2-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f517.png\" alt=\"\ud83d\udd17\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Recommended<\/strong>: <a href=\"https:\/\/blog.finxter.com\/openai-python-api-a-helpful-illustrated-guide-in-5-steps\/\">OpenAI Python API \u2013 A Helpful Illustrated Guide in 5 Steps<\/a><\/p>\n<h3 class=\"wp-block-heading\">Step 3: Asynchronous Function for API Requests<\/h3>\n<p>The <code>async_openai_request<\/code> function is defined to handle asynchronous requests to the OpenAI API. <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">async def async_openai_request(prompt): url = \"https:\/\/api.openai.com\/v1\/chat\/completions\" headers = { \"Authorization\": f\"Bearer {openai.api_key}\", \"Content-Type\": \"application\/json\" } data = { \"model\": \"gpt-4\", \"messages\": [ { \"role\": \"user\", \"content\": prompt } ], \"temperature\": 1, \"max_tokens\": 150, \"top_p\": 1, \"frequency_penalty\": 0, \"presence_penalty\": 0 } async with aiohttp.ClientSession() as session: async with session.post(url, json=data, headers=headers) as response: return await response.json()<\/pre>\n<p>When this function is called with a specific <code>prompt<\/code>, it prepares and sends an asynchronous request to the OpenAI API&#8217;s chat completions endpoint. <\/p>\n<p>The headers for the request include the authorization, which uses the API key, and the content type. The payload (<code>data<\/code>) sent to the API specifies several parameters, including the model to use (<code><a href=\"https:\/\/blog.finxter.com\/gpt-4-is-out-a-new-language-model-on-steroids\/\">gpt-4<\/a><\/code>), the message format containing the user&#8217;s prompt, and other parameters like <code>temperature<\/code>, <code>max_tokens<\/code>, <code>top_p<\/code>, <code>frequency_penalty<\/code>, and <code>presence_penalty<\/code> that influence the output.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"634\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-100-1024x634.png\" alt=\"\" class=\"wp-image-1651688\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-100-1024x634.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-100-300x186.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-100-768x476.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-100.png 1513w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p>The function then establishes an asynchronous session using <code>aiohttp<\/code> and sends a POST request with the specified data and headers. Once the response is received, it&#8217;s returned in JSON format.<\/p>\n<h3 class=\"wp-block-heading\">Step 4: Main Asynchronous Function<\/h3>\n<p>The <code>main<\/code> function encapsulates the primary logic of the program. It starts by defining a <a href=\"https:\/\/blog.finxter.com\/python-lists\/\">list<\/a> of example prompts.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\nasync def main(): # Gather results from all asynchronous tasks results = await asyncio.gather(*(async_openai_request(prompt) for prompt in prompts)) for prompt, result in zip(prompts, results): print(f\"Prompt: {prompt}\") print(f\"Response: {result['choices'][0]['message']['content']}\\n\")<\/pre>\n<p>For each of these prompts, <a href=\"https:\/\/blog.finxter.com\/python-async-requests-getting-urls-concurrently-via-https\/\">asynchronous requests<\/a> are sent to the OpenAI API using the previously defined <code>async_openai_request<\/code> function. The <code>asyncio.gather<\/code> method is employed to concurrently collect results from all the asynchronous tasks. Once all responses are received, the function iterates over the prompts and their corresponding results, printing them out for the user.<\/p>\n<h3 class=\"wp-block-heading\">Step 5: Execution<\/h3>\n<p>Finally, the <code>asyncio.run(main())<\/code> command is used to execute the <code>main<\/code> function. When the code is run, it will send asynchronous requests for each of the example prompts and display the responses in the console.<\/p>\n<p>The output is:<\/p>\n<pre class=\"wp-block-preformatted\"><code>Prompt: What is the capital of France?\nResponse: The capital of France is Paris. Prompt: How does photosynthesis work?\nResponse: Photosynthesis is a process used by plants, algae and certain bacteria to convert sunlight, water and carbon dioxide into food and oxygen. This process happens inside the chloroplasts, specifically using chlorophyll, the green pigment involved in photosynthesis. Photosynthesis occurs in two stages: the light-dependent reactions and the light-independent reactions, also known as the Calvin Cycle. In the light-dependent reactions, which take place in the thylakoid membrane of the chloroplasts, light energy is converted into chemical energy. When light is absorbed by chlorophyll, it excites the electrons, increasing their energy level and triggering a series of chemical reactions. Water molecules are split to produce oxygen, electrons, and hydrogen ions. The oxygen is released into the Prompt: Who wrote 'Pride and Prejudice'?\nResponse: 'Pride and Prejudice' was written by Jane Austen.<\/code><\/pre>\n<h2 class=\"wp-block-heading\">Method 2: Using OpenAI ChatCompletion&#8217;s acreate()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">An alternative to the above method of using asynchronous requests against the OpenAI API endpoint is to use OpenAI&#8217;s native asynchronous methods, as noted in the <a href=\"https:\/\/github.com\/openai\/openai-python#async-api\">docs<\/a>: <em>&#8220;Async support is available in the API by prepending <code>a<\/code> to a network-bound method&#8221;<\/em>.<\/p>\n<p>In the following code, I have only changed the OpenAI API calling function to use the <code>acreate()<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"9-11\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import aiohttp\nimport asyncio\nimport openai # Set up your OpenAI API key\nopenai.api_key = 'sk-...' async def create_chat_completion(prompt): chat_completion_resp = await openai.ChatCompletion.acreate(model=\"gpt-4\", messages=[{\"role\": \"user\", \"content\": prompt}]) return chat_completion_resp async def main(): # Example prompts prompts = [\"What is the capital of France?\", \"How does photosynthesis work?\", \"Who wrote 'Pride and Prejudice'?\"] # Gather results from all asynchronous tasks results = await asyncio.gather(*(create_chat_completion(prompt) for prompt in prompts)) for prompt, result in zip(prompts, results): print(f\"Prompt: {prompt}\") print(f\"Response: {result['choices'][0]['message']['content']}\\n\") # Run the main function\nasyncio.run(main())\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"wp-block-preformatted\"><code>Prompt: What is the capital of France?\nResponse: The capital of France is Paris. Prompt: How does photosynthesis work?\nResponse: Photosynthesis is the process by which green plants, algae and some bacteria convert light energy, usually from the sun, into chemical energy in the form of glucose (sugar). This process is essential for life on earth as it is the primary source of all oxygen in the atmosphere. This process takes place in a part of the plant cell called the chloroplast, more specifically, within the chlorophyll molecules, which absorbs sunlight (specifically, photons) and gives plants their green color. It can be divided into two main stages: the light-dependent reactions and the light-independent reactions or Calvin Cycle. In the light-dependent reactions, which take place in the thylakoid membrane of the chloroplasts, light is absorbed by the chlorophyll and converted into chemical energy - in the form of ATP (Adenosine triphosphate) and NADPH (Nicotinamide adenine dinucleotide phosphate). This process also splits water molecules (H2O) into oxygen (O2), which is released into the atmosphere, and hydrogen ions (H+), which are used in the next stage of photosynthesis. In the second stages, the light-independent reactions or Calvin Cycle, which take place in the stroma of the chloroplasts, the ATP and NADPH produced in the light-dependent reactions, along with carbon dioxide (CO2) from the atmosphere, are used to produce glucose (sugar), which is used as an energy source for plant growth and development. In summary, during photosynthesis, light energy is converted into chemical energy, which fuels the organisms' activities, and oxygen is released into the atmosphere as a byproduct. Prompt: Who wrote 'Pride and Prejudice'?\nResponse: 'Pride and Prejudice' was written by Jane Austen.<\/code><\/pre>\n<p>Make sure to check out our course on prompt engineering with Llama 2 in case you&#8217;re looking to leverage a free and open-source large language model (LLM) instead of the paid OpenAI API:<\/p>\n<h2 class=\"wp-block-heading\">Prompt Engineering with Llama 2<\/h2>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> The\u00a0<strong><a href=\"https:\/\/academy.finxter.com\/university\/prompt-engineering-with-llama-2\/\">Llama 2 Prompt Engineering course<\/a><\/strong> helps you stay on the right side of change.\u00a0Our course is meticulously designed to provide you with <em>hands-on experience through genuine projects<\/em>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/academy.finxter.com\/university\/prompt-engineering-with-llama-2\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" loading=\"lazy\" width=\"919\" height=\"261\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-101.png\" alt=\"\" class=\"wp-image-1651689\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-101.png 919w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-101-300x85.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-101-768x218.png 768w\" sizes=\"auto, (max-width: 919px) 100vw, 919px\" \/><\/a><\/figure>\n<\/div>\n<p>You&#8217;ll delve into practical applications such as book PDF querying, payroll auditing, and hotel review analytics. These aren&#8217;t just theoretical exercises; they&#8217;re real-world challenges that businesses face daily.<\/p>\n<p>By studying these projects, you&#8217;ll gain a deeper comprehension of how to harness the power of Llama 2 using <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f40d.png\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> Python, <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f517.png\" alt=\"\ud83d\udd17\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f99c.png\" alt=\"\ud83e\udd9c\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> Langchain, <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f332.png\" alt=\"\ud83c\udf32\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> Pinecone, and a whole stack of highly <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2692.png\" alt=\"\u2692\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f6e0.png\" alt=\"\ud83d\udee0\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> practical tools of exponential coders in a post-ChatGPT world.<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/blog.finxter.com\/how-to-request-openais-api-asynchronously-in-python\/\">Can I Run OpenAI&#8217;s API in Parallel? Yes, with Python Async!<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/blog.finxter.com\">Be on the Right Side of Change<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) If you&#8217;re like me, you&#8217;re using OpenAI API a lot in your Python code. So the natural question arises: &#8220;How to use OpenAI&#8217;s API asynchronously by issuing multiple requests at once?&#8221; I will give you my code for asynchronous OpenAI API requests for copy and paste below. But first, allow me [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-134893","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/134893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=134893"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/134893\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=134893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=134893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=134893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}