Skip to content

Commit 34bae5d

Browse files
committed
Call Khoj chat API as POST request with payload in request body
The chat API endpoint on the Khoj server was updated into a POST API endpoint accepting most query parameters (e.g `q') in request body This change updates the call the modified Khoj chat API to use this new calling pattern
1 parent 13bff8e commit 34bae5d

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/flint/helpers.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
logger = logging.getLogger(__name__)
2121

22-
KHOJ_CHAT_API_ENDPOINT = f"{KHOJ_API_URL}/api/chat?client_id={KHOJ_API_CLIENT_ID}"
22+
KHOJ_CHAT_API_ENDPOINT = f"{KHOJ_API_URL}/api/chat?client_id={KHOJ_API_CLIENT_ID}&client=whatsapp"
2323
KHOJ_INDEX_API_ENDPOINT = f"{KHOJ_API_URL}/api/v1/index/update?client_id={KHOJ_API_CLIENT_ID}&client=whatsapp"
2424

2525
KHOJ_CLOUD_API_SESSION = Session()
@@ -126,7 +126,6 @@ def send_message_to_khoj_chat(user_message: str, user_number: str) -> str:
126126
Send the user message to the backend LLM service and return the response
127127
"""
128128
start_time = time.time()
129-
encoded_phone_number = urllib.parse.quote(user_number)
130129

131130
if user_message.startswith(tuple(UNIMPLEMENTED_COMMANDS.keys())):
132131
return {
@@ -142,10 +141,14 @@ def send_message_to_khoj_chat(user_message: str, user_number: str) -> str:
142141
else:
143142
user_message = f"/default {user_message}"
144143

145-
encoded_user_message = urllib.parse.quote(user_message)
146-
147-
khoj_api = f"{KHOJ_CHAT_API_ENDPOINT}&phone_number={encoded_phone_number}&q={encoded_user_message}&stream=false&create_if_not_exists=true"
148-
response = KHOJ_CLOUD_API_SESSION.get(khoj_api)
144+
encoded_phone_number = urllib.parse.quote(user_number)
145+
khoj_api = f"{KHOJ_CHAT_API_ENDPOINT}&phone_number={encoded_phone_number}&create_if_not_exists=true"
146+
response = KHOJ_CLOUD_API_SESSION.post(
147+
khoj_api,
148+
json = {
149+
"q": user_message,
150+
"stream": False,
151+
})
149152

150153
end_time = time.time()
151154
response_time = end_time - start_time
@@ -173,7 +176,7 @@ def send_message_to_khoj_chat(user_message: str, user_number: str) -> str:
173176
)
174177
return {"response": "Sorry, I'm having trouble understanding you. Could you please try again?"}
175178
except Exception as e:
176-
logger.exception("An unexpected error occurred while processing the response from Khoj.")
179+
logger.exception(f"An unexpected error occurred while processing the response from Khoj.\nError: {e}")
177180
return {"response": "I encountered an unexpected issue. Could you please try again?"}
178181

179182

0 commit comments

Comments
 (0)