Here it is, a much-needed update to this service which offers numerous functionalities that the old code was unable to deliver to us.
As you may know, ChatGPT Plus subscribers now have the opportunity to request image analysis directly from GPT within the chat bar. Bing has also integrated this feature into its chatbot. With this new code, you can now provide an image using a data URI, with all the following supported extensions: jpg, jpeg, png, and gif!
**What is a data URI and how can I provide an image to Bing?**
Just to clarify, a data URI is a method for encoding data directly into a URI (Uniform Resource Identifier). It is typically used for embedding small data objects like images, text, or other resources within web pages or documents. Data URIs are widely used in web applications.
To provide an image from your desktop and retrieve it as a data URI, you can use this code: [GitHub link](https://gist.github.com/jsocol/1089733).
Now, here is a code snippet you can use to provide images to Bing:
```python
import g4f
provider = g4f.Provider.Bing
user_message = [{"role": "user", "content": "Hi, describe this image."}]
response = g4f.ChatCompletion.create(
model = g4f.models.gpt_4,
provider = g4f.provider, # Corrected the provider value
messages = user_message,
stream = True,
image = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/4RiSRXhpZgAASUkqAAg..." # Insert your full data URI image here
)
for message in response:
print(message, flush=True, end='')
```
If you don't want to analyze the image, just do not specify the image parameter.
Regarding the implementation, the image is preprocessed within the Bing.py code, which can be resource-intensive for a server-side implementation. When using the Bing chatbot in your web browser, the image is preprocessed on your computer before being sent to the server. This preprocessing includes tasks like image rotation and compression. Although this implementation works, it would be more efficient to delegate image preprocessing to the client as it happens in reality. I will try to provide a JavaScript code for that at a later time.
As you saw, I did mention in the title that it is in Beta. The way the code is written, Bing can sometimes mess up its answers. Indeed, Bing does not really stream its responses as the other providers do. Bing sends its answers like this on each iteration:
"Hi,"
"Hi, this,"
"Hi, this is,"
"Hi, this is Bing."
Instead of sending each segment one at a time, it already adds them on each iteration. So, to simulate a normal streaming response, other contributors made the code wait for the next iteration to retrieve the newer segments and yield them. However, this method ignores something that Bing does.
Bing processes its responses in a markdown detector, which searches for links while the AI answers. If it finds a link, it saves it and waits until the AI finishes its answer to put all the found links at the very end of the answer. So if the AI is writing a link, but then on the next iteration, it finishes writing this link, it will then be deleted from the answer and appear later at the very end. Example:
"Here is your link reference ["
"Here is your link reference [^"
"Here is your link reference [^1"
"Here is your link reference [^1^"
And then the response would get stuck there because the markdown detector would have deleted this link reference in the next response and waited until the AI is finished to put it at the very end.
For this reason, I am working on an update to anticipate the markdown detector.
So please, if you guys notice any bugs with this new implementation, I would greatly appreciate it if you could report them on the issue tab of this repo. Thanks in advance, and I hope that all these explanations were clear to you!
A better approach is to use the `.join()` method of strings, which reduces string concatenation operations and improves performance.
Additionally, using formatted strings (f-strings) makes the code cleaner and more readable.
Add format_prompt helper
Fix create_completion in AsyncGeneratorProvider
Move get_cookies from constructor to function
Add ow HuggingChat implement
Remove need auth form Liabots
Add staic cache for access_token in OpenaiChat
Add OpenAssistant provider
Support stream and async in You
Support async and add userId in Yqcloud
Add log_time module