4
4
from pathlib import Path
5
5
from types import FunctionType
6
6
7
+ from .utils .check import is_fake_httpx , is_fake_openai
7
8
from .utils .stack_switching import stack_switching_supported
8
9
9
10
10
11
def patch_promplate ():
11
12
import promplate
12
13
13
- class Loader ( promplate . template . Loader ):
14
- """making HTTP requests in pyodide runtime"""
14
+ if is_fake_httpx ( ):
15
+ from promplate . prompt . template import Loader
15
16
16
17
@classmethod
18
+ @wraps (Loader .afetch )
17
19
async def afetch (cls , url : str , ** kwargs ):
18
20
from pyodide .http import pyfetch
19
21
@@ -24,6 +26,7 @@ async def afetch(cls, url: str, **kwargs):
24
26
return obj
25
27
26
28
@classmethod
29
+ @wraps (Loader .fetch )
27
30
def fetch (cls , url : str , ** kwargs ):
28
31
if stack_switching_supported ():
29
32
from pyodide .ffi import run_sync # type: ignore
@@ -39,15 +42,11 @@ def fetch(cls, url: str, **kwargs):
39
42
40
43
return obj
41
44
42
- class Node ( Loader , promplate . Node ):
43
- """patched for making HTTP requests in pyodide runtime"""
45
+ Loader . afetch = afetch # type: ignore
46
+ Loader . fetch = fetch # type: ignore
44
47
45
- class Template (Loader , promplate .Template ):
46
- """patched for making HTTP requests in pyodide runtime"""
47
-
48
- promplate .template .Loader = Loader
49
- promplate .template .Template = promplate .Template = Template
50
- promplate .node .Node = promplate .Node = Node
48
+ if not is_fake_openai ():
49
+ return
51
50
52
51
patch_class (promplate .node .Interruptable )
53
52
@@ -87,12 +86,20 @@ async def patch_openai(fallback_import_url: str = "https://esm.sh/openai"):
87
86
88
87
await ensure_openai (fallback_import_url )
89
88
89
+ if not is_fake_openai ():
90
+ return
91
+
90
92
import openai
91
93
92
94
openai .Client = openai .AsyncClient = translate_openai ()
93
95
94
96
95
97
def patch_httpx ():
98
+ with suppress (ModuleNotFoundError ):
99
+ import httpx # noqa: F401
100
+
101
+ return
102
+
96
103
from pyodide .code import run_js
97
104
from pyodide .ffi import register_js_module
98
105
0 commit comments