Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Python Client for Copilot (formerly named Bing Chat), also known as Sydney.

License

NotificationsYou must be signed in to change notification settings

vsakkas/sydney.py

Repository files navigation

Sydney.py

Latest ReleasePythonManagedMIT License

Python Client for Copilot (formerly named Bing Chat), also known as Sydney.

Note

This is anunofficial client.

Features

  • Connect to Copilot, Microsoft's AI-powered personal assistant.
  • Ask questions and have a conversation in various conversation styles.
  • Compose content in various formats and tones.
  • Stream response tokens for real-time communication.
  • Retrieve citations and suggested user responses.
  • Enhance your prompts with images for an enriched experience.
  • Customize your experience using any of the supported personas.
  • Use asyncio for efficient and non-blocking I/O operations.

Requirements

  • Python 3.9 or newer
  • Microsoft account with access toCopilot(optional)

Installation

To install Sydney.py, run the following command:

pip install sydney-py

or, if you usepoetry:

poetry add sydney-py

Tip

Make sure you're using the latest version of Sydney.py to ensure best compatibility with Copilot.

Usage

Prerequisites

To use Sydney.py, you first need to extract all the cookies from the Copilot web page. These cookies are used to authenticate your requests to the Copilot API.

To get the cookies, follow these steps on Microsoft Edge:

  • Go to theCopilot web page.
  • Open the developer tools in your browser (usually by pressingF12 or right-clicking on the chat dialog and selectingInspect).
  • Select theNetwork tab to view all requests sent to Copilot.
  • Write a message on the chat dialog that appears on the web page.
  • Find a request namedcreate?bundleVersion=XYZ and click on it.
  • Scroll down to the requests headers section and copy the entire value after theCookie: field.

Then, set it as an environment variable in your shell:

export BING_COOKIES=<your-cookies>

or, in your Python code:

os.environ["BING_COOKIES"]="<your-cookies>"

Tip

In some regions, using cookies is not required, in which case the above instructions can be skipped.

Tip

It is also possible to use theCookie-Editor extension, export the cookies inHeader String format and set them the same way.

Important

For regions where a cookie is required, it is recommended to manually write messages to Copilot until a box containing aVerifying message appears, which should then switch to aSuccess! message. Without this step, it is possible that Sydney.py will fail with aCaptchaChallenge error.

Important

Copilot has been redesigned with a new, non-compatible API. Sydney currently useshttps://edgeservices.bing.com/edgesvc/chat instead ofhttps://copilot.microsoft.com as a workaround.

Example

You can use Sydney.py to easily create a CLI client for Copilot:

importasynciofromsydneyimportSydneyClientasyncdefmain()->None:asyncwithSydneyClient()assydney:whileTrue:prompt=input("You: ")ifprompt=="!reset":awaitsydney.reset_conversation()continueelifprompt=="!exit":breakprint("Sydney: ",end="",flush=True)asyncforresponseinsydney.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 Copilot which starts a conversation:

sydney=SydneyClient()awaitsydney.start_conversation()# Conversationawaitsydney.close_conversation()

Alternatively, you can use theasync with statement to keep the code compact:

asyncwithSydneyClient()assydney:# Conversation

Conversation Style

You can set the conversation style when creating a Sydney Client:

sydney=SydneyClient(style="creative")

The available options arecreative,balanced andprecise.

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:

asyncwithSydneyClient()assydney:# Conversationawaitsydney.reset_conversation(style="creative")

Ask

You can ask Copilot questions and (optionally) include citations in the results:

asyncwithSydneyClient()assydney:response=awaitsydney.ask("When was Bing Chat released?",citations=True)print(response)

You can also stream the response tokens:

asyncwithSydneyClient()assydney:asyncforresponseinsydney.ask_stream("When was Bing Chat released?",citations=True):print(response,end="",flush=True)

Both versions of theask method support the same parameters.

Attachment

It is also possible to provide a URL to an image or a local image file path as an attachment, which will be used as input together with the prompt:

asyncwithSydneyClient()assydney:response=awaitsydney.ask("What does this picture show?",attachment="<image-url-or-path>")print(response)

Web Context

You can also provide the contents of a web page as additional context to be used along with the prompt:

