Repository: LAION-AI/Open-Assistant
File: inference/server/oasst_inference_server/plugins/web_retriever/main.py (Python)

Code before:
        text_yaml = f"text_content: {MULTILINE_SYM}\n"
        for line in text.split("\n"):
            text_yaml += f"  {line}\n"

        images_yaml = "images:\n" if len(images) > 0 else ""
        for image in images:
            images_yaml += f"- {image}\n"

        yaml_text = f"{text_yaml}\n{images_yaml}"
        text = f"""{yaml_text}
Thought: I now know the answer{IMAGES_SUFIX if len(images) > 0 else "."}
"""
        return Response(content=text, media_type="text/plain")

    except Exception as e:
        logger.opt(exception=True).debug("web_retriever GET failed:")
        error_message = f"Sorry, the url is not available. {e}\nYou should report this message to the user!"
        return JSONResponse(content={"error": error_message}, status_code=500)


@app.get("/icon.png", include_in_schema=False)
async def api_icon():
    icon_path = folder_path / "icon.png"
    with icon_path.open("rb") as f:
        icon = f.read()
    return Response(content=icon, media_type="image/png")


@app.get("/ai-plugin.json", include_in_schema=False)
async def api_ai_plugin(request: Request):
    json_path = folder_path / "ai-plugin.json"
    with json_path.open("r") as f:
        ai_plugin_json = json.load(f)

    base_url, _, _ = request.url._url.rpartition("/")
    ai_plugin_json["logo_url"] = base_url + "/icon.png"
    ai_plugin_json["api"]["url"] = base_url + "/openapi.json"

    return Response(content=json.dumps(ai_plugin_json), media_type="application/json")


def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    openapi_schema = get_openapi(
        title="Web Retriever",
        version="0.1",
        routes=app.routes,
    )
    openapi_schema["tags"] = [
        {

Review comment (suggestion):
would it be simpler to:
```py
from starlette.responses import FileResponse 


@app.get("/icon.png", include_in_schema=False)
async def api_icon():
    return FileResponse(folder_path / "icon.png")
```

Code after:
            error_message = f"Sorry, unsupported content type '{content_type}' at {url}.\nYou should report this message to the user!"
            return JSONResponse(content={"error": error_message}, status_code=500)

        images = [f"http:{url}" if url.startswith("//") else url for url in images]
        images = limit_image_count(images, max_chars=IMAGES_CHAR_LIMIT)

        if len(text) > CHAR_LIMIT:
            text = text[:CHAR_LIMIT]

        MULTILINE_SYM = "|" if content_type != "applicaion/json" else ""
        text_yaml = f"text_content: {MULTILINE_SYM}\n"
        for line in text.split("\n"):
            text_yaml += f"  {line}\n"

        images_yaml = "images:\n" if len(images) > 0 else ""
        for image in images:
            images_yaml += f"- {image}\n"

        yaml_text = f"{text_yaml}\n{images_yaml}"
        text = f"""{yaml_text}
Thought: I now know the answer{IMAGES_SUFIX if len(images) > 0 else "."}
"""
        return Response(content=text, media_type="text/plain")

    except Exception as e:
        logger.opt(exception=True).debug("web_retriever GET failed:")
        error_message = f"Sorry, the url is not available. {e}\nYou should report this message to the user!"
        return JSONResponse(content={"error": error_message}, status_code=500)


@app.get("/icon.png", include_in_schema=False)
async def api_icon():
    return FileResponse(folder_path / "icon.png")


@app.get("/ai-plugin.json", include_in_schema=False)
async def api_ai_plugin(request: Request):
    json_path = folder_path / "ai-plugin.json"
    with json_path.open("r") as f:
        ai_plugin_json = json.load(f)

    base_url, _, _ = request.url._url.rpartition("/")
    ai_plugin_json["logo_url"] = base_url + "/icon.png"
    ai_plugin_json["api"]["url"] = base_url + "/openapi.json"

    return Response(content=json.dumps(ai_plugin_json), media_type="application/json")


def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema