TODAM-tw / todam-ticket-system

The frontend with gradio and combined with the API endpoints for the ticket system.
Apache License 2.0
0 stars 0 forks source link

[Enhancement] Status Code Handler #44

Closed ShiuanTHC closed 1 month ago

ShiuanTHC commented 1 month ago

https://github.com/TODAM-tw/todam-ticket-system/blob/a136a489bf37f4ee755cb2ca281a7d418c95c401/app/cases/ticket_summarized.py#L55-L59

Status Code == 400 -> input 格式有誤

# error_message = "User input is empty or invalid."
 if not user_input:
            print("[Error]:", error_message)    # Monitor in CloudWatch
            return {
                'statusCode': 400,
                'body': json.dumps({"error": error_message})
            }

Status Code == 500 -> 佔位符數量和儲存網址的 queue 數量不同 (進 model 前)

# error_message = f"Number of placeholders ({placeholders_count}) does not match the number of URLs ({urls_count})."  
user_input_str, url_queue, error_message = replace_urls_with_placeholders(user_input_str)
        if error_message:
            return {
                'statusCode': 500,
                'body': json.dumps({"error": error_message})
            }

Status Code == 500 -> 讀不到 prompt file

# error_message = "prompt_file not found."
system_prompt = get_system_prompt()
        if "[Error]:" in system_prompt:
            return {
                'statusCode': 500,
                'body': json.dumps(system_prompt)
            }

Status Code == 500 -> 儲存網址的 queue 的數量不夠 replace 佔位符數量 (出 model 後)

# error_message = "Ran out of URLs in the queue before replacing all placeholders."
for _ in range(modified_text.count('[URL]')):
            if not url_queue:
                error_message = "Ran out of URLs in the queue before replacing all placeholders."
                raise ValueError(error_message)

Status Code == 500 -> other error

    except Exception as e:
        error_message = str(e)
        print("[Error]:", error_message)    # Monitor in CloudWatch
        return {
            'statusCode': 500,
           'body': json.dumps({"error": error_message})
       }
1chooo commented 1 month ago

@ShiuanTHC 這樣是你要的效果嗎?

Screenshot 2024-05-28 at 2 40 49 PM

if status_code == 500 我直接把 API 拿到的 Response 顯示回畫面上!

# output format: preview_subject_title, preview_summerized_ticket_content, summerized_ticket_content, token_usage, token_cost

if response.status_code == 200:
    data: dict = json.loads(response.text)
elif response.status_code == 400:
    response.text
    return (
        f"<h1>Error: {response.text}</h1>", 
        "Maybe User input is empty or invalid.", 
        "Maybe User input is empty or invalid.",
        "💰 Token Cost: NULL", "🔒 Token Usage: NULL"
    )
elif response.status_code == 500:
    response.text
    return (
        f"<h1>Error: {response.text}</h1>", 
        "The number of placeholders does not match the number of URLs. or prompt_file not found or Ran out of URLs in the queue before replacing all placeholders. or others", 
        "The number of placeholders does not match the number of URLs. or prompt_file not found or Ran out of URLs in the queue before replacing all placeholders. or others", 
        "💰 Token Cost: NULL", "🔒 Token Usage: NULL"
    )
else:
    return (
        "<h1>Error: Something went wrong with the API</h1>", 
        "We meet error when we call the API", 
        "We meet error when we call the API", 
        "💰 Token Cost: NULL", "🔒 Token Usage: NULL"
    )
ShiuanTHC commented 1 month ago

因為我 body 分別會 return 不同的 error message,你可以直接讀 body 裡的內容並顯示出來嗎? 還有我記得原生的 error 好像會更明顯,會有彈窗效果,這部分可能要明天面對面討論比較清除,我預期的效果是有個彈窗效果顯示 error message 。

1chooo commented 1 month ago

@ShiuanTHC 原來,原本的彈窗效果是因為我的 Code return 的數量跟預期的不一樣,我可以研究一下彈窗效果,畫面上的內容我也可以直接顯示 Error 訊息就好!那我就顯示在 Subject 的欄位,另外彈窗效果要再研究一下!

ShiuanTHC commented 1 month ago

好,感謝! 麻煩你了,我要去考按摩了哈哈哈

1chooo commented 1 month ago

OK!

1chooo commented 1 month ago

@ShiuanTHC 我看官方文件好像這樣加就好了!不過我這邊不知道要怎麼觸發 Error。


if response.status_code == 200:
    data: dict = json.loads(response.text)
elif response.status_code == 400:
    raise gr.Error(f"Error: {response.text}")
elif response.status_code == 500:
    raise gr.Error(f"Error: {response.text}")
else:
    raise gr.Error(f"Error: {response.text}")```
ShiuanTHC commented 1 month ago

好~我看 commit 好像現在還沒有上到 dev 分支 那我明天再去試試效果

1chooo commented 1 month ago

因為我還沒 commit 明天一起看成果,同時需要一起做一些假的 Data 測試!