ka1kuk commited on
Commit
0cfb5e2
1 Parent(s): c1eeef0

Create lang/G4F.py

Browse files
Files changed (1) hide show
  1. lang/G4F.py +53 -0
lang/G4F.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from types import ModuleType
2
+ from typing import Optional, List, Any, Mapping, Union
3
+
4
+ import g4f
5
+ from langchain.callbacks.manager import CallbackManagerForLLMRun
6
+ from langchain.llms.base import LLM
7
+ from langchain.llms.utils import enforce_stop_tokens
8
+
9
+
10
+ class G4F(LLM):
11
+ # Model.model or str
12
+ model: Union[type, str]
13
+ # Provider.Provider
14
+ provider: Optional[ModuleType] = None
15
+ auth: Optional[Union[str, bool]] = None
16
+ create_kwargs: Optional[dict] = None
17
+
18
+ @property
19
+ def _llm_type(self) -> str:
20
+ return "custom"
21
+
22
+ def _call(
23
+ self,
24
+ prompt: str,
25
+ stop: Optional[List[str]] = None,
26
+ run_manager: Optional[CallbackManagerForLLMRun] = None,
27
+ **kwargs: Any,
28
+ ) -> str:
29
+ create_kwargs = {} if self.create_kwargs is None else self.create_kwargs.copy()
30
+ if self.model is not None:
31
+ create_kwargs["model"] = self.model
32
+ if self.provider is not None:
33
+ create_kwargs["provider"] = self.provider
34
+ if self.auth is not None:
35
+ create_kwargs["auth"] = self.auth
36
+
37
+ text = g4f.ChatCompletion.create(
38
+ messages=[{"role": "user", "content": prompt}],
39
+ **create_kwargs,
40
+ )
41
+ if stop is not None:
42
+ text = enforce_stop_tokens(text, stop)
43
+ return text
44
+
45
+ @property
46
+ def _identifying_params(self) -> Mapping[str, Any]:
47
+ """Get the identifying parameters."""
48
+ return {
49
+ "model": self.model,
50
+ "provider": self.provider,
51
+ "auth": self.auth,
52
+ "create_kwargs": self.create_kwargs,
53
+ }