Download openai_utils.py from tris4381/DemoChat: direct link, hf CLI and curl.
- Browser
- Download file 708 Bytes
-
https://huggingface.co/spaces/tris4381/DemoChat/resolve/main/openai_utils.py
- Command line
-
hf download hf://spaces/tris4381/DemoChat/openai_utils.py
-
curl -L -o openai_utils.py https://huggingface.co/spaces/tris4381/DemoChat/resolve/main/openai_utils.py
708 Bytes
| import openai | |
| from tenacity import retry, wait_random_exponential, stop_after_attempt | |
| import os | |
| GPT_MODEL_CHAT = "gpt-3.5-turbo" | |
| openai.api_key = os.environ['OPENAI_API_KEY'] | |
| def completion_with_backoff(**kwargs): | |
| return openai.ChatCompletion.create(**kwargs) | |
| def chat_completion_request(messages, model=GPT_MODEL_CHAT): | |
| try: | |
| completion = completion_with_backoff( | |
| model=model, | |
| messages=messages, | |
| max_tokens = 20 | |
| ) | |
| return completion | |
| except Exception as e: | |
| print("Unable to generate ChatCompletion response") | |
| print(f"Exception: {e}") | |
| return e |