From bead99f2305461fb07c7a7d228bd1e1998edb7a1 Mon Sep 17 00:00:00 2001 From: Anmol Singh <5581466+anmolsingh95@users.noreply.github.com> Date: Thu, 4 Apr 2024 20:42:12 -0700 Subject: [PATCH 1/6] add docstrings plus reference generation script --- docs/api_reference.md | 187 +++++++++++++++++++++++++++++++++ docs/generate_api_reference.py | 108 +++++++++++++++++++ src/fastapi_poe/base.py | 133 +++++++++++++++++------ src/fastapi_poe/client.py | 39 ++++++- src/fastapi_poe/types.py | 45 +++++++- 5 files changed, 476 insertions(+), 36 deletions(-) create mode 100644 docs/api_reference.md create mode 100644 docs/generate_api_reference.py diff --git a/docs/api_reference.md b/docs/api_reference.md new file mode 100644 index 0000000..9d258ff --- /dev/null +++ b/docs/api_reference.md @@ -0,0 +1,187 @@ +The following it the API reference for the +[fastapi_poe](https://github.com/poe-platform/fastapi_poe) client library. + +# `class PoeBot` + +The class that you use to define your bot behavior.Once you define your PoeBot class, +you pass it to make_run to serve that bot. + +#### Parameters: + +- `path`: This is the path at which your bot is served. By default, it's set to "/" but + this is something you can adjust. This is especially useful if you want to serve + multiple bots from one server. +- `access_key`: This is the access key for your bot and when provided is used to + validate that requests are coming from a trusted source. This access key should be the + same one that you provide when integrating your bot with Poe at: + https://poe.com/create_bot?server=1. You can also set this to None but certain + features like file-output that mandate an `access_key` will not be available to your + bot. +- `concat_attachments_to_message`: A flag to decide whether to parse out content from + attachments and concatenate it to the conversation message. This is set to `True` by + default and we recommend leaving it at that for most developers since it allows your + bot to comprehent attachments uploaded by users by default. + +### `PoeBot.get_response` + +Override this to return a response to user queries. + +### `PoeBot.get_response_with_context` + +A version of `get_response` that also passes in the request context information. By +default, this will call `get_response`. + +### `PoeBot.get_settings` + +Override this to return non-standard settings. + +### `PoeBot.get_settings_with_context` + +A version of `get_settings` that also passes in the request context information. By +default, this will call `get_settings`. + +### `PoeBot.on_feedback` + +Override this to record feedback from the user. + +### `PoeBot.on_feedback_with_context` + +A version of `on_feedback` that also passes in the request context information. By +default, this will call `on_feedback`. + +### `PoeBot.on_error` + +Override this to record errors from the Poe server. + +### `PoeBot.on_error_with_context` + +A version of `on_error` that also passes in the request context information. By default, +this will call `on_error`. + +### `PoeBot.post_message_attachment` + +Used to output an attachment in the response. + +#### Parameters: + +- `message_id` (str): the message id associated with the currentQueryRequest object + being processed. **Important**: This must be the request that is currently being + handled by get_response. Attempting to attach files to previously handled requests + will fail. +- `download_url` (str): provide a url to a file that will be attached to the message. +- `file_data` (bytes): the contents of the file to be uploaded. This should be a + bytes-like or file object. +- `filename` - the name of the file to be attached. + +**Note**: You need to provide either the `download_url` or both of `file_data` and +`filename`. + +### `PoeBot.concat_attachment_content_to_message_body` + +Concatenate received attachment file content into the message body. + +# `def make_app` + +Create an app object for your bot(s). + +#### Parameters: + +- `bot`: A bot object or a list of bot objects if you want to host multiple bots on one + server. +- `access_key`: The access key to use. If not provided, the server tries to read the + POE_ACCESS_KEY environment variable. If that is not set, the server will refuse to + start, unless `allow_without_key` is True. If multiple bots are provided, the access + key must be provided as part of the bot object. +- `api_key`: The previous name of access_key. This is not to be confused with the + api_key param needed by `stream_request`. This param is deprecated and will be removed + in a future version. +- allow_without_key`: If True, the server will start even if no access key is provided. + Requests will not be checked against any key. If an access key is provided, it is + still checked. +- `app`: A FastAPI app instance. If provided, the app will be configured with the + provided bots, access keys, and other settings. If not provided, a new FastAPI + application instance will be created and configured. + +# `def run` + +Run a Poe bot server using FastAPI. This function should be used when you are running +the bot locally. The arguments are the same as that for `make_app`. + +# `def stream_request` + +Entry point for the Bot Query API. This API allows you to use other bots on Poe for +inference in response to a user message. For more details, checkout: +https://creator.poe.com/docs/accessing-other-bots-on-poe + +#### Parameters: + +- `request`: A QueryRequest object representing a query from Poe. This object also + includes information needed to identify the user for compute point usage. +- `bot_name`: The bot you want to invoke. +- `api_key`: Your Poe API key, available at poe.com/api_key. You will need this in case + you are trying to use this function from a script/shell. compute points may apply + depending on the bot you call. + +# `def get_bot_response` + +Use this function to invoke another Poe bot from your shell. + +#### Parameters: + +- `messages`: A list of protocol messages representing your conversation +- `bot_name`: The bot that you want to invoke. +- `api_key`: Your Poe API key. This is available at: poe.com/api_key + +# `def get_final_response` + +Another helper function for you to be able to use the bot query API. Waits for all the +tokens and concatenates the full response before returning. + +#### Parameters: + +- `request`: A QueryRequest object representing a query from Poe. This object also + includes information needed to identify the user for compute point usage. +- `bot_name`: The bot you want to invoke. +- `api_key`: Your Poe API key, available at poe.com/api_key. You will need this in case + you are trying to use this function from a script/shell. compute points may apply + depending on the bot you call. + +# `class PartialResponse` + +Representation of a (possibly partial) response from a bot. Yield this in your +`PoeBot.get_response` or `PoeBot.get_response_with_context` while setting values +appropriately to communicate your response to Poe. + +#### Parameters: + +- `text`: The actual text you want to display to the user. Note that this solely be the + text in the next token as Poe will automatically concatenate all tokens before + displaying the response to the user. +- `data`: Used to send json data to Poe. This is currently only used in the context of + OpenAI function calling. +- `is_suggested_reply`: Seting this to true will create a suggested reply with the + provided text string. +- `is_replace_response`: Setting this to true will clear out the previously displayed + text to the user and replace it with the provided str. + +# `class ErrorResponse` + +Similar to `PartialResponse`. Yield this to communicate errors from your bot. + +#### Parameters: + +- `allow_retry`: Whether or not to allow a user to retry on error. +- `error_type`: An enum indicating what error to display. + +# `class MetaResponse` + +Similar to `Partial Response`. Yield this to communicate 'meta' events from server bots. + +#### Parameters: + +- `suggested_replies`: Whether or not enable suggested replies. +- `content_type`: Used to describe the format of the response. The currently supported + values are `text/plain` and `text/markdown`. +- `refetch_settings`: Used to trigger a settings fetch request from Poe. A more robust + way to trigger this is documented at: + https://creator.poe.com/docs/updating-bot-settings diff --git a/docs/generate_api_reference.py b/docs/generate_api_reference.py new file mode 100644 index 0000000..4ccbf06 --- /dev/null +++ b/docs/generate_api_reference.py @@ -0,0 +1,108 @@ +import inspect +import sys +from dataclasses import dataclass, field +from typing import Dict, List, Optional + +INITIAL_TEXT = """ + +The following it the API reference for the +[fastapi_poe](https://github.com/poe-platform/fastapi_poe) client library. + +""" + + +@dataclass +class DocumentationData: + name: str + docstring: Optional[str] + data_type: str + children: List = field(default_factory=lambda: []) + + +def _unwrap_func(func_obj): + """This is to ensure we get the docstring from the actual func and not a decorator.""" + if isinstance(func_obj, staticmethod): + return _unwrap_func(func_obj.__func__) + if hasattr(func_obj, "__wrapped__"): + return _unwrap_func(func_obj.__wrapped__) + return func_obj + + +def get_documentation_data(*, module, documented_items) -> Dict[str, DocumentationData]: + data_dict = {} + for name, obj in inspect.getmembers(module): + if ( + inspect.isclass(obj) or inspect.isfunction(obj) + ) and name in documented_items: + doc = inspect.getdoc(obj) + data_type = "class" if inspect.isclass(obj) else "function" + dd_obj = DocumentationData(name=name, docstring=doc, data_type=data_type) + + if inspect.isclass(obj): + children = [] + # for func_name, func_obj in inspect.getmembers(obj, inspect.isfunction): + for func_name, func_obj in obj.__dict__.items(): + if not inspect.isfunction(func_obj): + continue + if not func_name.startswith("_"): + func_obj = _unwrap_func(func_obj) + func_doc = inspect.getdoc(func_obj) + children.append( + DocumentationData( + name=func_name, docstring=func_doc, data_type="function" + ) + ) + dd_obj.children = children + data_dict[name] = dd_obj + return data_dict + + +def generate_documentation( + *, data_dict: Dict[str, DocumentationData], documented_items, output_filename +): + # reset the file first + with open(output_filename, "w") as f: + f.write("") + + with open(output_filename, "w") as f: + f.write(INITIAL_TEXT) + + for item in documented_items: + item_data = data_dict[item] + prefix = "class" if item_data.data_type == "class" else "def" + f.write(f"# `{prefix} {item_data.name}`\n\n") + f.write(f"{item_data.docstring}\n\n") + for child in item_data.children: + if not child.docstring: + continue + f.write(f"### `{item}.{child.name}`\n\n") + f.write(f"{child.docstring}\n\n") + f.write("\n\n") + + +# Usage example +sys.path.append("../src") +import fastapi_poe + +# Specify the names of classes and functions to document +documented_items = [ + "PoeBot", + "make_app", + "run", + "stream_request", + "get_bot_response", + "get_final_response", + "PartialResponse", + "ErrorResponse", + "MetaResponse", +] + +data_dict = get_documentation_data( + module=fastapi_poe, documented_items=documented_items +) +output_filename = "api_reference.md" +generate_documentation( + data_dict=data_dict, + documented_items=documented_items, + output_filename=output_filename, +) diff --git a/src/fastapi_poe/base.py b/src/fastapi_poe/base.py index e1fcbf8..8ca66e4 100644 --- a/src/fastapi_poe/base.py +++ b/src/fastapi_poe/base.py @@ -105,6 +105,27 @@ async def http_exception_handler(request: Request, ex: Exception) -> Response: @dataclass class PoeBot: + """ + + The class that you use to define your bot behavior.Once you define your PoeBot class, you + pass it to make_run to serve that bot. + + #### Parameters: + - `path`: This is the path at which your bot is served. By default, it's set to "/" + but this is something you can adjust. This is especially useful if you want to serve + multiple bots from one server. + - `access_key`: This is the access key for your bot and when provided is used to + validate that requests are coming from a trusted source. This access key should be the same + one that you provide when integrating your bot with Poe at: + https://poe.com/create_bot?server=1. You can also set this to None but certain features like + file-output that mandate an `access_key` will not be available to your bot. + - `concat_attachments_to_message`: A flag to decide whether to parse out content from + attachments and concatenate it to the conversation message. This is set to `True` by default + and we recommend leaving it at that for most developers since it allows your bot to + comprehent attachments uploaded by users by default. + + """ + path: str = "/" # Path where this bot will be exposed access_key: Optional[str] = None # Access key for this bot concat_attachments_to_message: bool = ( @@ -113,45 +134,70 @@ class PoeBot: # Override these for your bot + async def get_response( + self, request: QueryRequest + ) -> AsyncIterable[Union[PartialResponse, ServerSentEvent]]: + """Override this to return a response to user queries.""" + yield self.text_event("hello") + async def get_response_with_context( self, request: QueryRequest, context: RequestContext ) -> AsyncIterable[Union[PartialResponse, ServerSentEvent]]: + """ + + A version of `get_response` that also passes in the request context information. By + default, this will call `get_response`. + + """ + yield self.text_event("hello") async for event in self.get_response(request): yield event - async def get_response( - self, request: QueryRequest - ) -> AsyncIterable[Union[PartialResponse, ServerSentEvent]]: - """Override this to return a response to user queries.""" - yield self.text_event("hello") + async def get_settings(self, setting: SettingsRequest) -> SettingsResponse: + """Override this to return non-standard settings.""" + return SettingsResponse() async def get_settings_with_context( self, setting: SettingsRequest, context: RequestContext ) -> SettingsResponse: + """ + + A version of `get_settings` that also passes in the request context information. By + default, this will call `get_settings`. + + """ settings = await self.get_settings(setting) return settings - async def get_settings(self, setting: SettingsRequest) -> SettingsResponse: - """Override this to return non-standard settings.""" - return SettingsResponse() + async def on_feedback(self, feedback_request: ReportFeedbackRequest) -> None: + """Override this to record feedback from the user.""" + pass async def on_feedback_with_context( self, feedback_request: ReportFeedbackRequest, context: RequestContext ) -> None: + """ + + A version of `on_feedback` that also passes in the request context information. By + default, this will call `on_feedback`. + + """ await self.on_feedback(feedback_request) - async def on_feedback(self, feedback_request: ReportFeedbackRequest) -> None: - """Override this to record feedback from the user.""" - pass + async def on_error(self, error_request: ReportErrorRequest) -> None: + """Override this to record errors from the Poe server.""" + logger.error(f"Error from Poe server: {error_request}") async def on_error_with_context( self, error_request: ReportErrorRequest, context: RequestContext ) -> None: - await self.on_error(error_request) + """ - async def on_error(self, error_request: ReportErrorRequest) -> None: - """Override this to record errors from the Poe server.""" - logger.error(f"Error from Poe server: {error_request}") + A version of `on_error` that also passes in the request context information. By + default, this will call `on_error`. + + """ + await self.on_error(error_request) # Helpers for generating responses def __post_init__(self) -> None: @@ -199,6 +245,23 @@ async def post_message_attachment( content_type: Optional[str] = None, is_inline: bool = False, ) -> AttachmentUploadResponse: + """ + + Used to output an attachment in the response. + + #### Parameters: + - `message_id` (str): the message id associated with the currentQueryRequest object being + processed. **Important**: This must be the request that is currently being handled by + get_response. Attempting to attach files to previously handled requests will fail. + - `download_url` (str): provide a url to a file that will be attached to the message. + - `file_data` (bytes): the contents of the file to be uploaded. This should be a + bytes-like or file object. + - `filename` - the name of the file to be attached. + + **Note**: You need to provide either the `download_url` or both of `file_data` and + `filename`. + + """ if message_id is None: raise InvalidParameterError("message_id parameter is required") @@ -313,7 +376,7 @@ async def _process_pending_attachment_requests( def concat_attachment_content_to_message_body( self, query_request: QueryRequest ) -> QueryRequest: - """Concat received attachment file content into message content body.""" + """Concatenate received attachment file content into the message body.""" last_message = query_request.query[-1] concatenated_content = last_message.content for attachment in last_message.attachments: @@ -593,7 +656,26 @@ def make_app( allow_without_key: bool = False, app: Optional[FastAPI] = None, ) -> FastAPI: - """Create an app object. Arguments are as for run().""" + """ + + Create an app object for your bot(s). + + #### Parameters: + - `bot`: A bot object or a list of bot objects if you want to host multiple bots on one server. + - `access_key`: The access key to use. If not provided, the server tries to read + the POE_ACCESS_KEY environment variable. If that is not set, the server will + refuse to start, unless `allow_without_key` is True. If multiple bots are provided, + the access key must be provided as part of the bot object. + - `api_key`: The previous name of access_key. This is not to be confused with the api_key + param needed by `stream_request`. This param is deprecated and will be removed in a future + version. + - allow_without_key`: If True, the server will start even if no access key is provided. + Requests will not be checked against any key. If an access key is provided, it is still checked. + - `app`: A FastAPI app instance. If provided, the app will be configured with the provided bots, + access keys, and other settings. If not provided, a new FastAPI application instance will be + created and configured. + + """ if app is None: app = FastAPI() app.add_exception_handler(RequestValidationError, http_exception_handler) @@ -651,21 +733,10 @@ def run( app: Optional[FastAPI] = None, ) -> None: """ - Run a Poe bot server using FastAPI. - :param bot: The bot object or a list of bot objects. - :param access_key: The access key to use. If not provided, the server tries to read - the POE_ACCESS_KEY environment variable. If that is not set, the server will - refuse to start, unless *allow_without_key* is True. If multiple bots are provided, - the access key must be provided as part of the bot object. - :param api_key: The previous name of access_key. This param is deprecated and will be - removed in a future version - :param allow_without_key: If True, the server will start even if no access key - is provided. Requests will not be checked against any key. If an access key - is provided, it is still checked. - :param app: A FastAPI app instance. If provided, app will be configured with the - provided bots, access keys, and other settings. If not provided, a new FastAPI - application instance will be created and configured. + Run a Poe bot server using FastAPI. This function should be used when you are running the + bot locally. The arguments are the same as that for `make_app`. + """ app = make_app( diff --git a/src/fastapi_poe/client.py b/src/fastapi_poe/client.py index 99a70b3..33d32fa 100644 --- a/src/fastapi_poe/client.py +++ b/src/fastapi_poe/client.py @@ -307,6 +307,21 @@ async def stream_request( retry_sleep_time: float = 0.5, base_url: str = "https://api.poe.com/bot/", ) -> AsyncGenerator[BotMessage, None]: + """ + + Entry point for the Bot Query API. This API allows you to use other bots on Poe for inference + in response to a user message. For more details, checkout: + https://creator.poe.com/docs/accessing-other-bots-on-poe + + #### Parameters: + - `request`: A QueryRequest object representing a query from Poe. This object also includes + information needed to identify the user for compute point usage. + - `bot_name`: The bot you want to invoke. + - `api_key`: Your Poe API key, available at poe.com/api_key. You will need this in case you are + trying to use this function from a script/shell. compute points may apply depending on the bot + you call. + + """ tool_calls = None tool_results = None if tools is not None: @@ -499,6 +514,15 @@ def get_bot_response( base_url: str = "https://api.poe.com/bot/", session: Optional[httpx.AsyncClient] = None, ) -> AsyncGenerator[BotMessage, None]: + """ + + Use this function to invoke another Poe bot from your shell. + #### Parameters: + - `messages`: A list of protocol messages representing your conversation + - `bot_name`: The bot that you want to invoke. + - `api_key`: Your Poe API key. This is available at: poe.com/api_key + + """ additional_params = {} # This is so that we don't have to redefine the default values for these params. if temperature is not None: @@ -542,7 +566,20 @@ async def get_final_response( retry_sleep_time: float = 0.5, base_url: str = "https://api.poe.com/bot/", ) -> str: - """Gets the final response from a Poe bot.""" + """ + + Another helper function for you to be able to use the bot query API. Waits for all the tokens + and concatenates the full response before returning. + + #### Parameters: + - `request`: A QueryRequest object representing a query from Poe. This object also includes + information needed to identify the user for compute point usage. + - `bot_name`: The bot you want to invoke. + - `api_key`: Your Poe API key, available at poe.com/api_key. You will need this in case you are + trying to use this function from a script/shell. compute points may apply depending on the bot + you call. + + """ chunks: List[str] = [] async for message in stream_request( request, diff --git a/src/fastapi_poe/types.py b/src/fastapi_poe/types.py index bf00e7e..cd1afc2 100644 --- a/src/fastapi_poe/types.py +++ b/src/fastapi_poe/types.py @@ -104,7 +104,25 @@ class AttachmentUploadResponse(BaseModel): class PartialResponse(BaseModel): - """Representation of a (possibly partial) response from a bot.""" + """ + + Representation of a (possibly partial) response from a bot. Yield this in your + `PoeBot.get_response` or `PoeBot.get_response_with_context` while setting values + appropriately to communicate your response to Poe. + + #### Parameters: + - `text`: The actual text you want to display to the user. Note that this solely + be the text in the next token as Poe will automatically concatenate all tokens before + displaying the response to the user. + - `data`: Used to send json data to Poe. This is currently only used in the context + of OpenAI function calling. + - `is_suggested_reply`: Seting this to true will create a suggested reply with the provided + text string. + - `is_replace_response`: Setting this to true will clear out the previously displayed text + to the user and replace it with the provided str. + + + """ # These objects are usually instantiated in user code, so we # disallow extra fields to prevent mistakes. @@ -139,16 +157,35 @@ class PartialResponse(BaseModel): class ErrorResponse(PartialResponse): - """Communicate errors from server bots.""" + """ + + Similar to `PartialResponse`. Yield this to communicate errors from your bot. + + #### Parameters: + - `allow_retry`: Whether or not to allow a user to retry on error. + - `error_type`: An enum indicating what error to display. + + """ allow_retry: bool = False error_type: Optional[ErrorType] = None class MetaResponse(PartialResponse): - """Communicate 'meta' events from server bots.""" + """ + + Similar to `Partial Response`. Yield this to communicate 'meta' events from server bots. + + #### Parameters: + - `suggested_replies`: Whether or not enable suggested replies. + - `content_type`: Used to describe the format of the response. The currently supported values + are `text/plain` and `text/markdown`. + - `refetch_settings`: Used to trigger a settings fetch request from Poe. A more robust way + to trigger this is documented at: https://creator.poe.com/docs/updating-bot-settings + + """ - linkify: bool = True + linkify: bool = True # deprecated suggested_replies: bool = True content_type: ContentType = "text/markdown" refetch_settings: bool = False From 0aa8bb0da01d0d1e5704c1d5bd0b7a143451bd45 Mon Sep 17 00:00:00 2001 From: Anmol Singh <5581466+anmolsingh95@users.noreply.github.com> Date: Fri, 5 Apr 2024 14:54:11 -0700 Subject: [PATCH 2/6] fix typos in documentation --- docs/api_reference.md | 118 +++++++++++++++++---------------- docs/generate_api_reference.py | 26 +++++--- pyproject.toml | 4 +- src/fastapi_poe/base.py | 54 ++++++++------- src/fastapi_poe/client.py | 18 ++--- src/fastapi_poe/types.py | 21 +++--- 6 files changed, 127 insertions(+), 114 deletions(-) diff --git a/docs/api_reference.md b/docs/api_reference.md index 9d258ff..fccf668 100644 --- a/docs/api_reference.md +++ b/docs/api_reference.md @@ -1,10 +1,10 @@ The following it the API reference for the [fastapi_poe](https://github.com/poe-platform/fastapi_poe) client library. -# `class PoeBot` +## `fastapi_poe.PoeBot` -The class that you use to define your bot behavior.Once you define your PoeBot class, -you pass it to make_run to serve that bot. +The class that you use to define your bot behavior. Once you define your PoeBot class, +you pass it to `make_app` to create a FastAPI app that serves your bot. #### Parameters: @@ -12,32 +12,32 @@ you pass it to make_run to serve that bot. this is something you can adjust. This is especially useful if you want to serve multiple bots from one server. - `access_key`: This is the access key for your bot and when provided is used to - validate that requests are coming from a trusted source. This access key should be the - same one that you provide when integrating your bot with Poe at: + validate that the requests are coming from a trusted source. This access key should be + the same one that you provide when integrating your bot with Poe at: https://poe.com/create_bot?server=1. You can also set this to None but certain - features like file-output that mandate an `access_key` will not be available to your + features like file output that mandate an `access_key` will not be available for your bot. - `concat_attachments_to_message`: A flag to decide whether to parse out content from attachments and concatenate it to the conversation message. This is set to `True` by - default and we recommend leaving it at that for most developers since it allows your - bot to comprehent attachments uploaded by users by default. + default and we recommend leaving on since it allows your bot to comprehend attachments + uploaded by users by default. ### `PoeBot.get_response` -Override this to return a response to user queries. +Override this to define your bot's response given a user query. ### `PoeBot.get_response_with_context` -A version of `get_response` that also passes in the request context information. By +A version of `get_response` that also includes the request context information. By default, this will call `get_response`. ### `PoeBot.get_settings` -Override this to return non-standard settings. +Override this to define your bot's settings. ### `PoeBot.get_settings_with_context` -A version of `get_settings` that also passes in the request context information. By +A version of `get_settings` that also includes the request context information. By default, this will call `get_settings`. ### `PoeBot.on_feedback` @@ -46,7 +46,7 @@ Override this to record feedback from the user. ### `PoeBot.on_feedback_with_context` -A version of `on_feedback` that also passes in the request context information. By +A version of `on_feedback` that also includes the request context information. By default, this will call `on_feedback`. ### `PoeBot.on_error` @@ -55,32 +55,33 @@ Override this to record errors from the Poe server. ### `PoeBot.on_error_with_context` -A version of `on_error` that also passes in the request context information. By default, +A version of `on_error` that also includes the request context information. By default, this will call `on_error`. ### `PoeBot.post_message_attachment` -Used to output an attachment in the response. +Used to output an attachment in your bot's response. #### Parameters: -- `message_id` (str): the message id associated with the currentQueryRequest object - being processed. **Important**: This must be the request that is currently being - handled by get_response. Attempting to attach files to previously handled requests - will fail. -- `download_url` (str): provide a url to a file that will be attached to the message. -- `file_data` (bytes): the contents of the file to be uploaded. This should be a - bytes-like or file object. -- `filename` - the name of the file to be attached. +- `message_id`: The message id associated with the current QueryRequest object. + **Important**: This must be the request that is currently being handled by + get_response. Attempting to attach files to previously handled requests will fail. +- `download_url`: A url to the file to be attached to the message. +- `file_data`: The contents of the file to be uploaded. This should be a bytes-like or + file object. +- `filename`: The name of the file to be attached. **Note**: You need to provide either the `download_url` or both of `file_data` and `filename`. ### `PoeBot.concat_attachment_content_to_message_body` -Concatenate received attachment file content into the message body. +Concatenate received attachment file content into the message body. This will be called +by default if `concat_attachments_to_message` is set to `True` but can also be used +manually if needed. -# `def make_app` +## `fastapi_poe.make_app` Create an app object for your bot(s). @@ -92,24 +93,24 @@ Create an app object for your bot(s). POE_ACCESS_KEY environment variable. If that is not set, the server will refuse to start, unless `allow_without_key` is True. If multiple bots are provided, the access key must be provided as part of the bot object. -- `api_key`: The previous name of access_key. This is not to be confused with the - api_key param needed by `stream_request`. This param is deprecated and will be removed - in a future version. -- allow_without_key`: If True, the server will start even if no access key is provided. +- `api_key`: The previous name for `access_key`. This is not to be confused with the + `api_key` param needed by `stream_request`. This param is deprecated and will be + removed in a future version. +- `allow_without_key`: If True, the server will start even if no access key is provided. Requests will not be checked against any key. If an access key is provided, it is still checked. - `app`: A FastAPI app instance. If provided, the app will be configured with the provided bots, access keys, and other settings. If not provided, a new FastAPI application instance will be created and configured. -# `def run` +## `fastapi_poe.run` -Run a Poe bot server using FastAPI. This function should be used when you are running -the bot locally. The arguments are the same as that for `make_app`. +Serve a poe bot using a FastAPI app. This function should be used when you are running +the bot locally. The arguments are the same as they are for `make_app`. -# `def stream_request` +## `fastapi_poe.stream_request` -Entry point for the Bot Query API. This API allows you to use other bots on Poe for +The Entry point for the Bot Query API. This API allows you to use other bots on Poe for inference in response to a user message. For more details, checkout: https://creator.poe.com/docs/accessing-other-bots-on-poe @@ -119,10 +120,11 @@ https://creator.poe.com/docs/accessing-other-bots-on-poe includes information needed to identify the user for compute point usage. - `bot_name`: The bot you want to invoke. - `api_key`: Your Poe API key, available at poe.com/api_key. You will need this in case - you are trying to use this function from a script/shell. compute points may apply - depending on the bot you call. + you are trying to use this function from a script/shell. Note that if an `api_key` is + provided, compute points will be charged on the account corresponding to the + `api_key`. -# `def get_bot_response` +## `fastapi_poe.get_bot_response` Use this function to invoke another Poe bot from your shell. @@ -130,12 +132,13 @@ Use this function to invoke another Poe bot from your shell. - `messages`: A list of protocol messages representing your conversation - `bot_name`: The bot that you want to invoke. -- `api_key`: Your Poe API key. This is available at: poe.com/api_key +- `api_key`: Your Poe API key. This is available at: + [poe.com/api_key](https://poe.com/api_key) -# `def get_final_response` +## `fastapi_poe.get_final_response` -Another helper function for you to be able to use the bot query API. Waits for all the -tokens and concatenates the full response before returning. +A helper function for the bot query API that waits for all the tokens and concatenates +the full response before returning. #### Parameters: @@ -143,28 +146,29 @@ tokens and concatenates the full response before returning. includes information needed to identify the user for compute point usage. - `bot_name`: The bot you want to invoke. - `api_key`: Your Poe API key, available at poe.com/api_key. You will need this in case - you are trying to use this function from a script/shell. compute points may apply - depending on the bot you call. + you are trying to use this function from a script/shell. Note that if an `api_key` is + provided, compute points will be charged on the account corresponding to the + `api_key`. -# `class PartialResponse` +## `fastapi_poe.PartialResponse` -Representation of a (possibly partial) response from a bot. Yield this in your -`PoeBot.get_response` or `PoeBot.get_response_with_context` while setting values -appropriately to communicate your response to Poe. +Representation of a (possibly partial) response from a bot. Yield this in +`PoeBot.get_response` or `PoeBot.get_response_with_context` to communicate your response +to Poe. #### Parameters: -- `text`: The actual text you want to display to the user. Note that this solely be the - text in the next token as Poe will automatically concatenate all tokens before - displaying the response to the user. -- `data`: Used to send json data to Poe. This is currently only used in the context of +- `text`: The actual text you want to display to the user. Note that this should solely + be the text in the next token since Poe will automatically concatenate all tokens + before displaying the response to the user. +- `data`: Used to send arbitrary json data to Poe. This is currently only used for OpenAI function calling. - `is_suggested_reply`: Seting this to true will create a suggested reply with the - provided text string. + provided text value. - `is_replace_response`: Setting this to true will clear out the previously displayed - text to the user and replace it with the provided str. + text to the user and replace it with the provided text value. -# `class ErrorResponse` +## `fastapi_poe.ErrorResponse` Similar to `PartialResponse`. Yield this to communicate errors from your bot. @@ -173,13 +177,13 @@ Similar to `PartialResponse`. Yield this to communicate errors from your bot. - `allow_retry`: Whether or not to allow a user to retry on error. - `error_type`: An enum indicating what error to display. -# `class MetaResponse` +## `fastapi_poe.MetaResponse` -Similar to `Partial Response`. Yield this to communicate 'meta' events from server bots. +Similar to `Partial Response`. Yield this to communicate `meta` events from server bots. #### Parameters: -- `suggested_replies`: Whether or not enable suggested replies. +- `suggested_replies`: Whether or not to enable suggested replies. - `content_type`: Used to describe the format of the response. The currently supported values are `text/plain` and `text/markdown`. - `refetch_settings`: Used to trigger a settings fetch request from Poe. A more robust diff --git a/docs/generate_api_reference.py b/docs/generate_api_reference.py index 4ccbf06..3684e4e 100644 --- a/docs/generate_api_reference.py +++ b/docs/generate_api_reference.py @@ -1,7 +1,11 @@ import inspect import sys +import types from dataclasses import dataclass, field -from typing import Dict, List, Optional +from typing import Callable, Dict, List, Optional, Union + +sys.path.append("../src") +import fastapi_poe INITIAL_TEXT = """ @@ -19,7 +23,7 @@ class DocumentationData: children: List = field(default_factory=lambda: []) -def _unwrap_func(func_obj): +def _unwrap_func(func_obj: Union[staticmethod, Callable]) -> Callable: """This is to ensure we get the docstring from the actual func and not a decorator.""" if isinstance(func_obj, staticmethod): return _unwrap_func(func_obj.__func__) @@ -28,7 +32,9 @@ def _unwrap_func(func_obj): return func_obj -def get_documentation_data(*, module, documented_items) -> Dict[str, DocumentationData]: +def get_documentation_data( + *, module: types.ModuleType, documented_items: List[str] +) -> Dict[str, DocumentationData]: data_dict = {} for name, obj in inspect.getmembers(module): if ( @@ -58,8 +64,11 @@ def get_documentation_data(*, module, documented_items) -> Dict[str, Documentati def generate_documentation( - *, data_dict: Dict[str, DocumentationData], documented_items, output_filename -): + *, + data_dict: Dict[str, DocumentationData], + documented_items: List[str], + output_filename: str, +) -> None: # reset the file first with open(output_filename, "w") as f: f.write("") @@ -69,8 +78,7 @@ def generate_documentation( for item in documented_items: item_data = data_dict[item] - prefix = "class" if item_data.data_type == "class" else "def" - f.write(f"# `{prefix} {item_data.name}`\n\n") + f.write(f"## `fastapi_poe.{item_data.name}`\n\n") f.write(f"{item_data.docstring}\n\n") for child in item_data.children: if not child.docstring: @@ -80,10 +88,6 @@ def generate_documentation( f.write("\n\n") -# Usage example -sys.path.append("../src") -import fastapi_poe - # Specify the names of classes and functions to document documented_items = [ "PoeBot", diff --git a/pyproject.toml b/pyproject.toml index 80061ea..9117f98 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,7 +39,7 @@ target-version = ['py37'] skip-magic-trailing-comma = true [tool.ruff] -select = [ +lint.select = [ "F", "E", "I", # import sorting @@ -69,7 +69,7 @@ select = [ "PERF101", # unnecessary list() calls that can be replaced with literals ] -ignore = [ +lint.ignore = [ "B008", # do not perform function calls in argument defaults "ANN101", # missing type annotation for self in method "ANN102", # missing type annotation for cls in classmethod diff --git a/src/fastapi_poe/base.py b/src/fastapi_poe/base.py index 8ca66e4..534b188 100644 --- a/src/fastapi_poe/base.py +++ b/src/fastapi_poe/base.py @@ -107,22 +107,22 @@ async def http_exception_handler(request: Request, ex: Exception) -> Response: class PoeBot: """ - The class that you use to define your bot behavior.Once you define your PoeBot class, you - pass it to make_run to serve that bot. + The class that you use to define your bot behavior. Once you define your PoeBot class, you + pass it to `make_app` to create a FastAPI app that serves your bot. #### Parameters: - `path`: This is the path at which your bot is served. By default, it's set to "/" but this is something you can adjust. This is especially useful if you want to serve multiple bots from one server. - - `access_key`: This is the access key for your bot and when provided is used to - validate that requests are coming from a trusted source. This access key should be the same + - `access_key`: This is the access key for your bot and when provided is used to validate + that the requests are coming from a trusted source. This access key should be the same one that you provide when integrating your bot with Poe at: https://poe.com/create_bot?server=1. You can also set this to None but certain features like - file-output that mandate an `access_key` will not be available to your bot. + file output that mandate an `access_key` will not be available for your bot. - `concat_attachments_to_message`: A flag to decide whether to parse out content from attachments and concatenate it to the conversation message. This is set to `True` by default - and we recommend leaving it at that for most developers since it allows your bot to - comprehent attachments uploaded by users by default. + and we recommend leaving on since it allows your bot to comprehend attachments uploaded by + users by default. """ @@ -137,7 +137,7 @@ class PoeBot: async def get_response( self, request: QueryRequest ) -> AsyncIterable[Union[PartialResponse, ServerSentEvent]]: - """Override this to return a response to user queries.""" + """Override this to define your bot's response given a user query.""" yield self.text_event("hello") async def get_response_with_context( @@ -145,7 +145,7 @@ async def get_response_with_context( ) -> AsyncIterable[Union[PartialResponse, ServerSentEvent]]: """ - A version of `get_response` that also passes in the request context information. By + A version of `get_response` that also includes the request context information. By default, this will call `get_response`. """ @@ -154,7 +154,7 @@ async def get_response_with_context( yield event async def get_settings(self, setting: SettingsRequest) -> SettingsResponse: - """Override this to return non-standard settings.""" + """Override this to define your bot's settings.""" return SettingsResponse() async def get_settings_with_context( @@ -162,7 +162,7 @@ async def get_settings_with_context( ) -> SettingsResponse: """ - A version of `get_settings` that also passes in the request context information. By + A version of `get_settings` that also includes the request context information. By default, this will call `get_settings`. """ @@ -178,7 +178,7 @@ async def on_feedback_with_context( ) -> None: """ - A version of `on_feedback` that also passes in the request context information. By + A version of `on_feedback` that also includes the request context information. By default, this will call `on_feedback`. """ @@ -193,7 +193,7 @@ async def on_error_with_context( ) -> None: """ - A version of `on_error` that also passes in the request context information. By + A version of `on_error` that also includes the request context information. By default, this will call `on_error`. """ @@ -247,16 +247,16 @@ async def post_message_attachment( ) -> AttachmentUploadResponse: """ - Used to output an attachment in the response. + Used to output an attachment in your bot's response. #### Parameters: - - `message_id` (str): the message id associated with the currentQueryRequest object being - processed. **Important**: This must be the request that is currently being handled by + - `message_id`: The message id associated with the current QueryRequest object. + **Important**: This must be the request that is currently being handled by get_response. Attempting to attach files to previously handled requests will fail. - - `download_url` (str): provide a url to a file that will be attached to the message. - - `file_data` (bytes): the contents of the file to be uploaded. This should be a + - `download_url`: A url to the file to be attached to the message. + - `file_data`: The contents of the file to be uploaded. This should be a bytes-like or file object. - - `filename` - the name of the file to be attached. + - `filename`: The name of the file to be attached. **Note**: You need to provide either the `download_url` or both of `file_data` and `filename`. @@ -376,7 +376,13 @@ async def _process_pending_attachment_requests( def concat_attachment_content_to_message_body( self, query_request: QueryRequest ) -> QueryRequest: - """Concatenate received attachment file content into the message body.""" + """ + + Concatenate received attachment file content into the message body. This will be called + by default if `concat_attachments_to_message` is set to `True` but can also be used + manually if needed. + + """ last_message = query_request.query[-1] concatenated_content = last_message.content for attachment in last_message.attachments: @@ -666,10 +672,10 @@ def make_app( the POE_ACCESS_KEY environment variable. If that is not set, the server will refuse to start, unless `allow_without_key` is True. If multiple bots are provided, the access key must be provided as part of the bot object. - - `api_key`: The previous name of access_key. This is not to be confused with the api_key + - `api_key`: The previous name for `access_key`. This is not to be confused with the `api_key` param needed by `stream_request`. This param is deprecated and will be removed in a future version. - - allow_without_key`: If True, the server will start even if no access key is provided. + - `allow_without_key`: If True, the server will start even if no access key is provided. Requests will not be checked against any key. If an access key is provided, it is still checked. - `app`: A FastAPI app instance. If provided, the app will be configured with the provided bots, access keys, and other settings. If not provided, a new FastAPI application instance will be @@ -734,8 +740,8 @@ def run( ) -> None: """ - Run a Poe bot server using FastAPI. This function should be used when you are running the - bot locally. The arguments are the same as that for `make_app`. + Serve a poe bot using a FastAPI app. This function should be used when you are running the + bot locally. The arguments are the same as they are for `make_app`. """ diff --git a/src/fastapi_poe/client.py b/src/fastapi_poe/client.py index 33d32fa..bb0815e 100644 --- a/src/fastapi_poe/client.py +++ b/src/fastapi_poe/client.py @@ -309,8 +309,8 @@ async def stream_request( ) -> AsyncGenerator[BotMessage, None]: """ - Entry point for the Bot Query API. This API allows you to use other bots on Poe for inference - in response to a user message. For more details, checkout: + The Entry point for the Bot Query API. This API allows you to use other bots on Poe for + inference in response to a user message. For more details, checkout: https://creator.poe.com/docs/accessing-other-bots-on-poe #### Parameters: @@ -318,8 +318,8 @@ async def stream_request( information needed to identify the user for compute point usage. - `bot_name`: The bot you want to invoke. - `api_key`: Your Poe API key, available at poe.com/api_key. You will need this in case you are - trying to use this function from a script/shell. compute points may apply depending on the bot - you call. + trying to use this function from a script/shell. Note that if an `api_key` is provided, + compute points will be charged on the account corresponding to the `api_key`. """ tool_calls = None @@ -520,7 +520,7 @@ def get_bot_response( #### Parameters: - `messages`: A list of protocol messages representing your conversation - `bot_name`: The bot that you want to invoke. - - `api_key`: Your Poe API key. This is available at: poe.com/api_key + - `api_key`: Your Poe API key. This is available at: [poe.com/api_key](https://poe.com/api_key) """ additional_params = {} @@ -568,16 +568,16 @@ async def get_final_response( ) -> str: """ - Another helper function for you to be able to use the bot query API. Waits for all the tokens - and concatenates the full response before returning. + A helper function for the bot query API that waits for all the tokens and concatenates the full + response before returning. #### Parameters: - `request`: A QueryRequest object representing a query from Poe. This object also includes information needed to identify the user for compute point usage. - `bot_name`: The bot you want to invoke. - `api_key`: Your Poe API key, available at poe.com/api_key. You will need this in case you are - trying to use this function from a script/shell. compute points may apply depending on the bot - you call. + trying to use this function from a script/shell. Note that if an `api_key` is provided, + compute points will be charged on the account corresponding to the `api_key`. """ chunks: List[str] = [] diff --git a/src/fastapi_poe/types.py b/src/fastapi_poe/types.py index cd1afc2..a51628f 100644 --- a/src/fastapi_poe/types.py +++ b/src/fastapi_poe/types.py @@ -106,20 +106,19 @@ class AttachmentUploadResponse(BaseModel): class PartialResponse(BaseModel): """ - Representation of a (possibly partial) response from a bot. Yield this in your - `PoeBot.get_response` or `PoeBot.get_response_with_context` while setting values - appropriately to communicate your response to Poe. + Representation of a (possibly partial) response from a bot. Yield this in + `PoeBot.get_response` or `PoeBot.get_response_with_context` to communicate your response to Poe. #### Parameters: - - `text`: The actual text you want to display to the user. Note that this solely - be the text in the next token as Poe will automatically concatenate all tokens before + - `text`: The actual text you want to display to the user. Note that this should solely + be the text in the next token since Poe will automatically concatenate all tokens before displaying the response to the user. - - `data`: Used to send json data to Poe. This is currently only used in the context - of OpenAI function calling. + - `data`: Used to send arbitrary json data to Poe. This is currently only used for OpenAI + function calling. - `is_suggested_reply`: Seting this to true will create a suggested reply with the provided - text string. + text value. - `is_replace_response`: Setting this to true will clear out the previously displayed text - to the user and replace it with the provided str. + to the user and replace it with the provided text value. """ @@ -174,10 +173,10 @@ class ErrorResponse(PartialResponse): class MetaResponse(PartialResponse): """ - Similar to `Partial Response`. Yield this to communicate 'meta' events from server bots. + Similar to `Partial Response`. Yield this to communicate `meta` events from server bots. #### Parameters: - - `suggested_replies`: Whether or not enable suggested replies. + - `suggested_replies`: Whether or not to enable suggested replies. - `content_type`: Used to describe the format of the response. The currently supported values are `text/plain` and `text/markdown`. - `refetch_settings`: Used to trigger a settings fetch request from Poe. A more robust way From 4f5627030db1fad3a3c65a31bef522d5b041bb7c Mon Sep 17 00:00:00 2001 From: Anmol Singh <5581466+anmolsingh95@users.noreply.github.com> Date: Fri, 5 Apr 2024 15:29:23 -0700 Subject: [PATCH 3/6] fix formatting a bit and add mdignore --- docs/api_reference.md | 157 ++++++++++++++++----------------- docs/generate_api_reference.py | 2 +- 2 files changed, 79 insertions(+), 80 deletions(-) diff --git a/docs/api_reference.md b/docs/api_reference.md index fccf668..c027292 100644 --- a/docs/api_reference.md +++ b/docs/api_reference.md @@ -1,26 +1,25 @@ -The following it the API reference for the -[fastapi_poe](https://github.com/poe-platform/fastapi_poe) client library. + + +The following it the API reference for the [fastapi_poe](https://github.com/poe-platform/fastapi_poe) client library. ## `fastapi_poe.PoeBot` -The class that you use to define your bot behavior. Once you define your PoeBot class, -you pass it to `make_app` to create a FastAPI app that serves your bot. +The class that you use to define your bot behavior. Once you define your PoeBot class, you +pass it to `make_app` to create a FastAPI app that serves your bot. #### Parameters: - -- `path`: This is the path at which your bot is served. By default, it's set to "/" but - this is something you can adjust. This is especially useful if you want to serve - multiple bots from one server. -- `access_key`: This is the access key for your bot and when provided is used to - validate that the requests are coming from a trusted source. This access key should be - the same one that you provide when integrating your bot with Poe at: - https://poe.com/create_bot?server=1. You can also set this to None but certain - features like file output that mandate an `access_key` will not be available for your - bot. +- `path`: This is the path at which your bot is served. By default, it's set to "/" +but this is something you can adjust. This is especially useful if you want to serve +multiple bots from one server. +- `access_key`: This is the access key for your bot and when provided is used to validate +that the requests are coming from a trusted source. This access key should be the same +one that you provide when integrating your bot with Poe at: +https://poe.com/create_bot?server=1. You can also set this to None but certain features like +file output that mandate an `access_key` will not be available for your bot. - `concat_attachments_to_message`: A flag to decide whether to parse out content from - attachments and concatenate it to the conversation message. This is set to `True` by - default and we recommend leaving on since it allows your bot to comprehend attachments - uploaded by users by default. +attachments and concatenate it to the conversation message. This is set to `True` by default +and we recommend leaving on since it allows your bot to comprehend attachments uploaded by +users by default. ### `PoeBot.get_response` @@ -55,21 +54,20 @@ Override this to record errors from the Poe server. ### `PoeBot.on_error_with_context` -A version of `on_error` that also includes the request context information. By default, -this will call `on_error`. +A version of `on_error` that also includes the request context information. By +default, this will call `on_error`. ### `PoeBot.post_message_attachment` Used to output an attachment in your bot's response. #### Parameters: - - `message_id`: The message id associated with the current QueryRequest object. - **Important**: This must be the request that is currently being handled by - get_response. Attempting to attach files to previously handled requests will fail. +**Important**: This must be the request that is currently being handled by +get_response. Attempting to attach files to previously handled requests will fail. - `download_url`: A url to the file to be attached to the message. -- `file_data`: The contents of the file to be uploaded. This should be a bytes-like or - file object. +- `file_data`: The contents of the file to be uploaded. This should be a +bytes-like or file object. - `filename`: The name of the file to be attached. **Note**: You need to provide either the `download_url` or both of `file_data` and @@ -81,32 +79,35 @@ Concatenate received attachment file content into the message body. This will be by default if `concat_attachments_to_message` is set to `True` but can also be used manually if needed. + + ## `fastapi_poe.make_app` Create an app object for your bot(s). #### Parameters: - -- `bot`: A bot object or a list of bot objects if you want to host multiple bots on one - server. -- `access_key`: The access key to use. If not provided, the server tries to read the - POE_ACCESS_KEY environment variable. If that is not set, the server will refuse to - start, unless `allow_without_key` is True. If multiple bots are provided, the access - key must be provided as part of the bot object. -- `api_key`: The previous name for `access_key`. This is not to be confused with the - `api_key` param needed by `stream_request`. This param is deprecated and will be - removed in a future version. +- `bot`: A bot object or a list of bot objects if you want to host multiple bots on one server. +- `access_key`: The access key to use. If not provided, the server tries to read +the POE_ACCESS_KEY environment variable. If that is not set, the server will +refuse to start, unless `allow_without_key` is True. If multiple bots are provided, +the access key must be provided as part of the bot object. +- `api_key`: The previous name for `access_key`. This is not to be confused with the `api_key` +param needed by `stream_request`. This param is deprecated and will be removed in a future +version. - `allow_without_key`: If True, the server will start even if no access key is provided. - Requests will not be checked against any key. If an access key is provided, it is - still checked. -- `app`: A FastAPI app instance. If provided, the app will be configured with the - provided bots, access keys, and other settings. If not provided, a new FastAPI - application instance will be created and configured. +Requests will not be checked against any key. If an access key is provided, it is still checked. +- `app`: A FastAPI app instance. If provided, the app will be configured with the provided bots, +access keys, and other settings. If not provided, a new FastAPI application instance will be +created and configured. + + ## `fastapi_poe.run` -Serve a poe bot using a FastAPI app. This function should be used when you are running -the bot locally. The arguments are the same as they are for `make_app`. +Serve a poe bot using a FastAPI app. This function should be used when you are running the +bot locally. The arguments are the same as they are for `make_app`. + + ## `fastapi_poe.stream_request` @@ -115,77 +116,75 @@ inference in response to a user message. For more details, checkout: https://creator.poe.com/docs/accessing-other-bots-on-poe #### Parameters: - -- `request`: A QueryRequest object representing a query from Poe. This object also - includes information needed to identify the user for compute point usage. +- `request`: A QueryRequest object representing a query from Poe. This object also includes +information needed to identify the user for compute point usage. - `bot_name`: The bot you want to invoke. -- `api_key`: Your Poe API key, available at poe.com/api_key. You will need this in case - you are trying to use this function from a script/shell. Note that if an `api_key` is - provided, compute points will be charged on the account corresponding to the - `api_key`. +- `api_key`: Your Poe API key, available at poe.com/api_key. You will need this in case you are +trying to use this function from a script/shell. Note that if an `api_key` is provided, +compute points will be charged on the account corresponding to the `api_key`. + + ## `fastapi_poe.get_bot_response` Use this function to invoke another Poe bot from your shell. - #### Parameters: - - `messages`: A list of protocol messages representing your conversation - `bot_name`: The bot that you want to invoke. -- `api_key`: Your Poe API key. This is available at: - [poe.com/api_key](https://poe.com/api_key) +- `api_key`: Your Poe API key. This is available at: [poe.com/api_key](https://poe.com/api_key) + + ## `fastapi_poe.get_final_response` -A helper function for the bot query API that waits for all the tokens and concatenates -the full response before returning. +A helper function for the bot query API that waits for all the tokens and concatenates the full +response before returning. #### Parameters: - -- `request`: A QueryRequest object representing a query from Poe. This object also - includes information needed to identify the user for compute point usage. +- `request`: A QueryRequest object representing a query from Poe. This object also includes +information needed to identify the user for compute point usage. - `bot_name`: The bot you want to invoke. -- `api_key`: Your Poe API key, available at poe.com/api_key. You will need this in case - you are trying to use this function from a script/shell. Note that if an `api_key` is - provided, compute points will be charged on the account corresponding to the - `api_key`. +- `api_key`: Your Poe API key, available at poe.com/api_key. You will need this in case you are +trying to use this function from a script/shell. Note that if an `api_key` is provided, +compute points will be charged on the account corresponding to the `api_key`. + + ## `fastapi_poe.PartialResponse` Representation of a (possibly partial) response from a bot. Yield this in -`PoeBot.get_response` or `PoeBot.get_response_with_context` to communicate your response -to Poe. +`PoeBot.get_response` or `PoeBot.get_response_with_context` to communicate your response to Poe. #### Parameters: - - `text`: The actual text you want to display to the user. Note that this should solely - be the text in the next token since Poe will automatically concatenate all tokens - before displaying the response to the user. -- `data`: Used to send arbitrary json data to Poe. This is currently only used for - OpenAI function calling. -- `is_suggested_reply`: Seting this to true will create a suggested reply with the - provided text value. -- `is_replace_response`: Setting this to true will clear out the previously displayed - text to the user and replace it with the provided text value. +be the text in the next token since Poe will automatically concatenate all tokens before +displaying the response to the user. +- `data`: Used to send arbitrary json data to Poe. This is currently only used for OpenAI +function calling. +- `is_suggested_reply`: Seting this to true will create a suggested reply with the provided +text value. +- `is_replace_response`: Setting this to true will clear out the previously displayed text +to the user and replace it with the provided text value. + + ## `fastapi_poe.ErrorResponse` Similar to `PartialResponse`. Yield this to communicate errors from your bot. #### Parameters: - - `allow_retry`: Whether or not to allow a user to retry on error. - `error_type`: An enum indicating what error to display. + + ## `fastapi_poe.MetaResponse` Similar to `Partial Response`. Yield this to communicate `meta` events from server bots. #### Parameters: - - `suggested_replies`: Whether or not to enable suggested replies. -- `content_type`: Used to describe the format of the response. The currently supported - values are `text/plain` and `text/markdown`. -- `refetch_settings`: Used to trigger a settings fetch request from Poe. A more robust - way to trigger this is documented at: - https://creator.poe.com/docs/updating-bot-settings +- `content_type`: Used to describe the format of the response. The currently supported values +are `text/plain` and `text/markdown`. +- `refetch_settings`: Used to trigger a settings fetch request from Poe. A more robust way +to trigger this is documented at: https://creator.poe.com/docs/updating-bot-settings diff --git a/docs/generate_api_reference.py b/docs/generate_api_reference.py index 3684e4e..b96d7bb 100644 --- a/docs/generate_api_reference.py +++ b/docs/generate_api_reference.py @@ -9,7 +9,7 @@ INITIAL_TEXT = """ -The following it the API reference for the +The following it the API reference for the \ [fastapi_poe](https://github.com/poe-platform/fastapi_poe) client library. """ From 3628032e94632400973785cd35f802653b5149df Mon Sep 17 00:00:00 2001 From: Anmol Singh <5581466+anmolsingh95@users.noreply.github.com> Date: Fri, 5 Apr 2024 15:32:37 -0700 Subject: [PATCH 4/6] rename fastapi_poe to fp in the reference --- .prettierignore | 1 + docs/api_reference.md | 20 ++++++++++---------- docs/generate_api_reference.py | 5 +++-- 3 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..2e117bf --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +docs/*.md diff --git a/docs/api_reference.md b/docs/api_reference.md index c027292..080e577 100644 --- a/docs/api_reference.md +++ b/docs/api_reference.md @@ -1,8 +1,8 @@ -The following it the API reference for the [fastapi_poe](https://github.com/poe-platform/fastapi_poe) client library. +The following it the API reference for the [fastapi_poe](https://github.com/poe-platform/fastapi_poe) client library. The reference assumes that you used `import fastapi as fp`. -## `fastapi_poe.PoeBot` +## `fp.PoeBot` The class that you use to define your bot behavior. Once you define your PoeBot class, you pass it to `make_app` to create a FastAPI app that serves your bot. @@ -81,7 +81,7 @@ manually if needed. -## `fastapi_poe.make_app` +## `fp.make_app` Create an app object for your bot(s). @@ -102,14 +102,14 @@ created and configured. -## `fastapi_poe.run` +## `fp.run` Serve a poe bot using a FastAPI app. This function should be used when you are running the bot locally. The arguments are the same as they are for `make_app`. -## `fastapi_poe.stream_request` +## `fp.stream_request` The Entry point for the Bot Query API. This API allows you to use other bots on Poe for inference in response to a user message. For more details, checkout: @@ -125,7 +125,7 @@ compute points will be charged on the account corresponding to the `api_key`. -## `fastapi_poe.get_bot_response` +## `fp.get_bot_response` Use this function to invoke another Poe bot from your shell. #### Parameters: @@ -135,7 +135,7 @@ Use this function to invoke another Poe bot from your shell. -## `fastapi_poe.get_final_response` +## `fp.get_final_response` A helper function for the bot query API that waits for all the tokens and concatenates the full response before returning. @@ -150,7 +150,7 @@ compute points will be charged on the account corresponding to the `api_key`. -## `fastapi_poe.PartialResponse` +## `fp.PartialResponse` Representation of a (possibly partial) response from a bot. Yield this in `PoeBot.get_response` or `PoeBot.get_response_with_context` to communicate your response to Poe. @@ -168,7 +168,7 @@ to the user and replace it with the provided text value. -## `fastapi_poe.ErrorResponse` +## `fp.ErrorResponse` Similar to `PartialResponse`. Yield this to communicate errors from your bot. @@ -178,7 +178,7 @@ Similar to `PartialResponse`. Yield this to communicate errors from your bot. -## `fastapi_poe.MetaResponse` +## `fp.MetaResponse` Similar to `Partial Response`. Yield this to communicate `meta` events from server bots. diff --git a/docs/generate_api_reference.py b/docs/generate_api_reference.py index b96d7bb..5a5a4df 100644 --- a/docs/generate_api_reference.py +++ b/docs/generate_api_reference.py @@ -10,7 +10,8 @@ INITIAL_TEXT = """ The following it the API reference for the \ -[fastapi_poe](https://github.com/poe-platform/fastapi_poe) client library. +[fastapi_poe](https://github.com/poe-platform/fastapi_poe) client library. The reference assumes \ +that you used `import fastapi as fp`. """ @@ -78,7 +79,7 @@ def generate_documentation( for item in documented_items: item_data = data_dict[item] - f.write(f"## `fastapi_poe.{item_data.name}`\n\n") + f.write(f"## `fp.{item_data.name}`\n\n") f.write(f"{item_data.docstring}\n\n") for child in item_data.children: if not child.docstring: From 7f98f88358ce444aee94ff744121fd12e03efbe2 Mon Sep 17 00:00:00 2001 From: Anmol Singh <5581466+anmolsingh95@users.noreply.github.com> Date: Fri, 5 Apr 2024 15:42:07 -0700 Subject: [PATCH 5/6] document how to generate API reference --- CONTRIBUTING.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index df4b45e..8d10b8d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,6 +4,12 @@ - Pull requests should only be merged once all checks pass - The repo uses Black for formatting Python code, Prettier for formatting Markdown, Pyright for type-checking Python, and a few other tools +- To generate reference documentation: + - Add/update docstrings in the codebase. If you are adding a new class/function, add + it's name to `documented_items` in `docs/generate_api_reference.py` + - run `cd docs;python generate_api_reference.py` + - [Internal only] Copy the contents of `api_reference.md` to the reference page in + README. - To run the CI checks locally: - `pip install pre-commit` - `pre-commit run --all` (or `pre-commit install` to install the pre-commit hook) From b0cb29ed9cf3a10587b0a783e4196dffc3e34051 Mon Sep 17 00:00:00 2001 From: Anmol Singh <5581466+anmolsingh95@users.noreply.github.com> Date: Fri, 5 Apr 2024 15:48:43 -0700 Subject: [PATCH 6/6] fix pyright diagnostic issue --- docs/generate_api_reference.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/generate_api_reference.py b/docs/generate_api_reference.py index 5a5a4df..1fc40c5 100644 --- a/docs/generate_api_reference.py +++ b/docs/generate_api_reference.py @@ -25,11 +25,9 @@ class DocumentationData: def _unwrap_func(func_obj: Union[staticmethod, Callable]) -> Callable: - """This is to ensure we get the docstring from the actual func and not a decorator.""" + """Grab the underlying func_obj.""" if isinstance(func_obj, staticmethod): return _unwrap_func(func_obj.__func__) - if hasattr(func_obj, "__wrapped__"): - return _unwrap_func(func_obj.__wrapped__) return func_obj