poe-platform / server-bot-quick-start

Tutorial for Poe server bots
1.31k stars 192 forks source link

`function_calling_bot` example doesn't work #75

Closed ibehnam closed 7 months ago

ibehnam commented 7 months ago

I tried the function_calling_bot example but every time I send a request to the bot I get this in the terminal and the bot says " ran into a problem answering your request.".

Error in Poe bot: Bot request to GPT-3.5-Turbo failed on try 0
 BotError('{"allow_retry": true, "text": "Message m-000000xy92msflaxx6qefntpppd3e55u called on bot that has 0 or undefined call count in settings."}')
Error in Poe bot: Bot request to GPT-3.5-Turbo failed on try 1
 BotError('{"allow_retry": true, "text": "Message m-000000xy92msflaxx6qefntpppd3e55u called on bot that has 0 or undefined call count in settings."}')
ERROR:    Error responding to query
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/fastapi_poe/base.py", line 181, in handle_query
    async for event in self.get_response(request):
  File "/Users/behnam/Downloads/LLM/server-bot-quick-start/function_calling_bot.py", line 62, in get_response
    async for msg in fp.stream_request(
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/fastapi_poe/client.py", line 310, in stream_request
    tool_calls = await _get_tool_calls(
                 ^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/fastapi_poe/client.py", line 382, in _get_tool_calls
    async for message in stream_request_base(
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/fastapi_poe/client.py", line 455, in stream_request_base
    async for message in ctx.perform_query_request(
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/fastapi_poe/client.py", line 224, in perform_query_request
    raise BotError(event.data)
fastapi_poe.client.BotError: {"allow_retry": true, "text": "Message m-000000xy92msflaxx6qefntpppd3e55u called on bot that has 0 or undefined call count in settings."}

I didn't change the code of the file, except for running it locally instead of on Modal (but I made sure my local server was reachable by trying the echobot example first).

My Poe subscription just ended. Could it be because of that? (Although, GPT-3.5-Turbo is free).

ibehnam commented 7 months ago

Here's the code for reference:

"""

Sample bot that demonstrates how to use OpenAI function calling with the Poe API.

"""

from __future__ import annotations

import json
from typing import AsyncIterable

import fastapi_poe as fp
# from modal import Image, Stub, asgi_app
from fastapi import FastAPI

app = FastAPI()

def get_current_weather(location, unit="fahrenheit"):
    """Get the current weather in a given location"""
    if "tokyo" in location.lower():
        return json.dumps({"location": "Tokyo", "temperature": "11", "unit": unit})
    elif "san francisco" in location.lower():
        return json.dumps(
            {"location": "San Francisco", "temperature": "72", "unit": unit}
        )
    elif "paris" in location.lower():
        return json.dumps({"location": "Paris", "temperature": "22", "unit": unit})
    else:
        return json.dumps({"location": location, "temperature": "unknown"})

tools_executables = [get_current_weather]

tools_dict_list = [
    {
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA",
                    },
                    "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                },
                "required": ["location"],
            },
        },
    }
]
tools = [fp.ToolDefinition(**tools_dict) for tools_dict in tools_dict_list]

class GPT35FunctionCallingBot(fp.PoeBot):
    async def get_response(
        self, request: fp.QueryRequest
    ) -> AsyncIterable[fp.PartialResponse]:
        async for msg in fp.stream_request(
            request,
            "GPT-3.5-Turbo",
            request.access_key,
            tools=tools,
            tool_executables=tools_executables,
        ):
            yield msg

    async def get_settings(self, setting: fp.SettingsRequest) -> fp.SettingsResponse:
        return fp.SettingsResponse(server_bot_dependencies={"GPT-3.5-Turbo": 2})

# REQUIREMENTS = ["fastapi-poe==0.0.25"]
# image = Image.debian_slim().pip_install(*REQUIREMENTS)
# stub = Stub("function-calling-poe")

# @stub.function(image=image)
# @asgi_app()

@app.get("/")
def fastapi_app():
    bot = GPT35FunctionCallingBot()
    # Optionally, provide your Poe access key here:
    # 1. You can go to https://poe.com/create_bot?server=1 to generate an access key.
    # 2. We strongly recommend using a key for a production bot to prevent abuse,
    # but the starter examples disable the key check for convenience.
    # 3. You can also store your access key on modal.com and retrieve it in this function
    # by following the instructions at: https://modal.com/docs/guide/secrets
    # POE_ACCESS_KEY = ""
    app = fp.make_app(bot, access_key="<my access key>")
    # app = fp.make_app(bot, allow_without_key=True)
    return app
anmolsingh95 commented 7 months ago

Hi Behnam. I think this might be due to your dependency settings not having been synced. It's mentioned in the documentation page (see screenshot below) but easy to miss. Can you please try this out and share whether it addresses the issue for you?

image
ibehnam commented 7 months ago

@anmolsingh95 Thanks, that solved the issue!