Hansimov commited on
Commit
043f00b
1 Parent(s): 8cd1822

:pencil: [Doc] Readme: Thanks, Features, Run API service, API usage

Browse files
Files changed (1) hide show
  1. README.md +129 -15
README.md CHANGED
@@ -7,36 +7,63 @@ sdk: docker
7
  app_port: 22222
8
  ---
9
 
10
- ## Bing-Chat-API
11
 
12
- A successor to [EdgeGPT](https://github.com/acheong08/EdgeGPT) by [acheong08](https://github.com/acheong08).
13
 
14
- **Note: This project is in rapid progress, and currently is not ready to be used in production.**
 
 
 
15
 
16
- After completing some key features, I would focus on the quick deployment of this project.
17
 
18
- ## Install dependencies
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  ```bash
21
  # pipreqs . --force --mode no-pin
22
  pip install -r requirements.txt
23
  ```
24
 
25
- ## Docker Build
26
 
27
  ```bash
28
- sudo docker build -t bing-chat-api:1.0 . --build-arg http_proxy=$http_proxy --build-arg https_proxy=$https_proxy
29
  ```
30
 
31
- ## Run
32
 
33
- Command Line:
34
 
35
  ```bash
36
- python -m apis.chat_api
37
  ```
38
 
39
- Docker run:
40
 
41
  ```bash
42
  # no proxy
@@ -46,8 +73,95 @@ sudo docker run -p 22222:22222 bing-chat-api:1.0
46
  sudo docker run -p 22222:22222 --env http_proxy="http://<server>:<port>" bing-chat-api:1.0
47
  ```
48
 
49
- ## Example
50
-
51
- Command Line:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- ![Bing-Chat-API-CLI](./docs/bing-chat-api-cli.png)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  app_port: 22222
8
  ---
9
 
10
+ # Bing-Chat-API
11
 
12
+ Chat with Bing like what you do with OpenAI API.
13
 
14
+ ## Thanks
15
+ - [EdgeGPT](https://github.com/acheong08/EdgeGPT) by [@acheong08](https://github.com/acheong08)
16
+ - [bingo](https://github.com/weaigc/bingo) by [@weaigc](https://github.com/weaigc)
17
+ - [@ninomae](https://github.com/NINOMAE1995)
18
 
19
+ ## Features
20
 
21
+ Implemented:
22
+
23
+ - Support all conversation styles in New Bing
24
+ - `precise`, `balanced`, `creative`
25
+ - Enable/Disable search
26
+ - Model names suffixed with `offline` are disabling search
27
+ - `precise-offline`, `balanced-offline`, `creative-offline`
28
+ - Support OpenAI API format
29
+ - Can use api endpoint via official `openai-python` package
30
+ - Support stream response
31
+ - Support system prompt
32
+ - This means you could bring Sydney back!
33
+ - Support infinite-round chat
34
+ - As long as not exceeded the token limit (~32k)
35
+ - Support Docker deployment
36
+
37
+ 🔨 In progress:
38
+ - [ ] Enhance performance and reduce session create requests
39
+ - [ ] Authentication with API key
40
+
41
+ ## Run API service
42
+
43
+ ### Run in Command Line
44
+
45
+ **Install dependencies:**
46
 
47
  ```bash
48
  # pipreqs . --force --mode no-pin
49
  pip install -r requirements.txt
50
  ```
51
 
52
+ **Run API:**
53
 
54
  ```bash
55
+ python -m apis.chat_api
56
  ```
57
 
58
+ ## Run via Docker
59
 
60
+ **Docker build:**
61
 
62
  ```bash
63
+ sudo docker build -t bing-chat-api:1.0 . --build-arg http_proxy=$http_proxy --build-arg https_proxy=$https_proxy
64
  ```
65
 
66
+ **Docker run:**
67
 
68
  ```bash
69
  # no proxy
 
73
  sudo docker run -p 22222:22222 --env http_proxy="http://<server>:<port>" bing-chat-api:1.0
74
  ```
75
 
76
+ ## API Usage
77
+
78
+ ### Using `openai-python`
79
+
80
+ See: [examples/chat_with_openai.py](https://github.com/Hansimov/bing-chat-api/blob/main/examples/chat_with_openai.py)
81
+
82
+ ```py
83
+ from openai import OpenAI
84
+
85
+ # If runnning this service with proxy, you might need to unset `http(s)_proxy`.
86
+ base_url = "http://localhost:22222"
87
+ api_key = "sk-xxxxx"
88
+ client = OpenAI(base_url=base_url, api_key=api_key)
89
+ response = client.chat.completions.create(
90
+ model="precise",
91
+ messages=[
92
+ {
93
+ "role": "user",
94
+ "content": "search california's weather for me",
95
+ }
96
+ ],
97
+ stream=True,
98
+ )
99
+
100
+ for chunk in response:
101
+ if chunk.choices[0].delta.content is not None:
102
+ print(chunk.choices[0].delta.content, end="", flush=True)
103
+ elif chunk.choices[0].finish_reason == "stop":
104
+ print()
105
+ else:
106
+ pass
107
+ ```
108
 
109
+ ### Using post requests
110
+
111
+ See: [examples/chat_with_post.py](https://github.com/Hansimov/bing-chat-api/blob/main/examples/chat_with_post.py)
112
+
113
+ ```py
114
+ import ast
115
+ import httpx
116
+ import json
117
+ import re
118
+
119
+ # If runnning this service with proxy, you might need to unset `http(s)_proxy`.
120
+ chat_api = "http://localhost:22222"
121
+ api_key = "sk-xxxxx"
122
+ requests_headers = {}
123
+
124
+ requests_payload = {
125
+ "model": "precise",
126
+ "messages": [
127
+ {
128
+ "role": "user",
129
+ "content": "search and tell me today's weather of california",
130
+ }
131
+ ],
132
+ "stream": True,
133
+ }
134
+
135
+ with httpx.stream(
136
+ "POST",
137
+ chat_api + "/chat/completions",
138
+ headers=requests_headers,
139
+ json=requests_payload,
140
+ timeout=httpx.Timeout(connect=20, read=60, write=20, pool=None),
141
+ ) as response:
142
+ response_content = ""
143
+ for line in response.iter_lines():
144
+ remove_patterns = [r"^\s*data:\s*", r"^\s*\[DONE\]\s*"]
145
+ for pattern in remove_patterns:
146
+ line = re.sub(pattern, "", line).strip()
147
+
148
+ if line:
149
+ try:
150
+ line_data = json.loads(line)
151
+ except Exception as e:
152
+ try:
153
+ line_data = ast.literal_eval(line)
154
+ except:
155
+ print(f"Error: {line}")
156
+ raise e
157
+ delta_data = line_data["choices"][0]["delta"]
158
+ finish_reason = line_data["choices"][0]["finish_reason"]
159
+ if "role" in delta_data:
160
+ role = delta_data["role"]
161
+ if "content" in delta_data:
162
+ delta_content = delta_data["content"]
163
+ response_content += delta_content
164
+ print(delta_content, end="", flush=True)
165
+ if finish_reason == "stop":
166
+ print()
167
+ ```