Skip to content
This repository was archived by the owner on Mar 10, 2025. It is now read-only.

Commit 7fef481

Browse files
committed
✨ feat: [OpenAI] new params for Chat completion + size for Image gen
1 parent 9da281c commit 7fef481

File tree

10 files changed

+357
-175
lines changed

10 files changed

+357
-175
lines changed

.gitignore

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
**/__pycache__/
2+
*.egg-info/
13
build/
24
dist/
5+
.tox/
36
playground/
4-
poe_api_wrapper/openai/__pycache__/
5-
*.egg-info/
6-
poe_api_wrapper/__pycache__/
77
examples/
8-
langchain/
8+
langchain/
9+
Dockerfile

README.md

+68-37
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
```ShellSession
128128
pip install -U poe-api-wrapper
129129
```
130-
Or you can install a proxy-support version of this library for **Python 3.9+**
130+
Or you can install auto-proxy version of this library for **Python 3.9+**
131131
```ShellSession
132132
pip install -U 'poe-api-wrapper[proxy]'
133133
```
@@ -168,38 +168,39 @@ poe -b P-B_HERE -lat P-LAT_HERE -f FORMKEY_HERE
168168

169169
## 🦄 Documentation
170170
### Available Default Bots
171-
| Display Name | Model | Token Limit | Words | Access Type |
172-
| ---------------------- | ------------------------- | ----------- | ----- | --------------------------------------------------------------- |
173-
| Assistant | capybara | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
174-
| Claude-3.5-Sonnet | claude_3_igloo | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
175-
| Claude-3-Opus | claude_2_1_cedar | 4K | 3K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
176-
| Claude-3-Sonnet | claude_2_1_bamboo | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
177-
| Claude-3-Haiku | claude_3_haiku | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
178-
| Claude-3.5-Sonnet-200k | claude_3_igloo_200k | 200K | 150K | ![Free](https://img.shields.io/badge/free-2feb7a) |
179-
| Claude-3-Opus-200k | claude_3_opus_200k | 200K | 150K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
180-
| Claude-3-Sonnet-200k | claude_3_sonnet_200k | 200K | 150K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
181-
| Claude-3-Haiku-200k | claude_3_haiku_200k | 200K | 150K | ![Free](https://img.shields.io/badge/free-2feb7a) |
182-
| Claude-2 | claude_2_short | 4K | 3K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
183-
| Claude-2-100k | a2_2 | 100K | 75K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
184-
| Claude-instant | a2 | 9K | 7K | ![Free](https://img.shields.io/badge/free-2feb7a) |
185-
| Claude-instant-100k | a2_100k | 100K | 75K | ![Free](https://img.shields.io/badge/free-2feb7a) |
186-
| GPT-3.5-Turbo | chinchilla | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
187-
| GPT-3.5-Turbo-Raw | gpt3_5 | 2k | 1.5K | ![Free](https://img.shields.io/badge/free-2feb7a) |
188-
| GPT-3.5-Turbo-Instruct | chinchilla_instruct | 2K | 1.5K | ![Free](https://img.shields.io/badge/free-2feb7a) |
189-
| ChatGPT-16k | agouti | 16K | 12K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
190-
| GPT-4-Classic | gpt4_classic | 2K | 1.5K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
191-
| GPT-4-Turbo | beaver | 4K | 3K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
192-
| GPT-4-Turbo-128k | vizcacha | 128K | 96K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
193-
| GPT-4o | gpt4_o | 4k | 3k | ![Free](https://img.shields.io/badge/free-2feb7a) |
194-
| GPT-4o-128k | gpt4_o_128k | 128K | 96K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
195-
| Google-PaLM | acouchy | 8K | 6K | ![Free](https://img.shields.io/badge/free-2feb7a) |
196-
| Llama-2-7b | llama_2_7b_chat | 2K | 1.5K | ![Free](https://img.shields.io/badge/free-2feb7a) |
197-
| Llama-2-13b | llama_2_13b_chat | 2K | 1.5K | ![Free](https://img.shields.io/badge/free-2feb7a) |
198-
| Llama-2-70b | llama_2_70b_chat | 2K | 1.5K | ![Free](https://img.shields.io/badge/free-2feb7a) |
199-
| Code-Llama-7b | code_llama_7b_instruct | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
200-
| Code-Llama-13b | code_llama_13b_instruct | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
201-
| Code-Llama-34b | code_llama_34b_instruct | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
202-
| Solar-Mini | upstage_solar_0_70b_16bit | 2K | 1.5K | ![Free](https://img.shields.io/badge/free-2feb7a) |
171+
| Display Name | Model | Token Limit | Words | Access Type |
172+
| ----------------------- | ------------------------- | ----------- | ----- | --------------------------------------------------------------- |
173+
| Assistant | capybara | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
174+
| Claude-3.5-Sonnet | claude_3_igloo | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
175+
| Claude-3-Opus | claude_2_1_cedar | 4K | 3K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
176+
| Claude-3-Sonnet | claude_2_1_bamboo | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
177+
| Claude-3-Haiku | claude_3_haiku | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
178+
| Claude-3.5-Sonnet-200k | claude_3_igloo_200k | 200K | 150K | ![Free](https://img.shields.io/badge/free-2feb7a) |
179+
| Claude-3-Opus-200k | claude_3_opus_200k | 200K | 150K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
180+
| Claude-3-Sonnet-200k | claude_3_sonnet_200k | 200K | 150K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
181+
| Claude-3-Haiku-200k | claude_3_haiku_200k | 200K | 150K | ![Free](https://img.shields.io/badge/free-2feb7a) |
182+
| Claude-2 | claude_2_short | 4K | 3K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
183+
| Claude-2-100k | a2_2 | 100K | 75K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
184+
| Claude-instant | a2 | 9K | 7K | ![Free](https://img.shields.io/badge/free-2feb7a) |
185+
| Claude-instant-100k | a2_100k | 100K | 75K | ![Free](https://img.shields.io/badge/free-2feb7a) |
186+
| GPT-3.5-Turbo | chinchilla | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
187+
| GPT-3.5-Turbo-Raw | gpt3_5 | 2k | 1.5K | ![Free](https://img.shields.io/badge/free-2feb7a) |
188+
| GPT-3.5-Turbo-Instruct | chinchilla_instruct | 2K | 1.5K | ![Free](https://img.shields.io/badge/free-2feb7a) |
189+
| ChatGPT-16k | agouti | 16K | 12K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
190+
| GPT-4-Classic | gpt4_classic | 2K | 1.5K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
191+
| GPT-4-Turbo | beaver | 4K | 3K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
192+
| GPT-4-Turbo-128k | vizcacha | 128K | 96K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
193+
| GPT-4o | gpt4_o | 4k | 3k | ![Free](https://img.shields.io/badge/free-2feb7a) |
194+
| GPT-4o-128k | gpt4_o_128k | 128K | 96K | ![Subscriber](https://img.shields.io/badge/subscriber-fc4747) |
195+
| Google-PaLM | acouchy | 8K | 6K | ![Free](https://img.shields.io/badge/free-2feb7a) |
196+
| Llama-2-7b | llama_2_7b_chat | 2K | 1.5K | ![Free](https://img.shields.io/badge/free-2feb7a) |
197+
| Llama-2-13b | llama_2_13b_chat | 2K | 1.5K | ![Free](https://img.shields.io/badge/free-2feb7a) |
198+
| Llama-2-70b | llama_2_70b_chat | 2K | 1.5K | ![Free](https://img.shields.io/badge/free-2feb7a) |
199+
| Code-Llama-7b | code_llama_7b_instruct | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
200+
| Code-Llama-13b | code_llama_13b_instruct | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
201+
| Code-Llama-34b | code_llama_34b_instruct | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
202+
| Solar-Mini | upstage_solar_0_70b_16bit | 2K | 1.5K | ![Free](https://img.shields.io/badge/free-2feb7a) |
203+
| Gemini-1.5-Flash-Search | gemini_pro_search | 4K | 3K | ![Free](https://img.shields.io/badge/free-2feb7a) |
203204
> [!IMPORTANT]
204205
> The data on token limits and word counts listed above are approximate and may not be entirely accurate, as the pre-prompt engineering process of poe.com is private and not publicly disclosed.
205206
>
@@ -316,8 +317,36 @@ stream = client.chat.completions.create(
316317
)
317318

318319
for chunk in stream:
319-
if chunk.choices[0].delta.content is not None:
320-
print(chunk.choices[0].delta.content, end="")
320+
print(chunk.choices[0].delta.content or "", end="", flush=True)
321+
322+
# Set max_tokens
323+
stream_2 = client.chat.completions.create(
324+
model="claude-instant",
325+
messages = [
326+
{"role": "user", "content": "Can you tell me about the creation of blackholes?"}
327+
],
328+
stream=True,
329+
max_tokens=20, # if max_tokens reached, finish_reason will be 'length'
330+
)
331+
332+
for chunk in stream_2:
333+
print(chunk.choices[0].delta.content or "", end="", flush=True)
334+
335+
# Include usage
336+
stream_3 = client.chat.completions.create(
337+
model="claude-instant",
338+
messages = [
339+
{"role": "user", "content": "Write a 100-character meta description for my blog post about llamas"}
340+
],
341+
stream=True,
342+
max_tokens=4096,
343+
stream_options={
344+
"include_usage": True # last chunk contains prompts_tokens, completion_tokens and total_tokens
345+
}
346+
)
347+
348+
for chunk in stream_3:
349+
print(chunk, end="\n\n", flush=True)
321350
```
322351
- Image input example:
323352
```py
@@ -433,9 +462,10 @@ import openai
433462
client = openai.OpenAI(api_key="anything", base_url="http://127.0.0.1:8000/v1/", default_headers={"Authorization": "Bearer anything"})
434463

435464
images_url = client.images.generate(
436-
model="sdxl",
465+
model="playground-v2.5",
437466
prompt="A cute baby sea otter",
438467
n=2, # The number of images to generate
468+
size="1792x1024" # The size of image (view models.json for available sizes)
439469
)
440470

441471
print(images_url)
@@ -447,9 +477,10 @@ client = openai.OpenAI(api_key="anything", base_url="http://127.0.0.1:8000/v1/",
447477

448478
images_url = client.images.edit(
449479
image="https://imgcdn.stablediffusionweb.com/2024/4/29/0b0b8798-1965-4e3d-b0a8-d153728320d4.jpg",
450-
model="playground-v2.5",
480+
model="sdxl",
451481
prompt="A cute baby sea otter wearing a raincoat",
452482
n=1, # The number of images to generate
483+
size="1024x1024" # The size of image (view models.json for available sizes)
453484
)
454485

455486
print(images_url)

poe_api_wrapper/api.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def send_request(self, path: str, query_name: str="", variables: dict={}, file_f
169169
if isinstance(e, ReadTimeout):
170170
if query_name == "SendMessageMutation":
171171
logger.error(f"Failed to send message {variables['query']} due to ReadTimeout")
172-
logger.info(f"Attempting to retry message {variables['query']} 3 times...")
172+
raise e
173173
else:
174174
logger.error(f"Automatic retrying request {query_name} due to ReadTimeout")
175175
return self.send_request(path, query_name, variables, file_form)
@@ -198,7 +198,7 @@ def subscribe(self):
198198
raise RuntimeError(f'Failed to subscribe by sending SubscriptionsMutation. Raw response data: {response_json}')
199199

200200
def ws_run_thread(self):
201-
if not self.ws.sock:
201+
if self.ws and not self.ws.sock:
202202
kwargs = {"sslopt": {"cert_reqs": ssl.CERT_NONE}}
203203
self.ws.run_forever(**kwargs)
204204

@@ -259,7 +259,6 @@ def disconnect_ws(self):
259259
self.ws_connected = False
260260
if self.ws:
261261
self.ws.close()
262-
self.ws = None
263262
logger.info("Websocket connection closed.")
264263

265264
def on_ws_connect(self, ws):
@@ -284,7 +283,6 @@ def on_message(self, ws, msg):
284283
ws_data = orjson.loads(msg)
285284

286285
if "error" in ws_data.keys() and ws_data["error"] == "missed_messages":
287-
self.disconnect_ws()
288286
self.connect_ws()
289287
return
290288

@@ -295,7 +293,6 @@ def on_message(self, ws, msg):
295293
data = orjson.loads(data)
296294
message_type = data.get("message_type")
297295
if message_type == "refetchChannel":
298-
self.disconnect_ws()
299296
self.connect_ws()
300297
return
301298

poe_api_wrapper/async_api.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
try:
2-
from httpx import AsyncClient, ConnectError, ReadTimeout
3-
ASYNC = True
4-
except ImportError:
5-
ASYNC = False
1+
from httpx import AsyncClient, ConnectError, ReadTimeout
62
import asyncio, queue, orjson, random, ssl, threading, websocket, string, secrets, os, hashlib, re, aiofiles
73
from typing import AsyncIterator
84
from loguru import logger
@@ -41,8 +37,6 @@ class AsyncPoeApi:
4137

4238
def __init__(self, tokens: dict={}, proxy: list=[], auto_proxy: bool=False):
4339
self.client = None
44-
if not ASYNC:
45-
raise ImportError("Please install Async version using 'pip install poe-api-wrapper[async]'")
4640
if not {'p-b', 'p-lat'}.issubset(tokens):
4741
raise ValueError("Please provide valid p-b and p-lat cookies")
4842

@@ -190,7 +184,7 @@ async def send_request(self, path: str, query_name: str="", variables: dict={},
190184
if isinstance(e, ReadTimeout):
191185
if query_name == "SendMessageMutation":
192186
logger.error(f"Failed to send message {variables['query']} due to ReadTimeout")
193-
logger.info(f"Attempting to retry message {variables['query']} 3 times...")
187+
raise e
194188
else:
195189
logger.error(f"Automatic retrying request {query_name} due to ReadTimeout")
196190
return await self.send_request(path, query_name, variables, file_form)
@@ -220,7 +214,7 @@ async def subscribe(self):
220214
raise RuntimeError(f'Failed to subscribe by sending SubscriptionsMutation. Raw response data: {response_json}')
221215

222216
def ws_run_thread(self):
223-
if not self.ws.sock:
217+
if self.ws and not self.ws.sock:
224218
kwargs = {"sslopt": {"cert_reqs": ssl.CERT_NONE}}
225219
try:
226220
self.ws.run_forever(**kwargs)
@@ -292,7 +286,6 @@ def disconnect_ws(self):
292286
self.ws_connected = False
293287
if self.ws:
294288
self.ws.close()
295-
self.ws = None
296289
logger.info("Websocket connection closed.")
297290

298291
def on_ws_connect(self, ws):
@@ -363,10 +356,10 @@ def on_message(self, ws, msg):
363356

364357
except Exception:
365358
logger.exception(f"Failed to parse message: {msg}")
359+
self.disconnect_ws()
366360
self.refresh_ws()
367361

368362
def refresh_ws(self):
369-
self.disconnect_ws()
370363
self.loop = asyncio.new_event_loop()
371364
asyncio.set_event_loop(self.loop)
372365
self.loop.run_in_executor(None, self.connect_ws())

0 commit comments

Comments
 (0)