0
0
mirror of https://github.com/MIDORIBIN/langchain-gpt4free.git synced 2024-12-24 11:34:39 +03:00
This commit is contained in:
MIDORIBIN 2023-07-08 17:37:54 +09:00
parent 081c764cf9
commit c35b3ab08d
6 changed files with 302 additions and 0 deletions

160
.gitignore vendored Normal file
View File

@ -0,0 +1,160 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/

56
README.md Normal file
View File

@ -0,0 +1,56 @@
# LangChain gpt4free
LangChain gpt4free is an open-source project that assists in building applications using LLM (Large Language Models) and provides free access to GPT4/3.5.
## Installation
To install langchain_g4f, run the following command:
```shell
pip install git+https://github.com/MIDORIBIN/langchain-gpt4free.git
```
This command will install langchain_g4f.
## Usage
Here is an example of how to use langchain_g4f
```python
from g4f import Provider, Model
from langchain.llms.base import LLM
from langchain_g4f import G4FLLM
def main():
llm: LLM = G4FLLM(
model=Model.gpt_35_turbo,
provider=Provider.Aichat,
)
res = llm('hello')
print(res) # Hello! How can I assist you today?
if __name__ == '__main__':
main()
```
The above sample code demonstrates the basic usage of langchain_g4f. Choose the appropriate model and provider, initialize the LLM, and then pass input text to the LLM object to obtain the result.
## Support and Bug Reports
For support and bug reports, please use the GitHub Issues page.
Access the GitHub Issues page and create a new issue. Select the appropriate label and provide detailed information.
## Contributing
To contribute to langchain_g4f, follow these steps to submit a pull request:
1. Fork the project repository.
2. Clone the forked repository to your local machine.
3. Make the necessary changes.
4. Commit the changes and push to your forked repository.
5. Create a pull request towards the original project repository.

49
langchain_g4f/G4FLLM.py Normal file
View File

@ -0,0 +1,49 @@
from types import ModuleType
from typing import Optional, List, Any, Mapping, Union
import g4f
from langchain.callbacks.manager import CallbackManagerForLLMRun
from langchain.llms.base import LLM
from langchain.llms.utils import enforce_stop_tokens
class G4FLLM(LLM):
# Model.model or str
model: Union[type, str]
# Provider.Provider
provider: Optional[ModuleType] = None
auth: Optional[Union[str, bool]] = None
create_kwargs: Optional[dict] = None
@property
def _llm_type(self) -> str:
return 'custom'
def _call(self, prompt: str, stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None, **kwargs: Any) -> str:
create_kwargs = {} if self.create_kwargs is None else self.create_kwargs.copy()
if self.model is not None:
create_kwargs['model'] = self.model
if self.provider is not None:
create_kwargs['provider'] = self.provider
if self.auth is not None:
create_kwargs['auth'] = self.auth
text = g4f.ChatCompletion.create(
messages=[{'role': 'user', 'content': prompt}],
**create_kwargs,
)
if stop is not None:
text = enforce_stop_tokens(text, stop)
return text
@property
def _identifying_params(self) -> Mapping[str, Any]:
"""Get the identifying parameters."""
return {
'model': self.model,
'provider': self.provider,
'auth': self.auth,
'create_kwargs': self.create_kwargs,
}

View File

@ -0,0 +1 @@
from .G4FLLM import G4FLLM

18
pyproject.toml Normal file
View File

@ -0,0 +1,18 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "langchain_g4f"
version = "1.0.0"
description = ""
authors = []
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"langchain>=0.0.225",
"gpt4free @ git+https://github.com/xtekky/gpt4free.git",
]
[tool.setuptools.packages.find]
include = ["langchain_g4f"]

18
sample.py Normal file
View File

@ -0,0 +1,18 @@
from g4f import Provider, Model
from langchain.llms.base import LLM
from langchain_g4f import G4FLLM
def main():
llm: LLM = G4FLLM(
model=Model.gpt_35_turbo,
provider=Provider.Aichat,
)
res = llm('hello')
print(res) # Hello! How can I assist you today?
if __name__ == '__main__':
main()