Python Client for Bing Chat, also known as Sydney.
Project description
Sydney.py
Python Client for Bing Chat, also known as Sydney.
Note This is an unofficial client.
Requirements
- Python 3.10 or newer
- Microsoft account with access to Bing Chat
Installation
To install Sydney.py, run the following command:
pip install sydney-py
or, if you use poetry:
poetry add sydney-py
Usage
Prerequisites
To use Sydney.py you first need to extract the _U
cookie from Bing.
Then, set it as an environment variable in your shell:
export BING_U_COOKIE=<your-cookie>
or, in your Python code:
os.environ["BING_U_COOKIE"] = "<your-cookie>"
Example
You can Sydney.py to easily create a CLI client for Bing Chat:
import asyncio
from sydney import SydneyClient
async def main() -> None:
async with SydneyClient() as sydney:
while True:
prompt = input("You: ")
if prompt == "!reset":
await sydney.reset_conversation()
continue
elif prompt == "!exit":
break
print("Sydney: ", end="", flush=True)
async for response in sydney.ask_stream(prompt):
print(response, end="", flush=True)
print("\n")
if __name__ == "__main__":
asyncio.run(main())
Sydney Client
You can create a Sydney Client and initialize a connection with Bing Chat which starts a conversation:
sydney = SydneyClient()
await sydney.start_conversation()
# Conversation
await sydney.end_conversation()
Alternatively, you can use the async with
statement to keep the code compact:
async with SydneyClient() as sydney:
# Conversation
Conversation Style
You can set the conversation style when creating a Sydney Client:
sydney = SydneyClient(style="creative")
Reset Conversation
You can reset the conversation in order to make the client forget the previous conversation. You can also change the conversation style without creating a new client:
async withSydneyClient() as sydney:
# Conversation
await sydney.reset_conversation(style="creative")
Ask
You can ask Bing Chat questions and (optionally) include citations in the results:
async with SydneyClient() as sydney:
response = sydney.ask("When was Bing Chat released?", citations=True)
print(response)
You can also stream the response tokens:
async with SydneyClient() as sydney:
async for response in sydney.ask_stream("When was Bing Chat released?", citations=True):
print(response, end="", flush=True)
Both versions of the ask
method support the same parameters.
Compose
You can ask Bing Chat to compose different types of content, such emails, articles, ideas and more:
async with SydneyClient() as sydney:
response = sydney.compose("Why Python is a great language", format="ideas")
print(response)
You can also stream the response tokens:
async with SydneyClient() as sydney:
async for response in sydney.compose_stream("Why Python is a great language", format="ideas"):
print(response, end="", flush=True)
Both versions of the compose
method support parameters for setting the tone, format and length of the composed text.
Raw Response
You can also receive the raw JSON response that comes from Bing Chat instead of a text answer. Both ask
and compose
support this feature:
async with SydneyClient() as sydney:
response = sydney.ask("When was Bing Chat released?", raw=True)
print(response)
For more detailed documentation and options, please refer to the code docstrings.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for sydney_py-0.10.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e5d2cfe7ba0011a90a5dfcaf3e1d6d469ac3430698b68bb71e2b2e58d1f1384 |
|
MD5 | d26884e3c91cba97e60f427c7a542b59 |
|
BLAKE2b-256 | c0179a9239f459f6fe723b7ae8f7ec20cafabbde25018d8716fe7f08648106f4 |