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 diff --git a/main.py b/main.py index 941a78f..a979a48 100644 --- a/main.py +++ b/main.py @@ -35,20 +35,23 @@ 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 = [] 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})