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

🐛 Bug Report: Python Prompt-chatgpt template #297

Open
2 tasks done
sonicviz opened this issue Jul 5, 2024 · 3 comments
Open
2 tasks done

🐛 Bug Report: Python Prompt-chatgpt template #297

sonicviz opened this issue Jul 5, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@sonicviz
Copy link

sonicviz commented Jul 5, 2024

👟 Reproduction steps

https://github.com/appwrite/templates/tree/main/python/prompt_chatgpt
Fails to run from the included static HTML test interface.
Always returns "Failed to query model."

👍 Expected behavior

It should work?

👎 Actual Behavior

Always returns "Failed to query model."
No errors as there was no logging.

So I debugged it according to the latest OpenAI library https://github.com/openai/openai-python/tree/main
It appears the OpenAI calls have changed.

Debugged and working version is below, with logging. Suggest including sample logging in any templates to make it easier to debug and also helps users not so familiar with python upskill faster.
File: src\main.py

import os
from openai import OpenAI
from .utils import get_static_file, throw_if_missing


def main(context):
    throw_if_missing(os.environ, ["OPENAI_API_KEY"])

    if context.req.method == "GET":
        return context.res.send(
            get_static_file("index.html"),
            200,
            {
                "content-type": "text/html; charset=utf-8"
            },
        )

    context.log(context.req.body)

    try:
        throw_if_missing(context.req.body, ["prompt"])
    except ValueError as err:
        return context.res.json({"ok": False, "error": err.message}, 400)

    # openai.api_key = os.environ["OPENAI_API_KEY"]

    client = OpenAI(
    # This is the default and can be omitted
    api_key=os.environ.get("OPENAI_API_KEY"),
    )       

    context.log("OPENAI_API_KEY" + os.environ["OPENAI_API_KEY"])
    context.log("OPENAI_MAX_TOKENS" + os.environ["OPENAI_MAX_TOKENS"])

    try:
        # response = openai.ChatCompletion.create(
        #     model="gpt-3.5-turbo",
        #     max_tokens=int(os.environ.get("OPENAI_MAX_TOKENS", "512")),
        #     messages=[{"role": "user", "content": context.req.body["prompt"]}],
        # )
        response = client.chat.completions.create(
            model="gpt-4o",
            max_tokens=int(os.environ.get("OPENAI_MAX_TOKENS", "512")),
            messages=[{"role": "user", "content": context.req.body["prompt"]}],
        )
        context.log(response)
        completion = response.choices[0].message.content
        return context.res.json({"ok": True, "completion": completion}, 200)

    except Exception:
        return context.res.json({"ok": False, "error": "Failed to query model."}, 500)

🎲 Appwrite version

Appwrite Cloud

💻 Operating system

Windows

🧱 Your Environment

No response

👀 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

🏢 Have you read the Code of Conduct?

@sonicviz sonicviz added the bug Something isn't working label Jul 5, 2024
@loks0n
Copy link
Member

loks0n commented Jul 5, 2024

Thanks for the report! 🙏

Feel free to pick this up and open a PR if you are interested.

@stnguyen90 stnguyen90 changed the title Python Prompt-chatgpt template🐛 Bug Report: 🐛 Bug Report: Python Prompt-chatgpt template Nov 21, 2024
@ChiragAgg5k
Copy link

since @sonicviz isnt working on this,
i can work on this issue, can it be assigned to me?

@sonicviz
Copy link
Author

sonicviz commented Dec 14, 2024

Yeah, sorry, too busy on other projects atm.
All the details are there though. Suerte!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants