Skip to content

Commit

Permalink
Refactor attachment upload code (#73)
Browse files Browse the repository at this point in the history
- Move code that builds attachment upload argument into separate method
  • Loading branch information
vsakkas authored Oct 17, 2023
1 parent e87ae83 commit 2115aac
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "MIT"
Expand Down
24 changes: 20 additions & 4 deletions sydney/sydney.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down

0 comments on commit 2115aac

Please sign in to comment.