Skip to content

Commit 77accf1

Browse files
authored
Merge pull request #1551 from mito-ds/http_client_timeout
mito-ai: update http request timeout
2 parents 91e4cfe + 8b8e726 commit 77accf1

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

mito-ai/mito_ai/utils/open_ai_utils.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import json
77
from typing import Any, Dict, List, Optional, Type, Final, Union
88
from datetime import datetime, timedelta
9+
import os
910

11+
from mito_ai.utils.utils import is_running_test
1012
from pydantic import BaseModel
1113
from tornado.httpclient import AsyncHTTPClient
1214
from mito_ai.models import MessageType
@@ -107,8 +109,20 @@ async def get_ai_completion_from_mito_server(
107109
headers = {
108110
"Content-Type": "application/json",
109111
}
110-
111-
http_client = AsyncHTTPClient(defaults=dict(user_agent="Mito-AI client"))
112+
113+
http_client = None
114+
if is_running_test():
115+
# If we are running in a test environment, setting the request_timeout fails for some reason.
116+
http_client = AsyncHTTPClient(defaults=dict(user_agent="Mito-AI client"))
117+
else:
118+
119+
# The HTTP client timesout after 20 seconds by default. We update this to match the timeout
120+
# we give to OpenAI. The OpenAI timeouts are denoted in seconds, wherease the HTTP client
121+
# expects milliseconds. We also give the HTTP client a 10 second buffer to account for
122+
# the time it takes to send the request, etc.
123+
http_client_timeout = timeout * 1000 * max_retries + 10000
124+
http_client = AsyncHTTPClient(defaults=dict(user_agent="Mito-AI client"), request_timeout=http_client_timeout)
125+
112126
try:
113127
res = await http_client.fetch(
114128
# Important: DO NOT CHANGE MITO_AI_URL. If you want to use the dev endpoint,

0 commit comments

Comments
 (0)