From fad8ddf26c12c72e400dc9fc622e4c16db585991 Mon Sep 17 00:00:00 2001 From: Ranga Dananjaya Date: Sun, 8 Jun 2025 20:30:42 +0530 Subject: [PATCH 1/3] Fix context handling in ask_ai function to ensure proper formatting --- main.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 941a78f..54eb721 100644 --- a/main.py +++ b/main.py @@ -48,7 +48,10 @@ def ask_ai(data: InputData): history = [] if data.context: - joined_context = "\n\n".join(data.context) + if isinstance(data.context, list): + joined_context = "\n\n".join(data.context) + else: + joined_context = str(data.context) payload = json.dumps({"query": data.query, "context": joined_context, "history": history}) else: payload = json.dumps({"query": data.query, "history": history}) From 8236cd2b38703506b7c1554851d88274aa7c7256 Mon Sep 17 00:00:00 2001 From: Ranga Dananjaya Date: Sun, 8 Jun 2025 20:36:54 +0530 Subject: [PATCH 2/3] Handle the "too long history" issue --- main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 54eb721..a979a48 100644 --- a/main.py +++ b/main.py @@ -35,15 +35,15 @@ def run_script(): @app.post("/ask-ai") def ask_ai(data: InputData): try: - docs = list(users_collection.find({"user_id": data.user_ID})) + docs = list(users_collection.find({"user_id": data.user_ID}).sort("_id", -1).limit(5)) if docs: history = [] - for doc in docs: + for doc in docs[::-1]: history.append({ "user_prompt": doc.get("user_promt", ""), "AI": doc.get("AI", "") }) - print("History loaded from MongoDB:", history) + print("Last five history loaded from MongoDB:", history) else: history = [] From 939c9b50344c6c8a4b38fc6dc4442c14b1eeb9ab Mon Sep 17 00:00:00 2001 From: Ranga Dananjaya Date: Sun, 8 Jun 2025 20:38:07 +0530 Subject: [PATCH 3/3] Add example curl command in README --- README | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README b/README index c6879d9..2b0b7fe 100644 --- a/README +++ b/README @@ -5,3 +5,5 @@ docker run -d -p 8000:8000 -e GEMINI_API_KEY=api_key_here curl -X POST "http://:8000/ask-ai" -H "Content-Type: application/json" -d '{\"query\": \"\"}' curl -X POST "http://:8000/ask-ai" -H "Content-Type: application/json" -d "{\"query\": \"\"}" + +curl -X POST "http://127.0.0.1:8000/ask-ai" -H "Content-Type: application/json" -d "{\"query\": \"what is SnapDragon?\", \"user_ID\": \"0001\", \"context\": [\"nothing\"]}" \ No newline at end of file