from flask import Response import requests import json import os # Make sure to set the OPENAI_API_KEY environment variable in a .env file (create if does not exist) - see .env.example OPENAI_API_KEY="xxxxxxxxxxxxx" url = "https://lqfjun-c2.hf.space/v1/chat/completions" class OpenAI: @staticmethod def create_chat_body(body, stream=False): # Text messages are stored inside request body using the Deep Chat JSON format: # https://deepchat.dev/docs/connect body['messages']=[ { "role": "assistant" if message["role"] == "ai" else message["role"], "content": message["text"] } for message in body["messages"] ] if stream: body["stream"] = True return body def chat(self, body): headers = { "Content-Type": "application/json", "Authorization": "Bearer " + "xxxx" } chat_body = self.create_chat_body(body) response = requests.post(url, json=chat_body, headers=headers) json_response = response.json() if "error" in json_response: raise Exception(json_response["error"]["message"]) result = json_response["choices"][0]["message"]["content"] # Sends response back to Deep Chat using the Response format: # https://deepchat.dev/docs/connect/#Response return {"text": result} def chat_stream(self, body): headers = { "Content-Type": "application/json", "Authorization": "Bearer " + "xxxx" } chat_body = self.create_chat_body(body, stream=True) response = requests.post(url, json=chat_body, headers=headers, stream=True) def generate(): # increase chunk size if getting errors for long messages for chunk in response.iter_content(chunk_size=2048): if chunk: if not(chunk.decode().strip().startswith("data")): errorMessage = json.loads(chunk.decode())["error"]["message"] print("Error in the retrieved stream chunk:", errorMessage) # this exception is not caught, however it signals to the user that there was an error raise Exception(errorMessage) lines = chunk.decode().split("\n") filtered_lines = list( filter(lambda line: line.strip(), lines)) for line in filtered_lines: data = line.replace("data:", "").replace( "[DONE]", "").replace("data: [DONE]", "").strip() if data: try: result = json.loads(data) content = result["choices"][0].get("delta", {}).get("content", "") # Sends response back to Deep Chat using the Response format: # https://deepchat.dev/docs/connect/#Response yield "data: {}\n\n".format(json.dumps({"text": content})) except json.JSONDecodeError: # Incomplete JSON string, continue accumulating lines pass return Response(generate(), mimetype="text/event-stream") # By default - the OpenAI API will accept 1024x1024 png images, however other dimensions/formats can sometimes work by default # You can use an example image here: https://github.com/OvidijusParsiunas/deep-chat/blob/main/example-servers/ui/assets/example-image.png def image_variation(self, files): url = "https://api.openai.com/v1/images/variations" headers = { "Authorization": "Bearer " + os.getenv("OPENAI_API_KEY") } # Files are stored inside a files object # https://deepchat.dev/docs/connect image_file = files[0] form = { "image": (image_file.filename, image_file.read(), image_file.mimetype) } response = requests.post(url, files=form, headers=headers) json_response = response.json() if "error" in json_response: raise Exception(json_response["error"]["message"]) # Sends response back to Deep Chat using the Response format: # https://deepchat.dev/docs/connect/#Response return {"files": [{"type": "image", "src": json_response["data"][0]["url"]}]}