News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

使用 Python 流式传输来自 OpenAI API 的响应:分步指南

posted on 2023-06-03 20:53     read(575)     comment(0)     like(4)     collect(5)


OpenAI API provides a large number of cutting-edge AI models that can be used to perform various NLP tasks. However, in some cases, making an API request to OpenAI may not be enough, such as when real-time updates are required. This is where server-sent events (SSE) come into play.

SSE is a simple and effective technique for streaming data from a server to a client in real time. How to Backup Drivers in a Windows Computer In this article, we'll explore how to use Python and SSE to stream responses from the OpenAI API in real time. By the end of this tutorial, you'll have a solid understanding of how to implement this technique and be able to easily stream responses from the OpenAI API to your application.

Retrieve your OpenAI API key

In order to be able to access the OpenAI API from your Python scripts, we need an API key.

To retrieve your OpenAI API key, you need to create a user account at https://openai.com/ and visit the API keys section in the OpenAI dashboard to create a new API key.

This key is secret and must not be shared with anyone else. We'll need this key later when implementing a Python script to access OpenAI's API.

execute script

Create a file main.py in a new folder and start the implementation by inserting the following four lines of code:

  1. import requests
  2. import json
  3. import sseclient
  4. API_KEY = '[INSERT YOUR OPENAI API KEY HERE]'

This code imports three modules in Python: requests, json, and sseclient.

requests is a popular Python library for sending HTTP requests to servers and receiving responses.

json is a library that provides functions for manipulating JSON data. It allows you to encode and decode JSON data. sseclient is a library for handling Server-Sent Events (SSE) -- an event-driven communication between a client and a server over HTTP.

If you have not previously installed these libraries in your Python development environment, you first need to install these packages using the pip command:

pip install requests json sseclient-py

The code also defines a variable API_KEY to store the OpenAI API key. Please replace the placeholder text [INSERT YOUR OPENAI API KEY HERE] with the API key you retrieved earlier from your OpenAI account.

Next complete the implementation by implementing and executing the performRequestWithStreaming() function, which contains all the logic needed to perform a POST request to the OpenAI API completion endpoint and receive the answer in streaming mode:

  1. import requests
  2. import json
  3. import sseclient
  4. API_KEY = '[INSERT YOUR OPENAI API KEY HERE]'
  5. def performRequestWithStreaming():
  6. reqUrl = 'https://api.openai.com/v1/completions'
  7. reqHeaders = {
  8. 'Accept': 'text/event-stream',
  9. 'Authorization': 'Bearer ' + API_KEY
  10. }
  11. reqBody = {
  12. "model": "text-davinci-003",
  13. "prompt": "What is Python?",
  14. "max_tokens": 100,
  15. "temperature": 0,
  16. "stream": True,
  17. }
  18. request = requests.post(reqUrl, stream=True, headers=reqHeaders, json=reqBody)
  19. client = sseclient.SSEClient(request)
  20. for event in client.events():
  21. if event.data != '[DONE]':
  22. print(json.loads(event.data)['choices'][0]['text'], end="", flush=True),
  23. if __name__ == '__main__':
  24. performRequestWithStreaming()

This function makes a POST request to the URL https://api.openai.com/v1/completions using the request module . The request headers include Accept and Authorization fields, and the authorization header uses the API_KEY variable defined earlier in the code.

The request body is defined as the reqBody variable and contains several parameters of the API request, including the model name, prompt text, maximum number of tokens to return, temperature, and requirements for streaming responses.

Responses from API requests are assigned to request variables. Then, use the sseclient library to process the response and pass the request object to the SSEClient constructor to create a new client object.

The code then iterates through the events in the client object using a for loop. For each event, the event data is printed to the console unless the event data is equal to "[DONE]", in which case nothing is printed. The json.loads() function is used to parse the JSON data in the event, extract the choices field of the result dictionary, and obtain the completed text. The end and flush parameters of the print() function are used to ensure that the output is not buffered and displayed immediately on the console.

execute script

execute script

$ python main.py

You should then be able to see the answer provided by OpenAI (prompt "What is Python?") streamed to the command line.

in conclusion

In summary, using SSE and Python to stream responses from OpenAI APIs in real time is a powerful and efficient way to access AI models in applications. Following the step-by-step guide in this article, you should now be able to implement this technique and receive real-time updates from the OpenAI API. SSE is a simple and effective solution for streaming data from the server to the client, and it is a great choice for those looking to take the AI ​​capabilities of their applications to the next level .



Category of website: technical article > Blog

Author:Disheartened

link:http://www.pythonblackhole.com/blog/article/78487/c6ed929110bf69f13d7f/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

4 0
collect article
collected

Comment content: (supports up to 255 characters)