mirror of
https://github.com/MIDORIBIN/langchain-gpt4free.git
synced 2024-12-23 11:22:57 +03:00
Merge pull request #8 from AntonioSabbatellaUni/main
Implemented the retry mechanism for the G4FLLM.py script (5 times).
This commit is contained in:
commit
651e8edb7c
1
.gitignore
vendored
1
.gitignore
vendored
@ -159,3 +159,4 @@ cython_debug/
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
.idea/
|
||||
.vscode/
|
||||
test.py
|
||||
|
@ -8,7 +8,7 @@ from langchain.callbacks.manager import CallbackManagerForLLMRun, AsyncCallbackM
|
||||
from langchain.llms.base import LLM
|
||||
from langchain.llms.utils import enforce_stop_tokens
|
||||
|
||||
|
||||
MAX_TRIES = 5
|
||||
class G4FLLM(LLM):
|
||||
model: Union[Model, str]
|
||||
provider: Optional[type[BaseProvider]] = None
|
||||
@ -33,16 +33,25 @@ class G4FLLM(LLM):
|
||||
if self.auth is not None:
|
||||
create_kwargs["auth"] = self.auth
|
||||
|
||||
text = ChatCompletion.create(
|
||||
messages=[{"role": "user", "content": prompt}],
|
||||
**create_kwargs,
|
||||
)
|
||||
for i in range(MAX_TRIES):
|
||||
try:
|
||||
text = ChatCompletion.create(
|
||||
messages=[{"role": "user", "content": prompt}],
|
||||
**create_kwargs,
|
||||
)
|
||||
|
||||
# Generator -> str
|
||||
text = text if type(text) is str else "".join(text)
|
||||
if stop is not None:
|
||||
text = enforce_stop_tokens(text, stop)
|
||||
if text:
|
||||
return text
|
||||
print(f"Empty response, trying {i+1} of {MAX_TRIES}")
|
||||
except Exception as e:
|
||||
print(f"Error in G4FLLM._call: {e}, trying {i+1} of {MAX_TRIES}")
|
||||
return ""
|
||||
|
||||
|
||||
# Generator -> str
|
||||
text = text if type(text) is str else "".join(text)
|
||||
if stop is not None:
|
||||
text = enforce_stop_tokens(text, stop)
|
||||
return text
|
||||
async def _acall(self, prompt: str, stop: Optional[List[str]] = None, run_manager: Optional[AsyncCallbackManagerForLLMRun] = None, **kwargs: Any) -> str:
|
||||
create_kwargs = {} if self.create_kwargs is None else self.create_kwargs.copy()
|
||||
create_kwargs["model"] = self.model
|
||||
|
Loading…
Reference in New Issue
Block a user