Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new bot_name field to fp.make_app() #120

Merged
merged 31 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a152557
add helper to concatenate attachment content to message
JohntheLi Mar 29, 2024
de50de9
fix linter and change enforce author role alternation default to false
JohntheLi Mar 29, 2024
93df94c
update version for release
JohntheLi Mar 29, 2024
8d0bdb9
Merge branch 'main' of https://github.com/JohntheLi/fastapi_poe
JohntheLi Apr 17, 2024
c15b08e
add enable_multi_bot_chat_prompting setting and increase default timeout
JohntheLi Apr 17, 2024
9554776
update pyproject version
JohntheLi Apr 17, 2024
71f7b4f
Merge branch 'main' of https://github.com/JohntheLi/fastapi_poe
JohntheLi May 1, 2024
08d86f3
poe bot attachments: insert attachments in new messages
JohntheLi May 4, 2024
8d04623
update the pyproject version
JohntheLi May 4, 2024
b5895c1
unit tests: remove 3.7 test from arm64
JohntheLi May 4, 2024
d15b44e
protocol messages: add sender_id
JohntheLi May 15, 2024
f88defd
Merge branch 'poe-platform:main' into main
JohntheLi May 15, 2024
4044cbb
update pyproject to 0.0.44 for release
JohntheLi May 15, 2024
d87eebe
add default value as None for sender_id
JohntheLi May 15, 2024
697b15b
Merge branch 'main' of https://github.com/JohntheLi/fastapi_poe
JohntheLi Jun 10, 2024
0b9adb7
Add language_code and raw_response
JohntheLi Jun 10, 2024
19f5488
fix some docs
JohntheLi Jun 10, 2024
db88084
Merge branch 'main' of https://github.com/JohntheLi/fastapi_poe
JohntheLi Jul 1, 2024
c4979b1
poebot: improve error responses
JohntheLi Jul 2, 2024
f279bcf
update pyproject version for release
JohntheLi Jul 2, 2024
97bbc83
Merge branch 'main' of https://github.com/JohntheLi/fastapi_poe
JohntheLi Jul 23, 2024
4e7a5f9
sync_bot_settings: initialize it properly
JohntheLi Jul 23, 2024
da1504f
update pyproject.toml
JohntheLi Jul 23, 2024
5fdeeb3
Merge branch 'poe-platform:main' into main
JohntheLi Aug 16, 2024
1849679
Revert "Auto sync bot settings on bot server start (#117)"
JohntheLi Aug 17, 2024
8d8db98
re-add some good changes
JohntheLi Aug 17, 2024
16fa46f
Merge branch 'poe-platform:main' into main
JohntheLi Aug 27, 2024
8588433
bot settings: add auto-syncing
JohntheLi Aug 27, 2024
cb8212c
formatting some sync changes
JohntheLi Aug 28, 2024
8e174ae
update pyproject.toml for release
JohntheLi Aug 28, 2024
72532c9
allow bot_name to be passed into fp.make_app()
JohntheLi Aug 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "fastapi_poe"
version = "0.0.47"
version = "0.0.48"
authors = [
{ name="Lida Li", email="[email protected]" },
{ name="Jelle Zijlstra", email="[email protected]" },
Expand Down
45 changes: 43 additions & 2 deletions src/fastapi_poe/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from starlette.types import Message
from typing_extensions import deprecated, overload

from fastapi_poe.client import PROTOCOL_VERSION, sync_bot_settings
from fastapi_poe.templates import (
IMAGE_VISION_ATTACHMENT_TEMPLATE,
TEXT_ATTACHMENT_TEMPLATE,
Expand Down Expand Up @@ -132,6 +133,7 @@ class PoeBot:

path: str = "/" # Path where this bot will be exposed
access_key: Optional[str] = None # Access key for this bot
bot_name: Optional[str] = None # Name of the bot using this PoeBot instance in Poe
should_insert_attachment_messages: bool = (
True # Whether to insert attachment messages into the conversation
)
Expand Down Expand Up @@ -866,6 +868,7 @@ def make_app(
bot: Union[PoeBot, Sequence[PoeBot]],
access_key: str = "",
*,
bot_name: str = "",
api_key: str = "",
allow_without_key: bool = False,
app: Optional[FastAPI] = None,
Expand Down Expand Up @@ -910,11 +913,18 @@ def make_app(
raise ValueError(
"Cannot provide api_key if the bot object already has an access key"
)

if bot.bot_name is None:
bot.bot_name = bot_name
elif bot_name:
raise ValueError(
"Cannot provide bot_name if the bot object already has a bot_name"
)
bots = [bot]
else:
if access_key or api_key:
if access_key or api_key or bot_name:
raise ValueError(
"When serving multiple bots, the access_key must be set on each bot"
"When serving multiple bots, the access_key/bot_name must be set on each bot"
)
bots = bot

Expand All @@ -933,6 +943,37 @@ def make_app(
if bot_obj.access_key is None and not allow_without_key:
raise ValueError(f"Missing access key on {bot_obj}")
_add_routes_for_bot(app, bot_obj)
if not bot_obj.bot_name or not bot_obj.access_key:
logger.warning("\n************* Warning *************")
logger.warning(
"Bot name or access key is not set for PoeBot.\n"
"Bot settings will NOT be synced automatically on server start/update."
"Please remember to sync bot settings manually.\n\n"
"For more information, see: https://creator.poe.com/docs/server-bots-functional-guides#updating-bot-settings"
)
logger.warning("\n************* Warning *************")
else:
try:
settings_response = asyncio.run(
bot_obj.get_settings(
SettingsRequest(version=PROTOCOL_VERSION, type="settings")
)
)
sync_bot_settings(
bot_name=bot_obj.bot_name,
settings=settings_response.model_dump(),
access_key=bot_obj.access_key,
)
except Exception as e:
logger.error("\n*********** Error ***********")
logger.error(
f"Bot settings sync failed for {bot_obj.bot_name}: \n{e}\n\n"
)
logger.error("Please sync bot settings manually.\n\n")
logger.error(
"For more information, see: https://creator.poe.com/docs/server-bots-functional-guides#updating-bot-settings"
)
logger.error("\n*********** Error ***********")

# Uncomment this line to print out request and response
# app.add_middleware(LoggingMiddleware)
Expand Down
27 changes: 20 additions & 7 deletions src/fastapi_poe/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,17 +617,30 @@ async def get_final_response(
def sync_bot_settings(
bot_name: str,
access_key: str = "",
base_url: str = "https://api.poe.com/bot/fetch_settings/",
*,
settings: Optional[Dict[str, Any]] = None,
base_url: str = "https://api.poe.com/bot/",
) -> None:
"""Sync bot settings with the Poe server using bot name and its Access Key."""
"""Fetch settings from the running bot server, and then sync them with Poe."""
try:
response = httpx.post(f"{base_url}{bot_name}/{access_key}/{PROTOCOL_VERSION}")
if settings is None:
response = httpx.post(
f"{base_url}fetch_settings/{bot_name}/{access_key}/{PROTOCOL_VERSION}"
)
else:
headers = {"Content-Type": "application/json"}
response = httpx.post(
f"{base_url}update_settings/{bot_name}/{access_key}/{PROTOCOL_VERSION}",
headers=headers,
json=settings,
)
if response.status_code != 200:
raise BotError(
f"Error fetching settings for bot {bot_name}: {response.text}"
f"Error syncing settings for bot {bot_name}: {response.text}"
)
except httpx.ReadTimeout as e:
raise BotError(
f"Timeout fetching settings for bot {bot_name}. Try sync manually later."
) from e
error_message = f"Timeout syncing settings for bot {bot_name}."
if not settings:
error_message += " Check that the bot server is running."
raise BotError(error_message) from e
print(response.text)
Loading