From 2115aac615035f73f055c7841d321ec3a4b3f167 Mon Sep 17 00:00:00 2001 From: vsakkas Date: Tue, 17 Oct 2023 10:13:57 +0300 Subject: [PATCH] Refactor attachment upload code (#73) - Move code that builds attachment upload argument into separate method --- README.md | 2 +- pyproject.toml | 2 +- sydney/sydney.py | 24 ++++++++++++++++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 84d04d5..f6dda4b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Sydney.py -[![Latest Release](https://img.shields.io/github/v/release/vsakkas/sydney.py.svg)](https://github.com/vsakkas/sydney.py/releases/tag/v0.16.0) +[![Latest Release](https://img.shields.io/github/v/release/vsakkas/sydney.py.svg)](https://github.com/vsakkas/sydney.py/releases/tag/v0.16.1) [![Python](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/) [![MIT License](https://img.shields.io/badge/license-MIT-blue)](https://github.com/vsakkas/sydney.py/blob/master/LICENSE) diff --git a/pyproject.toml b/pyproject.toml index a369925..1986a3e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "sydney-py" -version = "0.16.0" +version = "0.16.1" description = "Python Client for Bing Chat, also known as Sydney." authors = ["vsakkas "] license = "MIT" diff --git a/sydney/sydney.py b/sydney/sydney.py index dd07ab8..2b7b6c7 100644 --- a/sydney/sydney.py +++ b/sydney/sydney.py @@ -207,6 +207,25 @@ def _build_compose_arguments( "type": 4, } + def _build_upload_arguments(self, attachment: str) -> str: + payload = { + "imageInfo": {"url": attachment}, + "knowledgeRequest": { + "invokedSkills": ["ImageById"], + "subscriptionId": "Bing.Chat.Multimodal", + "invokedSkillsRequestData": {"enableFaceBlur": True}, + "convoData": { + "convoid": self.conversation_id, + "convotone": str(self.conversation_style), + }, + }, + } + + return ( + f'--\r\nContent-Disposition: form-data; name="knowledgeRequest"\r\n\r\n' + f"{json.dumps(payload)}\r\n--\r\n" + ) + async def _upload_attachment(self, attachment: str) -> dict: """ Upload an image to Bing Chat. @@ -233,10 +252,7 @@ async def _upload_attachment(self, attachment: str) -> dict: else None, # Resolve HTTPS issue when proxy support is enabled. ) - data = ( - '--\r\nContent-Disposition: form-data; name="knowledgeRequest"\r\n\r\n{"imageInfo":{"url":"%s"},"knowledgeRequest":{"invokedSkills":["ImageById"],"subscriptionId":"Bing.Chat.Multimodal","invokedSkillsRequestData":{"enableFaceBlur":true},"convoData":{"convoid":"%s","convotone":"%s"}}}\r\n--\r\n' - % (attachment, self.conversation_id, str(self.conversation_style)) - ) + data = self._build_upload_arguments(attachment) async with session.post(BING_KBLOB_URL, data=data) as response: if response.status != 200: