返回提交历史
Modified
g4f/Provider/template/OpenaiTemplate.py
+2
-2
Modified
g4f/tools/run_tools.py
+8
-5
XFEstudio/gpt4free
feat: enhance API key handling to include validation for provider-specific keys
5c754a4d
代码差异
2 个文件
+10
-7
@@ -84,8 +84,8 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
84
84
try:
85
85
if api_key is None and cls.api_key is not None:
86
86
api_key = cls.api_key
87
if not api_key or AppConfig.disable_custom_api_key:
88
api_key = AuthManager.load_api_key(cls)
87
if not api_key or AppConfig.disable_custom_api_key or not cls.is_provider_api_key(api_key):
88
api_key = AuthManager.load_api_key(cls) or api_key
89
89
if base_url is None:
90
90
base_url = cls.base_url
91
91
if not cls.is_provider_api_key(api_key):
@@ -19,7 +19,7 @@ except ImportError:
19
19
20
20
from ..typing import Messages
21
21
from ..providers.helper import filter_none
22
from ..providers.asyncio import to_async_iterator, to_sync_generator
22
from ..providers.asyncio import to_sync_generator
23
23
from ..providers.response import Reasoning, FinishReason, Sources, Usage, ProviderInfo
24
24
from ..providers.types import ProviderType
25
25
from ..providers.base_provider import get_async_provider_method, get_provider_method, wait_for
@@ -41,6 +41,9 @@ TOOL_NAMES = {
41
41
"BUCKET": "bucket_tool",
42
42
}
43
43
44
def is_provider_api_key(api_key: str) -> bool:
45
return api_key and not api_key.startswith("g4f_") and not api_key.startswith("gfs_")
46
44
47
45
48
class ToolHandler:
46
49
"""Handles processing of different tool types"""
@@ -284,8 +287,8 @@ async def async_iter_run_tools(
284
287
messages, sources = await perform_web_search(messages, web_search)
285
288
286
289
# Get API key
287
if not kwargs.get("api_key") or AppConfig.disable_custom_api_key:
288
api_key = AuthManager.load_api_key(provider)
290
if not kwargs.get("api_key") or AppConfig.disable_custom_api_key or not is_provider_api_key(kwargs.get("api_key")):
291
api_key = AuthManager.load_api_key(provider) or kwargs.get("api_key")
289
292
if api_key:
290
293
kwargs["api_key"] = api_key
291
294
@@ -416,8 +419,8 @@ def iter_run_tools(
416
419
debug.error(f"Couldn't do web search:", e)
417
420
418
421
# Get API key if needed
419
if not kwargs.get("api_key") or AppConfig.disable_custom_api_key:
420
api_key = AuthManager.load_api_key(provider)
422
if not kwargs.get("api_key") or AppConfig.disable_custom_api_key or not is_provider_api_key(kwargs.get("api_key")):
423
api_key = AuthManager.load_api_key(provider) or kwargs.get("api_key")
421
424
if api_key:
422
425
kwargs["api_key"] = api_key
423
426