11import asyncio
22import json
3- import os
43import ssl
54import sys
65from time import time
2019from .conversation_style import CONVERSATION_STYLE_TYPE
2120from .proxy import get_proxy
2221from .request import ChatHubRequest
22+ from .upload_image import upload_image , upload_image_url
2323from .utilities import append_identifier
2424from .utilities import get_ran_hex
2525from .utilities import guess_locale
@@ -50,6 +50,7 @@ def __init__(
5050 client_id = conversation .struct ["clientId" ],
5151 conversation_id = conversation .struct ["conversationId" ],
5252 )
53+ self .conversation_id = conversation .struct ["conversationId" ] or self .request .conversation_id
5354 self .cookies = cookies
5455 self .proxy : str = get_proxy (proxy )
5556 self .session = httpx .AsyncClient (
@@ -68,7 +69,7 @@ async def get_conversation(
6869 conversation_signature : str = None ,
6970 client_id : str = None ,
7071 ) -> dict :
71- conversation_id = conversation_id or self .request .conversation_id
72+ self . conversation_id = conversation_id or self .request .conversation_id
7273 conversation_signature = (
7374 conversation_signature or self .request .conversation_signature
7475 )
@@ -101,6 +102,8 @@ async def ask_stream(
101102 webpage_context : Union [str , None ] = None ,
102103 search_result : bool = False ,
103104 locale : str = guess_locale (),
105+ # Use for attachment
106+ attachment : dict = None ,
104107 ) -> Generator [bool , Union [dict , str ], None ]:
105108 """ """
106109 if self .encrypted_conversation_signature is not None :
@@ -119,13 +122,23 @@ async def ask_stream(
119122 proxy = self .proxy ,
120123 )
121124 await _initial_handshake (wss )
125+ # Image
126+ image_url = None
127+ if attachment is not None :
128+ if attachment .get ("image_url" ) is not None :
129+ response = await upload_image_url (** attachment , conversation_id = self .conversation_id )
130+ else :
131+ response = await upload_image (** attachment )
132+ if response :
133+ image_url = f"https://www.bing.com/images/blob?bcid={ response } "
122134 # Construct a ChatHub request
123135 self .request .update (
124136 prompt = prompt ,
125137 conversation_style = conversation_style ,
126138 webpage_context = webpage_context ,
127139 search_result = search_result ,
128140 locale = locale ,
141+ image_url = image_url ,
129142 )
130143 # Send request
131144 await wss .send_str (append_identifier (self .request .struct ))
0 commit comments