Skip to content

Commit 6361500

Browse files
committed
fix: gpt4free msg truncated
1 parent e1c9f54 commit 6361500

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

free_one_api/impls/adapter/gpt4free.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ async def _select_provider(self):
145145
"role": "user",
146146
"content": "Hi, My name is Rock."
147147
}
148-
]
148+
],
149+
timeout=120
149150
)
150151
async for _ in resp:
151152
pass
@@ -194,7 +195,8 @@ async def query(self, req: request.Request) -> typing.AsyncGenerator[response.Re
194195
else:
195196
resp = provider.create_async_generator(
196197
model=req.model,
197-
messages=req.messages
198+
messages=req.messages,
199+
timeout=120
198200
)
199201

200202
if isinstance(resp, typing.Generator):

free_one_api/impls/app.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,23 @@ class Application:
4343

4444
watchdog: wdmgr.AbsWatchDog
4545

46+
logging_level: int = logging.INFO
47+
4648
def __init__(
4749
self,
4850
dbmgr: db.DatabaseInterface,
4951
router: routermgr.RouterManager,
5052
channel: chanmgr.AbsChannelManager,
5153
key: keymgr.AbsAPIKeyManager,
5254
watchdog: wdmgr.AbsWatchDog,
55+
logging_level: int = logging.INFO,
5356
):
5457
self.dbmgr = dbmgr
5558
self.router = router
5659
self.channel = channel
5760
self.key = key
5861
self.watchdog = watchdog
62+
self.logging_level = logging_level
5963

6064
async def run(self):
6165
"""Run application."""
@@ -124,12 +128,15 @@ async def make_application(config_path: str) -> Application:
124128

125129
if 'DEBUG' in os.environ and os.environ['DEBUG'] == 'true':
126130
logging_level = logging.DEBUG
127-
131+
132+
print("Logging level:", logging_level)
133+
logging.debug("Debug mode enabled.")
134+
128135
terminal_out = logging.StreamHandler()
129136

130137
terminal_out.setLevel(logging_level)
131138
terminal_out.setFormatter(colorlog.ColoredFormatter(
132-
"[%(asctime)s.%(msecs)03d] %(log_color)s%(filename)s (%(lineno)d) - [%(levelname)s] : "
139+
"[%(asctime)s.%(msecs)03d] %(log_color)s%(pathname)s (%(lineno)d) - [%(levelname)s] :\n"
133140
"%(message)s",
134141
datefmt="%Y-%m-%d %H:%M:%S",
135142
log_colors=log_colors_config,
@@ -163,7 +170,8 @@ async def make_application(config_path: str) -> Application:
163170
# database handler
164171
dblogger = log.SQLiteHandler(dbmgr)
165172

166-
dblogger.setLevel(logging_level)
173+
# failed to set debug level for db handler
174+
dblogger.setLevel(logging.INFO if logging_level <= logging.INFO else logging_level)
167175
dblogger.setFormatter(logging.Formatter("[%(asctime)s.%(msecs)03d] %(pathname)s (%(lineno)d) - [%(levelname)s] :\n%(message)s"))
168176

169177
logging.getLogger().addHandler(dblogger)
@@ -233,6 +241,7 @@ async def make_application(config_path: str) -> Application:
233241
channel=channelmgr,
234242
key=apikeymgr,
235243
watchdog=wdmgr,
244+
logging_level=logging_level,
236245
)
237246

238247
logging.info("Application initialized.")

free_one_api/impls/forward/mgr.py

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ async def _gen():
7676
"param": e.param,
7777
}
7878
}))
79+
except Exception as e:
80+
logging.warning("Exception should be processed by adapter but caught by forward manager:")
81+
logging.error(e)
7982

8083
spent_ms = int((time.time() - before)*1000)
8184

free_one_api/models/forward/mgr.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class AbsForwardManager(metaclass=abc.ABCMeta):
2929
"""API key manager."""
3030

3131
@abc.abstractmethod
32-
def query(
32+
async def query(
3333
self,
3434
path: str,
3535
req: request.Request,

main.py

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def main():
1515

1616
application = loop.run_until_complete(app.make_application("./data/config.yaml"))
1717

18+
logging.getLogger().setLevel(application.logging_level)
1819
loop.run_until_complete(application.run())
1920

2021
if __name__ == "__main__":

0 commit comments

Comments
 (0)