asyncwithSydneyClient()assydney:response=awaitsydney.ask("Describe the webpage",context="<web-page-source>")print(response)

Web Search

It is possible to determine if Copilot can search the web for information to use in the results:

asyncwithSydneyClient()assydney:response=awaitsydney.ask("When was Bing Chat released?",search=False)print(response)

Searching the web is enabled by default.

Note

Web search cannot be disabled when the response is streamed.

Personas

It is possible to use specialized versions of Copilot, suitable for specific tasks or conversations:

asyncwithSydneyClient(persona="travel")assydney:response=awaitsydney.ask("Tourist attractions in Sydney")print(response)

The available options for thepersona parameter are:

  • copilot
  • travel
  • cooking
  • fitness

By default, Sydney.py will use thecopilot persona.

Compose

You can ask Copilot to compose different types of content, such emails, articles, ideas and more:

asyncwithSydneyClient()assydney:response=awaitsydney.compose("Why Python is a great language",format="ideas")print(response)

You can also stream the response tokens:

asyncwithSydneyClient()assydney:asyncforresponseinsydney.compose_stream("Why Python is a great language",format="ideas"):print(response,end="",flush=True)

The default available options for thetone parameter are:

  • professional
  • casual
  • enthusiastic
  • informational
  • funny

It is also possible to provide any other value for thetone parameter.

The available options for theformat parameter are:

  • paragraph
  • email
  • blogpost
  • ideas

The available options for thelength parameter are:

  • short
  • medium
  • long

Both versions of thecompose method support the same parameters.

Suggested Responses

You can also receive the suggested user responses as generated by Copilot along with the text answer. Bothask,ask_stream support this feature:

asyncwithSydneyClient()assydney:response,suggested_responses=awaitsydney.ask("When was Bing Chat released?",suggestions=True)ifsuggested_responses:print("Suggestions:")forsuggestioninsuggested_responses:print(suggestion)

And alsocompose andcompose_stream:

asyncwithSydneyClient()assydney:response,suggested_responses=awaitsydney.compose("Why Python is a great language",format="ideas",suggestions=True    )ifsuggested_responses:print("Suggestions:")forsuggestioninsuggested_responses:print(suggestion)

Note

The suggested user responses are returned only if the suggestions parameter is true. Otherwise, allask andcompose methods return only the response.

Note

When using theask_stream orcompose_stream method with the suggestions parameter, only the lastly returned suggested user responses may contain a value. For all previous iterations, the suggested user responses will beNone.

Compose using Suggestions

You can also improve or alter the results ofcompose by using either the suggested responses or any other prompt:

asyncwithSydneyClient()assydney:response,suggested_responses=awaitsydney.compose(prompt="Why Python is a great language",format="ideas",suggestions=True,    )response,suggested_responses=awaitsydney.compose(prompt=suggested_responses[0],format="ideas",suggestions=True    )print(response)

Raw Response

You can also receive the raw JSON response that comes from Copilot instead of a text answer. Bothask andcompose support this feature:

asyncwithSydneyClient()assydney:response=awaitsydney.ask("When was Bing Chat released?",raw=True)print(response)

Conversations

You can also receive all existing conversations that were made with the current client:

asyncwithSydneyClient()assydney:response=awaitsydney.get_conversations()print(response)

Exceptions

When something goes wrong, Sydney.py might throw one of the following exceptions:

ExceptionMeaningSolution
NoConnectionExceptionNo connection to Copilot was foundRetry
ConnectionTimeoutExceptionAttempt to connect to Copilot timed outRetry
NoResponseExceptionNo response was returned from CopilotRetry or use new cookie
ThrottledRequestExceptionRequest is throttledWait and retry
CaptchaChallengeExceptionCaptcha challenge must be solvedUse new cookie
ConversationLimitExceptionReached conversation limit of N messagesStart new conversation
CreateConversationExceptionFailed to create conversationRetry or use new cookie
GetConversationsExceptionFailed to get conversationsRetry

For more detailed documentation and options, please refer to the code docstrings.

License

This project is licensed under the MIT License - see theLICENSE file for details.


[8]ページ先頭

©2009-2025 Movatter.jp