From c35b3ab08d282505a7618d76b4400bf3f5086bb1 Mon Sep 17 00:00:00 2001 From: MIDORIBIN Date: Sat, 8 Jul 2023 17:37:54 +0900 Subject: [PATCH] v1 --- .gitignore | 160 ++++++++++++++++++++++++++++++++++++++ README.md | 56 +++++++++++++ langchain_g4f/G4FLLM.py | 49 ++++++++++++ langchain_g4f/__init__.py | 1 + pyproject.toml | 18 +++++ sample.py | 18 +++++ 6 files changed, 302 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 langchain_g4f/G4FLLM.py create mode 100644 langchain_g4f/__init__.py create mode 100644 pyproject.toml create mode 100644 sample.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2dc53ca --- /dev/null +++ b/.gitignore @@ -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/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..453b42a --- /dev/null +++ b/README.md @@ -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. diff --git a/langchain_g4f/G4FLLM.py b/langchain_g4f/G4FLLM.py new file mode 100644 index 0000000..983a577 --- /dev/null +++ b/langchain_g4f/G4FLLM.py @@ -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, + } diff --git a/langchain_g4f/__init__.py b/langchain_g4f/__init__.py new file mode 100644 index 0000000..94015df --- /dev/null +++ b/langchain_g4f/__init__.py @@ -0,0 +1 @@ +from .G4FLLM import G4FLLM diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..4262e37 --- /dev/null +++ b/pyproject.toml @@ -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"] diff --git a/sample.py b/sample.py new file mode 100644 index 0000000..a1a5314 --- /dev/null +++ b/sample.py @@ -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()